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 "objectmovejob.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 ObjectMoveJob::ObjectMoveJob(const Object &object, const Folder &destinationFolder, QObject *parent)
0022     : KJob(parent)
0023     , mObject(object)
0024     , mDestinationFolder(destinationFolder)
0025 {
0026 }
0027 
0028 void ObjectMoveJob::start()
0029 {
0030     QDomDocument document;
0031     QDomElement propertyupdate = DAVUtils::addDavElement(document, document, QStringLiteral("propertyupdate"));
0032     QDomElement set = DAVUtils::addDavElement(document, propertyupdate, QStringLiteral("set"));
0033     QDomElement prop = DAVUtils::addDavElement(document, set, QStringLiteral("prop"));
0034     DAVUtils::addOxElement(document, prop, QStringLiteral("object_id"), OXUtils::writeNumber(mObject.objectId()));
0035     DAVUtils::addOxElement(document, prop, QStringLiteral("folder_id"), OXUtils::writeNumber(mObject.folderId()));
0036     DAVUtils::addOxElement(document, prop, QStringLiteral("last_modified"), OXUtils::writeString(mObject.lastModified()));
0037     DAVUtils::addOxElement(document, prop, QStringLiteral("folder"), OXUtils::writeNumber(mDestinationFolder.objectId()));
0038 
0039     const QString path = ObjectUtils::davPath(mObject.module());
0040 
0041     KIO::DavJob *job = DavManager::self()->createPatchJob(path, document);
0042     connect(job, &KIO::DavJob::result, this, &ObjectMoveJob::davJobFinished);
0043 }
0044 
0045 Object ObjectMoveJob::object() const
0046 {
0047     return mObject;
0048 }
0049 
0050 void ObjectMoveJob::davJobFinished(KJob *job)
0051 {
0052     if (job->error()) {
0053         setError(job->error());
0054         setErrorText(job->errorText());
0055         emitResult();
0056         return;
0057     }
0058 
0059     auto davJob = qobject_cast<KIO::DavJob *>(job);
0060 
0061     const QByteArray ba = davJob->responseData();
0062     QDomDocument document;
0063     document.setContent(ba);
0064 
0065     QString errorText, errorStatus;
0066     if (DAVUtils::davErrorOccurred(document, errorText, errorStatus)) {
0067         setError(UserDefinedError);
0068         setErrorText(errorText);
0069         emitResult();
0070         return;
0071     }
0072 
0073     QDomElement multistatus = document.documentElement();
0074     QDomElement response = multistatus.firstChildElement(QStringLiteral("response"));
0075     const QDomNodeList props = response.elementsByTagName(QStringLiteral("prop"));
0076     const QDomElement prop = props.at(0).toElement();
0077 
0078     QDomElement element = prop.firstChildElement();
0079     while (!element.isNull()) {
0080         if (element.tagName() == QLatin1StringView("last_modified")) {
0081             mObject.setLastModified(OXUtils::readString(element.text()));
0082         }
0083 
0084         element = element.nextSiblingElement();
0085     }
0086 
0087     emitResult();
0088 }
0089 
0090 #include "moc_objectmovejob.cpp"