Use short url plugin directly
[kdepim.git] / akregator / src / frame / webkit / webviewer.cpp
blob0d9ea07753a6883a741cc006957114278f652360
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 "webviewer.h"
19 #include "actionmanager.h"
20 #include "actions.h"
21 #include "webkit/urlhandlermanager.h"
23 #include <KPIMTextEdit/TextToSpeech>
24 #include <MessageViewer/AdBlockBlockableItemsDialog>
26 #include <KIO/KUriFilterSearchProviderActions>
27 #include <KActionCollection>
28 #include <KActionMenu>
29 #include <KLocalizedString>
31 #include <QContextMenuEvent>
32 #include <QMenu>
33 #include <QWebHistory>
35 using namespace Akregator;
37 WebViewer::WebViewer(KActionCollection *ac, QWidget *parent)
38 : ArticleViewerNg(ac, parent)
40 settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
41 settings()->setAttribute(QWebSettings::JavaEnabled, true);
42 settings()->setAttribute(QWebSettings::PluginsEnabled, true);
43 settings()->setAttribute(QWebSettings::DnsPrefetchEnabled, true);
44 settings()->setAttribute(QWebSettings::AutoLoadImages, true);
47 WebViewer::~WebViewer()
52 void WebViewer::displayContextMenu(const QPoint &pos)
54 mContextMenuHitResult = page()->mainFrame()->hitTestContent(pos);
55 mCurrentUrl = mContextMenuHitResult.linkUrl();
56 if (URLHandlerManager::instance()->handleContextMenuRequest(mCurrentUrl, mapToGlobal(pos), this)) {
57 return;
59 QMenu popup(this);
60 QWebHistory *history = page()->history();
61 bool needSeparator = false;
62 if (history->canGoBack()) {
63 popup.addAction(pageAction(QWebPage::Back));
64 needSeparator = true;
67 if (history->canGoForward()) {
68 popup.addAction(pageAction(QWebPage::Forward));
69 needSeparator = true;
71 if (needSeparator) {
72 popup.addSeparator();
74 popup.addAction(pageAction(QWebPage::Stop));
75 popup.addSeparator();
76 popup.addAction(pageAction(QWebPage::Reload));
77 popup.addSeparator();
79 const bool contentSelected = mContextMenuHitResult.isContentSelected();
80 if (!mCurrentUrl.isEmpty() && !contentSelected) {
81 popup.addAction(createOpenLinkInNewTabAction(mCurrentUrl, this, SLOT(slotOpenLinkInForegroundTab()), &popup));
82 popup.addAction(createOpenLinkInExternalBrowserAction(mCurrentUrl, this, SLOT(slotOpenLinkInBrowser()), &popup));
83 popup.addSeparator();
84 popup.addAction(mActionCollection->action(QStringLiteral("savelinkas")));
85 popup.addAction(mActionCollection->action(QStringLiteral("copylinkaddress")));
86 if (!mContextMenuHitResult.imageUrl().isEmpty()) {
87 popup.addSeparator();
88 popup.addAction(mActionCollection->action(QStringLiteral("copy_image_location")));
89 popup.addAction(mActionCollection->action(QStringLiteral("saveas_imageurl")));
90 if (adblockEnabled()) {
91 popup.addSeparator();
92 popup.addAction(mActionCollection->action(QStringLiteral("adblock_image")));
95 popup.addSeparator();
96 popup.addActions(viewerPluginActionList(MessageViewer::ViewerPluginInterface::NeedUrl));
97 popup.addSeparator();
98 popup.addAction(mShareServiceManager->menu());
99 } else {
100 if (contentSelected) {
101 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_copy")));
102 popup.addSeparator();
103 mWebShortcutMenuManager->setSelectedText(page()->selectedText());
104 mWebShortcutMenuManager->addWebShortcutsToMenu(&popup);
105 popup.addSeparator();
106 popup.addActions(viewerPluginActionList(MessageViewer::ViewerPluginInterface::NeedSelection));
107 popup.addSeparator();
109 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_print")));
110 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_printpreview")));
111 popup.addSeparator();
112 popup.addAction(i18n("Open Ad Block settings"), this, SLOT(slotOpenBlockableItemsDialog()));
114 popup.addSeparator();
115 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("find_in_messages")));
116 if (KPIMTextEdit::TextToSpeech::self()->isReady()) {
117 popup.addSeparator();
118 popup.addAction(ActionManager::getInstance()->action(QStringLiteral("speak_text")));
120 popup.exec(mapToGlobal(pos));
123 void WebViewer::slotOpenBlockableItemsDialog()
125 QPointer<MessageViewer::AdBlockBlockableItemsDialog> dlg = new MessageViewer::AdBlockBlockableItemsDialog(this);
126 dlg->setWebFrame(page()->mainFrame());
127 if (dlg->exec()) {
128 dlg->saveFilters();
130 delete dlg;