From af0f69d6c267d231d01b69525b91add8309e43e0 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 21 Oct 2015 12:05:09 +0200 Subject: ClientAPI: Don't require an explicit instance identifier --- common/clientapi.h | 59 +++++++++++++++++------------------ common/domain/applicationdomaintype.h | 7 +++++ common/facadeinterface.h | 25 +++++++++++++++ examples/client/main.cpp | 2 +- tests/clientapitest.cpp | 5 ++- tests/dummyresourcebenchmark.cpp | 4 +-- tests/dummyresourcetest.cpp | 32 +++++++++---------- 7 files changed, 82 insertions(+), 52 deletions(-) diff --git a/common/clientapi.h b/common/clientapi.h index e467b8f..6237cfb 100644 --- a/common/clientapi.h +++ b/common/clientapi.h @@ -134,20 +134,25 @@ public: // { // } + template + static std::shared_ptr > getFacade(const QByteArray &resourceInstanceIdentifier) + { + if (auto facade = FacadeFactory::instance().getFacade(resourceName(resourceInstanceIdentifier), resourceInstanceIdentifier)) { + return facade; + } + return std::make_shared >(); + } /** * Create a new entity. */ template - static KAsync::Job create(const DomainType &domainObject, const QByteArray &resourceIdentifier) { + static KAsync::Job create(const DomainType &domainObject) { //Potentially move to separate thread as well - auto facade = FacadeFactory::instance().getFacade(resourceName(resourceIdentifier), resourceIdentifier); - if (facade) { - return facade->create(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { - Warning() << "Failed to create"; - }); - } - return KAsync::error(-1, "Failed to create a facade"); + auto facade = getFacade(domainObject.resourceInstanceIdentifier()); + return facade->create(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { + Warning() << "Failed to create"; + }); } /** @@ -156,30 +161,24 @@ public: * This includes moving etc. since these are also simple settings on a property. */ template - static KAsync::Job modify(const DomainType &domainObject, const QByteArray &resourceIdentifier) { + static KAsync::Job modify(const DomainType &domainObject) { //Potentially move to separate thread as well - auto facade = FacadeFactory::instance().getFacade(resourceName(resourceIdentifier), resourceIdentifier); - if (facade) { - return facade->modify(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { - Warning() << "Failed to modify"; - }); - } - return KAsync::error(-1, "Failed to create a facade"); + auto facade = getFacade(domainObject.resourceInstanceIdentifier()); + return facade->modify(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { + Warning() << "Failed to modify"; + }); } /** * Remove an entity. */ template - static KAsync::Job remove(const DomainType &domainObject, const QByteArray &resourceIdentifier) { + static KAsync::Job remove(const DomainType &domainObject) { //Potentially move to separate thread as well - auto facade = FacadeFactory::instance().getFacade(resourceName(resourceIdentifier), resourceIdentifier); - if (facade) { - return facade->remove(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { - Warning() << "Failed to remove"; - }); - } - return KAsync::error(-1, "Failed to create a facade"); + auto facade = getFacade(domainObject.resourceInstanceIdentifier()); + return facade->remove(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { + Warning() << "Failed to remove"; + }); } static void shutdown(const QByteArray &resourceIdentifier); @@ -213,20 +212,20 @@ public: return ApplicationDomain::AkonadiResource::Ptr::create(); } - static void setConfiguration(const ApplicationDomain::AkonadiResource &resource, const QByteArray &resourceIdentifier) + static void setConfiguration(const ApplicationDomain::AkonadiResource &resource) { - Store::modify(resource, resourceIdentifier); + Store::modify(resource); } - static void createResource(const ApplicationDomain::AkonadiResource &resource, const QByteArray &resourceIdentifier) + static void createResource(const ApplicationDomain::AkonadiResource &resource) { - Store::create(resource, resourceIdentifier); + Store::create(resource); } static void removeResource(const QByteArray &resourceIdentifier) { - ApplicationDomain::AkonadiResource resource; - Store::remove(resource, resourceIdentifier); + ApplicationDomain::AkonadiResource resource(resourceIdentifier); + Store::remove(resource); } }; diff --git a/common/domain/applicationdomaintype.h b/common/domain/applicationdomaintype.h index 3cc34ec..bd7ff65 100644 --- a/common/domain/applicationdomaintype.h +++ b/common/domain/applicationdomaintype.h @@ -45,6 +45,13 @@ public: } + ApplicationDomainType(const QByteArray &resourceInstanceIdentifier) + :mAdaptor(new MemoryBufferAdaptor()), + mResourceInstanceIdentifier(resourceInstanceIdentifier) + { + + } + ApplicationDomainType(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier, qint64 revision, const QSharedPointer &adaptor) : mAdaptor(adaptor), mResourceInstanceIdentifier(resourceInstanceIdentifier), diff --git a/common/facadeinterface.h b/common/facadeinterface.h index ac60ae4..cd43fa1 100644 --- a/common/facadeinterface.h +++ b/common/facadeinterface.h @@ -48,4 +48,29 @@ public: virtual KAsync::Job load(const Query &query, const QSharedPointer > &resultProvider) = 0; }; +template +class NullFacade : public StoreFacade { +public: + virtual ~NullFacade(){}; + KAsync::Job create(const DomainType &domainObject) + { + return KAsync::error(-1, "Failed to create a facade"); + } + + KAsync::Job modify(const DomainType &domainObject) + { + return KAsync::error(-1, "Failed to create a facade"); + } + + KAsync::Job remove(const DomainType &domainObject) + { + return KAsync::error(-1, "Failed to create a facade"); + } + + KAsync::Job load(const Query &query, const QSharedPointer > &resultProvider) + { + return KAsync::error(-1, "Failed to create a facade"); + } +}; + } diff --git a/examples/client/main.cpp b/examples/client/main.cpp index 269f1aa..6bbec97 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp @@ -66,7 +66,7 @@ public: QObject::connect(removeButton, &QPushButton::pressed, [listView]() { for (auto index :listView->selectionModel()->selectedIndexes()) { auto object = index.data(DomainObjectRole).value(); - Akonadi2::Store::remove(*object, "org.kde.dummy.instance1").exec(); + Akonadi2::Store::remove(*object).exec(); } }); diff --git a/tests/clientapitest.cpp b/tests/clientapitest.cpp index 03cc732..8779716 100644 --- a/tests/clientapitest.cpp +++ b/tests/clientapitest.cpp @@ -95,8 +95,7 @@ private Q_SLOTS: res.setProperty("identifier", "dummyresource.identifier1"); res.setProperty("type", "dummyresource"); - Akonadi2::Store::create(res, "resourceconfig").exec().waitForFinished(); - + Akonadi2::Store::create(res).exec().waitForFinished(); { Akonadi2::Query query; query.resources << "resourceconfig"; @@ -106,7 +105,7 @@ private Q_SLOTS: QCOMPARE(result.size(), 1); } - Akonadi2::Store::remove(res, "resourceconfig").exec().waitForFinished(); + Akonadi2::Store::remove(res).exec().waitForFinished(); { Akonadi2::Query query; query.resources << "resourceconfig"; diff --git a/tests/dummyresourcebenchmark.cpp b/tests/dummyresourcebenchmark.cpp index c020c6b..370acd2 100644 --- a/tests/dummyresourcebenchmark.cpp +++ b/tests/dummyresourcebenchmark.cpp @@ -72,11 +72,11 @@ private Q_SLOTS: int num = 100; QList > waitCondition; for (int i = 0; i < num; i++) { - Akonadi2::ApplicationDomain::Event event; + Akonadi2::ApplicationDomain::Event event("org.kde.dummy.instance1"); event.setProperty("uid", "testuid"); QCOMPARE(event.getProperty("uid").toByteArray(), QByteArray("testuid")); event.setProperty("summary", "summaryValue"); - waitCondition << Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec(); + waitCondition << Akonadi2::Store::create(event).exec(); } waitForCompletion(waitCondition).exec().waitForFinished(); auto appendTime = time.elapsed(); diff --git a/tests/dummyresourcetest.cpp b/tests/dummyresourcetest.cpp index 9d7d092..48bb7b0 100644 --- a/tests/dummyresourcetest.cpp +++ b/tests/dummyresourcetest.cpp @@ -48,11 +48,11 @@ private Q_SLOTS: void testWriteToFacadeAndQueryByUid() { - Akonadi2::ApplicationDomain::Event event; + Akonadi2::ApplicationDomain::Event event("org.kde.dummy.instance1"); event.setProperty("uid", "testuid"); QCOMPARE(event.getProperty("uid").toByteArray(), QByteArray("testuid")); event.setProperty("summary", "summaryValue"); - Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::create(event).exec().waitForFinished(); Akonadi2::Query query; query.resources << "org.kde.dummy.instance1"; @@ -69,14 +69,14 @@ private Q_SLOTS: void testWriteToFacadeAndQueryByUid2() { - Akonadi2::ApplicationDomain::Event event; + Akonadi2::ApplicationDomain::Event event("org.kde.dummy.instance1"); event.setProperty("summary", "summaryValue"); event.setProperty("uid", "testuid"); - Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::create(event).exec().waitForFinished(); event.setProperty("uid", "testuid2"); - Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::create(event).exec().waitForFinished(); Akonadi2::Query query; query.resources << "org.kde.dummy.instance1"; @@ -94,15 +94,15 @@ private Q_SLOTS: void testWriteToFacadeAndQueryBySummary() { - Akonadi2::ApplicationDomain::Event event; + Akonadi2::ApplicationDomain::Event event("org.kde.dummy.instance1"); event.setProperty("uid", "testuid"); event.setProperty("summary", "summaryValue1"); - Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::create(event).exec().waitForFinished(); event.setProperty("uid", "testuid2"); event.setProperty("summary", "summaryValue2"); - Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::create(event).exec().waitForFinished(); Akonadi2::Query query; query.resources << "org.kde.dummy.instance1"; @@ -165,11 +165,11 @@ private Q_SLOTS: void testWriteModifyDelete() { - Akonadi2::ApplicationDomain::Event event; + Akonadi2::ApplicationDomain::Event event("org.kde.dummy.instance1"); event.setProperty("uid", "testuid"); QCOMPARE(event.getProperty("uid").toByteArray(), QByteArray("testuid")); event.setProperty("summary", "summaryValue"); - Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::create(event).exec().waitForFinished(); Akonadi2::Query query; query.resources << "org.kde.dummy.instance1"; @@ -191,7 +191,7 @@ private Q_SLOTS: event2.setProperty("uid", "testuid"); event2.setProperty("summary", "summaryValue2"); - Akonadi2::Store::modify(event2, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::modify(event2).exec().waitForFinished(); //Test modify { @@ -203,7 +203,7 @@ private Q_SLOTS: QCOMPARE(value->getProperty("summary").toByteArray(), QByteArray("summaryValue2")); } - Akonadi2::Store::remove(event2, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::remove(event2).exec().waitForFinished(); //Test remove { @@ -227,11 +227,11 @@ private Q_SLOTS: async::SyncListResult result(Akonadi2::Store::load(query)); result.exec(); - Akonadi2::ApplicationDomain::Event event; + Akonadi2::ApplicationDomain::Event event("org.kde.dummy.instance1"); event.setProperty("uid", "testuid"); QCOMPARE(event.getProperty("uid").toByteArray(), QByteArray("testuid")); event.setProperty("summary", "summaryValue"); - Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::create(event).exec().waitForFinished(); //Test create Akonadi2::ApplicationDomain::Event event2; @@ -245,7 +245,7 @@ private Q_SLOTS: event2.setProperty("uid", "testuid"); event2.setProperty("summary", "summaryValue2"); - Akonadi2::Store::modify(event2, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::modify(event2).exec().waitForFinished(); //Test modify { @@ -255,7 +255,7 @@ private Q_SLOTS: QCOMPARE(value->getProperty("summary").toByteArray(), QByteArray("summaryValue2")); } - Akonadi2::Store::remove(event2, "org.kde.dummy.instance1").exec().waitForFinished(); + Akonadi2::Store::remove(event2).exec().waitForFinished(); //Test remove { -- cgit v1.2.3