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

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