Akonadi

tag.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Christian Mollekopf <[email protected]>
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 
15 using namespace Akonadi;
16 
17 const char Akonadi::Tag::PLAIN[] = "PLAIN";
18 const char Akonadi::Tag::GENERIC[] = "GENERIC";
19 
20 uint Akonadi::qHash(const Tag &tag)
21 {
22  return ::qHash(tag.id());
23 }
24 
25 Tag::Tag()
26  : d_ptr(new TagPrivate)
27 {
28 }
29 
30 Tag::Tag(Tag::Id id)
31  : d_ptr(new TagPrivate)
32 {
33  d_ptr->id = id;
34 }
35 
36 Tag::Tag(const QString &name)
37  : d_ptr(new TagPrivate)
38 {
39  d_ptr->gid = name.toUtf8();
40  d_ptr->type = PLAIN;
41 }
42 
43 Tag::Tag(const Tag &) = default;
44 Tag::Tag(Tag &&) noexcept = default;
45 Tag::~Tag() = default;
46 
47 Tag &Tag::operator=(const Tag &) = default;
48 Tag &Tag::operator=(Tag &&) noexcept = default;
49 
50 bool 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 
66 bool Tag::operator!=(const Tag &other) const
67 {
68  return !operator==(other);
69 }
70 
71 Tag Tag::fromUrl(const QUrl &url)
72 {
73  if (url.scheme() != QLatin1String("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 
87 QUrl Tag::url() const
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 
108 bool 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 
123 const Attribute *Tag::attribute(const QByteArray &type) const
124 {
125  return d_ptr->mAttributeStorage.attribute(type);
126 }
127 
128 Attribute *Tag::attribute(const QByteArray &type)
129 {
130  markAttributeModified(type);
131  return d_ptr->mAttributeStorage.attribute(type);
132 }
133 
134 void Tag::setId(Tag::Id identifier)
135 {
136  d_ptr->id = identifier;
137 }
138 
139 Tag::Id Tag::id() const
140 {
141  return d_ptr->id;
142 }
143 
144 void Tag::setGid(const QByteArray &gid)
145 {
146  d_ptr->gid = gid;
147 }
148 
149 QByteArray Tag::gid() const
150 {
151  return d_ptr->gid;
152 }
153 
154 void Tag::setRemoteId(const QByteArray &remoteId)
155 {
156  d_ptr->remoteId = remoteId;
157 }
158 
159 QByteArray Tag::remoteId() const
160 {
161  return d_ptr->remoteId;
162 }
163 
164 void 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 
172 QString 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 
179 void Tag::setParent(const Tag &parent)
180 {
181  d_ptr->parent.reset(new Tag(parent));
182 }
183 
184 Tag Tag::parent() const
185 {
186  if (!d_ptr->parent) {
187  return Tag();
188  }
189  return *d_ptr->parent;
190 }
191 
192 void Tag::setType(const QByteArray &type)
193 {
194  d_ptr->type = type;
195 }
196 
197 QByteArray Tag::type() const
198 {
199  return d_ptr->type;
200 }
201 
202 bool Tag::isValid() const
203 {
204  return d_ptr->id >= 0;
205 }
206 
207 bool Tag::isImmutable() const
208 {
209  return (d_ptr->type.isEmpty() || d_ptr->type == PLAIN);
210 }
211 
212 QDebug 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 
228 bool 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 
237 void Tag::markAttributeModified(const QByteArray &type)
238 {
239  d_ptr->mAttributeStorage.markAttributeModified(type);
240 }
QString number(int n, int base)
QString fromUtf8(const char *str, int size)
QString scheme() const const
An Akonadi Tag.
Definition: tag.h:25
QDebug & nospace()
qlonglong toLongLong(bool *ok, int base) const const
QDataStream & operator<<(QDataStream &out, const KDateTime &dateTime)
QUrl url() const
Returns the url of the tag.
Definition: tag.cpp:87
Provides interface for custom attributes for Entity.
Definition: attribute.h:131
Definition: item.h:29
static Tag genericTag(const QString &name)
Returns a GENERIC tag with the given name and a valid gid.
Definition: tag.cpp:219
static const char GENERIC[]
The GENERIC type has the following properties:
Definition: tag.h:49
const T * attribute() const
Returns the attribute of the requested type or 0 if it is not available.
Definition: tag.h:225
void setScheme(const QString &scheme)
static const char PLAIN[]
The PLAIN type has the following properties:
Definition: tag.h:39
@ AddIfMissing
Creates the attribute if it is missing.
Definition: tag.h:115
QByteArray mid(int pos, int len) const const
bool isEmpty() const const
QByteArray toUtf8() const const
bool hasAttribute() const
Returns whether the entity has an attribute of the requested type.
Definition: tag.h:246
QUuid createUuid()
void clearAttributes()
Removes and deletes all attributes of the entity.
Definition: tag.cpp:118
AKONADI_CALENDAR_EXPORT QString displayName(Akonadi::ETMCalendar *calendar, const Akonadi::Collection &collection)
void setQuery(const QString &query, QUrl::ParsingMode mode)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
void addAttribute(Attribute *attribute)
Adds an attribute to the entity.
Definition: tag.cpp:98
QByteArray toByteArray() const const
Id id() const
Returns the unique identifier of the tag.
Definition: tag.cpp:139
void setId(Id identifier)
Sets the unique identifier of the tag.
Definition: tag.cpp:134
QString name(StandardShortcut id)
bool isImmutable() const
Returns true if the tag is immutable (cannot be modified after creation).
Definition: tag.cpp:207
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const
void removeAttribute()
Removes and deletes the attribute of the requested type.
Definition: tag.h:239
Helper integration between Akonadi and Qt.
Attribute::List attributes() const
Returns a list of all attributes of the entity.
Definition: tag.cpp:113
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Sep 25 2023 03:49:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.