2 Copyright (c) 2016 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "webengineviewer.h"
19 #include "articleviewer-ng/webengine/articleviewerwebenginepage.h"
21 #include "actionmanager.h"
23 #include "webengine/urlhandlerwebenginemanager.h"
25 #include <KPIMTextEdit/TextToSpeech>
26 #include <MessageViewer/AdBlockBlockableItemsDialog>
28 #include <KIO/KUriFilterSearchProviderActions>
29 #include <KActionCollection>
30 #include <KActionMenu>
31 #include <KLocalizedString>
32 #include <QWebEngineSettings>
33 #include <QContextMenuEvent>
35 #include <QWebEngineHistory>
36 #include <MessageViewer/WebHitTest>
37 #include <MessageViewer/WebHitTestResult>
38 using namespace Akregator
;
40 WebEngineViewer::WebEngineViewer(KActionCollection
*ac
, QWidget
*parent
)
41 : ArticleViewerWebEngine(ac
, parent
)
43 settings()->setAttribute(QWebEngineSettings::JavascriptEnabled
, true);
44 settings()->setAttribute(QWebEngineSettings::PluginsEnabled
, false);
45 settings()->setAttribute(QWebEngineSettings::AutoLoadImages
, true);
48 WebEngineViewer::~WebEngineViewer()
53 void WebEngineViewer::contextMenuEvent(QContextMenuEvent
*e
)
55 displayContextMenu(e
->pos());
58 void WebEngineViewer::slotWebHitFinished(const MessageViewer::WebHitTestResult
&result
)
60 mCurrentUrl
= result
.linkUrl();
61 if (URLHandlerWebEngineManager::instance()->handleContextMenuRequest(mCurrentUrl
, mapToGlobal(result
.pos()), this)) {
65 QWebEngineHistory
*history
= page()->history();
66 bool needSeparator
= false;
67 if (history
->canGoBack()) {
68 popup
.addAction(pageAction(QWebEnginePage::Back
));
72 if (history
->canGoForward()) {
73 popup
.addAction(pageAction(QWebEnginePage::Forward
));
79 popup
.addAction(pageAction(QWebEnginePage::Stop
));
81 popup
.addAction(pageAction(QWebEnginePage::Reload
));
84 const bool contentSelected
= !selectedText().isEmpty();
85 if (!contentSelected
) {
86 if (!mCurrentUrl
.isEmpty()) {
87 popup
.addAction(createOpenLinkInNewTabAction(mCurrentUrl
, this, SLOT(slotOpenLinkInForegroundTab()), &popup
));
88 popup
.addAction(createOpenLinkInExternalBrowserAction(mCurrentUrl
, this, SLOT(slotOpenLinkInBrowser()), &popup
));
90 popup
.addAction(mActionCollection
->action(QStringLiteral("savelinkas")));
91 popup
.addAction(mActionCollection
->action(QStringLiteral("copylinkaddress")));
93 if (!result
.imageUrl().isEmpty()) {
95 popup
.addAction(mActionCollection
->action(QStringLiteral("copy_image_location")));
96 popup
.addAction(mActionCollection
->action(QStringLiteral("saveas_imageurl")));
97 if (adblockEnabled()) {
99 popup
.addAction(mActionCollection
->action(QStringLiteral("adblock_image")));
102 popup
.addSeparator();
103 popup
.addActions(viewerPluginActionList(MessageViewer::ViewerPluginInterface::NeedUrl
));
104 popup
.addSeparator();
105 popup
.addAction(mShareServiceManager
->menu());
107 popup
.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_copy")));
108 popup
.addSeparator();
109 mWebShortcutMenuManager
->setSelectedText(page()->selectedText());
110 mWebShortcutMenuManager
->addWebShortcutsToMenu(&popup
);
111 popup
.addSeparator();
112 popup
.addActions(viewerPluginActionList(MessageViewer::ViewerPluginInterface::NeedSelection
));
113 popup
.addSeparator();
114 popup
.addAction(i18n("Open Ad Block settings"), this, SLOT(slotOpenBlockableItemsDialog()));
116 #if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
117 popup
.addSeparator();
118 popup
.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_print")));
119 popup
.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_printpreview")));
121 popup
.addSeparator();
122 popup
.addAction(ActionManager::getInstance()->action(QStringLiteral("find_in_messages")));
123 if (KPIMTextEdit::TextToSpeech::self()->isReady()) {
124 popup
.addSeparator();
125 popup
.addAction(ActionManager::getInstance()->action(QStringLiteral("speak_text")));
127 popup
.exec(mapToGlobal(result
.pos()));
130 void WebEngineViewer::displayContextMenu(const QPoint
&pos
)
132 MessageViewer::WebHitTest
*webHit
= mPageEngine
->hitTestContent(pos
);
133 connect(webHit
, &MessageViewer::WebHitTest::finished
, this, &WebEngineViewer::slotWebHitFinished
);
136 void WebEngineViewer::slotOpenBlockableItemsDialog()
139 QPointer
<MessageViewer::AdBlockBlockableItemsDialog
> dlg
= new MessageViewer::AdBlockBlockableItemsDialog(this);
140 dlg
->setWebFrame(page()->mainFrame());