Akonadi

tag.h
1 /*
2  SPDX-FileCopyrightText: 2014 Christian Mollekopf <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef AKONADI_TAG_H
8 #define AKONADI_TAG_H
9 
10 #include "akonadicore_export.h"
11 #include "attribute.h"
12 
13 #include <QString>
14 #include <QSharedPointer>
15 #include <QUrl>
16 #include <QDebug>
17 
18 namespace Akonadi
19 {
20 class TagModifyJob;
21 class TagPrivate;
22 
26 class AKONADICORE_EXPORT Tag
27 {
28 public:
29  typedef QVector<Tag> List;
30  typedef qint64 Id;
31 
40  static const char PLAIN[];
41 
50  static const char GENERIC[];
51 
52  Tag();
53  explicit Tag(Id id);
57  explicit Tag(const QString &name);
58 
59  Tag(const Tag &);
60 
61  Tag(Tag &&) noexcept;
62 
63  ~Tag();
64 
65  Tag &operator=(const Tag &);
66  Tag &operator=(Tag &&) noexcept;
67  //Avoid slicing
68  bool operator==(const Tag &) const;
69  bool operator!=(const Tag &) const;
70 
71  static Tag fromUrl(const QUrl &url);
72 
83  void addAttribute(Attribute *attribute);
84 
88  void removeAttribute(const QByteArray &name);
89 
94  bool hasAttribute(const QByteArray &name) const;
95 
99  Attribute::List attributes() const;
100 
104  void clearAttributes();
105 
109  const Attribute *attribute(const QByteArray &name) const;
110  Attribute *attribute(const QByteArray &name);
111 
117  DontCreate
118  };
119 
127  template <typename T>
128  inline T *attribute(CreateOption option = DontCreate);
129 
133  template <typename T>
134  inline const T *attribute() const;
135 
139  template <typename T>
140  inline void removeAttribute();
141 
145  template <typename T>
146  inline bool hasAttribute() const;
147 
151  QUrl url() const;
152 
156  void setId(Id identifier);
157 
161  Id id() const;
162 
163  void setGid(const QByteArray &gid);
164  QByteArray gid() const;
165 
166  void setRemoteId(const QByteArray &remoteId);
167  QByteArray remoteId() const;
168 
169  void setType(const QByteArray &type);
170  QByteArray type() const;
171 
172  void setName(const QString &name);
173  QString name() const;
174 
175  void setParent(const Tag &parent);
176  Tag parent() const;
177 
178  bool isValid() const;
179 
184  bool isImmutable() const;
185 
189  static Tag genericTag(const QString &name);
190 
191 private:
192  bool checkAttribute(const Attribute *attr, const QByteArray &type) const;
193  void markAttributeModified(const QByteArray &type);
194 
195  //@cond PRIVATE
196  friend class TagModifyJob;
197  friend class TagFetchJob;
198  friend class ProtocolHelper;
199 
201  //@endcond
202 };
203 
204 AKONADICORE_EXPORT uint qHash(const Akonadi::Tag &);
205 
206 template <typename T>
207 inline T *Tag::attribute(CreateOption option)
208 {
209  const QByteArray type = T().type();
210  markAttributeModified(type);
211  if (hasAttribute(type)) {
212  T *attr = dynamic_cast<T *>(attribute(type));
213  if (checkAttribute(attr, type)) {
214  return attr;
215  }
216  } else if (option == AddIfMissing) {
217  T *attr = new T();
218  addAttribute(attr);
219  return attr;
220  }
221 
222  return nullptr;
223 }
224 
225 template <typename T>
226 inline const T *Tag::attribute() const
227 {
228  const QByteArray type = T().type();
229  if (hasAttribute(type)) {
230  const T *attr = dynamic_cast<const T *>(attribute(type));
231  if (checkAttribute(attr, type)) {
232  return attr;
233  }
234  }
235 
236  return nullptr;
237 }
238 
239 template <typename T>
240 inline void Tag::removeAttribute()
241 {
242  const T dummy;
243  removeAttribute(dummy.type());
244 }
245 
246 template <typename T>
247 inline bool Tag::hasAttribute() const
248 {
249  const T dummy;
250  return hasAttribute(dummy.type());
251 }
252 
253 } // namespace Akonadi
254 
255 AKONADICORE_EXPORT QDebug operator<<(QDebug debug, const Akonadi::Tag &tag);
256 
257 Q_DECLARE_METATYPE(Akonadi::Tag)
258 Q_DECLARE_METATYPE(Akonadi::Tag::List)
259 Q_DECLARE_METATYPE(QSet<Akonadi::Tag>)
260 Q_DECLARE_TYPEINFO(Akonadi::Tag, Q_MOVABLE_TYPE);
261 
262 #endif
void removeAttribute()
Removes and deletes the attribute of the requested type.
Definition: tag.h:240
CreateOption
Describes the options that can be passed to access attributes.
Definition: tag.h:115
Provides interface for custom attributes for Entity.
Definition: attribute.h:126
Creates the attribute if it is missing.
Definition: tag.h:116
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
Helper integration between Akonadi and Qt.
Job that modifies a tag in the Akonadi storage.
Definition: tagmodifyjob.h:23
An Akonadi Tag.
Definition: tag.h:26
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
Job that fetches tags from the Akonadi storage.
Definition: tagfetchjob.h:30
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jul 10 2020 23:14:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.