Akonadi

tagcache.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Sandro KnauƟ <knauss@kolabsys.com>
3 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "tagcache.h"
8#include "akonadicore_debug.h"
9
10#include <Akonadi/Monitor>
11#include <Akonadi/TagAttribute>
12#include <Akonadi/TagFetchJob>
13#include <Akonadi/TagFetchScope>
14#include <Akonadi/TagModifyJob>
15
16namespace Akonadi
17{
18class TagCachePrivate
19{
20public:
21 void addTag(const Akonadi::Tag &tag);
22 void removeTag(const Akonadi::Tag &tag);
23
27 Akonadi::Monitor mMonitor;
28};
29}
30
31using namespace Akonadi;
32
33TagCache::TagCache(QObject *parent)
34 : QObject(parent)
35 , d(new TagCachePrivate)
36{
37 d->mMonitor.setObjectName(QLatin1StringView("TagCacheMonitor"));
38 d->mMonitor.setTypeMonitored(Akonadi::Monitor::Tags);
39 d->mMonitor.tagFetchScope().fetchAttribute<Akonadi::TagAttribute>();
40 connect(&d->mMonitor, &Akonadi::Monitor::tagAdded, this, [this](const Akonadi::Tag &tag) {
41 d->addTag(tag);
42 });
43 connect(&d->mMonitor, &Akonadi::Monitor::tagChanged, this, [this](const Akonadi::Tag &tag) {
44 d->addTag(tag);
45 });
46 connect(&d->mMonitor, &Akonadi::Monitor::tagRemoved, this, [this](const Akonadi::Tag &tag) {
47 d->removeTag(tag);
48 });
49
50 auto tagFetchJob = new Akonadi::TagFetchJob(this);
51 tagFetchJob->fetchScope().fetchAttribute<Akonadi::TagAttribute>();
52 connect(tagFetchJob, &Akonadi::TagFetchJob::result, this, [tagFetchJob, this]() {
53 if (tagFetchJob->error()) {
54 qCWarning(AKONADICORE_LOG) << "Failed to fetch tags: " << tagFetchJob->errorString();
55 return;
56 }
57 const Akonadi::Tag::List lst = tagFetchJob->tags();
58 for (const Akonadi::Tag &tag : lst) {
59 d->addTag(tag);
60 }
61 });
62}
63
64TagCache::~TagCache() = default;
65
67{
68 return d->mCache.value(d->mGidCache.value(gid));
69}
70
72{
73 return d->mCache.value(d->mNameCache.value(name));
74}
75
76void TagCachePrivate::addTag(const Akonadi::Tag &tag)
77{
78 mCache.insert(tag.id(), tag);
79 mGidCache.insert(tag.gid(), tag.id());
80 mNameCache.insert(tag.name(), tag.id());
81}
82
83void TagCachePrivate::removeTag(const Akonadi::Tag &tag)
84{
85 mCache.remove(tag.id());
86 mGidCache.remove(tag.gid());
87 mNameCache.remove(tag.name());
88}
89
90QColor TagCache::tagColor(const QString &tagName) const
91{
92 if (tagName.isEmpty()) {
93 return {};
94 }
95
96 const auto tag = tagByName(tagName);
97 if (const auto attr = tag.attribute<Akonadi::TagAttribute>()) {
98 return attr->backgroundColor();
99 }
100
101 return {};
102}
103
104void TagCache::setTagColor(const QString &tagName, const QColor &color)
105{
106 Akonadi::Tag tag = tagByName(tagName);
107 if (!tag.isValid()) {
108 return;
109 }
110
112 attr->setBackgroundColor(color);
113 new Akonadi::TagModifyJob(tag);
114}
115
117{
118 static TagCache s_instance;
119 return &s_instance;
120}
121
122#include "moc_tagcache.cpp"
Monitors an item or collection for changes.
Definition monitor.h:71
void tagRemoved(const Akonadi::Tag &tag)
This signal is emitted if a monitored tag is removed from the server storage.
void tagChanged(const Akonadi::Tag &tag)
This signal is emitted if a monitored tag is changed on the server.
void tagAdded(const Akonadi::Tag &tag)
This signal is emitted if a tag has been added to Akonadi storage.
Attribute that stores the properties that are used to display a tag.
Client-side cache of all exist tags.
Definition tagcache.h:33
Akonadi::Tag tagByName(const QString &name) const
Returns the tag with the name name, if available.
Definition tagcache.cpp:71
QColor tagColor(const QString &tagName) const
Returns the (background) color of the tag named tagName.
Definition tagcache.cpp:90
static TagCache * instance()
Returns the singleton instance.
Definition tagcache.cpp:116
Akonadi::Tag tagByGid(const QByteArray &gid) const
Returns the tag with the GID gid, if available.
Definition tagcache.cpp:66
void setTagColor(const QString &tagName, const QColor &color)
Sets the (background) color of the tag named tagName to color.
Definition tagcache.cpp:104
Job that fetches tags from the Akonadi storage.
Definition tagfetchjob.h:29
Job that modifies a tag in the Akonadi storage.
An Akonadi Tag.
Definition tag.h:26
Id id() const
Returns the unique identifier of the tag.
Definition tag.cpp:139
@ AddIfMissing
Creates the attribute if it is missing.
Definition tag.h:115
const Attribute * attribute(const QByteArray &name) const
Returns the attribute of the given type name if available, 0 otherwise.
Definition tag.cpp:123
void result(KJob *job)
Helper integration between Akonadi and Qt.
iterator insert(const Key &key, const T &value)
bool remove(const Key &key)
void setObjectName(QAnyStringView name)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:58:37 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.