summaryrefslogtreecommitdiffstats
path: root/common/configstore.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/configstore.cpp')
-rw-r--r--common/configstore.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/common/configstore.cpp b/common/configstore.cpp
new file mode 100644
index 0000000..a8469ba
--- /dev/null
+++ b/common/configstore.cpp
@@ -0,0 +1,96 @@
1/*
2 * Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19#include "configstore.h"
20
21#include <QSettings>
22#include <QSharedPointer>
23#include <QStandardPaths>
24#include <QFile>
25#include <log.h>
26
27static QSharedPointer<QSettings> getConfig(const QByteArray &identifier)
28{
29 return QSharedPointer<QSettings>::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/sink/" + identifier + ".ini", QSettings::IniFormat);
30}
31
32ConfigStore::ConfigStore(const QByteArray &identifier)
33 : mIdentifier(identifier),
34 mConfig(getConfig(identifier))
35{
36
37}
38
39QMap<QByteArray, QByteArray> ConfigStore::getEntries()
40{
41 QMap<QByteArray, QByteArray> resources;
42 for (const auto &identifier : mConfig->childGroups()) {
43 mConfig->beginGroup(identifier);
44 const auto type = mConfig->value("type").toByteArray();
45 resources.insert(identifier.toLatin1(), type);
46 mConfig->endGroup();
47 }
48 return resources;
49}
50
51void ConfigStore::add(const QByteArray &identifier, const QByteArray &type)
52{
53 Trace() << "Adding " << identifier;
54 mConfig->beginGroup(QString::fromLatin1(identifier));
55 mConfig->setValue("type", type);
56 mConfig->endGroup();
57 mConfig->sync();
58}
59
60void ConfigStore::remove(const QByteArray &identifier)
61{
62 Trace() << "Removing " << identifier;
63 mConfig->beginGroup(QString::fromLatin1(identifier));
64 mConfig->remove("");
65 mConfig->endGroup();
66 mConfig->sync();
67 QFile::remove(getConfig(identifier)->fileName());
68}
69
70void ConfigStore::clear()
71{
72 mConfig->clear();
73 mConfig->sync();
74}
75
76void ConfigStore::modify(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration)
77{
78 Trace() << "Modifying " << identifier;
79 auto config = getConfig(identifier);
80 config->clear();
81 for (const auto &key : configuration.keys()) {
82 config->setValue(key, configuration.value(key));
83 }
84 config->sync();
85}
86
87QMap<QByteArray, QVariant> ConfigStore::get(const QByteArray &identifier)
88{
89 QMap<QByteArray, QVariant> configuration;
90 auto config = getConfig(identifier);
91 for (const auto &key : config->allKeys()) {
92 configuration.insert(key.toLatin1(), config->value(key));
93 }
94 return configuration;
95}
96