diff --git a/src/miscellaneous/application.cpp b/src/miscellaneous/application.cpp index 66c14b6fe..2480224b7 100755 --- a/src/miscellaneous/application.cpp +++ b/src/miscellaneous/application.cpp @@ -59,6 +59,10 @@ Application::Application(const QString &id, int &argc, char **argv) connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &Application::downloadRequested); QWebEngineProfile::defaultProfile()->setRequestInterceptor(m_urlInterceptor); + + // TODO: Teď tam žádný nastavení není, ale jestli se DNT třeba + // přidá do dialogu nastavení, tak toto volat při ukládání nastavení. + m_urlInterceptor->loadSettings(); #endif } diff --git a/src/network-web/adblock/adblockmanager.cpp b/src/network-web/adblock/adblockmanager.cpp index 83096a515..e986afaf1 100755 --- a/src/network-web/adblock/adblockmanager.cpp +++ b/src/network-web/adblock/adblockmanager.cpp @@ -17,43 +17,46 @@ // along with RSS Guard. If not, see . #include "network-web/adblock/adblockmanager.h" -#include "network-web/adblock/adblockdialog.h" -#include "network-web/adblock/adblockmatcher.h" -#include "network-web/adblock/adblocksubscription.h" -#include "network-web/adblock/adblockurlinterceptor.h" #include "miscellaneous/application.h" #include "miscellaneous/settings.h" +#include "network-web/adblock/adblockdialog.h" +#include "network-web/adblock/adblockmatcher.h" + +#include "network-web/adblock/adblocksubscription.h" +#include "network-web/adblock/adblockurlinterceptor.h" #include "network-web/networkurlinterceptor.h" #include -#include #include -#include #include -#include #include #include -#include +#include +#include +#include #include +#include Q_GLOBAL_STATIC(AdBlockManager, qz_adblock_manager) -AdBlockManager::AdBlockManager(QObject *parent) - : QObject(parent), m_loaded(false), m_enabled(true), m_matcher(new AdBlockMatcher(this)), m_interceptor(new AdBlockUrlInterceptor(this)) { +AdBlockManager::AdBlockManager(QObject* parent) + : QObject(parent) + , m_loaded(false) + , m_enabled(true) + , m_matcher(new AdBlockMatcher(this)) + , m_interceptor(new AdBlockUrlInterceptor(this)) +{ load(); } -AdBlockManager::~AdBlockManager() { - qDeleteAll(m_subscriptions); -} +AdBlockManager::~AdBlockManager() { qDeleteAll(m_subscriptions); } -AdBlockManager *AdBlockManager::instance() { - return qz_adblock_manager(); -} +AdBlockManager* AdBlockManager::instance() { return qz_adblock_manager(); } -void AdBlockManager::setEnabled(bool enabled) { +void AdBlockManager::setEnabled(bool enabled) +{ if (m_enabled == enabled) { return; } @@ -61,26 +64,28 @@ void AdBlockManager::setEnabled(bool enabled) { m_enabled = enabled; emit enabledChanged(enabled); - qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, m_enabled); + qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, + m_enabled); load(); // TODO: Reload user stylesheet. - //mApp->reloadUserStyleSheet(); + // mApp->reloadUserStyleSheet(); QMutexLocker locker(&m_mutex); if (m_enabled) { m_matcher->update(); - } - else { + } else { m_matcher->clear(); } } -QList AdBlockManager::subscriptions() const { +QList AdBlockManager::subscriptions() const +{ return m_subscriptions; } -bool AdBlockManager::block(QWebEngineUrlRequestInfo &request) { +bool AdBlockManager::block(QWebEngineUrlRequestInfo& request) +{ QMutexLocker locker(&m_mutex); if (!isEnabled()) { @@ -96,47 +101,49 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo &request) { } bool res = false; - const AdBlockRule *blockedRule = m_matcher->match(request, urlDomain, urlString); + const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString); if (blockedRule) { res = true; if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { - // We are blocking main URL frame, we can display "AdBlock error page" or redirect to somewhere. + // 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"); // TODO request.redirect() přesměrovat na "chybovou stranku"; - //QUrl url(QSL("rssguard:adblock")); - //QUrlQuery query; - //query.addQueryItem(QSL("rule"), blockedRule->filter()); - //query.addQueryItem(QSL("subscription"), blockedRule->subscription()->title()); - //url.setQuery(query); - //request.redirect(url); + // QUrl url(QSL("rssguard:adblock")); + // QUrlQuery query; + // query.addQueryItem(QSL("rule"), blockedRule->filter()); + // query.addQueryItem(QSL("subscription"), + // blockedRule->subscription()->title()); + // url.setQuery(query); + // request.redirect(url); request.block(true); - } - else { + } else { request.block(true); } } return res; } -QStringList AdBlockManager::disabledRules() const { - return m_disabledRules; -} +QStringList AdBlockManager::disabledRules() const { return m_disabledRules; } -void AdBlockManager::addDisabledRule(const QString &filter) { +void AdBlockManager::addDisabledRule(const QString& filter) +{ m_disabledRules.append(filter); } -void AdBlockManager::removeDisabledRule(const QString &filter) { +void AdBlockManager::removeDisabledRule(const QString& filter) +{ m_disabledRules.removeOne(filter); } -bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url) { - const QList > queryItems = QUrlQuery(url).queryItems(QUrl::FullyDecoded); +bool AdBlockManager::addSubscriptionFromUrl(const QUrl& url) +{ + const QList> queryItems = QUrlQuery(url).queryItems(QUrl::FullyDecoded); QString subscriptionTitle; QString subscriptionUrl; @@ -146,8 +153,7 @@ bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url) { if (pair.first == QL1S("location")) { subscriptionUrl = pair.second; - } - else if (pair.first == QL1S("title")) { + } else if (pair.first == QL1S("title")) { subscriptionTitle = pair.second; } } @@ -158,35 +164,43 @@ bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url) { const QString message = tr("Do you want to add %1 subscription?").arg(subscriptionTitle); - QMessageBox::StandardButton result = QMessageBox::question(nullptr, tr("Add AdBlock subscription"), message, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); + QMessageBox::StandardButton result = QMessageBox::question( + nullptr, tr("Add AdBlock subscription"), message, + QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); if (result == QMessageBox::Yes) { - AdBlockManager::instance()->addSubscription(subscriptionTitle, subscriptionUrl); + AdBlockManager::instance()->addSubscription(subscriptionTitle, + subscriptionUrl); AdBlockManager::instance()->showDialog(); } return true; } -AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const QString &url) { +AdBlockSubscription* AdBlockManager::addSubscription(const QString& title, + const QString& url) +{ if (title.isEmpty() || url.isEmpty()) { return 0; } QString fileName = title + QSL(".txt"); QString filePath = storedListsPath() + QDir::separator() + fileName; - QByteArray data = QString("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]").arg(title, url).toLatin1(); + QByteArray data = QString("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]") + .arg(title, url) + .toLatin1(); QSaveFile file(filePath); if (!file.open(QFile::WriteOnly)) { - qWarning("Cannot save AdBlock subscription to file '%s'.", qPrintable(filePath)); + qWarning("Cannot save AdBlock subscription to file '%s'.", + qPrintable(filePath)); return 0; } file.write(data); file.commit(); - AdBlockSubscription *subscription = new AdBlockSubscription(title, this); + AdBlockSubscription* subscription = new AdBlockSubscription(title, this); subscription->setUrl(QUrl(url)); subscription->setFilePath(filePath); subscription->loadSubscription(m_disabledRules); @@ -194,13 +208,16 @@ AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const m_subscriptions.insert(m_subscriptions.count() - 1, subscription); // TODO: po změně subskripce přehrat user css? - //connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet())); - connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher())); + // connect(subscription, SIGNAL(subscriptionUpdated()), mApp, + // SLOT(reloadUserStyleSheet())); + connect(subscription, SIGNAL(subscriptionChanged()), this, + SLOT(updateMatcher())); return subscription; } -bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription) { +bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription) +{ QMutexLocker locker(&m_mutex); if (!m_subscriptions.contains(subscription) || !subscription->canBeRemoved()) { @@ -216,9 +233,10 @@ bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription) { return true; } -AdBlockCustomList *AdBlockManager::customList() const { - foreach (AdBlockSubscription *subscription, m_subscriptions) { - AdBlockCustomList *list = qobject_cast(subscription); +AdBlockCustomList* AdBlockManager::customList() const +{ + foreach (AdBlockSubscription* subscription, m_subscriptions) { + AdBlockCustomList* list = qobject_cast(subscription); if (list) { return list; @@ -228,20 +246,28 @@ AdBlockCustomList *AdBlockManager::customList() const { return 0; } -QString AdBlockManager::storedListsPath() const { +QString AdBlockManager::storedListsPath() const +{ return qApp->getUserDataPath() + QDir::separator() + ADBLOCK_LISTS_SUBDIRECTORY; } -void AdBlockManager::load() { +void AdBlockManager::load() +{ QMutexLocker locker(&m_mutex); if (m_loaded) { return; } - m_enabled = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::AdBlockEnabled)).toBool(); - m_disabledRules = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::DisabledRules)).toStringList(); - QDateTime lastUpdate = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::LastUpdatedOn)).toDateTime(); + m_enabled = qApp->settings() + ->value(GROUP(AdBlock), SETTING(AdBlock::AdBlockEnabled)) + .toBool(); + m_disabledRules = qApp->settings() + ->value(GROUP(AdBlock), SETTING(AdBlock::DisabledRules)) + .toStringList(); + QDateTime lastUpdate = qApp->settings() + ->value(GROUP(AdBlock), SETTING(AdBlock::LastUpdatedOn)) + .toDateTime(); if (!m_enabled) { return; @@ -254,7 +280,8 @@ void AdBlockManager::load() { QDir().mkpath(storedListsPath()); } - foreach (const QString &fileName, adblockDir.entryList(QStringList("*.txt"), QDir::Files)) { + foreach (const QString& fileName, + adblockDir.entryList(QStringList("*.txt"), QDir::Files)) { if (fileName == ADBLOCK_CUSTOMLIST_NAME) { continue; } @@ -272,7 +299,8 @@ void AdBlockManager::load() { QUrl url = QUrl(textStream.readLine(1024).remove(QLatin1String("Url: "))); if (title.isEmpty() || !url.isValid()) { - qWarning("Invalid AdBlock subscription file '%s'.", qPrintable(absolutePath)); + qWarning("Invalid AdBlock subscription file '%s'.", + qPrintable(absolutePath)); continue; } @@ -294,16 +322,18 @@ void AdBlockManager::load() { } // Append CustomList. - AdBlockCustomList *customList = new AdBlockCustomList(this); + AdBlockCustomList* customList = new AdBlockCustomList(this); 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? - //connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet())); - connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher())); + // connect(subscription, SIGNAL(subscriptionUpdated()), mApp, + // SLOT(reloadUserStyleSheet())); + connect(subscription, SIGNAL(subscriptionChanged()), this, + SLOT(updateMatcher())); } if (lastUpdate.addDays(5) < QDateTime::currentDateTime()) { @@ -316,65 +346,73 @@ void AdBlockManager::load() { qApp->urlIinterceptor()->installUrlInterceptor(m_interceptor); } -void AdBlockManager::updateMatcher() { +void AdBlockManager::updateMatcher() +{ QMutexLocker locker(&m_mutex); m_matcher->update(); } -void AdBlockManager::updateAllSubscriptions() { +void AdBlockManager::updateAllSubscriptions() +{ foreach (AdBlockSubscription* subscription, m_subscriptions) { subscription->updateSubscription(); } - qApp->settings()->setValue(GROUP(AdBlock), AdBlock::LastUpdatedOn, QDateTime::currentDateTime()); + qApp->settings()->setValue(GROUP(AdBlock), AdBlock::LastUpdatedOn, + QDateTime::currentDateTime()); } -void AdBlockManager::save() { +void AdBlockManager::save() +{ if (!m_loaded) { return; } - foreach (AdBlockSubscription *subscription, m_subscriptions) { + foreach (AdBlockSubscription* subscription, m_subscriptions) { subscription->saveSubscription(); } - qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, m_enabled); - qApp->settings()->setValue(GROUP(AdBlock), AdBlock::DisabledRules, m_disabledRules); + qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, + m_enabled); + qApp->settings()->setValue(GROUP(AdBlock), AdBlock::DisabledRules, + m_disabledRules); } -bool AdBlockManager::isEnabled() const { - return m_enabled; -} +bool AdBlockManager::isEnabled() const { return m_enabled; } -bool AdBlockManager::canRunOnScheme(const QString &scheme) const { +bool AdBlockManager::canRunOnScheme(const QString& scheme) const +{ return !(scheme == QSL("file") || scheme == QSL("qrc") || scheme == QSL("data") || scheme == QSL("abp")); } -bool AdBlockManager::canBeBlocked(const QUrl &url) const { +bool AdBlockManager::canBeBlocked(const QUrl& url) const +{ return !m_matcher->adBlockDisabledForUrl(url); } -QString AdBlockManager::elementHidingRules(const QUrl &url) const { +QString AdBlockManager::elementHidingRules(const QUrl& url) const +{ if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url)) { return QString(); - } - else { + } else { return m_matcher->elementHidingRules(); } } -QString AdBlockManager::elementHidingRulesForDomain(const QUrl &url) const { +QString AdBlockManager::elementHidingRulesForDomain(const QUrl& url) const +{ if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url)) { return QString(); - } - else { + } else { return m_matcher->elementHidingRulesForDomain(url.host()); } } -AdBlockSubscription* AdBlockManager::subscriptionByName(const QString &name) const { - foreach (AdBlockSubscription *subscription, m_subscriptions) { +AdBlockSubscription* AdBlockManager::subscriptionByName( + const QString& name) const +{ + foreach (AdBlockSubscription* subscription, m_subscriptions) { if (subscription->title() == name) { return subscription; } @@ -383,7 +421,8 @@ AdBlockSubscription* AdBlockManager::subscriptionByName(const QString &name) con return 0; } -AdBlockDialog *AdBlockManager::showDialog() { +AdBlockDialog* AdBlockManager::showDialog() +{ if (!m_adBlockDialog) { m_adBlockDialog = new AdBlockDialog; } @@ -395,9 +434,10 @@ AdBlockDialog *AdBlockManager::showDialog() { return m_adBlockDialog.data(); } -void AdBlockManager::showRule() { - if (QAction *action = qobject_cast(sender())) { - const AdBlockRule *rule = static_cast(action->data().value()); +void AdBlockManager::showRule() +{ + if (QAction* action = qobject_cast(sender())) { + const AdBlockRule* rule = static_cast(action->data().value()); if (rule) { showDialog()->showRule(rule); diff --git a/src/network-web/adblock/adblockmanager.h b/src/network-web/adblock/adblockmanager.h index 6b13056ff..1703960e3 100755 --- a/src/network-web/adblock/adblockmanager.h +++ b/src/network-web/adblock/adblockmanager.h @@ -19,57 +19,56 @@ #ifndef ADBLOCKMANAGER_H #define ADBLOCKMANAGER_H -#include -#include -#include #include - +#include +#include +#include class QUrl; class QWebEngineUrlRequestInfo; - -class AdBlockRule; -class AdBlockDialog; class AdBlockMatcher; class AdBlockCustomList; class AdBlockSubscription; +class AdBlockRule; +class AdBlockDialog; class AdBlockUrlInterceptor; class AdBlockManager : public QObject { Q_OBJECT public: - explicit AdBlockManager(QObject *parent = 0); + explicit AdBlockManager(QObject* parent = 0); virtual ~AdBlockManager(); void load(); void save(); bool isEnabled() const; - bool canRunOnScheme(const QString &scheme) const; + bool canRunOnScheme(const QString& scheme) const; - QString elementHidingRules(const QUrl &url) const; - QString elementHidingRulesForDomain(const QUrl &url) const; + QString elementHidingRules(const QUrl& url) const; + QString elementHidingRulesForDomain(const QUrl& url) const; - AdBlockSubscription *subscriptionByName(const QString &name) const; + AdBlockSubscription* subscriptionByName(const QString& name) const; QList subscriptions() const; - bool block(QWebEngineUrlRequestInfo &request); + bool block(QWebEngineUrlRequestInfo& request); QStringList disabledRules() const; - void addDisabledRule(const QString &filter); - void removeDisabledRule(const QString &filter); + void addDisabledRule(const QString& filter); + void removeDisabledRule(const QString& filter); - bool addSubscriptionFromUrl(const QUrl &url); + bool addSubscriptionFromUrl(const QUrl& url); - AdBlockSubscription *addSubscription(const QString &title, const QString &url); - bool removeSubscription(AdBlockSubscription *subscription); + AdBlockSubscription* addSubscription(const QString& title, + const QString& url); + bool removeSubscription(AdBlockSubscription* subscription); - AdBlockCustomList *customList() const; + AdBlockCustomList* customList() const; QString storedListsPath() const; - static AdBlockManager *instance(); + static AdBlockManager* instance(); signals: void enabledChanged(bool enabled); @@ -81,22 +80,21 @@ 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; QList m_subscriptions; - AdBlockMatcher *m_matcher; + AdBlockMatcher* m_matcher; QStringList m_disabledRules; - AdBlockUrlInterceptor *m_interceptor; + AdBlockUrlInterceptor* m_interceptor; QPointer m_adBlockDialog; QMutex m_mutex; }; #endif // ADBLOCKMANAGER_H -