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:
▪ 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
—————

View File

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

View File

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

View File

@ -24,6 +24,7 @@
#include "gui/dialogs/formmain.h"
#include "gui/tabwidget.h"
#include "gui/webbrowser.h"
#include "network-web/adblock/adblockicon.h"
#include <QWheelEvent>
@ -140,6 +141,18 @@ void WebViewer::clear() {
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) {
Q_UNUSED(type)

View File

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

View File

@ -40,6 +40,9 @@
#include <QProcess>
#if defined(USE_WEBENGINE)
#include "network-web/adblock/adblockicon.h"
#include "network-web/adblock/adblockmanager.h"
#include <QWebEngineProfile>
#include <QWebEngineDownloadItem>
#endif
@ -194,7 +197,7 @@ void Application::setMainForm(FormMain *main_form) {
}
QString Application::getConfigHomePath() {
return IOFactory::getSystemFolder(QStandardPaths::ConfigLocation);
return IOFactory::getSystemFolder(QStandardPaths::GenericConfigLocation);
}
QString Application::getUserDataAppPath() {
@ -374,6 +377,10 @@ void Application::onAboutToQuit() {
eliminateFirstRun();
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.
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 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 home_path = qApp->getUserDataHomePath();

View File

@ -42,12 +42,12 @@ AdBlockDialog::AdBlockDialog(QWidget* parent)
m_ui->adblockCheckBox->setChecked(m_manager->isEnabled());
QMenu* menu = new QMenu(m_ui->buttonOptions);
m_actionAddRule = menu->addAction(tr("Add Rule"), this, SLOT(addRule()));
m_actionRemoveRule = menu->addAction(tr("Remove Rule"), this, SLOT(removeRule()));
m_actionAddRule = menu->addAction(tr("Add rule"), this, SLOT(addRule()));
m_actionRemoveRule = menu->addAction(tr("Remove rule"), this, SLOT(removeRule()));
menu->addSeparator();
m_actionAddSubscription = menu->addAction(tr("Add Subscription"), this, SLOT(addSubscription()));
m_actionRemoveSubscription = menu->addAction(tr("Remove Subscription"), this, SLOT(removeSubscription()));
menu->addAction(tr("Update Subscriptions"), m_manager, SLOT(updateAllSubscriptions()));
m_actionAddSubscription = menu->addAction(tr("Add subscription"), this, SLOT(addSubscription()));
m_actionRemoveSubscription = menu->addAction(tr("Remove subscription"), this, SLOT(removeSubscription()));
menu->addAction(tr("Update subscriptions"), m_manager, SLOT(updateAllSubscriptions()));
menu->addSeparator();
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->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();

View File

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

View File

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

View File

@ -35,7 +35,7 @@ class AdBlockIcon : public ClickableLabel {
Q_OBJECT
public:
explicit AdBlockIcon(BrowserWindow* window, QWidget* parent = 0);
explicit AdBlockIcon(QWidget* parent = 0);
virtual ~AdBlockIcon();
void popupBlocked(const QString &ruleString, const QUrl &url);
@ -53,7 +53,6 @@ class AdBlockIcon : public ClickableLabel {
void stopAnimation();
private:
BrowserWindow *m_window;
QAction *m_menuAction;
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
// redirect to somewhere.
// 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";
// QUrl url(QSL("rssguard:adblock"));
@ -113,12 +114,13 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo& request) {
// url.setQuery(query);
// request.redirect(url);
request.block(true);
//request.block(true);
}
else {
request.block(true);
}
}
return res;
}
@ -227,7 +229,7 @@ AdBlockCustomList *AdBlockManager::customList() const {
return 0;
}
QString AdBlockManager::storedListsPath() const {
QString AdBlockManager::storedListsPath() {
return qApp->getUserDataPath() + QDir::separator() + ADBLOCK_LISTS_SUBDIRECTORY;
}
@ -297,7 +299,7 @@ void AdBlockManager::load() {
m_subscriptions.append(customList);
// Load all subscriptions
foreach (AdBlockSubscription* subscription, m_subscriptions) {
foreach (AdBlockSubscription *subscription, m_subscriptions) {
subscription->loadSubscription(m_disabledRules);
// TODO: po zmene subskripce prehrat user css?

View File

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

View File

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

View File

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

View File

@ -51,6 +51,7 @@
#include "definitions/definitions.h"
#include "network-web/silentnetworkaccessmanager.h"
#include "miscellaneous/iofactory.h"
#include "exceptions/applicationexception.h"
#include "miscellaneous/application.h"
#include <QFile>
@ -271,7 +272,7 @@ AdBlockSubscription::~AdBlockSubscription() {
AdBlockCustomList::AdBlockCustomList(QObject *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) {
@ -281,7 +282,14 @@ void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) {
const QString ddg1 = QSL("@@||duckduckgo.com^$document");
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());