File indexing completed on 2024-12-01 04:48:04
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 "objectcreatejob.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 ObjectCreateJob::ObjectCreateJob(const Object &object, QObject *parent) 0022 : KJob(parent) 0023 , mObject(object) 0024 { 0025 } 0026 0027 void ObjectCreateJob::start() 0028 { 0029 if (ObjectUtils::needsPreloading(mObject)) { 0030 KJob *job = ObjectUtils::preloadJob(mObject); 0031 connect(job, &KJob::result, this, &ObjectCreateJob::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, &ObjectCreateJob::davJobFinished); 0045 } 0046 } 0047 0048 Object ObjectCreateJob::object() const 0049 { 0050 return mObject; 0051 } 0052 0053 void ObjectCreateJob::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, &ObjectCreateJob::davJobFinished); 0068 } 0069 0070 void ObjectCreateJob::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 const QDomElement multistatus = document.documentElement(); 0094 const 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("object_id")) { 0101 mObject.setObjectId(OXUtils::readNumber(element.text())); 0102 } else if (element.tagName() == QLatin1StringView("last_modified")) { 0103 mObject.setLastModified(OXUtils::readString(element.text())); 0104 } 0105 0106 element = element.nextSiblingElement(); 0107 } 0108 0109 emitResult(); 0110 } 0111 0112 #include "moc_objectcreatejob.cpp"