From e8ab53258c044969146be385629a80afe801dee5 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 22 Dec 2015 16:35:09 +0100 Subject: Turn operations of resources into a special case. Resources don't live inside a resource context, and as such inherently are a special case. By also removing the option to manually specify the identifier, the commandline is a lot easier to use. --- common/resourceconfig.cpp | 9 +++++++++ common/resourceconfig.h | 1 + common/resourcefacade.cpp | 8 ++++++-- examples/client/main.cpp | 35 +++++++++++++++++++++++++++-------- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/common/resourceconfig.cpp b/common/resourceconfig.cpp index 3554b76..a0f39a8 100644 --- a/common/resourceconfig.cpp +++ b/common/resourceconfig.cpp @@ -33,6 +33,15 @@ static QSharedPointer getResourceConfig(const QByteArray &identifier) return QSharedPointer::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/" + identifier, QSettings::IniFormat); } +QByteArray ResourceConfig::newIdentifier(const QByteArray &type) +{ + auto settings = getSettings(); + const auto counter = settings->value("instanceCounter", 0).toInt() + 1; + const QByteArray identifier = type + ".instance" + QByteArray::number(counter); + settings->setValue("instanceCounter", counter); + settings->sync(); + return identifier; +} void ResourceConfig::addResource(const QByteArray &identifier, const QByteArray &type) { diff --git a/common/resourceconfig.h b/common/resourceconfig.h index fec9f56..cc9cb94 100644 --- a/common/resourceconfig.h +++ b/common/resourceconfig.h @@ -28,6 +28,7 @@ class ResourceConfig { public: static QMap getResources(); + static QByteArray newIdentifier(const QByteArray &type); static void addResource(const QByteArray &identifier, const QByteArray &type); static void removeResource(const QByteArray &identifier); static void clear(); diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp index 367704a..df52538 100644 --- a/common/resourcefacade.cpp +++ b/common/resourcefacade.cpp @@ -20,6 +20,9 @@ #include "resourceconfig.h" #include "query.h" +#include "definitions.h" +#include "storage.h" +#include ResourceFacade::ResourceFacade(const QByteArray &) : Akonadi2::StoreFacade() @@ -35,8 +38,9 @@ ResourceFacade::~ResourceFacade() KAsync::Job ResourceFacade::create(const Akonadi2::ApplicationDomain::AkonadiResource &resource) { return KAsync::start([resource, this]() { - const QByteArray identifier = resource.getProperty("identifier").toByteArray(); const QByteArray type = resource.getProperty("type").toByteArray(); + //It is currently a requirement that the resource starts with the type + const QByteArray identifier = ResourceConfig::newIdentifier(type); ResourceConfig::addResource(identifier, type); auto changedProperties = resource.changedProperties(); changedProperties.removeOne("identifier"); @@ -57,7 +61,7 @@ KAsync::Job ResourceFacade::modify(const Akonadi2::ApplicationDomain::Akon return KAsync::start([resource, this]() { const QByteArray identifier = resource.identifier(); if (identifier.isEmpty()) { - Warning() << "We need an \"identifier\" property to identify the resource to configure"; + Warning() << "We need an \"identifier\" property to identify the resource to configure."; return; } auto changedProperties = resource.changedProperties(); diff --git a/examples/client/main.cpp b/examples/client/main.cpp index 81bcc11..946557f 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp @@ -335,8 +335,15 @@ int main(int argc, char *argv[]) } else if (command == "create") { auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); auto &store = getStore(type); - auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); - auto object = store.getObject(resource); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object; + if (type == "resource") { + auto resourceType = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + object = store.getObject(""); + object->setProperty("type", resourceType); + } else { + auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + object = store.getObject(resource); + } auto map = consumeMap(args); for (auto i = map.begin(); i != map.end(); ++i) { object->setProperty(i.key().toLatin1(), i.value()); @@ -349,9 +356,15 @@ int main(int argc, char *argv[]) } else if (command == "modify") { auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); auto &store = getStore(type); - auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); - auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); - auto object = store.getObject(resource, identifier); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object; + if (type == "resource") { + auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + object = store.getObject("", identifier); + } else { + auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + object = store.getObject(resource, identifier); + } auto map = consumeMap(args); for (auto i = map.begin(); i != map.end(); ++i) { object->setProperty(i.key().toLatin1(), i.value()); @@ -364,9 +377,15 @@ int main(int argc, char *argv[]) } else if (command == "remove") { auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); auto &store = getStore(type); - auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); - auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); - auto object = store.getObject(resource, identifier); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object; + if (type == "resource") { + auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + object = store.getObject("", identifier); + } else { + auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + object = store.getObject(resource, identifier); + } auto result = store.remove(*object).exec(); result.waitForFinished(); if (result.errorCode()) { -- cgit v1.2.3