From 037b039bfbfeda2e9b7ebef7e38616575411c876 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 14 Jan 2018 20:34:50 +0100 Subject: Initial plugins testing --- CMakeLists.txt | 9 ++++++- plugins/ProfileEditor/CMakeLists.txt | 18 +++++++++++++ plugins/ProfileEditor/ProfileEditor.json | 5 ++++ plugins/ProfileEditor/profileeditorplugin.cpp | 27 +++++++++++++++++++ plugins/ProfileEditor/profileeditorplugin.h | 29 ++++++++++++++++++++ plugins/interfaces.h | 39 +++++++++++++++++++++++++++ src/browser.cpp | 17 ++++++++++++ src/browser.h | 4 ++- src/mainwindow.cpp | 23 +++++++++++++++- src/mainwindow.h | 2 ++ 10 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 plugins/ProfileEditor/CMakeLists.txt create mode 100644 plugins/ProfileEditor/ProfileEditor.json create mode 100644 plugins/ProfileEditor/profileeditorplugin.cpp create mode 100644 plugins/ProfileEditor/profileeditorplugin.h create mode 100644 plugins/interfaces.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 12afb57..7cb59e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,8 @@ add_subdirectory(lib/bookmarks) add_subdirectory(lib/downloads) add_subdirectory(lib/navigation) +add_subdirectory(plugins/ProfileEditor) + # configure a header file to pass version information # if you don't have git, or are building this off the source tarball, define versions in version.h.in execute_process(COMMAND "git" "describe" WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} OUTPUT_VARIABLE GitDescribe OUTPUT_STRIP_TRAILING_WHITESPACE) @@ -102,11 +104,16 @@ set(SourceCode "src/forms/cookiesform.cpp" "src/forms/cookiesform.h" "src/forms/cookiesform.ui" + + # plugin interfaces + plugins/interfaces.h ) add_executable(poi ${SourceCode}) -target_include_directories(poi PRIVATE src lib) +target_include_directories(poi + PRIVATE src lib + PRIVATE plugins) target_link_libraries(poi Qt5::Core Qt5::Widgets Qt5::Concurrent Qt5::WebEngineWidgets) target_link_libraries(poi configuration) diff --git a/plugins/ProfileEditor/CMakeLists.txt b/plugins/ProfileEditor/CMakeLists.txt new file mode 100644 index 0000000..3eaa0f5 --- /dev/null +++ b/plugins/ProfileEditor/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.1.0) + +find_package(Qt5Core REQUIRED) +find_package(Qt5Widgets REQUIRED) +find_package(Qt5WebEngineWidgets REQUIRED) + +set(CMAKE_AUTOMOC ON) + +add_library(ProfileEditorPlugin SHARED + profileeditorplugin.cpp + profileeditorplugin.h) + +target_include_directories(ProfileEditorPlugin + PUBLIC ..) + +target_link_libraries(ProfileEditorPlugin + PRIVATE Qt5::Widgets + PRIVATE Qt5::WebEngineWidgets) diff --git a/plugins/ProfileEditor/ProfileEditor.json b/plugins/ProfileEditor/ProfileEditor.json new file mode 100644 index 0000000..e371eee --- /dev/null +++ b/plugins/ProfileEditor/ProfileEditor.json @@ -0,0 +1,5 @@ +{ + "name": "Profile Editor", + "shortcut": "Ctrl+F2", + "author": "Aqua-sama +#include + +QWidget *ProfileEditorPlugin::createWidget(QWebEngineProfile *profile, QWidget *parent) +{ + QWidget *widget = new QWidget(parent); + widget->setWindowFlags(Qt::ToolTip); + widget->setVisible(false); + auto *layout = new QVBoxLayout(widget); + widget->setLayout(layout); + + QLabel *storageName_label = new QLabel(profile->storageName(), widget); + if(storageName_label->text().isEmpty()) + storageName_label->setText(tr("Off-the-record")); + layout->addWidget(storageName_label); + + return widget; +} diff --git a/plugins/ProfileEditor/profileeditorplugin.h b/plugins/ProfileEditor/profileeditorplugin.h new file mode 100644 index 0000000..3115ab2 --- /dev/null +++ b/plugins/ProfileEditor/profileeditorplugin.h @@ -0,0 +1,29 @@ +/* + * 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: git://neueland.iserlohn-fortress.net/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef PROFILEEDITORPLUGIN_H +#define PROFILEEDITORPLUGIN_H + +#include +#include +#include + +class ProfileEditorPlugin : public QObject, public PluginInterface, public ProfileInterface +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID ProfileInterfaceIid FILE "ProfileEditor.json") + Q_INTERFACES(PluginInterface ProfileInterface) + +public: + // PluginInterface + + // ProfileInterface + QWidget *createWidget(QWebEngineProfile *profile, QWidget *parent) override; +}; + +#endif //PROFILEEDITORPLUGIN_H diff --git a/plugins/interfaces.h b/plugins/interfaces.h new file mode 100644 index 0000000..a43964a --- /dev/null +++ b/plugins/interfaces.h @@ -0,0 +1,39 @@ +/* + * 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: git://neueland.iserlohn-fortress.net/smolbote.git + * + * SPDX-License-Identifier: MIT + */ + +#ifndef INTERFACES_H +#define INTERFACES_H + +#include + +class QString; +class QAction; +class QWidget; +class QWebEngineProfile; + +class PluginInterface +{ +public: + virtual ~PluginInterface() = default; +}; + +class ProfileInterface +{ +public: + virtual ~ProfileInterface() = default; + + virtual QWidget *createWidget(QWebEngineProfile *profile, QWidget *parent) = 0; +}; + +#define PluginInterfaceIid "net.iserlohn-fortress.smolbote.PluginInterface" +Q_DECLARE_INTERFACE(PluginInterface, PluginInterfaceIid) + +#define ProfileInterfaceIid "net.iserlohn-fortress.smolbote.ProfileInterface" +Q_DECLARE_INTERFACE(ProfileInterface, ProfileInterfaceIid) + +#endif //INTERFACES_H diff --git a/src/browser.cpp b/src/browser.cpp index 8b7f40f..542465b 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -13,6 +13,9 @@ #include #include +#include +#include + Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) { @@ -34,12 +37,25 @@ Browser::~Browser() } qDebug("Waiting for threads to wind down: %s", QThreadPool::globalInstance()->waitForDone() ? "done" : "failed"); + + qDeleteAll(m_plugins); } void Browser::setConfiguration(std::shared_ptr &config) { m_config = config; + // plugin loader + QPluginLoader loader("plugins/ProfileEditor/libProfileEditorPlugin.so"); + qDebug("Trying to load %s: %s", qUtf8Printable(loader.fileName()), loader.load() ? "ok" : "failed"); + if(!loader.isLoaded()) { + qDebug("Error: %s", qUtf8Printable(loader.errorString())); + } else { + PluginInterface *plugin = qobject_cast(loader.instance()); + m_plugins.append(plugin); + //qDebug("author: %s", qUtf8Printable(loader.metaData()["MetaData"].toObject()["author"].toString())); + } + m_bookmarksManager = std::make_shared(QString::fromStdString(m_config->value("bookmarks.path").value())); m_downloadManager = std::make_shared(QString::fromStdString(m_config->value("downloads.path").value())); @@ -57,6 +73,7 @@ MainWindow *Browser::createWindow() window->setBookmarksWidget(m_bookmarksManager); window->setDownloadsWidget(m_downloadManager); window->setProfile(m_defaultProfile); + window->addPlugins(m_plugins); m_windows.append(window); diff --git a/src/browser.h b/src/browser.h index 9649948..236b0fb 100644 --- a/src/browser.h +++ b/src/browser.h @@ -15,6 +15,7 @@ #include #include #include "webengine/cookieinterceptor.h" +#include class MainWindow; class BookmarksWidget; @@ -26,7 +27,7 @@ class Browser : public SingleApplication public: explicit Browser(int &argc, char *argv[]); - ~Browser(); + ~Browser() final; Q_DISABLE_COPY(Browser) void setConfiguration(std::shared_ptr &config); @@ -43,6 +44,7 @@ private: std::shared_ptr m_config; QVector m_windows; + QVector m_plugins; QHash> m_profiles; std::shared_ptr m_defaultProfile; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f57cab9..913885d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include MainWindow::MainWindow(std::shared_ptr config, QWidget *parent) @@ -308,3 +307,25 @@ void MainWindow::handleTitleUpdated(const QString &title) setWindowTitle(t); //setWindowTitle(browser->settings()->value("window.title").toString().replace("title", title).replace("profile", tabBar->profile()->name())); } + +void MainWindow::addPlugins(QVector &plugins) { + for(PluginInterface *plugin : plugins) { + ProfileInterface *profilePlugin = dynamic_cast(plugin); + QWidget *w = profilePlugin->createWidget(m_profile.get(), this); + + QAction *profileAction = new QAction(this); + profileAction->setText("Profile Action"); + ui->navigationToolBar->addAction(profileAction); + connect(profileAction, &QAction::triggered, this, [this, w]() { + w->setVisible(!w->isVisible()); + if(w->isVisible()) { + w->adjustSize(); + QPoint pos = ui->navigationToolBar->pos(); + pos.setX(pos.x() + ui->navigationToolBar->width() - w->width()); + pos.setY(pos.y() + ui->navigationToolBar->height()); + w->move(mapToGlobal(pos)); + w->show(); + } + }); + } +} diff --git a/src/mainwindow.h b/src/mainwindow.h index c83778e..6d1e190 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -16,6 +16,7 @@ #include #include #include +#include namespace Ui { @@ -59,6 +60,7 @@ public slots: void setBookmarksWidget(std::shared_ptr &widget); void setDownloadsWidget(std::shared_ptr &widget); + void addPlugins(QVector &plugins); void toggleFullscreen(); -- cgit v1.2.1