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 "tag_p.h"
9 #include "akonadicore_debug.h"
10 
11 #include "tagattribute.h"
12 #include <QUuid>
13 #include <QUrlQuery>
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
234  << ". Did you forget to call AttributeFactory::registerAttribute()?";
235  return false;
236 }
237 
238 void Tag::markAttributeModified(const QByteArray &type)
239 {
240  d_ptr->mAttributeStorage.markAttributeModified(type);
241 }
void removeAttribute()
Removes and deletes the attribute of the requested type.
Definition: tag.h:240
Attribute::List attributes() const
Returns a list of all attributes of the entity.
Definition: tag.cpp:113
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const
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
void clearAttributes()
Removes and deletes all attributes of the entity.
Definition: tag.cpp:118
bool isEmpty() const const
QDebug & nospace()
Id id() const
Returns the unique identifier of the tag.
Definition: tag.cpp:139
Provides interface for custom attributes for Entity.
Definition: attribute.h:126
void addQueryItem(const QString &key, const QString &value)
static const char GENERIC[]
The GENERIC type has the following properties:
Definition: tag.h:50
Creates the attribute if it is missing.
Definition: tag.h:116
QString number(int n, int base)
QString fromUtf8(const char *str, int size)
bool isImmutable() const
Returns true if the tag is immutable (cannot be modified after creation).
Definition: tag.cpp:207
void addAttribute(Attribute *attribute)
Adds an attribute to the entity.
Definition: tag.cpp:98
QUrl url() const
Returns the url of the tag.
Definition: tag.cpp:87
bool isEmpty() const const
void setScheme(const QString &scheme)
static const char PLAIN[]
The PLAIN type has the following properties:
Definition: tag.h:40
QByteArray mid(int pos, int len) const const
QString scheme() const const
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
QByteArray toByteArray() const const
Helper integration between Akonadi and Qt.
An Akonadi Tag.
Definition: tag.h:26
void setQuery(const QString &query, QUrl::ParsingMode mode)
const T * attribute() const
Returns the attribute of the requested type or 0 if it is not available.
Definition: tag.h:226
bool hasAttribute() const
Returns whether the entity has an attribute of the requested type.
Definition: tag.h:247
qlonglong toLongLong(bool *ok, int base) const const
QUuid createUuid()
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Nov 21 2020 23:17:06 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.