From c1a43cf6dabe29bc15186f8062ceeeee4ae77993 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 15 Dec 2015 13:42:28 +0100 Subject: A simple resource configuration mechanism We simply write all properties we don't know to a config file. Resources can pick up configurations as they are started. --- common/resourceconfig.cpp | 29 +++++++++++++++++++++++++++++ common/resourceconfig.h | 3 +++ common/resourcefacade.cpp | 30 +++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) (limited to 'common') diff --git a/common/resourceconfig.cpp b/common/resourceconfig.cpp index ec72fb9..99e111e 100644 --- a/common/resourceconfig.cpp +++ b/common/resourceconfig.cpp @@ -21,12 +21,19 @@ #include #include #include +#include static QSharedPointer getSettings() { return QSharedPointer::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/resources.ini", QSettings::IniFormat); } +static QSharedPointer getResourceConfig(const QByteArray &identifier) +{ + return QSharedPointer::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/" + identifier, QSettings::IniFormat); +} + + void ResourceConfig::addResource(const QByteArray &identifier, const QByteArray &type) { auto settings = getSettings(); @@ -44,6 +51,7 @@ void ResourceConfig::removeResource(const QByteArray &identifier) settings->remove(""); settings->endGroup(); settings->sync(); + QFile::remove(getResourceConfig(identifier)->fileName()); } QMap ResourceConfig::getResources() @@ -65,3 +73,24 @@ void ResourceConfig::clear() settings->clear(); settings->sync(); } + +void ResourceConfig::configureResource(const QByteArray &identifier, const QMap &configuration) +{ + auto config = getResourceConfig(identifier); + config->clear(); + for (const auto &key : configuration.keys()) { + config->setValue(key, configuration.value(key)); + } + config->sync(); +} + +QMap ResourceConfig::getConfiguration(const QByteArray &identifier) +{ + QMap configuration; + auto config = getResourceConfig(identifier); + for (const auto &key : config->allKeys()) { + configuration.insert(key.toLatin1(), config->value(key)); + } + return configuration; +} + diff --git a/common/resourceconfig.h b/common/resourceconfig.h index e1ba2bc..fec9f56 100644 --- a/common/resourceconfig.h +++ b/common/resourceconfig.h @@ -21,6 +21,7 @@ #include #include +#include #include class ResourceConfig @@ -30,4 +31,6 @@ public: static void addResource(const QByteArray &identifier, const QByteArray &type); static void removeResource(const QByteArray &identifier); static void clear(); + static void configureResource(const QByteArray &identifier, const QMap &configuration); + static QMap getConfiguration(const QByteArray &identifier); }; diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp index a00bab5..89d072c 100644 --- a/common/resourcefacade.cpp +++ b/common/resourcefacade.cpp @@ -38,12 +38,40 @@ KAsync::Job ResourceFacade::create(const Akonadi2::ApplicationDomain::Akon const QByteArray identifier = resource.getProperty("identifier").toByteArray(); const QByteArray type = resource.getProperty("type").toByteArray(); ResourceConfig::addResource(identifier, type); + auto changedProperties = resource.changedProperties(); + changedProperties.removeOne("identifier"); + changedProperties.removeOne("type"); + if (!changedProperties.isEmpty()) { + //We have some configuration values + QMap configurationValues; + for (const auto &property : changedProperties) { + configurationValues.insert(property, resource.getProperty(property)); + } + ResourceConfig::configureResource(identifier, configurationValues); + } }); } KAsync::Job ResourceFacade::modify(const Akonadi2::ApplicationDomain::AkonadiResource &resource) { - return KAsync::null(); + return KAsync::start([resource, this]() { + const QByteArray identifier = resource.getProperty("identifier").toByteArray(); + if (identifier.isEmpty()) { + Warning() << "We need an \"identifier\" property to identify the resource to configure"; + return; + } + auto changedProperties = resource.changedProperties(); + changedProperties.removeOne("identifier"); + changedProperties.removeOne("type"); + if (!changedProperties.isEmpty()) { + //We have some configuration values + QMap configurationValues; + for (const auto &property : changedProperties) { + configurationValues.insert(property, resource.getProperty(property)); + } + ResourceConfig::configureResource(identifier, configurationValues); + } + }); } KAsync::Job ResourceFacade::remove(const Akonadi2::ApplicationDomain::AkonadiResource &resource) -- cgit v1.2.3