aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webprofile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/webengine/webprofile.cpp')
-rw-r--r--src/webengine/webprofile.cpp198
1 files changed, 86 insertions, 112 deletions
diff --git a/src/webengine/webprofile.cpp b/src/webengine/webprofile.cpp
index 5224189..f0368c9 100644
--- a/src/webengine/webprofile.cpp
+++ b/src/webengine/webprofile.cpp
@@ -11,6 +11,7 @@
#include <QSettings>
#include <QWebEngineCookieStore>
#include <QWebEngineSettings>
+#include "urlinterceptor.h"
static WebProfile *s_profile = nullptr;
@@ -25,11 +26,89 @@ WebProfile *WebProfile::defaultProfile()
return s_profile;
}
-WebProfile::WebProfile(const QString &name, QObject *parent)
- : QWebEngineProfile(parent)
+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)
{
- m_name = name;
+ 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<QWebEngineSettings::WebAttribute>(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);
});
@@ -38,11 +117,12 @@ WebProfile::WebProfile(const QString &name, QObject *parent)
});
}
-WebProfile::WebProfile(const QString &storageName, const QString &name, QObject *parent)
+// default constructor
+WebProfile::WebProfile(const QString &id, const QString &storageName, QObject *parent)
: QWebEngineProfile(storageName, parent)
+ , m_id(id)
{
- m_name = name;
-
+ QWebEngineProfile::setUrlRequestInterceptor(new UrlRequestInterceptor(this));
connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) {
m_cookies.append(cookie);
});
@@ -50,109 +130,3 @@ WebProfile::WebProfile(const QString &storageName, const QString &name, QObject
m_cookies.removeAll(cookie);
});
}
-
-const QString WebProfile::name() const
-{
- return m_name;
-}
-
-void WebProfile::setName(const QString &name)
-{
- m_name = name;
- emit nameChanged(name);
-}
-
-QString WebProfile::search() const
-{
- return m_search;
-}
-
-void WebProfile::setSearch(const QString &url)
-{
- m_search = url;
- emit searchChanged(m_search);
-}
-
-QUrl WebProfile::homepage() const
-{
- return m_homepage;
-}
-
-void WebProfile::setHomepage(const QUrl &url)
-{
- m_homepage = url;
- emit homepageChanged(m_homepage);
-}
-
-QUrl WebProfile::newtab() const
-{
- return m_newtab;
-}
-
-void WebProfile::setNewtab(const QUrl &url)
-{
- m_newtab = url;
- emit newtabChanged(m_newtab);
-}
-
-void WebProfile::setCachePath(const QString &path)
-{
- QWebEngineProfile::setCachePath(path);
- emit propertyChanged("cachePath", path);
-}
-
-void WebProfile::setPersistentStoragePath(const QString &path)
-{
- QWebEngineProfile::setPersistentStoragePath(path);
- emit propertyChanged("persistentStoragePath", path);
-}
-
-void WebProfile::setPersistentCookiesPolicy(int policy)
-{
- QWebEngineProfile::setPersistentCookiesPolicy(static_cast<QWebEngineProfile::PersistentCookiesPolicy>(policy));
- emit propertyChanged("persistentCookiesPolicy", policy);
-}
-
-void WebProfile::setHttpAcceptLanguage(const QString &httpAcceptLanguage)
-{
- QWebEngineProfile::setHttpAcceptLanguage(httpAcceptLanguage);
- emit propertyChanged("httpAcceptLanguage", httpAcceptLanguage);
-}
-
-void WebProfile::setHttpCacheMaximumSize(int maxSize)
-{
- QWebEngineProfile::setHttpCacheMaximumSize(maxSize);
- emit propertyChanged("httpCacheMaximumSize", maxSize);
-}
-
-void WebProfile::setHttpCacheType(int type)
-{
- QWebEngineProfile::setHttpCacheType(static_cast<QWebEngineProfile::HttpCacheType>(type));
- emit propertyChanged("httpCacheType", type);
-}
-
-void WebProfile::setHttpUserAgent(const QString &userAgent)
-{
- QWebEngineProfile::setHttpUserAgent(userAgent);
- emit propertyChanged("httpUserAgent", userAgent);
-}
-
-void WebProfile::setHttpHeader(const QString &name, const QString &value)
-{
- m_headers[name.toLatin1()] = value.toLatin1();
- emit headerChanged(name, value);
-}
-
-void WebProfile::removeHttpHeader(const QString &name)
-{
- if(m_headers.contains(name.toLatin1())) {
- m_headers.remove(name.toLatin1());
- emit headerRemoved(name);
- }
-}
-
-void WebProfile::setSpellCheckEnabled(bool enable)
-{
- QWebEngineProfile::setSpellCheckEnabled(enable);
- emit propertyChanged("spellCheckEnabed", enable);
-}