diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-04-17 18:08:31 +0300 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-04-21 20:14:57 +0300 |
commit | a88921e71b7832216dedff7d2feba902390f23e5 (patch) | |
tree | 7d862d68262109ab61edf44ba2b7bad9dc0ec148 /staging/smolblok | |
parent | Parse type options into std::bitset (diff) | |
download | smolbote-a88921e71b7832216dedff7d2feba902390f23e5.tar.xz |
smolblok: rewrite filtermanager
Diffstat (limited to 'staging/smolblok')
-rw-r--r-- | staging/smolblok/README.md | 8 | ||||
-rw-r--r-- | staging/smolblok/filtermanager.cpp | 27 | ||||
-rw-r--r-- | staging/smolblok/filtermanager.hpp | 55 | ||||
-rw-r--r-- | staging/smolblok/meson.build | 18 | ||||
-rw-r--r-- | staging/smolblok/test/main.cpp | 18 | ||||
-rw-r--r-- | staging/smolblok/test/sample-filters.txt | 4 |
6 files changed, 130 insertions, 0 deletions
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 <QFile> + +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 <QSettings> +#include <QWebEngineUrlRequestInterceptor> +#include <smolbote/filterinterface.hpp> + +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<Filter *> 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 + |