Astyle kdelibs
[kdepim.git] / akregator / src / frame / webengine / webengineviewer.cpp
blobf78d4aa5fe3d478f1913d7b3ce79341990bf58e7
1 /*
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"
22 #include "actions.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>
34 #include <QMenu>
35 #include <QWebEngineHistory>
36 #include <MessageViewer/WebHitTest>
37 #include <MessageViewer/WebHitTestResult>
38 #include <MessageViewer/ScamCheckShortUrlManager>
39 #include <MessageViewer/ScamCheckShortUrl>
40 using namespace Akregator;
42 WebEngineViewer::WebEngineViewer(KActionCollection *ac, QWidget *parent)
43 : ArticleViewerWebEngine(ac, parent)
45 settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
46 settings()->setAttribute(QWebEngineSettings::PluginsEnabled, false);
47 settings()->setAttribute(QWebEngineSettings::AutoLoadImages, true);
50 WebEngineViewer::~WebEngineViewer()
55 void WebEngineViewer::contextMenuEvent(QContextMenuEvent *e)
57 displayContextMenu(e->pos());
60 void WebEngineViewer::slotWebHitFinished(const MessageViewer::WebHitTestResult &result)
62 mCurrentUrl = result.linkUrl();
63 if (URLHandlerWebEngineManager::instance()->handleContextMenuRequest(mCurrentUrl, mapToGlobal(result.pos()), this)) {
64 return;
66 QMenu popup(this);
67 QWebEngineHistory *history = page()->history();
68 bool needSeparator = false;
69 if (history->canGoBack()) {
70 popup.addAction(pageAction(QWebEnginePage::Back));
71 needSeparator = true;
74 if (history->canGoForward()) {
75 popup.addAction(pageAction(QWebEnginePage::Forward));
76 needSeparator = true;
78 if (needSeparator) {
79 popup.addSeparator();
81 popup.addAction(pageAction(QWebEnginePage::Stop));
82 popup.addSeparator();
83 popup.addAction(pageAction(QWebEnginePage::Reload));
84 popup.addSeparator();
86 const bool contentSelected = !selectedText().isEmpty();
87 if (!contentSelected) {
88 if (!mCurrentUrl.isEmpty()) {
89 popup.addAction(createOpenLinkInNewTabAction(mCurrentUrl, this, SLOT(slotOpenLinkInForegroundTab()), &popup));
90 popup.addAction(createOpenLinkInExternalBrowserAction(mCurrentUrl, this, SLOT(slotOpenLinkInBrowser()), &popup));
91 popup.addSeparator();
92 popup.addAction(mActionCollection->action(QStringLiteral("savelinkas")));
93 popup.addAction(mActionCollection->action(QStringLiteral("copylinkaddress")));
95 if (!result.imageUrl().isEmpty()) {
96 popup.addSeparator();
97 popup.addAction(mActionCollection->action(QStringLiteral("copy_image_location")));
98 popup.addAction(mActionCollection->action(QStringLiteral("saveas_imageurl")));
99 if (adblockEnabled()) {
100 popup.addSeparator();
101 popup.addAction(mActionCollection->action(QStringLiteral("adblock_image")));
104 if (MessageViewer::ScamCheckShortUrlManager::self()->scamCheckShortUrl()->isShortUrl(mCurrentUrl)) {
105 popup.addSeparator();
106 popup.addAction(mActionCollection->action(QStringLiteral("expand_short_url")));
108 popup.addSeparator();
109 popup.addAction(mShareServiceManager->menu());
110 } else {
111 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_copy")));
112 popup.addSeparator();
113 mWebShortcutMenuManager->setSelectedText(page()->selectedText());
114 mWebShortcutMenuManager->addWebShortcutsToMenu(&popup);
115 popup.addSeparator();
116 popup.addActions(viewerPluginActionList(MessageViewer::ViewerPluginInterface::NeedSelection));
117 popup.addSeparator();
118 popup.addAction(i18n("Open Ad Block settings"), this, SLOT(slotOpenBlockableItemsDialog()));
120 #if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
121 popup.addSeparator();
122 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_print")));
123 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_printpreview")));
124 #endif
125 popup.addSeparator();
126 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("find_in_messages")));
127 if (KPIMTextEdit::TextToSpeech::self()->isReady()) {
128 popup.addSeparator();
129 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("speak_text")));
131 popup.exec(mapToGlobal(result.pos()));
134 void WebEngineViewer::displayContextMenu(const QPoint &pos)
136 MessageViewer::WebHitTest *webHit = mPageEngine->hitTestContent(pos);
137 connect(webHit, &MessageViewer::WebHitTest::finished, this, &WebEngineViewer::slotWebHitFinished);
140 void WebEngineViewer::slotOpenBlockableItemsDialog()
142 #if 0
143 QPointer<MessageViewer::AdBlockBlockableItemsDialog> dlg = new MessageViewer::AdBlockBlockableItemsDialog(this);
144 dlg->setWebFrame(page()->mainFrame());
145 if (dlg->exec()) {
146 dlg->saveFilters();
148 delete dlg;
149 #endif