aboutsummaryrefslogtreecommitdiff
path: root/src/settings.cpp
blob: 6a997288d38c15e29601e8c1bb2f0902a4f9c7b4 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **
 ******************************************************************************/

#include "settings.h"
#include <QStandardPaths>
#include <fstream>
#include <cstdio>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFileSystemWatcher>

Settings::Settings(const QString &configFile, const QString &defaultsFile)
{
    m_configurationPath = configFile;
    m_defaultsPath = defaultsFile;

    // homeLocation is the user's home folder
    homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);

    // settingsLocation is the location of the configFile
    if(QFile::exists(configFile)) {
        settingsLocation = QFileInfo(configFile).dir().absolutePath();
    } else {
        // if file doesn't exist, use the generic location
        settingsLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    }

    // cacheLocation
    cacheLocation = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);

    userValues = parse(m_configurationPath);
    defaultValues = parse(m_defaultsPath);

    m_watcher = new QFileSystemWatcher();
    m_watcher->addPath(configFile);
    QObject::connect(m_watcher, &QFileSystemWatcher::fileChanged, [this](const QString &path) {
        if(path == m_configurationPath) {
#ifdef QT_DEBUG
            qDebug("Reloading user configuration");
#endif
            userValues = parse(m_configurationPath);
        }
    });

#ifdef QT_DEBUG
    qDebug(">> Configuration");
    qDebug("- userconf: [%s]", qUtf8Printable(m_configurationPath));
    qDebug("- defaults: [%s]", qUtf8Printable(m_defaultsPath));
    qDebug("- $home     [%s]", qUtf8Printable(homeLocation));
    qDebug("- $settings [%s]", qUtf8Printable(settingsLocation));
    qDebug("- $cache    [%s]", qUtf8Printable(cacheLocation));
#endif
}

Settings::~Settings()
{
    m_watcher->deleteLater();
}

QString Settings::configurationPath() const
{
    return m_configurationPath;
}

QString Settings::defaultsPath() const
{
    return m_defaultsPath;
}

bool Settings::isEmpty() const
{
    return userValues.empty();
}

QVariant Settings::value(const QString &key) const
{
    const toml::Value *cValue = userValues.find(key.toStdString());
    const toml::Value *dValue = defaultValues.find(key.toStdString());

    QVariant r;

    if(userValues.has(key.toStdString())) {
        r = valueToVariant(cValue);
    } else {
        if(defaultValues.has(key.toStdString())) {
            r = valueToVariant(dValue);
        }
    }

    // check if key is a path, in which case replace '~' with the home location
    if(key.endsWith(QLatin1String("path"), Qt::CaseInsensitive)) {
        QString value = r.toString();
        while(value.contains('$')) {
            value.replace("$settings", settingsLocation);
            value.replace("$cache", cacheLocation);
            value.replace("$home", homeLocation);
        }
        r = QVariant(value);
    }

    return r;
}

toml::Value Settings::parse(const QString &filename)
{
    toml::Value r;

    if(filename.isEmpty()) {
        qWarning("Empty configuration path.");

    } else {
        QFile file(filename);
        if(file.open(QIODevice::ReadOnly)) {
            std::stringstream d(file.readAll().toStdString());
            file.close();

            toml::ParseResult result = toml::parse(d);
            if(!result.valid()) {
                qWarning("Invalid configuration: %s", result.errorReason.c_str());
            }
            r = result.value;

        } else {
            qWarning("Cannot open configuration: %s", qUtf8Printable(filename));
        }
    }

    return r;
}

QVariant Settings::fromList(const toml::Value *list) const
{
    QStringList l;

    for(const toml::Value &v : list->as<toml::Array>()) {
        // TODO check value type
        l.append(QString::fromStdString(v.as<std::string>()));
    }

    return QVariant(l);
}

QVariant Settings::valueToVariant(const toml::Value *value) const
{
    QVariant r;
    switch (value->type()) {
    case toml::Value::NULL_TYPE:
        break;

    case toml::Value::BOOL_TYPE:
        r = QVariant(value->as<bool>());
        break;

    case toml::Value::INT_TYPE:
        r = QVariant(value->as<int>());
        break;

    case toml::Value::DOUBLE_TYPE:
        r = QVariant(value->as<double>());
        break;

    case toml::Value::STRING_TYPE:
        r = QVariant(QString::fromStdString(value->as<std::string>()));
        break;

    case toml::Value::ARRAY_TYPE:
        r = fromList(value);
        break;

    default:
        qWarning("Unhandled type in configuration");
        break;
    }

    return r;
}