diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-05-26 09:39:56 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-05-26 09:39:56 +0200 |
commit | d8bb83aac827f8ccc959a716834e6e4ffb2927ed (patch) | |
tree | 93c37d473ba37b0582a4fd029e6d3ed8f421cb31 | |
parent | Dock widget shortcut toggle widget visibility (diff) | |
download | smolbote-d8bb83aac827f8ccc959a716834e6e4ffb2927ed.tar.xz |
Profile command
-rw-r--r-- | plugins/ProfileEditor/profileeditorplugin.cpp | 7 | ||||
-rw-r--r-- | plugins/ProfileEditor/profileeditorplugin.h | 2 | ||||
-rw-r--r-- | plugins/interfaces.h | 2 | ||||
-rw-r--r-- | src/browser.cpp | 41 | ||||
-rw-r--r-- | src/browser.h | 4 | ||||
-rw-r--r-- | src/main.cpp | 5 |
6 files changed, 33 insertions, 28 deletions
diff --git a/plugins/ProfileEditor/profileeditorplugin.cpp b/plugins/ProfileEditor/profileeditorplugin.cpp index fc65350..b5e772f 100644 --- a/plugins/ProfileEditor/profileeditorplugin.cpp +++ b/plugins/ProfileEditor/profileeditorplugin.cpp @@ -10,11 +10,12 @@ #include "forms/profileview.h" #include <QHash> -QHash<QString, std::function<void()>> ProfileEditorPlugin::commands() +QHash<QString, std::function<int ()> > ProfileEditorPlugin::commands() { - QHash<QString, std::function<void()>> hash; - hash.insert("profileEditor:about", []() { + QHash<QString, std::function<int()>> hash; + hash.insert("profileEditor:about", []() -> int { qDebug("ProfileEditor for smolbote"); + return 0; }); return hash; } diff --git a/plugins/ProfileEditor/profileeditorplugin.h b/plugins/ProfileEditor/profileeditorplugin.h index 88a0783..4f88e1b 100644 --- a/plugins/ProfileEditor/profileeditorplugin.h +++ b/plugins/ProfileEditor/profileeditorplugin.h @@ -21,7 +21,7 @@ class ProfileEditorPlugin : public QObject, public PluginInterface, public Profi public: // PluginInterface - QHash<QString, std::function<void()>> commands() override; + QHash<QString, std::function<int()>> commands() override; // ProfileInterface QDialog *createWidget(QWebEngineProfile *profile, QWidget *parent) override; diff --git a/plugins/interfaces.h b/plugins/interfaces.h index ad3df97..196da0b 100644 --- a/plugins/interfaces.h +++ b/plugins/interfaces.h @@ -27,7 +27,7 @@ class PluginInterface { public: virtual ~PluginInterface() = default; - virtual QHash<QString, std::function<void()>> commands() = 0; + virtual QHash<QString, std::function<int()>> commands() = 0; }; class ProfileInterface diff --git a/src/browser.cpp b/src/browser.cpp index 8c2b010..512f207 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -79,6 +79,16 @@ void Browser::setup(const QString &defaultProfile) { Q_ASSERT_X(m_config, "Browser::setup", "Configuration not set"); + // load profiles + if(defaultProfile == "") { + auto *p = new WebProfile(this); + p->loadProfile(m_config->section("profile")); + WebProfile::setDefaultProfile(p); + } else { + auto *p = new WebProfile(defaultProfile, this); + WebProfile::setDefaultProfile(p); + } + // plugins m_plugins.append(loadPlugins(QString::fromStdString(m_config->value<std::string>("plugins.path").value()))); @@ -88,10 +98,18 @@ void Browser::setup(const QString &defaultProfile) if(plugin) { m_commands.unite(plugin->commands()); } + if(p.instance->inherits("ProfileInterface")) { + auto *profileEditor = qobject_cast<ProfileInterface *>(p.instance.get()); + Q_ASSERT_X(profileEditor != nullptr, "Browser::setup", "profile interface cast failed"); + m_commands.insert("profileEditor:editDefaultProfile", [profileEditor]() -> int { + return profileEditor->createWidget(WebProfile::defaultProfile(), nullptr)->exec(); + }); + } } // url request filter m_urlFilter = std::make_shared<UrlRequestInterceptor>(QString::fromStdString(m_config->value<std::string>("filter.path").value())); + WebProfile::defaultProfile()->setRequestInterceptor(m_urlFilter.get()); // cookie request filter @@ -100,33 +118,20 @@ void Browser::setup(const QString &defaultProfile) connect(m_bookmarks.get(), &BookmarksWidget::openUrl, this, [this](const QUrl &url) { m_windows.last()->createTab(url); }); + connect(WebProfile::defaultProfile(), &WebProfile::addBookmarkRequested, m_bookmarks.get(), &BookmarksWidget::addBookmark); // downloads m_downloads = std::make_shared<DownloadsWidget>(QString::fromStdString(m_config->value<std::string>("downloads.path").value())); - - // load profiles - if(defaultProfile == "") { - auto *p = new WebProfile(this); - p->loadProfile(m_config->section("profile")); - p->setRequestInterceptor(m_urlFilter.get()); - connect(p, &WebProfile::addBookmarkRequested, m_bookmarks.get(), &BookmarksWidget::addBookmark); - connect(p, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); - WebProfile::setDefaultProfile(p); - } else { - auto *p = new WebProfile(defaultProfile, this); - p->setRequestInterceptor(m_urlFilter.get()); - connect(p, &WebProfile::addBookmarkRequested, m_bookmarks.get(), &BookmarksWidget::addBookmark); - connect(p, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); - WebProfile::setDefaultProfile(p); - } + connect(WebProfile::defaultProfile(), &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); } -void Browser::command(const QString &command) +int Browser::command(const QString &command) { if(m_commands.contains(command)) { - m_commands.value(command)(); + return m_commands.value(command)(); } else { qWarning("No such command: %s", qUtf8Printable(command)); + return -1; } } diff --git a/src/browser.h b/src/browser.h index e5df9de..0a0d956 100644 --- a/src/browser.h +++ b/src/browser.h @@ -35,7 +35,7 @@ public: void setConfiguration(std::shared_ptr<Configuration> &config); void setup(const QString &defaultProfile); - void command(const QString &command); + int command(const QString &command); QStringList commands() const { return m_commands.keys(); @@ -53,7 +53,7 @@ private: QVector<MainWindow *> m_windows; QVector<Plugin> m_plugins; - QHash<QString, std::function<void()>> m_commands; + QHash<QString, std::function<int()>> m_commands; }; #endif // SMOLBOTE_BROWSER_H diff --git a/src/main.cpp b/src/main.cpp index 89f3005..c9b6007 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,12 +59,11 @@ int main(int argc, char **argv) for(const QString &cmd : app.commands()) { std::cout << cmd.toStdString() << std::endl; } - return 0; + exit(0); } if(config->exists("command")) { - app.command(QString::fromStdString(config->value<std::string>("command").value())); - return 0; + exit(app.command(QString::fromStdString(config->value<std::string>("command").value()))); } // set up socket |