diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-09-07 13:11:58 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-09-07 13:11:58 +0200 |
commit | 4739f509d9d5ebaef71a51cece8f75b6a7e4b3dc (patch) | |
tree | e3f89c1db2aaaa605f0cbd4d752479a611fb5aac | |
parent | Some cppcheck fixes (diff) | |
download | smolbote-4739f509d9d5ebaef71a51cece8f75b6a7e4b3dc.tar.xz |
Move ProfileManager to libweb
-rw-r--r-- | lib/web/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/web/profilemanager.cpp | 116 | ||||
-rw-r--r-- | lib/web/profilemanager.h (renamed from src/profilemanager.h) | 34 | ||||
-rw-r--r-- | lib/web/webprofile.cpp | 6 | ||||
-rw-r--r-- | lib/web/webprofile.h | 9 | ||||
-rw-r--r-- | plugins/ProfileEditor/forms/profilemanagerdialog.cpp | 6 | ||||
-rw-r--r-- | plugins/ProfileEditor/forms/profilemanagerdialog.h | 3 | ||||
-rw-r--r-- | plugins/ProfileEditor/forms/profileview.cpp | 2 | ||||
-rw-r--r-- | plugins/ProfileEditor/profileeditorplugin.cpp | 2 | ||||
-rw-r--r-- | plugins/interfaces.h | 2 | ||||
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/browser.cpp | 5 | ||||
-rw-r--r-- | src/browser.h | 2 | ||||
-rw-r--r-- | src/profilemanager.cpp | 102 |
14 files changed, 161 insertions, 132 deletions
diff --git a/lib/web/CMakeLists.txt b/lib/web/CMakeLists.txt index ed969cd..b2d6c82 100644 --- a/lib/web/CMakeLists.txt +++ b/lib/web/CMakeLists.txt @@ -5,6 +5,8 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) add_library(web + profilemanager.cpp + profilemanager.h webprofile.cpp webprofile.h diff --git a/lib/web/profilemanager.cpp b/lib/web/profilemanager.cpp new file mode 100644 index 0000000..f3de3b5 --- /dev/null +++ b/lib/web/profilemanager.cpp @@ -0,0 +1,116 @@ +/* + * 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 "webprofile.h" +#include <QFileInfo> +#include <QWebEngineSettings> + +ProfileManager *ProfileManager::s_instance = nullptr; + +ProfileManager::ProfileManager(const QHash<QString, QString> &profileSection, QObject *parent) : QObject(parent) + , defaults(profileSection) +{ +} + +void ProfileManager::setInstance(ProfileManager *instance) +{ + Q_CHECK_PTR(instance); + s_instance = instance; +} + +ProfileManager *ProfileManager::instance() +{ + Q_CHECK_PTR(s_instance); + return s_instance; +} + +WebProfile *ProfileManager::loadProfile(const QString &path) +{ + std::unique_ptr<ProfileData> ptr = std::make_unique<ProfileData>(path); + const QString id = QFileInfo(path).baseName(); + + if(ptr->settings.value("otr", true).toBool()) { + ptr->profile = new WebProfile(ptr->settings.value("name", id).toString(), nullptr); + } else { + ptr->profile = new WebProfile(id, ptr->settings.value("name", id).toString(), nullptr); + } + Q_CHECK_PTR(ptr->profile); + + ptr->profile->setSearch(ptr->settings.value("search", defaults.value("profile.search")).toString()); + connect(ptr->profile, &WebProfile::searchChanged, &ptr->settings, [&](const QString &url) { + ptr->settings.setValue("search", url); + }); + + ptr->profile->setHomepage(ptr->settings.value("homepage", defaults.value("profile.homepage")).toUrl()); + connect(ptr->profile, &WebProfile::homepageChanged, &ptr->settings, [&](const QUrl &url) { + ptr->settings.setValue("homepage", url); + }); + + ptr->profile->setNewtab(ptr->settings.value("newtab", defaults.value("profile.newtab")).toUrl()); + connect(ptr->profile, &WebProfile::newtabChanged, &ptr->settings, [&](const QUrl &url) { + ptr->settings.setValue("newtab", url); + }); + + ptr->settings.beginGroup("properties"); + { + const auto keys = ptr->settings.childKeys(); + for(const QString &key : keys) { + ptr->profile->setProperty(qUtf8Printable(key), ptr->settings.value(key)); + } + } + ptr->settings.endGroup(); // properties + + ptr->settings.beginGroup("attributes"); + { + const auto keys = ptr->settings.childKeys(); + auto *settings = ptr->profile->settings(); + for(const QString &key : keys) { + auto attribute = static_cast<QWebEngineSettings::WebAttribute>(key.toInt()); + settings->setAttribute(attribute, ptr->settings.value(key).toBool()); + } + } + ptr->settings.endGroup(); + + m_profiles[id] = std::move(ptr); + return m_profiles.at(id)->profile; +} + +const QString ProfileManager::id(WebProfile *profile) const +{ + for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) { + if(it->second->profile == profile) + return it->first; + } + return QString(); +} + +WebProfile *ProfileManager::profile(const QString &id) const +{ + if(m_profiles.count(id) > 0) { + return m_profiles.at(id)->profile; + } + return nullptr; +} + +const QString ProfileManager::configurationPath(const QString &id) const +{ + if(m_profiles.count(id) > 0) { + return m_profiles.at(id)->path; + } + return QString(); +} + +const QMap<QString, WebProfile *> ProfileManager::profileList() const +{ + QMap<QString, WebProfile *> profiles; + for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) { + profiles.insert(it->first, it->second->profile); + } + return profiles; +} diff --git a/src/profilemanager.h b/lib/web/profilemanager.h index 5ece510..f58fdb5 100644 --- a/src/profilemanager.h +++ b/lib/web/profilemanager.h @@ -12,6 +12,9 @@ #include <QObject> #include <QMap> #include <webprofile.h> +#include <QSettings> +#include <map> +#include <memory> typedef QMapIterator<QString, WebProfile *> ProfileIterator; @@ -22,13 +25,6 @@ class ProfileManager : public QObject { Q_OBJECT public: - - struct Profile - { - QString id; - WebProfile *profile = nullptr; - }; - explicit ProfileManager(const QHash<QString, QString> &profileSection, QObject *parent = nullptr); static void setInstance(ProfileManager *instance); @@ -36,12 +32,28 @@ public: WebProfile *loadProfile(const QString &path); - const QString id(WebProfile *profile); - WebProfile *profile(const QString &id); - const QMap<QString, WebProfile *>& profileList(); + const QString id(WebProfile *profile) const; + WebProfile *profile(const QString &id) const; + const QString configurationPath(const QString &id) const; + const QMap<QString, WebProfile *> profileList() const; private: - QMap<QString, WebProfile *> profiles; + struct ProfileData + { + ProfileData(const QString &path = QString()) : settings(path, QSettings::IniFormat) { + this->path = path; + } + + ~ProfileData() { + this->settings.sync(); + } + + WebProfile *profile = nullptr; + QSettings settings; + QString path; + }; + + std::map<QString, std::unique_ptr<ProfileData>> m_profiles; static ProfileManager *s_instance; const QHash<QString, QString> defaults; diff --git a/lib/web/webprofile.cpp b/lib/web/webprofile.cpp index 9b16a61..d42bb26 100644 --- a/lib/web/webprofile.cpp +++ b/lib/web/webprofile.cpp @@ -14,11 +14,10 @@ WebProfile *WebProfile::profile = nullptr; -WebProfile::WebProfile(const QString &name, const QString &configPath, QObject *parent) +WebProfile::WebProfile(const QString &name, QObject *parent) : QWebEngineProfile(parent) { m_name = name; - m_configPath = configPath; #ifdef QT_DEBUG qDebug("Creating otr profile %s", qUtf8Printable(m_name)); @@ -32,11 +31,10 @@ WebProfile::WebProfile(const QString &name, const QString &configPath, QObject * }); } -WebProfile::WebProfile(const QString &storageName, const QString &name, const QString &configPath, QObject *parent) +WebProfile::WebProfile(const QString &storageName, const QString &name, QObject *parent) : QWebEngineProfile(storageName, parent) { m_name = name; - m_configPath = configPath; #ifdef QT_DEBUG qDebug("Creating profile %s", qUtf8Printable(m_name)); diff --git a/lib/web/webprofile.h b/lib/web/webprofile.h index 5107b8f..260ff36 100644 --- a/lib/web/webprofile.h +++ b/lib/web/webprofile.h @@ -37,9 +37,9 @@ class WebProfile : public QWebEngineProfile public: // off-the-record constructor - explicit WebProfile(const QString &name, const QString &configPath, QObject *parent = nullptr); + explicit WebProfile(const QString &name, QObject *parent = nullptr); // default constructor - explicit WebProfile(const QString &storageName, const QString &name, const QString &configPath, QObject *parent = nullptr); + explicit WebProfile(const QString &storageName, const QString &name, QObject *parent = nullptr); ~WebProfile() = default; @@ -58,10 +58,6 @@ public: { return m_name; } - QString configurationPath() const - { - return m_configPath; - } const QVector<QNetworkCookie> cookies() const { @@ -106,7 +102,6 @@ signals: private: static WebProfile *profile; - QString m_configPath; QString m_name; QString m_search = QString("about:blank"); QUrl m_homepage = QUrl("about:blank"); diff --git a/plugins/ProfileEditor/forms/profilemanagerdialog.cpp b/plugins/ProfileEditor/forms/profilemanagerdialog.cpp index dac4a61..7920f90 100644 --- a/plugins/ProfileEditor/forms/profilemanagerdialog.cpp +++ b/plugins/ProfileEditor/forms/profilemanagerdialog.cpp @@ -6,7 +6,7 @@ #include <QPointer> #include "newprofiledialog.h" -ProfileManagerDialog::ProfileManagerDialog(const QVector<WebProfile *> &profiles, QWidget *parent) +ProfileManagerDialog::ProfileManagerDialog(const ProfileManager *profiles, QWidget *parent) : QDialog(parent) , ui(new Ui::ProfileManagerDialog) { @@ -27,7 +27,7 @@ ProfileManagerDialog::ProfileManagerDialog(const QVector<WebProfile *> &profiles deleteProfile(ui->listWidget->currentItem()); }); - for(auto *profile : profiles) { + for(auto *profile : profiles->profileList().values()) { addProfile(profile); } } @@ -85,7 +85,7 @@ void ProfileManagerDialog::deleteProfile(QListWidgetItem *item) Q_ASSERT(!profile.isNull()); qDebug("deleting profile %s", qUtf8Printable(profile->name())); - qDebug("deleting %s: %s", qUtf8Printable(profile->configurationPath()), QFile(profile->configurationPath()).remove() ? "okay" : "failed"); + //qDebug("deleting %s: %s", qUtf8Printable(profile->configurationPath()), QFile(profile->configurationPath()).remove() ? "okay" : "failed"); qDebug("deleting %s: %s", qUtf8Printable(profile->persistentStoragePath()), QDir(profile->persistentStoragePath()).removeRecursively() ? "okay" : "failed"); qDebug("deleting %s: %s", qUtf8Printable(profile->cachePath()), QDir(profile->cachePath()).removeRecursively() ? "okay" : "failed"); diff --git a/plugins/ProfileEditor/forms/profilemanagerdialog.h b/plugins/ProfileEditor/forms/profilemanagerdialog.h index 4f468c5..ce38777 100644 --- a/plugins/ProfileEditor/forms/profilemanagerdialog.h +++ b/plugins/ProfileEditor/forms/profilemanagerdialog.h @@ -3,6 +3,7 @@ #include <QDialog> #include <QVector> +#include <profilemanager.h> namespace Ui { @@ -16,7 +17,7 @@ class ProfileManagerDialog : public QDialog Q_OBJECT public: - explicit ProfileManagerDialog(const QVector<WebProfile *> &profiles, QWidget *parent = 0); + explicit ProfileManagerDialog(const ProfileManager *profiles, QWidget *parent = 0); ~ProfileManagerDialog(); signals: diff --git a/plugins/ProfileEditor/forms/profileview.cpp b/plugins/ProfileEditor/forms/profileview.cpp index 85dde12..e265e16 100644 --- a/plugins/ProfileEditor/forms/profileview.cpp +++ b/plugins/ProfileEditor/forms/profileview.cpp @@ -9,6 +9,7 @@ #include "profileview.h" #include "ui_profileview.h" #include <webprofile.h> +#include <profilemanager.h> #include <QWebEngineSettings> #include <QWebEngineCookieStore> #include <QDateTime> @@ -33,7 +34,6 @@ ProfileView::ProfileView(WebProfile *profile, QWidget *parent) // general tab ui->name->setText(profile->name()); ui->offTheRecord->setChecked(profile->isOffTheRecord()); - ui->configurationPath->setText(profile->configurationPath()); // http tab ui->userAgent->setPlainText(m_profile->httpUserAgent()); diff --git a/plugins/ProfileEditor/profileeditorplugin.cpp b/plugins/ProfileEditor/profileeditorplugin.cpp index 32b0b8d..b158d6b 100644 --- a/plugins/ProfileEditor/profileeditorplugin.cpp +++ b/plugins/ProfileEditor/profileeditorplugin.cpp @@ -26,7 +26,7 @@ QHash<QString, std::function<int()>> ProfileEditorPlugin::commands() QDialog *ProfileEditorPlugin::createWidget(QWidget *parent) { Q_CHECK_PTR(browser); - auto *widget = new ProfileManagerDialog(browser->profiles(), parent); + auto *widget = new ProfileManagerDialog(browser->getProfileManager(), parent); widget->setAttribute(Qt::WA_DeleteOnClose, true); connect(widget, &ProfileManagerDialog::createProfile, this, [=](const QString &id) { diff --git a/plugins/interfaces.h b/plugins/interfaces.h index fe25ed3..7ee10db 100644 --- a/plugins/interfaces.h +++ b/plugins/interfaces.h @@ -20,12 +20,14 @@ class QDialog; class WebProfile; class Configuration; +class ProfileManager; class BrowserInterface { public: virtual Configuration *getConfiguration() const = 0; virtual QPair<QString, WebProfile *> loadProfile(const QString &id) = 0; + virtual const ProfileManager *getProfileManager() const = 0; virtual const QVector<WebProfile *> profiles() const = 0; }; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4549155..5db6e36 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,8 +13,6 @@ set(poi_SRC 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 0af050f..1e2ad25 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -73,6 +73,11 @@ Configuration *Browser::getConfiguration() const return m_config.get(); } +const ProfileManager *Browser::getProfileManager() const +{ + return const_cast<ProfileManager *>(ProfileManager::instance()); +} + void Browser::registerPlugin(const Plugin &plugin) { Q_ASSERT(m_config); diff --git a/src/browser.h b/src/browser.h index ff84c32..6969087 100644 --- a/src/browser.h +++ b/src/browser.h @@ -38,6 +38,8 @@ public slots: public: // interface Configuration *getConfiguration() const override; + + const ProfileManager *getProfileManager() const override; const QVector<WebProfile *> profiles() const override; QPair<QString, WebProfile *> loadProfile(const QString &id) override; diff --git a/src/profilemanager.cpp b/src/profilemanager.cpp deleted file mode 100644 index 5b0f76e..0000000 --- a/src/profilemanager.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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 <web/webprofile.h> -#include <QSettings> -#include <QFileInfo> -#include <QWebEngineSettings> - -ProfileManager *ProfileManager::s_instance = nullptr; - -ProfileManager::ProfileManager(const QHash<QString, QString> &profileSection, QObject *parent) : QObject(parent) - , defaults(profileSection) -{ -} - -void ProfileManager::setInstance(ProfileManager *instance) -{ - Q_CHECK_PTR(instance); - s_instance = instance; -} - -ProfileManager *ProfileManager::instance() -{ - Q_CHECK_PTR(s_instance); - return s_instance; -} - -WebProfile *ProfileManager::loadProfile(const QString &path) -{ - 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<QWebEngineSettings::WebAttribute>(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<QString, WebProfile *> &ProfileManager::profileList() -{ - return profiles; -} |