From c0a8d67cde03e33bf21e0f186f85d4c89c7ff572 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 8 Jul 2015 09:57:40 +0200 Subject: Extracted resource config --- common/CMakeLists.txt | 1 + common/resourceconfig.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ common/resourceconfig.h | 32 ++++++++++++++++++++++++ common/resourcefacade.cpp | 36 +++++---------------------- 4 files changed, 101 insertions(+), 30 deletions(-) create mode 100644 common/resourceconfig.cpp create mode 100644 common/resourceconfig.h (limited to 'common') diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 58c7ea0..56ae59b 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -27,6 +27,7 @@ set(command_SRCS messagequeue.cpp index.cpp resourcefacade.cpp + resourceconfig.cpp domain/applicationdomaintype.cpp domain/event.cpp ${storage_SRCS}) diff --git a/common/resourceconfig.cpp b/common/resourceconfig.cpp new file mode 100644 index 0000000..1f8fcda --- /dev/null +++ b/common/resourceconfig.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2014 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include "resourceconfig.h" + +#include +#include +#include + +static QSharedPointer getSettings() +{ + return QSharedPointer::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/resources.ini", QSettings::IniFormat); +} + +void ResourceConfig::addResource(const QByteArray &identifier, const QByteArray &type) +{ + auto settings = getSettings(); + settings->beginGroup("resources"); + settings->setValue(QString::fromLatin1(identifier), type); + settings->endGroup(); + // settings->beginGroup(identifier); + // //Add some settings? + // settings->endGroup(); + settings->sync(); +} + +void ResourceConfig::removeResource(const QByteArray &identifier) +{ + auto settings = getSettings(); + settings->beginGroup("resources"); + settings->remove(QString::fromLatin1(identifier)); + settings->endGroup(); + settings->sync(); +} + +QList > ResourceConfig::getResources() +{ + QList > resources; + auto settings = getSettings(); + settings->beginGroup("resources"); + for (const auto &identifier : settings->childKeys()) { + const auto type = settings->value(identifier).toByteArray(); + resources << qMakePair(identifier.toLatin1(), type); + } + settings->endGroup(); + return resources; +} diff --git a/common/resourceconfig.h b/common/resourceconfig.h new file mode 100644 index 0000000..eda870f --- /dev/null +++ b/common/resourceconfig.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2014 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include +#include +#include + +class ResourceConfig +{ +public: + static QList > getResources(); + static void addResource(const QByteArray &identifier, const QByteArray &type); + static void removeResource(const QByteArray &identifier); +}; diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp index 2c6eabc..63b3126 100644 --- a/common/resourcefacade.cpp +++ b/common/resourcefacade.cpp @@ -18,8 +18,7 @@ */ #include "resourcefacade.h" -#include -#include +#include "resourceconfig.h" ResourceFacade::ResourceFacade(const QByteArray &) : Akonadi2::StoreFacade() @@ -32,25 +31,12 @@ ResourceFacade::~ResourceFacade() } -static QSharedPointer getSettings() -{ - return QSharedPointer::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/resources.ini", QSettings::IniFormat); -} - KAsync::Job ResourceFacade::create(const Akonadi2::ApplicationDomain::AkonadiResource &resource) { return KAsync::start([resource, this]() { - auto settings = getSettings(); const QByteArray identifier = resource.getProperty("identifier").toByteArray(); const QByteArray type = resource.getProperty("type").toByteArray(); - - settings->beginGroup("resources"); - settings->setValue(identifier, type); - settings->endGroup(); - settings->beginGroup(identifier); - //Add some settings? - settings->endGroup(); - settings->sync(); + ResourceConfig::addResource(identifier, type); }); } @@ -62,30 +48,20 @@ KAsync::Job ResourceFacade::modify(const Akonadi2::ApplicationDomain::Akon KAsync::Job ResourceFacade::remove(const Akonadi2::ApplicationDomain::AkonadiResource &resource) { return KAsync::start([resource, this]() { - auto settings = getSettings(); const QByteArray identifier = resource.getProperty("identifier").toByteArray(); - - settings->beginGroup("resources"); - settings->remove(identifier); - settings->endGroup(); - settings->sync(); + ResourceConfig::removeResource(identifier); }); } KAsync::Job ResourceFacade::load(const Akonadi2::Query &query, const QSharedPointer > &resultProvider) { return KAsync::start([query, resultProvider]() { - auto settings = getSettings(); - settings->beginGroup("resources"); - for (const auto &identifier : settings->childKeys()) { - const auto type = settings->value(identifier).toByteArray(); + for (const auto &res : ResourceConfig::getResources()) { auto resource = Akonadi2::ApplicationDomain::AkonadiResource::Ptr::create(); - resource->setProperty("identifier", identifier); - resource->setProperty("type", type); + resource->setProperty("identifier", res.first); + resource->setProperty("type", res.second); resultProvider->add(resource); } - settings->endGroup(); - //TODO initialResultSetComplete should be implicit resultProvider->initialResultSetComplete(); resultProvider->complete(); -- cgit v1.2.3