From d8bb83aac827f8ccc959a716834e6e4ffb2927ed Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 26 May 2018 09:39:56 +0200 Subject: Profile command --- src/browser.cpp | 41 +++++++++++++++++++++++------------------ src/browser.h | 4 ++-- src/main.cpp | 5 ++--- 3 files changed, 27 insertions(+), 23 deletions(-) (limited to 'src') 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