Libkdav2

davcollectionfetchjob.cpp
1 /*
2  Copyright (c) 2010 Tobias Koenig <[email protected]>
3  Copyright (c) 2018 RĂ©mi Nicole <[email protected]>
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 
30 using 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 
43  auto job = DavManager::self()->createPropFindJob(
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 
53 void 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();
83  QDomElement responseElement = Utils::firstChildElementNS(
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 }
DavJob * createPropFindJob(const QUrl &url, const QDomDocument &document, const QString &depth=QStringLiteral("1"))
Returns a preconfigured DAV PROPFIND job.
Definition: davmanager.cpp:66
QUrl url() const
Returns the url that identifies the DAV object.
Definition: davurl.cpp:41
DavUrl url() const
Returns the url that identifies the collection.
A helper class to store information about DAV collection.
Definition: davcollection.h:49
void emitResult()
static DavManager * self()
Returns the global instance of the DAV manager.
Definition: davmanager.cpp:52
base class for the jobs used by the resource.
Definition: davjobbase.h:37
DavCollectionFetchJob(const DavCollection &collection, QObject *parent=nullptr)
Creates a new dav collection fetch job.
void setError(int errorCode)
QDomElement documentElement() const const
DavCollection collection() const
Returns the fetched collection including current etag information.
QString localName() const const
bool extractCollection(const QDomElement &response, DavUrl url, DavCollection &collection)
Extract a DavCollection from the response element of a PROPFIND result.
Definition: utils.cpp:185
const DavProtocolBase * davProtocol(Protocol protocol)
Returns the DAV protocol dialect object for the given DAV protocol.
Definition: davmanager.cpp:140
CaseInsensitive
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
Protocol protocol() const
Returns the DAV protocol dialect that is used to retrieve the DAV object.
Definition: davurl.cpp:51
void setErrorFromJob(DavJob *, ErrorNumber jobErrorCode=ERR_PROBLEM_WITH_REQUEST)
Set the error of this job from a failed DavJob (executed by this job).
Definition: davjobbase.cpp:99
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int compare(const QString &other, Qt::CaseSensitivity cs) const const
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.
void start() override
Starts the job.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 23:10:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.