1 /*
2     SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "ewseffectiverights.h"
8 
9 #include <QBitArray>
10 #include <QSharedData>
11 #include <QXmlStreamReader>
12 
13 #include "ewsclient_debug.h"
14 
15 class EwsEffectiveRightsPrivate : public QSharedData
16 {
17 public:
18     enum Right {
19         CreateAssociated = 0,
20         CreateContents,
21         CreateHierarchy,
22         Delete,
23         Modify,
24         Read,
25         ViewPrivateItems,
26     };
27 
28     EwsEffectiveRightsPrivate();
29     virtual ~EwsEffectiveRightsPrivate();
30 
31     bool readRight(QXmlStreamReader &reader, Right right);
32 
33     bool mValid;
34 
35     QBitArray mRights;
36 };
37 
EwsEffectiveRightsPrivate()38 EwsEffectiveRightsPrivate::EwsEffectiveRightsPrivate()
39     : mValid(false)
40     , mRights(7)
41 {
42 }
43 
~EwsEffectiveRightsPrivate()44 EwsEffectiveRightsPrivate::~EwsEffectiveRightsPrivate()
45 {
46 }
47 
readRight(QXmlStreamReader & reader,Right right)48 bool EwsEffectiveRightsPrivate::readRight(QXmlStreamReader &reader, Right right)
49 {
50     QString elm = reader.name().toString();
51     if (reader.error() != QXmlStreamReader::NoError) {
52         qCWarning(EWSCLI_LOG) << QStringLiteral("Failed to read %1 element - invalid %2 element.").arg(QStringLiteral("EffectiveRights"), elm);
53         return false;
54     }
55 
56     const QString text = reader.readElementText();
57     if (text == QLatin1String("true")) {
58         mRights.setBit(right);
59     } else if (text == QLatin1String("false")) {
60         mRights.clearBit(right);
61     } else {
62         qCWarning(EWSCLI_LOG) << QStringLiteral("Failed to read %1 element - invalid %2 element value: %3.").arg(QStringLiteral("EffectiveRights"), elm, text);
63         return false;
64     }
65 
66     return true;
67 }
68 
EwsEffectiveRights()69 EwsEffectiveRights::EwsEffectiveRights()
70     : d(new EwsEffectiveRightsPrivate())
71 {
72 }
73 
EwsEffectiveRights(QXmlStreamReader & reader)74 EwsEffectiveRights::EwsEffectiveRights(QXmlStreamReader &reader)
75     : d(new EwsEffectiveRightsPrivate())
76 {
77     while (reader.readNextStartElement()) {
78         if (reader.namespaceUri() != ewsTypeNsUri) {
79             qCWarningNC(EWSCLI_LOG) << QStringLiteral("Unexpected namespace in mailbox element:") << reader.namespaceUri();
80             return;
81         }
82         const QStringRef readerName = reader.name();
83         if (readerName == QLatin1String("CreateAssociated")) {
84             if (!d->readRight(reader, EwsEffectiveRightsPrivate::CreateAssociated)) {
85                 return;
86             }
87         } else if (readerName == QLatin1String("CreateContents")) {
88             if (!d->readRight(reader, EwsEffectiveRightsPrivate::CreateContents)) {
89                 return;
90             }
91         } else if (readerName == QLatin1String("CreateHierarchy")) {
92             if (!d->readRight(reader, EwsEffectiveRightsPrivate::CreateHierarchy)) {
93                 return;
94             }
95         } else if (readerName == QLatin1String("Delete")) {
96             if (!d->readRight(reader, EwsEffectiveRightsPrivate::Delete)) {
97                 return;
98             }
99         } else if (readerName == QLatin1String("Modify")) {
100             if (!d->readRight(reader, EwsEffectiveRightsPrivate::Modify)) {
101                 return;
102             }
103         } else if (readerName == QLatin1String("Read")) {
104             if (!d->readRight(reader, EwsEffectiveRightsPrivate::Read)) {
105                 return;
106             }
107         } else if (readerName == QLatin1String("ViewPrivateItems")) {
108             if (!d->readRight(reader, EwsEffectiveRightsPrivate::ViewPrivateItems)) {
109                 return;
110             }
111         } else {
112             qCWarning(EWSCLI_LOG)
113                 << QStringLiteral("Failed to read %1 element - unknown element: %2.").arg(QStringLiteral("EffectiveRights"), readerName.toString());
114             return;
115         }
116     }
117 
118     d->mValid = true;
119 }
120 
EwsEffectiveRights(const EwsEffectiveRights & other)121 EwsEffectiveRights::EwsEffectiveRights(const EwsEffectiveRights &other)
122     : d(other.d)
123 {
124 }
125 
EwsEffectiveRights(EwsEffectiveRights && other)126 EwsEffectiveRights::EwsEffectiveRights(EwsEffectiveRights &&other)
127     : d(std::move(other.d))
128 {
129 }
130 
~EwsEffectiveRights()131 EwsEffectiveRights::~EwsEffectiveRights()
132 {
133 }
134 
operator =(const EwsEffectiveRights & other)135 EwsEffectiveRights &EwsEffectiveRights::operator=(const EwsEffectiveRights &other)
136 {
137     d = other.d;
138     return *this;
139 }
140 
operator =(EwsEffectiveRights && other)141 EwsEffectiveRights &EwsEffectiveRights::operator=(EwsEffectiveRights &&other)
142 {
143     d = std::move(other.d);
144     return *this;
145 }
146 
isValid() const147 bool EwsEffectiveRights::isValid() const
148 {
149     return d->mValid;
150 }
151 
canCreateAssociated() const152 bool EwsEffectiveRights::canCreateAssociated() const
153 {
154     return d->mRights.testBit(EwsEffectiveRightsPrivate::CreateAssociated);
155 }
156 
canCreateContents() const157 bool EwsEffectiveRights::canCreateContents() const
158 {
159     return d->mRights.testBit(EwsEffectiveRightsPrivate::CreateContents);
160 }
161 
canCreateHierarchy() const162 bool EwsEffectiveRights::canCreateHierarchy() const
163 {
164     return d->mRights.testBit(EwsEffectiveRightsPrivate::CreateHierarchy);
165 }
166 
canDelete() const167 bool EwsEffectiveRights::canDelete() const
168 {
169     return d->mRights.testBit(EwsEffectiveRightsPrivate::Delete);
170 }
171 
canModify() const172 bool EwsEffectiveRights::canModify() const
173 {
174     return d->mRights.testBit(EwsEffectiveRightsPrivate::Modify);
175 }
176 
canRead() const177 bool EwsEffectiveRights::canRead() const
178 {
179     return d->mRights.testBit(EwsEffectiveRightsPrivate::Read);
180 }
181 
canViewPrivateItems() const182 bool EwsEffectiveRights::canViewPrivateItems() const
183 {
184     return d->mRights.testBit(EwsEffectiveRightsPrivate::ViewPrivateItems);
185 }
186