Libkdav2

davcollectionfetchjob.cpp
1/*
2 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
3 Copyright (c) 2018 RĂ©mi Nicole <minijackson@riseup.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18*/
19
20#include "davcollectionfetchjob.h"
21
22#include <QString>
23
24#include "daverror.h"
25#include "davjob.h"
26#include "davmanager.h"
27#include "davprotocolbase.h"
28#include "utils.h"
29
30using namespace KDAV2;
31
33 : DavJobBase(parent), mCollection(collection)
34{
35}
36
38{
39 const DavProtocolBase *protocol = DavManager::self()->davProtocol(mCollection.url().protocol());
40 Q_ASSERT(protocol);
41 XMLQueryBuilder::Ptr builder(protocol->collectionsQuery());
42
44 mCollection.url().url(), builder->buildQuery(), /* depth = */ QStringLiteral("0"));
45 connect(job, &DavJob::result, this, &DavCollectionFetchJob::davJobFinished);
46}
47
49{
50 return mCollection;
51}
52
53void DavCollectionFetchJob::davJobFinished(KJob *job)
54{
55 auto storedJob = static_cast<DavJob*>(job);
56 if (storedJob->error()) {
57 setErrorFromJob(storedJob);
58 } else {
59 /*
60 * Extract data from a document like the following:
61 *
62 * <multistatus xmlns="DAV:">
63 * <response xmlns="DAV:">
64 * <href xmlns="DAV:">/path/to/collection/</href>
65 * <propstat xmlns="DAV:">
66 * <prop xmlns="DAV:">
67 * <displayname>collection name</displayname>
68 * <resourcetype>
69 * <collection/>
70 * <card:addressbook/>
71 * </resourcetype>
72 * <cs:getctag>ctag</cs:getctag>
73 * </prop>
74 * <status xmlns="DAV:">HTTP/1.1 200 OK</status>
75 * </propstat>
76 * </response>
77 * </multistatus>
78 */
79
80 const QDomDocument document = storedJob->response();
81
82 const QDomElement documentElement = document.documentElement();
84 documentElement, QStringLiteral("DAV:"), QStringLiteral("response"));
85
86 // Validate that we got a valid PROPFIND response
87 if (documentElement.localName().compare(QStringLiteral("multistatus"), Qt::CaseInsensitive) != 0) {
88 setError(ERR_COLLECTIONFETCH);
89 setErrorTextFromDavError();
90 emitResult();
91 return;
92 }
93
94 if (!Utils::extractCollection(responseElement, mCollection.url(), mCollection)) {
95 setError(ERR_COLLECTIONFETCH);
96 setErrorTextFromDavError();
97 emitResult();
98 return;
99 }
100 }
101
102 emitResult();
103}
DavCollectionFetchJob(const DavCollection &collection, QObject *parent=nullptr)
Creates a new dav collection fetch job.
DavCollection collection() const
Returns the fetched collection including current etag information.
void start() override
Starts the job.
A helper class to store information about DAV collection.
DavUrl url() const
Returns the url that identifies the collection.
base class for the jobs used by the resource.
Definition davjobbase.h:38
void setErrorFromJob(DavJob *, ErrorNumber jobErrorCode=ERR_PROBLEM_WITH_REQUEST)
Set the error of this job from a failed DavJob (executed by this job).
DavJob * createPropFindJob(const QUrl &url, const QDomDocument &document, const QString &depth=QStringLiteral("1"))
Returns a preconfigured DAV PROPFIND job.
static DavManager * self()
Returns the global instance of the DAV manager.
const DavProtocolBase * davProtocol(Protocol protocol)
Returns the DAV protocol dialect object for the given DAV protocol.
Base class for various DAV groupware dialects.
virtual XMLQueryBuilder::Ptr collectionsQuery() const =0
Returns the XML document that represents the DAV query to list all available DAV collections.
QUrl url() const
Returns the url that identifies the DAV object.
Definition davurl.cpp:41
Protocol protocol() const
Returns the DAV protocol dialect that is used to retrieve the DAV object.
Definition davurl.cpp:51
void emitResult()
void result(KJob *job)
void setError(int errorCode)
bool extractCollection(const QDomElement &response, DavUrl url, DavCollection &collection)
Extract a DavCollection from the response element of a PROPFIND result.
Definition utils.cpp:185
QDomElement KPIMKDAV2_EXPORT firstChildElementNS(const QDomElement &parent, const QString &namespaceUri, const QString &tagName)
Returns the first child element of parent that has the given tagName and is part of the namespaceUri.
Definition utils.cpp:37
QDomElement documentElement() const const
QString localName() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.