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

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 "objectutils.h"
0010 
0011 #include "contactutils.h"
0012 #include "davutils.h"
0013 #include "incidenceutils.h"
0014 #include "oxutils.h"
0015 
0016 #include <QDomElement>
0017 
0018 using namespace OXA;
0019 
0020 Object OXA::ObjectUtils::parseObject(const QDomElement &propElement, Folder::Module module)
0021 {
0022     Object object;
0023 
0024     QDomElement element = propElement.firstChildElement();
0025     while (!element.isNull()) {
0026         if (element.tagName() == QLatin1StringView("last_modified")) {
0027             object.setLastModified(OXUtils::readString(element.text()));
0028         } else if (element.tagName() == QLatin1StringView("object_id")) {
0029             object.setObjectId(OXUtils::readNumber(element.text()));
0030         } else if (element.tagName() == QLatin1StringView("folder_id")) {
0031             object.setFolderId(OXUtils::readNumber(element.text()));
0032         } else if (element.tagName() == QLatin1StringView("object_status")) {
0033             const QString content = OXUtils::readString(element.text());
0034             if (content == QLatin1StringView("CREATE")) {
0035                 object.setObjectStatus(Object::Created);
0036             } else if (content == QLatin1StringView("DELETE")) {
0037                 object.setObjectStatus(Object::Deleted);
0038             } else {
0039                 Q_ASSERT(false);
0040             }
0041         }
0042 
0043         element = element.nextSiblingElement();
0044     }
0045 
0046     switch (module) {
0047     case Folder::Contacts:
0048         ContactUtils::parseContact(propElement, object);
0049         break;
0050     case Folder::Calendar:
0051         IncidenceUtils::parseEvent(propElement, object);
0052         break;
0053     case Folder::Tasks:
0054         IncidenceUtils::parseTask(propElement, object);
0055         break;
0056     case Folder::Unbound:
0057         Q_ASSERT(false);
0058         break;
0059     }
0060 
0061     return object;
0062 }
0063 
0064 void OXA::ObjectUtils::addObjectElements(QDomDocument &document, QDomElement &propElement, const Object &object, void *preloadedData)
0065 {
0066     if (object.objectId() != -1) {
0067         DAVUtils::addOxElement(document, propElement, QStringLiteral("object_id"), OXUtils::writeNumber(object.objectId()));
0068     }
0069     if (object.folderId() != -1) {
0070         DAVUtils::addOxElement(document, propElement, QStringLiteral("folder_id"), OXUtils::writeNumber(object.folderId()));
0071     }
0072     if (!object.lastModified().isEmpty()) {
0073         DAVUtils::addOxElement(document, propElement, QStringLiteral("last_modified"), OXUtils::writeString(object.lastModified()));
0074     }
0075 
0076     switch (object.module()) {
0077     case Folder::Contacts:
0078         ContactUtils::addContactElements(document, propElement, object, preloadedData);
0079         break;
0080     case Folder::Calendar:
0081         IncidenceUtils::addEventElements(document, propElement, object);
0082         break;
0083     case Folder::Tasks:
0084         IncidenceUtils::addTaskElements(document, propElement, object);
0085         break;
0086     case Folder::Unbound:
0087         Q_ASSERT(false);
0088         break;
0089     }
0090 }
0091 
0092 bool OXA::ObjectUtils::needsPreloading(const Object &object)
0093 {
0094     if (object.module() == Folder::Contacts) {
0095         if (object.contactGroup().contactReferenceCount() != 0) { // we have to resolve these entries first
0096             return true;
0097         }
0098     }
0099 
0100     return false;
0101 }
0102 
0103 KJob *OXA::ObjectUtils::preloadJob(const Object &object)
0104 {
0105     if (object.module() == Folder::Contacts) {
0106         if (object.contactGroup().contactReferenceCount() != 0) {
0107             return ContactUtils::preloadJob(object);
0108         }
0109     }
0110 
0111     return nullptr;
0112 }
0113 
0114 void *OXA::ObjectUtils::preloadData(const Object &object, KJob *job)
0115 {
0116     if (object.module() == Folder::Contacts) {
0117         if (object.contactGroup().contactReferenceCount() != 0) {
0118             return ContactUtils::preloadData(object, job);
0119         }
0120     }
0121 
0122     return nullptr;
0123 }
0124 
0125 QString OXA::ObjectUtils::davPath(Folder::Module module)
0126 {
0127     switch (module) {
0128     case Folder::Contacts:
0129         return QStringLiteral("/servlet/webdav.contacts");
0130     case Folder::Calendar:
0131         return QStringLiteral("/servlet/webdav.calendar");
0132     case Folder::Tasks:
0133         return QStringLiteral("/servlet/webdav.tasks");
0134     case Folder::Unbound:
0135         Q_ASSERT(false);
0136         return {};
0137     }
0138 
0139     return {};
0140 }