summaryrefslogtreecommitdiff
path: root/kimap/acl.cpp
blob: 980538bfe2979522205cf70ccf38fa22eda338ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
    Copyright (c) 2009 Andras Mantia <amantia@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "acl.h"

#include <QtCore/QByteArray>
#include <QtCore/QMap>
#include <kglobal.h>

namespace KIMAP {
namespace Acl {

class RightsMap
{
  public:
    RightsMap()
    {
      map['l'] = Lookup;
      map['r'] = Read;
      map['s'] = KeepSeen;
      map['w'] = Write;
      map['i'] = Insert;
      map['p'] = Post;
      map['c'] = Create; //TODO: obsolete, keep it?
      map['d'] = Delete; //TODO: obsolete, keep it?
      map['k'] = CreateMailbox;
      map['x'] = DeleteMailbox;
      map['t'] = DeleteMessage;
      map['e'] = Expunge;
      map['a'] = Admin;
      map['n'] = WriteShared;
      map['0'] = Custom0;
      map['1'] = Custom1;
      map['2'] = Custom2;
      map['3'] = Custom3;
      map['4'] = Custom4;
      map['5'] = Custom5;
      map['6'] = Custom6;
      map['7'] = Custom7;
      map['8'] = Custom8;
      map['9'] = Custom9;
    }

    QMap<char, Right> map;
};

Q_GLOBAL_STATIC(RightsMap, globalRights)

}
}

KIMAP::Acl::Rights KIMAP::Acl::rightsFromString( const QByteArray &string )
{
  Rights result;

  if ( string.isEmpty() )
    return result;

  int pos = 0;
  if ( string[0] == '+' || string[0]== '-') { // Skip modifier if any
    pos++;
  }

  for ( int i = pos; i < string.size(); i++ ) {
    if ( globalRights()->map.contains( string[i] ) ) {
      result|= globalRights()->map[string[i]];
    }
  }

  return result;
}

QByteArray KIMAP::Acl::rightsToString( Rights rights )
{
  QByteArray result;

  for ( int right = Lookup; right<=Custom9; right<<=1 ) {
    if ( rights & right ) {
      result+= globalRights()->map.key( (Right)right );
    }
  }

  return result;
}

KIMAP::Acl::Rights KIMAP::Acl::normalizedRights( KIMAP::Acl::Rights rights )
{
  Rights normalized = rights;
  if ( normalized & Create ) {
    normalized |= ( CreateMailbox | DeleteMailbox );
    normalized &= ~Create;
  }
  if ( normalized & Delete ) {
    normalized |= ( DeleteMessage | Expunge );
    normalized &= ~Delete;
  }
  return normalized;
}

KIMAP::Acl::Rights KIMAP::Acl::denormalizedRights( KIMAP::Acl::Rights rights )
{
  Rights denormalized = normalizedRights( rights );
  if ( denormalized & ( CreateMailbox | DeleteMailbox ) ) {
    denormalized |= Create;
  }
  if ( denormalized & ( DeleteMessage | Expunge ) ) {
    denormalized |= Delete;
  }
  return denormalized;
}