1 /*
2     SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "protocolinfo.h"
8 #include "utils_p.h"
9 
10 #include "enums.h"
11 
12 #include "davitem.h"
13 #include "davmanager_p.h"
14 #include "davprotocolbase_p.h"
15 
16 #include <QString>
17 
18 #include "libkdav_debug.h"
19 
20 using namespace KDAV;
21 
firstChildElementNS(const QDomElement & parent,const QString & namespaceUri,const QString & tagName)22 QDomElement Utils::firstChildElementNS(const QDomElement &parent, const QString &namespaceUri, const QString &tagName)
23 {
24     for (QDomNode child = parent.firstChild(); !child.isNull(); child = child.nextSibling()) {
25         if (child.isElement()) {
26             const QDomElement elt = child.toElement();
27             if (tagName.isEmpty() || (elt.tagName() == tagName && elt.namespaceURI() == namespaceUri)) {
28                 return elt;
29             }
30         }
31     }
32 
33     return QDomElement();
34 }
35 
nextSiblingElementNS(const QDomElement & element,const QString & namespaceUri,const QString & tagName)36 QDomElement Utils::nextSiblingElementNS(const QDomElement &element, const QString &namespaceUri, const QString &tagName)
37 {
38     for (QDomNode sib = element.nextSibling(); !sib.isNull(); sib = sib.nextSibling()) {
39         if (sib.isElement()) {
40             const QDomElement elt = sib.toElement();
41             if (tagName.isEmpty() || (elt.tagName() == tagName && elt.namespaceURI() == namespaceUri)) {
42                 return elt;
43             }
44         }
45     }
46 
47     return QDomElement();
48 }
49 
extractPrivileges(const QDomElement & element)50 Privileges Utils::extractPrivileges(const QDomElement &element)
51 {
52     Privileges final = None;
53     QDomElement privElement = firstChildElementNS(element, QStringLiteral("DAV:"), QStringLiteral("privilege"));
54 
55     while (!privElement.isNull()) {
56         QDomElement child = privElement.firstChildElement();
57 
58         while (!child.isNull()) {
59             final |= parsePrivilege(child);
60             child = child.nextSiblingElement();
61         }
62 
63         privElement = Utils::nextSiblingElementNS(privElement, QStringLiteral("DAV:"), QStringLiteral("privilege"));
64     }
65 
66     return final;
67 }
68 
parsePrivilege(const QDomElement & element)69 Privileges Utils::parsePrivilege(const QDomElement &element)
70 {
71     Privileges final = None;
72 
73     if (!element.childNodes().isEmpty()) {
74         // This is an aggregate privilege, parse each of its children
75         QDomElement child = element.firstChildElement();
76         while (!child.isNull()) {
77             final |= parsePrivilege(child);
78             child = child.nextSiblingElement();
79         }
80     } else {
81         // This is a normal privilege
82         const QString privname = element.localName();
83 
84         if (privname == QLatin1String("read")) {
85             final |= KDAV::Read;
86         } else if (privname == QLatin1String("write")) {
87             final |= KDAV::Write;
88         } else if (privname == QLatin1String("write-properties")) {
89             final |= KDAV::WriteProperties;
90         } else if (privname == QLatin1String("write-content")) {
91             final |= KDAV::WriteContent;
92         } else if (privname == QLatin1String("unlock")) {
93             final |= KDAV::Unlock;
94         } else if (privname == QLatin1String("read-acl")) {
95             final |= KDAV::ReadAcl;
96         } else if (privname == QLatin1String("read-current-user-privilege-set")) {
97             final |= KDAV::ReadCurrentUserPrivilegeSet;
98         } else if (privname == QLatin1String("write-acl")) {
99             final |= KDAV::WriteAcl;
100         } else if (privname == QLatin1String("bind")) {
101             final |= KDAV::Bind;
102         } else if (privname == QLatin1String("unbind")) {
103             final |= KDAV::Unbind;
104         } else if (privname == QLatin1String("all")) {
105             final |= KDAV::All;
106         }
107     }
108 
109     return final;
110 }
111