/* * 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/gitea/aqua/smolbote * * SPDX-License-Identifier: GPL-3.0 */ #include "profilemanager.h" #include "webprofile.h" #include #include ProfileManager::ProfileManager(const QHash &profileSection, QObject *parent) : QObject(parent) , defaults(profileSection) { } WebProfile *ProfileManager::createProfile(const QString& id, bool isOffTheRecord) { QDir profileDir(defaults.value("profile.path")); const QString path = profileDir.absoluteFilePath(id + ".profile"); { QSettings init(path, QSettings::IniFormat); init.setValue("name", id); init.setValue("otr", isOffTheRecord); init.sync(); } return loadProfile(path); } 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); connect(ptr->profile, &WebProfile::nameChanged, ptr->profile, [this, id](const QString &name) { this->m_profiles.at(id)->settings.setValue("name", name); }); ptr->profile->setSearch(ptr->settings.value("search", defaults.value("profile.search")).toString()); connect(ptr->profile, &WebProfile::searchChanged, ptr->profile, [this, id](const QString &url) { this->m_profiles.at(id)->settings.setValue("search", url); }); ptr->profile->setHomepage(ptr->settings.value("homepage", defaults.value("profile.homepage")).toUrl()); connect(ptr->profile, &WebProfile::homepageChanged, ptr->profile, [this, id](const QUrl &url) { this->m_profiles.at(id)->settings.setValue("homepage", url); }); ptr->profile->setNewtab(ptr->settings.value("newtab", defaults.value("profile.newtab")).toUrl()); connect(ptr->profile, &WebProfile::newtabChanged, ptr->profile, [this, id](const QUrl &url) { this->m_profiles.at(id)->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 connect(ptr->profile, &WebProfile::propertyChanged, ptr->profile, [this, id](const QString &property, const QVariant &value) { this->m_profiles.at(id)->settings.setValue("properties/" + property, value); }); 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(); connect(ptr->profile, &WebProfile::attributeChanged, ptr->profile, [this, id](const QWebEngineSettings::WebAttribute attr, const bool value) { this->m_profiles.at(id)->settings.setValue("attributes/" + QString::number(attr), value); }); // headers ptr->settings.beginGroup("headers"); for(const QString &key : ptr->settings.childKeys()) { ptr->profile->setHttpHeader(key.toLatin1(), ptr->settings.value(key).toString().toLatin1()); } ptr->settings.endGroup(); connect(ptr->profile, &WebProfile::headerChanged, ptr->profile, [this, id](const QString &name, const QString &value) { this->m_profiles.at(id)->settings.setValue("headers/" + name, value); }); connect(ptr->profile, &WebProfile::headerRemoved, ptr->profile, [this, id](const QString &name) { this->m_profiles.at(id)->settings.remove("headers/" + name); }); m_profiles[id] = std::move(ptr); return m_profiles.at(id)->profile; } void ProfileManager::deleteProfile(const QString &id) { if(m_profiles.count(id) > 0) { auto profileData = m_profiles.extract(id); profileData.mapped()->deleteSelf = true; } } void ProfileManager::profilePickerMenu(QMenu *menu, WebProfile *current, std::function callback) const { auto *profileGroup = new QActionGroup(menu); for(const auto &profileData : m_profiles) { WebProfile *profile = profileData.second->profile; auto *action = menu->addAction(profile->name(), profile, [profile, callback]() { callback(profile); }); action->setCheckable(true); profileGroup->addAction(action); if(profile == current) action->setChecked(true); } connect(menu, &QMenu::aboutToHide, profileGroup, &QActionGroup::deleteLater); } QMenu *ProfileManager::createProfileMenu(std::function callback, QWidget *parent) const { auto *menu = new QMenu(parent); for(const auto &m_profile : m_profiles) { WebProfile *profile = m_profile.second->profile; QAction *action = menu->addAction(profile->name()); connect(action, &QAction::triggered, profile, [profile, callback]() { callback(profile); }); } return menu; } const QStringList ProfileManager::idList() const { QStringList ids; for(const auto &m_profile : m_profiles) { ids.append(m_profile.first); } return ids; } const QString ProfileManager::id(const WebProfile *profile) const { for(const auto &m_profile : m_profiles) { if(m_profile.second->profile == profile) return m_profile.first; } return QString(); } WebProfile *ProfileManager::profile(const QString &id) const { if(m_profiles.count(id) > 0) { return m_profiles.at(id)->profile; } return nullptr; }