diff --git a/plugins/webengineurlinterceptor/adblock/lib/adblockmanager.cpp b/plugins/webengineurlinterceptor/adblock/lib/adblockmanager.cpp index 1385eff2..18ee1397 100644 --- a/plugins/webengineurlinterceptor/adblock/lib/adblockmanager.cpp +++ b/plugins/webengineurlinterceptor/adblock/lib/adblockmanager.cpp @@ -1,102 +1,111 @@ /* Copyright (C) 2016 Montel Laurent 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. 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. 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. */ #include "adblockmanager.h" #include "adblockmatcher.h" #include "globalsettings_webengineurlinterceptoradblock.h" #include using namespace AdBlock; class AdblockManagerInstancePrivate { public: AdblockManagerInstancePrivate() : ablockManager(new AdblockManager) { } ~AdblockManagerInstancePrivate() { delete ablockManager; } AdblockManager *ablockManager; }; Q_GLOBAL_STATIC(AdblockManagerInstancePrivate, sInstance) AdblockManager *AdblockManager::self() { return sInstance->ablockManager; } AdblockManager::AdblockManager(QObject *parent) : QObject(parent) { mAdBlockMatcher = new AdBlockMatcher(this); reloadConfig(); } AdblockManager::~AdblockManager() { } void AdblockManager::reloadConfig() { + loadSubscriptions(); bool enabled = AdBlock::AdBlockSettings::self()->adBlockEnabled(); Q_EMIT enabledChanged(enabled); qDebug() << " void AdblockManager::reloadConfig()" << enabled << " " << this; } +void AdblockManager::loadSubscriptions() +{ + //Clear subscription + mSubscriptions.clear(); + //TODO + +} + bool AdblockManager::isEnabled() const { return mAdBlockMatcher->isEnabled(); } bool AdblockManager::interceptRequest(const QWebEngineUrlRequestInfo &info) { bool result = false; QUrl url = info.requestUrl(); const QString urlString = url.toString().toLower(); const QString host = url.host().toLower(); const QString scheme = url.scheme().toLower(); if (!canRunOnScheme(scheme)) { return result; } const AdBlockRule *blockedRule = mAdBlockMatcher->match(info, host, urlString); if (blockedRule) { result = true; //TODO } return result; } bool AdblockManager::canRunOnScheme(const QString &scheme) const { return (scheme != QLatin1String("file")); } QList AdblockManager::subscriptions() const { return mSubscriptions; } diff --git a/plugins/webengineurlinterceptor/adblock/lib/adblockmanager.h b/plugins/webengineurlinterceptor/adblock/lib/adblockmanager.h index 0039f9fd..b9d2c4e2 100644 --- a/plugins/webengineurlinterceptor/adblock/lib/adblockmanager.h +++ b/plugins/webengineurlinterceptor/adblock/lib/adblockmanager.h @@ -1,56 +1,57 @@ /* Copyright (C) 2016 Montel Laurent 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. 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. 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. */ #ifndef ADBLOCKMANAGER_H #define ADBLOCKMANAGER_H #include #include #include "adblocklib_export.h" namespace AdBlock { class AdBlockMatcher; class AdBlockSubscription; class ADBLOCKLIB_EXPORT AdblockManager : public QObject { Q_OBJECT public: static AdblockManager *self(); explicit AdblockManager(QObject *parent = Q_NULLPTR); ~AdblockManager(); bool isEnabled() const; bool interceptRequest(const QWebEngineUrlRequestInfo &info); QList subscriptions() const; Q_SIGNALS: void enabledChanged(bool); public Q_SLOTS: void reloadConfig(); private: + void loadSubscriptions(); bool canRunOnScheme(const QString &scheme) const; bool mEnabled; AdBlockMatcher *mAdBlockMatcher; QList mSubscriptions; }; } #endif // ADBLOCKMANAGER_H diff --git a/plugins/webengineurlinterceptor/adblock/lib/adblockmatcher.cpp b/plugins/webengineurlinterceptor/adblock/lib/adblockmatcher.cpp index 23924893..033e60b2 100644 --- a/plugins/webengineurlinterceptor/adblock/lib/adblockmatcher.cpp +++ b/plugins/webengineurlinterceptor/adblock/lib/adblockmatcher.cpp @@ -1,261 +1,261 @@ /* Copyright (C) 2016 Montel Laurent 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. 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. 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. */ /* ============================================================ * QupZilla - WebKit based browser * Copyright (C) 2014 David Rosca * * 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. * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "adblockmatcher.h" #include "adblockmanager.h" #include "adblockrule.h" #include "adblocksubscription.h" using namespace AdBlock; AdBlockMatcher::AdBlockMatcher(AdblockManager *manager) : QObject(manager) , m_manager(manager), m_enabled(false) { connect(manager, &AdblockManager::enabledChanged, this, &AdBlockMatcher::enabledChanged); } AdBlockMatcher::~AdBlockMatcher() { clear(); } const AdBlockRule *AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const { // Exception rules if (m_networkExceptionTree.find(request, urlDomain, urlString)) { return Q_NULLPTR; } int count = m_networkExceptionRules.count(); for (int i = 0; i < count; ++i) { const AdBlockRule *rule = m_networkExceptionRules.at(i); if (rule->networkMatch(request, urlDomain, urlString)) { return Q_NULLPTR; } } // Block rules if (const AdBlockRule *rule = m_networkBlockTree.find(request, urlDomain, urlString)) { return rule; } count = m_networkBlockRules.count(); for (int i = 0; i < count; ++i) { const AdBlockRule *rule = m_networkBlockRules.at(i); if (rule->networkMatch(request, urlDomain, urlString)) { return rule; } } return Q_NULLPTR; } bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const { const int count = m_documentRules.count(); for (int i = 0; i < count; ++i) if (m_documentRules.at(i)->urlMatch(url)) { return true; } return false; } bool AdBlockMatcher::elemHideDisabledForUrl(const QUrl &url) const { if (adBlockDisabledForUrl(url)) { return true; } const int count = m_elemhideRules.count(); for (int i = 0; i < count; ++i) if (m_elemhideRules.at(i)->urlMatch(url)) { return true; } return false; } QString AdBlockMatcher::elementHidingRules() const { return m_elementHidingRules; } QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const { QString rules; int addedRulesCount = 0; const int count = m_domainRestrictedCssRules.count(); for (int i = 0; i < count; ++i) { const AdBlockRule *rule = m_domainRestrictedCssRules.at(i); if (!rule->matchDomain(domain)) { continue; } if (Q_UNLIKELY(addedRulesCount == 1000)) { rules.append(rule->cssSelector()); rules.append(QLatin1String("{display:none !important;}\n")); addedRulesCount = 0; } else { rules.append(rule->cssSelector() + QLatin1Char(',')); addedRulesCount++; } } if (addedRulesCount != 0) { rules = rules.left(rules.size() - 1); rules.append(QLatin1String("{display:none !important;}\n")); } return rules; } bool AdBlockMatcher::isEnabled() const { return m_enabled; } void AdBlockMatcher::update() { clear(); QHash cssRulesHash; QVector exceptionCssRules; Q_FOREACH (AdBlockSubscription *subscription, m_manager->subscriptions()) { Q_FOREACH (const AdBlockRule *rule, subscription->allRules()) { // Don't add internally disabled rules to cache if (rule->isInternalDisabled()) { continue; } if (rule->isCssRule()) { // We will add only enabled css rules to cache, because there is no enabled/disabled // check on match. They are directly embedded to pages. if (!rule->isEnabled()) { continue; } if (rule->isException()) { exceptionCssRules.append(rule); } else { cssRulesHash.insert(rule->cssSelector(), rule); } } else if (rule->isDocument()) { m_documentRules.append(rule); } else if (rule->isElemhide()) { m_elemhideRules.append(rule); } else if (rule->isException()) { if (!m_networkExceptionTree.add(rule)) { m_networkExceptionRules.append(rule); } } else { if (!m_networkBlockTree.add(rule)) { m_networkBlockRules.append(rule); } } } } foreach (const AdBlockRule *rule, exceptionCssRules) { const AdBlockRule *originalRule = cssRulesHash.value(rule->cssSelector()); // If we don't have this selector, the exception does nothing if (!originalRule) { continue; } AdBlockRule *copiedRule = originalRule->copy(); copiedRule->m_options |= AdBlockRule::DomainRestrictedOption; copiedRule->m_blockedDomains.append(rule->m_allowedDomains); cssRulesHash[rule->cssSelector()] = copiedRule; m_createdRules.append(copiedRule); } // Apparently, excessive amount of selectors for one CSS rule is not what WebKit likes. // (In my testings, 4931 is the number that makes it crash) // So let's split it by 1000 selectors... int hidingRulesCount = 0; QHashIterator it(cssRulesHash); while (it.hasNext()) { it.next(); const AdBlockRule *rule = it.value(); if (rule->isDomainRestricted()) { m_domainRestrictedCssRules.append(rule); } else if (Q_UNLIKELY(hidingRulesCount == 1000)) { m_elementHidingRules.append(rule->cssSelector()); - m_elementHidingRules.append(QLatin1String("{display:none !important;} ")); + m_elementHidingRules.append(QStringLiteral("{display:none !important;} ")); hidingRulesCount = 0; } else { m_elementHidingRules.append(rule->cssSelector() + QLatin1Char(',')); hidingRulesCount++; } } if (hidingRulesCount != 0) { m_elementHidingRules = m_elementHidingRules.left(m_elementHidingRules.size() - 1); m_elementHidingRules.append(QLatin1String("{display:none !important;} ")); } } void AdBlockMatcher::clear() { m_networkExceptionTree.clear(); m_networkExceptionRules.clear(); m_networkBlockTree.clear(); m_networkBlockRules.clear(); m_domainRestrictedCssRules.clear(); m_elementHidingRules.clear(); m_documentRules.clear(); m_elemhideRules.clear(); qDeleteAll(m_createdRules); m_createdRules.clear(); } void AdBlockMatcher::enabledChanged(bool enabled) { m_enabled = enabled; if (m_enabled) { update(); } else { clear(); } } diff --git a/plugins/webengineurlinterceptor/adblock/lib/widgets/adblocksettingwidget.cpp b/plugins/webengineurlinterceptor/adblock/lib/widgets/adblocksettingwidget.cpp index abfdc5c0..0974f71c 100644 --- a/plugins/webengineurlinterceptor/adblock/lib/widgets/adblocksettingwidget.cpp +++ b/plugins/webengineurlinterceptor/adblock/lib/widgets/adblocksettingwidget.cpp @@ -1,493 +1,493 @@ /* ============================================================ * * This file is a part of the rekonq project * * Copyright (c) 2013-2016 Montel Laurent * based on code from rekonq * Copyright (C) 2010-2012 by Andrea Diamantini * * * 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) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * ============================================================ */ // Self Includes #include "adblocksettingwidget.h" #include "ui_settings_adblock.h" #include "globalsettings_webengineurlinterceptoradblock.h" #include "adblockaddsubscriptiondialog.h" #include "adblockinterceptor_debug.h" #include "adblockmanager.h" #include "adblockshowlistdialog.h" #include "adblockutil.h" #include "PimCommon/ConfigureImmutableWidgetUtils" using namespace PimCommon::ConfigureImmutableWidgetUtils; #include "PimCommon/PimUtil" // KDE Includes #include #include #include #include #include // Qt Includes #include #include #include #include #include #include #include #include using namespace AdBlock; AdBlockSettingWidget::AdBlockSettingWidget(QWidget *parent) : QWidget(parent) , mChanged(false) { mUi = new Ui::adblock; mUi->setupUi(this); mUi->hintLabel->setText(i18n("Filter expression (e.g. http://www.example.com/ad/*, more information):")); connect(mUi->hintLabel, &QLabel::linkActivated, this, &AdBlockSettingWidget::slotInfoLinkActivated); mUi->hintLabel->setContextMenuPolicy(Qt::NoContextMenu); mUi->manualFiltersListWidget->setSelectionMode(QAbstractItemView::MultiSelection); mUi->searchLine->setListWidget(mUi->manualFiltersListWidget); mUi->insertButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); connect(mUi->insertButton, &QToolButton::clicked, this, &AdBlockSettingWidget::insertRule); mUi->removeButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); connect(mUi->removeButton, &QPushButton::clicked, this, &AdBlockSettingWidget::removeRule); connect(mUi->removeSubscription, &QPushButton::clicked, this, &AdBlockSettingWidget::slotRemoveSubscription); connect(mUi->manualFiltersListWidget, &QListWidget::currentItemChanged, this, &AdBlockSettingWidget::slotUpdateManualButtons); connect(mUi->manualFiltersListWidget, &QListWidget::itemChanged, this, &AdBlockSettingWidget::hasChanged); mUi->spinBox->setSuffix(ki18np(" day", " days")); mUi->removeSubscription->setEnabled(false); mUi->showList->setEnabled(false); // Q_EMIT changed signal connect(mUi->checkEnableAdblock, &QCheckBox::stateChanged, this, &AdBlockSettingWidget::hasChanged); connect(mUi->checkHideAds, &QCheckBox::stateChanged, this, &AdBlockSettingWidget::hasChanged); connect(mUi->spinBox, static_cast(&KPluralHandlingSpinBox::valueChanged), this, &AdBlockSettingWidget::hasChanged); connect(mUi->addFilters, &QPushButton::clicked, this, &AdBlockSettingWidget::slotAddFilter); connect(mUi->showList, &QPushButton::clicked, this, &AdBlockSettingWidget::slotShowList); connect(mUi->editFilter, &QPushButton::clicked, this, &AdBlockSettingWidget::slotEditFilter); connect(mUi->automaticFiltersListWidget, &AdBlock::AdBlockListWidget::itemChanged, this, &AdBlockSettingWidget::hasChanged); connect(mUi->automaticFiltersListWidget, &AdBlock::AdBlockListWidget::currentItemChanged, this, &AdBlockSettingWidget::slotUpdateButtons); connect(mUi->automaticFiltersListWidget, &AdBlock::AdBlockListWidget::itemDoubleClicked, this, &AdBlockSettingWidget::slotAutomaticFilterDouble); connect(mUi->importFilters, &QPushButton::clicked, this, &AdBlockSettingWidget::slotImportFilters); connect(mUi->exportFilters, &QPushButton::clicked, this, &AdBlockSettingWidget::slotExportFilters); connect(mUi->addFilterLineEdit, &QLineEdit::textChanged, this, &AdBlockSettingWidget::slotManualFilterLineEditTextChanged); slotUpdateManualButtons(); mUi->insertButton->setEnabled(false); } AdBlockSettingWidget::~AdBlockSettingWidget() { delete mUi; } void AdBlockSettingWidget::slotManualFilterLineEditTextChanged(const QString &text) { mUi->insertButton->setEnabled(!text.trimmed().isEmpty()); } void AdBlockSettingWidget::slotEditFilter() { QListWidgetItem *item = mUi->manualFiltersListWidget->currentItem(); if (item) { mUi->manualFiltersListWidget->editItem(item); } } void AdBlockSettingWidget::slotUpdateButtons() { const bool enabled = mUi->automaticFiltersListWidget->currentItem(); mUi->removeSubscription->setEnabled(enabled); mUi->showList->setEnabled(enabled); } void AdBlockSettingWidget::slotUpdateManualButtons() { const bool enabled = mUi->manualFiltersListWidget->currentItem(); mUi->removeButton->setEnabled(enabled); mUi->editFilter->setEnabled(enabled); mUi->exportFilters->setEnabled(mUi->manualFiltersListWidget->count() > 0); } void AdBlockSettingWidget::slotInfoLinkActivated(const QString &url) { Q_UNUSED(url) const QString href = QStringLiteral("https://adblockplus.org/en/filters"); const QString hintHelpString = i18n("

Enter an expression to filter. Filters can be defined as either:" "

  • a shell-style wildcard, e.g. http://www.example.com/ads*, " "the wildcards *?[] may be used
  • " "
  • a full regular expression by surrounding the string with '/', " "e.g. /\\/(ad|banner)\\./
" "

Any filter string can be preceded by '@@' to whitelist (allow) any matching URL, " "which takes priority over any blacklist (blocking) filter.
%2", href, i18n("More information")); QWhatsThis::showText(QCursor::pos(), hintHelpString, this); } bool AdBlockSettingWidget::event(QEvent *event) { if (event->type() == QEvent::WhatsThisClicked) { QWhatsThisClickedEvent *clicked = static_cast(event); new KRun(QUrl(clicked->href()), this); return true; } return QWidget::event(event); } void AdBlockSettingWidget::insertRule() { const QString rule = mUi->addFilterLineEdit->text(); if (rule.isEmpty()) { return; } const int numberItem(mUi->manualFiltersListWidget->count()); for (int i = 0; i < numberItem; ++i) { if (mUi->manualFiltersListWidget->item(i)->text() == rule) { mUi->addFilterLineEdit->clear(); return; } } addManualFilter(rule); mUi->exportFilters->setEnabled(mUi->manualFiltersListWidget->count() > 0); mUi->addFilterLineEdit->clear(); hasChanged(); } void AdBlockSettingWidget::removeRule() { QList select = mUi->manualFiltersListWidget->selectedItems(); if (select.isEmpty()) { return; } Q_FOREACH (QListWidgetItem *item, select) { delete item; } mUi->exportFilters->setEnabled(mUi->manualFiltersListWidget->count() > 0); hasChanged(); } void AdBlockSettingWidget::doResetToDefaultsOther() { const bool bUseDefaults = AdBlock::AdBlockSettings::self()->useDefaults(true); loadWidget(mUi->checkEnableAdblock, AdBlock::AdBlockSettings::self()->adBlockEnabledItem()); mUi->tabWidget->setEnabled(AdBlock::AdBlockSettings::self()->adBlockEnabled()); saveCheckBox(mUi->checkHideAds, AdBlock::AdBlockSettings::self()->hideAdsEnabledItem()); loadWidget(mUi->spinBox, AdBlock::AdBlockSettings::self()->adBlockUpdateIntervalItem()); AdBlock::AdBlockSettings::self()->useDefaults(bUseDefaults); } void AdBlockSettingWidget::doLoadFromGlobalSettings() { mUi->manualFiltersListWidget->clear(); mUi->automaticFiltersListWidget->clear(); loadWidget(mUi->checkEnableAdblock, AdBlock::AdBlockSettings::self()->adBlockEnabledItem()); // update enabled status mUi->tabWidget->setEnabled(AdBlock::AdBlockSettings::self()->adBlockEnabled()); loadWidget(mUi->checkHideAds, AdBlock::AdBlockSettings::self()->hideAdsEnabledItem()); loadWidget(mUi->spinBox, AdBlock::AdBlockSettings::self()->adBlockUpdateIntervalItem()); // ------------------------------------------------------------------------------ // automatic filters KConfig config(QStringLiteral("AdBlockadblockrc")); const QStringList itemList = config.groupList().filter(QRegularExpression(QStringLiteral("FilterList \\d+"))); Q_FOREACH (const QString &item, itemList) { KConfigGroup filtersGroup(&config, item); const bool isFilterEnabled = filtersGroup.readEntry(QStringLiteral("FilterEnabled"), false); const QString url = filtersGroup.readEntry(QStringLiteral("url")); const QString path = filtersGroup.readEntry(QStringLiteral("path")); const QString name = filtersGroup.readEntry(QStringLiteral("name")); const QDateTime lastUpdate = filtersGroup.readEntry(QStringLiteral("lastUpdate"), QDateTime()); if (url.isEmpty() || path.isEmpty() || name.isEmpty()) { continue; } QListWidgetItem *subItem = new QListWidgetItem(mUi->automaticFiltersListWidget); subItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); if (isFilterEnabled) { subItem->setCheckState(Qt::Checked); } else { subItem->setCheckState(Qt::Unchecked); } subItem->setData(UrlList, url); subItem->setData(PathList, path); subItem->setData(LastUpdateList, lastUpdate); subItem->setText(name); } // ------------------------------------------------------------------------------ // local filters const QString localRulesFilePath = AdBlock::AdBlockUtil::localFilterPath(); QFile ruleFile(localRulesFilePath); if (!ruleFile.open(QFile::ReadOnly | QFile::Text)) { qCDebug(ADBLOCKINTERCEPTOR_LOG) << "Unable to open rule file" << localRulesFilePath; return; } KConfigGroup grp = config.group(QStringLiteral("DisableRules")); const QStringList disableRules = grp.readEntry("DisableRules", QStringList()); QTextStream in(&ruleFile); while (!in.atEnd()) { QString stringRule = in.readLine(); addManualFilter(stringRule, disableRules); } updateCheckBox(); } void AdBlockSettingWidget::save() { if (!mChanged) { return; } // General settings saveCheckBox(mUi->checkEnableAdblock, AdBlock::AdBlockSettings::self()->adBlockEnabledItem()); saveCheckBox(mUi->checkHideAds, AdBlock::AdBlockSettings::self()->hideAdsEnabledItem()); saveSpinBox(mUi->spinBox, AdBlock::AdBlockSettings::self()->adBlockUpdateIntervalItem()); // automatic filters KConfig config(QStringLiteral("AdBlockadblockrc")); const QStringList list = config.groupList().filter(QRegularExpression(QStringLiteral("FilterList \\d+"))); foreach (const QString &group, list) { config.deleteGroup(group); } const int numberItem(mUi->automaticFiltersListWidget->count()); for (int i = 0; i < numberItem; ++i) { QListWidgetItem *subItem = mUi->automaticFiltersListWidget->item(i); KConfigGroup grp = config.group(QStringLiteral("FilterList %1").arg(i)); grp.writeEntry(QStringLiteral("FilterEnabled"), subItem->checkState() == Qt::Checked); grp.writeEntry(QStringLiteral("url"), subItem->data(UrlList).toString()); grp.writeEntry(QStringLiteral("name"), subItem->text()); if (subItem->data(LastUpdateList).toDateTime().isValid()) { grp.writeEntry(QStringLiteral("lastUpdate"), subItem->data(LastUpdateList).toDateTime()); } QString path = subItem->data(PathList).toString(); if (path.isEmpty()) { - path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QStringLiteral("kmail2/adblockrules-%1").arg(i); + path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/adblock/adblockrules-%1").arg(i); } grp.writeEntry(QStringLiteral("path"), path); } config.sync(); // local filters const QString localRulesFilePath = AdBlock::AdBlockUtil::localFilterPath(); QFile ruleFile(localRulesFilePath); if (!ruleFile.open(QFile::WriteOnly | QFile::Text)) { qCDebug(ADBLOCKINTERCEPTOR_LOG) << "Unable to open rule file" << localRulesFilePath; return; } QStringList disableCustomFilter; QTextStream out(&ruleFile); for (int i = 0; i < mUi->manualFiltersListWidget->count(); ++i) { QListWidgetItem *subItem = mUi->manualFiltersListWidget->item(i); const QString stringRule = subItem->text(); if (!stringRule.trimmed().isEmpty()) { out << stringRule << '\n'; } if (subItem->checkState() == Qt::Unchecked) { disableCustomFilter << stringRule; } } if (!disableCustomFilter.isEmpty()) { KConfigGroup grp = config.group(QStringLiteral("DisableRules")); grp.writeEntry("DisableRules", disableCustomFilter); } else { config.deleteGroup(QStringLiteral("DisableRules")); } // ------------------------------------------------------------------------------- mChanged = false; Q_EMIT changed(false); AdBlock::AdBlockSettings::self()->save(); AdBlock::AdblockManager::self()->reloadConfig(); } void AdBlockSettingWidget::updateCheckBox() { // update enabled status mUi->checkHideAds->setEnabled(mUi->checkEnableAdblock->isChecked()); mUi->tabWidget->setEnabled(mUi->checkEnableAdblock->isChecked()); } void AdBlockSettingWidget::hasChanged() { updateCheckBox(); mChanged = true; Q_EMIT changed(true); Q_EMIT settingsChanged(); } bool AdBlockSettingWidget::changed() const { return mChanged; } void AdBlockSettingWidget::slotAddFilter() { QStringList excludeList; const int numberItem(mUi->automaticFiltersListWidget->count()); excludeList.reserve(numberItem); for (int i = 0; i < numberItem; ++i) { excludeList << mUi->automaticFiltersListWidget->item(i)->text(); } QPointer dlg = new AdBlock::AdBlockAddSubscriptionDialog(excludeList, this); if (dlg->exec()) { QString name; QString url; dlg->selectedList(name, url); QListWidgetItem *subItem = new QListWidgetItem(mUi->automaticFiltersListWidget); subItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); subItem->setCheckState(Qt::Checked); subItem->setText(name); subItem->setData(UrlList, url); subItem->setData(LastUpdateList, QDateTime()); subItem->setData(PathList, QString()); hasChanged(); } delete dlg; } void AdBlockSettingWidget::slotRemoveSubscription() { QListWidgetItem *item = mUi->automaticFiltersListWidget->currentItem(); if (item) { if (KMessageBox::questionYesNo(this, i18n("Do you want to delete list \"%1\"?", item->text()), i18n("Delete current list")) == KMessageBox::Yes) { const QString path = item->data(PathList).toString(); if (!path.isEmpty()) { if (!QFile(path).remove()) { qCDebug(ADBLOCKINTERCEPTOR_LOG) << " we can not remove file:" << path; } } delete item; } hasChanged(); } } void AdBlockSettingWidget::slotShowList() { showAutomaticFilterList(mUi->automaticFiltersListWidget->currentItem()); } void AdBlockSettingWidget::showAutomaticFilterList(QListWidgetItem *item) { if (item) { QPointer dlg = new AdBlockShowListDialog(true, this); dlg->setListName(item->text()); dlg->setAdBlockListPath(item->data(PathList).toString(), item->data(UrlList).toString()); connect(dlg.data(), &AdBlockShowListDialog::deleteList, this, &AdBlockSettingWidget::slotDeleteList); dlg->exec(); delete dlg; } } void AdBlockSettingWidget::slotDeleteList(const QString &listName) { QListWidgetItem *item = mUi->automaticFiltersListWidget->currentItem(); if (item && item->text() == listName) { const QString path = item->data(PathList).toString(); if (!path.isEmpty()) { if (!QFile(path).remove()) { qCDebug(ADBLOCKINTERCEPTOR_LOG) << " we can not remove file:" << path; } } delete item; hasChanged(); } } void AdBlockSettingWidget::slotImportFilters() { const QString filter = i18n("All Files (*)"); const QString result = PimCommon::Util::loadToFile(filter, this, QUrl(), i18n("Import Filters")); if (result.isEmpty()) { return; } const QStringList listFilter = result.split(QLatin1Char('\n')); QStringList excludeFilter; const int numberOfElements(mUi->manualFiltersListWidget->count()); excludeFilter.reserve(numberOfElements); for (int i = 0; i < numberOfElements; ++i) { QListWidgetItem *subItem = mUi->manualFiltersListWidget->item(i); excludeFilter.append(subItem->text()); } Q_FOREACH (const QString &element, listFilter) { if (element == QLatin1String("\n")) { continue; } if (excludeFilter.contains(element)) { continue; } addManualFilter(element); } } void AdBlockSettingWidget::addManualFilter(const QString &text, const QStringList &excludeRules) { QListWidgetItem *subItem = new QListWidgetItem(mUi->manualFiltersListWidget); subItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); subItem->setCheckState(excludeRules.contains(text) ? Qt::Unchecked : Qt::Checked); subItem->setText(text); } void AdBlockSettingWidget::slotExportFilters() { const QString filter = i18n("All Files (*)"); QString exportFilters; const int numberOfElement(mUi->manualFiltersListWidget->count()); for (int i = 0; i < numberOfElement; ++i) { QListWidgetItem *subItem = mUi->manualFiltersListWidget->item(i); const QString stringRule = subItem->text(); if (!stringRule.isEmpty()) { exportFilters += stringRule + QLatin1Char('\n'); } } PimCommon::Util::saveTextAs(exportFilters, filter, this, QUrl(), i18n("Export Filters")); } void AdBlockSettingWidget::slotAutomaticFilterDouble(QListWidgetItem *item) { showAutomaticFilterList(item); } diff --git a/plugins/webengineurlinterceptor/adblock/lib/widgets/adblockutil.cpp b/plugins/webengineurlinterceptor/adblock/lib/widgets/adblockutil.cpp index c4a0203c..672f56cf 100644 --- a/plugins/webengineurlinterceptor/adblock/lib/widgets/adblockutil.cpp +++ b/plugins/webengineurlinterceptor/adblock/lib/widgets/adblockutil.cpp @@ -1,73 +1,73 @@ /* Copyright (C) 2013-2016 Montel Laurent 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. 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. 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. */ #include "adblockutil.h" #include QMap AdBlock::AdBlockUtil::listSubscriptions() { QMap lst; lst.insert(QStringLiteral("EasyList"), QStringLiteral("https://easylist-downloads.adblockplus.org/easylist.txt")); lst.insert(QStringLiteral("EasyList without element hiding"), QStringLiteral("https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt")); lst.insert(QStringLiteral("Corset"), QStringLiteral("http://brianyi.com/corset.txt")); lst.insert(QStringLiteral("EasyList Germany+EasyList"), QStringLiteral("https://easylist-downloads.adblockplus.org/ares+easylist.txt")); lst.insert(QStringLiteral("EasyList Germany"), QStringLiteral("https://easylist-downloads.adblockplus.org/easylistgermany.txt")); lst.insert(QStringLiteral("Liste FR+EasyList"), QStringLiteral("https://easylist-downloads.adblockplus.org/liste_fr+easylist.txt")); lst.insert(QStringLiteral("Liste FR"), QStringLiteral("http://lian.info.tm/liste_fr.txt")); lst.insert(QStringLiteral("ROList+EasyList"), QStringLiteral("https://easylist-downloads.adblockplus.org/rolist+easylist.txt")); lst.insert(QStringLiteral("ROList"), QStringLiteral("http://www.picpoc.ro/menetzrolist.txt")); lst.insert(QStringLiteral("Việt Nam List"), QStringLiteral("http://adblockplus-vietnam.googlecode.com/svn/trunk/abpvn.txt")); lst.insert(QStringLiteral("AdblockList.org"), QStringLiteral("http://adblocklist.org/adblock-pxf-polish.txt")); lst.insert(QStringLiteral("Bulgarian list"), QStringLiteral("http://stanev.org/abp/adblock_bg.txt")); lst.insert(QStringLiteral("EasyPrivacy+EasyList"), QStringLiteral("https://easylist-downloads.adblockplus.org/easyprivacy+easylist.txt")); lst.insert(QStringLiteral("EasyPrivacy+Cédrics Liste"), QStringLiteral("https://easylist-downloads.adblockplus.org/easyprivacy+cedrics.txt")); lst.insert(QStringLiteral("EasyPrivacy"), QStringLiteral("https://easylist-downloads.adblockplus.org/easyprivacy.txt")); lst.insert(QStringLiteral("void.gr"), QStringLiteral("http://www.void.gr/kargig/void-gr-filters.txt")); //lst.insert(QStringLiteral("Wiltteri"), QStringLiteral("http://www.wiltteri.net/wiltteri.txt")); lst.insert(QStringLiteral("ChinaList"), QStringLiteral("http://adblock-chinalist.googlecode.com/svn/trunk/adblock.txt")); lst.insert(QStringLiteral("Filter von Dr.Evil"), QStringLiteral("http://adblock.maltekraus.de/adblock.txt")); lst.insert(QStringLiteral("RuAdList"), QStringLiteral("http://ruadlist.googlecode.com/svn/trunk/adblock.txt")); //lst.insert(QStringLiteral("AdblockRules.org"), QStringLiteral("http://adblockrules.org/download.php?typeall")); lst.insert(QStringLiteral("BSI Lista Polska"), QStringLiteral("http://www.bsi.info.pl/filtrABP.txt")); lst.insert(QStringLiteral("Czech List"), QStringLiteral("http://dajbych.net/adblock.txt")); lst.insert(QStringLiteral("Cédrics Liste"), QStringLiteral("http://chewey.de/mozilla/data/adblock.txt")); lst.insert(QStringLiteral("Fanboy's List"), QStringLiteral("http://www.fanboy.co.nz/adblock/fanboy-adblocklist-current-expanded.txt")); lst.insert(QStringLiteral("Filter von MonztA"), QStringLiteral("http://monzta.maltekraus.de/adblock.txt")); lst.insert(QStringLiteral("hufilter"), QStringLiteral("http://pete.teamlupus.hu/hufilter.txt")); lst.insert(QStringLiteral("Iceland List"), QStringLiteral("http://adblock-iceland.googlecode.com/files/icelandic%20filter.txt")); lst.insert(QStringLiteral("Japanese General Filter"), QStringLiteral("http://adblock-plus-japanese-filter.googlecode.com/svn/trunk/abp_jp_general.txt")); lst.insert(QStringLiteral("Japanese Site-Specific Filter"), QStringLiteral("http://adblock-plus-japanese-filter.googlecode.com/svn/trunk/abp_jp_site_specific.txt")); lst.insert(QStringLiteral("NLBlock"), QStringLiteral("http://www.verzijlbergh.com/adblock/nlblock.txt")); lst.insert(QStringLiteral("PLgeneral"), QStringLiteral("http://www.niecko.pl/adblock/adblock.txt")); lst.insert(QStringLiteral("Schacks Adblock Plus liste"), QStringLiteral("http://adblock.schack.dk/block.txt")); lst.insert(QStringLiteral("Xfiles"), QStringLiteral("http://mozilla.gfsolone.com/filtri.txt")); lst.insert(QStringLiteral("adblock.free.fr"), QStringLiteral("http://adblock.free.fr/adblock.txt")); lst.insert(QStringLiteral("adblock.free.fr basic (bloque les pubs uniquement)"), QStringLiteral("http://adblock.free.fr/adblock_basic.txt")); lst.insert(QStringLiteral("Ajnasz's list"), QStringLiteral("http://ajnasz.hu/adblock/recent")); lst.insert(QStringLiteral("Schuzak's Universal Filter"), QStringLiteral("http://www.schuzak.jp/other/abp.txt")); lst.insert(QStringLiteral("Rickroll Blacklist"), QStringLiteral("http://rickrolldb.com/ricklist.txt")); lst.insert(QStringLiteral("Corset+EasyList"), QStringLiteral("https://easylist-downloads.adblockplus.org/corset+easylist.txt")); return lst; } QString AdBlock::AdBlockUtil::localFilterPath() { - return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kmail2/adblockrules_local"); + return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/adblock/adblockrules_local"); }