aboutsummaryrefslogtreecommitdiff
path: root/src/profilemanager.cpp
blob: bb967b9bab1bd7634c6ecd5777372479c9ab3c80 (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
/*
 * 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 <web/webprofile.h>
#include <QSettings>
#include <QFileInfo>
#include <QWebEngineSettings>

QMap<QString, WebProfile *> ProfileManager::profiles;

ProfileManager::ProfileManager(QObject *parent) : QObject(parent)
{
}

WebProfile *ProfileManager::loadProfile(const QString &path, const QHash<QString, QString> &defaults)
{
    WebProfile *profile = nullptr;
#ifdef QT_DEBUG
    qDebug("==> Reading profile config : %s", qUtf8Printable(path));
#endif

    const QString id = QFileInfo(path).baseName();
    QSettings config(path, QSettings::IniFormat);

    if(config.value("otr", true).toBool()) {
        profile = new WebProfile(config.value("name", id).toString(), path, nullptr);
    } else {
        profile = new WebProfile(id, config.value("name", id).toString(), path, nullptr);
    }

    Q_CHECK_PTR(profile);
    profiles.insert(id, profile);

    profile->setSearch(config.value("search", defaults.value("profile.search")).toString());
    profile->setHomepage(config.value("homepage", defaults.value("profile.homepage")).toUrl());
    profile->setNewtab(config.value("newtab", defaults.value("profile.newtab")).toUrl());

    config.beginGroup("properties");
    {
        const auto keys = config.childKeys();
        for(const QString &key : keys) {
#ifdef QT_DEBUG
            qDebug("- set property %s to %s", qUtf8Printable(key), qUtf8Printable(config.value(key).toString()));
#endif
            profile->setProperty(qUtf8Printable(key), config.value(key));
        }
    }
    config.endGroup(); // properties

    config.beginGroup("attributes");
    {
        const auto keys = config.childKeys();
        auto *settings = profile->settings();
        for(const QString &key : keys) {
#ifdef QT_DEBUG
            qDebug("- set attribute %s to %s", qUtf8Printable(key), qUtf8Printable(config.value(key).toString()));
#endif
            auto attribute = static_cast<QWebEngineSettings::WebAttribute>(key.toInt());
            settings->setAttribute(attribute, config.value(key).toBool());
        }
    }
    config.endGroup();

    return profile;
}

const QString ProfileManager::id(WebProfile *profile)
{
    return profiles.key(profile);
}

WebProfile *ProfileManager::profile(const QString &id)
{
    if(profiles.contains(id))
        return profiles.value(id);
    else
        return WebProfile::defaultProfile();
}

const QMap<QString, WebProfile *> &ProfileManager::profileList()
{
    return profiles;
}