Akonadi

tag.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "tag.h"
8#include "akonadicore_debug.h"
9#include "tag_p.h"
10
11#include "tagattribute.h"
12#include <QUrlQuery>
13#include <QUuid>
14
15using namespace Akonadi;
16
17const char Akonadi::Tag::PLAIN[] = "PLAIN";
18const char Akonadi::Tag::GENERIC[] = "GENERIC";
19
20size_t Akonadi::qHash(const Tag &tag, size_t seed) noexcept
21{
22 return ::qHash(tag.id(), seed);
23}
24
25Tag::Tag()
26 : d_ptr(new TagPrivate)
27{
28}
29
30Tag::Tag(Tag::Id id)
31 : d_ptr(new TagPrivate)
32{
33 d_ptr->id = id;
34}
35
36Tag::Tag(const QString &name)
37 : d_ptr(new TagPrivate)
38{
39 d_ptr->gid = name.toUtf8();
40 d_ptr->type = PLAIN;
41}
42
43Tag::Tag(const Tag &) = default;
44Tag::Tag(Tag &&) noexcept = default;
45Tag::~Tag() = default;
46
47Tag &Tag::operator=(const Tag &) = default;
48Tag &Tag::operator=(Tag &&) noexcept = default;
49
50bool Tag::operator==(const Tag &other) const
51{
52 // Valid tags are equal if their IDs are equal
53 if (isValid() && other.isValid()) {
54 return d_ptr->id == other.d_ptr->id;
55 }
56
57 // Invalid tags are equal if their GIDs are non empty but equal
58 if (!d_ptr->gid.isEmpty() || !other.d_ptr->gid.isEmpty()) {
59 return d_ptr->gid == other.d_ptr->gid;
60 }
61
62 // Invalid tags are equal if both are invalid
63 return !isValid() && !other.isValid();
64}
65
66bool Tag::operator!=(const Tag &other) const
67{
68 return !operator==(other);
69}
70
71Tag Tag::fromUrl(const QUrl &url)
72{
73 if (url.scheme() != QLatin1StringView("akonadi")) {
74 return Tag();
75 }
76
77 const QString tagStr = QUrlQuery(url).queryItemValue(QStringLiteral("tag"));
78 bool ok = false;
79 Tag::Id itemId = tagStr.toLongLong(&ok);
80 if (!ok) {
81 return Tag();
82 }
83
84 return Tag(itemId);
85}
86
88{
89 QUrlQuery query;
90 query.addQueryItem(QStringLiteral("tag"), QString::number(id()));
91
92 QUrl url;
93 url.setScheme(QStringLiteral("akonadi"));
94 url.setQuery(query);
95 return url;
96}
97
99{
100 d_ptr->mAttributeStorage.addAttribute(attr);
101}
102
104{
105 d_ptr->mAttributeStorage.removeAttribute(type);
106}
107
108bool Tag::hasAttribute(const QByteArray &type) const
109{
110 return d_ptr->mAttributeStorage.hasAttribute(type);
111}
112
114{
115 return d_ptr->mAttributeStorage.attributes();
116}
117
119{
120 d_ptr->mAttributeStorage.clearAttributes();
121}
122
123const Attribute *Tag::attribute(const QByteArray &type) const
124{
125 return d_ptr->mAttributeStorage.attribute(type);
126}
127
129{
130 markAttributeModified(type);
131 return d_ptr->mAttributeStorage.attribute(type);
132}
133
134void Tag::setId(Tag::Id identifier)
135{
136 d_ptr->id = identifier;
137}
138
139Tag::Id Tag::id() const
140{
141 return d_ptr->id;
142}
143
144void Tag::setGid(const QByteArray &gid)
145{
146 d_ptr->gid = gid;
147}
148
149QByteArray Tag::gid() const
150{
151 return d_ptr->gid;
152}
153
154void Tag::setRemoteId(const QByteArray &remoteId)
155{
156 d_ptr->remoteId = remoteId;
157}
158
159QByteArray Tag::remoteId() const
160{
161 return d_ptr->remoteId;
162}
163
164void Tag::setName(const QString &name)
165{
166 if (!name.isEmpty()) {
167 auto *const attr = attribute<TagAttribute>(Tag::AddIfMissing);
168 attr->setDisplayName(name);
169 }
170}
171
172QString Tag::name() const
173{
174 const auto *const attr = attribute<TagAttribute>();
175 const QString displayName = attr ? attr->displayName() : QString();
176 return !displayName.isEmpty() ? displayName : QString::fromUtf8(d_ptr->gid);
177}
178
179void Tag::setParent(const Tag &parent)
180{
181 d_ptr->parent.reset(new Tag(parent));
182}
183
184Tag Tag::parent() const
185{
186 if (!d_ptr->parent) {
187 return Tag();
188 }
189 return *d_ptr->parent;
190}
191
192void Tag::setType(const QByteArray &type)
193{
194 d_ptr->type = type;
195}
196
197QByteArray Tag::type() const
198{
199 return d_ptr->type;
200}
201
202bool Tag::isValid() const
203{
204 return d_ptr->id >= 0;
205}
206
208{
209 return (d_ptr->type.isEmpty() || d_ptr->type == PLAIN);
210}
211
212QDebug operator<<(QDebug debug, const Tag &tag)
213{
214 QDebugStateSaver saver(debug);
215 debug.nospace() << "Akonadi::Tag(ID " << tag.id() << ", GID " << tag.gid() << ", parent tag ID " << tag.parent().id() << ")";
216 return debug;
217}
218
220{
221 Tag tag;
222 tag.d_ptr->type = GENERIC;
223 tag.d_ptr->gid = QUuid::createUuid().toByteArray().mid(1, 36);
224 tag.setName(name);
225 return tag;
226}
227
228bool Tag::checkAttribute(const Attribute *attr, const QByteArray &type) const
229{
230 if (attr) {
231 return true;
232 }
233 qCWarning(AKONADICORE_LOG) << "Found attribute of unknown type" << type << ". Did you forget to call AttributeFactory::registerAttribute()?";
234 return false;
235}
236
237void Tag::markAttributeModified(const QByteArray &type)
238{
239 d_ptr->mAttributeStorage.markAttributeModified(type);
240}
Provides interface for custom attributes for Entity.
Definition attribute.h:132
An Akonadi Tag.
Definition tag.h:26
Id id() const
Returns the unique identifier of the tag.
Definition tag.cpp:139
bool isImmutable() const
Returns true if the tag is immutable (cannot be modified after creation).
Definition tag.cpp:207
void removeAttribute()
Removes and deletes the attribute of the requested type.
Definition tag.h:239
static const char GENERIC[]
The GENERIC type has the following properties:
Definition tag.h:49
void addAttribute(Attribute *attribute)
Adds an attribute to the entity.
Definition tag.cpp:98
@ AddIfMissing
Creates the attribute if it is missing.
Definition tag.h:115
const T * attribute() const
Returns the attribute of the requested type or 0 if it is not available.
Definition tag.h:225
static const char PLAIN[]
The PLAIN type has the following properties:
Definition tag.h:39
void clearAttributes()
Removes and deletes all attributes of the entity.
Definition tag.cpp:118
Attribute::List attributes() const
Returns a list of all attributes of the entity.
Definition tag.cpp:113
QUrl url() const
Returns the url of the tag.
Definition tag.cpp:87
bool hasAttribute() const
Returns whether the entity has an attribute of the requested type.
Definition tag.h:246
void setId(Id identifier)
Sets the unique identifier of the tag.
Definition tag.cpp:134
static Tag genericTag(const QString &name)
Returns a GENERIC tag with the given name and a valid gid.
Definition tag.cpp:219
AKONADI_CALENDAR_EXPORT QString displayName(Akonadi::ETMCalendar *calendar, const Akonadi::Collection &collection)
Helper integration between Akonadi and Qt.
QString name(GameStandardAction id)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QByteArray mid(qsizetype pos, qsizetype len) const const
QDebug & nospace()
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QString number(double n, char format, int precision)
qlonglong toLongLong(bool *ok, int base) const const
QByteArray toUtf8() const const
QString scheme() const const
void setQuery(const QString &query, ParsingMode mode)
void setScheme(const QString &scheme)
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const
QUuid createUuid()
QByteArray toByteArray(StringFormat mode) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:31:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.