diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-05 10:55:56 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-05 14:59:10 +0200 |
commit | 7918bce1422fb217b59131111c6fb1ccdf52ddc4 (patch) | |
tree | 369fce128f1b021c0c88455039c0011a2014b9d7 /src/webengine | |
parent | Disable plugins as broken (diff) | |
download | smolbote-7918bce1422fb217b59131111c6fb1ccdf52ddc4.tar.xz |
Code cleanup
- Replace Browser::getProfileManager with WebProfileManager::instance
- Make WebProfileManager::profileMenu a free function
- Replace Browser::loadProfile with Browser::loadProfiles
Diffstat (limited to 'src/webengine')
-rw-r--r-- | src/webengine/webprofilemanager.cpp | 44 | ||||
-rw-r--r-- | src/webengine/webprofilemanager.h | 23 | ||||
-rw-r--r-- | src/webengine/webview.cpp | 3 |
3 files changed, 50 insertions, 20 deletions
diff --git a/src/webengine/webprofilemanager.cpp b/src/webengine/webprofilemanager.cpp index 2fe6222..80762f7 100644 --- a/src/webengine/webprofilemanager.cpp +++ b/src/webengine/webprofilemanager.cpp @@ -12,6 +12,17 @@ #include <QFileInfo> #include <QWebEngineSettings> +static WebProfileManager *s_instance = nullptr; + +auto WebProfileManager::instance() -> const WebProfileManager * +{ + return s_instance; +} +void WebProfileManager::setInstance(WebProfileManager *ptr) +{ + s_instance = ptr; +} + WebProfileManager::WebProfileManager(QObject *parent) : QObject(parent) { @@ -37,11 +48,21 @@ WebProfileManager::~WebProfileManager() } } -WebProfile *WebProfileManager::profile(const QString &id, const QString &path, bool isOffTheRecord) +WebProfile *WebProfileManager::profile(const QString &id) const { // Check if profile exists - if(profiles.contains(id)) + if(profiles.contains(id)) { return profiles.value(id).ptr; + } + + return nullptr; +} + +WebProfile *WebProfileManager::add(const QString &id, const QString &path, bool isOffTheRecord) +{ + if(profiles.contains(id)) { + return nullptr; + } Configuration conf; Profile profile; @@ -52,12 +73,13 @@ WebProfile *WebProfileManager::profile(const QString &id, const QString &path, b profile.settings = new QSettings; // QWebEngineCore cleans up profiles automatically, so no need to set parent - profile.ptr = [id, isOffTheRecord, profile]() { - if(profile.settings->value("otr", isOffTheRecord).toBool()) - return new WebProfile(/* name */ profile.settings->value("name", id).toString(), /* parent */ nullptr); - else - return new WebProfile(/* storageName */ id, /* name */ profile.settings->value("name", id).toString(), /* parent */ nullptr); - }(); + if(profile.settings->value("otr", isOffTheRecord).toBool()) { + // name parent + profile.ptr = new WebProfile(profile.settings->value("name", id).toString(), nullptr); + } else { + // storageName name parent + profile.ptr = new WebProfile(id, profile.settings->value("name", id).toString(), nullptr); + } profile.settings->setParent(profile.ptr); @@ -133,12 +155,12 @@ void WebProfileManager::deleteProfile(const QString &id) } } -void WebProfileManager::profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current, bool checkable) const +void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current, bool checkable) { auto *group = new QActionGroup(menu); - connect(menu, &QMenu::aboutToHide, group, &QActionGroup::deleteLater); + QObject::connect(menu, &QMenu::aboutToHide, group, &QActionGroup::deleteLater); - for(const auto &profile : profiles) { + for(const auto &profile : s_instance->profiles) { auto *action = menu->addAction(profile.ptr->name(), profile.ptr, [profile, callback]() { callback(profile.ptr); }); diff --git a/src/webengine/webprofilemanager.h b/src/webengine/webprofilemanager.h index 2d9cd29..0e18d5f 100644 --- a/src/webengine/webprofilemanager.h +++ b/src/webengine/webprofilemanager.h @@ -18,28 +18,34 @@ #include <QSettings> #include <functional> +void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current = nullptr, bool checkable = false); + +class Browser; class WebProfileManager : public QObject { Q_OBJECT + + friend class Browser; + friend void profileMenu(QMenu *, const std::function<void(WebProfile *)> &, WebProfile *current, bool); + public: explicit WebProfileManager(QObject *parent); - ~WebProfileManager(); + ~WebProfileManager() override; + + static auto instance() -> const WebProfileManager *; + static void setInstance(WebProfileManager *ptr); /** Create a profile with specified id * param id The profile ID - * param path The path to the profile settings - * param isOffTheRecord Off-the-record toggle * return WebProfile* The profile, or nullptr if one could not be created */ - WebProfile *profile(const QString &id, const QString &path = QString(), bool isOffTheRecord = true); + WebProfile *profile(const QString &id) const; /** Set a profile for deletion * param id The profile ID * return void */ - void deleteProfile(const QString &id); - - void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current = nullptr, bool checkable = false) const; + [[deprecated]] void deleteProfile(const QString &id); const QStringList idList() const { @@ -56,6 +62,9 @@ public: return QString(); } +protected: + WebProfile *add(const QString &id, const QString &path = QString(), bool isOffTheRecord = true); + private: struct Profile { WebProfile *ptr = nullptr; diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index 4fa227f..5b2b6fd 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -7,7 +7,6 @@ */ #include "webview.h" -#include "browser.h" #include "subwindow/subwindow.h" #include "wallet/wallet.h" #include "webpage.h" @@ -255,7 +254,7 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) }); auto *newTabMenu = menu->addMenu(tr("Open link in new tab with profile")); - dynamic_cast<Browser *>(qApp)->getProfileManager()->profileMenu(newTabMenu, [this, ctxdata](WebProfile *profile) { + profileMenu(newTabMenu, [this, ctxdata](WebProfile *profile) { auto *view = this->createWindow(QWebEnginePage::WebBrowserTab); view->setProfile(profile); view->load(ctxdata.linkUrl()); |