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/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);
connect(ptr->profile, &WebProfile::nameChanged, [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, [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, [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, [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, [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<QWebEngineSettings::WebAttribute>(key.toInt());
settings->setAttribute(attribute, ptr->settings.value(key).toBool());
}
}
ptr->settings.endGroup();
connect(ptr->profile, &WebProfile::attributeChanged, [this, id](const QWebEngineSettings::WebAttribute attr, const bool value) {
this->m_profiles.at(id)->settings.setValue("attributes/" + QString::number(attr), value);
});
m_profiles[id] = std::move(ptr);
return m_profiles.at(id)->profile;
}
QMenu *ProfileManager::createProfileMenu(std::function<void(WebProfile *)> callback, QWidget *parent) const
{
QMenu *menu = new QMenu(parent);
for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) {
WebProfile *profile = it->second->profile;
QAction *action = menu->addAction(profile->name());
connect(action, &QAction::triggered, [profile, callback]() {
callback(profile);
});
}
return menu;
}
const QStringList ProfileManager::idList() const
{
QStringList ids;
for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) {
ids.append(it->first);
}
return ids;
}
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();
}
|