aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webprofile.cpp
blob: f1e71fb9e4e778f62815d8c5d9610a680f4ed408 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * 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 "urlinterceptor.h"
#include <QFileInfo>
#include <QSettings>
#include <QWebEngineCookieStore>
#include <QWebEngineSettings>
#include <spdlog/spdlog.h>

static WebProfile *s_profile = nullptr;

void WebProfile::setDefaultProfile(WebProfile *profile)
{
    s_profile = profile;
}
WebProfile *WebProfile::defaultProfile()
{
    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(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");
        const auto keys = settings->childKeys();
        for(const QString &key : keys) {
            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) {
        spdlog::debug("[{}]: +cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
        m_cookies.append(cookie);
    });
    connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) {
        spdlog::debug("[{}]: -cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
        m_cookies.removeOne(cookie);
    });
    cookieStore()->loadAllCookies();
}

// 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) {
        spdlog::debug("[{}]: +cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
        m_cookies.append(cookie);
    });
    connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) {
        spdlog::debug("[{}]: -cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
        m_cookies.removeOne(cookie);
    });
    cookieStore()->loadAllCookies();
}