articleviewerwebengine.cpp 17.5 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
2
   Copyright (C) 2015-2017 Montel Laurent <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

Laurent Montel's avatar
Laurent Montel committed
4 5 6 7
   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 2 of the License, or (at your option) any later version.
Laurent Montel's avatar
Laurent Montel committed
8

Laurent Montel's avatar
Laurent Montel committed
9 10 11 12
   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.
Laurent Montel's avatar
Laurent Montel committed
13

Laurent Montel's avatar
Laurent Montel committed
14 15 16 17
   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
Laurent Montel's avatar
Laurent Montel committed
18 19 20
*/

#include "articleviewerwebengine.h"
Laurent Montel's avatar
Laurent Montel committed
21 22
#include "akregator_debug.h"
#include "articleviewerwebenginepage.h"
23
#include "webengine/urlhandlerwebenginemanager.h"
24
#include <WebEngineViewer/ZoomActionMenu>
Laurent Montel's avatar
Laurent Montel committed
25 26 27
#include "actionmanager.h"
#include "akregatorconfig.h"
#include "actions/actions.h"
Laurent Montel's avatar
Laurent Montel committed
28
#include "urlhandler/webengine/urlhandlerwebengine.h"
Laurent Montel's avatar
Laurent Montel committed
29

Laurent Montel's avatar
Laurent Montel committed
30
#include <WebEngineViewer/InterceptorManager>
31
#include <WebEngineViewer/WebEngineAccessKey>
Laurent Montel's avatar
Laurent Montel committed
32 33 34 35
#include <KPIMTextEdit/TextToSpeech>
#include <KActionMenu>

#include <KParts/BrowserRun>
36
#include <GrantleeTheme/GrantleeThemeManager>
Laurent Montel's avatar
Laurent Montel committed
37 38 39
#include <KActionCollection>
#include <KLocalizedString>
#include <KAboutData>
Laurent Montel's avatar
Laurent Montel committed
40 41 42 43 44 45 46 47 48 49 50
#include <KGuiItem>
#include <openurlrequest.h>
#include <KPimPrintPreviewDialog>
#include <KMessageBox>
#include <QPrinter>
#include <QWebEngineSettings>
#include <QMouseEvent>
#include <QPrintDialog>
#include <QApplication>
#include <QClipboard>
#include <QMenu>
51
#include <viewerplugintoolmanager.h>
Laurent Montel's avatar
Laurent Montel committed
52
#include <QWebEngineProfile>
Laurent Montel's avatar
Laurent Montel committed
53
#include <QWebEngineDownloadItem>
54 55
#include <QPointer>

56 57
#include <WebEngineViewer/WebHitTestResult>
#include <WebEngineViewer/WebHitTest>
Laurent Montel's avatar
Laurent Montel committed
58
#include <WebEngineViewer/WebEngineScript>
Laurent Montel's avatar
Laurent Montel committed
59

60 61
#include <WebEngineViewer/LocalDataBaseManager>

Laurent Montel's avatar
Laurent Montel committed
62
#include <KIO/KUriFilterSearchProviderActions>
Laurent Montel's avatar
Laurent Montel committed
63 64 65

using namespace Akregator;

Laurent Montel's avatar
Laurent Montel committed
66
ArticleViewerWebEngine::ArticleViewerWebEngine(KActionCollection *ac, QWidget *parent)
67
    : WebEngineViewer::WebEngineView(parent),
Laurent Montel's avatar
Laurent Montel committed
68
      mActionCollection(ac),
69
      mLastButtonClicked(LeftButton),
Laurent Montel's avatar
Laurent Montel committed
70
      mViewerPluginToolManager(nullptr)
Laurent Montel's avatar
Laurent Montel committed
71
{
Laurent Montel's avatar
Laurent Montel committed
72
    mNetworkAccessManager = new WebEngineViewer::InterceptorManager(this, ac, this);
Laurent Montel's avatar
Laurent Montel committed
73 74
    QWebEngineProfile *profile = new QWebEngineProfile(this);
    mPageEngine = new ArticleViewerWebEnginePage(profile, this);
Laurent Montel's avatar
Laurent Montel committed
75
    profile->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
Laurent Montel's avatar
Minor  
Laurent Montel committed
76

Laurent Montel's avatar
Laurent Montel committed
77
    setPage(mPageEngine);
Laurent Montel's avatar
Laurent Montel committed
78

Laurent Montel's avatar
Laurent Montel committed
79 80
    connect(this, &ArticleViewerWebEngine::showContextMenu, this, &ArticleViewerWebEngine::slotShowContextMenu);

Laurent Montel's avatar
Laurent Montel committed
81
    setFocusPolicy(Qt::WheelFocus);
Laurent Montel's avatar
Laurent Montel committed
82
    connect(mPageEngine, &ArticleViewerWebEnginePage::urlClicked, this, &ArticleViewerWebEngine::slotLinkClicked);
Laurent Montel's avatar
Laurent Montel committed
83

84
    mWebEngineViewAccessKey = new WebEngineViewer::WebEngineAccessKey(this, this);
Laurent Montel's avatar
Laurent Montel committed
85
    mWebEngineViewAccessKey->setActionCollection(mActionCollection);
86
    connect(mWebEngineViewAccessKey, &WebEngineViewer::WebEngineAccessKey::openUrl, this, &ArticleViewerWebEngine::slotLinkClicked);
Laurent Montel's avatar
Laurent Montel committed
87 88 89 90 91

    connect(this, &ArticleViewerWebEngine::loadStarted, this, &ArticleViewerWebEngine::slotLoadStarted);
    connect(this, &ArticleViewerWebEngine::loadFinished, this, &ArticleViewerWebEngine::slotLoadFinished);
    connect(page(), &QWebEnginePage::linkHovered, this, &ArticleViewerWebEngine::slotLinkHovered);

Laurent Montel's avatar
Laurent Montel committed
92
    setContextMenuPolicy(Qt::DefaultContextMenu);
Laurent Montel's avatar
Laurent Montel committed
93 94 95
    mWebShortcutMenuManager = new KIO::KUriFilterSearchProviderActions(this);
    mShareServiceManager = new PimCommon::ShareServiceUrlManager(this);
    connect(mShareServiceManager, &PimCommon::ShareServiceUrlManager::serviceUrlSelected, this, &ArticleViewerWebEngine::slotServiceUrlSelected);
96 97 98 99
    connect(page(), &QWebEnginePage::audioMutedChanged,
            this, &ArticleViewerWebEngine::slotWebPageMutedOrAudibleChanged);
    connect(page(), &QWebEnginePage::recentlyAudibleChanged,
            this, &ArticleViewerWebEngine::slotWebPageMutedOrAudibleChanged);
100 101 102

    connect(WebEngineViewer::LocalDataBaseManager::self(), &WebEngineViewer::LocalDataBaseManager::checkUrlFinished, this, &ArticleViewerWebEngine::slotCheckedUrlFinished);

Laurent Montel's avatar
Laurent Montel committed
103 104 105 106 107 108
}

ArticleViewerWebEngine::~ArticleViewerWebEngine()
{

}
Laurent Montel's avatar
Laurent Montel committed
109

110 111 112 113 114
void ArticleViewerWebEngine::slotWebPageMutedOrAudibleChanged()
{
    Q_EMIT webPageMutedOrAudibleChanged(page()->isAudioMuted(), page()->recentlyAudible());
}

Laurent Montel's avatar
Laurent Montel committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
QVariantHash ArticleViewerWebEngine::introductionData() const
{
    QVariantHash data;
    data[QStringLiteral("icon")] = QStringLiteral("akregator");
    data[QStringLiteral("name")] = i18n("Akregator");
    data[QStringLiteral("subtitle")] = i18n("Akregator is a KDE news feed reader.");
    data[QStringLiteral("version")] = KAboutData::applicationData().version();
    return data;
}

void ArticleViewerWebEngine::showAboutPage()
{
    paintAboutScreen(QStringLiteral("introduction_akregator.html"), introductionData());
}

void ArticleViewerWebEngine::paintAboutScreen(const QString &templateName, const QVariantHash &data)
{
    GrantleeTheme::ThemeManager manager(QStringLiteral("splashPage"),
                                        QStringLiteral("splash.theme"),
Laurent Montel's avatar
Laurent Montel committed
134
                                        nullptr,
Laurent Montel's avatar
Laurent Montel committed
135 136
                                        QStringLiteral("messageviewer/about/"));
    GrantleeTheme::Theme theme = manager.theme(QStringLiteral("default"));
Laurent Montel's avatar
Laurent Montel committed
137
    if (theme.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
138
        setHtml(theme.render(templateName, data, QByteArrayLiteral("akregator")),
Laurent Montel's avatar
Laurent Montel committed
139
                QUrl::fromLocalFile(theme.absolutePath() + QLatin1Char('/')));
Laurent Montel's avatar
Laurent Montel committed
140 141
    } else {
        qCDebug(AKREGATOR_LOG) << "Theme error: failed to find splash theme";
Laurent Montel's avatar
Laurent Montel committed
142 143 144
    }
}

Laurent Montel's avatar
Laurent Montel committed
145
void ArticleViewerWebEngine::slotServiceUrlSelected(PimCommon::ShareServiceUrlManager::ServiceType type)
Laurent Montel's avatar
Laurent Montel committed
146
{
Laurent Montel's avatar
Laurent Montel committed
147 148
    const QUrl url = mShareServiceManager->generateServiceUrl(mCurrentUrl.url(), QString(), type);
    mShareServiceManager->openUrl(url);
Laurent Montel's avatar
Laurent Montel committed
149 150
}

Laurent Montel's avatar
Laurent Montel committed
151
void ArticleViewerWebEngine::slotSaveLinkAs()
Laurent Montel's avatar
Laurent Montel committed
152 153 154 155 156 157 158 159 160 161
{
    QUrl tmp(mCurrentUrl);

    if (tmp.fileName().isEmpty()) {
        tmp = tmp.adjusted(QUrl::RemoveFilename);
        tmp.setPath(tmp.path() + QLatin1String("index.html"));
    }
    KParts::BrowserRun::simpleSave(tmp, tmp.fileName());
}

Laurent Montel's avatar
Laurent Montel committed
162
void ArticleViewerWebEngine::slotSaveImageOnDiskInFrame()
Laurent Montel's avatar
Laurent Montel committed
163 164 165 166
{
    slotSaveLinkAs();
}

Laurent Montel's avatar
Laurent Montel committed
167
void ArticleViewerWebEngine::slotCopyImageLocationInFrame()
Laurent Montel's avatar
Laurent Montel committed
168 169 170 171
{
    slotCopyLinkAddress();
}

Laurent Montel's avatar
Laurent Montel committed
172 173 174 175 176
void ArticleViewerWebEngine::slotMute(bool mute)
{
    page()->setAudioMuted(mute);
}

Laurent Montel's avatar
Laurent Montel committed
177
void ArticleViewerWebEngine::slotCopyLinkAddress()
Laurent Montel's avatar
Laurent Montel committed
178 179 180 181 182 183 184 185 186 187 188 189
{
    if (mCurrentUrl.isEmpty()) {
        return;
    }
    QClipboard *cb = QApplication::clipboard();
    cb->setText(mCurrentUrl.toString(), QClipboard::Clipboard);
    // don't set url to selection as it's a no-no according to a fd.o spec
    // which spec? Nobody seems to care (tested Firefox (3.5.10) Konqueror,and KMail (4.2.3)), so I re-enable the following line unless someone gives
    // a good reason to remove it again (bug 183022) --Frank
    cb->setText(mCurrentUrl.toString(), QClipboard::Selection);
}

Laurent Montel's avatar
Laurent Montel committed
190 191 192 193 194
void ArticleViewerWebEngine::contextMenuEvent(QContextMenuEvent *e)
{
    displayContextMenu(e->pos());
}

Laurent Montel's avatar
Laurent Montel committed
195
void ArticleViewerWebEngine::slotShowContextMenu(const QPoint &pos)
Laurent Montel's avatar
Laurent Montel committed
196 197 198 199
{
    displayContextMenu(pos);
}

Laurent Montel's avatar
Laurent Montel committed
200
void ArticleViewerWebEngine::slotCopy()
Laurent Montel's avatar
Laurent Montel committed
201
{
Laurent Montel's avatar
Laurent Montel committed
202
    triggerPageAction(QWebEnginePage::Copy);
Laurent Montel's avatar
Laurent Montel committed
203 204
}

Laurent Montel's avatar
Laurent Montel committed
205
void ArticleViewerWebEngine::slotLoadFinished()
Laurent Montel's avatar
Laurent Montel committed
206
{
207
    restoreCurrentPosition();
Laurent Montel's avatar
Laurent Montel committed
208
    unsetCursor();
209
    clearRelativePosition();
Laurent Montel's avatar
Laurent Montel committed
210 211
}

Laurent Montel's avatar
Laurent Montel committed
212
void ArticleViewerWebEngine::slotLoadStarted()
Laurent Montel's avatar
Laurent Montel committed
213
{
Laurent Montel's avatar
Laurent Montel committed
214
    mWebEngineViewAccessKey->hideAccessKeys();
Laurent Montel's avatar
Laurent Montel committed
215 216 217
    setCursor(Qt::WaitCursor);
}

218
void ArticleViewerWebEngine::slotWebHitFinished(const WebEngineViewer::WebHitTestResult &result)
Laurent Montel's avatar
Laurent Montel committed
219
{
220 221
    mCurrentUrl = result.linkUrl();
    if (URLHandlerWebEngineManager::instance()->handleContextMenuRequest(mCurrentUrl, mapToGlobal(result.pos()), this)) {
Laurent Montel's avatar
Laurent Montel committed
222 223 224 225
        return;
    }

    QMenu popup(this);
Laurent Montel's avatar
Laurent Montel committed
226
    const bool contentSelected = !selectedText().isEmpty();
Laurent Montel's avatar
Laurent Montel committed
227 228 229 230 231 232 233 234
    if (!contentSelected) {
        if (!mCurrentUrl.isEmpty()) {
            popup.addAction(createOpenLinkInNewTabAction(mCurrentUrl, this, SLOT(slotOpenLinkInForegroundTab()), &popup));
            popup.addAction(createOpenLinkInExternalBrowserAction(mCurrentUrl, this, SLOT(slotOpenLinkInBrowser()), &popup));
            popup.addSeparator();
            popup.addAction(mActionCollection->action(QStringLiteral("savelinkas")));
            popup.addAction(mActionCollection->action(QStringLiteral("copylinkaddress")));
        }
235
        if (!result.imageUrl().isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
236 237 238 239
            popup.addSeparator();
            popup.addAction(mActionCollection->action(QStringLiteral("copy_image_location")));
            popup.addAction(mActionCollection->action(QStringLiteral("saveas_imageurl")));
        }
Laurent Montel's avatar
Laurent Montel committed
240 241
        popup.addSeparator();
        popup.addActions(viewerPluginActionList(MessageViewer::ViewerPluginInterface::NeedUrl));
Laurent Montel's avatar
Laurent Montel committed
242 243 244
        popup.addSeparator();
        popup.addAction(mShareServiceManager->menu());
    } else {
Laurent Montel's avatar
Laurent Montel committed
245 246 247 248
        popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_copy")));
        popup.addSeparator();
        mWebShortcutMenuManager->setSelectedText(page()->selectedText());
        mWebShortcutMenuManager->addWebShortcutsToMenu(&popup);
249 250
        popup.addSeparator();
        popup.addActions(viewerPluginActionList(MessageViewer::ViewerPluginInterface::NeedSelection));
Laurent Montel's avatar
Laurent Montel committed
251 252 253 254
    }
    popup.addSeparator();
    popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_print")));
    popup.addAction(ActionManager::getInstance()->action(QStringLiteral("viewer_printpreview")));
Laurent Montel's avatar
Laurent Montel committed
255
    popup.addSeparator();
Laurent Montel's avatar
Laurent Montel committed
256 257
    popup.addAction(ActionManager::getInstance()->action(QStringLiteral("tab_mute")));
    popup.addAction(ActionManager::getInstance()->action(QStringLiteral("tab_unmute")));
Laurent Montel's avatar
Laurent Montel committed
258 259 260 261 262 263
    popup.addSeparator();
    popup.addAction(ActionManager::getInstance()->action(QStringLiteral("find_in_messages")));
    if (KPIMTextEdit::TextToSpeech::self()->isReady()) {
        popup.addSeparator();
        popup.addAction(ActionManager::getInstance()->action(QStringLiteral("speak_text")));
    }
264 265 266 267 268
    popup.exec(mapToGlobal(result.pos()));
}

void ArticleViewerWebEngine::displayContextMenu(const QPoint &pos)
{
269 270
    WebEngineViewer::WebHitTest *webHit = mPageEngine->hitTestContent(pos);
    connect(webHit, &WebEngineViewer::WebHitTest::finished, this, &ArticleViewerWebEngine::slotWebHitFinished);
Laurent Montel's avatar
Laurent Montel committed
271 272
}

Laurent Montel's avatar
Laurent Montel committed
273
void ArticleViewerWebEngine::slotLinkHovered(const QString &link)
Laurent Montel's avatar
Laurent Montel committed
274
{
Laurent Montel's avatar
Laurent Montel committed
275
    QString msg = URLHandlerWebEngineManager::instance()->statusBarMessage(QUrl(link), this);
Laurent Montel's avatar
Laurent Montel committed
276 277 278 279 280 281 282
    if (msg.isEmpty()) {
        msg = link;
    }

    Q_EMIT showStatusBarMessage(msg);
}

Laurent Montel's avatar
Laurent Montel committed
283
void ArticleViewerWebEngine::forwardKeyReleaseEvent(QKeyEvent *e)
Laurent Montel's avatar
Laurent Montel committed
284 285
{
    if (Settings::self()->accessKeyEnabled()) {
Laurent Montel's avatar
Laurent Montel committed
286
        mWebEngineViewAccessKey->keyReleaseEvent(e);
Laurent Montel's avatar
Laurent Montel committed
287 288 289
    }
}

Laurent Montel's avatar
Laurent Montel committed
290
void ArticleViewerWebEngine::forwardKeyPressEvent(QKeyEvent *e)
Laurent Montel's avatar
Laurent Montel committed
291 292 293
{
    if (e && hasFocus()) {
        if (Settings::self()->accessKeyEnabled()) {
Laurent Montel's avatar
Laurent Montel committed
294
            mWebEngineViewAccessKey->keyPressEvent(e);
Laurent Montel's avatar
Laurent Montel committed
295 296 297 298
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
299
void ArticleViewerWebEngine::forwardWheelEvent(QWheelEvent *e)
Laurent Montel's avatar
Laurent Montel committed
300 301
{
    if (Settings::self()->accessKeyEnabled()) {
Laurent Montel's avatar
Laurent Montel committed
302
        mWebEngineViewAccessKey->wheelEvent(e);
Laurent Montel's avatar
Laurent Montel committed
303
    }
304 305 306 307 308 309 310 311
    if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
        const int numDegrees = e->delta() / 8;
        const int numSteps = numDegrees / 15;
        const qreal factor = ActionManager::getInstance()->zoomActionMenu()->zoomFactor() + numSteps * 10;
        if (factor >= 10 && factor <= 300) {
            ActionManager::getInstance()->zoomActionMenu()->setZoomFactor(factor);
            ActionManager::getInstance()->zoomActionMenu()->setWebViewerZoomFactor(factor / 100.0);
        }
Laurent Montel's avatar
Laurent Montel committed
312
        e->accept();
313
    }
Laurent Montel's avatar
Laurent Montel committed
314 315
}

Laurent Montel's avatar
Laurent Montel committed
316
void ArticleViewerWebEngine::resizeEvent(QResizeEvent *e)
Laurent Montel's avatar
Laurent Montel committed
317 318
{
    if (Settings::self()->accessKeyEnabled()) {
Laurent Montel's avatar
Laurent Montel committed
319
        mWebEngineViewAccessKey->resizeEvent(e);
Laurent Montel's avatar
Laurent Montel committed
320
    }
Laurent Montel's avatar
Laurent Montel committed
321
    QWebEngineView::resizeEvent(e);
Laurent Montel's avatar
Laurent Montel committed
322 323
}

Laurent Montel's avatar
Laurent Montel committed
324
void ArticleViewerWebEngine::disableIntroduction()
Laurent Montel's avatar
Laurent Montel committed
325 326 327 328 329 330 331 332 333 334 335 336
{
    KGuiItem yesButton(KStandardGuiItem::yes());
    yesButton.setText(i18n("Disable"));
    KGuiItem noButton(KStandardGuiItem::no());
    noButton.setText(i18n("Keep Enabled"));
    if (KMessageBox::questionYesNo(this, i18n("Are you sure you want to disable this introduction page?"),
                                   i18n("Disable Introduction Page"), yesButton, noButton) == KMessageBox::Yes) {
        Settings::self()->setDisableIntroduction(true);
        Settings::self()->save();
    }
}

Laurent Montel's avatar
Laurent Montel committed
337
void ArticleViewerWebEngine::setArticleAction(ArticleViewerWebEngine::ArticleAction type, const QString &articleId, const QString &feed)
Laurent Montel's avatar
Laurent Montel committed
338 339 340 341
{
    Q_EMIT articleAction(type, articleId, feed);
}

342 343
void ArticleViewerWebEngine::restoreCurrentPosition()
{
Laurent Montel's avatar
Laurent Montel committed
344
    mPageEngine->runJavaScript(WebEngineViewer::WebEngineScript::scrollToRelativePosition(relativePosition()));
345 346
}

Laurent Montel's avatar
Laurent Montel committed
347
void ArticleViewerWebEngine::forwardMouseReleaseEvent(QMouseEvent *event)
Laurent Montel's avatar
Laurent Montel committed
348 349 350 351 352 353 354 355 356 357 358
{
    if (event->button() & Qt::RightButton) {
        Q_EMIT showContextMenu(event->pos());
        mLastButtonClicked = RightButton;
    } else if (event->button() & Qt::MiddleButton) {
        mLastButtonClicked = MiddleButton;
    } else if (event->button() & Qt::LeftButton) {
        mLastButtonClicked = LeftButton;
    }
}

Laurent Montel's avatar
Laurent Montel committed
359 360 361 362 363 364 365 366
bool ArticleViewerWebEngine::urlIsAMalwareButContinue()
{
    if (KMessageBox::No == KMessageBox::warningYesNo(this, i18n("This web site is a malware, do you want to continue to show it?"), i18n("Malware"))) {
        return false;
    }
    return true;
}

367
void ArticleViewerWebEngine::slotCheckedUrlFinished(const QUrl &url, WebEngineViewer::CheckPhishingUrlUtil::UrlStatus status)
Laurent Montel's avatar
Laurent Montel committed
368 369
{
    switch (status) {
Laurent Montel's avatar
Laurent Montel committed
370
    case WebEngineViewer::CheckPhishingUrlUtil::BrokenNetwork:
Laurent Montel's avatar
Laurent Montel committed
371 372
        KMessageBox::error(this, i18n("The network is broken."), i18n("Check Phishing Url"));
        break;
Laurent Montel's avatar
Laurent Montel committed
373
    case WebEngineViewer::CheckPhishingUrlUtil::InvalidUrl:
Laurent Montel's avatar
Laurent Montel committed
374 375
        KMessageBox::error(this, i18n("The url %1 is not valid.", url.toString()), i18n("Check Phishing Url"));
        break;
Laurent Montel's avatar
Laurent Montel committed
376
    case WebEngineViewer::CheckPhishingUrlUtil::Ok:
Laurent Montel's avatar
Laurent Montel committed
377
        break;
Laurent Montel's avatar
Laurent Montel committed
378
    case WebEngineViewer::CheckPhishingUrlUtil::MalWare:
Laurent Montel's avatar
Laurent Montel committed
379
        if (!urlIsAMalwareButContinue()) {
Laurent Montel's avatar
Laurent Montel committed
380 381 382
            return;
        }
        break;
Laurent Montel's avatar
Laurent Montel committed
383
    case WebEngineViewer::CheckPhishingUrlUtil::Unknown:
384
        qCWarning(AKREGATOR_LOG) << "ArticleViewerWebEngine::slotCheckedUrlFinished unknown error ";
Laurent Montel's avatar
Laurent Montel committed
385 386
        break;
    }
Laurent Montel's avatar
Laurent Montel committed
387
    openSafeUrl(url);
388

Laurent Montel's avatar
Laurent Montel committed
389 390
}

Laurent Montel's avatar
Laurent Montel committed
391
void ArticleViewerWebEngine::slotLinkClicked(const QUrl &url)
Laurent Montel's avatar
Laurent Montel committed
392
{
Laurent Montel's avatar
Laurent Montel committed
393
    if (URLHandlerWebEngineManager::instance()->handleClick(url, this)) {
Laurent Montel's avatar
Laurent Montel committed
394 395
        return;
    }
396
    if (Settings::checkPhishingUrl()) {
397
        WebEngineViewer::LocalDataBaseManager::self()->checkUrl(url);
398 399 400
    } else {
        openSafeUrl(url);
    }
Laurent Montel's avatar
Laurent Montel committed
401 402 403 404
}

void ArticleViewerWebEngine::openSafeUrl(const QUrl &url)
{
Laurent Montel's avatar
Laurent Montel committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    mCurrentUrl = url;
    OpenUrlRequest req(mCurrentUrl);
    if (mLastButtonClicked == LeftButton) {
        switch (Settings::lMBBehaviour()) {
        case Settings::EnumLMBBehaviour::OpenInExternalBrowser:
            req.setOptions(OpenUrlRequest::ExternalBrowser);
            break;
        case Settings::EnumLMBBehaviour::OpenInBackground:
            req.setOpenInBackground(true);
            req.setOptions(OpenUrlRequest::NewTab);
            break;
        default:
            break;
        }
    } else if (mLastButtonClicked == MiddleButton) {
        switch (Settings::mMBBehaviour()) {
        case Settings::EnumMMBBehaviour::OpenInExternalBrowser:
            req.setOptions(OpenUrlRequest::ExternalBrowser);
            break;
        case Settings::EnumMMBBehaviour::OpenInBackground:
            req.setOpenInBackground(true);
            req.setOptions(OpenUrlRequest::NewTab);
            break;
        default:
            break;
        }
    }
    Q_EMIT signalOpenUrlRequest(req);
}

Laurent Montel's avatar
Laurent Montel committed
435
void ArticleViewerWebEngine::slotOpenLinkInForegroundTab()
Laurent Montel's avatar
Laurent Montel committed
436 437 438 439 440 441
{
    OpenUrlRequest req(mCurrentUrl);
    req.setOptions(OpenUrlRequest::NewTab);
    Q_EMIT signalOpenUrlRequest(req);
}

Laurent Montel's avatar
Laurent Montel committed
442
void ArticleViewerWebEngine::slotOpenLinkInBackgroundTab()
Laurent Montel's avatar
Laurent Montel committed
443 444 445 446 447 448 449
{
    OpenUrlRequest req(mCurrentUrl);
    req.setOptions(OpenUrlRequest::NewTab);
    req.setOpenInBackground(true);
    Q_EMIT signalOpenUrlRequest(req);
}

Laurent Montel's avatar
Laurent Montel committed
450
void ArticleViewerWebEngine::slotOpenLinkInBrowser()
Laurent Montel's avatar
Laurent Montel committed
451 452 453 454 455 456
{
    OpenUrlRequest req(mCurrentUrl);
    req.setOptions(OpenUrlRequest::ExternalBrowser);
    Q_EMIT signalOpenUrlRequest(req);
}

457 458 459 460 461 462 463
void ArticleViewerWebEngine::createViewerPluginToolManager(KActionCollection *ac, QWidget *parent)
{
    mViewerPluginToolManager = new MessageViewer::ViewerPluginToolManager(parent, this);
    mViewerPluginToolManager->setActionCollection(ac);
    mViewerPluginToolManager->setPluginName(QStringLiteral("akregator"));
    mViewerPluginToolManager->setServiceTypeName(QStringLiteral("Akregator/ViewerPlugin"));
    if (!mViewerPluginToolManager->initializePluginList()) {
Laurent Montel's avatar
Laurent Montel committed
464
        qCWarning(AKREGATOR_LOG) << " Impossible to initialize plugins";
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    }
    mViewerPluginToolManager->createView();
    connect(mViewerPluginToolManager, &MessageViewer::ViewerPluginToolManager::activatePlugin, this, &ArticleViewerWebEngine::slotActivatePlugin);
}

QList<QAction *> ArticleViewerWebEngine::viewerPluginActionList(MessageViewer::ViewerPluginInterface::SpecificFeatureTypes features)
{
    if (mViewerPluginToolManager) {
        return mViewerPluginToolManager->viewerPluginActionList(features);
    }
    return QList<QAction *>();
}

void ArticleViewerWebEngine::slotActivatePlugin(MessageViewer::ViewerPluginInterface *interface)
{
    const QString text = selectedText();
    if (!text.isEmpty()) {
        interface->setText(text);
    }
Laurent Montel's avatar
Laurent Montel committed
484
    interface->setUrl(mCurrentUrl);
Laurent Montel's avatar
Laurent Montel committed
485
    interface->execute();
486
}