Akonadi

collection.cpp
1 /*
2  Copyright (c) 2006 - 2007 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "collection.h"
8 #include "collection_p.h"
9 
10 #include "attributefactory.h"
11 #include "cachepolicy.h"
12 #include "collectionrightsattribute_p.h"
13 #include "collectionstatistics.h"
14 #include "entitydisplayattribute.h"
15 
16 
17 #include <QDebug>
18 #include <QHash>
19 #include <QString>
20 #include <QStringList>
21 
22 #include <QUrl>
23 #include <QUrlQuery>
24 
25 using namespace Akonadi;
26 
27 Q_GLOBAL_STATIC(Akonadi::Collection, s_defaultParentCollection) // NOLINT(readability-redundant-member-init)
28 
29 uint Akonadi::qHash(const Akonadi::Collection &collection)
30 {
31  return ::qHash(collection.id());
32 }
33 
37 static void assignCollectionPrivate(QSharedDataPointer<CollectionPrivate> &one,
39 {
40  // We can't simply do one = other here, we have to use a temp.
41  // Otherwise ProtocolHelperTest::testParentCollectionAfterCollectionParsing()
42  // will break.
43  //
44  // The reason are assignments like
45  // col = col.parentCollection()
46  //
47  // Here, parentCollection() actually returns a reference to a pointer owned
48  // by col. So when col (or rather, it's private class) is deleted, the pointer
49  // to the parent collection and therefore the reference becomes invalid.
50  //
51  // With a single-line assignment here, the parent collection would be deleted
52  // before it is assigned, and therefore the resulting object would point to
53  // uninitalized memory.
54  const QSharedDataPointer<CollectionPrivate> temp = other; // NOLINT(performance-unnecessary-copy-initialization): see above
55  one = temp;
56 }
57 
58 class CollectionRoot : public Collection
59 {
60 public:
61  CollectionRoot()
62  : Collection(0)
63  {
64  setContentMimeTypes({ Collection::mimeType() });
65 
66  // The root collection is read-only for the users
67  setRights(Collection::ReadOnly);
68  }
69 };
70 
71 Q_GLOBAL_STATIC(CollectionRoot, s_root) // NOLINT(readability-redundant-member-init)
72 
74  : d_ptr(new CollectionPrivate)
75 {
76  static int lastId = -1;
77  d_ptr->mId = lastId--;
78 }
79 
81  : d_ptr(new CollectionPrivate(id))
82 {
83 }
84 
86 {
87  assignCollectionPrivate(d_ptr, other.d_ptr);
88 }
89 
90 Collection::Collection(Collection &&) noexcept = default;
91 
92 Collection::~Collection() = default;
93 
95 {
96  d_ptr->mId = identifier;
97 }
98 
100 {
101  return d_ptr->mId;
102 }
103 
105 {
106  d_ptr->mRemoteId = id;
107 }
108 
110 {
111  return d_ptr->mRemoteId;
112 }
113 
115 {
116  d_ptr->mRemoteRevision = revision;
117 }
118 
120 {
121  return d_ptr->mRemoteRevision;
122 }
123 
125 {
126  return (d_ptr->mId >= 0);
127 }
128 
129 bool Collection::operator==(const Collection &other) const
130 {
131  // Invalid collections are the same, no matter what their internal ID is
132  return (!isValid() && !other.isValid()) || (d_ptr->mId == other.d_ptr->mId);
133 }
134 
136 {
137  return (isValid() || other.isValid()) && (d_ptr->mId != other.d_ptr->mId);
138 }
139 
141 {
142  if (this != &other) {
143  assignCollectionPrivate(d_ptr, other.d_ptr);
144  }
145 
146  return *this;
147 }
148 
150 {
151  return d_ptr->mId < other.d_ptr->mId;
152 }
153 
155 {
156  d_ptr->mAttributeStorage.addAttribute(attr);
157 }
158 
160 {
161  d_ptr->mAttributeStorage.removeAttribute(type);
162 }
163 
164 bool Collection::hasAttribute(const QByteArray &type) const
165 {
166  return d_ptr->mAttributeStorage.hasAttribute(type);
167 }
168 
170 {
171  return d_ptr->mAttributeStorage.attributes();
172 }
173 
175 {
176  d_ptr->mAttributeStorage.clearAttributes();
177 }
178 
180 {
181  markAttributeModified(type);
182  return d_ptr->mAttributeStorage.attribute(type);
183 }
184 
185 const Attribute *Collection::attribute(const QByteArray &type) const
186 {
187  return d_ptr->mAttributeStorage.attribute(type);
188 }
189 
191 {
192  if (!d_ptr->mParent) {
193  d_ptr->mParent.reset(new Collection());
194  }
195  return *d_ptr->mParent;
196 }
197 
199 {
200  if (!d_ptr->mParent) {
201  return *(s_defaultParentCollection);
202  } else {
203  return *d_ptr->mParent;
204  }
205 }
206 
208 {
209  d_ptr->mParent.reset(new Collection(parent));
210 }
211 
213 {
214  return d_ptr->name;
215 }
216 
218 {
219  const EntityDisplayAttribute *const attr = attribute<EntityDisplayAttribute>();
220  const QString displayName = attr ? attr->displayName() : QString();
221  return !displayName.isEmpty() ? displayName : d_ptr->name;
222 }
223 
225 {
226  d_ptr->name = name;
227 }
228 
230 {
231  if (const auto *const attr = attribute<CollectionRightsAttribute>()) {
232  return attr->rights();
233  } else {
234  return AllRights;
235  }
236 }
237 
239 {
240  attribute<CollectionRightsAttribute>(AddIfMissing)->setRights(rights);
241 }
242 
244 {
245  return d_ptr->contentTypes;
246 }
247 
249 {
250  if (d_ptr->contentTypes != types) {
251  d_ptr->contentTypes = types;
252  d_ptr->contentTypesChanged = true;
253  }
254 }
255 
257 {
258  QUrlQuery query;
259  query.addQueryItem(QStringLiteral("collection"), QString::number(id()));
260  if (type == UrlWithName) {
261  query.addQueryItem(QStringLiteral("name"), name());
262  }
263 
264  QUrl url;
265  url.setScheme(QStringLiteral("akonadi"));
266  url.setQuery(query);
267  return url;
268 }
269 
271 {
272  if (url.scheme() != QLatin1String("akonadi")) {
273  return Collection();
274  }
275 
276  const QString colStr = QUrlQuery(url).queryItemValue(QStringLiteral("collection"));
277  bool ok = false;
278  Collection::Id colId = colStr.toLongLong(&ok);
279  if (!ok) {
280  return Collection();
281  }
282 
283  if (colId == 0) {
284  return Collection::root();
285  }
286 
287  return Collection(colId);
288 }
289 
291 {
292  return *s_root;
293 }
294 
296 {
297  return QStringLiteral("inode/directory");
298 }
299 
301 {
302  return QStringLiteral("application/x-vnd.akonadi.collection.virtual");
303 }
304 
306 {
307  return d_ptr->resource;
308 }
309 
311 {
312  d_ptr->resource = resource;
313 }
314 
315 QDebug operator <<(QDebug d, const Akonadi::Collection &collection)
316 {
317  return d << "Collection ID:" << collection.id()
318  << " remote ID:" << collection.remoteId() << '\n'
319  << " name:" << collection.name() << '\n'
320  << " url:" << collection.url() << '\n'
321  << " parent:" << collection.parentCollection().id() << collection.parentCollection().remoteId() << '\n'
322  << " resource:" << collection.resource() << '\n'
323  << " rights:" << collection.rights() << '\n'
324  << " contents mime type:" << collection.contentMimeTypes() << '\n'
325  << " isVirtual:" << collection.isVirtual() << '\n'
326  << " " << collection.cachePolicy() << '\n'
327  << " " << collection.statistics();
328 }
329 
331 {
332  return d_ptr->statistics;
333 }
334 
336 {
337  d_ptr->statistics = statistics;
338 }
339 
341 {
342  return d_ptr->cachePolicy;
343 }
344 
346 {
347  d_ptr->cachePolicy = cachePolicy;
348  d_ptr->cachePolicyChanged = true;
349 }
350 
352 {
353  return d_ptr->isVirtual;
354 }
355 
357 {
358  d_ptr->isVirtual = isVirtual;
359 }
360 
362 {
363  d_ptr->enabledChanged = true;
364  d_ptr->enabled = enabled;
365 }
366 
368 {
369  return d_ptr->enabled;
370 }
371 
373 {
374  switch (purpose) {
375  case ListDisplay:
376  d_ptr->displayPreference = preference;
377  break;
378  case ListSync:
379  d_ptr->syncPreference = preference;
380  break;
381  case ListIndex:
382  d_ptr->indexPreference = preference;
383  break;
384  }
385  d_ptr->listPreferenceChanged = true;
386 }
387 
389 {
390  switch (purpose) {
391  case ListDisplay:
392  return d_ptr->displayPreference;
393  case ListSync:
394  return d_ptr->syncPreference;
395  case ListIndex:
396  return d_ptr->indexPreference;
397  }
398  return ListDefault;
399 }
400 
402 {
403  if (localListPreference(purpose) == ListDefault) {
404  return enabled();
405  }
406  return (localListPreference(purpose) == ListEnabled);
407 }
408 
409 void Collection::setShouldList(ListPurpose purpose, bool list)
410 {
411  if (localListPreference(purpose) == ListDefault) {
412  setEnabled(list);
413  } else {
415  }
416 }
417 
419 {
420  d_ptr->keepLocalChanges = parts;
421 }
422 
424 {
425  return d_ptr->keepLocalChanges;
426 }
427 
428 void Collection::markAttributeModified(const QByteArray &type)
429 {
430  d_ptr->mAttributeStorage.markAttributeModified(type);
431 }
ListPreference
Describes the list preference value.
Definition: collection.h:457
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const
bool isValid() const
Returns whether the collection is valid.
Definition: collection.cpp:124
QString name() const
Returns the i18n&#39;ed name of the collection.
Definition: collection.cpp:212
QString displayName() const
Returns the display name (EntityDisplayAttribute::displayName()) if set, and Collection::name() other...
Definition: collection.cpp:217
void removeAttribute()
Removes and deletes the attribute of the requested type.
Definition: collection.h:578
Provides statistics information of a Collection.
bool shouldList(ListPurpose purpose) const
Returns whether the collection should be listed or not for the specified purpose Takes enabled state ...
Definition: collection.cpp:401
QString displayName() const
Returns the name that should be used for display.
Has all rights on this storage collection.
Definition: collection.h:89
Represents a collection of PIM items.
Definition: collection.h:63
qint64 Id
Describes the unique id type.
Definition: collection.h:69
void setId(Id identifier)
Sets the unique identifier of the collection.
Definition: collection.cpp:94
static QString virtualMimeType()
Returns the mimetype used for virtual collections.
Definition: collection.cpp:300
Listing for display to the user.
Definition: collection.h:470
Creates the attribute if it is missing.
Definition: collection.h:270
static QString mimeType()
Returns the mimetype used for collections.
Definition: collection.cpp:295
bool operator==(const Collection &other) const
Returns whether this collections&#39;s id equals the id of the other collection.
Definition: collection.cpp:129
void setRemoteId(const QString &id)
Sets the remote id of the collection.
Definition: collection.cpp:104
Attribute::List attributes() const
Returns a list of all attributes of the collection.
Definition: collection.cpp:169
void clearAttributes()
Removes and deletes all attributes of the collection.
Definition: collection.cpp:174
Listing for indexing the content.
Definition: collection.h:471
Provides interface for custom attributes for Entity.
Definition: attribute.h:126
void setName(const QString &name)
Sets the i18n&#39;ed name of the collection.
Definition: collection.cpp:224
void setParentCollection(const Collection &parent)
Set the parent collection of this object.
Definition: collection.cpp:207
Collection()
Creates an invalid collection.
QSet< QByteArray > keepLocalChanges() const
Returns what parts are only default values.
Definition: collection.cpp:423
void setRights(Rights rights)
Sets the rights the user has on the collection.
Definition: collection.cpp:238
void setEnabled(bool enabled)
Sets the collection&#39;s enabled state.
Definition: collection.cpp:361
Can only read items or subcollection of this collection.
Definition: collection.h:80
void setVirtual(bool isVirtual)
Sets whether the collection is virtual or not.
Definition: collection.cpp:356
void addQueryItem(const QString &key, const QString &value)
QUrl url(UrlType type=UrlShort) const
Returns the url of the collection.
Definition: collection.cpp:256
Collection & operator=(const Collection &other)
Assigns the other to this collection and returns a reference to this collection.
Definition: collection.cpp:140
Disable collection for specified purpose.
Definition: collection.h:459
QString number(int n, int base)
bool hasAttribute() const
Returns whether the collection has an attribute of the requested type.
Definition: collection.h:584
A url with identifier and name.
Definition: collection.h:399
void setStatistics(const CollectionStatistics &statistics)
Sets the collection statistics for the collection.
Definition: collection.cpp:335
Enable collection for specified purpose.
Definition: collection.h:458
void setRemoteRevision(const QString &revision)
Sets the remote revision of the collection.
Definition: collection.cpp:114
void addAttribute(Attribute *attribute)
Adds an attribute to the collection.
Definition: collection.cpp:154
ListPurpose
Describes the purpose of the listing.
Definition: collection.h:468
UrlType
Describes the type of url which is returned in url().
Definition: collection.h:397
void setScheme(const QString &scheme)
static Collection root()
Returns the root collection.
Definition: collection.cpp:290
QString remoteRevision() const
Returns the remote revision of the collection.
Definition: collection.cpp:119
Collection parentCollection() const
Returns the parent collection of this object.
Definition: collection.cpp:198
Represents the caching policy for a collection.
Definition: cachepolicy.h:59
QString scheme() const const
void setCachePolicy(const CachePolicy &policy)
Sets the cache policy of the collection.
Definition: collection.cpp:345
Rights rights() const
Returns the rights the user has on the collection.
Definition: collection.cpp:229
Fallback to enabled state.
Definition: collection.h:460
void setLocalListPreference(ListPurpose purpose, ListPreference preference)
Sets the local list preference for the specified purpose.
Definition: collection.cpp:372
void setShouldList(ListPurpose purpose, bool shouldList)
Sets whether the collection should be listed or not for the specified purpose.
Definition: collection.cpp:409
Id id() const
Returns the unique identifier of the collection.
Definition: collection.cpp:99
bool operator<(const Collection &other) const
Definition: collection.cpp:149
void setKeepLocalChanges(const QSet< QByteArray > &parts)
Set during sync to indicate that the provided parts are only default values;.
Definition: collection.cpp:418
bool operator!=(const Collection &other) const
Returns whether the collection&#39;s id does not equal the id of the other collection.
Definition: collection.cpp:135
Helper integration between Akonadi and Qt.
QString remoteId() const
Returns the remote id of the collection.
Definition: collection.cpp:109
const T * attribute() const
Returns the attribute of the requested type or 0 if it is not available.
Definition: collection.h:563
QStringList contentMimeTypes() const
Returns a list of possible content mimetypes, e.g.
Definition: collection.cpp:243
void setQuery(const QString &query, QUrl::ParsingMode mode)
CachePolicy cachePolicy() const
Returns the cache policy of the collection.
Definition: collection.cpp:340
~Collection()
Destroys the collection.
bool enabled() const
Returns the collection&#39;s enabled state.
Definition: collection.cpp:367
QString resource() const
Returns the identifier of the resource owning the collection.
Definition: collection.cpp:305
ListPreference localListPreference(ListPurpose purpose) const
Returns the local list preference for the specified purpose.
Definition: collection.cpp:388
CollectionStatistics statistics() const
Returns the collection statistics of the collection.
Definition: collection.cpp:330
Listing for synchronization.
Definition: collection.h:469
qlonglong toLongLong(bool *ok, int base) const const
void setResource(const QString &identifier)
Sets the identifier of the resource owning the collection.
Definition: collection.cpp:310
Attribute that stores the properties that are used to display an entity.
void setContentMimeTypes(const QStringList &types)
Sets the list of possible content mime types.
Definition: collection.cpp:248
bool isVirtual() const
Returns whether the collection is virtual, for example a search collection.
Definition: collection.cpp:351
static Collection fromUrl(const QUrl &url)
Creates a collection from the given url.
Definition: collection.cpp:270
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 4 2020 23:16:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.