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

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 "objectrequestjob.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 
0019 using namespace OXA;
0020 
0021 ObjectRequestJob::ObjectRequestJob(const Object &object, QObject *parent)
0022     : KJob(parent)
0023     , mObject(object)
0024 {
0025 }
0026 
0027 void ObjectRequestJob::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(mObject.objectId()));
0033 
0034     const QString path = ObjectUtils::davPath(mObject.module());
0035 
0036     KIO::DavJob *job = DavManager::self()->createFindJob(path, document);
0037     connect(job, &KIO::DavJob::result, this, &ObjectRequestJob::davJobFinished);
0038 }
0039 
0040 Object ObjectRequestJob::object() const
0041 {
0042     return mObject;
0043 }
0044 
0045 void ObjectRequestJob::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     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     mObject = ObjectUtils::parseObject(prop, mObject.module());
0073 
0074     emitResult();
0075 }
0076 
0077 #include "moc_objectrequestjob.cpp"