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 "webpage.h"
2012-01-21 20:20:48 +01:00
# include "tabbedwebview.h"
2011-03-02 16:57:41 +01:00
# include "tabwidget.h"
# include "qupzilla.h"
# include "downloadmanager.h"
# include "webpluginfactory.h"
# include "mainapplication.h"
2012-01-21 20:20:48 +01:00
# ifdef NONBLOCK_JS_DIALOGS
2011-05-22 10:47:03 +02:00
# include "ui_jsconfirm.h"
# include "ui_jsalert.h"
# include "ui_jsprompt.h"
2012-01-21 20:20:48 +01:00
# endif
# include "ui_closedialog.h"
2011-05-22 10:47:03 +02:00
# include "widget.h"
2011-09-18 15:35:44 +02:00
# include "globalfunctions.h"
2011-12-02 23:25:27 +01:00
# include "pluginproxy.h"
2011-12-09 21:56:01 +01:00
# include "speeddial.h"
2012-01-21 20:20:48 +01:00
# include "popupwebpage.h"
# include "popupwebview.h"
# include "networkmanagerproxy.h"
2011-03-02 16:57:41 +01:00
2011-12-11 19:37:16 +01:00
QString WebPage : : UserAgent = " " ;
2011-10-26 19:23:50 +02:00
QString WebPage : : m_lastUploadLocation = QDir : : homePath ( ) ;
2012-01-21 20:20:48 +01:00
WebPage : : WebPage ( QupZilla * mainClass )
: QWebPage ( )
2011-10-26 19:23:50 +02:00
, p_QupZilla ( mainClass )
2012-01-21 20:20:48 +01:00
, m_view ( 0 )
2011-12-02 23:25:27 +01:00
, m_speedDial ( mApp - > plugins ( ) - > speedDial ( ) )
2011-12-15 19:08:33 +01:00
, m_fileWatcher ( 0 )
2011-12-13 15:38:09 +01:00
, m_runningLoop ( 0 )
2011-10-26 19:23:50 +02:00
, m_blockAlerts ( false )
, m_secureStatus ( false )
2012-01-21 20:20:48 +01:00
, m_isClosing ( false )
2011-03-02 16:57:41 +01:00
{
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 ) ) ) ;
2011-12-02 23:25:27 +01:00
connect ( mainFrame ( ) , SIGNAL ( javaScriptWindowObjectCleared ( ) ) , this , SLOT ( addJavaScriptObject ( ) ) ) ;
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-01-21 20:20:48 +01:00
if ( m_view & & m_view - > isLoading ( ) ) {
2011-11-12 09:45:32 +01:00
m_adjustingScheduled = true ;
}
else {
mainFrame ( ) - > setZoomFactor ( mainFrame ( ) - > zoomFactor ( ) + 1 ) ;
mainFrame ( ) - > setZoomFactor ( mainFrame ( ) - > zoomFactor ( ) - 1 ) ;
}
2012-01-21 20:20:48 +01:00
}
2011-11-12 09:45:32 +01:00
2012-01-21 20:20:48 +01:00
bool WebPage : : isRunningLoop ( )
{
return m_runningLoop ;
2011-11-12 09:45:32 +01:00
}
2011-10-09 14:51:25 +02:00
void WebPage : : urlChanged ( const QUrl & url )
{
Q_UNUSED ( url )
m_adBlockedEntries . clear ( ) ;
2011-10-30 16:32:36 +01:00
m_blockAlerts = false ;
2011-07-11 20:30:49 +02:00
}
void WebPage : : progress ( int prog )
{
Q_UNUSED ( prog )
bool secStatus = sslCertificate ( ) . isValid ( ) ;
if ( secStatus ! = m_secureStatus ) {
m_secureStatus = secStatus ;
emit privacyChanged ( sslCertificate ( ) . isValid ( ) ) ;
}
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 ) ;
}
2012-01-21 20:20:48 +01:00
if ( url ( ) . scheme ( ) = = " file " ) {
2011-12-15 19:08:33 +01:00
if ( ! m_fileWatcher ) {
m_fileWatcher = new QFileSystemWatcher ( this ) ;
connect ( m_fileWatcher , SIGNAL ( fileChanged ( QString ) ) , this , SLOT ( watchedFileChanged ( QString ) ) ) ;
}
2012-01-21 20:20:48 +01:00
QString filePath = url ( ) . toLocalFile ( ) ;
2011-12-15 19:08:33 +01:00
2012-01-21 20:20:48 +01:00
if ( QFile : : exists ( filePath ) & & ! m_fileWatcher - > files ( ) . contains ( filePath ) ) {
2011-12-16 12:03:47 +01:00
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 ( ) ) ;
}
2011-10-09 14:51:25 +02:00
QTimer : : singleShot ( 100 , this , SLOT ( 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 ( )
{
2012-01-21 20:20:48 +01:00
if ( url ( ) . toString ( ) ! = " qupzilla:speeddial " ) {
2011-12-02 23:25:27 +01:00
return ;
}
mainFrame ( ) - > addToJavaScriptWindowObject ( " speeddial " , m_speedDial ) ;
m_speedDial - > addWebFrame ( mainFrame ( ) ) ;
}
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
}
2011-03-02 16:57:41 +01:00
QUrl url = reply - > url ( ) ;
2011-11-06 17:01:23 +01:00
switch ( reply - > error ( ) ) {
2011-03-02 16:57:41 +01:00
case QNetworkReply : : NoError :
if ( reply - > header ( QNetworkRequest : : ContentTypeHeader ) . isValid ( ) ) {
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 ;
}
break ;
case QNetworkReply : : ProtocolUnknownError :
qDebug ( ) < < url < < " ProtocolUnknowError " ;
QDesktopServices : : openUrl ( url ) ;
return ;
break ;
default :
break ;
}
2011-03-24 16:51:19 +01:00
qDebug ( ) < < " WebPage::UnsupportedContent error " < < reply - > errorString ( ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
void WebPage : : downloadRequested ( const QNetworkRequest & request )
{
DownloadManager * dManager = mApp - > downManager ( ) ;
dManager - > download ( request , this ) ;
}
2011-04-24 09:08:53 +02:00
void WebPage : : setSSLCertificate ( const QSslCertificate & cert )
2011-03-22 21:36:15 +01:00
{
2012-01-21 20:20:48 +01:00
// if (cert != m_SslCert)
2011-07-11 20:30:49 +02:00
m_SslCert = cert ;
2011-03-22 21:36:15 +01:00
}
2011-04-04 16:00:27 +02:00
QSslCertificate WebPage : : sslCertificate ( )
{
2012-01-21 20:20:48 +01:00
if ( url ( ) . scheme ( ) = = " https " & &
m_SslCert . subjectInfo ( QSslCertificate : : CommonName ) . remove ( " * " ) . contains ( QRegExp ( url ( ) . host ( ) ) ) ) {
2011-04-04 16:29:25 +02:00
return m_SslCert ;
2011-11-06 17:01:23 +01:00
}
else {
2011-04-04 16:29:25 +02:00
return QSslCertificate ( ) ;
2011-11-06 17:01:23 +01:00
}
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_lastRequest = request ;
m_lastRequestType = type ;
QString scheme = request . url ( ) . scheme ( ) ;
if ( scheme = = " mailto " | | scheme = = " ftp " ) {
QDesktopServices : : openUrl ( request . url ( ) ) ;
return false ;
}
if ( type = = QWebPage : : NavigationTypeFormResubmitted ) {
2012-01-21 20:20:48 +01:00
QString message = tr ( " To show this page, QupZilla must resend request which do it again \n "
" (like searching on making an shoping, 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
QVariant variant = qVariantFromValue ( ( void * ) pagePointer ) ;
2011-03-02 16:57:41 +01:00
request . setAttribute ( ( QNetworkRequest : : Attribute ) ( QNetworkRequest : : User + 100 ) , variant ) ;
request . setAttribute ( ( QNetworkRequest : : Attribute ) ( QNetworkRequest : : User + 101 ) , m_lastRequestType ) ;
}
QWebPage * WebPage : : createWindow ( QWebPage : : WebWindowType type )
{
2012-01-21 20:20:48 +01:00
#if 0
2011-03-02 16:57:41 +01:00
Q_UNUSED ( type ) ;
2012-01-18 16:52:30 +01:00
int index = p_QupZilla - > tabWidget ( ) - > addView ( QUrl ( ) , TabWidget : : CleanSelectedPage ) ;
2011-03-02 16:57:41 +01:00
return p_QupZilla - > weView ( index ) - > page ( ) ;
2012-01-21 20:20:48 +01:00
# endif
return new PopupWebPage ( type , p_QupZilla ) ;
2011-03-02 16:57:41 +01:00
}
2011-03-29 20:30:05 +02:00
void WebPage : : addAdBlockRule ( const QString & filter , const QUrl & url )
{
AdBlockedEntry entry ;
entry . rule = filter ;
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
}
void WebPage : : cleanBlockedObjects ( )
{
QStringList findingStrings ;
2011-11-06 17:01:23 +01:00
foreach ( AdBlockedEntry entry , m_adBlockedEntries ) {
if ( entry . url . toString ( ) . endsWith ( " .js " ) ) {
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
findingStrings . append ( entry . url . toString ( ) ) ;
2012-01-21 20:20:48 +01:00
QUrl mainFrameUrl = url ( ) ;
2011-10-09 14:51:25 +02:00
if ( entry . url . scheme ( ) = = mainFrameUrl . scheme ( ) & & entry . url . host ( ) = = mainFrameUrl . host ( ) ) {
//May be relative url
QString relativeUrl = qz_makeRelativeUrl ( mainFrameUrl , entry . url ) . toString ( ) ;
findingStrings . append ( relativeUrl ) ;
2011-11-06 17:01:23 +01:00
if ( relativeUrl . startsWith ( " / " ) ) {
2011-10-09 14:51:25 +02:00
findingStrings . append ( relativeUrl . right ( relativeUrl . size ( ) - 1 ) ) ;
2011-11-06 17:01:23 +01:00
}
2011-10-09 14:51:25 +02:00
}
}
2011-08-04 21:37:04 +02:00
QWebElement docElement = mainFrame ( ) - > documentElement ( ) ;
QWebElementCollection elements ;
2011-11-06 17:01:23 +01:00
foreach ( QString s , findingStrings )
elements . append ( docElement . findAll ( " *[src= \" " + s + " \" ] " ) ) ;
foreach ( QWebElement element , elements )
element . setAttribute ( " style " , " display:none; " ) ;
2011-03-29 20:30:05 +02:00
}
2011-12-11 19:37:16 +01:00
QString WebPage : : userAgentForUrl ( const QUrl & url ) const
{
if ( UserAgent . isEmpty ( ) ) {
UserAgent = QWebPage : : userAgentForUrl ( url ) ;
2012-01-16 16:28:41 +01:00
# ifdef Q_WS_MAC
# ifdef __i386__ || __x86_64__
UserAgent . replace ( " PPC Mac OS X " , " Intel Mac OS X " ) ;
# endif
# endif
2011-12-11 19:37:16 +01:00
}
return UserAgent ;
}
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 ( ) ) {
suggestedFileName = exOption - > suggestedFileNames . first ( ) ;
}
exReturn - > fileNames = QFileDialog : : getOpenFileNames ( 0 , tr ( " Select files to upload... " ) , suggestedFileName ) ;
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 :
errorString = tr ( " Proxy host name not found " ) ;
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 ;
2011-04-05 18:45:17 +02:00
case QNetworkReply : : ContentAccessDenied :
if ( exOption - > errorString . startsWith ( " AdBlockRule " ) ) {
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 " ) ) ;
2011-11-06 17:01:23 +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 ) ) {
2011-04-21 13:28:07 +02:00
element . setAttribute ( " style " , " display:none; " ) ;
2011-11-06 17:01:23 +01:00
}
2011-04-21 13:28:07 +02: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 ;
rule . remove ( " AdBlockRule: " ) ;
QFile file ( " :/html/adblockPage.html " ) ;
file . open ( QFile : : ReadOnly ) ;
QString errString = file . readAll ( ) ;
errString . replace ( " %TITLE% " , tr ( " AdBlocked Content " ) ) ;
2011-12-03 14:43:13 +01:00
errString . replace ( " %IMAGE% " , " qrc:html/adblock_big.png " ) ;
errString . replace ( " %FAVICON% " , " qrc:html/adblock_big.png " ) ;
2011-07-31 00:50:40 +02:00
errString . replace ( " %RULE% " , tr ( " Blocked by rule <i>%1</i> " ) . arg ( rule ) ) ;
2011-04-21 13:28:07 +02:00
2011-07-31 00:50:40 +02:00
exReturn - > baseUrl = exOption - > url . toString ( ) ;
exReturn - > content = errString . toUtf8 ( ) ;
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 :
2011-07-31 00:50:40 +02:00
qDebug ( ) < < " Content error: " < < exOption - > errorString ;
return false ;
2011-03-02 16:57:41 +01:00
}
}
else if ( exOption - > domain = = QWebPage : : Http ) {
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
QString loadedUrl = exOption - > url . toString ( ) ;
exReturn - > baseUrl = loadedUrl ;
QFile file ( " :/html/errorPage.html " ) ;
file . open ( QFile : : ReadOnly ) ;
QString errString = file . readAll ( ) ;
errString . replace ( " %TITLE% " , tr ( " Failed loading page " ) ) ;
2011-11-06 17:01:23 +01:00
errString . replace ( " %IMAGE% " , qz_pixmapToByteArray ( MainApplication : : style ( ) - > standardIcon ( QStyle : : SP_MessageBoxWarning ) . pixmap ( 45 , 45 ) ) ) ;
errString . replace ( " %FAVICON% " , qz_pixmapToByteArray ( MainApplication : : style ( ) - > standardIcon ( QStyle : : SP_MessageBoxWarning ) . pixmap ( 16 , 16 ) ) ) ;
2011-12-02 23:25:27 +01:00
errString . replace ( " %BOX-BORDER% " , " qrc:html/box-border.png " ) ;
2011-03-02 16:57:41 +01:00
errString . replace ( " %HEADING% " , errorString ) ;
2011-03-26 13:34:08 +01:00
errString . replace ( " %HEADING2% " , tr ( " QupZilla can't load page from %1. " ) . arg ( QUrl ( loadedUrl ) . host ( ) ) ) ;
2011-03-02 16:57:41 +01:00
errString . replace ( " %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 ( " %LI-2% " , tr ( " If you are unable to load any pages, check your computer's network connection. " ) ) ;
errString . replace ( " %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 ( " %TRY-AGAIN% " , tr ( " Try Again " ) ) ;
exReturn - > content = errString . toUtf8 ( ) ;
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-01-21 20:20:48 +01:00
if ( eLoop . exec ( ) = = 1 | | m_isClosing ) {
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-01-21 20:20:48 +01:00
if ( eLoop . exec ( ) = = 1 | | m_isClosing ) {
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-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
QDialog dialog ( view ( ) ) ;
Ui_CloseDialog * ui = new Ui_CloseDialog ( ) ;
ui - > setupUi ( & dialog ) ;
ui - > buttonBox - > setStandardButtons ( QDialogButtonBox : : Ok ) ;
ui - > dontAskAgain - > setText ( tr ( " Prevent this page from creating additional dialogs " ) ) ;
ui - > textLabel - > setText ( Qt : : escape ( msg ) ) ;
ui - > iconLabel - > setPixmap ( mApp - > style ( ) - > standardPixmap ( QStyle : : SP_MessageBoxInformation ) ) ;
dialog . setWindowTitle ( tr ( " JavaScript alert - %1 " ) . arg ( originatingFrame - > url ( ) . host ( ) ) ) ;
dialog . exec ( ) ;
if ( ui - > dontAskAgain - > isChecked ( ) ) {
m_blockAlerts = true ;
}
# 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-01-21 20:20:48 +01:00
if ( eLoop . exec ( ) = = 1 | | m_isClosing ) {
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
}
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 ;
2011-11-06 17:01:23 +01:00
if ( oldFile . isEmpty ( ) ) {
2011-05-22 11:05:36 +02:00
suggFileName = m_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
}
2011-05-22 11:05:36 +02:00
QString fileName = QFileDialog : : getOpenFileName ( originatingFrame - > page ( ) - > view ( ) , tr ( " Choose file... " ) , suggFileName ) ;
2011-11-06 17:01:23 +01:00
if ( ! fileName . isEmpty ( ) ) {
2011-05-22 11:05:36 +02:00
m_lastUploadLocation = fileName ;
2011-11-06 17:01:23 +01:00
}
2011-05-22 11:05:36 +02:00
return fileName ;
}
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-01-14 11:46:06 +01:00
disconnect ( this ) ;
2012-01-21 20:20:48 +01:00
m_networkProxy - > disconnectObjects ( ) ;
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
}
2011-03-02 16:57:41 +01:00
}