2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2014-01-11 16:11:42 +01:00
* Copyright ( C ) 2010 - 2014 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 "webpage.h"
2012-01-21 20:20:48 +01:00
# include "tabbedwebview.h"
2011-03-02 16:57:41 +01:00
# include "tabwidget.h"
2014-02-19 22:07:21 +01:00
# include "browserwindow.h"
2013-01-17 15:24:30 +01:00
# include "pluginproxy.h"
2011-03-02 16:57:41 +01:00
# include "downloadmanager.h"
# include "webpluginfactory.h"
# include "mainapplication.h"
2012-03-15 19:35:37 +01:00
# include "checkboxdialog.h"
2011-05-22 10:47:03 +02:00
# include "widget.h"
2013-01-22 19:04:22 +01:00
# include "qztools.h"
2011-12-09 21:56:01 +01:00
# include "speeddial.h"
2013-05-14 17:25:55 +02:00
# include "autofill.h"
2012-01-21 20:20:48 +01:00
# include "popupwebpage.h"
# include "popupwebview.h"
# include "networkmanagerproxy.h"
2012-02-04 18:45:59 +01:00
# include "adblockicon.h"
2012-06-25 16:07:25 +02:00
# include "adblockmanager.h"
2012-03-28 16:42:50 +02:00
# include "iconprovider.h"
2012-08-10 21:16:43 +02:00
# include "qzsettings.h"
2012-08-16 23:15:31 +02:00
# include "useragentmanager.h"
2013-04-02 13:14:19 +02:00
# include "delayedfilewatcher.h"
2012-08-21 20:28:38 +02:00
# include "recoverywidget.h"
2013-01-19 17:28:12 +01:00
# include "html5permissions/html5permissionsmanager.h"
2012-08-23 16:17:56 +02:00
# include "schemehandlers/fileschemehandler.h"
2014-02-03 23:50:18 +01:00
# include "javascript/externaljsobject.h"
2011-03-02 16:57:41 +01:00
2012-03-16 23:28:23 +01:00
# ifdef NONBLOCK_JS_DIALOGS
# include "ui_jsconfirm.h"
# include "ui_jsalert.h"
# include "ui_jsprompt.h"
# include <QPushButton>
# endif
2012-02-29 18:33:50 +01:00
# include <QDir>
2012-08-31 15:19:07 +02:00
# include <QMouseEvent>
2012-02-29 18:33:50 +01:00
# include <QWebHistory>
# include <QTimer>
# include <QNetworkReply>
# include <QDebug>
# include <QDesktopServices>
# include <QMessageBox>
# include <QFileDialog>
# include <QWebFrame>
2013-01-23 18:48:51 +01:00
# include <QWebSecurityOrigin>
2012-02-29 18:33:50 +01:00
2012-04-17 18:26:01 +02:00
QString WebPage : : s_lastUploadLocation = QDir : : homePath ( ) ;
QUrl WebPage : : s_lastUnsupportedUrl ;
2012-08-09 19:09:52 +02:00
QTime WebPage : : s_lastUnsupportedUrlTime ;
2012-04-17 18:26:01 +02:00
QList < WebPage * > WebPage : : s_livingPages ;
2011-10-26 19:23:50 +02:00
2013-05-12 22:55:53 +02:00
WebPage : : WebPage ( QObject * parent )
: QWebPage ( parent )
2012-01-21 20:20:48 +01:00
, m_view ( 0 )
2011-12-15 19:08:33 +01:00
, m_fileWatcher ( 0 )
2011-12-13 15:38:09 +01:00
, m_runningLoop ( 0 )
2012-07-03 15:22:42 +02:00
, m_loadProgress ( - 1 )
2011-10-26 19:23:50 +02:00
, m_blockAlerts ( false )
, m_secureStatus ( false )
2012-04-04 17:41:33 +02:00
, m_adjustingScheduled ( false )
2011-03-02 16:57:41 +01:00
{
2014-01-25 17:48:30 +01:00
m_javaScriptEnabled = mApp - > webSettings ( ) - > testAttribute ( QWebSettings : : JavascriptEnabled ) ;
2012-01-21 20:20:48 +01:00
m_networkProxy = new NetworkManagerProxy ( this ) ;
m_networkProxy - > setPrimaryNetworkAccessManager ( mApp - > networkManager ( ) ) ;
m_networkProxy - > setPage ( this ) ;
setNetworkAccessManager ( m_networkProxy ) ;
2011-03-02 16:57:41 +01:00
setForwardUnsupportedContent ( true ) ;
setPluginFactory ( new WebPluginFactory ( this ) ) ;
2011-07-31 00:50:40 +02:00
history ( ) - > setMaximumItemCount ( 20 ) ;
2011-12-02 23:25:27 +01:00
2012-01-14 11:46:06 +01:00
connect ( this , SIGNAL ( unsupportedContent ( QNetworkReply * ) ) , this , SLOT ( handleUnsupportedContent ( QNetworkReply * ) ) ) ;
2011-07-11 20:30:49 +02:00
connect ( this , SIGNAL ( loadProgress ( int ) ) , this , SLOT ( progress ( int ) ) ) ;
2011-07-11 22:53:32 +02:00
connect ( this , SIGNAL ( loadFinished ( bool ) ) , this , SLOT ( finished ( ) ) ) ;
2012-01-21 20:20:48 +01:00
connect ( this , SIGNAL ( printRequested ( QWebFrame * ) ) , this , SLOT ( printFrame ( QWebFrame * ) ) ) ;
connect ( this , SIGNAL ( downloadRequested ( QNetworkRequest ) ) , this , SLOT ( downloadRequested ( QNetworkRequest ) ) ) ;
2012-03-04 14:25:52 +01:00
connect ( this , SIGNAL ( windowCloseRequested ( ) ) , this , SLOT ( windowCloseRequested ( ) ) ) ;
2011-12-02 23:25:27 +01:00
2013-03-06 09:05:41 +01:00
connect ( this , SIGNAL ( databaseQuotaExceeded ( QWebFrame * , QString ) ) ,
2013-01-23 18:48:51 +01:00
this , SLOT ( dbQuotaExceeded ( QWebFrame * ) ) ) ;
2011-12-02 23:25:27 +01:00
connect ( mainFrame ( ) , SIGNAL ( javaScriptWindowObjectCleared ( ) ) , this , SLOT ( addJavaScriptObject ( ) ) ) ;
2012-03-15 19:35:37 +01:00
2013-01-20 12:10:28 +01:00
# if QTWEBKIT_FROM_2_2
2013-03-06 09:05:41 +01:00
connect ( this , SIGNAL ( featurePermissionRequested ( QWebFrame * , QWebPage : : Feature ) ) ,
this , SLOT ( featurePermissionRequested ( QWebFrame * , QWebPage : : Feature ) ) ) ;
2012-03-15 19:35:37 +01:00
# endif
2012-04-03 19:28:12 +02:00
2013-01-23 18:48:51 +01:00
# if QTWEBKIT_FROM_2_3
2013-03-06 09:05:41 +01:00
connect ( this , SIGNAL ( applicationCacheQuotaExceeded ( QWebSecurityOrigin * , quint64 , quint64 ) ) ,
this , SLOT ( appCacheQuotaExceeded ( QWebSecurityOrigin * , quint64 ) ) ) ;
2013-01-31 11:54:42 +01:00
# elif QTWEBKIT_FROM_2_2
2013-03-06 09:05:41 +01:00
connect ( this , SIGNAL ( applicationCacheQuotaExceeded ( QWebSecurityOrigin * , quint64 ) ) ,
this , SLOT ( appCacheQuotaExceeded ( QWebSecurityOrigin * , quint64 ) ) ) ;
2013-01-23 18:48:51 +01:00
# endif
2012-04-17 18:26:01 +02:00
s_livingPages . append ( this ) ;
2012-01-21 20:20:48 +01:00
}
2011-12-13 15:38:09 +01:00
2012-01-21 20:20:48 +01:00
QUrl WebPage : : url ( ) const
{
return mainFrame ( ) - > url ( ) ;
}
void WebPage : : setWebView ( TabbedWebView * view )
{
if ( m_view = = view ) {
return ;
}
if ( m_view ) {
delete m_view ;
m_view = 0 ;
}
m_view = view ;
m_view - > setWebPage ( this ) ;
connect ( m_view , SIGNAL ( urlChanged ( QUrl ) ) , this , SLOT ( urlChanged ( QUrl ) ) ) ;
2011-10-09 14:51:25 +02:00
}
2011-11-12 09:45:32 +01:00
void WebPage : : scheduleAdjustPage ( )
{
2012-03-04 14:25:52 +01:00
WebView * webView = qobject_cast < WebView * > ( view ( ) ) ;
if ( ! webView ) {
return ;
}
if ( webView - > isLoading ( ) ) {
2011-11-12 09:45:32 +01:00
m_adjustingScheduled = true ;
}
else {
2013-12-30 13:43:48 +01:00
const QSize originalSize = webView - > size ( ) ;
2012-03-04 14:25:52 +01:00
QSize newSize ( originalSize . width ( ) - 1 , originalSize . height ( ) - 1 ) ;
webView - > resize ( newSize ) ;
webView - > resize ( originalSize ) ;
2011-11-12 09:45:32 +01:00
}
2012-01-21 20:20:48 +01:00
}
2011-11-12 09:45:32 +01:00
2012-04-04 14:45:45 +02:00
bool WebPage : : loadingError ( ) const
{
return ! mainFrame ( ) - > findFirstElement ( " span[id= \" qupzilla-error-page \" ] " ) . isNull ( ) ;
}
2012-07-03 15:22:42 +02:00
void WebPage : : addRejectedCerts ( const QList < QSslCertificate > & certs )
{
2013-03-06 09:05:41 +01:00
foreach ( const QSslCertificate & cert , certs ) {
2012-07-03 15:22:42 +02:00
if ( ! m_rejectedSslCerts . contains ( cert ) ) {
m_rejectedSslCerts . append ( cert ) ;
}
}
}
2012-07-03 21:54:04 +02:00
bool WebPage : : containsRejectedCerts ( const QList < QSslCertificate > & certs )
2012-07-03 15:22:42 +02:00
{
int matches = 0 ;
2013-03-06 09:05:41 +01:00
foreach ( const QSslCertificate & cert , certs ) {
2012-07-03 15:22:42 +02:00
if ( m_rejectedSslCerts . contains ( cert ) ) {
+ + matches ;
}
2012-07-03 21:54:04 +02:00
if ( m_sslCert = = cert ) {
m_sslCert . clear ( ) ;
}
2012-07-03 15:22:42 +02:00
}
return matches = = certs . count ( ) ;
}
2013-04-07 20:19:21 +02:00
QWebElement WebPage : : activeElement ( ) const
{
QRect activeRect = inputMethodQuery ( Qt : : ImMicroFocus ) . toRect ( ) ;
return mainFrame ( ) - > hitTestContent ( activeRect . center ( ) ) . element ( ) ;
}
2012-01-21 20:20:48 +01:00
bool WebPage : : isRunningLoop ( )
{
return m_runningLoop ;
2011-11-12 09:45:32 +01:00
}
2012-07-03 15:22:42 +02:00
bool WebPage : : isLoading ( ) const
{
return m_loadProgress < 100 ;
}
2011-10-09 14:51:25 +02:00
void WebPage : : urlChanged ( const QUrl & url )
{
2014-01-20 12:46:39 +01:00
// Make sure JavaScript is enabled for qupzilla pages regardless of user settings
if ( url . scheme ( ) = = QLatin1String ( " qupzilla " ) ) {
settings ( ) - > setAttribute ( QWebSettings : : JavascriptEnabled , true ) ;
}
2012-02-04 19:43:43 +01:00
2012-07-03 15:22:42 +02:00
if ( isLoading ( ) ) {
m_adBlockedEntries . clear ( ) ;
m_blockAlerts = false ;
}
2011-07-11 20:30:49 +02:00
}
void WebPage : : progress ( int prog )
{
2012-07-03 15:22:42 +02:00
m_loadProgress = prog ;
2012-02-04 19:43:43 +01:00
2013-01-22 19:04:22 +01:00
bool secStatus = QzTools : : isCertificateValid ( sslCertificate ( ) ) ;
2011-07-11 20:30:49 +02:00
if ( secStatus ! = m_secureStatus ) {
m_secureStatus = secStatus ;
2013-01-22 19:04:22 +01:00
emit privacyChanged ( QzTools : : isCertificateValid ( sslCertificate ( ) ) ) ;
2011-07-11 20:30:49 +02:00
}
2011-03-02 16:57:41 +01:00
}
2011-07-11 22:53:32 +02:00
void WebPage : : finished ( )
{
progress ( 100 ) ;
2011-10-30 16:32:36 +01:00
if ( m_adjustingScheduled ) {
m_adjustingScheduled = false ;
mainFrame ( ) - > setZoomFactor ( mainFrame ( ) - > zoomFactor ( ) + 1 ) ;
mainFrame ( ) - > setZoomFactor ( mainFrame ( ) - > zoomFactor ( ) - 1 ) ;
}
2013-02-08 18:44:26 +01:00
// File scheme watcher
2012-09-04 12:42:45 +02:00
if ( url ( ) . scheme ( ) = = QLatin1String ( " file " ) ) {
2012-08-23 16:17:56 +02:00
QFileInfo info ( url ( ) . toLocalFile ( ) ) ;
if ( info . isFile ( ) ) {
if ( ! m_fileWatcher ) {
2013-04-02 13:14:19 +02:00
m_fileWatcher = new DelayedFileWatcher ( this ) ;
connect ( m_fileWatcher , SIGNAL ( delayedFileChanged ( QString ) ) , this , SLOT ( watchedFileChanged ( QString ) ) ) ;
2012-08-23 16:17:56 +02:00
}
2011-12-15 19:08:33 +01:00
2013-12-30 13:43:48 +01:00
const QString filePath = url ( ) . toLocalFile ( ) ;
2011-12-15 19:08:33 +01:00
2012-08-23 16:17:56 +02:00
if ( QFile : : exists ( filePath ) & & ! m_fileWatcher - > files ( ) . contains ( filePath ) ) {
m_fileWatcher - > addPath ( filePath ) ;
}
2011-12-15 19:08:33 +01:00
}
}
2012-01-21 20:20:48 +01:00
else if ( m_fileWatcher & & ! m_fileWatcher - > files ( ) . isEmpty ( ) ) {
2011-12-15 19:08:33 +01:00
m_fileWatcher - > removePaths ( m_fileWatcher - > files ( ) ) ;
}
2013-02-08 18:44:26 +01:00
// Autofill
2013-05-14 17:25:55 +02:00
m_passwordEntries = mApp - > autoFill ( ) - > completePage ( this ) ;
2013-02-08 18:44:26 +01:00
// AdBlock
2012-07-15 23:18:08 +02:00
cleanBlockedObjects ( ) ;
2011-07-11 22:53:32 +02:00
}
2011-12-15 19:08:33 +01:00
void WebPage : : watchedFileChanged ( const QString & file )
{
2012-01-21 20:20:48 +01:00
if ( url ( ) . toLocalFile ( ) = = file ) {
2011-12-15 19:08:33 +01:00
triggerAction ( QWebPage : : Reload ) ;
}
}
2012-01-07 10:29:38 +01:00
void WebPage : : printFrame ( QWebFrame * frame )
2012-01-04 19:02:33 +01:00
{
2012-01-21 20:20:48 +01:00
WebView * webView = qobject_cast < WebView * > ( view ( ) ) ;
if ( ! webView ) {
return ;
}
2012-01-04 19:02:33 +01:00
2012-01-21 20:20:48 +01:00
webView - > printPage ( frame ) ;
}
2011-05-22 10:47:03 +02:00
2011-12-02 23:25:27 +01:00
void WebPage : : addJavaScriptObject ( )
{
2014-01-20 12:46:39 +01:00
// Make sure all other sites have JavaScript set by user preferences
// (JavaScript is enabled in WebPage::urlChanged)
if ( url ( ) . scheme ( ) ! = QLatin1String ( " qupzilla " ) ) {
2014-01-25 17:48:30 +01:00
settings ( ) - > setAttribute ( QWebSettings : : JavascriptEnabled , m_javaScriptEnabled ) ;
2014-01-20 12:46:39 +01:00
}
2014-02-03 23:50:18 +01:00
ExternalJsObject * jsObject = new ExternalJsObject ( this ) ;
mainFrame ( ) - > addToJavaScriptWindowObject ( " external " , jsObject ) ;
2011-12-02 23:25:27 +01:00
2014-02-03 23:50:18 +01:00
if ( url ( ) . toString ( ) = = QLatin1String ( " qupzilla:speeddial " ) ) {
jsObject - > setOnSpeedDial ( true ) ;
mApp - > plugins ( ) - > speedDial ( ) - > addWebFrame ( mainFrame ( ) ) ;
}
2011-12-02 23:25:27 +01:00
}
2011-03-02 16:57:41 +01:00
void WebPage : : handleUnsupportedContent ( QNetworkReply * reply )
{
2011-11-06 17:01:23 +01:00
if ( ! reply ) {
2011-03-02 16:57:41 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2012-02-04 19:43:43 +01:00
2013-12-30 13:43:48 +01:00
const QUrl url = reply - > url ( ) ;
2011-03-02 16:57:41 +01:00
2011-11-06 17:01:23 +01:00
switch ( reply - > error ( ) ) {
2011-03-02 16:57:41 +01:00
case QNetworkReply : : NoError :
2012-02-11 14:19:41 +01:00
if ( reply - > header ( QNetworkRequest : : ContentTypeHeader ) . isValid ( ) ) {
QString requestUrl = reply - > request ( ) . url ( ) . toString ( QUrl : : RemoveFragment | QUrl : : RemoveQuery ) ;
2012-09-04 12:42:45 +02:00
if ( requestUrl . endsWith ( QLatin1String ( " .swf " ) ) ) {
2013-12-30 13:43:48 +01:00
const QWebElement docElement = mainFrame ( ) - > documentElement ( ) ;
const QWebElement object = docElement . findFirst ( QString ( " object[src= \" %1 \" ] " ) . arg ( requestUrl ) ) ;
const QWebElement embed = docElement . findFirst ( QString ( " embed[src= \" %1 \" ] " ) . arg ( requestUrl ) ) ;
2012-02-11 14:19:41 +01:00
if ( ! object . isNull ( ) | | ! embed . isNull ( ) ) {
qDebug ( ) < < " WebPage::UnsupportedContent " < < url < < " Attempt to download flash object on site! " ;
reply - > deleteLater ( ) ;
return ;
}
}
2011-03-04 13:59:07 +01:00
DownloadManager * dManager = mApp - > downManager ( ) ;
2011-11-08 15:20:53 +01:00
dManager - > handleUnsupportedContent ( reply , this ) ;
2011-03-02 16:57:41 +01:00
return ;
}
2014-02-01 19:21:49 +01:00
// Falling unsupported content with invalid ContentTypeHeader to be handled as UnknownProtocol
2012-02-11 12:52:23 +01:00
2012-02-17 18:55:34 +01:00
case QNetworkReply : : ProtocolUnknownError : {
2012-09-04 12:42:45 +02:00
if ( url . scheme ( ) = = QLatin1String ( " file " ) ) {
2012-08-23 16:17:56 +02:00
FileSchemeHandler : : handleUrl ( url ) ;
return ;
}
2012-02-11 12:52:23 +01:00
qDebug ( ) < < " WebPage::UnsupportedContent " < < url < < " ProtocolUnknowError " ;
2012-08-09 19:09:52 +02:00
desktopServicesOpen ( url ) ;
2012-02-12 18:02:05 +01:00
2012-02-11 12:52:23 +01:00
reply - > deleteLater ( ) ;
2011-03-02 16:57:41 +01:00
return ;
2012-02-17 18:55:34 +01:00
}
2011-03-02 16:57:41 +01:00
default :
break ;
}
2012-02-04 19:43:43 +01:00
2012-02-11 12:52:23 +01:00
qDebug ( ) < < " WebPage::UnsupportedContent error " < < url < < reply - > errorString ( ) ;
reply - > deleteLater ( ) ;
2011-03-02 16:57:41 +01:00
}
2012-04-13 13:26:32 +02:00
void WebPage : : handleUnknownProtocol ( const QUrl & url )
{
2013-12-30 13:43:48 +01:00
const QString protocol = url . scheme ( ) ;
2012-04-13 13:26:32 +02:00
2013-04-12 17:52:51 +02:00
if ( protocol = = QLatin1String ( " mailto " ) ) {
desktopServicesOpen ( url ) ;
return ;
}
2012-08-10 21:16:43 +02:00
if ( qzSettings - > blockedProtocols . contains ( protocol ) ) {
2012-04-13 13:26:32 +02:00
qDebug ( ) < < " WebPage::handleUnknownProtocol Protocol " < < protocol < < " is blocked! " ;
return ;
}
2012-08-10 21:16:43 +02:00
if ( qzSettings - > autoOpenProtocols . contains ( protocol ) ) {
2012-08-09 19:09:52 +02:00
desktopServicesOpen ( url ) ;
2012-04-13 13:26:32 +02:00
return ;
}
2012-05-13 17:01:35 +02:00
CheckBoxDialog dialog ( QDialogButtonBox : : Yes | QDialogButtonBox : : No , view ( ) ) ;
2013-12-30 13:43:48 +01:00
const QString wrappedUrl = QzTools : : alignTextToWidth ( url . toString ( ) , " <br/> " , dialog . fontMetrics ( ) , 450 ) ;
const QString text = tr ( " QupZilla cannot handle <b>%1:</b> links. The requested link "
" is <ul><li>%2</li></ul>Do you want QupZilla to try "
" open this link in system application? " ) . arg ( protocol , wrappedUrl ) ;
2012-05-13 17:01:35 +02:00
2012-04-13 13:26:32 +02:00
dialog . setText ( text ) ;
dialog . setCheckBoxText ( tr ( " Remember my choice for this protocol " ) ) ;
dialog . setWindowTitle ( tr ( " External Protocol Request " ) ) ;
2012-04-22 20:51:28 +02:00
dialog . setIcon ( qIconProvider - > standardIcon ( QStyle : : SP_MessageBoxQuestion ) ) ;
2012-04-13 13:26:32 +02:00
switch ( dialog . exec ( ) ) {
case QDialog : : Accepted :
if ( dialog . isChecked ( ) ) {
2012-08-10 21:16:43 +02:00
qzSettings - > autoOpenProtocols . append ( protocol ) ;
qzSettings - > saveSettings ( ) ;
2012-04-13 13:26:32 +02:00
}
2014-03-03 12:55:35 +01:00
2012-04-13 13:26:32 +02:00
QDesktopServices : : openUrl ( url ) ;
break ;
case QDialog : : Rejected :
if ( dialog . isChecked ( ) ) {
2012-08-10 21:16:43 +02:00
qzSettings - > blockedProtocols . append ( protocol ) ;
qzSettings - > saveSettings ( ) ;
2012-04-13 13:26:32 +02:00
}
break ;
default :
break ;
}
}
2012-08-09 19:09:52 +02:00
void WebPage : : desktopServicesOpen ( const QUrl & url )
{
// Open same url only once in 2 secs
2014-03-03 12:55:35 +01:00
const int sameUrlTimeout = 2 * 1000 ;
2012-08-09 19:09:52 +02:00
2014-03-03 12:55:35 +01:00
if ( s_lastUnsupportedUrl ! = url | | s_lastUnsupportedUrlTime . isNull ( ) | | s_lastUnsupportedUrlTime . elapsed ( ) > sameUrlTimeout ) {
2012-08-09 19:09:52 +02:00
s_lastUnsupportedUrl = url ;
2014-03-03 12:55:35 +01:00
s_lastUnsupportedUrlTime . restart ( ) ;
2012-08-09 19:09:52 +02:00
QDesktopServices : : openUrl ( url ) ;
}
else {
qWarning ( ) < < " WebPage::desktopServicesOpen Url " < < url < < " has already been opened! \n "
" Ignoring it to prevent infinite loop! " ;
}
}
2012-01-21 20:20:48 +01:00
void WebPage : : downloadRequested ( const QNetworkRequest & request )
{
DownloadManager * dManager = mApp - > downManager ( ) ;
dManager - > download ( request , this ) ;
}
2012-03-04 14:25:52 +01:00
void WebPage : : windowCloseRequested ( )
{
WebView * webView = qobject_cast < WebView * > ( view ( ) ) ;
if ( ! webView ) {
return ;
}
webView - > closeView ( ) ;
}
2013-01-23 18:48:51 +01:00
void WebPage : : dbQuotaExceeded ( QWebFrame * frame )
{
if ( ! frame ) {
return ;
}
2013-12-30 13:43:48 +01:00
const QWebSecurityOrigin origin = frame - > securityOrigin ( ) ;
2013-01-23 18:48:51 +01:00
const qint64 oldQuota = origin . databaseQuota ( ) ;
frame - > securityOrigin ( ) . setDatabaseQuota ( oldQuota * 2 ) ;
}
2013-06-17 21:03:45 +02:00
# ifdef USE_QTWEBKIT_2_2
2013-01-23 18:48:51 +01:00
void WebPage : : appCacheQuotaExceeded ( QWebSecurityOrigin * origin , quint64 originalQuota )
{
if ( ! origin ) {
return ;
}
origin - > setApplicationCacheQuota ( originalQuota * 2 ) ;
}
2012-03-15 19:35:37 +01:00
void WebPage : : featurePermissionRequested ( QWebFrame * frame , const QWebPage : : Feature & feature )
{
2013-01-19 17:28:12 +01:00
mApp - > html5permissions ( ) - > requestPermissions ( this , frame , feature ) ;
2012-03-15 19:35:37 +01:00
}
2013-06-17 21:03:45 +02:00
# endif // USE_QTWEBKIT_2_2
2012-01-21 20:20:48 +01:00
2012-03-16 16:57:24 +01:00
bool WebPage : : event ( QEvent * event )
2012-03-16 15:55:34 +01:00
{
if ( event - > type ( ) = = QEvent : : Leave ) {
// QWebPagePrivate::leaveEvent():
// Fake a mouse move event just outside of the widget, since all
// the interesting mouse-out behavior like invalidating scrollbars
// is handled by the WebKit event handler's mouseMoved function.
// However, its implementation fake mouse move event on QCursor::pos()
// position that is in global screen coordinates. So instead of
// really faking it, it just creates mouse move event somewhere in
// page. It can for example focus a link, and then link url gets
// stuck in status bar message.
// So we are faking mouse move event with proper coordinates for
// so called "just outside of the widget" position
2012-05-27 11:38:21 +02:00
const QPoint cursorPos = view ( ) - > mapFromGlobal ( QCursor : : pos ( ) ) ;
QPoint mousePos ;
2012-05-08 14:28:54 +02:00
2012-05-27 11:38:21 +02:00
if ( cursorPos . y ( ) < 0 ) {
// Left on top
mousePos = QPoint ( cursorPos . x ( ) , - 1 ) ;
}
else if ( cursorPos . x ( ) < 0 ) {
// Left on left
mousePos = QPoint ( - 1 , cursorPos . y ( ) ) ;
}
2012-05-27 12:44:56 +02:00
else if ( cursorPos . y ( ) > view ( ) - > height ( ) ) {
2012-05-27 11:38:21 +02:00
// Left on bottom
mousePos = QPoint ( cursorPos . x ( ) , view ( ) - > height ( ) + 1 ) ;
}
else {
// Left on right
mousePos = QPoint ( view ( ) - > width ( ) + 1 , cursorPos . y ( ) ) ;
}
QMouseEvent fakeEvent ( QEvent : : MouseMove , mousePos , Qt : : NoButton , Qt : : NoButton , Qt : : NoModifier ) ;
2012-03-16 15:55:34 +01:00
return QWebPage : : event ( & fakeEvent ) ;
}
return QWebPage : : event ( event ) ;
}
2011-04-24 09:08:53 +02:00
void WebPage : : setSSLCertificate ( const QSslCertificate & cert )
2011-03-22 21:36:15 +01:00
{
2012-02-17 18:55:34 +01:00
// if (cert != m_SslCert)
2012-07-03 21:54:04 +02:00
m_sslCert = cert ;
2011-03-22 21:36:15 +01:00
}
2011-04-04 16:00:27 +02:00
QSslCertificate WebPage : : sslCertificate ( )
{
2013-01-22 19:04:22 +01:00
if ( url ( ) . scheme ( ) = = QLatin1String ( " https " ) & & QzTools : : isCertificateValid ( m_sslCert ) ) {
2012-07-03 21:54:04 +02:00
return m_sslCert ;
2011-11-06 17:01:23 +01:00
}
2012-07-03 21:54:04 +02:00
return QSslCertificate ( ) ;
2011-04-04 16:00:27 +02:00
}
2011-03-17 17:03:04 +01:00
bool WebPage : : acceptNavigationRequest ( QWebFrame * frame , const QNetworkRequest & request , NavigationType type )
2011-03-02 16:57:41 +01:00
{
m_lastRequestType = type ;
2012-07-13 11:04:14 +02:00
m_lastRequestUrl = request . url ( ) ;
2011-03-02 16:57:41 +01:00
if ( type = = QWebPage : : NavigationTypeFormResubmitted ) {
2013-01-26 12:20:46 +01:00
// Don't show this dialog if app is still starting
2013-02-07 14:38:31 +01:00
if ( ! view ( ) | | ! view ( ) - > isVisible ( ) ) {
2013-01-26 12:20:46 +01:00
return false ;
}
2012-01-21 20:20:48 +01:00
QString message = tr ( " To show this page, QupZilla must resend request which do it again \n "
2012-08-09 19:09:52 +02:00
" (like searching on making an shopping, which has been already done.) " ) ;
2012-01-22 11:49:58 +01:00
bool result = ( QMessageBox : : question ( view ( ) , tr ( " Confirm form resubmission " ) ,
2012-01-21 20:20:48 +01:00
message , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : No ) = = QMessageBox : : Yes ) ;
2011-11-06 17:01:23 +01:00
if ( ! result ) {
2011-03-02 16:57:41 +01:00
return false ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
}
bool accept = QWebPage : : acceptNavigationRequest ( frame , request , type ) ;
return accept ;
}
void WebPage : : populateNetworkRequest ( QNetworkRequest & request )
{
2011-12-09 21:56:01 +01:00
WebPage * pagePointer = this ;
2011-11-08 15:20:53 +01:00
2013-02-05 22:33:54 +01:00
QVariant variant = QVariant : : fromValue ( ( void * ) pagePointer ) ;
2011-03-02 16:57:41 +01:00
request . setAttribute ( ( QNetworkRequest : : Attribute ) ( QNetworkRequest : : User + 100 ) , variant ) ;
2012-07-13 11:04:14 +02:00
if ( m_lastRequestUrl = = request . url ( ) ) {
request . setAttribute ( ( QNetworkRequest : : Attribute ) ( QNetworkRequest : : User + 101 ) , m_lastRequestType ) ;
if ( m_lastRequestType = = NavigationTypeLinkClicked ) {
request . setRawHeader ( " X-QupZilla-UserLoadAction " , QByteArray ( " 1 " ) ) ;
}
}
2011-03-02 16:57:41 +01:00
}
QWebPage * WebPage : : createWindow ( QWebPage : : WebWindowType type )
{
2013-05-12 22:55:53 +02:00
if ( m_view ) {
return new PopupWebPage ( type , m_view - > mainWindow ( ) ) ;
}
if ( PopupWebPage * popupPage = qobject_cast < PopupWebPage * > ( this ) ) {
return new PopupWebPage ( type , popupPage - > mainWindow ( ) ) ;
}
return 0 ;
2011-03-02 16:57:41 +01:00
}
2013-01-23 18:48:51 +01:00
QObject * WebPage : : createPlugin ( const QString & classid , const QUrl & url ,
const QStringList & paramNames , const QStringList & paramValues )
2012-04-15 15:44:08 +02:00
{
2012-08-21 20:28:38 +02:00
Q_UNUSED ( url )
Q_UNUSED ( paramNames )
Q_UNUSED ( paramValues )
2013-05-12 22:55:53 +02:00
if ( classid = = QLatin1String ( " RecoveryWidget " ) & & mApp - > restoreManager ( ) & & m_view ) {
return new RecoveryWidget ( m_view , m_view - > mainWindow ( ) ) ;
2012-08-21 20:28:38 +02:00
}
2012-09-04 12:42:45 +02:00
else {
mainFrame ( ) - > load ( QUrl ( " qupzilla:start " ) ) ;
}
2012-08-21 20:28:38 +02:00
return 0 ;
2012-04-15 15:44:08 +02:00
}
2012-07-01 18:13:49 +02:00
void WebPage : : addAdBlockRule ( const AdBlockRule * rule , const QUrl & url )
2011-03-29 20:30:05 +02:00
{
AdBlockedEntry entry ;
2012-07-01 18:13:49 +02:00
entry . rule = rule ;
2011-03-29 20:30:05 +02:00
entry . url = url ;
2011-11-06 17:01:23 +01:00
if ( ! m_adBlockedEntries . contains ( entry ) ) {
2011-10-09 14:51:25 +02:00
m_adBlockedEntries . append ( entry ) ;
2011-11-06 17:01:23 +01:00
}
2011-10-09 14:51:25 +02:00
}
2013-02-26 12:56:11 +01:00
QVector < WebPage : : AdBlockedEntry > WebPage : : adBlockedEntries ( ) const
2013-02-08 18:44:26 +01:00
{
return m_adBlockedEntries ;
}
bool WebPage : : hasMultipleUsernames ( ) const
{
2013-05-14 17:25:55 +02:00
return m_passwordEntries . count ( ) > 1 ;
2013-02-08 18:44:26 +01:00
}
2013-05-14 17:25:55 +02:00
QVector < PasswordEntry > WebPage : : autoFillData ( ) const
2013-02-08 18:44:26 +01:00
{
2013-05-14 17:25:55 +02:00
return m_passwordEntries ;
2013-02-08 18:44:26 +01:00
}
2011-10-09 14:51:25 +02:00
void WebPage : : cleanBlockedObjects ( )
{
2012-07-04 10:08:55 +02:00
AdBlockManager * manager = AdBlockManager : : instance ( ) ;
2012-07-13 14:12:48 +02:00
if ( ! manager - > isEnabled ( ) ) {
2012-06-25 16:07:25 +02:00
return ;
}
2013-12-30 13:43:48 +01:00
const QWebElement docElement = mainFrame ( ) - > documentElement ( ) ;
2011-10-09 14:51:25 +02:00
2013-03-06 09:05:41 +01:00
foreach ( const AdBlockedEntry & entry , m_adBlockedEntries ) {
2013-12-30 13:43:48 +01:00
const QString urlString = entry . url . toString ( ) ;
2012-09-04 12:42:45 +02:00
if ( urlString . endsWith ( QLatin1String ( " .js " ) ) | | urlString . endsWith ( QLatin1String ( " .css " ) ) ) {
2011-10-09 14:51:25 +02:00
continue ;
2011-11-06 17:01:23 +01:00
}
2011-10-09 14:51:25 +02:00
2012-07-13 14:12:48 +02:00
QString urlEnd ;
2012-09-04 12:42:45 +02:00
int pos = urlString . lastIndexOf ( QLatin1Char ( ' / ' ) ) ;
2012-07-13 14:12:48 +02:00
if ( pos > 8 ) {
urlEnd = urlString . mid ( pos + 1 ) ;
}
2012-09-04 12:42:45 +02:00
if ( urlString . endsWith ( QLatin1Char ( ' / ' ) ) ) {
2012-07-13 14:12:48 +02:00
urlEnd = urlString . left ( urlString . size ( ) - 1 ) ;
2011-10-09 14:51:25 +02:00
}
2011-08-04 21:37:04 +02:00
2012-09-04 12:42:45 +02:00
QString selector ( " img[src$= \" %1 \" ], iframe[src$= \" %1 \" ],embed[src$= \" %1 \" ] " ) ;
QWebElementCollection elements = docElement . findAll ( selector . arg ( urlEnd ) ) ;
2012-01-23 17:30:34 +01:00
2013-03-06 09:05:41 +01:00
foreach ( QWebElement element , elements ) {
2012-07-04 10:08:55 +02:00
QString src = element . attribute ( " src " ) ;
2012-09-04 12:42:45 +02:00
src . remove ( QLatin1String ( " ../ " ) ) ;
2012-01-23 17:30:34 +01:00
2012-07-13 14:12:48 +02:00
if ( urlString . contains ( src ) ) {
element . setStyleProperty ( " display " , " none " ) ;
2012-07-04 10:08:55 +02:00
}
}
2012-01-23 17:30:34 +01:00
}
2012-06-25 16:07:25 +02:00
// Apply domain-specific element hiding rules
2013-02-07 14:38:31 +01:00
QString elementHiding = manager - > elementHidingRulesForDomain ( url ( ) ) ;
2012-07-04 16:00:53 +02:00
if ( elementHiding . isEmpty ( ) ) {
return ;
}
2013-05-02 21:01:17 +02:00
elementHiding . append ( QLatin1String ( " \n </style> " ) ) ;
2012-06-25 16:07:25 +02:00
2012-07-04 10:08:55 +02:00
QWebElement bodyElement = docElement . findFirst ( " body " ) ;
bodyElement . appendInside ( " <style type= \" text/css \" > \n /* AdBlock for QupZilla */ \n " + elementHiding ) ;
2013-02-06 23:45:43 +01:00
2013-02-07 11:04:09 +01:00
// When hiding some elements, scroll position of page will change
// If user loaded anchor link in background tab (and didn't show it yet), fix the scroll position
2013-02-07 14:38:31 +01:00
if ( view ( ) & & ! view ( ) - > isVisible ( ) & & ! url ( ) . fragment ( ) . isEmpty ( ) ) {
2013-02-06 23:45:43 +01:00
mainFrame ( ) - > scrollToAnchor ( url ( ) . fragment ( ) ) ;
}
2011-03-29 20:30:05 +02:00
}
2011-12-11 19:37:16 +01:00
QString WebPage : : userAgentForUrl ( const QUrl & url ) const
{
2012-08-16 23:15:31 +02:00
QString userAgent = mApp - > uaManager ( ) - > userAgentForUrl ( url ) ;
2012-03-31 21:36:27 +02:00
2012-08-16 23:15:31 +02:00
if ( userAgent . isEmpty ( ) ) {
userAgent = QWebPage : : userAgentForUrl ( url ) ;
2012-09-03 22:48:52 +02:00
# ifdef Q_OS_MAC
2012-01-16 16:28:41 +01:00
# ifdef __i386__ || __x86_64__
2012-09-04 12:42:45 +02:00
userAgent . replace ( QLatin1String ( " PPC Mac OS X " ) , QLatin1String ( " Intel Mac OS X " ) ) ;
2012-01-16 16:28:41 +01:00
# endif
# endif
2011-12-11 19:37:16 +01:00
}
2012-08-16 23:15:31 +02:00
return userAgent ;
2011-12-11 19:37:16 +01:00
}
2012-01-21 20:20:48 +01:00
bool WebPage : : supportsExtension ( Extension extension ) const
{
Q_UNUSED ( extension )
return true ;
}
2011-04-25 20:56:45 +02:00
bool WebPage : : extension ( Extension extension , const ExtensionOption * option , ExtensionReturn * output )
2011-03-02 16:57:41 +01:00
{
2011-11-06 17:01:23 +01:00
if ( extension = = ChooseMultipleFilesExtension ) {
2012-01-21 20:20:48 +01:00
const QWebPage : : ChooseMultipleFilesExtensionOption * exOption = static_cast < const QWebPage : : ChooseMultipleFilesExtensionOption * > ( option ) ;
QWebPage : : ChooseMultipleFilesExtensionReturn * exReturn = static_cast < QWebPage : : ChooseMultipleFilesExtensionReturn * > ( output ) ;
if ( ! exOption | | ! exReturn ) {
return QWebPage : : extension ( extension , option , output ) ;
}
QString suggestedFileName ;
if ( ! exOption - > suggestedFileNames . isEmpty ( ) ) {
2012-03-28 16:42:50 +02:00
suggestedFileName = exOption - > suggestedFileNames . at ( 0 ) ;
2012-01-21 20:20:48 +01:00
}
2013-12-25 19:06:58 +01:00
exReturn - > fileNames = QzTools : : getOpenFileNames ( " WebPage-UploadFiles " , 0 , tr ( " Select files to upload... " ) , suggestedFileName ) ;
2012-01-21 20:20:48 +01:00
return true ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
const ErrorPageExtensionOption * exOption = static_cast < const QWebPage : : ErrorPageExtensionOption * > ( option ) ;
ErrorPageExtensionReturn * exReturn = static_cast < QWebPage : : ErrorPageExtensionReturn * > ( output ) ;
2012-01-21 20:20:48 +01:00
if ( ! exOption | | ! exReturn ) {
return QWebPage : : extension ( extension , option , output ) ;
}
2012-01-13 21:04:40 +01:00
WebPage * erPage = qobject_cast < WebPage * > ( exOption - > frame - > page ( ) ) ;
2012-01-21 20:20:48 +01:00
if ( ! erPage ) {
return QWebPage : : extension ( extension , option , output ) ;
}
2011-03-02 16:57:41 +01:00
QString errorString ;
if ( exOption - > domain = = QWebPage : : QtNetwork ) {
switch ( exOption - > error ) {
case QNetworkReply : : ConnectionRefusedError :
errorString = tr ( " Server refused the connection " ) ;
break ;
case QNetworkReply : : RemoteHostClosedError :
errorString = tr ( " Server closed the connection " ) ;
break ;
case QNetworkReply : : HostNotFoundError :
errorString = tr ( " Server not found " ) ;
break ;
case QNetworkReply : : TimeoutError :
errorString = tr ( " Connection timed out " ) ;
break ;
case QNetworkReply : : SslHandshakeFailedError :
errorString = tr ( " Untrusted connection " ) ;
break ;
2012-01-14 12:44:14 +01:00
case QNetworkReply : : TemporaryNetworkFailureError :
errorString = tr ( " Temporary network failure " ) ;
break ;
case QNetworkReply : : ProxyConnectionRefusedError :
errorString = tr ( " Proxy connection refused " ) ;
break ;
case QNetworkReply : : ProxyNotFoundError :
2012-02-02 16:09:35 +01:00
errorString = tr ( " Proxy server not found " ) ;
2012-01-14 12:44:14 +01:00
break ;
case QNetworkReply : : ProxyTimeoutError :
errorString = tr ( " Proxy connection timed out " ) ;
break ;
case QNetworkReply : : ProxyAuthenticationRequiredError :
errorString = tr ( " Proxy authentication required " ) ;
break ;
case QNetworkReply : : ContentNotFoundError :
errorString = tr ( " Content not found " ) ;
break ;
2012-03-21 15:05:04 +01:00
case QNetworkReply : : UnknownNetworkError :
errorString = exOption - > errorString . isEmpty ( ) ? tr ( " Unknown network error " ) : exOption - > errorString ;
break ;
2012-06-27 20:32:13 +02:00
case QNetworkReply : : ProtocolUnknownError : {
// Sometimes exOption->url returns just "?" instead of actual url
2012-09-04 12:42:45 +02:00
const QUrl unknownProtocolUrl = ( exOption - > url . toString ( ) = = QLatin1String ( " ? " ) ) ? erPage - > mainFrame ( ) - > requestedUrl ( ) : exOption - > url ;
2012-06-27 20:32:13 +02:00
handleUnknownProtocol ( unknownProtocolUrl ) ;
2012-04-13 13:26:32 +02:00
return false ;
2012-06-27 20:32:13 +02:00
}
2011-04-05 18:45:17 +02:00
case QNetworkReply : : ContentAccessDenied :
2012-09-04 12:42:45 +02:00
if ( exOption - > errorString . startsWith ( QLatin1String ( " AdBlock " ) ) ) {
2012-01-13 21:04:40 +01:00
if ( exOption - > frame ! = erPage - > mainFrame ( ) ) { //Content in <iframe>
QWebElement docElement = erPage - > mainFrame ( ) - > documentElement ( ) ;
2011-04-21 13:28:07 +02:00
QWebElementCollection elements ;
elements . append ( docElement . findAll ( " iframe " ) ) ;
2012-01-23 17:30:34 +01:00
2013-03-06 09:05:41 +01:00
foreach ( QWebElement element , elements ) {
2011-04-21 13:28:07 +02:00
QString src = element . attribute ( " src " ) ;
2011-11-06 17:01:23 +01:00
if ( exOption - > url . toString ( ) . contains ( src ) ) {
2012-01-23 17:30:34 +01:00
element . setStyleProperty ( " visibility " , " hidden " ) ;
2011-11-06 17:01:23 +01:00
}
2011-04-21 13:28:07 +02:00
}
2012-01-23 17:30:34 +01:00
2011-07-31 00:50:40 +02:00
return false ;
2011-11-06 17:01:23 +01:00
}
else { //The whole page is blocked
2011-07-31 00:50:40 +02:00
QString rule = exOption - > errorString ;
2012-09-04 12:42:45 +02:00
rule . remove ( QLatin1String ( " AdBlock: " ) ) ;
2011-07-31 00:50:40 +02:00
2013-01-22 19:04:22 +01:00
QString errString = QzTools : : readAllFileContents ( " :/html/adblockPage.html " ) ;
2012-09-04 12:42:45 +02:00
errString . replace ( QLatin1String ( " %TITLE% " ) , tr ( " AdBlocked Content " ) ) ;
errString . replace ( QLatin1String ( " %IMAGE% " ) , QLatin1String ( " qrc:html/adblock_big.png " ) ) ;
errString . replace ( QLatin1String ( " %FAVICON% " ) , QLatin1String ( " qrc:html/adblock_big.png " ) ) ;
2011-07-31 00:50:40 +02:00
2012-09-04 12:42:45 +02:00
errString . replace ( QLatin1String ( " %RULE% " ) , tr ( " Blocked by <i>%1</i> " ) . arg ( rule ) ) ;
2013-01-22 19:04:22 +01:00
errString = QzTools : : applyDirectionToPage ( errString ) ;
2011-04-21 13:28:07 +02:00
2012-01-31 21:10:22 +01:00
exReturn - > baseUrl = exOption - > url ;
2012-04-04 14:45:45 +02:00
exReturn - > content = QString ( errString + " <span id= \" qupzilla-error-page \" ></span> " ) . toUtf8 ( ) ;
2012-02-04 18:45:59 +01:00
if ( PopupWebPage * popupPage = qobject_cast < PopupWebPage * > ( exOption - > frame - > page ( ) ) ) {
WebView * view = qobject_cast < WebView * > ( popupPage - > view ( ) ) ;
if ( view ) {
// Closing blocked popup
2013-05-12 22:55:53 +02:00
popupPage - > mainWindow ( ) - > adBlockIcon ( ) - > popupBlocked ( rule , exOption - > url ) ;
2012-02-04 18:45:59 +01:00
view - > closeView ( ) ;
}
}
2011-07-31 00:50:40 +02:00
return true ;
}
2011-04-05 18:45:17 +02:00
}
errorString = tr ( " Content Access Denied " ) ;
break ;
2011-03-02 16:57:41 +01:00
default :
2012-09-05 19:57:36 +02:00
if ( exOption - > errorString ! = QLatin1String ( " QupZilla:No Error " ) ) {
2012-06-24 23:46:32 +02:00
qDebug ( ) < < " Content error: " < < exOption - > errorString < < exOption - > error ;
}
2011-07-31 00:50:40 +02:00
return false ;
2011-03-02 16:57:41 +01:00
}
}
else if ( exOption - > domain = = QWebPage : : Http ) {
2012-04-22 21:23:51 +02:00
// 200 status code = OK
// It shouldn't be reported as an error, but sometimes it is ...
if ( exOption - > error = = 200 ) {
return false ;
}
2011-03-02 16:57:41 +01:00
errorString = tr ( " Error code %1 " ) . arg ( exOption - > error ) ;
}
2011-11-06 17:01:23 +01:00
else if ( exOption - > domain = = QWebPage : : WebKit ) {
return false ; // Downloads
}
2011-03-02 16:57:41 +01:00
2013-12-30 13:43:48 +01:00
const QUrl loadedUrl = exOption - > url ;
2011-03-02 16:57:41 +01:00
exReturn - > baseUrl = loadedUrl ;
QFile file ( " :/html/errorPage.html " ) ;
file . open ( QFile : : ReadOnly ) ;
QString errString = file . readAll ( ) ;
2012-09-04 12:42:45 +02:00
errString . replace ( QLatin1String ( " %TITLE% " ) , tr ( " Failed loading page " ) ) ;
2011-03-02 16:57:41 +01:00
2013-01-22 19:04:22 +01:00
errString . replace ( QLatin1String ( " %IMAGE% " ) , QzTools : : pixmapToByteArray ( qIconProvider - > standardIcon ( QStyle : : SP_MessageBoxWarning ) . pixmap ( 45 , 45 ) ) ) ;
errString . replace ( QLatin1String ( " %FAVICON% " ) , QzTools : : pixmapToByteArray ( qIconProvider - > standardIcon ( QStyle : : SP_MessageBoxWarning ) . pixmap ( 16 , 16 ) ) ) ;
2012-09-04 12:42:45 +02:00
errString . replace ( QLatin1String ( " %BOX-BORDER% " ) , QLatin1String ( " qrc:html/box-border.png " ) ) ;
2011-03-02 16:57:41 +01:00
2012-08-23 16:17:56 +02:00
QString heading2 = loadedUrl . host ( ) . isEmpty ( ) ? tr ( " QupZilla can't load page. " ) : tr ( " QupZilla can't load page from %1. " ) . arg ( loadedUrl . host ( ) ) ;
2012-09-04 12:42:45 +02:00
errString . replace ( QLatin1String ( " %HEADING% " ) , errorString ) ;
errString . replace ( QLatin1String ( " %HEADING2% " ) , heading2 ) ;
errString . replace ( QLatin1String ( " %LI-1% " ) , tr ( " Check the address for typing errors such as <b>ww.</b>example.com instead of <b>www.</b>example.com " ) ) ;
errString . replace ( QLatin1String ( " %LI-2% " ) , tr ( " If you are unable to load any pages, check your computer's network connection. " ) ) ;
errString . replace ( QLatin1String ( " %LI-3% " ) , tr ( " If your computer or network is protected by a firewall or proxy, make sure that QupZilla is permitted to access the Web. " ) ) ;
errString . replace ( QLatin1String ( " %TRY-AGAIN% " ) , tr ( " Try Again " ) ) ;
2013-01-22 19:04:22 +01:00
errString = QzTools : : applyDirectionToPage ( errString ) ;
2011-03-02 16:57:41 +01:00
2012-04-04 14:45:45 +02:00
exReturn - > content = QString ( errString + " <span id= \" qupzilla-error-page \" ></span> " ) . toUtf8 ( ) ;
2011-03-02 16:57:41 +01:00
return true ;
}
2011-05-22 10:47:03 +02:00
bool WebPage : : javaScriptPrompt ( QWebFrame * originatingFrame , const QString & msg , const QString & defaultValue , QString * result )
{
2012-01-21 20:20:48 +01:00
# ifndef NONBLOCK_JS_DIALOGS
return QWebPage : : javaScriptPrompt ( originatingFrame , msg , defaultValue , result ) ;
# else
if ( m_runningLoop ) {
return false ;
}
WebView * webView = qobject_cast < WebView * > ( originatingFrame - > page ( ) - > view ( ) ) ;
ResizableFrame * widget = new ResizableFrame ( webView - > overlayForJsAlert ( ) ) ;
2011-05-22 10:47:03 +02:00
2011-11-24 22:18:45 +01:00
widget - > setObjectName ( " jsFrame " ) ;
2011-05-22 10:47:03 +02:00
Ui_jsPrompt * ui = new Ui_jsPrompt ( ) ;
ui - > setupUi ( widget ) ;
ui - > message - > setText ( msg ) ;
ui - > lineEdit - > setText ( defaultValue ) ;
ui - > lineEdit - > setFocus ( ) ;
widget - > resize ( originatingFrame - > page ( ) - > viewportSize ( ) ) ;
widget - > show ( ) ;
2012-01-21 20:20:48 +01:00
connect ( webView , SIGNAL ( viewportResized ( QSize ) ) , widget , SLOT ( slotResize ( QSize ) ) ) ;
2011-05-22 10:47:03 +02:00
connect ( ui - > lineEdit , SIGNAL ( returnPressed ( ) ) , ui - > buttonBox - > button ( QDialogButtonBox : : Ok ) , SLOT ( animateClick ( ) ) ) ;
QEventLoop eLoop ;
2011-12-13 15:38:09 +01:00
m_runningLoop = & eLoop ;
2011-11-06 17:01:23 +01:00
connect ( ui - > buttonBox , SIGNAL ( clicked ( QAbstractButton * ) ) , & eLoop , SLOT ( quit ( ) ) ) ;
2011-12-13 15:38:09 +01:00
2012-04-04 14:45:45 +02:00
if ( eLoop . exec ( ) = = 1 ) {
2011-12-13 15:38:09 +01:00
return result ;
}
m_runningLoop = 0 ;
2011-05-22 10:47:03 +02:00
QString x = ui - > lineEdit - > text ( ) ;
bool _result = ui - > buttonBox - > clickedButtonRole ( ) = = QDialogButtonBox : : AcceptRole ;
* result = x ;
2011-12-13 15:38:09 +01:00
delete widget ;
2012-01-21 20:20:48 +01:00
webView - > setFocus ( ) ;
2011-05-22 10:47:03 +02:00
return _result ;
2012-01-21 20:20:48 +01:00
# endif
2011-05-22 10:47:03 +02:00
}
bool WebPage : : javaScriptConfirm ( QWebFrame * originatingFrame , const QString & msg )
{
2012-01-21 20:20:48 +01:00
# ifndef NONBLOCK_JS_DIALOGS
return QWebPage : : javaScriptConfirm ( originatingFrame , msg ) ;
# else
if ( m_runningLoop ) {
return false ;
}
WebView * webView = qobject_cast < WebView * > ( originatingFrame - > page ( ) - > view ( ) ) ;
ResizableFrame * widget = new ResizableFrame ( webView - > overlayForJsAlert ( ) ) ;
2011-05-22 10:47:03 +02:00
2011-11-24 22:18:45 +01:00
widget - > setObjectName ( " jsFrame " ) ;
2011-05-22 10:47:03 +02:00
Ui_jsConfirm * ui = new Ui_jsConfirm ( ) ;
ui - > setupUi ( widget ) ;
ui - > message - > setText ( msg ) ;
ui - > buttonBox - > button ( QDialogButtonBox : : Ok ) - > setFocus ( ) ;
widget - > resize ( originatingFrame - > page ( ) - > viewportSize ( ) ) ;
widget - > show ( ) ;
2012-01-21 20:20:48 +01:00
connect ( webView , SIGNAL ( viewportResized ( QSize ) ) , widget , SLOT ( slotResize ( QSize ) ) ) ;
2011-05-22 10:47:03 +02:00
QEventLoop eLoop ;
2011-12-13 15:38:09 +01:00
m_runningLoop = & eLoop ;
2011-11-06 17:01:23 +01:00
connect ( ui - > buttonBox , SIGNAL ( clicked ( QAbstractButton * ) ) , & eLoop , SLOT ( quit ( ) ) ) ;
2011-12-13 15:38:09 +01:00
2012-04-04 14:45:45 +02:00
if ( eLoop . exec ( ) = = 1 ) {
2011-12-13 15:38:09 +01:00
return false ;
}
m_runningLoop = 0 ;
2011-05-22 10:47:03 +02:00
bool result = ui - > buttonBox - > clickedButtonRole ( ) = = QDialogButtonBox : : AcceptRole ;
2011-12-13 15:38:09 +01:00
delete widget ;
2012-01-21 20:20:48 +01:00
webView - > setFocus ( ) ;
2011-05-22 10:47:03 +02:00
return result ;
2012-01-21 20:20:48 +01:00
# endif
2011-05-22 10:47:03 +02:00
}
void WebPage : : javaScriptAlert ( QWebFrame * originatingFrame , const QString & msg )
{
2012-02-05 17:13:08 +01:00
Q_UNUSED ( originatingFrame )
2012-01-21 20:20:48 +01:00
if ( m_blockAlerts | | m_runningLoop ) {
2011-05-22 10:47:03 +02:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-05-22 10:47:03 +02:00
2012-01-21 20:20:48 +01:00
# ifndef NONBLOCK_JS_DIALOGS
2012-03-15 19:35:37 +01:00
QString title = tr ( " JavaScript alert " ) ;
if ( ! url ( ) . host ( ) . isEmpty ( ) ) {
title . append ( QString ( " - %1 " ) . arg ( url ( ) . host ( ) ) ) ;
}
CheckBoxDialog dialog ( QDialogButtonBox : : Ok , view ( ) ) ;
dialog . setWindowTitle ( title ) ;
dialog . setText ( msg ) ;
dialog . setCheckBoxText ( tr ( " Prevent this page from creating additional dialogs " ) ) ;
2012-04-22 20:51:28 +02:00
dialog . setIcon ( qIconProvider - > standardIcon ( QStyle : : SP_MessageBoxInformation ) ) ;
2012-03-15 19:35:37 +01:00
dialog . exec ( ) ;
m_blockAlerts = dialog . isChecked ( ) ;
2012-01-21 20:20:48 +01:00
# else
WebView * webView = qobject_cast < WebView * > ( originatingFrame - > page ( ) - > view ( ) ) ;
ResizableFrame * widget = new ResizableFrame ( webView - > overlayForJsAlert ( ) ) ;
2011-05-22 10:47:03 +02:00
2011-11-24 22:18:45 +01:00
widget - > setObjectName ( " jsFrame " ) ;
2011-05-22 10:47:03 +02:00
Ui_jsAlert * ui = new Ui_jsAlert ( ) ;
ui - > setupUi ( widget ) ;
ui - > message - > setText ( msg ) ;
ui - > buttonBox - > button ( QDialogButtonBox : : Ok ) - > setFocus ( ) ;
widget - > resize ( originatingFrame - > page ( ) - > viewportSize ( ) ) ;
widget - > show ( ) ;
2012-01-21 20:20:48 +01:00
connect ( webView , SIGNAL ( viewportResized ( QSize ) ) , widget , SLOT ( slotResize ( QSize ) ) ) ;
2011-05-22 10:47:03 +02:00
QEventLoop eLoop ;
2011-12-13 15:38:09 +01:00
m_runningLoop = & eLoop ;
2011-11-06 17:01:23 +01:00
connect ( ui - > buttonBox , SIGNAL ( clicked ( QAbstractButton * ) ) , & eLoop , SLOT ( quit ( ) ) ) ;
2011-12-13 15:38:09 +01:00
2012-04-04 14:45:45 +02:00
if ( eLoop . exec ( ) = = 1 ) {
2011-12-13 15:38:09 +01:00
return ;
}
m_runningLoop = 0 ;
2011-05-22 10:47:03 +02:00
m_blockAlerts = ui - > preventAlerts - > isChecked ( ) ;
delete widget ;
2012-01-21 20:20:48 +01:00
webView - > setFocus ( ) ;
# endif
2011-05-22 10:47:03 +02:00
}
2014-01-25 17:48:30 +01:00
void WebPage : : setJavaScriptEnabled ( bool enabled )
{
settings ( ) - > setAttribute ( QWebSettings : : JavascriptEnabled , enabled ) ;
m_javaScriptEnabled = enabled ;
}
2011-11-06 17:01:23 +01:00
QString WebPage : : chooseFile ( QWebFrame * originatingFrame , const QString & oldFile )
2011-05-22 11:05:36 +02:00
{
QString suggFileName ;
2012-02-04 19:43:43 +01:00
2011-11-06 17:01:23 +01:00
if ( oldFile . isEmpty ( ) ) {
2012-04-17 18:26:01 +02:00
suggFileName = s_lastUploadLocation ;
2011-11-06 17:01:23 +01:00
}
else {
2011-05-22 11:05:36 +02:00
suggFileName = oldFile ;
2011-11-06 17:01:23 +01:00
}
2012-02-04 19:43:43 +01:00
2013-12-30 13:43:48 +01:00
const QString fileName = QzTools : : getOpenFileName ( " WebPage-ChooseFile " , originatingFrame - > page ( ) - > view ( ) , tr ( " Choose file... " ) , suggFileName ) ;
2011-05-22 11:05:36 +02:00
2011-11-06 17:01:23 +01:00
if ( ! fileName . isEmpty ( ) ) {
2012-04-17 18:26:01 +02:00
s_lastUploadLocation = fileName ;
2013-04-04 20:32:34 +02:00
// Check if we can read from file
QFile file ( fileName ) ;
if ( ! file . open ( QFile : : ReadOnly ) ) {
2013-12-30 13:43:48 +01:00
const QString msg = tr ( " Cannot read data from <b>%1</b>. Upload was cancelled! " ) . arg ( fileName ) ;
2013-04-04 20:32:34 +02:00
QMessageBox : : critical ( view ( ) , tr ( " Cannot read file! " ) , msg ) ;
return QString ( ) ;
}
2011-11-06 17:01:23 +01:00
}
2011-05-22 11:05:36 +02:00
return fileName ;
}
2012-04-03 19:28:12 +02:00
bool WebPage : : isPointerSafeToUse ( WebPage * page )
2012-04-02 19:33:05 +02:00
{
2012-04-03 19:28:12 +02:00
// Pointer to WebPage is passed with every QNetworkRequest casted to void*
// So there is no way to test whether pointer is still valid or not, except
// this hack.
2012-04-17 18:26:01 +02:00
return page = = 0 ? false : s_livingPages . contains ( page ) ;
2012-04-02 19:33:05 +02:00
}
2012-01-14 11:46:06 +01:00
void WebPage : : disconnectObjects ( )
{
2012-01-21 20:20:48 +01:00
if ( m_runningLoop ) {
m_runningLoop - > exit ( 1 ) ;
m_runningLoop = 0 ;
}
2012-04-17 18:26:01 +02:00
s_livingPages . removeOne ( this ) ;
2012-04-02 19:33:05 +02:00
2012-01-14 11:46:06 +01:00
disconnect ( this ) ;
2012-01-21 20:20:48 +01:00
m_networkProxy - > disconnectObjects ( ) ;
2012-04-01 10:48:50 +02:00
mApp - > plugins ( ) - > emitWebPageDeleted ( this ) ;
2012-01-14 11:46:06 +01:00
}
2011-03-02 16:57:41 +01:00
WebPage : : ~ WebPage ( )
{
2011-12-13 15:38:09 +01:00
if ( m_runningLoop ) {
m_runningLoop - > exit ( 1 ) ;
2012-01-21 20:20:48 +01:00
m_runningLoop = 0 ;
2011-12-13 15:38:09 +01:00
}
2012-04-02 19:33:05 +02:00
2012-04-17 18:26:01 +02:00
s_livingPages . removeOne ( this ) ;
2011-03-02 16:57:41 +01:00
}