File indexing completed on 2024-11-24 04:44:28

0001 /*
0002     This file is part of oxaccess.
0003 
0004     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "folderutils.h"
0010 
0011 #include "davutils.h"
0012 #include "folder.h"
0013 #include "oxutils.h"
0014 
0015 #include <QDomElement>
0016 
0017 using namespace OXA;
0018 
0019 static void createFolderPermissions(const Folder &folder, QDomDocument &document, QDomElement &permissions)
0020 {
0021     {
0022         const Folder::UserPermissions userPermissions = folder.userPermissions();
0023         Folder::UserPermissions::ConstIterator it = userPermissions.constBegin();
0024         while (it != userPermissions.constEnd()) {
0025             QDomElement user = DAVUtils::addOxElement(document, permissions, QStringLiteral("user"), OXUtils::writeNumber(it.key()));
0026             DAVUtils::setOxAttribute(user, QStringLiteral("folderpermission"), OXUtils::writeNumber(it.value().folderPermission()));
0027             DAVUtils::setOxAttribute(user, QStringLiteral("objectreadpermission"), OXUtils::writeNumber(it.value().objectReadPermission()));
0028             DAVUtils::setOxAttribute(user, QStringLiteral("objectwritepermission"), OXUtils::writeNumber(it.value().objectWritePermission()));
0029             DAVUtils::setOxAttribute(user, QStringLiteral("objectdeletepermission"), OXUtils::writeNumber(it.value().objectDeletePermission()));
0030             DAVUtils::setOxAttribute(user, QStringLiteral("admin_flag"), OXUtils::writeBoolean(it.value().adminFlag()));
0031 
0032             ++it;
0033         }
0034     }
0035 
0036     {
0037         const Folder::GroupPermissions groupPermissions = folder.groupPermissions();
0038         Folder::GroupPermissions::ConstIterator it = groupPermissions.constBegin();
0039         while (it != groupPermissions.constEnd()) {
0040             QDomElement group = DAVUtils::addOxElement(document, permissions, QStringLiteral("group"), OXUtils::writeNumber(it.key()));
0041             DAVUtils::setOxAttribute(group, QStringLiteral("folderpermission"), OXUtils::writeNumber(it.value().folderPermission()));
0042             DAVUtils::setOxAttribute(group, QStringLiteral("objectreadpermission"), OXUtils::writeNumber(it.value().objectReadPermission()));
0043             DAVUtils::setOxAttribute(group, QStringLiteral("objectwritepermission"), OXUtils::writeNumber(it.value().objectWritePermission()));
0044             DAVUtils::setOxAttribute(group, QStringLiteral("objectdeletepermission"), OXUtils::writeNumber(it.value().objectDeletePermission()));
0045             DAVUtils::setOxAttribute(group, QStringLiteral("admin_flag"), OXUtils::writeBoolean(it.value().adminFlag()));
0046 
0047             ++it;
0048         }
0049     }
0050 }
0051 
0052 static void parseFolderPermissions(const QDomElement &permissions, Folder &folder)
0053 {
0054     Folder::UserPermissions userPermissions;
0055     Folder::GroupPermissions groupPermissions;
0056 
0057     QDomElement element = permissions.firstChildElement();
0058     while (!element.isNull()) {
0059         if (element.tagName() == QLatin1StringView("user")) {
0060             Folder::Permissions permissions;
0061             permissions.setFolderPermission(
0062                 (Folder::Permissions::FolderPermission)OXUtils::readNumber(element.attribute(QStringLiteral("folderpermission"), QStringLiteral("0"))));
0063             permissions.setObjectReadPermission(
0064                 (Folder::Permissions::ObjectReadPermission)OXUtils::readNumber(element.attribute(QStringLiteral("objectreadpermission"), QStringLiteral("0"))));
0065             permissions.setObjectWritePermission((Folder::Permissions::ObjectWritePermission)OXUtils::readNumber(
0066                 element.attribute(QStringLiteral("objectwritepermission"), QStringLiteral("0"))));
0067             permissions.setObjectDeletePermission((Folder::Permissions::ObjectDeletePermission)OXUtils::readNumber(
0068                 element.attribute(QStringLiteral("objectdeletepermission"), QStringLiteral("0"))));
0069             permissions.setAdminFlag(OXUtils::readBoolean(element.attribute(QStringLiteral("admin_flag"), QStringLiteral("false"))));
0070 
0071             userPermissions.insert(OXUtils::readNumber(element.text()), permissions);
0072         } else if (element.tagName() == QLatin1StringView("group")) {
0073             Folder::Permissions permissions;
0074             permissions.setFolderPermission(
0075                 (Folder::Permissions::FolderPermission)OXUtils::readNumber(element.attribute(QStringLiteral("folderpermission"), QStringLiteral("0"))));
0076             permissions.setObjectReadPermission(
0077                 (Folder::Permissions::ObjectReadPermission)OXUtils::readNumber(element.attribute(QStringLiteral("objectreadpermission"), QStringLiteral("0"))));
0078             permissions.setObjectWritePermission((Folder::Permissions::ObjectWritePermission)OXUtils::readNumber(
0079                 element.attribute(QStringLiteral("objectwritepermission"), QStringLiteral("0"))));
0080             permissions.setObjectDeletePermission((Folder::Permissions::ObjectDeletePermission)OXUtils::readNumber(
0081                 element.attribute(QStringLiteral("objectdeletepermission"), QStringLiteral("0"))));
0082             permissions.setAdminFlag(OXUtils::readBoolean(element.attribute(QStringLiteral("admin_flag"), QStringLiteral("false"))));
0083 
0084             groupPermissions.insert(OXUtils::readNumber(element.text()), permissions);
0085         }
0086 
0087         element = element.nextSiblingElement();
0088     }
0089 
0090     folder.setUserPermissions(userPermissions);
0091     folder.setGroupPermissions(groupPermissions);
0092 }
0093 
0094 Folder OXA::FolderUtils::parseFolder(const QDomElement &propElement)
0095 {
0096     Folder folder;
0097 
0098     QDomElement element = propElement.firstChildElement();
0099     while (!element.isNull()) {
0100         if (element.tagName() == QLatin1StringView("object_status")) {
0101             const QString content = OXUtils::readString(element.text());
0102             if (content == QLatin1StringView("CREATE")) {
0103                 folder.setObjectStatus(Folder::Created);
0104             } else if (content == QLatin1StringView("DELETE")) {
0105                 folder.setObjectStatus(Folder::Deleted);
0106             } else {
0107                 Q_ASSERT(false);
0108             }
0109         } else if (element.tagName() == QLatin1StringView("title")) {
0110             folder.setTitle(OXUtils::readString(element.text()));
0111         } else if (element.tagName() == QLatin1StringView("owner")) {
0112             folder.setOwner(OXUtils::readNumber(element.text()));
0113         } else if (element.tagName() == QLatin1StringView("module")) {
0114             const QString content = OXUtils::readString(element.text());
0115             if (content == QLatin1StringView("calendar")) {
0116                 folder.setModule(Folder::Calendar);
0117             } else if (content == QLatin1StringView("contact")) {
0118                 folder.setModule(Folder::Contacts);
0119             } else if (content == QLatin1StringView("task")) {
0120                 folder.setModule(Folder::Tasks);
0121             } else {
0122                 folder.setModule(Folder::Unbound);
0123             }
0124         } else if (element.tagName() == QLatin1StringView("type")) {
0125             const QString content = OXUtils::readString(element.text());
0126             if (content == QLatin1StringView("public")) {
0127                 folder.setType(Folder::Public);
0128             } else if (content == QLatin1StringView("private")) {
0129                 folder.setType(Folder::Private);
0130             } else {
0131                 Q_ASSERT(false);
0132             }
0133         } else if (element.tagName() == QLatin1StringView("defaultfolder")) {
0134             folder.setIsDefaultFolder(OXUtils::readBoolean(element.text()));
0135         } else if (element.tagName() == QLatin1StringView("last_modified")) {
0136             folder.setLastModified(OXUtils::readString(element.text()));
0137         } else if (element.tagName() == QLatin1StringView("object_id")) {
0138             folder.setObjectId(OXUtils::readNumber(element.text()));
0139         } else if (element.tagName() == QLatin1StringView("folder_id")) {
0140             folder.setFolderId(OXUtils::readNumber(element.text()));
0141         } else if (element.tagName() == QLatin1StringView("permissions")) {
0142             parseFolderPermissions(element, folder);
0143         }
0144 
0145         element = element.nextSiblingElement();
0146     }
0147 
0148     return folder;
0149 }
0150 
0151 void OXA::FolderUtils::addFolderElements(QDomDocument &document, QDomElement &propElement, const Folder &folder)
0152 {
0153     DAVUtils::addOxElement(document, propElement, QStringLiteral("title"), OXUtils::writeString(folder.title()));
0154     DAVUtils::addOxElement(document, propElement, QStringLiteral("folder_id"), OXUtils::writeNumber(folder.folderId()));
0155 
0156     const QString type = (folder.type() == Folder::Public ? QStringLiteral("public") : QStringLiteral("private"));
0157     DAVUtils::addOxElement(document, propElement, QStringLiteral("type"), OXUtils::writeString(type));
0158 
0159     QString module;
0160     switch (folder.module()) {
0161     case Folder::Calendar:
0162         module = QStringLiteral("calendar");
0163         break;
0164     case Folder::Contacts:
0165         module = QStringLiteral("contact");
0166         break;
0167     case Folder::Tasks:
0168         module = QStringLiteral("task");
0169         break;
0170     default:
0171         break;
0172     }
0173     DAVUtils::addOxElement(document, propElement, QStringLiteral("module"), OXUtils::writeString(module));
0174 
0175     QDomElement permissions = DAVUtils::addOxElement(document, propElement, QStringLiteral("permissions"));
0176     createFolderPermissions(folder, document, permissions);
0177 }