00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "acl.h"
00021
00022 #include <QtCore/QByteArray>
00023 #include <QtCore/QMap>
00024 #include <KDE/KGlobal>
00025
00026 namespace KIMAP {
00027 namespace Acl {
00028
00029 class RightsMap
00030 {
00031 public:
00032 RightsMap()
00033 {
00034 map['l'] = Lookup;
00035 map['r'] = Read;
00036 map['s'] = KeepSeen;
00037 map['w'] = Write;
00038 map['i'] = Insert;
00039 map['p'] = Post;
00040 map['c'] = Create;
00041 map['d'] = Delete;
00042 map['k'] = CreateMailbox;
00043 map['x'] = DeleteMailbox;
00044 map['t'] = DeleteMessage;
00045 map['e'] = Expunge;
00046 map['a'] = Admin;
00047 map['n'] = WriteShared;
00048 map['0'] = Custom0;
00049 map['1'] = Custom1;
00050 map['2'] = Custom2;
00051 map['3'] = Custom3;
00052 map['4'] = Custom4;
00053 map['5'] = Custom5;
00054 map['6'] = Custom6;
00055 map['7'] = Custom7;
00056 map['8'] = Custom8;
00057 map['9'] = Custom9;
00058 }
00059
00060 QMap<char, Right> map;
00061 };
00062
00063 K_GLOBAL_STATIC(RightsMap, globalRights)
00064
00065 }
00066 }
00067
00068 KIMAP::Acl::Rights KIMAP::Acl::rightsFromString( const QByteArray &string )
00069 {
00070 Rights result;
00071
00072 if ( string.isEmpty() )
00073 return result;
00074
00075 int pos = 0;
00076 if ( string[0] == '+' || string[0]== '-') {
00077 pos++;
00078 }
00079
00080 for ( int i = pos; i < string.size(); i++ ) {
00081 if ( globalRights->map.contains( string[i] ) ) {
00082 result|= globalRights->map[string[i]];
00083 }
00084 }
00085
00086 return result;
00087 }
00088
00089 QByteArray KIMAP::Acl::rightsToString( Rights rights )
00090 {
00091 QByteArray result;
00092
00093 for ( int right = Lookup; right<=Custom9; right<<=1 ) {
00094 if ( rights & right ) {
00095 result+= globalRights->map.key( (Right)right );
00096 }
00097 }
00098
00099 return result;
00100 }
00101
00102 KIMAP::Acl::Rights KIMAP::Acl::normalizedRights( KIMAP::Acl::Rights rights )
00103 {
00104 Rights normalized = rights;
00105 if ( normalized & Create ) {
00106 normalized |= ( CreateMailbox | DeleteMailbox );
00107 normalized &= ~Create;
00108 }
00109 if ( normalized & Delete ) {
00110 normalized |= ( DeleteMessage | Expunge );
00111 normalized &= ~Delete;
00112 }
00113 return normalized;
00114 }
00115
00116 KIMAP::Acl::Rights KIMAP::Acl::denormalizedRights( KIMAP::Acl::Rights rights )
00117 {
00118 Rights denormalized = normalizedRights( rights );
00119 if ( denormalized & ( CreateMailbox | DeleteMailbox ) ) {
00120 denormalized |= Create;
00121 }
00122 if ( denormalized & ( DeleteMessage | Expunge ) ) {
00123 denormalized |= Delete;
00124 }
00125 return denormalized;
00126 }
00127