diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-08-25 20:07:54 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-08-25 20:07:54 +0200 |
commit | 3b611f42bf5c233ca66e406474896022b6ed78cc (patch) | |
tree | cbf30d20220f91c5eccaf8d89a2aac1989241b08 | |
parent | MainWindow: fix load session action (diff) | |
download | smolbote-3b611f42bf5c233ca66e406474896022b6ed78cc.tar.xz |
Add BrowserInterface to simplify plugins a bit
-rw-r--r-- | plugins/ConfigurationEditor/configurationeditorplugin.cpp | 8 | ||||
-rw-r--r-- | plugins/ConfigurationEditor/configurationeditorplugin.h | 11 | ||||
-rw-r--r-- | plugins/ProfileEditor/profileeditorplugin.cpp | 15 | ||||
-rw-r--r-- | plugins/ProfileEditor/profileeditorplugin.h | 12 | ||||
-rw-r--r-- | plugins/interfaces.h | 31 | ||||
-rw-r--r-- | src/browser.cpp | 36 | ||||
-rw-r--r-- | src/browser.h | 6 |
7 files changed, 40 insertions, 79 deletions
diff --git a/plugins/ConfigurationEditor/configurationeditorplugin.cpp b/plugins/ConfigurationEditor/configurationeditorplugin.cpp index 93b1c69..0d44a8a 100644 --- a/plugins/ConfigurationEditor/configurationeditorplugin.cpp +++ b/plugins/ConfigurationEditor/configurationeditorplugin.cpp @@ -18,13 +18,7 @@ CommandHash_t ConfigurationEditorPlugin::commands() QDialog *ConfigurationEditorPlugin::createWidget(QWidget *parent) { - auto *dlg = new ConfigurationDialog(m_config, parent); + auto *dlg = new ConfigurationDialog(browser->getConfiguration(), parent); dlg->setAttribute(Qt::WA_DeleteOnClose, true); return dlg; } - -void ConfigurationEditorPlugin::setConfiguration(Configuration *cfg) -{ - Q_CHECK_PTR(cfg); - m_config = cfg; -} diff --git a/plugins/ConfigurationEditor/configurationeditorplugin.h b/plugins/ConfigurationEditor/configurationeditorplugin.h index 6054723..6bb72ce 100644 --- a/plugins/ConfigurationEditor/configurationeditorplugin.h +++ b/plugins/ConfigurationEditor/configurationeditorplugin.h @@ -5,22 +5,17 @@ #include <interfaces.h> class QDialog; -class ConfigurationEditorPlugin : public QObject, public PluginInterface, public ConfigurationInterface +class ConfigurationEditorPlugin : public QObject, public PluginInterface { Q_OBJECT - Q_PLUGIN_METADATA(IID ProfileInterfaceIid FILE "ConfigurationEditor.json") - Q_INTERFACES(PluginInterface ConfigurationInterface) + Q_PLUGIN_METADATA(IID PluginInterfaceIid FILE "ConfigurationEditor.json") + Q_INTERFACES(PluginInterface) public: // PluginInterface CommandHash_t commands() override; QDialog *createWidget(QWidget *parent = nullptr) override; - // ConfigurationInterface - void setConfiguration(Configuration *cfg) override; - -private: - Configuration *m_config = nullptr; }; #endif // CONFIGURATIONEDITOR_PLUGIN_H diff --git a/plugins/ProfileEditor/profileeditorplugin.cpp b/plugins/ProfileEditor/profileeditorplugin.cpp index d13e819..5d5c18a 100644 --- a/plugins/ProfileEditor/profileeditorplugin.cpp +++ b/plugins/ProfileEditor/profileeditorplugin.cpp @@ -20,24 +20,13 @@ QHash<QString, std::function<int()>> ProfileEditorPlugin::commands() auto *dialog = createWidget(nullptr); return dialog->exec(); }); - - hash.insert("list-profiles", [this]() -> int { - for(const WebProfile *profile : qAsConst(profiles)) { - qDebug(" - %s", qUtf8Printable(profile->name())); - } - return 0; - }); return hash; } QDialog *ProfileEditorPlugin::createWidget(QWidget *parent) { - auto *widget = new ProfileManagerDialog(profiles, parent); + Q_CHECK_PTR(browser); + auto *widget = new ProfileManagerDialog(browser->profiles(), parent); widget->setAttribute(Qt::WA_DeleteOnClose, true); return widget; } - -void ProfileEditorPlugin::registerProfile(WebProfile *profile) -{ - profiles.append(profile); -} diff --git a/plugins/ProfileEditor/profileeditorplugin.h b/plugins/ProfileEditor/profileeditorplugin.h index bc95708..c3e08af 100644 --- a/plugins/ProfileEditor/profileeditorplugin.h +++ b/plugins/ProfileEditor/profileeditorplugin.h @@ -14,11 +14,11 @@ class QWebEngineProfile; class QDialog; -class ProfileEditorPlugin : public QObject, public PluginInterface, public ProfileInterface +class ProfileEditorPlugin : public QObject, public PluginInterface { Q_OBJECT - Q_PLUGIN_METADATA(IID ProfileInterfaceIid FILE "ProfileEditor.json") - Q_INTERFACES(PluginInterface ProfileInterface) + Q_PLUGIN_METADATA(IID PluginInterfaceIid FILE "ProfileEditor.json") + Q_INTERFACES(PluginInterface) public: // PluginInterface @@ -26,12 +26,6 @@ public: // ProfileInterface QDialog *createWidget(QWidget *parent) override; - -public slots: - void registerProfile(WebProfile *profile) override; - -private: - QVector<WebProfile *> profiles; }; #endif //PROFILEEDITOR_PLUGIN_H diff --git a/plugins/interfaces.h b/plugins/interfaces.h index aad43c1..fc8eac8 100644 --- a/plugins/interfaces.h +++ b/plugins/interfaces.h @@ -20,6 +20,13 @@ class QDialog; class WebProfile; class Configuration; +class BrowserInterface +{ +public: + virtual Configuration *getConfiguration() const = 0; + virtual const QVector<WebProfile *> profiles() const = 0; +}; + struct Plugin { QString name; @@ -36,29 +43,17 @@ public: virtual ~PluginInterface() = default; virtual CommandHash_t commands() = 0; virtual QDialog *createWidget(QWidget *parent = nullptr) = 0; -}; -class ProfileInterface -{ -public: - virtual ~ProfileInterface() = default; - virtual void registerProfile(WebProfile *profile) = 0; -}; + void setBrowserInterface(BrowserInterface *browser) + { + this->browser = browser; + } -class ConfigurationInterface -{ -public: - virtual ~ConfigurationInterface() = default; - virtual void setConfiguration(Configuration *cfg) = 0; +protected: + BrowserInterface *browser = nullptr; }; #define PluginInterfaceIid "net.iserlohn-fortress.smolbote.PluginInterface" Q_DECLARE_INTERFACE(PluginInterface, PluginInterfaceIid) -#define ProfileInterfaceIid "net.iserlohn-fortress.smolbote.ProfileInterface" -Q_DECLARE_INTERFACE(ProfileInterface, ProfileInterfaceIid) - -#define ConfigurationInterfaceIid "net.iserlohn-fortress.smolbote.ConfigurationInterface" -Q_DECLARE_INTERFACE(ConfigurationInterface, ConfigurationInterfaceIid) - #endif // SMOLBOTE_PLUGIN_INTERFACES_H diff --git a/src/browser.cpp b/src/browser.cpp index 375796c..e248a3f 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -58,30 +58,20 @@ void Browser::setConfiguration(std::unique_ptr<Configuration> &config) m_config = std::move(config); } -void Browser::registerPlugin(const Plugin &plugin) +Configuration *Browser::getConfiguration() const { Q_ASSERT(m_config); + return m_config.get(); +} - if(plugin.instance->inherits("ProfileInterface")) { - auto *profileEditor = qobject_cast<ProfileInterface *>(plugin.instance); - Q_ASSERT_X(profileEditor != nullptr, "Browser::setup", "profile interface cast failed"); - - ProfileIterator it(ProfileManager::profileList()); - while(it.hasNext()) { - it.next(); - profileEditor->registerProfile(it.value()); - } - connect(this, &Browser::registerProfile, [=](WebProfile *profile) { - profileEditor->registerProfile(profile); - }); - } +void Browser::registerPlugin(const Plugin &plugin) +{ + Q_ASSERT(m_config); - if(plugin.instance->inherits("ConfigurationInterface")) { - auto *configEditor = qobject_cast<ConfigurationInterface *>(plugin.instance); - Q_CHECK_PTR(configEditor); - configEditor->setConfiguration(m_config.get()); - } + auto *p = qobject_cast<PluginInterface *>(plugin.instance); + Q_CHECK_PTR(p); + p->setBrowserInterface(this); m_plugins.append(plugin); } @@ -117,7 +107,6 @@ void Browser::setup(const QString &defaultProfile) auto *profile = ProfileManager::loadProfile(f.absoluteFilePath(), defaults); connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); profile->setRequestInterceptor(m_urlFilter.get()); - emit registerProfile(profile); } } @@ -127,7 +116,6 @@ void Browser::setup(const QString &defaultProfile) auto *profile = ProfileManager::loadProfile(QString(), defaults); connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); profile->setRequestInterceptor(m_urlFilter.get()); - emit registerProfile(profile); } WebProfile::setDefaultProfile(ProfileManager::profile(defaultProfile)); } @@ -139,6 +127,12 @@ void Browser::setup(const QString &defaultProfile) }); } +const QVector<WebProfile *> Browser::profiles() const +{ + const QMap<QString, WebProfile *> profileList = ProfileManager::profileList(); + return QVector<WebProfile *>::fromList(profileList.values()); +} + void Browser::createSession(const QJsonObject &object) { MainWindow *mainwindow = nullptr; diff --git a/src/browser.h b/src/browser.h index 99da8a7..340a16d 100644 --- a/src/browser.h +++ b/src/browser.h @@ -24,7 +24,7 @@ class DownloadsWidget; class UrlRequestInterceptor; class MainWindow; class WebProfile; -class Browser : public SingleApplication +class Browser : public SingleApplication, public BrowserInterface { Q_OBJECT @@ -37,6 +37,7 @@ public slots: public: void setConfiguration(std::unique_ptr<Configuration> &config); + Configuration *getConfiguration() const override; void registerPlugin(const Plugin &plugin); void setup(const QString &defaultProfile); @@ -46,8 +47,7 @@ public: return m_bookmarks; } -signals: - void registerProfile(WebProfile *profile); + const QVector<WebProfile *> profiles() const override; public slots: void createSession(const QJsonObject &object); |