From 228a3380328535f30fcb187cae7db2415ec2d314 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 8 Jul 2015 09:19:43 +0200 Subject: We can add resources. --- common/CMakeLists.txt | 1 + common/clientapi.cpp | 6 +++ common/clientapi.h | 19 ++++++++-- common/resourcefacade.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++ common/resourcefacade.h | 34 +++++++++++++++++ 5 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 common/resourcefacade.cpp create mode 100644 common/resourcefacade.h (limited to 'common') diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 9e30752..58c7ea0 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -26,6 +26,7 @@ set(command_SRCS threadboundary.cpp messagequeue.cpp index.cpp + resourcefacade.cpp domain/applicationdomaintype.cpp domain/event.cpp ${storage_SRCS}) diff --git a/common/clientapi.cpp b/common/clientapi.cpp index 2f8763f..88349db 100644 --- a/common/clientapi.cpp +++ b/common/clientapi.cpp @@ -2,6 +2,7 @@ #include "clientapi.h" #include "resourceaccess.h" #include "commands.h" +#include "resourcefacade.h" #include "log.h" #include @@ -16,6 +17,11 @@ namespace async namespace Akonadi2 { +void FacadeFactory::registerStaticFacades() +{ + FacadeFactory::instance().registerFacade("resourceconfig"); +} + void Store::shutdown(const QByteArray &identifier) { Trace() << "shutdown"; diff --git a/common/clientapi.h b/common/clientapi.h index 2d926fd..d658003 100644 --- a/common/clientapi.h +++ b/common/clientapi.h @@ -110,6 +110,8 @@ class FacadeFactory { public: typedef std::function(const QByteArray &)> FactoryFunction; + static void registerStaticFacades(); + //FIXME: proper singleton implementation static FacadeFactory &instance() { @@ -179,6 +181,9 @@ public: static QByteArray resourceName(const QByteArray &instanceIdentifier) { auto split = instanceIdentifier.split('.'); + if (split.size() <= 1) { + return instanceIdentifier; + } split.removeLast(); return split.join('.'); } @@ -195,6 +200,7 @@ public: //We must guarantee that the emitter is returned before the first result is emitted. //The result provider must be threadsafe. async::run([query, resultSet](){ + //TODO if query.resources is empty, search for all resource that provide the type we're looking for // Query all resources and aggregate results KAsync::iterate(query.resources) .template each([query, resultSet](const QByteArray &resource, KAsync::Future &future) { @@ -205,7 +211,6 @@ public: //Keep the facade alive for the lifetime of the resultSet. resultSet->setFacade(facade); } else { - qWarning() << "Could not find facade for resource " << resource; //Ignore the error and carry on future.setFinished(); } @@ -245,7 +250,9 @@ public: static void create(const DomainType &domainObject, const QByteArray &resourceIdentifier) { //Potentially move to separate thread as well auto facade = FacadeFactory::instance().getFacade(resourceName(resourceIdentifier), resourceIdentifier); - facade->create(domainObject).exec().waitForFinished(); + if (facade) { + facade->create(domainObject).exec().waitForFinished(); + } //TODO return job? } @@ -258,7 +265,9 @@ public: static void modify(const DomainType &domainObject, const QByteArray &resourceIdentifier) { //Potentially move to separate thread as well auto facade = FacadeFactory::instance().getFacade(resourceName(resourceIdentifier), resourceIdentifier); - facade->modify(domainObject).exec().waitForFinished(); + if (facade) { + facade->modify(domainObject).exec().waitForFinished(); + } //TODO return job? } @@ -269,7 +278,9 @@ public: static void remove(const DomainType &domainObject, const QByteArray &resourceIdentifier) { //Potentially move to separate thread as well auto facade = FacadeFactory::instance().getFacade(resourceName(resourceIdentifier), resourceIdentifier); - facade->remove(domainObject).exec().waitForFinished(); + if (facade) { + facade->remove(domainObject).exec().waitForFinished(); + } //TODO return job? } diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp new file mode 100644 index 0000000..2c6eabc --- /dev/null +++ b/common/resourcefacade.cpp @@ -0,0 +1,94 @@ +/* + * 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 "resourcefacade.h" + +#include +#include + +ResourceFacade::ResourceFacade(const QByteArray &) + : Akonadi2::StoreFacade() +{ + +} + +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(); + }); +} + +KAsync::Job ResourceFacade::modify(const Akonadi2::ApplicationDomain::AkonadiResource &resource) +{ + return KAsync::null(); +} + +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(); + }); +} + +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(); + auto resource = Akonadi2::ApplicationDomain::AkonadiResource::Ptr::create(); + resource->setProperty("identifier", identifier); + resource->setProperty("type", type); + resultProvider->add(resource); + } + settings->endGroup(); + + //TODO initialResultSetComplete should be implicit + resultProvider->initialResultSetComplete(); + resultProvider->complete(); + }); +} + diff --git a/common/resourcefacade.h b/common/resourcefacade.h new file mode 100644 index 0000000..2b36f21 --- /dev/null +++ b/common/resourcefacade.h @@ -0,0 +1,34 @@ +/* + * 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 "common/clientapi.h" +#include "common/resultprovider.h" + +class ResourceFacade : public Akonadi2::StoreFacade +{ +public: + ResourceFacade(const QByteArray &instanceIdentifier); + virtual ~ResourceFacade(); + KAsync::Job create(const Akonadi2::ApplicationDomain::AkonadiResource &resource) Q_DECL_OVERRIDE; + KAsync::Job modify(const Akonadi2::ApplicationDomain::AkonadiResource &resource) Q_DECL_OVERRIDE; + KAsync::Job remove(const Akonadi2::ApplicationDomain::AkonadiResource &resource) Q_DECL_OVERRIDE; + KAsync::Job load(const Akonadi2::Query &query, const QSharedPointer > &resultProvider) Q_DECL_OVERRIDE; +}; -- cgit v1.2.3