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

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 "objectmodifyjob.h"
0010 
0011 #include "davmanager.h"
0012 #include "davutils.h"
0013 #include "objectutils.h"
0014 #include "oxutils.h"
0015 
0016 #include <KIO/DavJob>
0017 #include <QDomDocument>
0018 
0019 using namespace OXA;
0020 
0021 ObjectModifyJob::ObjectModifyJob(const Object &object, QObject *parent)
0022     : KJob(parent)
0023     , mObject(object)
0024 {
0025 }
0026 
0027 void ObjectModifyJob::start()
0028 {
0029     if (ObjectUtils::needsPreloading(mObject)) {
0030         KJob *job = ObjectUtils::preloadJob(mObject);
0031         connect(job, &KJob::result, this, &ObjectModifyJob::preloadingJobFinished);
0032         job->start();
0033     } else {
0034         QDomDocument document;
0035         QDomElement propertyupdate = DAVUtils::addDavElement(document, document, QStringLiteral("propertyupdate"));
0036         QDomElement set = DAVUtils::addDavElement(document, propertyupdate, QStringLiteral("set"));
0037         QDomElement prop = DAVUtils::addDavElement(document, set, QStringLiteral("prop"));
0038 
0039         ObjectUtils::addObjectElements(document, prop, mObject);
0040 
0041         const QString path = ObjectUtils::davPath(mObject.module());
0042 
0043         KIO::DavJob *job = DavManager::self()->createPatchJob(path, document);
0044         connect(job, &KJob::result, this, &ObjectModifyJob::davJobFinished);
0045     }
0046 }
0047 
0048 Object ObjectModifyJob::object() const
0049 {
0050     return mObject;
0051 }
0052 
0053 void ObjectModifyJob::preloadingJobFinished(KJob *job)
0054 {
0055     void *preloadedData = ObjectUtils::preloadData(mObject, job);
0056 
0057     QDomDocument document;
0058     QDomElement propertyupdate = DAVUtils::addDavElement(document, document, QStringLiteral("propertyupdate"));
0059     QDomElement set = DAVUtils::addDavElement(document, propertyupdate, QStringLiteral("set"));
0060     QDomElement prop = DAVUtils::addDavElement(document, set, QStringLiteral("prop"));
0061 
0062     ObjectUtils::addObjectElements(document, prop, mObject, preloadedData);
0063 
0064     const QString path = ObjectUtils::davPath(mObject.module());
0065 
0066     KIO::DavJob *davJob = DavManager::self()->createPatchJob(path, document);
0067     connect(davJob, &KIO::DavJob::result, this, &ObjectModifyJob::davJobFinished);
0068 }
0069 
0070 void ObjectModifyJob::davJobFinished(KJob *job)
0071 {
0072     if (job->error()) {
0073         setError(job->error());
0074         setErrorText(job->errorText());
0075         emitResult();
0076         return;
0077     }
0078 
0079     auto davJob = qobject_cast<KIO::DavJob *>(job);
0080 
0081     const QByteArray ba = davJob->responseData();
0082     QDomDocument document;
0083     document.setContent(ba);
0084 
0085     QString errorText, errorStatus;
0086     if (DAVUtils::davErrorOccurred(document, errorText, errorStatus)) {
0087         setError(UserDefinedError);
0088         setErrorText(errorText);
0089         emitResult();
0090         return;
0091     }
0092 
0093     QDomElement multistatus = document.documentElement();
0094     QDomElement response = multistatus.firstChildElement(QStringLiteral("response"));
0095     const QDomNodeList props = response.elementsByTagName(QStringLiteral("prop"));
0096     const QDomElement prop = props.at(0).toElement();
0097 
0098     QDomElement element = prop.firstChildElement();
0099     while (!element.isNull()) {
0100         if (element.tagName() == QLatin1StringView("last_modified")) {
0101             mObject.setLastModified(OXUtils::readString(element.text()));
0102         }
0103 
0104         element = element.nextSiblingElement();
0105     }
0106 
0107     emitResult();
0108 }
0109 
0110 #include "moc_objectmodifyjob.cpp"