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 "folderrequestjob.h"
0010 
0011 #include "davmanager.h"
0012 #include "davutils.h"
0013 #include "folderutils.h"
0014 #include "oxutils.h"
0015 
0016 #include <KIO/DavJob>
0017 #include <QDomDocument>
0018 
0019 using namespace OXA;
0020 
0021 FolderRequestJob::FolderRequestJob(const Folder &folder, QObject *parent)
0022     : KJob(parent)
0023     , mFolder(folder)
0024 {
0025 }
0026 
0027 void FolderRequestJob::start()
0028 {
0029     QDomDocument document;
0030     QDomElement multistatus = DAVUtils::addDavElement(document, document, QStringLiteral("multistatus"));
0031     QDomElement prop = DAVUtils::addDavElement(document, multistatus, QStringLiteral("prop"));
0032     DAVUtils::addOxElement(document, prop, QStringLiteral("object_id"), OXUtils::writeNumber(mFolder.objectId()));
0033 
0034     const QString path = QStringLiteral("/servlet/webdav.folders");
0035 
0036     KIO::DavJob *job = DavManager::self()->createFindJob(path, document);
0037     connect(job, &KIO::DavJob::result, this, &FolderRequestJob::davJobFinished);
0038 }
0039 
0040 Folder FolderRequestJob::folder() const
0041 {
0042     return mFolder;
0043 }
0044 
0045 void FolderRequestJob::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     const QByteArray ba = davJob->responseData();
0056     QDomDocument document;
0057     document.setContent(ba);
0058     QString errorText, errorStatus;
0059     if (DAVUtils::davErrorOccurred(document, errorText, errorStatus)) {
0060         setError(UserDefinedError);
0061         setErrorText(errorText);
0062         emitResult();
0063         return;
0064     }
0065 
0066     QDomElement multistatus = document.documentElement();
0067     QDomElement response = multistatus.firstChildElement(QStringLiteral("response"));
0068     const QDomNodeList props = response.elementsByTagName(QStringLiteral("prop"));
0069     const QDomElement prop = props.at(0).toElement();
0070     mFolder = FolderUtils::parseFolder(prop);
0071 
0072     emitResult();
0073 }
0074 
0075 #include "moc_folderrequestjob.cpp"