diff --git a/resources/kolab/kolabhelpers.h b/resources/kolab/kolabhelpers.h --- a/resources/kolab/kolabhelpers.h +++ b/resources/kolab/kolabhelpers.h @@ -24,6 +24,10 @@ #include //libkolab #include //libkolab +#define KOLAB_COLOR_ANNOTATION "/vendor/kolab/color" + +class QColor; + class KolabHelpers { public: static bool checkForErrors(const Akonadi::Item &affectedItem); @@ -33,6 +37,8 @@ static Kolab::FolderType folderTypeFromString(const QByteArray &folderTypeName); static QByteArray getFolderTypeAnnotation( const QMap &annotations); static void setFolderTypeAnnotation( QMap &annotations, const QByteArray &value); + static QColor getFolderColor(const QMap &annotations); + static void setFolderColor(QMap &annotations, const QColor &color); static Kolab::ObjectType getKolabTypeFromMimeType(const QString &type); static QByteArray kolabTypeForMimeType( const QStringList &contentMimeTypes ); static QStringList getContentMimeTypes(Kolab::FolderType type); diff --git a/resources/kolab/kolabhelpers.cpp b/resources/kolab/kolabhelpers.cpp --- a/resources/kolab/kolabhelpers.cpp +++ b/resources/kolab/kolabhelpers.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "tracer.h" bool KolabHelpers::checkForErrors(const Akonadi::Item &item) @@ -432,6 +433,23 @@ annotations["/shared" KOLAB_FOLDER_TYPE_ANNOTATION] = value; } +QColor KolabHelpers::getFolderColor(const QMap &annotations) +{ + // kolab saves the color without a "#", so we need to add it to the rgb string to have a propper QColor + if (annotations.contains("/shared" KOLAB_COLOR_ANNOTATION) && !annotations.value("/shared" KOLAB_COLOR_ANNOTATION).isEmpty()) { + return QColor(QLatin1String("#") + annotations.value("/shared" KOLAB_COLOR_ANNOTATION)); + } else if (annotations.contains("/private" KOLAB_COLOR_ANNOTATION) && !annotations.value("/private" KOLAB_COLOR_ANNOTATION).isEmpty()) { + return QColor(QLatin1String("#") + annotations.value("/private" KOLAB_COLOR_ANNOTATION)); + } + return QColor(); +} + +void KolabHelpers::setFolderColor(QMap &annotations, const QColor &color) +{ + // kolab saves the color without a "#", so we need to delete the prefix "#" if we save it to the annotations + annotations["/shared" KOLAB_COLOR_ANNOTATION] = color.name().toAscii().remove(0,1); +} + QString KolabHelpers::getIcon(Kolab::FolderType type) { switch (type) { diff --git a/resources/kolab/kolabresource.cpp b/resources/kolab/kolabresource.cpp --- a/resources/kolab/kolabresource.cpp +++ b/resources/kolab/kolabresource.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -47,6 +49,7 @@ KolabResource::KolabResource(const QString& id) :ImapResource(id) { + Akonadi::AttributeFactory::registerAttribute(); //Load translations from imap resource KGlobal::locale()->insertCatalog(QLatin1String("akonadi_imap_resource")); //Ensure we have up-to date metadata before attempting to sync folder @@ -148,11 +151,24 @@ Trace() << collection.id(); //Set the annotations on new folders const QByteArray kolabType = KolabHelpers::kolabTypeForMimeType(collection.contentMimeTypes()); + Akonadi::Collection col = collection; + Akonadi::CollectionAnnotationsAttribute *attr = col.attribute(Akonadi::Collection::AddIfMissing); + QMap annotations = attr->annotations(); + + bool changed = false; + Akonadi::CollectionColorAttribute *colorAttribute = col.attribute(); + if (colorAttribute) { + const QColor color = colorAttribute->color(); + if (color.isValid()) { + KolabHelpers::setFolderColor(annotations, color); + changed = true; + } + } if (!kolabType.isEmpty()) { - Akonadi::Collection col = collection; - Akonadi::CollectionAnnotationsAttribute *attr = col.attribute(Akonadi::Collection::AddIfMissing); - QMap annotations = attr->annotations(); KolabHelpers::setFolderTypeAnnotation(annotations, kolabType); + changed = true; + } + if (changed) { attr->setAnnotations(annotations); return col; } @@ -172,11 +188,15 @@ void KolabResource::collectionChanged(const Akonadi::Collection& collection, const QSet< QByteArray >& parts) { Trace() << collection.id() << parts; + QSet p = parts; //Update annotations if necessary const Akonadi::Collection col = updateAnnotations(collection); + if(parts.contains(Akonadi::CollectionColorAttribute().type())) { + p << Akonadi::CollectionAnnotationsAttribute().type(); + } //TODO we need to save the collections as well if the annotations have changed emit status( AgentBase::Running, i18nc( "@info:status", "Updating folder '%1'", collection.name() ) ); - ChangeCollectionTask *task = new ChangeCollectionTask( createResourceState(TaskArguments(collection, parts)), this ); + ChangeCollectionTask *task = new ChangeCollectionTask( createResourceState(TaskArguments(collection, p)), this ); task->syncEnabledState(true); startTask(task); } diff --git a/resources/kolab/kolabretrievecollectionstask.cpp b/resources/kolab/kolabretrievecollectionstask.cpp --- a/resources/kolab/kolabretrievecollectionstask.cpp +++ b/resources/kolab/kolabretrievecollectionstask.cpp @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -42,6 +43,7 @@ #include #include +#include bool isNamespaceFolder(const QString &path, const QList &namespaces, bool matchCompletePath = false) @@ -196,6 +198,8 @@ { mRequestedMetadata << "/shared/vendor/kolab/folder-type"; mRequestedMetadata << "/private/vendor/kolab/folder-type"; + mRequestedMetadata << "/shared" KOLAB_COLOR_ANNOTATION + << "/private" KOLAB_COLOR_ANNOTATION; } KolabRetrieveCollectionsTask::~KolabRetrieveCollectionsTask() @@ -480,6 +484,10 @@ const Kolab::FolderType folderType = KolabHelpers::folderTypeFromString(type); // kDebug() << mailbox << metadata << type << folderType << KolabHelpers::getContentMimeTypes(folderType); collection.setContentMimeTypes(KolabHelpers::getContentMimeTypes(folderType)); + const QColor color = KolabHelpers::getFolderColor(metadata); + if (color.isValid()) { + collection.attribute(Akonadi::Collection::AddIfMissing)->setColor(color); + } QSet keepLocalChanges = collection.keepLocalChanges(); keepLocalChanges.remove(cContentMimeTypes); collection.setKeepLocalChanges(keepLocalChanges);