From 1581e94b9ac98f5d385a71a5bfbde81da22da2b2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 24 Jun 2018 18:42:40 +0200 Subject: Add ProfileManager --- lib/web/webprofile.cpp | 63 --------------------------- lib/web/webprofile.h | 9 ---- src/CMakeLists.txt | 2 + src/browser.cpp | 29 ++++--------- src/browser.h | 10 +---- src/mainwindow/mainwindow.cpp | 4 +- src/mainwindow/subwindow.cpp | 45 +++++-------------- src/mainwindow/subwindow.h | 1 - src/profilemanager.cpp | 89 ++++++++++++++++++++++++++++++++++++++ src/profilemanager.h | 35 +++++++++++++++ src/session.cpp | 4 +- src/session.h | 2 +- src/webengine/widgets/pagemenu.cpp | 12 ++--- 13 files changed, 159 insertions(+), 146 deletions(-) create mode 100644 src/profilemanager.cpp create mode 100644 src/profilemanager.h diff --git a/lib/web/webprofile.cpp b/lib/web/webprofile.cpp index 1613e52..9b16a61 100644 --- a/lib/web/webprofile.cpp +++ b/lib/web/webprofile.cpp @@ -13,72 +13,10 @@ #include WebProfile *WebProfile::profile = nullptr; -QMap WebProfile::profiles; - -WebProfile* WebProfile::loadProfile(const QString &name, const QHash &defaults, const QString &path, QObject *parent) -{ - WebProfile *profile = nullptr; -#ifdef QT_DEBUG - qDebug("+ Reading config for profile '%s': %s", qUtf8Printable(name), qUtf8Printable(path)); -#endif - QSettings config(path, QSettings::IniFormat); - - if(name.isEmpty()) { - // a default otr profile - profile = new WebProfile(QObject::tr("Off-the-record"), path, parent); - - } else if(config.value("otr").toBool()) { - // a named otr profile - profile = new WebProfile(config.value("name", name).toString(), path, parent); - - } else { - // a named profile - profile = new WebProfile(name, config.value("name", name).toString(), path, parent); - } - - Q_CHECK_PTR(profile); - profiles.insert(name, profile); - - profile->setSearch(config.value("search", defaults.value("profile.search")).toString()); - profile->setHomepage(config.value("homepage", defaults.value("profile.homepage")).toUrl()); - profile->setNewtab(config.value("newtab", defaults.value("profile.newtab")).toUrl()); - - config.beginGroup("properties"); - { - const auto keys = config.childKeys(); - for(const QString &key : keys) { -#ifdef QT_DEBUG - qDebug("- set property %s to %s", qUtf8Printable(key), qUtf8Printable(config.value(key).toString())); -#endif - profile->setProperty(qUtf8Printable(key), config.value(key)); - } - } - config.endGroup(); // properties - - config.beginGroup("attributes"); - { - const auto keys = config.childKeys(); - auto *settings = profile->settings(); - for(const QString &key : keys) { -#ifdef QT_DEBUG - qDebug("- set attribute %s to %s", qUtf8Printable(key), qUtf8Printable(config.value(key).toString())); -#endif - auto attribute = static_cast(key.toInt()); - settings->setAttribute(attribute, config.value(key).toBool()); - } - } - config.endGroup(); - - return profile; -} WebProfile::WebProfile(const QString &name, const QString &configPath, QObject *parent) : QWebEngineProfile(parent) { - if(configPath.isEmpty()) - m_id = configPath; - else - m_id = QFileInfo(configPath).baseName(); m_name = name; m_configPath = configPath; @@ -97,7 +35,6 @@ WebProfile::WebProfile(const QString &name, const QString &configPath, QObject * WebProfile::WebProfile(const QString &storageName, const QString &name, const QString &configPath, QObject *parent) : QWebEngineProfile(storageName, parent) { - m_id = QFileInfo(configPath).baseName(); m_name = name; m_configPath = configPath; diff --git a/lib/web/webprofile.h b/lib/web/webprofile.h index d41625c..5107b8f 100644 --- a/lib/web/webprofile.h +++ b/lib/web/webprofile.h @@ -54,13 +54,6 @@ public: return WebProfile::profile; } - static WebProfile* loadProfile(const QString &name, const QHash &defaults, const QString &path = QString(), QObject *parent = nullptr); - - const QString id() const - { - return profiles.key(const_cast(this), ""); - } - QString name() const { return m_name; @@ -112,9 +105,7 @@ signals: private: static WebProfile *profile; - static QMap profiles; - QString m_id; QString m_configPath; QString m_name; QString m_search = QString("about:blank"); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 796f8e6..bb8f229 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,8 @@ add_executable(poi browser.h session.cpp session.h + profilemanager.cpp + profilemanager.h ../data/resources.qrc # main window diff --git a/src/browser.cpp b/src/browser.cpp index d5a3d3e..73b466f 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -23,6 +23,7 @@ #include #include #include +#include "profilemanager.h" Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) @@ -62,7 +63,9 @@ void Browser::registerPlugin(const Plugin &plugin) auto *profileEditor = qobject_cast(plugin.instance); Q_ASSERT_X(profileEditor != nullptr, "Browser::setup", "profile interface cast failed"); - for(auto it = m_profiles.constBegin(); it != m_profiles.constEnd(); ++it) { + ProfileIterator it(ProfileManager::profileList()); + while(it.hasNext()) { + it.next(); profileEditor->registerProfile(it.value()); } connect(this, &Browser::registerProfile, [=](WebProfile *profile) { @@ -94,26 +97,18 @@ void Browser::setup(const QString &defaultProfile) for(const QFileInfo &f : entries) { auto name = f.baseName(); - auto *profile = WebProfile::loadProfile(name, defaults, f.absoluteFilePath(), this); - m_profiles.insert(name, profile); - connect(profile, &WebProfile::destroyed, this, [=]() { - m_profiles.remove(name); - }); + auto *profile = ProfileManager::loadProfile(f.absoluteFilePath(), defaults); emit registerProfile(profile); } } // set default profile - if(!m_profiles.contains(defaultProfile)) { + if(ProfileManager::profile(defaultProfile) == nullptr) { // if this profile has not been added, it doesn't have a path - auto *profile = WebProfile::loadProfile(defaultProfile, defaults, QString(), this); - m_profiles.insert(defaultProfile, profile); - connect(profile, &WebProfile::destroyed, this, [=]() { - m_profiles.remove(defaultProfile); - }); + auto *profile = ProfileManager::loadProfile(QString(), defaults); emit registerProfile(profile); } - WebProfile::setDefaultProfile(m_profiles.value(defaultProfile)); + WebProfile::setDefaultProfile(ProfileManager::profile(defaultProfile)); } // url request filter @@ -133,14 +128,6 @@ void Browser::setup(const QString &defaultProfile) connect(WebProfile::defaultProfile(), &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); } -WebProfile *Browser::profile(const QString &name) const -{ - if(m_profiles.contains(name)) - return m_profiles.value(name); - else - return nullptr; -} - void Browser::createSession(const QString &profileName, bool newWindow, const QStringList &urls) { if(m_windows.isEmpty()) { diff --git a/src/browser.h b/src/browser.h index c16e495..b511b45 100644 --- a/src/browser.h +++ b/src/browser.h @@ -31,7 +31,6 @@ class Browser : public SingleApplication public: explicit Browser(int &argc, char *argv[]); ~Browser() final; - Q_DISABLE_COPY(Browser) public slots: void about(); @@ -47,12 +46,6 @@ public: return m_bookmarks; } - WebProfile *profile(const QString &name) const; - const QStringList profiles() const - { - return m_profiles.keys(); - } - signals: void registerProfile(WebProfile *profile); @@ -61,12 +54,13 @@ public slots: MainWindow *createWindow(); private: + Q_DISABLE_COPY(Browser) + std::shared_ptr m_config; std::shared_ptr m_bookmarks; std::shared_ptr m_downloads; std::shared_ptr m_urlFilter; - QMap m_profiles; QVector m_windows; QVector m_plugins; }; diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index 0932aee..07ac8f0 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -193,8 +193,8 @@ void MainWindow::createMenuBar() auto *debugMenu = menuBar()->addMenu(tr("Debug")); debugMenu->addAction(tr("Print window session"), [this]() { - auto json = Session::toJson(this); - qDebug("session data >>>\n%s\n<<<", qUtf8Printable(json.toJson())); + auto json = Session::toJsonObject(this); + qDebug("session data >>>\n%s\n<<<", qUtf8Printable(QJsonDocument(json).toJson())); }); #endif } diff --git a/src/mainwindow/subwindow.cpp b/src/mainwindow/subwindow.cpp index cbfe46a..354e07e 100644 --- a/src/mainwindow/subwindow.cpp +++ b/src/mainwindow/subwindow.cpp @@ -19,6 +19,7 @@ #include #include #include +#include "profilemanager.h" SubWindow::SubWindow(const QHash &config, QWidget *parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) @@ -44,12 +45,15 @@ SubWindow::SubWindow(const QHash &config, QWidget *parent, Qt: Browser *browser = qobject_cast(qApp); Q_CHECK_PTR(browser); - for(const QString &name : browser->profiles()) { - auto *loadAction = loadProfile_menu->addAction(name); - connect(loadAction, &QAction::triggered, this, [name, browser, profileName_action, this]() { - auto *profile = browser->profile(name); + ProfileIterator it(ProfileManager::profileList()); + while(it.hasNext()) { + it.next(); + auto *profile =it.value(); + auto *loadAction = loadProfile_menu->addAction(profile->name()); + + connect(loadAction, &QAction::triggered, this, [=]() { this->setProfile(profile); - profileName_action->setText(tr("Profile: %1").arg(name)); + profileName_action->setText(tr("Profile: %1").arg(profile->name())); }); } } @@ -149,14 +153,14 @@ void SubWindow::setCurrentTab(int index) QJsonObject SubWindow::session() const { QJsonObject obj; - obj.insert("profile", profile->id()); + obj.insert("profile", ProfileManager::id(profile)); QJsonArray tabs; for(int i = 0; i < tabWidget->count(); ++i) { auto *view = qobject_cast(tabWidget->widget(i)); if(view) { QJsonObject tab; - tab.insert(view->url().toString(), view->profile()->id()); + tab.insert(view->url().toString(), ProfileManager::id(view->profile())); tabs.append(tab); } } @@ -164,30 +168,3 @@ QJsonObject SubWindow::session() const return obj; } - -void SubWindow::restoreSession(const QJsonObject &sessionData) -{ - auto *browser = qobject_cast(qApp); - Q_CHECK_PTR(browser); - - Q_ASSERT_X(sessionData.value("profile") != QJsonValue::Undefined, "Window::restoreSession", "no profile in json"); - if(browser->profiles().contains(sessionData.value("profile").toString())) - profile = browser->profile(sessionData.value("profile").toString()); - - Q_ASSERT_X(sessionData.value("tabs") != QJsonValue::Undefined, "Window::restoreSession", "no tabs in json"); - const QJsonArray tabs = sessionData.value("tabs").toArray(); - - if(tabs.count() == 0) { - // open a newtab - auto *view = new WebView(profile, this); - view->load(profile->newtab()); - tabWidget->addTab(view); - return; - } - - for(const auto &tab : tabs) { - auto *view = new WebView(profile, this); - view->load(QUrl::fromUserInput(tab.toString())); - tabWidget->addTab(view); - } -} diff --git a/src/mainwindow/subwindow.h b/src/mainwindow/subwindow.h index ac1610a..d5f4f4c 100644 --- a/src/mainwindow/subwindow.h +++ b/src/mainwindow/subwindow.h @@ -30,7 +30,6 @@ public: void setProfile(WebProfile *profile); QJsonObject session() const; - void restoreSession(const QJsonObject &sessionData); signals: void currentViewChanged(WebView *view); diff --git a/src/profilemanager.cpp b/src/profilemanager.cpp new file mode 100644 index 0000000..1038598 --- /dev/null +++ b/src/profilemanager.cpp @@ -0,0 +1,89 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "profilemanager.h" +#include +#include +#include +#include + +QMap ProfileManager::profiles; + +ProfileManager::ProfileManager(QObject *parent) : QObject(parent) +{ +} + +WebProfile *ProfileManager::loadProfile(const QString &path, const QHash &defaults) +{ + WebProfile *profile = nullptr; +#ifdef QT_DEBUG + qDebug("==> Reading profile config : %s", qUtf8Printable(path)); +#endif + + const QString id = QFileInfo(path).baseName(); + QSettings config(path, QSettings::IniFormat); + + if(config.value("otr", true).toBool()) { + profile = new WebProfile(config.value("name", id).toString(), path, nullptr); + } else { + profile = new WebProfile(id, config.value("name", id).toString(), path, nullptr); + } + + Q_CHECK_PTR(profile); + profiles.insert(id, profile); + + profile->setSearch(config.value("search", defaults.value("profile.search")).toString()); + profile->setHomepage(config.value("homepage", defaults.value("profile.homepage")).toUrl()); + profile->setNewtab(config.value("newtab", defaults.value("profile.newtab")).toUrl()); + + config.beginGroup("properties"); + { + const auto keys = config.childKeys(); + for(const QString &key : keys) { +#ifdef QT_DEBUG + qDebug("- set property %s to %s", qUtf8Printable(key), qUtf8Printable(config.value(key).toString())); +#endif + profile->setProperty(qUtf8Printable(key), config.value(key)); + } + } + config.endGroup(); // properties + + config.beginGroup("attributes"); + { + const auto keys = config.childKeys(); + auto *settings = profile->settings(); + for(const QString &key : keys) { +#ifdef QT_DEBUG + qDebug("- set attribute %s to %s", qUtf8Printable(key), qUtf8Printable(config.value(key).toString())); +#endif + auto attribute = static_cast(key.toInt()); + settings->setAttribute(attribute, config.value(key).toBool()); + } + } + config.endGroup(); + + return profile; +} + +const QString ProfileManager::id(WebProfile *profile) +{ + return profiles.key(profile); +} + +WebProfile *ProfileManager::profile(const QString &id) +{ + if(profiles.contains(id)) + return profiles.value(id); + else + return nullptr; +} + +const QMap &ProfileManager::profileList() +{ + return profiles; +} diff --git a/src/profilemanager.h b/src/profilemanager.h new file mode 100644 index 0000000..b2509a1 --- /dev/null +++ b/src/profilemanager.h @@ -0,0 +1,35 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_PROFILEMANAGER_H +#define SMOLBOTE_PROFILEMANAGER_H + +#include +#include +#include + +typedef QMapIterator ProfileIterator; + +class WebProfile; +class ProfileManager : public QObject +{ + Q_OBJECT +public: + explicit ProfileManager(QObject *parent = nullptr); + + static WebProfile *loadProfile(const QString &path, const QHash &defaults); + + static const QString id(WebProfile *profile); + static WebProfile *profile(const QString &id); + static const QMap& profileList(); + +private: + static QMap profiles; +}; + +#endif // SMOLBOTE_PROFILEMANAGER_H diff --git a/src/session.cpp b/src/session.cpp index 8e4f9a8..281e657 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -16,7 +16,7 @@ Session::Session(QObject *parent) : QObject(parent) { } -QJsonDocument Session::toJson(MainWindow *window) +QJsonObject Session::toJsonObject(MainWindow *window) { QJsonObject session; @@ -26,5 +26,5 @@ QJsonDocument Session::toJson(MainWindow *window) } session.insert("subwindows", subwindows); - return QJsonDocument(session); + return session; } diff --git a/src/session.h b/src/session.h index bb4f842..2926468 100644 --- a/src/session.h +++ b/src/session.h @@ -19,7 +19,7 @@ class Session : public QObject public: explicit Session(QObject *parent = nullptr); - static QJsonDocument toJson(MainWindow *window); + static QJsonObject toJsonObject(MainWindow *window); signals: diff --git a/src/webengine/widgets/pagemenu.cpp b/src/webengine/widgets/pagemenu.cpp index efd176b..deff511 100644 --- a/src/webengine/widgets/pagemenu.cpp +++ b/src/webengine/widgets/pagemenu.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "profilemanager.h" PageMenu::PageMenu(WebView *parent) : QMenu(tr("Page"), parent) @@ -108,12 +109,13 @@ void PageMenu::updateLoadProfileMenu() auto *browser = qobject_cast(qApp); Q_CHECK_PTR(browser); - for(const QString &name : browser->profiles()) { - auto *profile = browser->profile(name); - Q_CHECK_PTR(profile); + ProfileIterator it(ProfileManager::profileList()); + while(it.hasNext()) { + it.next(); + auto *profile =it.value(); + auto *loadAction = loadProfile_menu->addAction(profile->name()); - auto *profileAction = loadProfile_menu->addAction(profile->name()); - connect(profileAction, &QAction::triggered, this, [this, profile]() { + connect(loadAction, &QAction::triggered, this, [=]() { view->setProfile(profile); }); } -- cgit v1.2.1