File indexing completed on 2024-11-17 04:44:00

0001 /*
0002     Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
0003     Copyright (c) 2018 RĂ©mi Nicole <minijackson@riseup.net>
0004 
0005     This program is free software; you can redistribute it and/or modify
0006     it under the terms of the GNU General Public License as published by
0007     the Free Software Foundation; either version 2 of the License, or
0008     (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License
0016     along with this program; if not, write to the Free Software
0017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "davcollectionfetchjob.h"
0021 
0022 #include <QString>
0023 
0024 #include "daverror.h"
0025 #include "davjob.h"
0026 #include "davmanager.h"
0027 #include "davprotocolbase.h"
0028 #include "utils.h"
0029 
0030 using namespace KDAV2;
0031 
0032 DavCollectionFetchJob::DavCollectionFetchJob(const DavCollection &collection, QObject *parent)
0033     : DavJobBase(parent), mCollection(collection)
0034 {
0035 }
0036 
0037 void DavCollectionFetchJob::start()
0038 {
0039     const DavProtocolBase *protocol = DavManager::self()->davProtocol(mCollection.url().protocol());
0040     Q_ASSERT(protocol);
0041     XMLQueryBuilder::Ptr builder(protocol->collectionsQuery());
0042 
0043     auto job = DavManager::self()->createPropFindJob(
0044         mCollection.url().url(), builder->buildQuery(), /* depth = */ QStringLiteral("0"));
0045     connect(job, &DavJob::result, this, &DavCollectionFetchJob::davJobFinished);
0046 }
0047 
0048 DavCollection DavCollectionFetchJob::collection() const
0049 {
0050     return mCollection;
0051 }
0052 
0053 void DavCollectionFetchJob::davJobFinished(KJob *job)
0054 {
0055     auto storedJob = static_cast<DavJob*>(job);
0056     if (storedJob->error()) {
0057         setErrorFromJob(storedJob);
0058     } else {
0059         /*
0060          * Extract data from a document like the following:
0061          *
0062          * <multistatus xmlns="DAV:">
0063          *   <response xmlns="DAV:">
0064          *     <href xmlns="DAV:">/path/to/collection/</href>
0065          *     <propstat xmlns="DAV:">
0066          *       <prop xmlns="DAV:">
0067          *         <displayname>collection name</displayname>
0068          *         <resourcetype>
0069          *           <collection/>
0070          *           <card:addressbook/>
0071          *         </resourcetype>
0072          *         <cs:getctag>ctag</cs:getctag>
0073          *       </prop>
0074          *       <status xmlns="DAV:">HTTP/1.1 200 OK</status>
0075          *     </propstat>
0076          *   </response>
0077          * </multistatus>
0078          */
0079 
0080         const QDomDocument document       = storedJob->response();
0081 
0082         const QDomElement documentElement = document.documentElement();
0083         QDomElement responseElement       = Utils::firstChildElementNS(
0084             documentElement, QStringLiteral("DAV:"), QStringLiteral("response"));
0085 
0086         // Validate that we got a valid PROPFIND response
0087         if (documentElement.localName().compare(QStringLiteral("multistatus"), Qt::CaseInsensitive) != 0) {
0088             setError(ERR_COLLECTIONFETCH);
0089             setErrorTextFromDavError();
0090             emitResult();
0091             return;
0092         }
0093 
0094         if (!Utils::extractCollection(responseElement, mCollection.url(), mCollection)) {
0095             setError(ERR_COLLECTIONFETCH);
0096             setErrorTextFromDavError();
0097             emitResult();
0098             return;
0099         }
0100     }
0101 
0102     emitResult();
0103 }