From d8bb83aac827f8ccc959a716834e6e4ffb2927ed Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 26 May 2018 09:39:56 +0200 Subject: Profile command --- plugins/ProfileEditor/profileeditorplugin.cpp | 7 +++-- plugins/ProfileEditor/profileeditorplugin.h | 2 +- plugins/interfaces.h | 2 +- src/browser.cpp | 41 +++++++++++++++------------ src/browser.h | 4 +-- 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> ProfileEditorPlugin::commands() +QHash > ProfileEditorPlugin::commands() { - QHash> hash; - hash.insert("profileEditor:about", []() { + QHash> 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> commands() override; + QHash> 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> commands() = 0; + virtual QHash> 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("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(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(QString::fromStdString(m_config->value("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(QString::fromStdString(m_config->value("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 &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 m_windows; QVector m_plugins; - QHash> m_commands; + QHash> 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("command").value())); - return 0; + exit(app.command(QString::fromStdString(config->value("command").value()))); } // set up socket -- cgit v1.2.1