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