1/*
2 Copyright (c) 2007 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#include "collectionrightsattribute_p.h"
21
22using namespace Akonadi;
23
24static const char *s_accessRightsIdentifier = "AccessRights";
25
26static Collection::Rights dataToRights(const QByteArray &data)
27{
28 Collection::Rights rights = Collection::ReadOnly;
29
30 if (data.isEmpty()) {
31 return Collection::ReadOnly;
32 }
33
34 if (data.at(0) == 'a') {
35 return Collection::AllRights;
36 }
37
38 for (int i = 0; i < data.count(); ++i) {
39 switch (data.at(i)) {
40 case 'w':
41 rights |= Collection::CanChangeItem;
42 break;
43 case 'c':
44 rights |= Collection::CanCreateItem;
45 break;
46 case 'd':
47 rights |= Collection::CanDeleteItem;
48 break;
49 case 'l':
50 rights |= Collection::CanLinkItem;
51 break;
52 case 'u':
53 rights |= Collection::CanUnlinkItem;
54 break;
55 case 'W':
56 rights |= Collection::CanChangeCollection;
57 break;
58 case 'C':
59 rights |= Collection::CanCreateCollection;
60 break;
61 case 'D':
62 rights |= Collection::CanDeleteCollection;
63 break;
64 }
65 }
66
67 return rights;
68}
69
70static QByteArray rightsToData(Collection::Rights &rights)
71{
72 if (rights == Collection::AllRights) {
73 return QByteArray("a");
74 }
75
76 QByteArray data;
77 if (rights & Collection::CanChangeItem) {
78 data.append('w');
79 }
80 if (rights & Collection::CanCreateItem) {
81 data.append('c');
82 }
83 if (rights & Collection::CanDeleteItem) {
84 data.append('d');
85 }
86 if (rights & Collection::CanChangeCollection) {
87 data.append('W');
88 }
89 if (rights & Collection::CanCreateCollection) {
90 data.append('C');
91 }
92 if (rights & Collection::CanDeleteCollection) {
93 data.append('D');
94 }
95 if (rights & Collection::CanLinkItem) {
96 data.append('l');
97 }
98 if (rights & Collection::CanUnlinkItem) {
99 data.append('u');
100 }
101
102 return data;
103}
104
105/**
106 * @internal
107 */
108class CollectionRightsAttribute::Private
109{
110public:
111 QByteArray mData;
112};
113
114CollectionRightsAttribute::CollectionRightsAttribute()
115 : Attribute()
116 , d(new Private)
117{
118}
119
120CollectionRightsAttribute::~CollectionRightsAttribute()
121{
122 delete d;
123}
124
125void CollectionRightsAttribute::setRights(Collection::Rights rights)
126{
127 d->mData = rightsToData(rights);
128}
129
130Collection::Rights CollectionRightsAttribute::rights() const
131{
132 return dataToRights(d->mData);
133}
134
135CollectionRightsAttribute *CollectionRightsAttribute::clone() const
136{
137 CollectionRightsAttribute *attr = new CollectionRightsAttribute();
138 attr->d->mData = d->mData;
139
140 return attr;
141}
142
143QByteArray CollectionRightsAttribute::type() const
144{
145 return s_accessRightsIdentifier;
146}
147
148QByteArray CollectionRightsAttribute::serialized() const
149{
150 return d->mData;
151}
152
153void CollectionRightsAttribute::deserialize(const QByteArray &data)
154{
155 d->mData = data;
156}
157