2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2014-01-05 00:57:47 +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/>.
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
2013-01-17 15:24:30 +01:00
2011-03-02 16:57:41 +01:00
# include "webview.h"
# include "webpage.h"
# include "mainapplication.h"
2013-01-22 19:04:22 +01:00
# include "qztools.h"
2011-04-25 20:56:45 +02:00
# include "iconprovider.h"
2012-04-30 16:33:14 +02:00
# include "history.h"
2013-01-17 15:24:30 +01:00
# include "pluginproxy.h"
2012-01-21 20:20:48 +01:00
# include "downloadmanager.h"
# include "sourceviewer.h"
# include "siteinfo.h"
2011-10-21 23:26:34 +02:00
# include "searchenginesmanager.h"
2012-01-22 11:49:58 +01:00
# include "browsinglibrary.h"
2014-02-08 23:01:01 +01:00
# include "bookmarkstools.h"
2012-01-24 15:16:33 +01:00
# include "settings.h"
2012-08-10 21:16:43 +02:00
# include "qzsettings.h"
2012-01-24 23:28:18 +01:00
# include "enhancedmenu.h"
2011-03-02 16:57:41 +01:00
2013-02-05 20:43:53 +01:00
# ifdef USE_HUNSPELL
# include "qtwebkit/spellcheck/speller.h"
# endif
2013-05-04 15:15:43 +02:00
# ifdef Q_OS_MAC
# include "macwebviewscroller.h"
# endif
2012-02-29 18:33:50 +01:00
# include <QDir>
2012-03-30 13:43:47 +02:00
# include <QTimer>
2012-02-29 18:33:50 +01:00
# include <QDesktopServices>
# include <QNetworkRequest>
# include <QWebHistory>
# include <QWebFrame>
# include <QClipboard>
2012-08-31 15:20:00 +02:00
# include <QTouchEvent>
2012-02-29 18:33:50 +01:00
# include <QPrintPreviewDialog>
2012-01-21 20:20:48 +01:00
WebView : : WebView ( QWidget * parent )
: QWebView ( parent )
2011-10-30 17:20:22 +01:00
, m_currentZoom ( 100 )
, m_isLoading ( false )
2012-01-21 20:20:48 +01:00
, m_progress ( 0 )
2012-01-22 11:49:58 +01:00
, m_clickedFrame ( 0 )
2012-04-03 19:28:12 +02:00
, m_page ( 0 )
2012-04-01 10:48:50 +02:00
, m_actionReload ( 0 )
, m_actionStop ( 0 )
, m_actionsInitialized ( false )
2012-08-31 15:20:00 +02:00
, m_disableTouchMocking ( false )
2012-12-08 21:18:30 +01:00
, m_isReloading ( false )
2013-02-10 14:09:11 +01:00
, m_hasRss ( false )
, m_rssChecked ( false )
2011-03-02 16:57:41 +01:00
{
2012-01-21 20:20:48 +01:00
connect ( this , SIGNAL ( loadStarted ( ) ) , this , SLOT ( slotLoadStarted ( ) ) ) ;
connect ( this , SIGNAL ( loadProgress ( int ) ) , this , SLOT ( slotLoadProgress ( int ) ) ) ;
connect ( this , SIGNAL ( loadFinished ( bool ) ) , this , SLOT ( slotLoadFinished ( ) ) ) ;
connect ( this , SIGNAL ( iconChanged ( ) ) , this , SLOT ( slotIconChanged ( ) ) ) ;
2012-08-31 15:20:00 +02:00
connect ( this , SIGNAL ( urlChanged ( QUrl ) ) , this , SLOT ( slotUrlChanged ( QUrl ) ) ) ;
2011-10-24 17:46:45 +02:00
2011-10-29 18:37:28 +02:00
// Zoom levels same as in firefox
2011-03-02 16:57:41 +01:00
m_zoomLevels < < 30 < < 50 < < 67 < < 80 < < 90 < < 100 < < 110 < < 120 < < 133 < < 150 < < 170 < < 200 < < 240 < < 300 ;
2011-11-05 21:27:01 +01:00
2013-01-26 21:58:09 +01:00
# if QTWEBKIT_TO_2_3
2012-03-26 17:47:05 +02:00
installEventFilter ( this ) ;
2013-01-26 21:58:09 +01:00
# endif
2013-05-04 15:15:43 +02:00
# ifdef Q_OS_MAC
new MacWebViewScroller ( this ) ;
# endif
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
QIcon WebView : : icon ( ) const
2011-03-02 16:57:41 +01:00
{
2012-09-04 12:42:45 +02:00
if ( url ( ) . scheme ( ) = = QLatin1String ( " qupzilla " ) ) {
2012-01-21 20:20:48 +01:00
return QIcon ( " :icons/qupzilla.png " ) ;
2011-11-06 17:01:23 +01:00
}
2011-07-11 20:30:49 +02:00
2012-09-04 12:42:45 +02:00
if ( url ( ) . scheme ( ) = = QLatin1String ( " file " ) ) {
2014-03-07 18:03:42 +01:00
return IconProvider : : standardIcon ( QStyle : : SP_DriveHDIcon ) ;
2012-08-23 16:17:56 +02:00
}
2013-01-29 14:23:10 +01:00
if ( url ( ) . scheme ( ) = = QLatin1String ( " ftp " ) ) {
2014-03-07 18:03:42 +01:00
return IconProvider : : standardIcon ( QStyle : : SP_ComputerIcon ) ;
2013-01-29 14:23:10 +01:00
}
2012-01-21 20:20:48 +01:00
if ( ! QWebView : : icon ( ) . isNull ( ) ) {
return QWebView : : icon ( ) ;
2011-07-11 20:30:49 +02:00
}
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
if ( ! m_siteIcon . isNull ( ) & & m_siteIconUrl . host ( ) = = url ( ) . host ( ) ) {
return m_siteIcon ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
2014-03-07 18:03:42 +01:00
return IconProvider : : iconForUrl ( url ( ) ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
QString WebView : : title ( ) const
2011-12-04 16:54:57 +01:00
{
2012-01-21 20:20:48 +01:00
QString title = QWebView : : title ( ) ;
2011-12-04 16:54:57 +01:00
2012-01-21 20:20:48 +01:00
if ( title . isEmpty ( ) ) {
title = url ( ) . toString ( QUrl : : RemoveFragment ) ;
2011-12-04 16:54:57 +01:00
}
2012-09-04 12:42:45 +02:00
if ( title . isEmpty ( ) | | title = = QLatin1String ( " about:blank " ) ) {
2012-01-21 20:20:48 +01:00
return tr ( " No Named Page " ) ;
2011-12-04 16:54:57 +01:00
}
2012-01-21 20:20:48 +01:00
return title ;
2011-12-04 16:54:57 +01:00
}
2012-01-21 20:20:48 +01:00
QUrl WebView : : url ( ) const
2011-03-02 16:57:41 +01:00
{
2012-04-03 19:28:12 +02:00
QUrl returnUrl = page ( ) - > url ( ) ;
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
if ( returnUrl . isEmpty ( ) ) {
returnUrl = m_aboutToLoadUrl ;
2011-03-02 16:57:41 +01:00
}
2011-12-20 18:58:42 +01:00
2012-09-04 12:42:45 +02:00
if ( returnUrl . toString ( ) = = QLatin1String ( " about:blank " ) ) {
2012-05-13 22:41:10 +02:00
returnUrl = QUrl ( ) ;
}
2012-01-21 20:20:48 +01:00
return returnUrl ;
2011-03-02 16:57:41 +01:00
}
2012-04-03 19:28:12 +02:00
WebPage * WebView : : page ( ) const
{
return m_page ;
}
2012-01-27 23:01:17 +01:00
void WebView : : setPage ( QWebPage * page )
2012-01-24 19:58:20 +01:00
{
2012-07-06 14:47:34 +02:00
if ( m_page = = page ) {
return ;
}
2012-01-24 19:58:20 +01:00
QWebView : : setPage ( page ) ;
2012-04-03 19:28:12 +02:00
m_page = qobject_cast < WebPage * > ( page ) ;
2012-01-24 19:58:20 +01:00
2012-08-10 21:16:43 +02:00
setZoom ( qzSettings - > defaultZoom ) ;
2013-03-06 09:05:41 +01:00
connect ( m_page , SIGNAL ( saveFrameStateRequested ( QWebFrame * , QWebHistoryItem * ) ) , this , SLOT ( frameStateChanged ( ) ) ) ;
2012-04-03 20:23:15 +02:00
connect ( m_page , SIGNAL ( privacyChanged ( bool ) ) , this , SIGNAL ( privacyChanged ( bool ) ) ) ;
2012-04-01 10:48:50 +02:00
2012-04-03 19:28:12 +02:00
mApp - > plugins ( ) - > emitWebPageCreated ( m_page ) ;
2012-12-06 22:23:11 +01:00
2013-06-06 13:14:12 +02:00
// Set white background by default.
// Fixes issue with dark themes. See #602
2012-12-06 22:23:11 +01:00
QPalette pal = palette ( ) ;
pal . setBrush ( QPalette : : Base , Qt : : white ) ;
page - > setPalette ( pal ) ;
2012-01-24 19:58:20 +01:00
}
2012-01-21 20:20:48 +01:00
void WebView : : load ( const QUrl & url )
2011-03-02 16:57:41 +01:00
{
2012-03-10 13:57:50 +01:00
load ( QNetworkRequest ( url ) ) ;
}
void WebView : : load ( const QNetworkRequest & request , QNetworkAccessManager : : Operation operation , const QByteArray & body )
{
2013-12-30 13:43:48 +01:00
const QUrl reqUrl = request . url ( ) ;
2012-03-10 13:57:50 +01:00
2012-09-04 12:42:45 +02:00
if ( reqUrl . scheme ( ) = = QLatin1String ( " javascript " ) ) {
2012-01-29 17:46:02 +01:00
// Getting scriptSource from PercentEncoding to properly load bookmarklets
2013-01-28 23:10:49 +01:00
// First check if url is percent encoded (let's just look for space)
QString scriptSource ;
if ( reqUrl . path ( ) . trimmed ( ) . contains ( ' ' ) ) {
scriptSource = reqUrl . toString ( ) . mid ( 11 ) ;
}
else {
scriptSource = QUrl : : fromPercentEncoding ( reqUrl . toString ( ) . mid ( 11 ) . toUtf8 ( ) ) ;
}
2012-01-29 17:46:02 +01:00
page ( ) - > mainFrame ( ) - > evaluateJavaScript ( scriptSource ) ;
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
2012-05-13 22:41:10 +02:00
if ( reqUrl . isEmpty ( ) | | isUrlValid ( reqUrl ) ) {
2012-03-10 13:57:50 +01:00
QWebView : : load ( request , operation , body ) ;
2012-05-13 22:41:10 +02:00
m_aboutToLoadUrl = reqUrl ;
2011-10-15 16:37:32 +02:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-10-15 16:37:32 +02:00
2013-11-09 13:32:03 +01:00
SearchEnginesManager : : SearchResult res = mApp - > searchEnginesManager ( ) - > searchResult ( reqUrl . toString ( ) ) ;
2013-12-30 13:43:48 +01:00
const QUrl searchUrl = res . request . url ( ) ;
2013-11-09 13:32:03 +01:00
QWebView : : load ( res . request , res . operation , res . data ) ;
2012-01-21 20:20:48 +01:00
m_aboutToLoadUrl = searchUrl ;
2011-03-02 16:57:41 +01:00
}
2012-04-04 14:45:45 +02:00
bool WebView : : loadingError ( ) const
{
return page ( ) - > loadingError ( ) ;
}
2012-01-21 20:20:48 +01:00
bool WebView : : isLoading ( ) const
2011-04-04 17:03:41 +02:00
{
2012-01-21 20:20:48 +01:00
return m_isLoading ;
2011-04-04 17:03:41 +02:00
}
2012-06-27 18:29:00 +02:00
int WebView : : loadingProgress ( ) const
2011-03-02 16:57:41 +01:00
{
2012-01-21 20:20:48 +01:00
return m_progress ;
2011-03-02 16:57:41 +01:00
}
2012-06-27 18:29:00 +02:00
void WebView : : fakeLoadingProgress ( int progress )
{
emit loadStarted ( ) ;
emit loadProgress ( progress ) ;
}
2013-02-10 14:09:11 +01:00
bool WebView : : hasRss ( ) const
{
return m_hasRss ;
2013-04-07 20:19:21 +02:00
}
QWebElement WebView : : activeElement ( ) const
{
QRect activeRect = inputMethodQuery ( Qt : : ImMicroFocus ) . toRect ( ) ;
return page ( ) - > mainFrame ( ) - > hitTestContent ( activeRect . center ( ) ) . element ( ) ;
2013-02-10 14:09:11 +01:00
}
2014-02-11 22:25:32 +01:00
bool WebView : : onBeforeUnload ( )
{
const QString res = page ( ) - > mainFrame ( ) - > evaluateJavaScript ( " window.onbeforeunload(new Event( \" beforeunload \" )) " ) . toString ( ) ;
if ( ! res . isEmpty ( ) ) {
return page ( ) - > javaScriptConfirm ( page ( ) - > mainFrame ( ) , res ) ;
}
return true ;
}
2012-01-21 20:20:48 +01:00
bool WebView : : isUrlValid ( const QUrl & url )
2011-03-02 16:57:41 +01:00
{
2013-12-05 19:52:13 +01:00
// Valid url must have scheme and actually contains something (therefore scheme:// is invalid)
2013-12-22 00:44:49 +01:00
return url . isValid ( ) & & ! url . scheme ( ) . isEmpty ( ) & & ( ! url . host ( ) . isEmpty ( ) | | ! url . path ( ) . isEmpty ( ) | | url . hasQuery ( ) ) ;
2011-03-02 16:57:41 +01:00
}
QUrl WebView : : guessUrlFromString ( const QString & string )
{
QString trimmedString = string . trimmed ( ) ;
// Check the most common case of a valid url with scheme and host first
QUrl url = QUrl : : fromEncoded ( trimmedString . toUtf8 ( ) , QUrl : : TolerantMode ) ;
2011-11-06 17:01:23 +01:00
if ( url . isValid ( ) & & ! url . scheme ( ) . isEmpty ( ) & & ! url . host ( ) . isEmpty ( ) ) {
2011-03-02 16:57:41 +01:00
return url ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
// Absolute files that exists
2011-11-06 17:01:23 +01:00
if ( QDir : : isAbsolutePath ( trimmedString ) & & QFile : : exists ( trimmedString ) ) {
2011-03-02 16:57:41 +01:00
return QUrl : : fromLocalFile ( trimmedString ) ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
// If the string is missing the scheme or the scheme is not valid prepend a scheme
QString scheme = url . scheme ( ) ;
if ( scheme . isEmpty ( ) | | scheme . contains ( QLatin1Char ( ' . ' ) ) | | scheme = = QLatin1String ( " localhost " ) ) {
// Do not do anything for strings such as "foo", only "foo.com"
int dotIndex = trimmedString . indexOf ( QLatin1Char ( ' . ' ) ) ;
if ( dotIndex ! = - 1 | | trimmedString . startsWith ( QLatin1String ( " localhost " ) ) ) {
const QString hostscheme = trimmedString . left ( dotIndex ) . toLower ( ) ;
QByteArray scheme = ( hostscheme = = QLatin1String ( " ftp " ) ) ? " ftp " : " http " ;
trimmedString = QLatin1String ( scheme ) + QLatin1String ( " :// " ) + trimmedString ;
}
url = QUrl : : fromEncoded ( trimmedString . toUtf8 ( ) , QUrl : : TolerantMode ) ;
}
2011-11-06 17:01:23 +01:00
if ( url . isValid ( ) ) {
2011-03-02 16:57:41 +01:00
return url ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
return QUrl ( ) ;
}
2012-01-21 20:20:48 +01:00
void WebView : : addNotification ( QWidget * notif )
2011-03-03 14:25:02 +01:00
{
2012-01-21 20:20:48 +01:00
emit showNotification ( notif ) ;
2011-03-03 14:25:02 +01:00
}
2012-01-21 20:20:48 +01:00
void WebView : : applyZoom ( )
2011-03-02 16:57:41 +01:00
{
2012-01-21 20:20:48 +01:00
setZoomFactor ( qreal ( m_currentZoom ) / 100.0 ) ;
}
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
void WebView : : zoomIn ( )
{
int i = m_zoomLevels . indexOf ( m_currentZoom ) ;
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
if ( i < m_zoomLevels . count ( ) - 1 ) {
m_currentZoom = m_zoomLevels [ i + 1 ] ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
applyZoom ( ) ;
}
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
void WebView : : zoomOut ( )
{
int i = m_zoomLevels . indexOf ( m_currentZoom ) ;
if ( i > 0 ) {
m_currentZoom = m_zoomLevels [ i - 1 ] ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
applyZoom ( ) ;
}
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
void WebView : : zoomReset ( )
{
m_currentZoom = 100 ;
applyZoom ( ) ;
}
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
void WebView : : reload ( )
{
2012-12-08 21:18:30 +01:00
m_isReloading = true ;
2012-01-21 20:20:48 +01:00
if ( QWebView : : url ( ) . isEmpty ( ) & & ! m_aboutToLoadUrl . isEmpty ( ) ) {
load ( m_aboutToLoadUrl ) ;
return ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
QWebView : : reload ( ) ;
}
2012-01-09 12:39:34 +01:00
2012-01-21 20:20:48 +01:00
void WebView : : back ( )
{
QWebHistory * history = page ( ) - > history ( ) ;
2012-01-09 12:39:34 +01:00
2012-04-04 13:21:53 +02:00
if ( history - > canGoBack ( ) ) {
history - > back ( ) ;
2011-10-21 23:26:34 +02:00
2012-01-21 20:20:48 +01:00
emit urlChanged ( url ( ) ) ;
emit iconChanged ( ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
}
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
void WebView : : forward ( )
{
QWebHistory * history = page ( ) - > history ( ) ;
2012-04-04 13:21:53 +02:00
if ( history - > canGoForward ( ) ) {
history - > forward ( ) ;
2012-01-21 20:20:48 +01:00
emit urlChanged ( url ( ) ) ;
emit iconChanged ( ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
}
2011-03-02 16:57:41 +01:00
2013-03-18 15:53:16 +01:00
void WebView : : editDelete ( )
{
QKeyEvent ev ( QEvent : : KeyPress , Qt : : Key_Delete , Qt : : NoModifier ) ;
QApplication : : sendEvent ( this , & ev ) ;
}
2012-01-21 20:20:48 +01:00
void WebView : : selectAll ( )
{
triggerPageAction ( QWebPage : : SelectAll ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
void WebView : : slotLoadStarted ( )
2011-07-21 17:16:53 +02:00
{
2012-01-21 20:20:48 +01:00
m_isLoading = true ;
m_progress = 0 ;
2012-04-01 10:48:50 +02:00
if ( m_actionsInitialized ) {
m_actionStop - > setEnabled ( true ) ;
m_actionReload - > setEnabled ( false ) ;
}
2013-02-10 14:09:11 +01:00
m_rssChecked = false ;
emit rssChanged ( false ) ;
2011-07-21 17:16:53 +02:00
}
2012-01-21 20:20:48 +01:00
void WebView : : slotLoadProgress ( int progress )
2011-03-02 16:57:41 +01:00
{
2012-01-21 20:20:48 +01:00
m_progress = progress ;
2013-02-10 14:09:11 +01:00
if ( m_progress > 60 ) {
checkRss ( ) ;
}
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
void WebView : : slotLoadFinished ( )
2011-03-02 16:57:41 +01:00
{
2012-01-21 20:20:48 +01:00
m_isLoading = false ;
m_progress = 100 ;
2012-04-01 10:48:50 +02:00
if ( m_actionsInitialized ) {
m_actionStop - > setEnabled ( false ) ;
m_actionReload - > setEnabled ( true ) ;
}
2012-12-08 21:18:30 +01:00
if ( ! m_isReloading ) {
2012-01-21 20:20:48 +01:00
mApp - > history ( ) - > addHistoryEntry ( this ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
2012-12-08 21:18:30 +01:00
m_isReloading = false ;
2012-01-21 20:20:48 +01:00
m_lastUrl = url ( ) ;
}
2012-03-30 13:43:47 +02:00
void WebView : : frameStateChanged ( )
{
// QWebFrame::baseUrl() is not updated yet, so we are invoking 0 second timer
QTimer : : singleShot ( 0 , this , SLOT ( emitChangedUrl ( ) ) ) ;
}
void WebView : : emitChangedUrl ( )
{
emit urlChanged ( url ( ) ) ;
}
2013-02-10 14:09:11 +01:00
void WebView : : checkRss ( )
{
if ( m_rssChecked ) {
return ;
}
m_rssChecked = true ;
QWebFrame * frame = page ( ) - > mainFrame ( ) ;
2013-12-30 13:43:48 +01:00
const QWebElementCollection links = frame - > findAllElements ( " link[type= \" application/rss+xml \" ] " ) ;
2013-02-10 14:09:11 +01:00
m_hasRss = links . count ( ) ! = 0 ;
emit rssChanged ( m_hasRss ) ;
}
2012-01-21 20:20:48 +01:00
void WebView : : slotIconChanged ( )
{
2012-04-04 14:45:45 +02:00
if ( ! loadingError ( ) ) {
m_siteIcon = icon ( ) ;
m_siteIconUrl = url ( ) ;
2014-03-07 18:03:42 +01:00
IconProvider : : instance ( ) - > saveIcon ( this ) ;
2012-04-04 14:45:45 +02:00
}
2011-03-02 16:57:41 +01:00
}
2012-08-31 15:20:00 +02:00
void WebView : : slotUrlChanged ( const QUrl & url )
{
2012-11-28 09:43:47 +01:00
static QStringList exceptions ;
2012-11-28 11:27:30 +01:00
if ( exceptions . isEmpty ( ) ) {
2012-11-28 09:43:47 +01:00
exceptions < < " google. " < < " twitter. " ;
2012-11-28 11:27:30 +01:00
}
2012-11-28 09:43:47 +01:00
// Disable touch mocking on pages known not to work properly
2013-12-30 13:43:48 +01:00
const QString host = url . host ( ) ;
2012-11-28 09:43:47 +01:00
m_disableTouchMocking = false ;
2013-03-06 09:05:41 +01:00
foreach ( const QString & site , exceptions ) {
2012-11-28 09:43:47 +01:00
if ( host . contains ( site ) ) {
m_disableTouchMocking = true ;
}
2012-08-31 15:20:00 +02:00
}
}
2011-03-02 16:57:41 +01:00
void WebView : : openUrlInNewWindow ( )
{
2011-03-17 17:03:04 +01:00
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
2014-03-10 00:47:07 +01:00
mApp - > createWindow ( Qz : : BW_NewWindow , action - > data ( ) . toUrl ( ) ) ;
2011-03-02 16:57:41 +01:00
}
}
void WebView : : sendLinkByMail ( )
{
2011-03-17 17:03:04 +01:00
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
2013-12-30 13:43:48 +01:00
const QUrl mailUrl = QUrl : : fromEncoded ( " mailto:%20?body= " + QUrl : : toPercentEncoding ( action - > data ( ) . toUrl ( ) . toEncoded ( ) ) ) ;
2012-03-31 22:16:20 +02:00
QDesktopServices : : openUrl ( mailUrl ) ;
2011-03-02 16:57:41 +01:00
}
}
2012-03-31 22:16:20 +02:00
void WebView : : sendPageByMail ( )
{
2013-12-30 13:43:48 +01:00
const QUrl mailUrl = QUrl : : fromEncoded ( " mailto:%20?body= " + QUrl : : toPercentEncoding ( url ( ) . toEncoded ( ) ) + " &subject= " + QUrl : : toPercentEncoding ( title ( ) ) ) ;
2012-03-31 22:16:20 +02:00
QDesktopServices : : openUrl ( mailUrl ) ;
}
2011-03-02 16:57:41 +01:00
void WebView : : copyLinkToClipboard ( )
{
2011-03-17 17:03:04 +01:00
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
2013-02-23 17:21:33 +01:00
QApplication : : clipboard ( ) - > setText ( action - > data ( ) . toUrl ( ) . toEncoded ( ) ) ;
2011-03-02 16:57:41 +01:00
}
}
2012-08-14 18:53:55 +02:00
void WebView : : savePageAs ( )
2012-03-23 13:58:31 +01:00
{
2013-06-06 11:48:43 +02:00
if ( url ( ) . isEmpty ( ) | | url ( ) . toString ( ) = = QLatin1String ( " about:blank " ) ) {
return ;
}
2012-03-23 13:58:31 +01:00
QNetworkRequest request ( url ( ) ) ;
2013-01-22 19:04:22 +01:00
QString suggestedFileName = QzTools : : getFileNameFromUrl ( url ( ) ) ;
2012-09-04 12:42:45 +02:00
if ( ! suggestedFileName . contains ( QLatin1Char ( ' . ' ) ) ) {
suggestedFileName . append ( QLatin1String ( " .html " ) ) ;
2012-03-23 13:58:31 +01:00
}
2012-08-14 18:53:55 +02:00
DownloadManager : : DownloadInfo info ;
info . page = page ( ) ;
info . suggestedFileName = suggestedFileName ;
info . askWhatToDo = false ;
info . forceChoosingPath = true ;
2014-03-10 00:47:07 +01:00
DownloadManager * dManager = mApp - > downloadManager ( ) ;
2012-08-14 18:53:55 +02:00
dManager - > download ( request , info ) ;
2012-03-23 13:58:31 +01:00
}
2014-02-12 13:48:34 +01:00
void WebView : : openUrlInNewTab ( const QUrl & url , Qz : : NewTabPositionFlags position )
2013-11-09 13:32:03 +01:00
{
loadInNewTab ( QNetworkRequest ( url ) , QNetworkAccessManager : : GetOperation , QByteArray ( ) , position ) ;
}
2012-03-23 13:58:31 +01:00
void WebView : : downloadUrlToDisk ( )
2011-03-02 16:57:41 +01:00
{
2011-03-24 22:31:38 +01:00
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
QNetworkRequest request ( action - > data ( ) . toUrl ( ) ) ;
2012-03-23 13:58:31 +01:00
2012-08-14 18:53:55 +02:00
DownloadManager : : DownloadInfo info ;
info . page = page ( ) ;
info . suggestedFileName = QString ( ) ;
info . askWhatToDo = false ;
info . forceChoosingPath = true ;
2014-03-10 00:47:07 +01:00
DownloadManager * dManager = mApp - > downloadManager ( ) ;
2012-08-14 18:53:55 +02:00
dManager - > download ( request , info ) ;
2011-03-24 22:31:38 +01:00
}
2011-03-02 16:57:41 +01:00
}
void WebView : : copyImageToClipboard ( )
{
triggerPageAction ( QWebPage : : CopyImageToClipboard ) ;
}
2012-01-21 20:20:48 +01:00
void WebView : : openActionUrl ( )
2011-03-02 16:57:41 +01:00
{
2011-03-17 17:03:04 +01:00
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
2012-01-21 20:20:48 +01:00
load ( action - > data ( ) . toUrl ( ) ) ;
2011-03-02 16:57:41 +01:00
}
}
2012-01-21 20:20:48 +01:00
void WebView : : showSource ( QWebFrame * frame , const QString & selectedHtml )
2011-07-19 22:04:08 +02:00
{
2012-01-21 20:20:48 +01:00
if ( ! frame ) {
frame = page ( ) - > mainFrame ( ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
SourceViewer * source = new SourceViewer ( frame , selectedHtml ) ;
2013-01-22 19:04:22 +01:00
QzTools : : centerWidgetToParent ( source , this ) ;
2012-01-21 20:20:48 +01:00
source - > show ( ) ;
2011-03-02 16:57:41 +01:00
}
void WebView : : showSiteInfo ( )
{
2012-01-21 20:20:48 +01:00
SiteInfo * s = new SiteInfo ( this , this ) ;
s - > show ( ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-22 11:49:58 +01:00
void WebView : : searchSelectedText ( )
{
2012-02-24 21:03:44 +01:00
SearchEngine engine = mApp - > searchEnginesManager ( ) - > activeEngine ( ) ;
if ( QAction * act = qobject_cast < QAction * > ( sender ( ) ) ) {
if ( act - > data ( ) . isValid ( ) ) {
2012-12-20 14:45:35 +01:00
engine = act - > data ( ) . value < SearchEngine > ( ) ;
2012-02-24 21:03:44 +01:00
}
}
2013-11-09 13:32:03 +01:00
SearchEnginesManager : : SearchResult res = mApp - > searchEnginesManager ( ) - > searchResult ( engine , selectedText ( ) ) ;
loadInNewTab ( res . request , res . operation , res . data , Qz : : NT_SelectedTab ) ;
2012-01-22 11:49:58 +01:00
}
2012-06-01 20:25:37 +02:00
void WebView : : searchSelectedTextInBackgroundTab ( )
{
SearchEngine engine = mApp - > searchEnginesManager ( ) - > activeEngine ( ) ;
if ( QAction * act = qobject_cast < QAction * > ( sender ( ) ) ) {
if ( act - > data ( ) . isValid ( ) ) {
2012-12-20 14:45:35 +01:00
engine = act - > data ( ) . value < SearchEngine > ( ) ;
2012-06-01 20:25:37 +02:00
}
}
2013-11-09 13:32:03 +01:00
SearchEnginesManager : : SearchResult res = mApp - > searchEnginesManager ( ) - > searchResult ( engine , selectedText ( ) ) ;
loadInNewTab ( res . request , res . operation , res . data , Qz : : NT_NotSelectedTab ) ;
2012-06-01 20:25:37 +02:00
}
2012-01-22 11:49:58 +01:00
void WebView : : bookmarkLink ( )
{
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
if ( action - > data ( ) . isNull ( ) ) {
2014-02-08 23:01:01 +01:00
BookmarksTools : : addBookmarkDialog ( this , url ( ) , title ( ) ) ;
2012-01-22 11:49:58 +01:00
}
else {
2014-01-26 11:21:53 +01:00
const QVariantList bData = action - > data ( ) . value < QVariantList > ( ) ;
const QString bookmarkTitle = bData . at ( 1 ) . toString ( ) . isEmpty ( ) ? title ( ) : bData . at ( 1 ) . toString ( ) ;
2014-02-08 23:01:01 +01:00
BookmarksTools : : addBookmarkDialog ( this , bData . at ( 0 ) . toUrl ( ) , bookmarkTitle ) ;
2012-01-22 11:49:58 +01:00
}
}
}
void WebView : : showSourceOfSelection ( )
{
2013-01-20 12:10:28 +01:00
# if QTWEBKIT_FROM_2_2
2012-01-22 11:49:58 +01:00
showSource ( page ( ) - > mainFrame ( ) , selectedHtml ( ) ) ;
# endif
}
2012-01-22 15:15:43 +01:00
void WebView : : openUrlInSelectedTab ( )
{
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
2014-02-12 13:48:34 +01:00
openUrlInNewTab ( action - > data ( ) . toUrl ( ) , Qz : : NT_CleanSelectedTab ) ;
2012-01-22 15:15:43 +01:00
}
}
void WebView : : openUrlInBackgroundTab ( )
{
if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
2014-02-12 13:48:34 +01:00
openUrlInNewTab ( action - > data ( ) . toUrl ( ) , Qz : : NT_CleanNotSelectedTab ) ;
2012-01-22 15:15:43 +01:00
}
2012-01-24 15:16:33 +01:00
}
2012-01-22 15:15:43 +01:00
2012-07-03 11:28:14 +02:00
void WebView : : userDefinedOpenUrlInNewTab ( const QUrl & url , bool invert )
2012-06-26 23:39:23 +02:00
{
2014-02-12 13:48:34 +01:00
Qz : : NewTabPositionFlags position = qzSettings - > newTabPosition ;
2012-07-03 11:28:14 +02:00
if ( invert ) {
2014-02-12 13:48:34 +01:00
if ( position & Qz : : NT_SelectedTab ) {
position & = ~ Qz : : NT_SelectedTab ;
position | = Qz : : NT_NotSelectedTab ;
}
else {
position & = ~ Qz : : NT_NotSelectedTab ;
position | = Qz : : NT_SelectedTab ;
}
2012-07-03 11:28:14 +02:00
}
2013-01-26 12:04:30 +01:00
QUrl actionUrl ;
if ( ! url . isEmpty ( ) ) {
actionUrl = url ;
2012-06-26 23:39:23 +02:00
}
2013-01-26 12:04:30 +01:00
else if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
actionUrl = action - > data ( ) . toUrl ( ) ;
2012-06-26 23:39:23 +02:00
}
2013-01-26 12:04:30 +01:00
openUrlInNewTab ( actionUrl , position ) ;
}
void WebView : : userDefinedOpenUrlInBgTab ( const QUrl & url )
{
QUrl actionUrl ;
if ( ! url . isEmpty ( ) ) {
actionUrl = url ;
}
else if ( QAction * action = qobject_cast < QAction * > ( sender ( ) ) ) {
actionUrl = action - > data ( ) . toUrl ( ) ;
}
userDefinedOpenUrlInNewTab ( actionUrl , true ) ;
2012-06-26 23:39:23 +02:00
}
2012-01-22 11:49:58 +01:00
void WebView : : loadClickedFrame ( )
{
2012-03-30 13:43:47 +02:00
QUrl frameUrl = m_clickedFrame - > baseUrl ( ) ;
2012-01-22 11:49:58 +01:00
if ( frameUrl . isEmpty ( ) ) {
frameUrl = m_clickedFrame - > requestedUrl ( ) ;
}
load ( frameUrl ) ;
}
2013-01-26 12:04:30 +01:00
void WebView : : loadClickedFrameInNewTab ( bool invert )
2012-01-22 11:49:58 +01:00
{
2012-03-30 13:43:47 +02:00
QUrl frameUrl = m_clickedFrame - > baseUrl ( ) ;
2012-01-22 11:49:58 +01:00
if ( frameUrl . isEmpty ( ) ) {
frameUrl = m_clickedFrame - > requestedUrl ( ) ;
}
2013-01-26 12:04:30 +01:00
userDefinedOpenUrlInNewTab ( frameUrl , invert ) ;
}
void WebView : : loadClickedFrameInBgTab ( )
{
loadClickedFrameInNewTab ( true ) ;
2012-01-22 11:49:58 +01:00
}
void WebView : : reloadClickedFrame ( )
{
2012-03-30 13:43:47 +02:00
QUrl frameUrl = m_clickedFrame - > baseUrl ( ) ;
2012-01-22 11:49:58 +01:00
if ( frameUrl . isEmpty ( ) ) {
frameUrl = m_clickedFrame - > requestedUrl ( ) ;
}
m_clickedFrame - > load ( frameUrl ) ;
}
void WebView : : printClickedFrame ( )
{
printPage ( m_clickedFrame ) ;
}
void WebView : : clickedFrameZoomIn ( )
{
qreal zFactor = m_clickedFrame - > zoomFactor ( ) + 0.1 ;
if ( zFactor > 2.5 ) {
zFactor = 2.5 ;
}
m_clickedFrame - > setZoomFactor ( zFactor ) ;
}
void WebView : : clickedFrameZoomOut ( )
{
qreal zFactor = m_clickedFrame - > zoomFactor ( ) - 0.1 ;
if ( zFactor < 0.5 ) {
zFactor = 0.5 ;
}
m_clickedFrame - > setZoomFactor ( zFactor ) ;
}
void WebView : : clickedFrameZoomReset ( )
{
m_clickedFrame - > setZoomFactor ( zoomFactor ( ) ) ;
}
void WebView : : showClickedFrameSource ( )
{
showSource ( m_clickedFrame ) ;
}
2012-01-21 20:20:48 +01:00
void WebView : : printPage ( QWebFrame * frame )
2011-03-02 16:57:41 +01:00
{
2012-01-21 20:20:48 +01:00
QPrintPreviewDialog * dialog = new QPrintPreviewDialog ( this ) ;
dialog - > resize ( 800 , 750 ) ;
2011-03-02 16:57:41 +01:00
2012-01-21 20:20:48 +01:00
if ( ! frame ) {
connect ( dialog , SIGNAL ( paintRequested ( QPrinter * ) ) , this , SLOT ( print ( QPrinter * ) ) ) ;
2011-11-06 17:01:23 +01:00
}
2012-01-21 20:20:48 +01:00
else {
connect ( dialog , SIGNAL ( paintRequested ( QPrinter * ) ) , frame , SLOT ( print ( QPrinter * ) ) ) ;
}
dialog - > exec ( ) ;
dialog - > deleteLater ( ) ;
2011-03-02 16:57:41 +01:00
}
2012-01-21 20:20:48 +01:00
QUrl WebView : : lastUrl ( )
2011-03-02 16:57:41 +01:00
{
2012-01-21 20:20:48 +01:00
return m_lastUrl ;
2011-03-02 16:57:41 +01:00
}
2012-01-22 00:20:29 +01:00
bool WebView : : isMediaElement ( const QWebElement & element )
{
2012-09-04 12:42:45 +02:00
return ( element . tagName ( ) . toLower ( ) = = QLatin1String ( " video " )
| | element . tagName ( ) . toLower ( ) = = QLatin1String ( " audio " ) ) ;
2012-01-22 00:20:29 +01:00
}
2012-03-10 13:57:50 +01:00
void WebView : : checkForForm ( QMenu * menu , const QWebElement & element )
2012-03-08 13:05:57 +01:00
{
QWebElement parentElement = element . parent ( ) ;
while ( ! parentElement . isNull ( ) ) {
2012-09-04 12:42:45 +02:00
if ( parentElement . tagName ( ) . toLower ( ) = = QLatin1String ( " form " ) ) {
2012-03-08 13:05:57 +01:00
break ;
}
parentElement = parentElement . parent ( ) ;
}
if ( parentElement . isNull ( ) ) {
return ;
}
2013-12-30 13:43:48 +01:00
const QString url = parentElement . attribute ( " action " ) ;
const QString method = parentElement . hasAttribute ( " method " ) ? parentElement . attribute ( " method " ) . toUpper ( ) : " GET " ;
2012-03-08 13:05:57 +01:00
2013-11-09 15:48:24 +01:00
if ( ! url . isEmpty ( ) & & ( method = = QLatin1String ( " GET " ) | | method = = QLatin1String ( " POST " ) ) ) {
2012-03-08 13:05:57 +01:00
menu - > addAction ( QIcon ( " :icons/menu/search-icon.png " ) , tr ( " Create Search Engine " ) , this , SLOT ( createSearchEngine ( ) ) ) ;
m_clickedElement = element ;
}
}
void WebView : : createSearchEngine ( )
{
mApp - > searchEnginesManager ( ) - > addEngineFromForm ( m_clickedElement , this ) ;
}
2012-01-22 11:49:58 +01:00
void WebView : : createContextMenu ( QMenu * menu , const QWebHitTestResult & hitTest , const QPoint & pos )
{
2012-04-01 10:48:50 +02:00
if ( ! m_actionsInitialized ) {
m_actionsInitialized = true ;
2012-01-22 11:49:58 +01:00
pageAction ( QWebPage : : Cut ) - > setIcon ( QIcon : : fromTheme ( " edit-cut " ) ) ;
2013-03-18 15:53:16 +01:00
pageAction ( QWebPage : : Cut ) - > setText ( tr ( " Cut " ) ) ;
2012-01-22 11:49:58 +01:00
pageAction ( QWebPage : : Copy ) - > setIcon ( QIcon : : fromTheme ( " edit-copy " ) ) ;
2013-03-18 15:53:16 +01:00
pageAction ( QWebPage : : Copy ) - > setText ( tr ( " Copy " ) ) ;
2012-01-22 11:49:58 +01:00
pageAction ( QWebPage : : Paste ) - > setIcon ( QIcon : : fromTheme ( " edit-paste " ) ) ;
2013-03-18 15:53:16 +01:00
pageAction ( QWebPage : : Paste ) - > setText ( tr ( " Paste " ) ) ;
2012-01-22 11:49:58 +01:00
pageAction ( QWebPage : : SelectAll ) - > setIcon ( QIcon : : fromTheme ( " edit-select-all " ) ) ;
2013-03-18 15:53:16 +01:00
pageAction ( QWebPage : : SelectAll ) - > setText ( tr ( " Select All " ) ) ;
2012-04-01 10:48:50 +02:00
2013-05-02 12:02:41 +02:00
pageAction ( QWebPage : : SetTextDirectionDefault ) - > setText ( tr ( " Default " ) ) ;
pageAction ( QWebPage : : SetTextDirectionLeftToRight ) - > setText ( tr ( " Left to Right " ) ) ;
pageAction ( QWebPage : : SetTextDirectionRightToLeft ) - > setText ( tr ( " Right to Left " ) ) ;
pageAction ( QWebPage : : ToggleBold ) - > setText ( tr ( " Bold " ) ) ;
pageAction ( QWebPage : : ToggleItalic ) - > setText ( tr ( " Italic " ) ) ;
pageAction ( QWebPage : : ToggleUnderline ) - > setText ( tr ( " Underline " ) ) ;
2014-03-07 18:03:42 +01:00
m_actionReload = new QAction ( IconProvider : : standardIcon ( QStyle : : SP_BrowserReload ) , tr ( " &Reload " ) , this ) ;
m_actionStop = new QAction ( IconProvider : : standardIcon ( QStyle : : SP_BrowserStop ) , tr ( " S&top " ) , this ) ;
2012-04-01 10:48:50 +02:00
connect ( m_actionReload , SIGNAL ( triggered ( ) ) , this , SLOT ( reload ( ) ) ) ;
connect ( m_actionStop , SIGNAL ( triggered ( ) ) , this , SLOT ( stop ( ) ) ) ;
m_actionReload - > setEnabled ( ! isLoading ( ) ) ;
m_actionStop - > setEnabled ( isLoading ( ) ) ;
2012-01-22 11:49:58 +01:00
}
2013-04-28 17:48:51 +02:00
// cppcheck-suppress variableScope
2013-02-05 20:43:53 +01:00
int spellCheckActionCount = 0 ;
# ifdef USE_HUNSPELL
// Show spellcheck menu as the first
if ( hitTest . isContentEditable ( ) & & ! hitTest . isContentSelected ( ) ) {
2014-03-08 14:21:08 +01:00
Speller : : instance ( ) - > populateContextMenu ( menu , hitTest ) ;
2013-02-05 20:43:53 +01:00
spellCheckActionCount = menu - > actions ( ) . count ( ) ;
}
# endif
2012-09-04 12:42:45 +02:00
if ( ! hitTest . linkUrl ( ) . isEmpty ( ) & & hitTest . linkUrl ( ) . scheme ( ) ! = QLatin1String ( " javascript " ) ) {
2012-01-22 11:49:58 +01:00
createLinkContextMenu ( menu , hitTest ) ;
}
if ( ! hitTest . imageUrl ( ) . isEmpty ( ) ) {
createImageContextMenu ( menu , hitTest ) ;
}
if ( isMediaElement ( hitTest . element ( ) ) ) {
createMediaContextMenu ( menu , hitTest ) ;
}
2012-02-20 18:28:09 +01:00
if ( hitTest . isContentEditable ( ) ) {
2013-04-28 17:48:51 +02:00
// This only checks if the menu is empty (only spellchecker actions added)
2013-02-05 20:43:53 +01:00
if ( menu - > actions ( ) . count ( ) = = spellCheckActionCount ) {
2012-01-22 11:49:58 +01:00
QMenu * pageMenu = page ( ) - > createStandardContextMenu ( ) ;
2013-02-08 12:10:12 +01:00
// Apparently createStandardContextMenu() can return null pointer
if ( pageMenu ) {
2014-01-04 19:00:01 +01:00
if ( qzSettings - > enableFormsUndoRedo ) {
pageAction ( QWebPage : : Undo ) - > setIcon ( QIcon : : fromTheme ( " edit-undo " ) ) ;
pageAction ( QWebPage : : Undo ) - > setText ( tr ( " Undo " ) ) ;
menu - > addAction ( pageAction ( QWebPage : : Undo ) ) ;
pageAction ( QWebPage : : Redo ) - > setIcon ( QIcon : : fromTheme ( " edit-redo " ) ) ;
pageAction ( QWebPage : : Redo ) - > setText ( tr ( " Redo " ) ) ;
menu - > addAction ( pageAction ( QWebPage : : Redo ) ) ;
menu - > addSeparator ( ) ;
}
2013-02-08 12:10:12 +01:00
int i = 0 ;
2013-03-06 09:05:41 +01:00
foreach ( QAction * act , pageMenu - > actions ( ) ) {
2013-02-08 12:10:12 +01:00
if ( act - > isSeparator ( ) ) {
menu - > addSeparator ( ) ;
continue ;
}
2012-01-22 11:49:58 +01:00
2013-02-08 12:10:12 +01:00
// Hiding double Direction + Fonts menu (bug in QtWebKit 2.2)
if ( i < = 1 & & act - > menu ( ) ) {
if ( act - > menu ( ) - > actions ( ) . contains ( pageAction ( QWebPage : : SetTextDirectionDefault ) ) | |
act - > menu ( ) - > actions ( ) . contains ( pageAction ( QWebPage : : ToggleBold ) ) ) {
act - > setVisible ( false ) ;
}
2012-02-20 18:28:09 +01:00
}
2012-01-22 11:49:58 +01:00
2013-02-08 12:10:12 +01:00
menu - > addAction ( act ) ;
2012-01-22 11:49:58 +01:00
2013-03-18 15:53:16 +01:00
if ( act = = pageAction ( QWebPage : : Paste ) ) {
QAction * a = menu - > addAction ( QIcon : : fromTheme ( " edit-delete " ) , tr ( " Delete " ) , this , SLOT ( editDelete ( ) ) ) ;
a - > setEnabled ( ! selectedText ( ) . isEmpty ( ) ) ;
}
2013-02-08 12:10:12 +01:00
+ + i ;
}
2012-01-22 11:49:58 +01:00
2013-02-08 12:10:12 +01:00
if ( menu - > actions ( ) . last ( ) = = pageAction ( QWebPage : : InspectElement ) ) {
// We have own Inspect Element action
menu - > actions ( ) . last ( ) - > setVisible ( false ) ;
}
2012-01-22 11:49:58 +01:00
2013-02-08 12:10:12 +01:00
delete pageMenu ;
}
2012-01-22 11:49:58 +01:00
}
2012-09-04 12:42:45 +02:00
if ( hitTest . element ( ) . tagName ( ) . toLower ( ) = = QLatin1String ( " input " ) ) {
2012-03-08 17:17:41 +01:00
checkForForm ( menu , hitTest . element ( ) ) ;
}
2013-02-05 22:31:16 +01:00
2014-03-08 14:21:08 +01:00
# ifdef USE_HUNSPELL
Speller : : instance ( ) - > createContextMenu ( menu ) ;
# endif
2012-03-08 13:05:57 +01:00
}
2012-01-22 11:49:58 +01:00
if ( ! selectedText ( ) . isEmpty ( ) ) {
createSelectedTextContextMenu ( menu , hitTest ) ;
}
2012-01-23 17:30:34 +01:00
if ( menu - > isEmpty ( ) ) {
2012-01-22 11:49:58 +01:00
createPageContextMenu ( menu , pos ) ;
}
2012-02-19 17:50:02 +01:00
menu - > addSeparator ( ) ;
mApp - > plugins ( ) - > populateWebViewMenu ( menu , this , hitTest ) ;
2013-01-20 12:10:28 +01:00
# if QTWEBKIT_FROM_2_2
2012-01-26 20:19:56 +01:00
// still bugged? in 4.8 RC (it shows selection of webkit's internal source, not html from page)
// it may or may not be bug, but this implementation is useless for us
//
// if (!selectedHtml().isEmpty())
// menu->addAction(tr("Show source of selection"), this, SLOT(showSourceOfSelection()));
2012-01-22 11:49:58 +01:00
# endif
}
void WebView : : createPageContextMenu ( QMenu * menu , const QPoint & pos )
{
QWebFrame * frameAtPos = page ( ) - > frameAt ( pos ) ;
QAction * action = menu - > addAction ( tr ( " &Back " ) , this , SLOT ( back ( ) ) ) ;
2014-03-07 18:03:42 +01:00
action - > setIcon ( IconProvider : : standardIcon ( QStyle : : SP_ArrowBack ) ) ;
2012-04-04 13:21:53 +02:00
action - > setEnabled ( history ( ) - > canGoBack ( ) ) ;
2012-01-22 11:49:58 +01:00
action = menu - > addAction ( tr ( " &Forward " ) , this , SLOT ( forward ( ) ) ) ;
2014-03-07 18:03:42 +01:00
action - > setIcon ( IconProvider : : standardIcon ( QStyle : : SP_ArrowForward ) ) ;
2012-04-04 13:21:53 +02:00
action - > setEnabled ( history ( ) - > canGoForward ( ) ) ;
2012-01-22 11:49:58 +01:00
2014-01-08 10:29:01 +01:00
if ( url ( ) ! = QUrl ( " qupzilla:speeddial " ) ) {
2014-01-04 22:37:18 +01:00
menu - > addAction ( m_actionReload ) ;
menu - > addAction ( m_actionStop ) ;
menu - > addSeparator ( ) ;
if ( frameAtPos & & page ( ) - > mainFrame ( ) ! = frameAtPos ) {
m_clickedFrame = frameAtPos ;
Menu * frameMenu = new Menu ( tr ( " This frame " ) ) ;
2014-02-10 21:34:46 +01:00
frameMenu - > setCloseOnMiddleClick ( true ) ;
2014-01-04 22:37:18 +01:00
frameMenu - > addAction ( tr ( " Show &only this frame " ) , this , SLOT ( loadClickedFrame ( ) ) ) ;
Action * act = new Action ( QIcon : : fromTheme ( " tab-new " , QIcon ( " :/icons/menu/tab-new.png " ) ) , tr ( " Show this frame in new &tab " ) ) ;
connect ( act , SIGNAL ( triggered ( ) ) , this , SLOT ( loadClickedFrameInNewTab ( ) ) ) ;
2014-02-09 01:14:00 +01:00
connect ( act , SIGNAL ( ctrlTriggered ( ) ) , this , SLOT ( loadClickedFrameInBgTab ( ) ) ) ;
2014-01-04 22:37:18 +01:00
frameMenu - > addAction ( act ) ;
frameMenu - > addSeparator ( ) ;
2014-03-07 18:03:42 +01:00
frameMenu - > addAction ( IconProvider : : standardIcon ( QStyle : : SP_BrowserReload ) , tr ( " &Reload " ) , this , SLOT ( reloadClickedFrame ( ) ) ) ;
2014-01-04 22:37:18 +01:00
frameMenu - > addAction ( QIcon : : fromTheme ( " document-print " ) , tr ( " Print frame " ) , this , SLOT ( printClickedFrame ( ) ) ) ;
frameMenu - > addSeparator ( ) ;
frameMenu - > addAction ( QIcon : : fromTheme ( " zoom-in " ) , tr ( " Zoom &in " ) , this , SLOT ( clickedFrameZoomIn ( ) ) ) ;
frameMenu - > addAction ( QIcon : : fromTheme ( " zoom-out " ) , tr ( " &Zoom out " ) , this , SLOT ( clickedFrameZoomOut ( ) ) ) ;
frameMenu - > addAction ( QIcon : : fromTheme ( " zoom-original " ) , tr ( " Reset " ) , this , SLOT ( clickedFrameZoomReset ( ) ) ) ;
frameMenu - > addSeparator ( ) ;
frameMenu - > addAction ( QIcon : : fromTheme ( " text-html " ) , tr ( " Show so&urce of frame " ) , this , SLOT ( showClickedFrameSource ( ) ) ) ;
menu - > addMenu ( frameMenu ) ;
}
2013-12-24 14:55:43 +01:00
2014-01-04 22:37:18 +01:00
menu - > addSeparator ( ) ;
2014-03-07 18:03:42 +01:00
menu - > addAction ( IconProvider : : iconFromTheme ( " bookmark-new " ) , tr ( " Book&mark page " ) , this , SLOT ( bookmarkLink ( ) ) ) ;
2014-01-04 22:37:18 +01:00
menu - > addAction ( QIcon : : fromTheme ( " document-save " ) , tr ( " &Save page as... " ) , this , SLOT ( savePageAs ( ) ) ) ;
menu - > addAction ( QIcon : : fromTheme ( " edit-copy " ) , tr ( " &Copy page link " ) , this , SLOT ( copyLinkToClipboard ( ) ) ) - > setData ( url ( ) ) ;
menu - > addAction ( QIcon : : fromTheme ( " mail-message-new " ) , tr ( " Send page link... " ) , this , SLOT ( sendPageByMail ( ) ) ) ;
menu - > addAction ( QIcon : : fromTheme ( " document-print " ) , tr ( " &Print page " ) , this , SLOT ( printPage ( ) ) ) ;
menu - > addSeparator ( ) ;
menu - > addAction ( QIcon : : fromTheme ( " edit-select-all " ) , tr ( " Select &all " ) , this , SLOT ( selectAll ( ) ) ) ;
menu - > addSeparator ( ) ;
if ( url ( ) . scheme ( ) = = QLatin1String ( " http " ) | | url ( ) . scheme ( ) = = QLatin1String ( " https " ) ) {
const QUrl w3url = QUrl : : fromEncoded ( " http://validator.w3.org/check?uri= " + QUrl : : toPercentEncoding ( url ( ) . toEncoded ( ) ) ) ;
menu - > addAction ( QIcon ( " :icons/sites/w3.png " ) , tr ( " Validate page " ) , this , SLOT ( openUrlInSelectedTab ( ) ) ) - > setData ( w3url ) ;
QByteArray langCode = mApp - > currentLanguage ( ) . left ( 2 ) . toUtf8 ( ) ;
const QUrl gturl = QUrl : : fromEncoded ( " http://translate.google.com/translate?sl=auto&tl= " + langCode + " &u= " + QUrl : : toPercentEncoding ( url ( ) . toEncoded ( ) ) ) ;
menu - > addAction ( QIcon ( " :icons/sites/translate.png " ) , tr ( " Translate page " ) , this , SLOT ( openUrlInSelectedTab ( ) ) ) - > setData ( gturl ) ;
}
2013-12-24 14:55:43 +01:00
2014-01-04 22:37:18 +01:00
menu - > addSeparator ( ) ;
menu - > addAction ( QIcon : : fromTheme ( " text-html " ) , tr ( " Show so&urce code " ) , this , SLOT ( showSource ( ) ) ) ;
menu - > addAction ( QIcon : : fromTheme ( " dialog-information " ) , tr ( " Show info ab&out site " ) , this , SLOT ( showSiteInfo ( ) ) ) ;
2012-01-22 11:49:58 +01:00
}
2014-01-04 22:37:18 +01:00
else {
menu - > addSeparator ( ) ;
2014-01-05 11:32:51 +01:00
menu - > addAction ( QIcon : : fromTheme ( " list-add " ) , tr ( " &Add New Page " ) , this , SLOT ( addSpeedDial ( ) ) ) ;
menu - > addAction ( QIcon : : fromTheme ( " configure " ) , tr ( " &Configure Speed Dial " ) , this , SLOT ( configureSpeedDial ( ) ) ) ;
2014-01-04 22:37:18 +01:00
}
2012-01-22 11:49:58 +01:00
}
void WebView : : createLinkContextMenu ( QMenu * menu , const QWebHitTestResult & hitTest )
{
// Workaround for QtWebKit <= 2.0 when selecting link text on right click
if ( page ( ) - > selectedText ( ) = = hitTest . linkText ( ) ) {
2012-09-02 11:42:41 +02:00
findText ( QString ( ) ) ;
2012-01-22 11:49:58 +01:00
}
menu - > addSeparator ( ) ;
2014-01-04 22:37:18 +01:00
Action * act = new Action ( QIcon : : fromTheme ( " tab-new " , QIcon ( " :/icons/menu/tab-new.png " ) ) , tr ( " Open link in new &tab " ) ) ;
2013-01-26 12:04:30 +01:00
act - > setData ( hitTest . linkUrl ( ) ) ;
connect ( act , SIGNAL ( triggered ( ) ) , this , SLOT ( userDefinedOpenUrlInNewTab ( ) ) ) ;
2014-02-09 01:14:00 +01:00
connect ( act , SIGNAL ( ctrlTriggered ( ) ) , this , SLOT ( userDefinedOpenUrlInBgTab ( ) ) ) ;
2013-01-26 12:04:30 +01:00
menu - > addAction ( act ) ;
2012-01-22 11:49:58 +01:00
menu - > addAction ( QIcon : : fromTheme ( " window-new " ) , tr ( " Open link in new &window " ) , this , SLOT ( openUrlInNewWindow ( ) ) ) - > setData ( hitTest . linkUrl ( ) ) ;
2014-02-22 14:57:22 +01:00
menu - > addAction ( QIcon ( " :icons/locationbar/privatebrowsing.png " ) , tr ( " Open link in &private window " ) , mApp , SLOT ( startPrivateBrowsing ( ) ) ) - > setData ( hitTest . linkUrl ( ) ) ;
2012-01-22 11:49:58 +01:00
menu - > addSeparator ( ) ;
2014-01-26 11:21:53 +01:00
QVariantList bData ;
bData < < hitTest . linkUrl ( ) < < hitTest . linkTitle ( ) ;
2014-03-07 18:03:42 +01:00
menu - > addAction ( IconProvider : : iconFromTheme ( " bookmark-new " ) , tr ( " B&ookmark link " ) , this , SLOT ( bookmarkLink ( ) ) ) - > setData ( bData ) ;
2014-01-26 11:21:53 +01:00
2012-03-23 13:58:31 +01:00
menu - > addAction ( QIcon : : fromTheme ( " document-save " ) , tr ( " &Save link as... " ) , this , SLOT ( downloadUrlToDisk ( ) ) ) - > setData ( hitTest . linkUrl ( ) ) ;
2012-01-22 11:49:58 +01:00
menu - > addAction ( QIcon : : fromTheme ( " mail-message-new " ) , tr ( " Send link... " ) , this , SLOT ( sendLinkByMail ( ) ) ) - > setData ( hitTest . linkUrl ( ) ) ;
menu - > addAction ( QIcon : : fromTheme ( " edit-copy " ) , tr ( " &Copy link address " ) , this , SLOT ( copyLinkToClipboard ( ) ) ) - > setData ( hitTest . linkUrl ( ) ) ;
menu - > addSeparator ( ) ;
if ( ! selectedText ( ) . isEmpty ( ) ) {
pageAction ( QWebPage : : Copy ) - > setIcon ( QIcon : : fromTheme ( " edit-copy " ) ) ;
menu - > addAction ( pageAction ( QWebPage : : Copy ) ) ;
}
}
void WebView : : createImageContextMenu ( QMenu * menu , const QWebHitTestResult & hitTest )
{
menu - > addSeparator ( ) ;
2012-01-24 23:28:18 +01:00
Action * act = new Action ( tr ( " Show i&mage " ) ) ;
act - > setData ( hitTest . imageUrl ( ) ) ;
connect ( act , SIGNAL ( triggered ( ) ) , this , SLOT ( openActionUrl ( ) ) ) ;
2014-02-09 01:14:00 +01:00
connect ( act , SIGNAL ( ctrlTriggered ( ) ) , this , SLOT ( userDefinedOpenUrlInNewTab ( ) ) ) ;
2012-01-24 23:28:18 +01:00
menu - > addAction ( act ) ;
2012-01-22 11:49:58 +01:00
menu - > addAction ( tr ( " Copy im&age " ) , this , SLOT ( copyImageToClipboard ( ) ) ) - > setData ( hitTest . imageUrl ( ) ) ;
menu - > addAction ( QIcon : : fromTheme ( " edit-copy " ) , tr ( " Copy image ad&dress " ) , this , SLOT ( copyLinkToClipboard ( ) ) ) - > setData ( hitTest . imageUrl ( ) ) ;
menu - > addSeparator ( ) ;
2012-03-23 13:58:31 +01:00
menu - > addAction ( QIcon : : fromTheme ( " document-save " ) , tr ( " &Save image as... " ) , this , SLOT ( downloadUrlToDisk ( ) ) ) - > setData ( hitTest . imageUrl ( ) ) ;
2012-01-22 11:49:58 +01:00
menu - > addAction ( QIcon : : fromTheme ( " mail-message-new " ) , tr ( " Send image... " ) , this , SLOT ( sendLinkByMail ( ) ) ) - > setData ( hitTest . imageUrl ( ) ) ;
menu - > addSeparator ( ) ;
if ( ! selectedText ( ) . isEmpty ( ) ) {
pageAction ( QWebPage : : Copy ) - > setIcon ( QIcon : : fromTheme ( " edit-copy " ) ) ;
menu - > addAction ( pageAction ( QWebPage : : Copy ) ) ;
}
}
void WebView : : createSelectedTextContextMenu ( QMenu * menu , const QWebHitTestResult & hitTest )
{
Q_UNUSED ( hitTest )
QString selectedText = page ( ) - > selectedText ( ) ;
menu - > addSeparator ( ) ;
if ( ! menu - > actions ( ) . contains ( pageAction ( QWebPage : : Copy ) ) ) {
menu - > addAction ( pageAction ( QWebPage : : Copy ) ) ;
}
menu - > addAction ( QIcon : : fromTheme ( " mail-message-new " ) , tr ( " Send text... " ) , this , SLOT ( sendLinkByMail ( ) ) ) - > setData ( selectedText ) ;
menu - > addSeparator ( ) ;
2013-12-26 21:26:30 +01:00
QString langCode = mApp - > currentLanguage ( ) . left ( 2 ) . toUtf8 ( ) ;
QUrl googleTranslateUrl = QUrl ( QString ( " http://translate.google.com/#auto/%1/%2 " ) . arg ( langCode , selectedText ) ) ;
2012-06-28 17:16:45 +02:00
Action * gtwact = new Action ( QIcon ( " :icons/sites/translate.png " ) , tr ( " Google Translate " ) ) ;
2012-02-25 12:34:16 +01:00
gtwact - > setData ( googleTranslateUrl ) ;
connect ( gtwact , SIGNAL ( triggered ( ) ) , this , SLOT ( openUrlInSelectedTab ( ) ) ) ;
2014-02-09 01:14:00 +01:00
connect ( gtwact , SIGNAL ( ctrlTriggered ( ) ) , this , SLOT ( openUrlInBackgroundTab ( ) ) ) ;
2012-02-24 20:56:19 +01:00
menu - > addAction ( gtwact ) ;
2013-12-26 21:26:30 +01:00
2012-02-24 20:56:19 +01:00
Action * dictact = new Action ( QIcon : : fromTheme ( " accessories-dictionary " ) , tr ( " Dictionary " ) ) ;
2012-09-02 11:42:41 +02:00
dictact - > setData ( QUrl ( " http:// " + ( ! langCode . isEmpty ( ) ? langCode + " . " : langCode ) + " wiktionary.org/wiki/Special:Search?search= " + selectedText ) ) ;
2012-02-25 12:34:16 +01:00
connect ( dictact , SIGNAL ( triggered ( ) ) , this , SLOT ( openUrlInSelectedTab ( ) ) ) ;
2014-02-09 01:14:00 +01:00
connect ( dictact , SIGNAL ( ctrlTriggered ( ) ) , this , SLOT ( openUrlInBackgroundTab ( ) ) ) ;
2012-02-24 20:56:19 +01:00
menu - > addAction ( dictact ) ;
2012-01-22 11:49:58 +01:00
2012-04-14 10:54:03 +02:00
// #379: Remove newlines
2012-09-04 12:42:45 +02:00
QString selectedString = selectedText . trimmed ( ) . remove ( QLatin1Char ( ' \n ' ) ) ;
if ( ! selectedString . contains ( QLatin1Char ( ' . ' ) ) ) {
2012-01-22 11:49:58 +01:00
// Try to add .com
2012-09-04 12:42:45 +02:00
selectedString . append ( QLatin1String ( " .com " ) ) ;
2012-01-22 11:49:58 +01:00
}
QUrl guessedUrl = QUrl : : fromUserInput ( selectedString ) ;
if ( isUrlValid ( guessedUrl ) ) {
2012-02-24 20:55:35 +01:00
Action * act = new Action ( QIcon : : fromTheme ( " document-open-remote " ) , tr ( " Go to &web address " ) ) ;
2012-02-01 15:37:45 +01:00
act - > setData ( guessedUrl ) ;
2012-06-01 20:25:37 +02:00
2012-02-01 15:37:45 +01:00
connect ( act , SIGNAL ( triggered ( ) ) , this , SLOT ( openActionUrl ( ) ) ) ;
2014-02-09 01:14:00 +01:00
connect ( act , SIGNAL ( ctrlTriggered ( ) ) , this , SLOT ( userDefinedOpenUrlInNewTab ( ) ) ) ;
2012-02-01 15:37:45 +01:00
menu - > addAction ( act ) ;
2012-01-22 11:49:58 +01:00
}
2012-02-24 20:55:35 +01:00
menu - > addSeparator ( ) ;
2012-01-22 11:49:58 +01:00
selectedText . truncate ( 20 ) ;
2013-12-28 15:26:56 +01:00
// KDE is displaying newlines in menu actions ... weird -,-
selectedText . replace ( QLatin1Char ( ' \n ' ) , QLatin1Char ( ' ' ) ) . replace ( QLatin1Char ( ' \t ' ) , QLatin1Char ( ' ' ) ) ;
2012-01-22 11:49:58 +01:00
SearchEngine engine = mApp - > searchEnginesManager ( ) - > activeEngine ( ) ;
2012-06-01 20:25:37 +02:00
Action * act = new Action ( engine . icon , tr ( " Search \" %1 .. \" with %2 " ) . arg ( selectedText , engine . name ) ) ;
connect ( act , SIGNAL ( triggered ( ) ) , this , SLOT ( searchSelectedText ( ) ) ) ;
2014-02-09 01:14:00 +01:00
connect ( act , SIGNAL ( ctrlTriggered ( ) ) , this , SLOT ( searchSelectedTextInBackgroundTab ( ) ) ) ;
2012-06-01 20:25:37 +02:00
menu - > addAction ( act ) ;
// Search with ...
Menu * swMenu = new Menu ( tr ( " Search with... " ) , menu ) ;
2014-02-10 21:34:46 +01:00
swMenu - > setCloseOnMiddleClick ( true ) ;
2012-02-24 21:13:06 +01:00
SearchEnginesManager * searchManager = mApp - > searchEnginesManager ( ) ;
2013-03-06 09:05:41 +01:00
foreach ( const SearchEngine & en , searchManager - > allEngines ( ) ) {
2012-06-01 20:25:37 +02:00
Action * act = new Action ( en . icon , en . name ) ;
2013-02-05 22:33:54 +01:00
act - > setData ( QVariant : : fromValue ( en ) ) ;
2012-06-01 20:25:37 +02:00
connect ( act , SIGNAL ( triggered ( ) ) , this , SLOT ( searchSelectedText ( ) ) ) ;
2014-02-09 01:14:00 +01:00
connect ( act , SIGNAL ( ctrlTriggered ( ) ) , this , SLOT ( searchSelectedTextInBackgroundTab ( ) ) ) ;
2012-06-01 20:25:37 +02:00
swMenu - > addAction ( act ) ;
2012-02-24 20:54:05 +01:00
}
2012-06-01 20:25:37 +02:00
2012-02-24 20:54:05 +01:00
menu - > addMenu ( swMenu ) ;
2012-01-22 11:49:58 +01:00
}
void WebView : : createMediaContextMenu ( QMenu * menu , const QWebHitTestResult & hitTest )
2012-01-22 00:20:29 +01:00
{
2012-03-08 13:05:57 +01:00
m_clickedElement = hitTest . element ( ) ;
2012-01-22 00:20:29 +01:00
2012-03-08 13:05:57 +01:00
if ( m_clickedElement . isNull ( ) ) {
2012-01-22 11:49:58 +01:00
return ;
}
2012-03-08 13:05:57 +01:00
bool paused = m_clickedElement . evaluateJavaScript ( " this.paused " ) . toBool ( ) ;
bool muted = m_clickedElement . evaluateJavaScript ( " this.muted " ) . toBool ( ) ;
QUrl videoUrl = m_clickedElement . evaluateJavaScript ( " this.currentSrc " ) . toUrl ( ) ;
2012-01-22 00:20:29 +01:00
2012-01-22 11:49:58 +01:00
menu - > addSeparator ( ) ;
2012-01-22 01:24:35 +01:00
menu - > addAction ( paused ? tr ( " &Play " ) : tr ( " &Pause " ) , this , SLOT ( pauseMedia ( ) ) ) - > setIcon ( QIcon : : fromTheme ( paused ? " media-playback-start " : " media-playback-pause " ) ) ;
2012-01-22 01:43:09 +01:00
menu - > addAction ( muted ? tr ( " Un&mute " ) : tr ( " &Mute " ) , this , SLOT ( muteMedia ( ) ) ) - > setIcon ( QIcon : : fromTheme ( muted ? " audio-volume-muted " : " audio-volume-high " ) ) ;
2012-01-22 00:20:29 +01:00
menu - > addSeparator ( ) ;
2012-01-22 01:24:35 +01:00
menu - > addAction ( QIcon : : fromTheme ( " edit-copy " ) , tr ( " &Copy Media Address " ) , this , SLOT ( copyLinkToClipboard ( ) ) ) - > setData ( videoUrl ) ;
menu - > addAction ( QIcon : : fromTheme ( " mail-message-new " ) , tr ( " &Send Media Address " ) , this , SLOT ( sendLinkByMail ( ) ) ) - > setData ( videoUrl ) ;
2012-03-23 13:58:31 +01:00
menu - > addAction ( QIcon : : fromTheme ( " document-save " ) , tr ( " Save Media To &Disk " ) , this , SLOT ( downloadUrlToDisk ( ) ) ) - > setData ( videoUrl ) ;
2012-01-22 00:20:29 +01:00
}
void WebView : : pauseMedia ( )
{
2012-03-08 13:05:57 +01:00
bool paused = m_clickedElement . evaluateJavaScript ( " this.paused " ) . toBool ( ) ;
2012-01-22 00:20:29 +01:00
if ( paused ) {
2012-03-08 13:05:57 +01:00
m_clickedElement . evaluateJavaScript ( " this.play() " ) ;
2012-01-22 00:20:29 +01:00
}
else {
2012-03-08 13:05:57 +01:00
m_clickedElement . evaluateJavaScript ( " this.pause() " ) ;
2012-01-22 00:20:29 +01:00
}
}
void WebView : : muteMedia ( )
{
2012-03-08 13:05:57 +01:00
bool muted = m_clickedElement . evaluateJavaScript ( " this.muted " ) . toBool ( ) ;
2012-01-22 00:20:29 +01:00
if ( muted ) {
2012-03-08 13:05:57 +01:00
m_clickedElement . evaluateJavaScript ( " this.muted = false " ) ;
2012-01-22 00:20:29 +01:00
}
else {
2012-03-08 13:05:57 +01:00
m_clickedElement . evaluateJavaScript ( " this.muted = true " ) ;
2012-01-22 00:20:29 +01:00
}
}
2014-01-04 22:37:18 +01:00
void WebView : : addSpeedDial ( )
{
page ( ) - > mainFrame ( ) - > evaluateJavaScript ( " addSpeedDial() " ) ;
}
void WebView : : configureSpeedDial ( )
{
page ( ) - > mainFrame ( ) - > evaluateJavaScript ( " configureSpeedDial() " ) ;
}
2011-03-17 17:03:04 +01:00
void WebView : : wheelEvent ( QWheelEvent * event )
2011-03-02 16:57:41 +01:00
{
2012-03-07 12:19:08 +01:00
if ( mApp - > plugins ( ) - > processWheelEvent ( Qz : : ON_WebView , this , event ) ) {
return ;
}
2011-03-02 16:57:41 +01:00
if ( event - > modifiers ( ) & Qt : : ControlModifier ) {
2012-07-13 18:00:03 +02:00
event - > delta ( ) > 0 ? zoomIn ( ) : zoomOut ( ) ;
2011-03-02 16:57:41 +01:00
event - > accept ( ) ;
2012-07-13 18:00:03 +02:00
2011-03-02 16:57:41 +01:00
return ;
}
2012-01-21 20:20:48 +01:00
QWebView : : wheelEvent ( event ) ;
2012-01-02 13:56:52 +01:00
}
2011-12-24 12:21:23 +01:00
void WebView : : mousePressEvent ( QMouseEvent * event )
{
2012-02-22 18:33:44 +01:00
if ( mApp - > plugins ( ) - > processMousePress ( Qz : : ON_WebView , this , event ) ) {
return ;
}
2011-12-24 12:21:23 +01:00
switch ( event - > button ( ) ) {
case Qt : : XButton1 :
back ( ) ;
2012-01-14 09:54:51 +01:00
event - > accept ( ) ;
2011-12-24 12:21:23 +01:00
break ;
2012-01-21 20:20:48 +01:00
2011-12-24 12:21:23 +01:00
case Qt : : XButton2 :
forward ( ) ;
2012-01-14 09:54:51 +01:00
event - > accept ( ) ;
2011-12-24 12:21:23 +01:00
break ;
2012-01-21 20:20:48 +01:00
2012-01-22 15:15:43 +01:00
case Qt : : MiddleButton : {
2012-01-24 19:12:31 +01:00
QWebFrame * frame = page ( ) - > frameAt ( event - > pos ( ) ) ;
2012-01-26 20:19:56 +01:00
if ( frame ) {
m_clickedUrl = frame - > hitTestContent ( event - > pos ( ) ) . linkUrl ( ) ;
2012-01-28 16:04:02 +01:00
if ( ! m_clickedUrl . isEmpty ( ) ) {
return ;
}
2012-01-26 20:19:56 +01:00
}
2012-01-28 16:04:02 +01:00
break ;
2012-01-22 15:15:43 +01:00
}
case Qt : : LeftButton : {
QWebFrame * frame = page ( ) - > frameAt ( event - > pos ( ) ) ;
2012-01-24 19:12:31 +01:00
if ( frame ) {
2013-12-30 13:43:48 +01:00
const QUrl link = frame - > hitTestContent ( event - > pos ( ) ) . linkUrl ( ) ;
2012-07-03 11:28:14 +02:00
if ( event - > modifiers ( ) & Qt : : ControlModifier & & isUrlValid ( link ) ) {
userDefinedOpenUrlInNewTab ( link , event - > modifiers ( ) & Qt : : ShiftModifier ) ;
2012-01-26 20:19:56 +01:00
event - > accept ( ) ;
return ;
}
2012-01-24 19:12:31 +01:00
}
}
default :
break ;
}
QWebView : : mousePressEvent ( event ) ;
}
2012-01-22 15:15:43 +01:00
2012-01-24 19:12:31 +01:00
void WebView : : mouseReleaseEvent ( QMouseEvent * event )
{
2012-02-22 18:33:44 +01:00
if ( mApp - > plugins ( ) - > processMouseRelease ( Qz : : ON_WebView , this , event ) ) {
return ;
}
2012-01-24 19:12:31 +01:00
switch ( event - > button ( ) ) {
case Qt : : MiddleButton : {
QWebFrame * frame = page ( ) - > frameAt ( event - > pos ( ) ) ;
if ( frame ) {
2013-12-30 13:43:48 +01:00
const QUrl link = frame - > hitTestContent ( event - > pos ( ) ) . linkUrl ( ) ;
2012-01-24 19:12:31 +01:00
if ( m_clickedUrl = = link & & isUrlValid ( link ) ) {
2012-07-03 11:28:14 +02:00
userDefinedOpenUrlInNewTab ( link , event - > modifiers ( ) & Qt : : ShiftModifier ) ;
2012-01-22 15:15:43 +01:00
event - > accept ( ) ;
return ;
}
}
break ;
}
2011-12-24 12:21:23 +01:00
default :
break ;
}
2012-01-22 15:15:43 +01:00
2012-01-24 19:12:31 +01:00
QWebView : : mouseReleaseEvent ( event ) ;
2011-12-24 12:21:23 +01:00
}
2012-02-22 18:33:44 +01:00
void WebView : : mouseMoveEvent ( QMouseEvent * event )
{
if ( mApp - > plugins ( ) - > processMouseMove ( Qz : : ON_WebView , this , event ) ) {
return ;
}
QWebView : : mouseMoveEvent ( event ) ;
}
2011-12-24 12:21:23 +01:00
void WebView : : keyPressEvent ( QKeyEvent * event )
{
2012-02-22 18:33:44 +01:00
if ( mApp - > plugins ( ) - > processKeyPress ( Qz : : ON_WebView , this , event ) ) {
return ;
}
2013-04-09 15:57:44 +02:00
int eventKey = event - > key ( ) ;
2013-04-15 23:21:00 +02:00
// The right/left arrow keys within contents with right to left (RTL) layout have
// reversed behavior than left to right (LTR) layout.
// Example: Key_Right within LTR layout triggers QWebPage::MoveToNextChar but,
// Key_Right within RTL layout should trigger QWebPage::MoveToPreviousChar
2014-02-03 00:10:14 +01:00
// event->spontaneous() check guards recursive calling of keyPressEvent
// Events created from app have spontaneous() == false
if ( event - > spontaneous ( ) & & ( eventKey = = Qt : : Key_Left | | eventKey = = Qt : : Key_Right ) ) {
2013-12-30 13:43:48 +01:00
const QWebElement elementHasCursor = activeElement ( ) ;
2013-04-09 15:57:44 +02:00
if ( ! elementHasCursor . isNull ( ) ) {
2013-12-30 13:43:48 +01:00
const QString direction = elementHasCursor . styleProperty ( " direction " , QWebElement : : ComputedStyle ) ;
2013-04-15 23:21:00 +02:00
if ( direction = = QLatin1String ( " rtl " ) ) {
2013-04-09 15:57:44 +02:00
eventKey = eventKey = = Qt : : Key_Left ? Qt : : Key_Right : Qt : : Key_Left ;
2013-04-15 23:21:00 +02:00
QKeyEvent ev ( event - > type ( ) , eventKey , event - > modifiers ( ) , event - > text ( ) , event - > isAutoRepeat ( ) ) ;
2014-02-03 00:10:14 +01:00
keyPressEvent ( & ev ) ;
return ;
2013-04-09 15:57:44 +02:00
}
}
}
switch ( eventKey ) {
2011-12-24 12:21:23 +01:00
case Qt : : Key_C :
if ( event - > modifiers ( ) = = Qt : : ControlModifier ) {
2012-02-01 15:37:45 +01:00
triggerPageAction ( QWebPage : : Copy ) ;
2012-01-14 09:54:51 +01:00
event - > accept ( ) ;
2011-12-24 12:21:23 +01:00
return ;
}
break ;
case Qt : : Key_A :
if ( event - > modifiers ( ) = = Qt : : ControlModifier ) {
selectAll ( ) ;
2012-01-14 09:54:51 +01:00
event - > accept ( ) ;
2011-12-24 12:21:23 +01:00
return ;
}
break ;
2013-02-17 11:07:02 +01:00
case Qt : : Key_Up :
if ( event - > modifiers ( ) & Qt : : ShiftModifier ) {
triggerPageAction ( QWebPage : : SelectPreviousLine ) ;
event - > accept ( ) ;
return ;
}
break ;
case Qt : : Key_Down :
if ( event - > modifiers ( ) & Qt : : ShiftModifier ) {
triggerPageAction ( QWebPage : : SelectNextLine ) ;
event - > accept ( ) ;
return ;
}
break ;
case Qt : : Key_Left :
if ( event - > modifiers ( ) & Qt : : ShiftModifier ) {
if ( event - > modifiers ( ) = = Qt : : ShiftModifier ) {
triggerPageAction ( QWebPage : : SelectPreviousChar ) ;
}
else if ( event - > modifiers ( ) = = ( Qt : : ShiftModifier | Qt : : ControlModifier ) ) {
triggerPageAction ( QWebPage : : SelectPreviousWord ) ;
}
event - > accept ( ) ;
return ;
}
break ;
case Qt : : Key_Right :
if ( event - > modifiers ( ) & Qt : : ShiftModifier ) {
if ( event - > modifiers ( ) = = Qt : : ShiftModifier ) {
triggerPageAction ( QWebPage : : SelectNextChar ) ;
}
else if ( event - > modifiers ( ) = = ( Qt : : ShiftModifier | Qt : : ControlModifier ) ) {
triggerPageAction ( QWebPage : : SelectNextWord ) ;
}
event - > accept ( ) ;
return ;
}
break ;
case Qt : : Key_Home :
if ( event - > modifiers ( ) & Qt : : ShiftModifier ) {
if ( event - > modifiers ( ) = = Qt : : ShiftModifier ) {
triggerPageAction ( QWebPage : : SelectStartOfLine ) ;
}
else if ( event - > modifiers ( ) = = ( Qt : : ShiftModifier | Qt : : ControlModifier ) ) {
triggerPageAction ( QWebPage : : SelectStartOfDocument ) ;
}
event - > accept ( ) ;
return ;
}
break ;
case Qt : : Key_End :
if ( event - > modifiers ( ) & Qt : : ShiftModifier ) {
if ( event - > modifiers ( ) = = Qt : : ShiftModifier ) {
triggerPageAction ( QWebPage : : SelectEndOfLine ) ;
}
else if ( event - > modifiers ( ) = = ( Qt : : ShiftModifier | Qt : : ControlModifier ) ) {
triggerPageAction ( QWebPage : : SelectEndOfDocument ) ;
}
event - > accept ( ) ;
return ;
}
break ;
2013-05-19 10:22:40 +02:00
case Qt : : Key_Insert :
if ( event - > modifiers ( ) = = Qt : : ControlModifier ) {
triggerPageAction ( QWebPage : : Copy ) ;
event - > accept ( ) ;
return ;
}
if ( event - > modifiers ( ) = = Qt : : ShiftModifier ) {
triggerPageAction ( QWebPage : : Paste ) ;
event - > accept ( ) ;
return ;
}
break ;
2011-12-24 12:21:23 +01:00
default :
break ;
}
QWebView : : keyPressEvent ( event ) ;
}
2012-02-22 18:33:44 +01:00
void WebView : : keyReleaseEvent ( QKeyEvent * event )
{
if ( mApp - > plugins ( ) - > processKeyRelease ( Qz : : ON_WebView , this , event ) ) {
return ;
}
QWebView : : keyReleaseEvent ( event ) ;
}
2011-12-24 12:21:23 +01:00
void WebView : : resizeEvent ( QResizeEvent * event )
{
QWebView : : resizeEvent ( event ) ;
2012-01-21 20:20:48 +01:00
emit viewportResized ( page ( ) - > viewportSize ( ) ) ;
2011-12-24 12:21:23 +01:00
}
2012-01-21 20:20:48 +01:00
void WebView : : setZoom ( int zoom )
2011-12-24 12:21:23 +01:00
{
2012-01-21 20:20:48 +01:00
m_currentZoom = zoom ;
applyZoom ( ) ;
2011-12-24 12:21:23 +01:00
}
2011-11-05 21:27:01 +01:00
///
2011-12-17 14:30:54 +01:00
// This function was taken and modified from QTestBrowser to fix bug #33 with flightradar24.com
2011-11-05 21:27:01 +01:00
// You can find original source and copyright here:
// http://gitorious.org/+qtwebkit-developers/webkit/qtwebkit/blobs/qtwebkit-2.2/Tools/QtTestBrowser/launcherwindow.cpp
///
bool WebView : : eventFilter ( QObject * obj , QEvent * event )
{
2013-01-09 19:00:48 +01:00
// This hack is no longer needed with QtWebKit 2.3 (bundled in Qt 5)
2013-01-20 12:10:28 +01:00
# if QTWEBKIT_TO_2_3
2012-08-31 15:20:00 +02:00
if ( obj ! = this | | m_disableTouchMocking ) {
2011-12-24 12:21:23 +01:00
return false ;
}
2011-11-05 21:27:01 +01:00
if ( event - > type ( ) = = QEvent : : MouseButtonPress | |
2011-11-06 17:01:23 +01:00
event - > type ( ) = = QEvent : : MouseButtonRelease | |
event - > type ( ) = = QEvent : : MouseButtonDblClick | |
event - > type ( ) = = QEvent : : MouseMove ) {
2011-11-05 21:27:01 +01:00
QMouseEvent * ev = static_cast < QMouseEvent * > ( event ) ;
2012-08-31 15:20:00 +02:00
2012-01-23 17:30:34 +01:00
if ( ev - > type ( ) = = QEvent : : MouseMove & & ! ( ev - > buttons ( ) & Qt : : LeftButton ) ) {
return false ;
}
2012-08-31 15:20:00 +02:00
if ( ev - > type ( ) = = QEvent : : MouseButtonPress & & ! ( ev - > buttons ( ) & Qt : : LeftButton ) ) {
2011-11-05 21:27:01 +01:00
return false ;
2011-11-06 17:01:23 +01:00
}
2011-11-05 21:27:01 +01:00
2012-09-16 20:42:07 +02:00
QEvent : : Type type = QEvent : : TouchUpdate ;
2011-11-05 21:27:01 +01:00
QTouchEvent : : TouchPoint touchPoint ;
touchPoint . setId ( 0 ) ;
touchPoint . setScreenPos ( ev - > globalPos ( ) ) ;
touchPoint . setPos ( ev - > pos ( ) ) ;
touchPoint . setPressure ( 1 ) ;
2012-08-31 15:20:00 +02:00
switch ( ev - > type ( ) ) {
case QEvent : : MouseButtonPress :
case QEvent : : MouseButtonDblClick :
touchPoint . setState ( Qt : : TouchPointPressed ) ;
type = QEvent : : TouchBegin ;
2011-11-05 21:27:01 +01:00
2012-08-31 15:20:00 +02:00
break ;
2011-11-05 21:27:01 +01:00
2012-08-31 15:20:00 +02:00
case QEvent : : MouseButtonRelease :
touchPoint . setState ( Qt : : TouchPointReleased ) ;
type = QEvent : : TouchEnd ;
2011-11-05 21:27:01 +01:00
2012-08-31 15:20:00 +02:00
break ;
case QEvent : : MouseMove :
touchPoint . setState ( Qt : : TouchPointMoved ) ;
type = QEvent : : TouchUpdate ;
break ;
default :
break ;
2011-11-05 21:27:01 +01:00
}
2012-08-31 15:20:00 +02:00
QList < QTouchEvent : : TouchPoint > touchPoints ;
touchPoints < < touchPoint ;
QTouchEvent touchEv ( type ) ;
touchEv . setTouchPoints ( touchPoints ) ;
QCoreApplication : : sendEvent ( page ( ) , & touchEv ) ;
2011-11-05 21:27:01 +01:00
return false ;
}
2012-12-21 16:04:57 +01:00
# endif
2011-11-06 12:05:16 +01:00
return QWebView : : eventFilter ( obj , event ) ;
2011-11-05 21:27:01 +01:00
}
2012-03-05 13:16:34 +01:00
void WebView : : disconnectObjects ( )
{
disconnect ( this ) ;
}