Warning, file /pim/kdepim-runtime/resources/shared/singlefileresource/imapaclattribute.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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