File indexing completed on 2024-05-19 05:17:40

0001 /*
0002     Copyright (c) 2009 Andras Mantia <amantia@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #include "acl.h"
0021 
0022 #include <QtCore/QByteArray>
0023 #include <QtCore/QMap>
0024 
0025 namespace KIMAP2
0026 {
0027 namespace Acl
0028 {
0029 
0030 class RightsMap
0031 {
0032 public:
0033     RightsMap()
0034     {
0035         map['l'] = Lookup;
0036         map['r'] = Read;
0037         map['s'] = KeepSeen;
0038         map['w'] = Write;
0039         map['i'] = Insert;
0040         map['p'] = Post;
0041         map['c'] = Create; //TODO: obsolete, keep it?
0042         map['d'] = Delete; //TODO: obsolete, keep it?
0043         map['k'] = CreateMailbox;
0044         map['x'] = DeleteMailbox;
0045         map['t'] = DeleteMessage;
0046         map['e'] = Expunge;
0047         map['a'] = Admin;
0048         map['n'] = WriteShared;
0049         map['0'] = Custom0;
0050         map['1'] = Custom1;
0051         map['2'] = Custom2;
0052         map['3'] = Custom3;
0053         map['4'] = Custom4;
0054         map['5'] = Custom5;
0055         map['6'] = Custom6;
0056         map['7'] = Custom7;
0057         map['8'] = Custom8;
0058         map['9'] = Custom9;
0059     }
0060 
0061     QMap<char, Right> map;
0062 };
0063 
0064 Q_GLOBAL_STATIC(RightsMap, globalRights)
0065 
0066 }
0067 }
0068 
0069 KIMAP2::Acl::Rights KIMAP2::Acl::rightsFromString(const QByteArray &string)
0070 {
0071     Rights result;
0072 
0073     if (string.isEmpty()) {
0074         return result;
0075     }
0076 
0077     int pos = 0;
0078     if (string[0] == '+' || string[0] == '-') {   // Skip modifier if any
0079         pos++;
0080     }
0081 
0082     for (int i = pos; i < string.size(); i++) {
0083         if (globalRights->map.contains(string[i])) {
0084             result |= globalRights->map[string[i]];
0085         }
0086     }
0087 
0088     return result;
0089 }
0090 
0091 QByteArray KIMAP2::Acl::rightsToString(Rights rights)
0092 {
0093     QByteArray result;
0094 
0095     for (int right = Lookup; right <= Custom9; right <<= 1) {
0096         if (rights & right) {
0097             result += globalRights->map.key((Right)right);
0098         }
0099     }
0100 
0101     return result;
0102 }
0103 
0104 KIMAP2::Acl::Rights KIMAP2::Acl::normalizedRights(KIMAP2::Acl::Rights rights)
0105 {
0106     Rights normalized = rights;
0107     if (normalized & Create) {
0108         normalized |= (CreateMailbox | DeleteMailbox);
0109         normalized &= ~Create;
0110     }
0111     if (normalized & Delete) {
0112         normalized |= (DeleteMessage | Expunge);
0113         normalized &= ~Delete;
0114     }
0115     return normalized;
0116 }
0117 
0118 KIMAP2::Acl::Rights KIMAP2::Acl::denormalizedRights(KIMAP2::Acl::Rights rights)
0119 {
0120     Rights denormalized = normalizedRights(rights);
0121     if (denormalized & (CreateMailbox | DeleteMailbox)) {
0122         denormalized |= Create;
0123     }
0124     if (denormalized & (DeleteMessage | Expunge)) {
0125         denormalized |= Delete;
0126     }
0127     return denormalized;
0128 }