File indexing completed on 2025-01-05 04:58:18
0001 /* 0002 SPDX-FileCopyrightText: 2009 Kevin Ottens <ervin@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 // THIS file should not exist and is only a copy of 0008 // kdepim-runtime/resources/shared/singlefileresource 0009 0010 // Any improvements should be done at kdepim-runtime and 0011 // than afterwards copy the new version 0012 0013 #include "imapaclattribute.h" 0014 0015 #include <QByteArray> 0016 #include <QList> 0017 0018 using namespace PimCommon; 0019 0020 class PimCommon::ImapAclAttributePrivate 0021 { 0022 public: 0023 ImapAclAttributePrivate() = default; 0024 0025 QMap<QByteArray, KIMAP::Acl::Rights> mRights; 0026 QMap<QByteArray, KIMAP::Acl::Rights> mOldRights; 0027 KIMAP::Acl::Rights mMyRights; 0028 }; 0029 0030 ImapAclAttribute::ImapAclAttribute() 0031 : d(new PimCommon::ImapAclAttributePrivate) 0032 { 0033 } 0034 0035 ImapAclAttribute::ImapAclAttribute(const QMap<QByteArray, KIMAP::Acl::Rights> &rights, const QMap<QByteArray, KIMAP::Acl::Rights> &oldRights) 0036 : d(new PimCommon::ImapAclAttributePrivate) 0037 { 0038 d->mRights = rights; 0039 d->mOldRights = oldRights; 0040 } 0041 0042 ImapAclAttribute::~ImapAclAttribute() = default; 0043 0044 void ImapAclAttribute::setRights(const QMap<QByteArray, KIMAP::Acl::Rights> &rights) 0045 { 0046 d->mOldRights = d->mRights; 0047 d->mRights = rights; 0048 } 0049 0050 QMap<QByteArray, KIMAP::Acl::Rights> ImapAclAttribute::rights() const 0051 { 0052 return d->mRights; 0053 } 0054 0055 QMap<QByteArray, KIMAP::Acl::Rights> ImapAclAttribute::oldRights() const 0056 { 0057 return d->mOldRights; 0058 } 0059 0060 void ImapAclAttribute::setMyRights(KIMAP::Acl::Rights rights) 0061 { 0062 d->mMyRights = rights; 0063 } 0064 0065 KIMAP::Acl::Rights ImapAclAttribute::myRights() const 0066 { 0067 return d->mMyRights; 0068 } 0069 0070 QByteArray ImapAclAttribute::type() const 0071 { 0072 static const QByteArray sType("imapacl"); 0073 return sType; 0074 } 0075 0076 ImapAclAttribute *ImapAclAttribute::clone() const 0077 { 0078 auto attr = new ImapAclAttribute(d->mRights, d->mOldRights); 0079 attr->setMyRights(d->mMyRights); 0080 return attr; 0081 } 0082 0083 QByteArray ImapAclAttribute::serialized() const 0084 { 0085 QByteArray result; 0086 0087 bool added = false; 0088 for (auto it = d->mRights.cbegin(), end = d->mRights.cend(); it != end; ++it) { 0089 result += it.key(); 0090 result += ' '; 0091 result += KIMAP::Acl::rightsToString(it.value()); 0092 result += " % "; // We use this separator as '%' is not allowed in keys or values 0093 added = true; 0094 } 0095 0096 if (added) { 0097 result.chop(3); 0098 } 0099 0100 result += " %% "; 0101 0102 added = false; 0103 for (auto it = d->mOldRights.cbegin(), end = d->mOldRights.cend(); it != end; ++it) { 0104 result += it.key(); 0105 result += ' '; 0106 result += KIMAP::Acl::rightsToString(it.value()); 0107 result += " % "; // We use this separator as '%' is not allowed in keys or values 0108 added = true; 0109 } 0110 0111 if (added) { 0112 result.chop(3); 0113 } 0114 0115 if (d->mMyRights) { 0116 result += " %% "; 0117 result += KIMAP::Acl::rightsToString(d->mMyRights); 0118 } 0119 0120 return result; 0121 } 0122 0123 static void fillRightsMap(const QList<QByteArray> &rights, QMap<QByteArray, KIMAP::Acl::Rights> &map) 0124 { 0125 for (const QByteArray &right : rights) { 0126 const QByteArray trimmed = right.trimmed(); 0127 const int wsIndex = trimmed.indexOf(' '); 0128 const QByteArray id = trimmed.mid(0, wsIndex).trimmed(); 0129 if (!id.isEmpty()) { 0130 const bool noValue = (wsIndex == -1); 0131 if (noValue) { 0132 map[id] = KIMAP::Acl::None; 0133 } else { 0134 const QByteArray value = trimmed.mid(wsIndex + 1, right.length() - wsIndex).trimmed(); 0135 map[id] = KIMAP::Acl::rightsFromString(value); 0136 } 0137 } 0138 } 0139 } 0140 0141 void ImapAclAttribute::deserialize(const QByteArray &data) 0142 { 0143 d->mRights.clear(); 0144 d->mOldRights.clear(); 0145 d->mMyRights = KIMAP::Acl::None; 0146 0147 QList<QByteArray> parts; 0148 int lastPos = 0; 0149 int pos; 0150 while ((pos = data.indexOf(" %% ", lastPos)) != -1) { 0151 parts << data.mid(lastPos, pos - lastPos); 0152 lastPos = pos + 4; 0153 } 0154 parts << data.mid(lastPos); 0155 0156 if (parts.size() < 2) { 0157 return; 0158 } 0159 fillRightsMap(parts.at(0).split('%'), d->mRights); 0160 fillRightsMap(parts.at(1).split('%'), d->mOldRights); 0161 if (parts.size() >= 3) { 0162 d->mMyRights = KIMAP::Acl::rightsFromString(parts.at(2)); 0163 } 0164 } 0165 0166 bool ImapAclAttribute::operator==(const ImapAclAttribute &other) const 0167 { 0168 return (oldRights() == other.oldRights()) && (rights() == other.rights()) && (myRights() == other.myRights()); 0169 }