Pimcommon

imapaclattribute.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Kevin Ottens <ervin@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7// THIS file should not exist and is only a copy of
8// kdepim-runtime/resources/shared/singlefileresource
9
10// Any improvements should be done at kdepim-runtime and
11// than afterwards copy the new version
12
13#include "imapaclattribute.h"
14
15#include <QByteArray>
16#include <QList>
17
18using namespace PimCommon;
19
20class PimCommon::ImapAclAttributePrivate
21{
22public:
23 ImapAclAttributePrivate() = default;
24
27 KIMAP::Acl::Rights mMyRights;
28};
29
30ImapAclAttribute::ImapAclAttribute()
31 : d(new PimCommon::ImapAclAttributePrivate)
32{
33}
34
35ImapAclAttribute::ImapAclAttribute(const QMap<QByteArray, KIMAP::Acl::Rights> &rights, const QMap<QByteArray, KIMAP::Acl::Rights> &oldRights)
36 : d(new PimCommon::ImapAclAttributePrivate)
37{
38 d->mRights = rights;
39 d->mOldRights = oldRights;
40}
41
42ImapAclAttribute::~ImapAclAttribute() = default;
43
44void ImapAclAttribute::setRights(const QMap<QByteArray, KIMAP::Acl::Rights> &rights)
45{
46 d->mOldRights = d->mRights;
47 d->mRights = rights;
48}
49
50QMap<QByteArray, KIMAP::Acl::Rights> ImapAclAttribute::rights() const
51{
52 return d->mRights;
53}
54
55QMap<QByteArray, KIMAP::Acl::Rights> ImapAclAttribute::oldRights() const
56{
57 return d->mOldRights;
58}
59
60void ImapAclAttribute::setMyRights(KIMAP::Acl::Rights rights)
61{
62 d->mMyRights = rights;
63}
64
65KIMAP::Acl::Rights ImapAclAttribute::myRights() const
66{
67 return d->mMyRights;
68}
69
70QByteArray ImapAclAttribute::type() const
71{
72 static const QByteArray sType("imapacl");
73 return sType;
74}
75
76ImapAclAttribute *ImapAclAttribute::clone() const
77{
78 auto attr = new ImapAclAttribute(d->mRights, d->mOldRights);
79 attr->setMyRights(d->mMyRights);
80 return attr;
81}
82
83QByteArray ImapAclAttribute::serialized() const
84{
85 QByteArray result;
86
87 bool added = false;
88 for (auto it = d->mRights.cbegin(), end = d->mRights.cend(); it != end; ++it) {
89 result += it.key();
90 result += ' ';
91 result += KIMAP::Acl::rightsToString(it.value());
92 result += " % "; // We use this separator as '%' is not allowed in keys or values
93 added = true;
94 }
95
96 if (added) {
97 result.chop(3);
98 }
99
100 result += " %% ";
101
102 added = false;
103 for (auto it = d->mOldRights.cbegin(), end = d->mOldRights.cend(); it != end; ++it) {
104 result += it.key();
105 result += ' ';
106 result += KIMAP::Acl::rightsToString(it.value());
107 result += " % "; // We use this separator as '%' is not allowed in keys or values
108 added = true;
109 }
110
111 if (added) {
112 result.chop(3);
113 }
114
115 if (d->mMyRights) {
116 result += " %% ";
117 result += KIMAP::Acl::rightsToString(d->mMyRights);
118 }
119
120 return result;
121}
122
123static void fillRightsMap(const QList<QByteArray> &rights, QMap<QByteArray, KIMAP::Acl::Rights> &map)
124{
125 for (const QByteArray &right : rights) {
126 const QByteArray trimmed = right.trimmed();
127 const int wsIndex = trimmed.indexOf(' ');
128 const QByteArray id = trimmed.mid(0, wsIndex).trimmed();
129 if (!id.isEmpty()) {
130 const bool noValue = (wsIndex == -1);
131 if (noValue) {
132 map[id] = KIMAP::Acl::None;
133 } else {
134 const QByteArray value = trimmed.mid(wsIndex + 1, right.length() - wsIndex).trimmed();
136 }
137 }
138 }
139}
140
141void ImapAclAttribute::deserialize(const QByteArray &data)
142{
143 d->mRights.clear();
144 d->mOldRights.clear();
145 d->mMyRights = KIMAP::Acl::None;
146
147 QList<QByteArray> parts;
148 int lastPos = 0;
149 int pos;
150 while ((pos = data.indexOf(" %% ", lastPos)) != -1) {
151 parts << data.mid(lastPos, pos - lastPos);
152 lastPos = pos + 4;
153 }
154 parts << data.mid(lastPos);
155
156 if (parts.size() < 2) {
157 return;
158 }
159 fillRightsMap(parts.at(0).split('%'), d->mRights);
160 fillRightsMap(parts.at(1).split('%'), d->mOldRights);
161 if (parts.size() >= 3) {
162 d->mMyRights = KIMAP::Acl::rightsFromString(parts.at(2));
163 }
164}
165
166bool ImapAclAttribute::operator==(const ImapAclAttribute &other) const
167{
168 return (oldRights() == other.oldRights()) && (rights() == other.rights()) && (myRights() == other.myRights());
169}
The ImapAclAttribute class.
KIMAP_EXPORT Rights rightsFromString(const QByteArray &string)
KIMAP_EXPORT QByteArray rightsToString(Rights rights)
folderdialogacltab.h
void chop(qsizetype n)
qsizetype indexOf(QByteArrayView bv, qsizetype from) const const
QByteArray mid(qsizetype pos, qsizetype len) const const
QByteArray trimmed() const const
const_reference at(qsizetype i) const const
qsizetype size() const const
QTextStream & right(QTextStream &stream)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:05 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.