adblockinterceptorinterface.cpp 3.16 KB
Newer Older
1
/*
2
   SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel@kde.org>
3

4
   SPDX-License-Identifier: GPL-2.0-or-later
5 6 7
*/

#include "adblockinterceptorinterface.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include "../lib/widgets/adblockblockableitemsdialog.h"
9
#include "../lib/adblockblockableitemsjob.h"
10
#include <QtWebEngineCore/qwebengineurlrequestinfo.h>
Laurent Montel's avatar
Laurent Montel committed
11
#include "lib/adblockmanager.h"
Laurent Montel's avatar
Laurent Montel committed
12 13 14
#include <KLocalizedString>
#include <KActionCollection>
#include <QAction>
Laurent Montel's avatar
Laurent Montel committed
15 16
#include <QPointer>
#include <QWebEngineView>
Laurent Montel's avatar
Laurent Montel committed
17
#include <WebEngineViewer/WebHitTestResult>
18 19

AdblockInterceptorInterface::AdblockInterceptorInterface(QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
20
    : WebEngineViewer::NetworkPluginUrlInterceptorInterface(parent)
21
{
22
    mAdblockManager = new AdBlock::AdblockManager(this);
23 24 25 26 27 28
}

AdblockInterceptorInterface::~AdblockInterceptorInterface()
{
}

Laurent Montel's avatar
Fix api  
Laurent Montel committed
29
bool AdblockInterceptorInterface::interceptRequest(QWebEngineUrlRequestInfo &info)
30
{
31
    if (!mAdblockManager->isEnabled()) {
Laurent Montel's avatar
Fix api  
Laurent Montel committed
32
        return false;
33
    }
Laurent Montel's avatar
Laurent Montel committed
34
    return mAdblockManager->interceptRequest(info);
35
}
Laurent Montel's avatar
Laurent Montel committed
36

Laurent Montel's avatar
Laurent Montel committed
37
QList<QAction *> AdblockInterceptorInterface::interceptorUrlActions(const WebEngineViewer::WebHitTestResult &result) const
Laurent Montel's avatar
Laurent Montel committed
38
{
Laurent Montel's avatar
Laurent Montel committed
39 40 41
    QList<QAction *> lstAction;
    if (mAdblockManager->isEnabled()) {
        lstAction.append(mShowBlockableItems);
Laurent Montel's avatar
Laurent Montel committed
42 43 44
        if (!result.imageUrl().isEmpty()) {
            lstAction.append(mBlockImage);
        }
Laurent Montel's avatar
Laurent Montel committed
45
        mCurrentWebHitTest = result;
Laurent Montel's avatar
Laurent Montel committed
46 47
    }
    return lstAction;
Laurent Montel's avatar
Laurent Montel committed
48 49 50 51 52
}

void AdblockInterceptorInterface::createActions(KActionCollection *ac)
{
    if (ac) {
Laurent Montel's avatar
Laurent Montel committed
53 54 55
        mShowBlockableItems = new QAction(i18n("Open Blockable Items..."), this);
        ac->addAction(QStringLiteral("adblock_blockable_items"), mShowBlockableItems);
        connect(mShowBlockableItems, &QAction::triggered, this, &AdblockInterceptorInterface::slotShowBlockableElement);
Laurent Montel's avatar
Laurent Montel committed
56

Laurent Montel's avatar
Laurent Montel committed
57 58 59 60
        mBlockImage = new QAction(i18n("Block Image"), this);
        ac->addAction(QStringLiteral("adblock_image"), mBlockImage);
        ac->setShortcutsConfigurable(mBlockImage, false);
        connect(mBlockImage, &QAction::triggered, this, &AdblockInterceptorInterface::slotBlockImage);
Laurent Montel's avatar
Laurent Montel committed
61 62
    }
}
Laurent Montel's avatar
Laurent Montel committed
63 64 65 66 67 68 69 70 71 72

QWebEngineView *AdblockInterceptorInterface::webEngineView() const
{
    return mWebEngineView;
}

void AdblockInterceptorInterface::setWebEngineView(QWebEngineView *webEngineView)
{
    mWebEngineView = webEngineView;
}
Laurent Montel's avatar
Laurent Montel committed
73

Laurent Montel's avatar
Laurent Montel committed
74 75
void AdblockInterceptorInterface::slotBlockImage()
{
Laurent Montel's avatar
Laurent Montel committed
76
    if (mCurrentWebHitTest.imageUrl().isValid()) {
Laurent Montel's avatar
Debug--  
Laurent Montel committed
77
        //qDebug() << " mCurrentWebHitTest.imageUrl()"<<mCurrentWebHitTest.imageUrl();
Laurent Montel's avatar
Laurent Montel committed
78 79
        //TODO block it.
    }
Laurent Montel's avatar
Laurent Montel committed
80 81 82
    //TODO
}

83 84
void AdblockInterceptorInterface::slotSearchItemsDone(const QVector<AdBlock::AdBlockResult> &result)
{
Laurent Montel's avatar
Laurent Montel committed
85
    //TODO update
86 87 88 89 90 91
    QPointer<AdBlock::AdBlockBlockableItemsDialog> dlg = new AdBlock::AdBlockBlockableItemsDialog(mWebEngineView);
    dlg->setAdblockResult(result);
    dlg->exec();
    delete dlg;
}

Laurent Montel's avatar
Laurent Montel committed
92 93
void AdblockInterceptorInterface::slotShowBlockableElement()
{
Laurent Montel's avatar
Laurent Montel committed
94
    if (mWebEngineView) {
95 96 97 98
        AdBlock::AdBlockBlockableItemsJob *job = new AdBlock::AdBlockBlockableItemsJob(this);
        job->setWebEngineView(mWebEngineView);
        connect(job, &AdBlock::AdBlockBlockableItemsJob::searchItemsDone, this, &AdblockInterceptorInterface::slotSearchItemsDone);
        job->start();
Laurent Montel's avatar
Laurent Montel committed
99
    }
Laurent Montel's avatar
Laurent Montel committed
100
}