1/*
2 Copyright (c) 2009 Andras Mantia <amantia@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 "acl.h"
21
22#include <QtCore/QByteArray>
23#include <QtCore/QMap>
24#include <KDE/KGlobal>
25
26namespace KIMAP {
27namespace Acl {
28
29class RightsMap
30{
31 public:
32 RightsMap() {
33 map['l'] = Lookup;
34 map['r'] = Read;
35 map['s'] = KeepSeen;
36 map['w'] = Write;
37 map['i'] = Insert;
38 map['p'] = Post;
39 map['c'] = Create; //TODO: obsolete, keep it?
40 map['d'] = Delete; //TODO: obsolete, keep it?
41 map['k'] = CreateMailbox;
42 map['x'] = DeleteMailbox;
43 map['t'] = DeleteMessage;
44 map['e'] = Expunge;
45 map['a'] = Admin;
46 map['n'] = WriteShared;
47 map['0'] = Custom0;
48 map['1'] = Custom1;
49 map['2'] = Custom2;
50 map['3'] = Custom3;
51 map['4'] = Custom4;
52 map['5'] = Custom5;
53 map['6'] = Custom6;
54 map['7'] = Custom7;
55 map['8'] = Custom8;
56 map['9'] = Custom9;
57 }
58
59 QMap<char, Right> map;
60};
61
62K_GLOBAL_STATIC( RightsMap, globalRights )
63
64}
65}
66
67KIMAP::Acl::Rights KIMAP::Acl::rightsFromString( const QByteArray &string )
68{
69 Rights result;
70
71 if ( string.isEmpty() ) {
72 return result;
73 }
74
75 int pos = 0;
76 if ( string[0] == '+' || string[0] == '-' ) { // Skip modifier if any
77 pos++;
78 }
79
80 for ( int i = pos; i < string.size(); i++ ) {
81 if ( globalRights->map.contains( string[i] ) ) {
82 result|= globalRights->map[string[i]];
83 }
84 }
85
86 return result;
87}
88
89QByteArray KIMAP::Acl::rightsToString( Rights rights )
90{
91 QByteArray result;
92
93 for ( int right = Lookup; right <= Custom9; right <<= 1 ) {
94 if ( rights & right ) {
95 result += globalRights->map.key( (Right)right );
96 }
97 }
98
99 return result;
100}
101
102KIMAP::Acl::Rights KIMAP::Acl::normalizedRights( KIMAP::Acl::Rights rights )
103{
104 Rights normalized = rights;
105 if ( normalized & Create ) {
106 normalized |= ( CreateMailbox | DeleteMailbox );
107 normalized &= ~Create;
108 }
109 if ( normalized & Delete ) {
110 normalized |= ( DeleteMessage | Expunge );
111 normalized &= ~Delete;
112 }
113 return normalized;
114}
115
116KIMAP::Acl::Rights KIMAP::Acl::denormalizedRights( KIMAP::Acl::Rights rights )
117{
118 Rights denormalized = normalizedRights( rights );
119 if ( denormalized & ( CreateMailbox | DeleteMailbox ) ) {
120 denormalized |= Create;
121 }
122 if ( denormalized & ( DeleteMessage | Expunge ) ) {
123 denormalized |= Delete;
124 }
125 return denormalized;
126}
127