From 616e680aa8af8f5056b5133dd44258c252ca656f Mon Sep 17 00:00:00 2001 From: aqua Date: Tue, 16 Aug 2022 16:19:04 +0300 Subject: Turn WebEngine into a plugin --- src/plugins/CMakeLists.txt | 7 +++++++ src/plugins/pluginloader.cpp | 17 +++++++++++++++++ src/plugins/pluginloader.h | 25 ++++++++++++++++++++++++ src/plugins/rplugininterface.hpp | 23 ++++++++++++++++++++++ src/plugins/rview.hpp | 25 ++++++++++++++++++++++++ src/plugins/test/pluginloader.cpp | 40 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 src/plugins/CMakeLists.txt create mode 100644 src/plugins/pluginloader.cpp create mode 100644 src/plugins/pluginloader.h create mode 100644 src/plugins/rplugininterface.hpp create mode 100644 src/plugins/rview.hpp create mode 100644 src/plugins/test/pluginloader.cpp (limited to 'src/plugins') diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt new file mode 100644 index 00000000..01b13ae1 --- /dev/null +++ b/src/plugins/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library(pluginloader STATIC pluginloader.cpp pluginloader.h) +target_link_libraries(pluginloader PUBLIC Qt6::Core Qt6::Widgets) + +if(TESTING) + add_executable(load_plugin test/pluginloader.cpp) + target_link_libraries(load_plugin GTest::gtest GTest::gtest_main Qt6::Core) +endif() diff --git a/src/plugins/pluginloader.cpp b/src/plugins/pluginloader.cpp new file mode 100644 index 00000000..ad5dbc14 --- /dev/null +++ b/src/plugins/pluginloader.cpp @@ -0,0 +1,17 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua + * ============================================================ + * Description: rekonq plugin loader + * ============================================================ */ + +#include "pluginloader.h" +#include + +PluginLoader::PluginLoader(const QString &fileName, QObject *parent) : QPluginLoader(fileName, parent) +{ + const auto metadata = metaData()["MetaData"].toObject(); + for (const auto &value : metadata["schemes"].toArray()) { m_schemes.append(value.toString()); } +} diff --git a/src/plugins/pluginloader.h b/src/plugins/pluginloader.h new file mode 100644 index 00000000..f764da9f --- /dev/null +++ b/src/plugins/pluginloader.h @@ -0,0 +1,25 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua + * ============================================================ + * Description: rekonq plugin loader + * ============================================================ */ + +#pragma once + +#include "rplugininterface.hpp" +#include + +class PluginLoader : public QPluginLoader { + +public: + explicit PluginLoader(const QString &path, QObject *parent = nullptr); + [[nodiscard]] bool hasScheme(const QString &scheme) { return m_schemes.contains(scheme); } + + [[nodiscard]] RekonqPluginInterface *interface() { return qobject_cast(instance()); } + +private: + QStringList m_schemes; +}; diff --git a/src/plugins/rplugininterface.hpp b/src/plugins/rplugininterface.hpp new file mode 100644 index 00000000..16186591 --- /dev/null +++ b/src/plugins/rplugininterface.hpp @@ -0,0 +1,23 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua + * ============================================================ + * Description: rekonq plugin interface + * ============================================================ */ + +#pragma once + +#include "rview.hpp" +#include + +class RekonqPluginInterface : public QObject { + Q_OBJECT + +public: + virtual rView *view(const QUrl &url) = 0; +}; + +#define RekonqPluginInterface_iid "rekonq.3.RekongPluginInterface" +Q_DECLARE_INTERFACE(RekonqPluginInterface, RekonqPluginInterface_iid) diff --git a/src/plugins/rview.hpp b/src/plugins/rview.hpp new file mode 100644 index 00000000..71bf5ddf --- /dev/null +++ b/src/plugins/rview.hpp @@ -0,0 +1,25 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua + * ============================================================ + * Description: View Interface + * ============================================================ */ + +#pragma once + +#include +#include + +class rView : public QWidget { + Q_OBJECT + +public: + explicit rView(const QUrl &url = QUrl(), QWidget *parent = nullptr) : QWidget(parent) {} + +signals: + void iconChanged(const QIcon &); + void urlChanged(const QUrl &); + void titleChanged(const QString &); +}; diff --git a/src/plugins/test/pluginloader.cpp b/src/plugins/test/pluginloader.cpp new file mode 100644 index 00000000..83b30c40 --- /dev/null +++ b/src/plugins/test/pluginloader.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +char *pluginPath; + +class PluginLoaderTest : public ::testing::Test { + +protected: + void SetUp() override { loader = new QPluginLoader; } + void TearDown() override { delete loader; } + + QPluginLoader *loader = nullptr; +}; + +TEST_F(PluginLoaderTest, InterfaceTest) +{ + // Ensure plugin loads + loader->setFileName(pluginPath); + EXPECT_TRUE(loader->load()) << "For plugin: " << pluginPath << '\n' << qUtf8Printable(loader->errorString()); + + // Ensure plugin has metadata + const auto metadata = loader->metaData()["MetaData"].toObject(); + EXPECT_TRUE(metadata.contains("name")); + EXPECT_FALSE(metadata["name"].toString().isEmpty()); + EXPECT_TRUE(metadata.contains("schemes")); + EXPECT_FALSE(metadata["schemes"].toArray().isEmpty()); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " [path to plugin]" << std::endl; + return -1; + } + pluginPath = argv[1]; + return RUN_ALL_TESTS(); +} -- cgit v1.2.1