1/*
2 Copyright (c) 2008 Tobias Koenig <tokoe@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#ifndef AKONADI_COLLECTIONUTILS_P_H
21#define AKONADI_COLLECTIONUTILS_P_H
22
23#include <QtCore/QStringList>
24#include <akonadi/entitydisplayattribute.h>
25#include <akonadi/collectionstatistics.h>
26#include <akonadi/item.h>
27
28namespace Akonadi {
29
30/**
31 * @internal
32 */
33namespace CollectionUtils
34{
35inline bool isVirtualParent(const Collection &collection)
36{
37 return (collection.parentCollection() == Collection::root() && collection.isVirtual());
38}
39
40inline bool isReadOnly(const Collection &collection)
41{
42 return !(collection.rights() &Collection::CanCreateItem);
43}
44
45inline bool isRoot(const Collection &collection)
46{
47 return (collection == Collection::root());
48}
49
50inline bool isResource(const Collection &collection)
51{
52 return (collection.parentCollection() == Collection::root());
53}
54
55inline bool isStructural(const Collection &collection)
56{
57 return collection.contentMimeTypes().isEmpty();
58}
59
60inline bool isFolder(const Collection &collection)
61{
62 return (!isRoot(collection) &&
63 !isResource(collection) &&
64 !isStructural(collection) &&
65 collection.resource() != QLatin1String("akonadi_search_resource") &&
66 collection.resource() != QLatin1String("akonadi_nepomuktag_resource"));
67}
68
69inline QString defaultIconName(const Collection &col)
70{
71 if (CollectionUtils::isVirtualParent(col)) {
72 return QLatin1String("edit-find");
73 }
74 if (col.isVirtual()) {
75 return QLatin1String("document-preview");
76 }
77 if (CollectionUtils::isResource(col)) {
78 return QLatin1String("network-server");
79 }
80 if (CollectionUtils::isStructural(col)) {
81 return QLatin1String("folder-grey");
82 }
83 if (CollectionUtils::isReadOnly(col)) {
84 return QLatin1String("folder-grey");
85 }
86
87 const QStringList content = col.contentMimeTypes();
88 if ((content.size() == 1) ||
89 (content.size() == 2 && content.contains(Collection::mimeType()))) {
90 if (content.contains(QLatin1String("text/x-vcard")) ||
91 content.contains(QLatin1String("text/directory")) ||
92 content.contains(QLatin1String("text/vcard"))) {
93 return QLatin1String("x-office-address-book");
94 }
95 // TODO: add all other content types and/or fix their mimetypes
96 if (content.contains(QLatin1String("akonadi/event")) || content.contains(QLatin1String("text/ical"))) {
97 return QLatin1String("view-pim-calendar");
98 }
99 if (content.contains(QLatin1String("akonadi/task"))) {
100 return QLatin1String("view-pim-tasks");
101 }
102 } else if (content.isEmpty()) {
103 return QLatin1String("folder-grey");
104 }
105 return QLatin1String("folder");
106}
107inline QString displayIconName(const Collection &col)
108{
109 QString iconName = defaultIconName(col);
110 if (col.hasAttribute<EntityDisplayAttribute>() &&
111 !col.attribute<EntityDisplayAttribute>()->iconName().isEmpty()) {
112 if (!col.attribute<EntityDisplayAttribute>()->activeIconName().isEmpty() && col.statistics().unreadCount() > 0) {
113 iconName = col.attribute<EntityDisplayAttribute>()->activeIconName();
114 } else {
115 iconName = col.attribute<EntityDisplayAttribute>()->iconName();
116 }
117 }
118 return iconName;
119
120}
121inline bool hasValidHierarchicalRID(const Collection &col)
122{
123 if (col == Collection::root()) {
124 return true;
125 }
126 if (col.remoteId().isEmpty()) {
127 return false;
128 }
129 return hasValidHierarchicalRID(col.parentCollection());
130}
131inline bool hasValidHierarchicalRID(const Item &item)
132{
133 return !item.remoteId().isEmpty() && hasValidHierarchicalRID(item.parentCollection());
134}
135}
136
137}
138
139#endif
140