aboutsummaryrefslogtreecommitdiff
path: root/lib/web/profilemanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/web/profilemanager.cpp')
-rw-r--r--lib/web/profilemanager.cpp116
1 files changed, 116 insertions, 0 deletions
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;
+}