Commit 8c6a9467 authored by Volker Krause's avatar Volker Krause
Browse files

Use the Akonadi::TagCache from Akonadi

Moved there, so we can eventually port away from the KCalPrefs category
color API to use that directly.

Replacing the implementation here first though, so that even during the
transitional period we don't have duplicate and thus possibly out of sync
tag state in applications.

# Conflicts:
#	CMakeLists.txt
parent 4e9440c0
Pipeline #205594 passed with stage
in 4 minutes and 39 seconds
......@@ -19,7 +19,6 @@ target_sources(KF5CalendarSupport PRIVATE
messagewidget.cpp
noteeditdialog.cpp
utils.cpp
tagcache.cpp
urihandler.cpp
printing/calprintpluginbase.cpp
......@@ -60,8 +59,6 @@ target_sources(KF5CalendarSupport PRIVATE
freebusymodel/freebusyitem.h
collectionselection.h
messagewidget.h
tagcache.h
)
ki18n_wrap_ui(KF5CalendarSupport
......
......@@ -8,9 +8,9 @@
#include "kcalprefs.h"
#include "calendarsupport_debug.h"
#include "identitymanager.h"
#include "tagcache.h"
#include <Akonadi/TagAttribute>
#include <Akonadi/TagCache>
#include <Akonadi/TagModifyJob>
#include <KMime/HeaderParsing>
......@@ -37,7 +37,6 @@ public:
Akonadi::Collection::Id mDefaultCalendarId;
TagCache mTagCache;
const QColor mDefaultCategoryColor;
QDateTime mDayBegins;
};
......@@ -46,6 +45,7 @@ KCalPrefs::KCalPrefs()
: KCalPrefsBase()
, d(new KCalPrefsPrivate())
{
Akonadi::TagCache::instance();
}
KCalPrefs::~KCalPrefs() = default;
......@@ -261,23 +261,12 @@ bool KCalPrefs::thatIsMe(const QString &_email)
void KCalPrefs::setCategoryColor(const QString &cat, const QColor &color)
{
Akonadi::Tag tag = d->mTagCache.getTagByName(cat);
auto attr = tag.attribute<Akonadi::TagAttribute>(Akonadi::Tag::AddIfMissing);
attr->setBackgroundColor(color);
new Akonadi::TagModifyJob(tag);
Akonadi::TagCache::instance()->setTagColor(cat, color);
}
QColor KCalPrefs::categoryColor(const QString &cat) const
{
QColor color;
if (!cat.isEmpty()) {
const Akonadi::Tag &tag = d->mTagCache.getTagByName(cat);
if (const auto attr = tag.attribute<Akonadi::TagAttribute>()) {
color = attr->backgroundColor();
}
}
QColor color = Akonadi::TagCache::instance()->tagColor(cat);
return color.isValid() ? color : d->mDefaultCategoryColor;
}
......
/*
SPDX-FileCopyrightText: 2015 Sandro Knauß <knauss@kolabsys.com>
SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
*/
#include "tagcache.h"
#include "calendarsupport_debug.h"
#include <Akonadi/Monitor>
#include <Akonadi/TagAttribute>
#include <Akonadi/TagFetchJob>
#include <Akonadi/TagFetchScope>
using namespace CalendarSupport;
TagCache::TagCache()
: QObject()
, mMonitor(new Akonadi::Monitor(this))
{
mMonitor->setObjectName(QStringLiteral("TagCacheMonitor"));
mMonitor->setTypeMonitored(Akonadi::Monitor::Tags);
mMonitor->tagFetchScope().fetchAttribute<Akonadi::TagAttribute>();
connect(mMonitor, &Akonadi::Monitor::tagAdded, this, &TagCache::onTagAdded);
connect(mMonitor, &Akonadi::Monitor::tagRemoved, this, &TagCache::onTagRemoved);
connect(mMonitor, &Akonadi::Monitor::tagChanged, this, &TagCache::onTagChanged);
retrieveTags();
}
Akonadi::Tag TagCache::getTagByGid(const QByteArray &gid) const
{
return mCache.value(mGidCache.value(gid));
}
Akonadi::Tag TagCache::getTagByName(const QString &name) const
{
return mCache.value(mNameCache.value(name));
}
void TagCache::onTagAdded(const Akonadi::Tag &tag)
{
mCache.insert(tag.id(), tag);
mGidCache.insert(tag.gid(), tag.id());
mNameCache.insert(tag.name(), tag.id());
}
void TagCache::onTagChanged(const Akonadi::Tag &tag)
{
onTagAdded(tag);
}
void TagCache::onTagRemoved(const Akonadi::Tag &tag)
{
mCache.remove(tag.id());
mGidCache.remove(tag.gid());
mNameCache.remove(tag.name());
}
void TagCache::retrieveTags()
{
auto tagFetchJob = new Akonadi::TagFetchJob(this);
tagFetchJob->fetchScope().fetchAttribute<Akonadi::TagAttribute>();
connect(tagFetchJob, &Akonadi::TagFetchJob::result, this, &TagCache::onTagsFetched);
}
void TagCache::onTagsFetched(KJob *job)
{
if (job->error()) {
qCWarning(CALENDARSUPPORT_LOG) << "Failed to fetch tags: " << job->errorString();
return;
}
auto fetchJob = static_cast<Akonadi::TagFetchJob *>(job);
const Akonadi::Tag::List lst = fetchJob->tags();
for (const Akonadi::Tag &tag : lst) {
onTagAdded(tag);
}
}
/*
SPDX-FileCopyrightText: 2015 Sandro Knauß <knauss@kolabsys.com>
SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
*/
#pragma once
#include <Akonadi/Tag>
namespace Akonadi
{
class Monitor;
}
class KJob;
namespace CalendarSupport
{
/**
* A tag cache
*/
class TagCache : public QObject
{
Q_OBJECT
public:
TagCache();
Q_REQUIRED_RESULT Akonadi::Tag getTagByGid(const QByteArray &gid) const;
Q_REQUIRED_RESULT Akonadi::Tag getTagByName(const QString &name) const;
private Q_SLOTS:
void onTagAdded(const Akonadi::Tag &);
void onTagChanged(const Akonadi::Tag &);
void onTagRemoved(const Akonadi::Tag &);
void onTagsFetched(KJob *);
private:
void retrieveTags();
QHash<Akonadi::Tag::Id, Akonadi::Tag> mCache;
QHash<QByteArray, Akonadi::Tag::Id> mGidCache;
QHash<QString, Akonadi::Tag::Id> mNameCache;
Akonadi::Monitor *const mMonitor;
};
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment