From a88921e71b7832216dedff7d2feba902390f23e5 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 17 Apr 2020 18:08:31 +0300 Subject: smolblok: rewrite filtermanager --- meson.build | 2 +- staging/adblock/filterlist.h | 1 - staging/adblock/meson.build | 15 ++++---- staging/adblock/options.h | 6 ---- staging/filterlist/downloadmanager.cpp | 53 ---------------------------- staging/filterlist/downloadmanager.h | 21 ------------ staging/filterlist/meson.build | 24 ------------- staging/filterlist/test/main.cpp | 44 ------------------------ staging/filterlist/test/parser.cpp | 37 -------------------- staging/filterlist/test/sample-filters.txt | 12 ------- staging/smolblok/README.md | 8 +++++ staging/smolblok/filtermanager.cpp | 27 +++++++++++++++ staging/smolblok/filtermanager.hpp | 55 ++++++++++++++++++++++++++++++ staging/smolblok/meson.build | 18 ++++++++++ staging/smolblok/test/main.cpp | 18 ++++++++++ staging/smolblok/test/sample-filters.txt | 4 +++ 16 files changed, 139 insertions(+), 206 deletions(-) delete mode 100644 staging/filterlist/downloadmanager.cpp delete mode 100644 staging/filterlist/downloadmanager.h delete mode 100644 staging/filterlist/meson.build delete mode 100644 staging/filterlist/test/main.cpp delete mode 100644 staging/filterlist/test/parser.cpp delete mode 100644 staging/filterlist/test/sample-filters.txt create mode 100644 staging/smolblok/README.md create mode 100644 staging/smolblok/filtermanager.cpp create mode 100644 staging/smolblok/filtermanager.hpp create mode 100644 staging/smolblok/meson.build create mode 100644 staging/smolblok/test/main.cpp create mode 100644 staging/smolblok/test/sample-filters.txt diff --git a/meson.build b/meson.build index 92a3c72..fdb9ae2 100644 --- a/meson.build +++ b/meson.build @@ -91,8 +91,8 @@ subdir('tools') subdir('test/firefox-bookmarks-json-parser') subdir('test/matcherbenchmark') -subdir('staging/filterlist') subdir('staging/adblock') +subdir('staging/smolblok') ssconfig = poi_sourceset.apply(cdata) diff --git a/staging/adblock/filterlist.h b/staging/adblock/filterlist.h index 1cdbb86..b67c187 100644 --- a/staging/adblock/filterlist.h +++ b/staging/adblock/filterlist.h @@ -20,7 +20,6 @@ class Rule; class FilterList : public Filter { public: - FilterList() = default; explicit FilterList(QIODevice &from); ~FilterList() { diff --git a/staging/adblock/meson.build b/staging/adblock/meson.build index fb92481..942f325 100644 --- a/staging/adblock/meson.build +++ b/staging/adblock/meson.build @@ -4,6 +4,11 @@ lib_adblockfilter = static_library('adblockfilter', dependencies: [ dep_qt5 ] ) +dep_adblockfilter = declare_dependency( + include_directories: ['.', smolbote_interfaces], + link_with: lib_adblockfilter +) + #AdblockPlusFilterPlugin = shared_library('AdblockPlusPlugin', # [ 'plugin/plugin.cpp', # mod_qt5.preprocess(include_directories: smolbote_interfaces, @@ -18,20 +23,16 @@ lib_adblockfilter = static_library('adblockfilter', test('adblock: rule', executable('libadblockfilter_rule', sources: 'test/rule.cpp', - link_with: lib_adblockfilter, - dependencies: [ dep_qt5, dep_catch ] + dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] )) test('adblock: options', executable('libadblockfilter_options', sources: 'test/options.cpp', - link_with: lib_adblockfilter, - dependencies: [ dep_qt5, dep_catch ] + dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] )) test('adblock: filterlist', executable('libadblockfilter_filterlist', sources: 'test/filterlist.cpp', - include_directories: smolbote_interfaces, - link_with: lib_adblockfilter, - dependencies: [ dep_qt5, dep_catch ] + dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] )) diff --git a/staging/adblock/options.h b/staging/adblock/options.h index d8f7d2b..efc47a6 100644 --- a/staging/adblock/options.h +++ b/staging/adblock/options.h @@ -17,12 +17,6 @@ namespace AdblockPlus { -enum OptionState { - Allow, - Block, - Unset -}; - struct Options { // request handling options bool exception = false; diff --git a/staging/filterlist/downloadmanager.cpp b/staging/filterlist/downloadmanager.cpp deleted file mode 100644 index bbc9287..0000000 --- a/staging/filterlist/downloadmanager.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "downloadmanager.h" - -QString saveFileName(const QUrl &url, const QString &filename) -{ - if(!filename.isEmpty()) - return filename; - - QString path = url.path(); - QString basename = QFileInfo(path).fileName(); - - if(basename.isEmpty()) - basename = "download"; - - return basename; -} - -DownloadManager::DownloadManager(QObject *parent) - : QObject(parent) -{ -} - -QNetworkReply *DownloadManager::download(const QUrl &url, const QString &filename) -{ - auto *file = new QFile(saveFileName(url, filename), this); - if(!file->open(QIODevice::WriteOnly)) { - delete file; - return nullptr; - } - - QNetworkRequest request(url); - QNetworkReply *reply = manager.get(request); - - connect(reply, &QNetworkReply::readyRead, this, [reply, file]() { - file->write(reply->readAll()); - }); - - connect(reply, &QNetworkReply::finished, this, [reply, file]() { - file->write(reply->readAll()); - file->close(); - file->deleteLater(); - reply->deleteLater(); - }); - - return reply; -} diff --git a/staging/filterlist/downloadmanager.h b/staging/filterlist/downloadmanager.h deleted file mode 100644 index edc011d..0000000 --- a/staging/filterlist/downloadmanager.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include - -class DownloadManager : public QObject -{ - Q_OBJECT - -public: - DownloadManager(QObject *parent = nullptr); - QNetworkReply *download(const QUrl &url, const QString &filename = QString()); - -private: - QNetworkAccessManager manager; -}; diff --git a/staging/filterlist/meson.build b/staging/filterlist/meson.build deleted file mode 100644 index eb5b61f..0000000 --- a/staging/filterlist/meson.build +++ /dev/null @@ -1,24 +0,0 @@ -dep_staging_utils = declare_dependency( - include_directories: include_directories('.'), - link_with: static_library('staging-utils', - [ 'downloadmanager.cpp', - mod_qt5.preprocess(moc_headers: 'downloadmanager.h') - ], - dependencies: dep_qt5 - ) -) - -executable('filterlist', - dependencies: [ dep_qt5, dep_staging_utils ], - sources: [ 'test/main.cpp' ] -) - -#test('adblockfilter: parser', -# executable('adblockfilter-parsefilter', -# dependencies: [ dep_qt5, dep_gtest, dep_adblockfilter ], -# sources: [ 'test/parser.cpp' ] -# ), -# workdir: meson.current_source_dir() / 'test', -# should_fail: true -#) - diff --git a/staging/filterlist/test/main.cpp b/staging/filterlist/test/main.cpp deleted file mode 100644 index ccaae47..0000000 --- a/staging/filterlist/test/main.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "downloadmanager.h" -#include - -int main(int argc, char **argv) -{ - if(argc != 2) { - qDebug("Usage: %s filters.txt", argv[0]); - return 77; - } - - QCoreApplication app(argc, argv); - - DownloadManager manager; - QSettings listconf(argv[1], QSettings::IniFormat); - - QVector downloads; - - qDebug("Filters:"); - for(auto &g : listconf.childGroups()) { - listconf.beginGroup(g); - const auto url = listconf.value("Href").toUrl(); - qDebug("|%s |%s|", qUtf8Printable(g.leftJustified(16, ' ', true)), qUtf8Printable(listconf.value("Href").toString().leftJustified(100, ' ', true))); - auto *reply = manager.download(url); - downloads.append(reply); - - QObject::connect(reply, &QNetworkReply::finished, [&downloads, reply]() { - if(reply->error() == QNetworkReply::NoError) { - qDebug("downloaded %s", qUtf8Printable(reply->url().toString())); - } else { - qDebug("failed %s", qUtf8Printable(reply->url().toString())); - qDebug("error [%i]: %s", reply->error(), qUtf8Printable(reply->errorString())); - } - - downloads.removeAll(reply); - if(downloads.isEmpty()) { - QCoreApplication::instance()->quit(); - } - }); - listconf.endGroup(); - } - qDebug("---"); - - return app.exec(); -} diff --git a/staging/filterlist/test/parser.cpp b/staging/filterlist/test/parser.cpp deleted file mode 100644 index 2b925e0..0000000 --- a/staging/filterlist/test/parser.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "filterlist.h" -#include -#include -#include "plugin/plugin.h" - -int main(int argc, char **argv) -{ - if(argc < 2) { - qDebug("usage: %s list1.txt ...", argv[0]); - return 77; - } - - for(int i = 1; i < argc; ++i) { - QFile f(argv[i]); - if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) { - qDebug("could not open %s", argv[i]); - return -1; - } - - AdblockPlusFilterPlugin p; - auto *l = p.load(&f); -/* - AdblockPlus::FilterList list; - QTextStream stream(&f); - const auto result = list.parse(stream); - qDebug("[%s]: %s", argv[i], (result.state == AdblockPlus::FilterList::Ok) ? "okay" : "failed"); - qDebug(" total: %i\t\tmodified: %s", result.lines_total, qUtf8Printable(list.modified().toString())); - qDebug("comments: %i\t\t expires: %s", result.lines_comments, qUtf8Printable(list.expiresOn().toString())); - qDebug(" ignored: %i\t\t valid: %s", result.lines_ignored, list.isUpToDate() ? "yes" : "no"); - qDebug(" parsed: %i", result.lines_parsed); - qDebug(" failed: %i", result.lines_failed); - qDebug("-------- --------"); -*/ - f.close(); - } - return 0; -} diff --git a/staging/filterlist/test/sample-filters.txt b/staging/filterlist/test/sample-filters.txt deleted file mode 100644 index a7c04fa..0000000 --- a/staging/filterlist/test/sample-filters.txt +++ /dev/null @@ -1,12 +0,0 @@ -[easylist] -Title = -Href = https://easylist.to/easylist/easylist.txt -Updated = -Valid = - -[easylist-noelemhide] -Title = -Href = https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt -Updated = -Valid = - diff --git a/staging/smolblok/README.md b/staging/smolblok/README.md new file mode 100644 index 0000000..1793009 --- /dev/null +++ b/staging/smolblok/README.md @@ -0,0 +1,8 @@ +## smolblok + +### What is this +This is a C++ library for URL filtering for Qt applications using QtWebEngine. + +### Supported formats +- AdblockPlus without element hiding rules + diff --git a/staging/smolblok/filtermanager.cpp b/staging/smolblok/filtermanager.cpp new file mode 100644 index 0000000..05beb59 --- /dev/null +++ b/staging/smolblok/filtermanager.cpp @@ -0,0 +1,27 @@ +#include "filtermanager.hpp" +#include "filterlist.h" +#include + +Filter *FilterManager::addSubscription(const QString &filename, const QUrl &url) +{ + if(filename.isEmpty() && url.isEmpty()) { + return nullptr; + } + + QFile f(filename); + if(!f.exists()) { + // TODO download file + return nullptr; + } + + if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) { + return nullptr; + } + + try { + auto *filter = new AdblockPlus::FilterList(f); + return filter; + } catch(std::exception &) { + return nullptr; + } +} diff --git a/staging/smolblok/filtermanager.hpp b/staging/smolblok/filtermanager.hpp new file mode 100644 index 0000000..2003d76 --- /dev/null +++ b/staging/smolblok/filtermanager.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include + +class FilterManager : public QWebEngineUrlRequestInterceptor +{ +public: + FilterManager(QObject *parent = nullptr) + : QWebEngineUrlRequestInterceptor(parent) + { + } + ~FilterManager() + { + qDeleteAll(filters); + } + + auto installSubscriptions(const QString &filename) + { + struct { + bool success = true; + } ret; + + QSettings listconf(filename, QSettings::IniFormat); + + for(const auto &group : listconf.childGroups()) { + listconf.beginGroup(group); + auto *r = addSubscription(listconf.value("File").toString(), listconf.value("Href").toString()); + if(r != nullptr) { + filters.append(r); + } else { + ret.success = false; + } + listconf.endGroup(); + } + + return ret; + } + + void interceptRequest(QWebEngineUrlRequestInfo &info) override + { + for(const auto *filter : filters) { + if(filter->filter(info)) { + return; + } + } + } + +protected: + static Filter *addSubscription(const QString &filename, const QUrl &url); + +private: + QList filters; +}; diff --git a/staging/smolblok/meson.build b/staging/smolblok/meson.build new file mode 100644 index 0000000..d563fcd --- /dev/null +++ b/staging/smolblok/meson.build @@ -0,0 +1,18 @@ +dep_smolblok = declare_dependency( + include_directories: [ '.', smolbote_interfaces ], + link_with: library('smolblok', + [ + 'filtermanager.cpp', + ], + include_directories: smolbote_interfaces, + dependencies: [ dep_qt5, dep_adblockfilter ] + ) +) + +test('smolblok', executable('filterlist', + dependencies: [ dep_qt5, dep_smolblok ], + sources: [ 'test/main.cpp' ] + ), + args: files('test/sample-filters.txt') +) + diff --git a/staging/smolblok/test/main.cpp b/staging/smolblok/test/main.cpp new file mode 100644 index 0000000..11f2b80 --- /dev/null +++ b/staging/smolblok/test/main.cpp @@ -0,0 +1,18 @@ +#include "filtermanager.hpp" + +int main(int argc, char **argv) +{ + if(argc != 2) { + qDebug("Usage: %s filters.txt", argv[0]); + return 77; + } + + FilterManager sub; + const auto r = sub.installSubscriptions(argv[1]); + + if(!r.success) { + return 77; + } + + return 0; +} diff --git a/staging/smolblok/test/sample-filters.txt b/staging/smolblok/test/sample-filters.txt new file mode 100644 index 0000000..574501f --- /dev/null +++ b/staging/smolblok/test/sample-filters.txt @@ -0,0 +1,4 @@ +[easylist-noelemhide] +File = easylist_noelemhide.txt +Href = https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt + -- cgit v1.2.1