aboutsummaryrefslogtreecommitdiff
path: root/lib/web/profilemanager.cpp
blob: 74ddc75f35250363d2e9faee0547ec6ca47a786b (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
/*
 * 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 <QFileInfo>
#include <QWebEngineSettings>

ProfileManager::ProfileManager(const QHash<QString, QString> &profileSection, QObject *parent)
    : QObject(parent)
    , defaults(profileSection)
{
}

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, 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<QWebEngineSettings::WebAttribute>(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);
    });

    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;
    }
}

QMenu *ProfileManager::createProfileMenu(std::function<void(WebProfile *)> 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;
}