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 "objectdeletejob.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 #include <QDomElement>
0019 
0020 using namespace OXA;
0021 
0022 ObjectDeleteJob::ObjectDeleteJob(const Object &object, QObject *parent)
0023     : KJob(parent)
0024     , mObject(object)
0025 {
0026 }
0027 
0028 void ObjectDeleteJob::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("method"), OXUtils::writeString(QStringLiteral("DELETE")));
0037     DAVUtils::addOxElement(document, prop, QStringLiteral("last_modified"), OXUtils::writeString(mObject.lastModified()));
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, &ObjectDeleteJob::davJobFinished);
0043 }
0044 
0045 void ObjectDeleteJob::davJobFinished(KJob *job)
0046 {
0047     if (job->error()) {
0048         setError(job->error());
0049         setErrorText(job->errorText());
0050         emitResult();
0051         return;
0052     }
0053 
0054     auto davJob = qobject_cast<KIO::DavJob *>(job);
0055 
0056     const QByteArray ba = davJob->responseData();
0057     QDomDocument document;
0058     document.setContent(ba);
0059 
0060     QString errorText, errorStatus;
0061     if (DAVUtils::davErrorOccurred(document, errorText, errorStatus)) {
0062         setError(UserDefinedError);
0063         setErrorText(errorText);
0064         emitResult();
0065         return;
0066     }
0067 
0068     emitResult();
0069 }
0070 
0071 #include "moc_objectdeletejob.cpp"