From 4739f509d9d5ebaef71a51cece8f75b6a7e4b3dc Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 7 Sep 2018 13:11:58 +0200 Subject: Move ProfileManager to libweb --- lib/web/CMakeLists.txt | 2 + lib/web/profilemanager.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++ lib/web/profilemanager.h | 62 ++++++++++++++++++++++++ lib/web/webprofile.cpp | 6 +-- lib/web/webprofile.h | 9 +--- 5 files changed, 184 insertions(+), 11 deletions(-) create mode 100644 lib/web/profilemanager.cpp create mode 100644 lib/web/profilemanager.h (limited to 'lib') 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 +#include + +ProfileManager *ProfileManager::s_instance = nullptr; + +ProfileManager::ProfileManager(const QHash &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 ptr = std::make_unique(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(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 ProfileManager::profileList() const +{ + QMap profiles; + for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) { + profiles.insert(it->first, it->second->profile); + } + return profiles; +} diff --git a/lib/web/profilemanager.h b/lib/web/profilemanager.h new file mode 100644 index 0000000..f58fdb5 --- /dev/null +++ b/lib/web/profilemanager.h @@ -0,0 +1,62 @@ +/* + * 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 +#include +#include +#include + +typedef QMapIterator ProfileIterator; + +#define profileManager ProfileManager::instance() + +class WebProfile; +class ProfileManager : public QObject +{ + Q_OBJECT +public: + explicit ProfileManager(const QHash &profileSection, QObject *parent = nullptr); + + static void setInstance(ProfileManager *instance); + static ProfileManager *instance(); + + WebProfile *loadProfile(const QString &path); + + const QString id(WebProfile *profile) const; + WebProfile *profile(const QString &id) const; + const QString configurationPath(const QString &id) const; + const QMap profileList() const; + +private: + 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> m_profiles; + + static ProfileManager *s_instance; + const QHash defaults; +}; + +#endif // SMOLBOTE_PROFILEMANAGER_H 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 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"); -- cgit v1.2.1