aboutsummaryrefslogtreecommitdiff
path: root/qmidiplayer-desktop/qmpsettings.cpp
blob: 6ce542ad89cc8525143f4b9e9f120dff2d394187 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "qmpsettings.hpp"
#include <QFile>
#include <QStandardPaths>

#define QMP_CONFIGURATION_FILE_REV 2

QSettings *qmpSettings::settings = nullptr;
qmpSettings::qmpSettings()
{
    qRegisterMetaTypeStreamOperators<QPair<QString, QString>>();
    QString confpath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::ConfigLocation) + QString("/qmprc");
    settings = new QSettings(confpath, QSettings::IniFormat);
    if (settings->value("ConfigurationFileRevision").toInt() != QMP_CONFIGURATION_FILE_REV &&
        QFile::exists(confpath))
    {
        qWarning("Your current configuration file is not compatible with this version of QMidiPlayer. "
            "QMidiPlayer will start with its default configuration. A backup of the old configuration "
            "is automatically saved as qmprc.old.");
        QFile::remove(confpath + ".old");
        QFile::copy(confpath, confpath + ".old");
        settings->clear();
        settings->setValue("ConfigurationFileRevision", QMP_CONFIGURATION_FILE_REV);
    }
}

qmpSettings::~qmpSettings()
{
    delete settings;
    settings = nullptr;
}

void qmpSettings::registerOptionInt(std::string tab, std::string desc, std::string key, int min, int max, int defaultval)
{
    optionlist.push_back(key);
    options[key] = qmpOption(tab, desc, qmpOption::ParameterType::parameter_int, nullptr, defaultval, min, max);
}
int qmpSettings::getOptionInt(std::string key)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_int)
        return settings->value(QString(key.c_str()), options[key].defaultval).toInt();
    qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    return options[key].defaultval.toInt();
}
void qmpSettings::setOptionInt(std::string key, int val)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_int)
        settings->setValue(QString(key.c_str()), val);
    else
        qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    //call qmpSettingsWindow::load(key)?
}

void qmpSettings::registerOptionUint(std::string tab, std::string desc, std::string key, unsigned min, unsigned max, unsigned defaultval)
{
    optionlist.push_back(key);
    options[key] = qmpOption(tab, desc, qmpOption::ParameterType::parameter_uint, nullptr, defaultval, min, max);
}
unsigned qmpSettings::getOptionUint(std::string key)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_uint)
        return settings->value(QString(key.c_str()), options[key].defaultval).toUInt();
    qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    return options[key].defaultval.toUInt();
}
void qmpSettings::setOptionUint(std::string key, unsigned val)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_uint)
        settings->setValue(QString(key.c_str()), val);
    else
        qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
}

void qmpSettings::registerOptionBool(std::string tab, std::string desc, std::string key, bool defaultval)
{
    optionlist.push_back(key);
    options[key] = qmpOption(tab, desc, qmpOption::ParameterType::parameter_bool, nullptr, defaultval);
}
bool qmpSettings::getOptionBool(std::string key)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_bool)
        return settings->value(QString(key.c_str()), options[key].defaultval).toBool();
    qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    return options[key].defaultval.toBool();
}
void qmpSettings::setOptionBool(std::string key, bool val)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_bool)
        settings->setValue(QString(key.c_str()), val);
    else
        qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
}

void qmpSettings::registerOptionDouble(std::string tab, std::string desc, std::string key, double min, double max, double defaultval)
{
    optionlist.push_back(key);
    options[key] = qmpOption(tab, desc, qmpOption::ParameterType::parameter_double, nullptr, defaultval, min, max);
}
double qmpSettings::getOptionDouble(std::string key)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_double)
        return settings->value(QString(key.c_str()), options[key].defaultval).toDouble();
    qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    return options[key].defaultval.toDouble();
}
void qmpSettings::setOptionDouble(std::string key, double val)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_double)
        settings->setValue(QString(key.c_str()), val);
    else
        qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
}

void qmpSettings::registerOptionString(std::string tab, std::string desc, std::string key, std::string defaultval, bool is_url)
{
    optionlist.push_back(key);
    options[key] = qmpOption(tab, desc,
            is_url ? qmpOption::ParameterType::parameter_url : qmpOption::ParameterType::parameter_str,
            nullptr, QString(defaultval.c_str()));
}
std::string qmpSettings::getOptionString(std::string key)
{
    if (options.find(key) != options.end() &&
        (options[key].type == qmpOption::ParameterType::parameter_str || options[key].type == qmpOption::ParameterType::parameter_url))
        return settings->value(QString(key.c_str()), options[key].defaultval).toString().toStdString();
    qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    return options[key].defaultval.toString().toStdString();
}
void qmpSettings::setOptionString(std::string key, std::string val)
{
    if (options.find(key) != options.end() &&
        (options[key].type == qmpOption::ParameterType::parameter_str || options[key].type == qmpOption::ParameterType::parameter_url))
        settings->setValue(QString(key.c_str()), QString(val.c_str()));
    else
        qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
}

void qmpSettings::registerOptionEnumInt(std::string tab, std::string desc, std::string key, std::vector<std::string> enumlist, int defaultval)
{
    optionlist.push_back(key);
    options[key] = qmpOption(tab, desc, qmpOption::ParameterType::parameter_enum, nullptr, defaultval);
    options[key].enumlist = enumlist;
}
int qmpSettings::getOptionEnumInt(std::string key)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_enum)
    {
        std::string curitm = settings->value(QString(key.c_str()), options[key].defaultval).toString().toStdString();
        auto curidx = std::find(options[key].enumlist.begin(), options[key].enumlist.end(), curitm);
        if (curidx != options[key].enumlist.end())
            return static_cast<int>(curidx - options[key].enumlist.begin());
        else
        {
            qWarning("Invalid value set for option \"%s\".", key.c_str());
            return options[key].defaultval.toInt();
        }
    }
    qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    return options[key].defaultval.toInt();
}

std::string qmpSettings::getOptionEnumIntOptName(std::string key)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_enum)
    {
        std::string curitm = settings->value(QString(key.c_str()), options[key].defaultval).toString().toStdString();
        auto curidx = std::find(options[key].enumlist.begin(), options[key].enumlist.end(), curitm);
        if (curidx != options[key].enumlist.end())
            return curitm;
        else
        {
            qWarning("Invalid value set for option \"%s\".", key.c_str());
            return options[key].enumlist[static_cast<size_t>(options[key].defaultval.toInt())];
        }
    }
    qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    return options[key].enumlist[static_cast<size_t>(options[key].defaultval.toInt())];
}
void qmpSettings::setOptionEnumInt(std::string key, int val)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_enum)
    {
        if (static_cast<size_t>(val) < options[key].enumlist.size())
            settings->setValue(QString(key.c_str()), QString(options[key].enumlist[static_cast<size_t>(val)].c_str()));
        else
            qWarning("Trying to set invalid value for option \"%s\".", key.c_str());
    }
    else
        qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
}

void qmpSettings::setOptionEnumIntOptName(std::string key, std::string valname)
{
    if (options.find(key) != options.end() && options[key].type == qmpOption::ParameterType::parameter_enum)
    {
        auto curidx = std::find(options[key].enumlist.begin(), options[key].enumlist.end(), valname);
        if (curidx != options[key].enumlist.end())
            settings->setValue(QString(key.c_str()), QString(valname.c_str()));
        else
            qWarning("Trying to set invalid value for option \"%s\".", key.c_str());
    }
    else
        qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
}

void qmpSettings::registerOptionCustom(std::string tab, std::string desc, std::string key, void *widget, void *defaultval, std::function<void *()> save_func, std::function<void(void *)> load_func)
{
    optionlist.push_back(key);
    options[key] = qmpOption(tab, desc, qmpOption::parameter_custom,
            static_cast<QWidget *>(widget),
            *static_cast<QVariant *>(defaultval),
            QVariant(), QVariant(), save_func, load_func);
}
void *qmpSettings::getOptionCustom(std::string key)
{
    if (options.find(key) != options.end() || options[key].type != qmpOption::ParameterType::parameter_custom)
        return static_cast<void *>(new QVariant(settings->value(QString(key.c_str()), options[key].defaultval)));
    qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
    return nullptr;
}
void qmpSettings::setOptionCustom(std::string key, void *val)
{
    if (options.find(key) != options.end() || options[key].type != qmpOption::ParameterType::parameter_custom)
        settings->setValue(QString(key.c_str()), *static_cast<QVariant *>(val));
    else
        qWarning("Unregistered option or mismatching option type: %s.", key.c_str());
}

void qmpSettings::setOptionRaw(QString key, QVariant val)
{
    settings->setValue(key, val);
}

QVariant qmpSettings::getOptionRaw(QString key, QVariant defval)
{
    return settings->value(key, defval);
}

QDataStream &operator<<(QDataStream &out, const QPair<QString, QString> &o)
{
    out << o.first << o.second;
    return out;
}

QDataStream &operator>>(QDataStream &in, QPair<QString, QString> &o)
{
    in >> o.first >> o.second;
    return in;
}