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 "foldercreatejob.h"
0010 
0011 #include "davmanager.h"
0012 #include "davutils.h"
0013 #include "folderutils.h"
0014 #include "oxutils.h"
0015 
0016 #include <KIO/DavJob>
0017 
0018 using namespace OXA;
0019 
0020 FolderCreateJob::FolderCreateJob(const Folder &folder, QObject *parent)
0021     : KJob(parent)
0022     , mFolder(folder)
0023 {
0024 }
0025 
0026 void FolderCreateJob::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 
0033     FolderUtils::addFolderElements(document, prop, mFolder);
0034 
0035     const QString path = QStringLiteral("/servlet/webdav.folders");
0036 
0037     KIO::DavJob *job = DavManager::self()->createPatchJob(path, document);
0038     connect(job, &KIO::DavJob::result, this, &FolderCreateJob::davJobFinished);
0039 }
0040 
0041 Folder FolderCreateJob::folder() const
0042 {
0043     return mFolder;
0044 }
0045 
0046 void FolderCreateJob::davJobFinished(KJob *job)
0047 {
0048     if (job->error()) {
0049         setError(job->error());
0050         setErrorText(job->errorText());
0051         emitResult();
0052         return;
0053     }
0054 
0055     auto davJob = qobject_cast<KIO::DavJob *>(job);
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     QDomElement multistatus = document.documentElement();
0069     QDomElement response = multistatus.firstChildElement(QStringLiteral("response"));
0070     const QDomNodeList props = response.elementsByTagName(QStringLiteral("prop"));
0071     const QDomElement prop = props.at(0).toElement();
0072 
0073     QDomElement element = prop.firstChildElement();
0074     while (!element.isNull()) {
0075         if (element.tagName() == QLatin1StringView("object_id")) {
0076             mFolder.setObjectId(OXUtils::readNumber(element.text()));
0077         } else 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_foldercreatejob.cpp"