File indexing completed on 2024-05-12 05:26:03
0001 /* 0002 * Copyright (C) 2014 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 "resourceconfig.h" 0020 0021 #include <QSettings> 0022 #include <QSharedPointer> 0023 #include <QFile> 0024 #include <log.h> 0025 #include <definitions.h> 0026 #include <applicationdomaintype.h> 0027 0028 static QSharedPointer<QSettings> getConfig(const QByteArray &identifier) 0029 { 0030 return QSharedPointer<QSettings>::create(Sink::configLocation() +"/" + identifier + ".ini", QSettings::IniFormat); 0031 } 0032 0033 QByteArray ResourceConfig::newIdentifier(const QByteArray &type) 0034 { 0035 auto settings = getConfig("resources"); 0036 const auto counter = settings->value("instanceCounter", 0).toInt() + 1; 0037 const QByteArray identifier = type + ".instance" + QByteArray::number(counter); 0038 settings->setValue("instanceCounter", counter); 0039 settings->sync(); 0040 return identifier; 0041 } 0042 0043 void ResourceConfig::addResource(const QByteArray &identifier, const QByteArray &type) 0044 { 0045 auto settings = getConfig("resources"); 0046 settings->beginGroup(QString::fromLatin1(identifier)); 0047 settings->setValue(Sink::ApplicationDomain::SinkResource::ResourceType::name, type); 0048 settings->endGroup(); 0049 settings->sync(); 0050 } 0051 0052 void ResourceConfig::setResourceType(const QByteArray &identifier, const QByteArray &type) 0053 { 0054 auto settings = getConfig("resources"); 0055 settings->beginGroup(QString::fromLatin1(identifier)); 0056 settings->setValue(Sink::ApplicationDomain::SinkResource::ResourceType::name, type); 0057 settings->endGroup(); 0058 settings->sync(); 0059 } 0060 0061 0062 void ResourceConfig::removeResource(const QByteArray &identifier) 0063 { 0064 auto settings = getConfig("resources"); 0065 settings->beginGroup(QString::fromLatin1(identifier)); 0066 settings->remove(""); 0067 settings->endGroup(); 0068 settings->sync(); 0069 QFile::remove(getConfig(identifier)->fileName()); 0070 } 0071 0072 QMap<QByteArray, QByteArray> ResourceConfig::getResources() 0073 { 0074 QMap<QByteArray, QByteArray> resources; 0075 auto settings = getConfig("resources"); 0076 for (const auto &identifier : settings->childGroups()) { 0077 settings->beginGroup(identifier); 0078 const auto type = settings->value(Sink::ApplicationDomain::SinkResource::ResourceType::name).toByteArray(); 0079 resources.insert(identifier.toLatin1(), type); 0080 settings->endGroup(); 0081 } 0082 return resources; 0083 } 0084 0085 QByteArray ResourceConfig::getResourceType(const QByteArray &identifier) 0086 { 0087 return getResources().value(identifier); 0088 } 0089 0090 void ResourceConfig::clear() 0091 { 0092 auto settings = getConfig("resources"); 0093 settings->clear(); 0094 settings->sync(); 0095 } 0096 0097 void ResourceConfig::configureResource(const QByteArray &identifier, const QMap<QByteArray, QVariant> &configuration) 0098 { 0099 auto config = getConfig(identifier); 0100 config->clear(); 0101 for (const auto &key : configuration.keys()) { 0102 config->setValue(key, configuration.value(key)); 0103 } 0104 config->sync(); 0105 } 0106 0107 QMap<QByteArray, QVariant> ResourceConfig::getConfiguration(const QByteArray &identifier) 0108 { 0109 QMap<QByteArray, QVariant> configuration; 0110 auto config = getConfig(identifier); 0111 for (const auto &key : config->allKeys()) { 0112 configuration.insert(key.toLatin1(), config->value(key)); 0113 } 0114 return configuration; 0115 }