File indexing completed on 2024-05-12 05:25:58

0001 /*
0002  *   Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 #include "configstore.h"
0020 
0021 #include <QSettings>
0022 #include <QSharedPointer>
0023 #include <QFile>
0024 #include <log.h>
0025 #include <definitions.h>
0026 
0027 static QSharedPointer<QSettings> getConfig(const QByteArray &identifier)
0028 {
0029     return QSharedPointer<QSettings>::create(Sink::configLocation() + "/" + identifier + ".ini", QSettings::IniFormat);
0030 }
0031 
0032 ConfigStore::ConfigStore(const QByteArray &identifier, const QByteArray &typeName)
0033     : mIdentifier(identifier),
0034     mTypeName(typeName),
0035     mConfig(getConfig(identifier))
0036 {
0037 
0038 }
0039 
0040 QMap<QByteArray, QByteArray> ConfigStore::getEntries()
0041 {
0042     QMap<QByteArray, QByteArray> resources;
0043     for (const auto &identifier : mConfig->childGroups()) {
0044         mConfig->beginGroup(identifier);
0045         const auto type = mConfig->value(mTypeName).toByteArray();
0046         resources.insert(identifier.toLatin1(), type);
0047         mConfig->endGroup();
0048     }
0049     return resources;
0050 }
0051 
0052 void ConfigStore::add(const QByteArray &identifier, const QByteArray &type)
0053 {
0054     SinkTrace() << "Adding " << identifier;
0055     mConfig->beginGroup(QString::fromLatin1(identifier));
0056     mConfig->setValue(mTypeName, type);
0057     mConfig->endGroup();
0058     mConfig->sync();
0059 }
0060 
0061 void ConfigStore::remove(const QByteArray &identifier)
0062 {
0063     SinkTrace() << "Removing " << identifier;
0064     mConfig->beginGroup(QString::fromLatin1(identifier));
0065     mConfig->remove("");
0066     mConfig->endGroup();
0067     mConfig->sync();
0068     QFile::remove(getConfig(identifier)->fileName());
0069 }
0070 
0071 void ConfigStore::clear()
0072 {
0073     mConfig->clear();
0074     mConfig->sync();
0075 }
0076 
0077 void ConfigStore::modify(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration)
0078 {
0079     SinkTrace() << "Modifying " << identifier;
0080     auto config = getConfig(identifier);
0081     for (const auto &key : configuration.keys()) {
0082         auto value = configuration.value(key);
0083         if (value.isValid()) {
0084             config->setValue(key, configuration.value(key));
0085         } else {
0086             config->remove(key);
0087         }
0088     }
0089     config->sync();
0090 }
0091 
0092 QMap<QByteArray, QVariant> ConfigStore::get(const QByteArray &identifier)
0093 {
0094     QMap<QByteArray, QVariant> configuration;
0095     auto config = getConfig(identifier);
0096     for (const auto &key : config->allKeys()) {
0097         configuration.insert(key.toLatin1(), config->value(key));
0098     }
0099     return configuration;
0100 }
0101