2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2012-01-01 15:29:55 +01:00
* Copyright ( C ) 2010 - 2012 David Rosca < nowrep @ gmail . com >
2011-03-03 18:29:20 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
2011-03-02 16:57:41 +01:00
# include "siteinfo.h"
# include "ui_siteinfo.h"
# include "qupzilla.h"
# include "webview.h"
2011-03-22 21:36:15 +01:00
# include "webpage.h"
2011-03-23 22:36:03 +01:00
# include "downloaditem.h"
2011-10-09 14:51:25 +02:00
# include "certificateinfowidget.h"
2011-11-05 11:51:46 +01:00
# include "globalfunctions.h"
# include "iconprovider.h"
2011-03-02 16:57:41 +01:00
2011-04-26 19:47:12 +02:00
QString SiteInfo : : showCertInfo ( const QString & string )
{
2011-11-06 17:01:23 +01:00
if ( string . isEmpty ( ) ) {
2011-04-26 19:47:12 +02:00
return tr ( " <not set in certificate> " ) ;
2011-11-06 17:01:23 +01:00
}
else {
return string ;
}
2011-04-26 19:47:12 +02:00
}
2011-10-09 14:51:25 +02:00
SiteInfo : : SiteInfo ( QupZilla * mainClass , QWidget * parent )
: QDialog ( parent )
, ui ( new Ui : : SiteInfo )
, p_QupZilla ( mainClass )
, m_certWidget ( 0 )
2011-03-02 16:57:41 +01:00
{
ui - > setupUi ( this ) ;
2011-11-19 18:21:22 +01:00
ui - > listWidget - > item ( 0 ) - > setIcon ( QIcon : : fromTheme ( " document-properties " , QIcon ( " :/icons/preferences/document-properties.png " ) ) ) ;
ui - > listWidget - > item ( 1 ) - > setIcon ( QIcon : : fromTheme ( " applications-graphics " , QIcon ( " :/icons/preferences/applications-graphics.png " ) ) ) ;
ui - > listWidget - > item ( 2 ) - > setIcon ( QIcon : : fromTheme ( " dialog-password " , QIcon ( " :/icons/preferences/dialog-password.png " ) ) ) ;
2011-12-03 14:43:13 +01:00
ui - > listWidget - > item ( 0 ) - > setSelected ( true ) ;
2011-11-19 18:21:22 +01:00
2011-03-02 16:57:41 +01:00
WebView * view = p_QupZilla - > weView ( ) ;
QWebFrame * frame = view - > page ( ) - > mainFrame ( ) ;
QString title = view - > title ( ) ;
2011-03-22 21:36:15 +01:00
QSslCertificate cert = view - > webPage ( ) - > sslCertificate ( ) ;
2011-12-03 14:43:13 +01:00
m_baseUrl = view - > url ( ) ;
2011-03-02 16:57:41 +01:00
2011-03-22 21:36:15 +01:00
//GENERAL
ui - > heading - > setText ( QString ( " <b>%1</b>: " ) . arg ( title ) ) ;
2011-03-02 16:57:41 +01:00
ui - > siteAddress - > setText ( frame - > baseUrl ( ) . toString ( ) ) ;
2011-11-06 17:01:23 +01:00
ui - > sizeLabel - > setText ( DownloadItem : : fileSizeToString ( view - > webPage ( ) - > totalBytes ( ) ) ) ;
2011-03-23 22:36:03 +01:00
QString encoding ;
2011-03-22 21:36:15 +01:00
//Meta
2011-03-02 16:57:41 +01:00
QWebElementCollection meta = frame - > findAllElements ( " meta " ) ;
2011-11-06 17:01:23 +01:00
for ( int i = 0 ; i < meta . count ( ) ; i + + ) {
2011-03-02 16:57:41 +01:00
QWebElement element = meta . at ( i ) ;
QString content = element . attribute ( " content " ) ;
QString name = element . attribute ( " name " ) ;
2011-11-06 17:01:23 +01:00
if ( name . isEmpty ( ) ) {
2011-03-02 16:57:41 +01:00
name = element . attribute ( " http-equiv " ) ;
2011-11-06 17:01:23 +01:00
}
if ( ! element . attribute ( " charset " ) . isEmpty ( ) ) {
2011-03-23 22:36:03 +01:00
encoding = element . attribute ( " charset " ) ;
2011-11-06 17:01:23 +01:00
}
if ( content . contains ( " charset= " ) ) {
encoding = content . mid ( content . indexOf ( " charset= " ) + 8 ) ;
}
2011-03-02 16:57:41 +01:00
2011-11-06 17:01:23 +01:00
if ( content . isEmpty ( ) | | name . isEmpty ( ) ) {
2011-03-02 16:57:41 +01:00
continue ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
QTreeWidgetItem * item = new QTreeWidgetItem ( ui - > treeTags ) ;
item - > setText ( 0 , name ) ;
item - > setText ( 1 , content ) ;
ui - > treeTags - > addTopLevelItem ( item ) ;
}
2011-11-06 17:01:23 +01:00
if ( encoding . isEmpty ( ) ) {
2011-03-23 22:36:03 +01:00
encoding = mApp - > webSettings ( ) - > defaultTextEncoding ( ) ;
2011-11-06 17:01:23 +01:00
}
2011-03-23 22:36:03 +01:00
ui - > encodingLabel - > setText ( encoding . toUpper ( ) ) ;
2011-03-02 16:57:41 +01:00
2011-03-22 21:36:15 +01:00
//MEDIA
2011-03-02 16:57:41 +01:00
QWebElementCollection img = frame - > findAllElements ( " img " ) ;
2011-11-06 17:01:23 +01:00
for ( int i = 0 ; i < img . count ( ) ; i + + ) {
2011-03-02 16:57:41 +01:00
QWebElement element = img . at ( i ) ;
QString src = element . attribute ( " src " ) ;
QString alt = element . attribute ( " alt " ) ;
if ( alt . isEmpty ( ) ) {
2011-11-06 17:01:23 +01:00
if ( src . indexOf ( " / " ) = = - 1 ) {
2011-03-02 16:57:41 +01:00
alt = src ;
2011-11-06 17:01:23 +01:00
}
2011-03-24 22:31:38 +01:00
else {
2011-03-02 16:57:41 +01:00
int pos = src . lastIndexOf ( " / " ) ;
alt = src . mid ( pos ) ;
alt . remove ( " / " ) ;
}
}
2011-11-06 17:01:23 +01:00
if ( src . isEmpty ( ) | | alt . isEmpty ( ) ) {
2011-03-02 16:57:41 +01:00
continue ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
QTreeWidgetItem * item = new QTreeWidgetItem ( ui - > treeImages ) ;
item - > setText ( 0 , alt ) ;
item - > setText ( 1 , src ) ;
ui - > treeImages - > addTopLevelItem ( item ) ;
}
2011-03-22 21:36:15 +01:00
//SECURITY
if ( cert . isValid ( ) ) {
2011-03-23 22:36:03 +01:00
ui - > securityLabel - > setText ( tr ( " <b>Connection is Encrypted.</b> " ) ) ;
ui - > certLabel - > setText ( tr ( " <b>Your connection to this page is secured with this certificate: </b> " ) ) ;
2011-10-09 14:51:25 +02:00
m_certWidget = new CertificateInfoWidget ( cert ) ;
ui - > certFrame - > addWidget ( m_certWidget ) ;
2011-11-06 17:01:23 +01:00
}
else {
2011-03-23 22:36:03 +01:00
ui - > securityLabel - > setText ( tr ( " <b>Connection Not Encrypted.</b> " ) ) ;
2011-03-22 21:36:15 +01:00
ui - > certLabel - > setText ( tr ( " <b>Your connection to this page is not secured!</b> " ) ) ;
}
2011-11-06 17:01:23 +01:00
connect ( ui - > listWidget , SIGNAL ( currentItemChanged ( QListWidgetItem * , QListWidgetItem * ) ) , this , SLOT ( itemChanged ( QListWidgetItem * ) ) ) ;
2011-03-23 22:36:03 +01:00
connect ( ui - > secDetailsButton , SIGNAL ( clicked ( ) ) , this , SLOT ( securityDetailsClicked ( ) ) ) ;
2011-11-06 17:01:23 +01:00
connect ( ui - > treeImages , SIGNAL ( currentItemChanged ( QTreeWidgetItem * , QTreeWidgetItem * ) ) , this , SLOT ( showImagePreview ( QTreeWidgetItem * ) ) ) ;
2011-03-24 22:31:38 +01:00
ui - > treeImages - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
2011-11-06 17:01:23 +01:00
connect ( ui - > treeImages , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( imagesCustomContextMenuRequested ( const QPoint & ) ) ) ;
2011-03-24 22:31:38 +01:00
}
2011-11-06 17:01:23 +01:00
void SiteInfo : : imagesCustomContextMenuRequested ( const QPoint & p )
2011-03-24 22:31:38 +01:00
{
QTreeWidgetItem * item = ui - > treeImages - > itemAt ( p ) ;
2011-11-06 17:01:23 +01:00
if ( ! item ) {
2011-03-24 22:31:38 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-03-24 22:31:38 +01:00
QMenu menu ;
menu . addAction ( QIcon : : fromTheme ( " edit-copy " ) , tr ( " Copy Image Location " ) , this , SLOT ( copyActionData ( ) ) ) - > setData ( item - > text ( 1 ) ) ;
menu . addAction ( tr ( " Copy Image Name " ) , this , SLOT ( copyActionData ( ) ) ) - > setData ( item - > text ( 0 ) ) ;
menu . addSeparator ( ) ;
menu . addAction ( QIcon : : fromTheme ( " document-save " ) , tr ( " Save Image to Disk " ) , this , SLOT ( downloadImage ( ) ) ) - > setData ( ui - > treeImages - > indexOfTopLevelItem ( item ) ) ;
menu . exec ( QCursor : : pos ( ) ) ;
}
void SiteInfo : : copyActionData ( )
{
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
qApp - > clipboard ( ) - > setText ( action - > data ( ) . toString ( ) ) ;
}
}
void SiteInfo : : downloadImage ( )
{
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
QTreeWidgetItem * item = ui - > treeImages - > topLevelItem ( action - > data ( ) . toInt ( ) ) ;
2011-11-06 17:01:23 +01:00
if ( ! item ) {
2011-03-24 22:31:38 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-03-24 22:31:38 +01:00
2011-11-05 11:51:46 +01:00
if ( m_activePixmap . isNull ( ) ) {
2011-03-24 22:31:38 +01:00
QMessageBox : : warning ( this , tr ( " Error! " ) , tr ( " This preview is not available! " ) ) ;
return ;
}
2011-11-05 11:51:46 +01:00
QString imageFileName = qz_getFileNameFromUrl ( QUrl ( item - > text ( 1 ) ) ) ;
QString filePath = QFileDialog : : getSaveFileName ( this , tr ( " Save image... " ) , QDir : : homePath ( ) + " / " + imageFileName ) ;
2011-11-06 17:01:23 +01:00
if ( filePath . isEmpty ( ) ) {
2011-03-24 22:31:38 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-03-24 22:31:38 +01:00
2011-11-05 11:51:46 +01:00
if ( ! m_activePixmap . save ( filePath ) ) {
2011-03-24 22:31:38 +01:00
QMessageBox : : critical ( this , tr ( " Error! " ) , tr ( " Cannot write to file! " ) ) ;
return ;
}
}
2011-03-23 22:36:03 +01:00
}
2011-11-06 17:01:23 +01:00
void SiteInfo : : showImagePreview ( QTreeWidgetItem * item )
2011-03-23 22:36:03 +01:00
{
2011-11-06 17:01:23 +01:00
if ( ! item ) {
2011-03-23 22:36:03 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-03-23 22:36:03 +01:00
QUrl imageUrl = item - > text ( 1 ) ;
2011-12-03 14:43:13 +01:00
if ( imageUrl . isRelative ( ) ) {
imageUrl = m_baseUrl . resolved ( imageUrl ) ;
}
2011-03-24 22:31:38 +01:00
QGraphicsScene * scene = new QGraphicsScene ( ui - > mediaPreview ) ;
2011-03-23 22:36:03 +01:00
2011-11-05 11:51:46 +01:00
if ( imageUrl . scheme ( ) = = " data " ) {
2011-12-04 16:54:57 +01:00
QByteArray encodedUrl = item - > text ( 1 ) . toUtf8 ( ) ;
2011-11-05 11:51:46 +01:00
QByteArray imageData = encodedUrl . mid ( encodedUrl . indexOf ( " , " ) + 1 ) ;
m_activePixmap = qz_pixmapFromByteArray ( imageData ) ;
}
2011-12-03 14:43:13 +01:00
else if ( imageUrl . scheme ( ) = = " file " ) {
2011-12-07 18:57:42 +01:00
m_activePixmap = QPixmap ( imageUrl . toLocalFile ( ) ) ;
2011-12-03 14:43:13 +01:00
}
else if ( imageUrl . scheme ( ) = = " qrc " ) {
m_activePixmap = QPixmap ( imageUrl . toString ( ) . mid ( 3 ) ) ; // Remove qrc from url
}
2011-03-24 22:31:38 +01:00
else {
2011-11-05 11:51:46 +01:00
QIODevice * cacheData = mApp - > networkCache ( ) - > data ( imageUrl ) ;
2011-11-06 17:01:23 +01:00
if ( ! cacheData ) {
2011-11-05 11:51:46 +01:00
m_activePixmap = QPixmap ( ) ;
2011-11-06 17:01:23 +01:00
}
else {
2011-11-05 11:51:46 +01:00
m_activePixmap . loadFromData ( cacheData - > readAll ( ) ) ;
2011-11-06 17:01:23 +01:00
}
2011-03-24 22:31:38 +01:00
}
2011-11-05 11:51:46 +01:00
2011-11-06 17:01:23 +01:00
if ( m_activePixmap . isNull ( ) ) {
2011-03-24 22:31:38 +01:00
scene - > addText ( tr ( " Preview not available " ) ) ;
2011-11-06 17:01:23 +01:00
}
else {
2011-11-05 11:51:46 +01:00
scene - > addPixmap ( m_activePixmap ) ;
2011-11-06 17:01:23 +01:00
}
2011-03-23 22:36:03 +01:00
ui - > mediaPreview - > setScene ( scene ) ;
}
void SiteInfo : : securityDetailsClicked ( )
{
ui - > listWidget - > setCurrentRow ( 2 ) ;
2011-03-22 21:36:15 +01:00
}
2011-11-06 17:01:23 +01:00
void SiteInfo : : itemChanged ( QListWidgetItem * item )
2011-03-22 21:36:15 +01:00
{
2011-11-06 17:01:23 +01:00
if ( ! item ) {
2011-03-22 21:36:15 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-03-22 21:36:15 +01:00
int index = item - > whatsThis ( ) . toInt ( ) ;
ui - > stackedWidget - > setCurrentIndex ( index ) ;
2011-03-02 16:57:41 +01:00
}
SiteInfo : : ~ SiteInfo ( )
{
delete ui ;
2011-11-06 17:01:23 +01:00
if ( m_certWidget ) {
2011-10-09 14:51:25 +02:00
delete m_certWidget ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
}