From 4aca027e7b7456a430bb05c6d4ad306c2fda49fa Mon Sep 17 00:00:00 2001 From: David Jarvie Date: Thu, 17 Dec 2020 23:58:22 +0000 Subject: [PATCH] Alter priority for removing duplicate Akonadi resources Give priority to keeping enabled, standard, resources. --- Changelog | 3 +- src/resources/akonadiresource.cpp | 60 +++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/Changelog b/Changelog index fae5717a..1b7c4344 100644 --- a/Changelog +++ b/Changelog @@ -1,7 +1,8 @@ KAlarm Change Log -=== Version 3.1.1 (KDE Applications 20.12.1) --- 16 December 2020 === +=== Version 3.1.1 (KDE Applications 20.12.1) --- 18 December 2020 === * Ensure that build uses the file resources option. +* When removing duplicate Akonadi resources, in priority keep enabled/standard ones. * Fix regression introduced in version 3.1.0: Show correct alarm columns in main window on first run of KAlarm. diff --git a/src/resources/akonadiresource.cpp b/src/resources/akonadiresource.cpp index 363c4e3e..b7273d77 100644 --- a/src/resources/akonadiresource.cpp +++ b/src/resources/akonadiresource.cpp @@ -34,6 +34,20 @@ using namespace Akonadi; namespace { + +// Holds an Akonadi Collection's properties. +struct CollectionProperties +{ + QColor backgroundColour; + CalEvent::Types alarmTypes; + CalEvent::Types enabledTypes {CalEvent::EMPTY}; + CalEvent::Types standardTypes {CalEvent::EMPTY}; + bool readOnly; + + // Fetch the properties of a Collection which has been fetched by CollectionFetchJob. + CollectionProperties(const Collection&); +}; + const Collection::Rights WritableRights = Collection::CanChangeItem | Collection::CanCreateItem | Collection::CanDeleteItem; const QRegularExpression MatchMimeType(QStringLiteral("^application/x-vnd\\.kde\\.alarm.*"), @@ -65,10 +79,10 @@ private: struct ResourceCol { QString resourceId; // Akonadi resource identifier - ResourceId collectionId; // Akonadi collection ID + Collection collection; // Akonadi collection ResourceCol() {} - ResourceCol(const QString& r, ResourceId c) - : resourceId(r), collectionId(c) {} + ResourceCol(const QString& r, const Collection& c) + : resourceId(r), collection(c) {} }; QHash mAgentPaths; // path, (resource identifier, collection ID) pairs void (*mCompletionFunc)() {nullptr}; // function to call on completion @@ -597,14 +611,25 @@ void DuplicateResourceObject::collectionFetchResult(KJob* j) { if (c.contentMimeTypes().indexOf(MatchMimeType) >= 0) { - ResourceCol thisRes(job->fetchScope().resource(), c.id()); + ResourceCol thisRes(job->fetchScope().resource(), c); auto it = mAgentPaths.constFind(c.remoteId()); if (it != mAgentPaths.constEnd()) { - // Remove the resource containing the higher numbered Collection - // ID, which is likely to be the more recently created. + // Remove the resource which, in decreasing order of priority: + // - Is disabled; + // - Is not a standard resource; + // - Contains the higher numbered Collection ID, which is likely + // to be the more recently created. const ResourceCol prevRes = it.value(); - if (thisRes.collectionId > prevRes.collectionId) + const CollectionProperties properties[2] = { CollectionProperties(prevRes.collection), + CollectionProperties(thisRes.collection) }; + int propToUse = (thisRes.collection.id() < prevRes.collection.id()) ? 1 : 0; + if (properties[1 - propToUse].standardTypes && !properties[propToUse].standardTypes) + propToUse = 1 - propToUse; + if (properties[1 - propToUse].enabledTypes && !properties[propToUse].enabledTypes) + propToUse = 1 - propToUse; + + if (propToUse == 0) { qCWarning(KALARM_LOG) << "AkonadiResource::collectionFetchResult: Removing duplicate resource" << thisRes.resourceId; agentManager->removeInstance(agentManager->instance(thisRes.resourceId)); @@ -967,6 +992,27 @@ void AkonadiResource::modifyCollectionAttrJobDone(KJob* j) } } +namespace +{ + +/****************************************************************************** +* Fetch an Akonadi collection's properties. +*/ +CollectionProperties::CollectionProperties(const Collection& collection) +{ + readOnly = (collection.rights() & WritableRights) != WritableRights; + alarmTypes = CalEvent::types(collection.contentMimeTypes()); + if (collection.hasAttribute()) + { + const auto* attr = collection.attribute(); + enabledTypes = attr->enabled() & alarmTypes; + standardTypes = attr->standard() & enabledTypes; + backgroundColour = attr->backgroundColor(); + } +} + +} + #include "akonadiresource.moc" // vim: et sw=4: -- GitLab