From 511032175004959b634f12d0822b48a22f5b769e Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 19 Apr 2020 22:10:12 +0300 Subject: Add plugin loading code to smolblok --- staging/smolblok/filtermanager.cpp | 27 --------------- staging/smolblok/filtermanager.hpp | 36 ++++++-------------- staging/smolblok/meson.build | 11 +++--- staging/smolblok/smolblok.cpp | 58 ++++++++++++++++++++++++++++++++ staging/smolblok/smolblok.hpp | 48 ++++++++++++++++++++++++++ staging/smolblok/test/main.cpp | 28 ++++++++------- staging/smolblok/test/sample-filters.txt | 6 ++++ 7 files changed, 143 insertions(+), 71 deletions(-) delete mode 100644 staging/smolblok/filtermanager.cpp create mode 100644 staging/smolblok/smolblok.cpp create mode 100644 staging/smolblok/smolblok.hpp (limited to 'staging/smolblok') diff --git a/staging/smolblok/filtermanager.cpp b/staging/smolblok/filtermanager.cpp deleted file mode 100644 index 05beb59..0000000 --- a/staging/smolblok/filtermanager.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#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 index 2620bd8..6ee4d3f 100644 --- a/staging/smolblok/filtermanager.hpp +++ b/staging/smolblok/filtermanager.hpp @@ -1,6 +1,13 @@ +/* + * 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 + */ + #pragma once -#include #include #include @@ -16,26 +23,8 @@ public: 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 addFilterList(FilterList *list) { + filters.append(list); } void interceptRequest(QWebEngineUrlRequestInfo &info) override @@ -47,9 +36,6 @@ public: } } -protected: - static Filter *addSubscription(const QString &filename, const QUrl &url); - private: - QList filters; + QList filters; }; diff --git a/staging/smolblok/meson.build b/staging/smolblok/meson.build index d563fcd..8ae0c74 100644 --- a/staging/smolblok/meson.build +++ b/staging/smolblok/meson.build @@ -1,18 +1,15 @@ dep_smolblok = declare_dependency( include_directories: [ '.', smolbote_interfaces ], link_with: library('smolblok', - [ - 'filtermanager.cpp', - ], + [ 'smolblok.cpp' ], include_directories: smolbote_interfaces, - dependencies: [ dep_qt5, dep_adblockfilter ] + dependencies: dep_qt5 ) ) test('smolblok', executable('filterlist', - dependencies: [ dep_qt5, dep_smolblok ], + dependencies: [ dep_qt5, dep_catch, dep_smolblok ], sources: [ 'test/main.cpp' ] - ), - args: files('test/sample-filters.txt') + ) ) diff --git a/staging/smolblok/smolblok.cpp b/staging/smolblok/smolblok.cpp new file mode 100644 index 0000000..6095082 --- /dev/null +++ b/staging/smolblok/smolblok.cpp @@ -0,0 +1,58 @@ +/* + * 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 "smolblok.hpp" +#include +#include + +bool smolblok::registerFormatPlugin(const QString &format, const QString &filename) +{ + if(format.isEmpty() || filename.isEmpty()) { + return false; + } + + auto *plugin = new QPluginLoader(filename); + if(!plugin->load()) { + delete plugin; + return false; + } + + auto *instance = qobject_cast(plugin->instance()); + if(instance == nullptr) { + delete plugin; + return false; + } + + m_formats[format] = PluginInfo{ plugin, instance }; + return false; +} + +bool smolblok::addSubscriptions(const QString &filename) +{ + if(filename.isEmpty()) { + return false; + } + + QSettings listconf(filename, QSettings::IniFormat); + + for(auto &group : listconf.childGroups()) { + listconf.beginGroup(group); + const auto *loader = m_formats.value(listconf.value("Format").toString()).instance; + if(loader != nullptr) { + QFile f(listconf.value("File").toString()); + if(!f.exists() && !loader->update(&f, listconf.value("Href").toUrl())) { + continue; + } + + m_subscriptions.addFilterList(loader->load(&f)); + } + listconf.endGroup(); + } + return false; +} + diff --git a/staging/smolblok/smolblok.hpp b/staging/smolblok/smolblok.hpp new file mode 100644 index 0000000..d3e63fb --- /dev/null +++ b/staging/smolblok/smolblok.hpp @@ -0,0 +1,48 @@ +/* + * 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 + */ + +#ifndef SMOLBOTE_SMOLBLOK_HPP +#define SMOLBOTE_SMOLBLOK_HPP + +#include "filtermanager.hpp" +#include +#include +#include + +class smolblok +{ +public: + ~smolblok() + { + for(auto &plugin : m_formats) { + delete plugin.loader; + } + } + bool registerFormatPlugin(const QString &format, const QString &filename); + const auto formats() const + { + return m_formats.keys(); + } + + bool addSubscriptions(const QString &filename); + QWebEngineUrlRequestInterceptor *interceptor() + { + return &m_subscriptions; + } + +private: + struct PluginInfo { + QPluginLoader *loader = nullptr; + FilterPlugin *instance = nullptr; + }; + + QHash m_formats; + FilterManager m_subscriptions; +}; + +#endif // SMOLBOTE_SMOLBLOK_HPP diff --git a/staging/smolblok/test/main.cpp b/staging/smolblok/test/main.cpp index 11f2b80..5624ee9 100644 --- a/staging/smolblok/test/main.cpp +++ b/staging/smolblok/test/main.cpp @@ -1,18 +1,22 @@ -#include "filtermanager.hpp" +#define CATCH_CONFIG_MAIN -int main(int argc, char **argv) -{ - if(argc != 2) { - qDebug("Usage: %s filters.txt", argv[0]); - return 77; - } +#include "smolblok.hpp" +#include - FilterManager sub; - const auto r = sub.installSubscriptions(argv[1]); +SCENARIO("smolblok") +{ + smolblok s; - if(!r.success) { - return 77; + GIVEN("invalid plugins") + { + REQUIRE(!s.registerFormatPlugin("", "")); + REQUIRE(!s.registerFormatPlugin("Format", "missing.dll")); } - return 0; + GIVEN("invalid subscriptions") + { + REQUIRE(!s.addSubscriptions("")); + REQUIRE(!s.addSubscriptions("missing.txt")); + } } + diff --git a/staging/smolblok/test/sample-filters.txt b/staging/smolblok/test/sample-filters.txt index 574501f..59e0e7b 100644 --- a/staging/smolblok/test/sample-filters.txt +++ b/staging/smolblok/test/sample-filters.txt @@ -1,4 +1,10 @@ [easylist-noelemhide] +Format = AdblockPlus File = easylist_noelemhide.txt Href = https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt +[StevenBlack] +Format = Hostlist +File = stevenblack.txt +Href = https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts + -- cgit v1.2.1