Akonadi

tag.cpp
1 /*
2  Copyright (c) 2014 Christian Mollekopf <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "tag.h"
21 #include "tag_p.h"
22 #include "akonadicore_debug.h"
23 
24 #include "tagattribute.h"
25 #include <QUuid>
26 #include <QUrlQuery>
27 
28 using namespace Akonadi;
29 
30 const char Akonadi::Tag::PLAIN[] = "PLAIN";
31 const char Akonadi::Tag::GENERIC[] = "GENERIC";
32 
33 uint Akonadi::qHash(const Tag &tag)
34 {
35  return ::qHash(tag.id());
36 }
37 
38 Tag::Tag()
39  : d_ptr(new TagPrivate)
40 {
41 }
42 
43 Tag::Tag(Tag::Id id)
44  : d_ptr(new TagPrivate)
45 {
46  d_ptr->id = id;
47 }
48 
49 Tag::Tag(const QString &name)
50  : d_ptr(new TagPrivate)
51 {
52  d_ptr->gid = name.toUtf8();
53  d_ptr->type = PLAIN;
54 }
55 
56 Tag::Tag(const Tag &) = default;
57 Tag::Tag(Tag &&) noexcept = default;
58 Tag::~Tag() = default;
59 
60 Tag &Tag::operator=(const Tag &) = default;
61 Tag &Tag::operator=(Tag &&) noexcept = default;
62 
63 bool Tag::operator==(const Tag &other) const
64 {
65  // Valid tags are equal if their IDs are equal
66  if (isValid() && other.isValid()) {
67  return d_ptr->id == other.d_ptr->id;
68  }
69 
70  // Invalid tags are equal if their GIDs are non empty but equal
71  if (!d_ptr->gid.isEmpty() || !other.d_ptr->gid.isEmpty()) {
72  return d_ptr->gid == other.d_ptr->gid;
73  }
74 
75  // Invalid tags are equal if both are invalid
76  return !isValid() && !other.isValid();
77 }
78 
79 bool Tag::operator!=(const Tag &other) const
80 {
81  return !operator==(other);
82 }
83 
84 Tag Tag::fromUrl(const QUrl &url)
85 {
86  if (url.scheme() != QLatin1String("akonadi")) {
87  return Tag();
88  }
89 
90  const QString tagStr = QUrlQuery(url).queryItemValue(QStringLiteral("tag"));
91  bool ok = false;
92  Tag::Id itemId = tagStr.toLongLong(&ok);
93  if (!ok) {
94  return Tag();
95  }
96 
97  return Tag(itemId);
98 }
99 
100 QUrl Tag::url() const
101 {
102  QUrlQuery query;
103  query.addQueryItem(QStringLiteral("tag"), QString::number(id()));
104 
105  QUrl url;
106  url.setScheme(QStringLiteral("akonadi"));
107  url.setQuery(query);
108  return url;
109 }
110 
112 {
113  d_ptr->mAttributeStorage.addAttribute(attr);
114 }
115 
117 {
118  d_ptr->mAttributeStorage.removeAttribute(type);
119 }
120 
121 bool Tag::hasAttribute(const QByteArray &type) const
122 {
123  return d_ptr->mAttributeStorage.hasAttribute(type);
124 }
125 
127 {
128  return d_ptr->mAttributeStorage.attributes();
129 }
130 
132 {
133  d_ptr->mAttributeStorage.clearAttributes();
134 }
135 
136 const Attribute *Tag::attribute(const QByteArray &type) const
137 {
138  return d_ptr->mAttributeStorage.attribute(type);
139 }
140 
141 Attribute *Tag::attribute(const QByteArray &type)
142 {
143  markAttributeModified(type);
144  return d_ptr->mAttributeStorage.attribute(type);
145 }
146 
147 void Tag::setId(Tag::Id identifier)
148 {
149  d_ptr->id = identifier;
150 }
151 
152 Tag::Id Tag::id() const
153 {
154  return d_ptr->id;
155 }
156 
157 void Tag::setGid(const QByteArray &gid)
158 {
159  d_ptr->gid = gid;
160 }
161 
162 QByteArray Tag::gid() const
163 {
164  return d_ptr->gid;
165 }
166 
167 void Tag::setRemoteId(const QByteArray &remoteId)
168 {
169  d_ptr->remoteId = remoteId;
170 }
171 
172 QByteArray Tag::remoteId() const
173 {
174  return d_ptr->remoteId;
175 }
176 
177 void Tag::setName(const QString &name)
178 {
179  if (!name.isEmpty()) {
180  TagAttribute *const attr = attribute<TagAttribute>(Tag::AddIfMissing);
181  attr->setDisplayName(name);
182  }
183 }
184 
185 QString Tag::name() const
186 {
187  const TagAttribute *const attr = attribute<TagAttribute>();
188  const QString displayName = attr ? attr->displayName() : QString();
189  return !displayName.isEmpty() ? displayName : QString::fromUtf8(d_ptr->gid);
190 }
191 
192 void Tag::setParent(const Tag &parent)
193 {
194  d_ptr->parent.reset(new Tag(parent));
195 }
196 
197 Tag Tag::parent() const
198 {
199  if (!d_ptr->parent) {
200  return Tag();
201  }
202  return *d_ptr->parent;
203 }
204 
205 void Tag::setType(const QByteArray &type)
206 {
207  d_ptr->type = type;
208 }
209 
210 QByteArray Tag::type() const
211 {
212  return d_ptr->type;
213 }
214 
215 bool Tag::isValid() const
216 {
217  return d_ptr->id >= 0;
218 }
219 
220 bool Tag::isImmutable() const
221 {
222  return (d_ptr->type.isEmpty() || d_ptr->type == PLAIN);
223 }
224 
225 QDebug operator<<(QDebug debug, const Tag &tag)
226 {
227  QDebugStateSaver saver(debug);
228  debug.nospace() << "Akonadi::Tag(ID " << tag.id() << ", GID " << tag.gid() << ", parent tag ID " << tag.parent().id() << ")";
229  return debug;
230 }
231 
233 {
234  Tag tag;
235  tag.d_ptr->type = GENERIC;
236  tag.d_ptr->gid = QUuid::createUuid().toByteArray().mid(1, 36);
237  tag.setName(name);
238  return tag;
239 }
240 
241 bool Tag::checkAttribute(const Attribute *attr, const QByteArray &type) const
242 {
243  if (attr) {
244  return true;
245  }
246  qCWarning(AKONADICORE_LOG) << "Found attribute of unknown type" << type
247  << ". Did you forget to call AttributeFactory::registerAttribute()?";
248  return false;
249 }
250 
251 void Tag::markAttributeModified(const QByteArray &type)
252 {
253  d_ptr->mAttributeStorage.markAttributeModified(type);
254 }
void removeAttribute()
Removes and deletes the attribute of the requested type.
Definition: tag.h:253
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Attribute::List attributes() const
Returns a list of all attributes of the entity.
Definition: tag.cpp:126
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const
void setId(Id identifier)
Sets the unique identifier of the tag.
Definition: tag.cpp:147
static Tag genericTag(const QString &name)
Returns a GENERIC tag with the given name and a valid gid.
Definition: tag.cpp:232
void clearAttributes()
Removes and deletes all attributes of the entity.
Definition: tag.cpp:131
bool isEmpty() const const
QDebug & nospace()
Id id() const
Returns the unique identifier of the tag.
Definition: tag.cpp:152
void setDisplayName(const QString &name)
Sets the name that should be used for display.
Provides interface for custom attributes for Entity.
Definition: attribute.h:139
void addQueryItem(const QString &key, const QString &value)
static const char GENERIC[]
The GENERIC type has the following properties:
Definition: tag.h:63
Creates the attribute if it is missing.
Definition: tag.h:129
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:220
void addAttribute(Attribute *attribute)
Adds an attribute to the entity.
Definition: tag.cpp:111
QUrl url() const
Returns the url of the tag.
Definition: tag.cpp:100
bool isEmpty() const const
void setScheme(const QString &scheme)
static const char PLAIN[]
The PLAIN type has the following properties:
Definition: tag.h:53
QByteArray mid(int pos, int len) const const
QString scheme() const const
QString displayName() const
Returns the name that should be used for display.
Attribute that stores the properties that are used to display a tag.
Definition: tagattribute.h:37
QByteArray toByteArray() const const
Helper integration between Akonadi and Qt.
An Akonadi Tag.
Definition: tag.h:39
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:239
bool hasAttribute() const
Returns whether the entity has an attribute of the requested type.
Definition: tag.h:260
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 Sun May 24 2020 22:46:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.