Basic AdBlock functionality - #123.

This commit is contained in:
Martin Rotter 2017-07-20 12:21:18 +02:00
parent 29d8b6762d
commit e41dbbfe41
16 changed files with 104 additions and 31 deletions

View File

@ -2,7 +2,7 @@
————— —————
Added: Added:
▪ New Ad-Block feature for internal web browser. Based on Qt examples and QupZilla. Original license GNU GPLv3 is retained. ▪ New Ad-Block feature for internal web browser. Based on Qt examples and QupZilla. Original license GNU GPLv3 is retained. (#123)
3.4.1 3.4.1
————— —————

View File

@ -55,9 +55,19 @@
#include <QTimer> #include <QTimer>
#include <QFileDialog> #include <QFileDialog>
#if defined(USE_WEBENGINE)
#include "network-web/adblock/adblockicon.h"
#endif
FormMain::FormMain(QWidget *parent, Qt::WindowFlags f) FormMain::FormMain(QWidget *parent, Qt::WindowFlags f)
: QMainWindow(parent, f), m_ui(new Ui::FormMain) { : QMainWindow(parent, f), m_ui(new Ui::FormMain) {
#if defined(USE_WEBENGINE)
m_adblockIcon = new AdBlockIcon(this);
m_adblockIconAction = m_adblockIcon->menuAction();
m_adblockIconAction->setObjectName(QSL("m_adblockIconAction"));
#endif
m_ui->setupUi(this); m_ui->setupUi(this);
qApp->setMainForm(this); qApp->setMainForm(this);
@ -66,6 +76,10 @@ FormMain::FormMain(QWidget *parent, Qt::WindowFlags f)
// even if main menu is not visible. // even if main menu is not visible.
addActions(allActions()); addActions(allActions());
#if defined(USE_WEBENGINE)
addAction(m_adblockIconAction);
#endif
m_statusBar = new StatusBar(this); m_statusBar = new StatusBar(this);
setStatusBar(m_statusBar); setStatusBar(m_statusBar);
@ -182,6 +196,7 @@ QList<QAction*> FormMain::allActions() const {
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
actions << m_ui->m_actionTabNewWebBrowser; actions << m_ui->m_actionTabNewWebBrowser;
actions << m_adblockIconAction;
#endif #endif
actions << m_ui->m_actionTabsCloseAll; actions << m_ui->m_actionTabsCloseAll;

View File

@ -23,6 +23,10 @@
#include "ui_formmain.h" #include "ui_formmain.h"
#if defined(USE_WEBENGINE)
class AdBlockIcon;
#endif
class StatusBar; class StatusBar;
class FormMain : public QMainWindow { class FormMain : public QMainWindow {
@ -55,6 +59,12 @@ class FormMain : public QMainWindow {
void loadSize(); void loadSize();
void saveSize(); void saveSize();
#if defined(USE_WEBENGINE)
AdBlockIcon *adblockIcon() const {
return m_adblockIcon;
}
#endif
public slots: public slots:
// Displays window on top or switches its visibility. // Displays window on top or switches its visibility.
void display(); void display();
@ -102,6 +112,11 @@ class FormMain : public QMainWindow {
// Sets up proper icons for this widget. // Sets up proper icons for this widget.
void setupIcons(); void setupIcons();
#if defined(USE_WEBENGINE)
AdBlockIcon *m_adblockIcon;
QAction *m_adblockIconAction;
#endif
QScopedPointer<Ui::FormMain> m_ui; QScopedPointer<Ui::FormMain> m_ui;
QMenu *m_trayMenu; QMenu *m_trayMenu;
StatusBar *m_statusBar; StatusBar *m_statusBar;

View File

@ -24,6 +24,7 @@
#include "gui/dialogs/formmain.h" #include "gui/dialogs/formmain.h"
#include "gui/tabwidget.h" #include "gui/tabwidget.h"
#include "gui/webbrowser.h" #include "gui/webbrowser.h"
#include "network-web/adblock/adblockicon.h"
#include <QWheelEvent> #include <QWheelEvent>
@ -140,6 +141,18 @@ void WebViewer::clear() {
setEnabled(previously_enabled); setEnabled(previously_enabled);
} }
void WebViewer::contextMenuEvent(QContextMenuEvent *event) {
event->accept();
QMenu *menu = page()->createStandardContextMenu();
menu->addAction(qApp->mainForm()->adblockIcon()->menuAction());
const QPoint pos = event->globalPos();
QPoint p(pos.x(), pos.y() + 1);
menu->popup(p);
}
QWebEngineView *WebViewer::createWindow(QWebEnginePage::WebWindowType type) { QWebEngineView *WebViewer::createWindow(QWebEnginePage::WebWindowType type) {
Q_UNUSED(type) Q_UNUSED(type)

View File

@ -51,6 +51,8 @@ class WebViewer : public QWebEngineView {
void clear(); void clear();
protected: protected:
void contextMenuEvent(QContextMenuEvent *event);
QWebEngineView *createWindow(QWebEnginePage::WebWindowType type); QWebEngineView *createWindow(QWebEnginePage::WebWindowType type);
void wheelEvent(QWheelEvent *event); void wheelEvent(QWheelEvent *event);

View File

@ -40,6 +40,9 @@
#include <QProcess> #include <QProcess>
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
#include "network-web/adblock/adblockicon.h"
#include "network-web/adblock/adblockmanager.h"
#include <QWebEngineProfile> #include <QWebEngineProfile>
#include <QWebEngineDownloadItem> #include <QWebEngineDownloadItem>
#endif #endif
@ -194,7 +197,7 @@ void Application::setMainForm(FormMain *main_form) {
} }
QString Application::getConfigHomePath() { QString Application::getConfigHomePath() {
return IOFactory::getSystemFolder(QStandardPaths::ConfigLocation); return IOFactory::getSystemFolder(QStandardPaths::GenericConfigLocation);
} }
QString Application::getUserDataAppPath() { QString Application::getUserDataAppPath() {
@ -374,6 +377,10 @@ void Application::onAboutToQuit() {
eliminateFirstRun(); eliminateFirstRun();
eliminateFirstRun(APP_VERSION); eliminateFirstRun(APP_VERSION);
#if defined(USE_WEBENGINE)
AdBlockManager::instance()->save();
#endif
// Make sure that we obtain close lock BEFORE even trying to quit the application. // Make sure that we obtain close lock BEFORE even trying to quit the application.
const bool locked_safely = feedUpdateLock()->tryLock(4 * CLOSE_LOCK_TIMEOUT); const bool locked_safely = feedUpdateLock()->tryLock(4 * CLOSE_LOCK_TIMEOUT);

View File

@ -354,7 +354,7 @@ Settings *Settings::setupSettings(QObject *parent) {
SettingsProperties Settings::determineProperties() { SettingsProperties Settings::determineProperties() {
SettingsProperties properties; SettingsProperties properties;
properties.m_settingsSuffix = QDir::separator() + QString(APP_CFG_PATH) + QDir::separator() + QString(APP_CFG_FILE); properties.m_settingsSuffix = QDir::separator() + QSL(APP_CFG_PATH) + QDir::separator() + QSL(APP_CFG_FILE);
const QString app_path = qApp->getUserDataAppPath(); const QString app_path = qApp->getUserDataAppPath();
const QString home_path = qApp->getUserDataHomePath(); const QString home_path = qApp->getUserDataHomePath();

View File

@ -42,12 +42,12 @@ AdBlockDialog::AdBlockDialog(QWidget* parent)
m_ui->adblockCheckBox->setChecked(m_manager->isEnabled()); m_ui->adblockCheckBox->setChecked(m_manager->isEnabled());
QMenu* menu = new QMenu(m_ui->buttonOptions); QMenu* menu = new QMenu(m_ui->buttonOptions);
m_actionAddRule = menu->addAction(tr("Add Rule"), this, SLOT(addRule())); m_actionAddRule = menu->addAction(tr("Add rule"), this, SLOT(addRule()));
m_actionRemoveRule = menu->addAction(tr("Remove Rule"), this, SLOT(removeRule())); m_actionRemoveRule = menu->addAction(tr("Remove rule"), this, SLOT(removeRule()));
menu->addSeparator(); menu->addSeparator();
m_actionAddSubscription = menu->addAction(tr("Add Subscription"), this, SLOT(addSubscription())); m_actionAddSubscription = menu->addAction(tr("Add subscription"), this, SLOT(addSubscription()));
m_actionRemoveSubscription = menu->addAction(tr("Remove Subscription"), this, SLOT(removeSubscription())); m_actionRemoveSubscription = menu->addAction(tr("Remove subscription"), this, SLOT(removeSubscription()));
menu->addAction(tr("Update Subscriptions"), m_manager, SLOT(updateAllSubscriptions())); menu->addAction(tr("Update subscriptions"), m_manager, SLOT(updateAllSubscriptions()));
menu->addSeparator(); menu->addSeparator();
menu->addAction(tr("Learn about writing rules..."), this, SLOT(learnAboutRules())); menu->addAction(tr("Learn about writing rules..."), this, SLOT(learnAboutRules()));
@ -56,7 +56,7 @@ AdBlockDialog::AdBlockDialog(QWidget* parent)
connect(m_ui->adblockCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableAdBlock(bool))); connect(m_ui->adblockCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableAdBlock(bool)));
connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int))); connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));
connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(close())); connect(m_ui->buttonBox, &QDialogButtonBox::clicked, this, &AdBlockDialog::close);
load(); load();

View File

@ -2,6 +2,9 @@
<ui version="4.0"> <ui version="4.0">
<class>AdBlockDialog</class> <class>AdBlockDialog</class>
<widget class="QWidget" name="AdBlockDialog"> <widget class="QWidget" name="AdBlockDialog">
<property name="windowModality">
<enum>Qt::NonModal</enum>
</property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
@ -11,7 +14,7 @@
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>AdBlock Configuration</string> <string>AdBlock configuration</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
@ -104,7 +107,7 @@
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
<property name="standardButtons"> <property name="standardButtons">
<set>QDialogButtonBox::Ok</set> <set>QDialogButtonBox::Close</set>
</property> </property>
</widget> </widget>
</item> </item>

View File

@ -28,17 +28,20 @@
#include "gui/dialogs/formmain.h" #include "gui/dialogs/formmain.h"
#include <QMenu> #include <QMenu>
#include <QMessageBox>
#include <QTimer> #include <QTimer>
AdBlockIcon::AdBlockIcon(BrowserWindow* window, QWidget* parent) AdBlockIcon::AdBlockIcon(QWidget* parent)
: ClickableLabel(parent), m_window(window), m_menuAction(0), m_flashTimer(0), m_timerTicks(0), m_enabled(false) { : ClickableLabel(parent), m_menuAction(0), m_flashTimer(0), m_timerTicks(0), m_enabled(false) {
setCursor(Qt::PointingHandCursor); setCursor(Qt::PointingHandCursor);
setToolTip(tr("AdBlock lets you block unwanted content on web pages")); setToolTip(tr("AdBlock lets you block unwanted content on web pages"));
setFixedSize(16, 16); setFixedSize(16, 16);
connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint))); connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint)));
connect(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool))); connect(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool)));
m_enabled = AdBlockManager::instance()->isEnabled();
} }
AdBlockIcon::~AdBlockIcon() { AdBlockIcon::~AdBlockIcon() {
@ -81,8 +84,9 @@ void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) {
QAction *AdBlockIcon::menuAction() { QAction *AdBlockIcon::menuAction() {
if (!m_menuAction) { if (!m_menuAction) {
m_menuAction = new QAction(tr("AdBlock"), this); m_menuAction = new QAction(tr("AdBlock"), this);
m_menuAction->setMenu(new QMenu); m_menuAction->setMenu(new QMenu(this));
connect(m_menuAction->menu(), SIGNAL(aboutToShow()), this, SLOT(createMenu())); connect(m_menuAction->menu(), SIGNAL(aboutToShow()), this, SLOT(createMenu()));
connect(m_menuAction, &QAction::triggered, AdBlockManager::instance(), &AdBlockManager::showDialog);
} }
m_menuAction->setIcon(m_enabled ? qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE) : qApp->icons()->miscIcon(ADBLOCK_ICON_DISABLED)); m_menuAction->setIcon(m_enabled ? qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE) : qApp->icons()->miscIcon(ADBLOCK_ICON_DISABLED));
@ -203,5 +207,9 @@ void AdBlockIcon::setEnabled(bool enabled) {
setPixmap(qApp->icons()->miscIcon(ADBLOCK_ICON_DISABLED).pixmap(16)); setPixmap(qApp->icons()->miscIcon(ADBLOCK_ICON_DISABLED).pixmap(16));
} }
if (m_menuAction != nullptr) {
m_menuAction->setIcon(enabled ? qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE) : qApp->icons()->miscIcon(ADBLOCK_ICON_DISABLED));
}
m_enabled = enabled; m_enabled = enabled;
} }

View File

@ -35,7 +35,7 @@ class AdBlockIcon : public ClickableLabel {
Q_OBJECT Q_OBJECT
public: public:
explicit AdBlockIcon(BrowserWindow* window, QWidget* parent = 0); explicit AdBlockIcon(QWidget* parent = 0);
virtual ~AdBlockIcon(); virtual ~AdBlockIcon();
void popupBlocked(const QString &ruleString, const QUrl &url); void popupBlocked(const QString &ruleString, const QUrl &url);
@ -53,7 +53,6 @@ class AdBlockIcon : public ClickableLabel {
void stopAnimation(); void stopAnimation();
private: private:
BrowserWindow *m_window;
QAction *m_menuAction; QAction *m_menuAction;
QVector<QPair<AdBlockRule*, QUrl> > m_blockedPopups; QVector<QPair<AdBlockRule*, QUrl> > m_blockedPopups;

View File

@ -102,7 +102,8 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo& request) {
// We are blocking main URL frame, we can display "AdBlock error page" or // We are blocking main URL frame, we can display "AdBlock error page" or
// redirect to somewhere. // redirect to somewhere.
// TODO: dodělat lepší // TODO: dodělat lepší
QMessageBox::warning(nullptr, "blocked website", "blocket"); qApp->showGuiMessage(request.requestUrl().toString(), "aaa", QSystemTrayIcon::Warning);
request.redirect(QUrl("http://www.seznam.cz"));
// TODO request.redirect() přesměrovat na "chybovou stranku"; // TODO request.redirect() přesměrovat na "chybovou stranku";
// QUrl url(QSL("rssguard:adblock")); // QUrl url(QSL("rssguard:adblock"));
@ -113,12 +114,13 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo& request) {
// url.setQuery(query); // url.setQuery(query);
// request.redirect(url); // request.redirect(url);
request.block(true); //request.block(true);
} }
else { else {
request.block(true); request.block(true);
} }
} }
return res; return res;
} }
@ -227,7 +229,7 @@ AdBlockCustomList *AdBlockManager::customList() const {
return 0; return 0;
} }
QString AdBlockManager::storedListsPath() const { QString AdBlockManager::storedListsPath() {
return qApp->getUserDataPath() + QDir::separator() + ADBLOCK_LISTS_SUBDIRECTORY; return qApp->getUserDataPath() + QDir::separator() + ADBLOCK_LISTS_SUBDIRECTORY;
} }
@ -297,7 +299,7 @@ void AdBlockManager::load() {
m_subscriptions.append(customList); m_subscriptions.append(customList);
// Load all subscriptions // Load all subscriptions
foreach (AdBlockSubscription* subscription, m_subscriptions) { foreach (AdBlockSubscription *subscription, m_subscriptions) {
subscription->loadSubscription(m_disabledRules); subscription->loadSubscription(m_disabledRules);
// TODO: po zmene subskripce prehrat user css? // TODO: po zmene subskripce prehrat user css?

View File

@ -24,6 +24,7 @@
#include <QPointer> #include <QPointer>
#include <QStringList> #include <QStringList>
class QUrl; class QUrl;
class QWebEngineUrlRequestInfo; class QWebEngineUrlRequestInfo;
class AdBlockMatcher; class AdBlockMatcher;
@ -60,15 +61,15 @@ class AdBlockManager : public QObject {
bool addSubscriptionFromUrl(const QUrl& url); bool addSubscriptionFromUrl(const QUrl& url);
AdBlockSubscription* addSubscription(const QString& title, AdBlockSubscription *addSubscription(const QString& title,
const QString& url); const QString &url);
bool removeSubscription(AdBlockSubscription* subscription); bool removeSubscription(AdBlockSubscription* subscription);
AdBlockCustomList* customList() const; AdBlockCustomList *customList() const;
QString storedListsPath() const; static QString storedListsPath();
static AdBlockManager* instance(); static AdBlockManager *instance();
signals: signals:
void enabledChanged(bool enabled); void enabledChanged(bool enabled);
@ -80,10 +81,10 @@ class AdBlockManager : public QObject {
void updateMatcher(); void updateMatcher();
void updateAllSubscriptions(); void updateAllSubscriptions();
AdBlockDialog* showDialog(); AdBlockDialog *showDialog();
private: private:
inline bool canBeBlocked(const QUrl& url) const; inline bool canBeBlocked(const QUrl &url) const;
bool m_loaded; bool m_loaded;
bool m_enabled; bool m_enabled;

View File

@ -24,7 +24,7 @@
#include "definitions/definitions.h" #include "definitions/definitions.h"
AdBlockMatcher::AdBlockMatcher(AdBlockManager* manager) AdBlockMatcher::AdBlockMatcher(AdBlockManager *manager)
: QObject(manager), m_manager(manager) { : QObject(manager), m_manager(manager) {
} }

View File

@ -34,7 +34,7 @@ class AdBlockMatcher : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit AdBlockMatcher(AdBlockManager* manager); explicit AdBlockMatcher(AdBlockManager *manager);
virtual ~AdBlockMatcher(); virtual ~AdBlockMatcher();
const AdBlockRule* match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const; const AdBlockRule* match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const;

View File

@ -51,6 +51,7 @@
#include "definitions/definitions.h" #include "definitions/definitions.h"
#include "network-web/silentnetworkaccessmanager.h" #include "network-web/silentnetworkaccessmanager.h"
#include "miscellaneous/iofactory.h" #include "miscellaneous/iofactory.h"
#include "exceptions/applicationexception.h"
#include "miscellaneous/application.h" #include "miscellaneous/application.h"
#include <QFile> #include <QFile>
@ -271,7 +272,7 @@ AdBlockSubscription::~AdBlockSubscription() {
AdBlockCustomList::AdBlockCustomList(QObject *parent) AdBlockCustomList::AdBlockCustomList(QObject *parent)
: AdBlockSubscription(tr("Custom rules"), parent) { : AdBlockSubscription(tr("Custom rules"), parent) {
setFilePath(AdBlockManager::instance()->storedListsPath() + QDir::separator() + ADBLOCK_CUSTOMLIST_NAME); setFilePath(AdBlockManager::storedListsPath() + QDir::separator() + ADBLOCK_CUSTOMLIST_NAME);
} }
void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) { void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) {
@ -281,7 +282,14 @@ void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) {
const QString ddg1 = QSL("@@||duckduckgo.com^$document"); const QString ddg1 = QSL("@@||duckduckgo.com^$document");
const QString ddg2 = QSL("duckduckgo.com#@#.has-ad"); const QString ddg2 = QSL("duckduckgo.com#@#.has-ad");
const QString rules = QString::fromUtf8(IOFactory::readTextFile(filePath())); QString rules;
try {
rules = QString::fromUtf8(IOFactory::readTextFile(filePath()));
}
catch (ApplicationException&) {
}
QFile file(filePath()); QFile file(filePath());