/* * 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 "webprofile.h" #include #include #include #include #include "urlinterceptor.h" static WebProfile *s_profile = nullptr; void WebProfile::setDefaultProfile(WebProfile *profile) { Q_CHECK_PTR(profile); s_profile = profile; } WebProfile *WebProfile::defaultProfile() { Q_CHECK_PTR(s_profile); return s_profile; } QSettings *WebProfile::load(const QString &path, const QString &search, const QUrl &homepage, const QUrl &newtab) { auto *settings = new QSettings(path, QSettings::IniFormat); if(!settings->contains("search")) { settings->setValue("search", search); } if(!settings->contains("homepage")) { settings->setValue("homepage", homepage); } if(!settings->contains("newtab")) { settings->setValue("newtab", newtab); } return settings; } WebProfile *WebProfile::load(const QString &id, QSettings *settings, bool isOffTheRecord) { WebProfile *profile = nullptr; if(isOffTheRecord || settings->value("otr", isOffTheRecord).toBool()) { profile = new WebProfile(id, nullptr); } else { profile = new WebProfile(id, id, nullptr); } profile->m_name = settings->value("name", id).toString(); connect(profile, &WebProfile::nameChanged, profile, [settings](const QString &name) { settings->setValue("name", name); }); profile->m_search = settings->value("search", "").toString(); connect(profile, &WebProfile::searchChanged, settings, [settings](const QString &url) { settings->setValue("search", url); }); profile->m_homepage = settings->value("homepage", "").toUrl(); connect(profile, &WebProfile::homepageChanged, settings, [settings](const QUrl &url) { settings->setValue("homepage", url); }); profile->m_newtab = settings->value("newtab", "").toUrl(); connect(profile, &WebProfile::newtabChanged, settings, [settings](const QUrl &url) { settings->setValue("newtab", url); }); { settings->beginGroup("properties"); const auto keys = settings->childKeys(); for(const QString &key : keys) { profile->setProperty(qUtf8Printable(key), settings->value(key)); } settings->endGroup(); // properties connect(profile, &WebProfile::propertyChanged, [settings](const QString &property, const QVariant &value) { settings->setValue("properties/" + property, value); }); } { settings->beginGroup("attributes"); const auto keys = settings->childKeys(); auto *s = profile->settings(); for(const QString &key : keys) { auto attribute = static_cast(key.toInt()); s->setAttribute(attribute, settings->value(key).toBool()); } settings->endGroup(); connect(profile, &WebProfile::attributeChanged, [settings](const QWebEngineSettings::WebAttribute attr, const bool value) { settings->setValue("attributes/" + QString::number(attr), value); }); } { // headers settings->beginGroup("headers"); for(const QString &key : settings->childKeys()) { profile->setHttpHeader(key.toLatin1(), settings->value(key).toString().toLatin1()); } settings->endGroup(); connect(profile, &WebProfile::headerChanged, [settings](const QString &name, const QString &value) { settings->setValue("headers/" + name, value); }); connect(profile, &WebProfile::headerRemoved, [settings](const QString &name) { settings->remove("headers/" + name); }); } return profile; } // off-the-record constructor WebProfile::WebProfile(const QString &id, QObject *parent) : QWebEngineProfile(parent) , m_id(id) { QWebEngineProfile::setUrlRequestInterceptor(new UrlRequestInterceptor(this)); connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) { m_cookies.append(cookie); }); connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) { m_cookies.removeAll(cookie); }); } // default constructor WebProfile::WebProfile(const QString &id, const QString &storageName, QObject *parent) : QWebEngineProfile(storageName, parent) , m_id(id) { QWebEngineProfile::setUrlRequestInterceptor(new UrlRequestInterceptor(this)); connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) { m_cookies.append(cookie); }); connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) { m_cookies.removeAll(cookie); }); }