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 "objectsrequestjob.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 ObjectsRequestJob::ObjectsRequestJob(const Folder &folder, qulonglong lastSync, Mode mode, QObject *parent)
0022     : KJob(parent)
0023     , mFolder(folder)
0024     , mLastSync(lastSync)
0025     , mMode(mode)
0026 {
0027 }
0028 
0029 void ObjectsRequestJob::start()
0030 {
0031     QDomDocument document;
0032     QDomElement multistatus = DAVUtils::addDavElement(document, document, QStringLiteral("multistatus"));
0033     QDomElement prop = DAVUtils::addDavElement(document, multistatus, QStringLiteral("prop"));
0034     DAVUtils::addOxElement(document, prop, QStringLiteral("folder_id"), OXUtils::writeNumber(mFolder.objectId()));
0035     DAVUtils::addOxElement(document, prop, QStringLiteral("lastsync"), OXUtils::writeNumber(mLastSync));
0036     if (mMode == Modified) {
0037         DAVUtils::addOxElement(document, prop, QStringLiteral("objectmode"), QStringLiteral("MODIFIED"));
0038     } else {
0039         DAVUtils::addOxElement(document, prop, QStringLiteral("objectmode"), QStringLiteral("DELETED"));
0040     }
0041 
0042     const QString path = ObjectUtils::davPath(mFolder.module());
0043 
0044     KIO::DavJob *job = DavManager::self()->createFindJob(path, document);
0045     connect(job, &KIO::DavJob::result, this, &ObjectsRequestJob::davJobFinished);
0046 }
0047 
0048 Object::List ObjectsRequestJob::objects() const
0049 {
0050     return mObjects;
0051 }
0052 
0053 void ObjectsRequestJob::davJobFinished(KJob *job)
0054 {
0055     if (job->error()) {
0056         setError(job->error());
0057         setErrorText(job->errorText());
0058         emitResult();
0059         return;
0060     }
0061 
0062     auto davJob = qobject_cast<KIO::DavJob *>(job);
0063 
0064     const QByteArray ba = davJob->responseData();
0065     QDomDocument document;
0066     document.setContent(ba);
0067 
0068     QString errorText, errorStatus;
0069     if (DAVUtils::davErrorOccurred(document, errorText, errorStatus)) {
0070         setError(UserDefinedError);
0071         setErrorText(errorText);
0072         emitResult();
0073         return;
0074     }
0075 
0076     QDomElement multistatus = document.documentElement();
0077     QDomElement response = multistatus.firstChildElement(QStringLiteral("response"));
0078     while (!response.isNull()) {
0079         const QDomNodeList props = response.elementsByTagName(QStringLiteral("prop"));
0080         const QDomElement prop = props.at(0).toElement();
0081         mObjects.append(ObjectUtils::parseObject(prop, mFolder.module()));
0082         response = response.nextSiblingElement();
0083     }
0084 
0085     emitResult();
0086 }
0087 
0088 #include "moc_objectsrequestjob.cpp"