File indexing completed on 2024-12-01 04:48:03

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