1/*
2 Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
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 "collection.h"
21#include "collection_p.h"
22
23#include "attributefactory.h"
24#include "cachepolicy.h"
25#include "collectionrightsattribute_p.h"
26#include "collectionstatistics.h"
27#include "entity_p.h"
28#include "entitydisplayattribute.h"
29
30#include <QtCore/QDebug>
31#include <QtCore/QHash>
32#include <QtCore/QString>
33#include <QtCore/QStringList>
34
35#include <KUrl>
36#include <KGlobal>
37
38using namespace Akonadi;
39
40class CollectionRoot : public Collection
41{
42public:
43 CollectionRoot()
44 : Collection(0)
45 {
46 QStringList types;
47 types << Collection::mimeType();
48 setContentMimeTypes(types);
49
50 // The root collection is read-only for the users
51 Collection::Rights rights;
52 rights |= Collection::ReadOnly;
53 setRights(rights);
54 }
55};
56
57K_GLOBAL_STATIC(CollectionRoot, s_root)
58
59Collection::Collection()
60 : Entity(new CollectionPrivate)
61{
62 Q_D(Collection);
63 static int lastId = -1;
64 d->mId = lastId--;
65}
66
67Collection::Collection(Id id)
68 : Entity(new CollectionPrivate(id))
69{
70}
71
72Collection::Collection(const Collection &other)
73 : Entity(other)
74{
75}
76
77Collection::~Collection()
78{
79}
80
81QString Collection::name() const
82{
83 return d_func()->name;
84}
85
86QString Collection::displayName() const
87{
88 const EntityDisplayAttribute *const attr = attribute<EntityDisplayAttribute>();
89 const QString displayName = attr ? attr->displayName() : QString();
90 return !displayName.isEmpty() ? displayName : d_func()->name;
91}
92
93void Collection::setName(const QString &name)
94{
95 Q_D(Collection);
96 d->name = name;
97}
98
99Collection::Rights Collection::rights() const
100{
101 CollectionRightsAttribute *attr = attribute<CollectionRightsAttribute>();
102 if (attr) {
103 return attr->rights();
104 } else {
105 return AllRights;
106 }
107}
108
109void Collection::setRights(Rights rights)
110{
111 CollectionRightsAttribute *attr = attribute<CollectionRightsAttribute>(AddIfMissing);
112 attr->setRights(rights);
113}
114
115QStringList Collection::contentMimeTypes() const
116{
117 return d_func()->contentTypes;
118}
119
120void Collection::setContentMimeTypes(const QStringList &types)
121{
122 Q_D(Collection);
123 if (d->contentTypes != types) {
124 d->contentTypes = types;
125 d->contentTypesChanged = true;
126 }
127}
128
129Collection::Id Collection::parent() const
130{
131 return parentCollection().id();
132}
133
134void Collection::setParent(Id parent)
135{
136 parentCollection().setId(parent);
137}
138
139void Collection::setParent(const Collection &collection)
140{
141 setParentCollection(collection);
142}
143
144QString Collection::parentRemoteId() const
145{
146 return parentCollection().remoteId();
147}
148
149void Collection::setParentRemoteId(const QString &remoteParent)
150{
151 parentCollection().setRemoteId(remoteParent);
152}
153
154KUrl Collection::url() const
155{
156 return url(UrlShort);
157}
158
159KUrl Collection::url(UrlType type) const
160{
161 KUrl url;
162 url.setProtocol(QString::fromLatin1("akonadi"));
163 url.addQueryItem(QLatin1String("collection"), QString::number(id()));
164
165 if (type == UrlWithName) {
166 url.addQueryItem(QLatin1String("name"), name());
167 }
168
169 return url;
170}
171
172Collection Collection::fromUrl(const KUrl &url)
173{
174 if (url.protocol() != QLatin1String("akonadi")) {
175 return Collection();
176 }
177
178 const QString colStr = url.queryItem(QLatin1String("collection"));
179 bool ok = false;
180 Collection::Id colId = colStr.toLongLong(&ok);
181 if (!ok) {
182 return Collection();
183 }
184
185 if (colId == 0) {
186 return Collection::root();
187 }
188
189 return Collection(colId);
190}
191
192Collection Collection::root()
193{
194 return *s_root;
195}
196
197QString Collection::mimeType()
198{
199 return QString::fromLatin1("inode/directory");
200}
201
202QString Akonadi::Collection::virtualMimeType()
203{
204 return QString::fromLatin1("application/x-vnd.akonadi.collection.virtual");
205}
206
207QString Collection::resource() const
208{
209 return d_func()->resource;
210}
211
212void Collection::setResource(const QString &resource)
213{
214 Q_D(Collection);
215 d->resource = resource;
216}
217
218uint qHash(const Akonadi::Collection &collection)
219{
220 return qHash(collection.id());
221}
222
223QDebug operator <<(QDebug d, const Akonadi::Collection &collection)
224{
225 return d << "Collection ID:" << collection.id()
226 << " remote ID:" << collection.remoteId() << endl
227 << " name:" << collection.name() << endl
228 << " url:" << collection.url() << endl
229 << " parent:" << collection.parentCollection().id() << collection.parentCollection().remoteId() << endl
230 << " resource:" << collection.resource() << endl
231 << " rights:" << collection.rights() << endl
232 << " contents mime type:" << collection.contentMimeTypes() << endl
233 << " isVirtual:" << collection.isVirtual() << endl
234 << " " << collection.cachePolicy() << endl
235 << " " << collection.statistics();
236}
237
238CollectionStatistics Collection::statistics() const
239{
240 return d_func()->statistics;
241}
242
243void Collection::setStatistics(const CollectionStatistics &statistics)
244{
245 Q_D(Collection);
246 d->statistics = statistics;
247}
248
249CachePolicy Collection::cachePolicy() const
250{
251 return d_func()->cachePolicy;
252}
253
254void Collection::setCachePolicy(const CachePolicy &cachePolicy)
255{
256 Q_D(Collection);
257 d->cachePolicy = cachePolicy;
258 d->cachePolicyChanged = true;
259}
260
261bool Collection::isVirtual() const
262{
263 return d_func()->isVirtual;
264}
265
266void Akonadi::Collection::setVirtual(bool isVirtual)
267{
268 Q_D(Collection);
269
270 d->isVirtual = isVirtual;
271}
272
273void Collection::setEnabled(bool enabled)
274{
275 Q_D(Collection);
276
277 if (enabled != d->enabled) {
278 d->enabledChanged = true;
279 }
280 d->enabled = enabled;
281}
282
283bool Collection::enabled() const
284{
285 Q_D(const Collection);
286
287 return d->enabled;
288}
289
290void Collection::setLocalListPreference(Collection::ListPurpose purpose, Collection::ListPreference preference)
291{
292 Q_D(Collection);
293
294 switch(purpose) {
295 case ListDisplay:
296 d->displayPreference = preference;
297 break;
298 case ListSync:
299 d->syncPreference = preference;
300 break;
301 case ListIndex:
302 d->indexPreference = preference;
303 break;
304 }
305 d->listPreferenceChanged = true;
306}
307
308Collection::ListPreference Collection::localListPreference(Collection::ListPurpose purpose) const
309{
310 Q_D(const Collection);
311
312 switch(purpose) {
313 case ListDisplay:
314 return d->displayPreference;
315 case ListSync:
316 return d->syncPreference;
317 case ListIndex:
318 return d->indexPreference;
319 }
320 return ListDefault;
321}
322
323bool Collection::shouldList(Collection::ListPurpose purpose) const
324{
325 if (localListPreference(purpose) == ListDefault) {
326 return enabled();
327 }
328 return (localListPreference(purpose) == ListEnabled);
329}
330
331void Collection::setShouldList(ListPurpose purpose, bool list)
332{
333 if (localListPreference(purpose) == ListDefault) {
334 setEnabled(list);
335 } else {
336 setLocalListPreference(purpose, list ? ListEnabled : ListDisabled);
337 }
338}
339
340void Collection::setReferenced(bool referenced)
341{
342 Q_D(Collection);
343
344 if (d->referenced != referenced) {
345 d->referencedChanged = true;
346 }
347 d->referenced = referenced;
348}
349
350bool Collection::referenced() const
351{
352 Q_D(const Collection);
353
354 return d->referenced;
355}
356
357AKONADI_DEFINE_PRIVATE(Akonadi::Collection)
358