KDAV

davitemfetchjob.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "davitemfetchjob.h"
8#include "davjobbase_p.h"
9
10#include "daverror.h"
11
12#include <KIO/DavJob>
13#include <KIO/StoredTransferJob>
14
15using namespace KDAV;
16namespace KDAV
17{
18class DavItemFetchJobPrivate : public DavJobBasePrivate
19{
20public:
21 void davJobFinished(KJob *job);
22
23 DavUrl mUrl;
24 DavItem mItem;
25};
26}
27
28static QString etagFromHeaders(const QString &headers)
29{
30 const QStringList allHeaders = headers.split(QLatin1Char('\n'));
31
32 QString etag;
33 for (const QString &header : allHeaders) {
34 if (header.startsWith(QLatin1String("etag:"), Qt::CaseInsensitive)) {
35 etag = header.section(QLatin1Char(' '), 1);
36 }
37 }
38
39 return etag;
40}
41
43 : DavJobBase(new DavItemFetchJobPrivate, parent)
44{
46 d->mItem = item;
47}
48
50{
52 KIO::StoredTransferJob *job = KIO::storedGet(d->mItem.url().url(), KIO::Reload, KIO::HideProgressInfo | KIO::DefaultFlags);
53 job->addMetaData(QStringLiteral("PropagateHttpHeader"), QStringLiteral("true"));
54 // Work around a strange bug in Zimbra (seen at least on CE 5.0.18) : if the user-agent
55 // contains "Mozilla", some strange debug data is displayed in the shared calendars.
56 // This kinda mess up the events parsing...
57 job->addMetaData(QStringLiteral("UserAgent"), QStringLiteral("KDE DAV groupware client"));
58 job->addMetaData(QStringLiteral("cookies"), QStringLiteral("none"));
59 job->addMetaData(QStringLiteral("no-auth-prompt"), QStringLiteral("true"));
60
61 connect(job, &KIO::StoredTransferJob::result, this, [d](KJob *job) {
62 d->davJobFinished(job);
63 });
64}
65
67{
68 Q_D(const DavItemFetchJob);
69 return d->mItem;
70}
71
72void DavItemFetchJobPrivate::davJobFinished(KJob *job)
73{
74 KIO::StoredTransferJob *storedJob = qobject_cast<KIO::StoredTransferJob *>(job);
75 const QString responseCodeStr = storedJob->queryMetaData(QStringLiteral("responsecode"));
76 const int responseCode = responseCodeStr.isEmpty() ? 0 : responseCodeStr.toInt();
77
78 setLatestResponseCode(responseCode);
79
80 if (storedJob->error()) {
81 setLatestResponseCode(responseCode);
82 setError(ERR_PROBLEM_WITH_REQUEST);
83 setJobErrorText(storedJob->errorText());
84 setJobError(storedJob->error());
85 setErrorTextFromDavError();
86 } else {
87 mItem.setData(storedJob->data());
88 mItem.setContentType(storedJob->queryMetaData(QStringLiteral("content-type")));
89 mItem.setEtag(etagFromHeaders(storedJob->queryMetaData(QStringLiteral("HTTP-Headers"))));
90 }
91
92 emitResult();
93}
94
95#include "moc_davitemfetchjob.cpp"
A job that fetches a DAV item from the DAV server.
DavItem item() const
Returns the fetched item including current ETag information.
DavItemFetchJob(const DavItem &item, QObject *parent=nullptr)
Creates a new DAV item fetch job.
void start() override
Starts the job.
A helper class to store information about DAV resources.
Definition davitem.h:39
void setEtag(const QString &etag)
Sets the etag of the item.
Definition davitem.cpp:72
void setData(const QByteArray &data)
Sets the raw content data of the item.
Definition davitem.cpp:62
void setContentType(const QString &type)
Sets the content type of the item.
Definition davitem.cpp:52
base class for the jobs used by the resource.
Definition davjobbase.h:27
A helper class to combine URL and protocol of a DAV URL.
Definition davurl.h:27
void addMetaData(const QMap< QString, QString > &values)
QString queryMetaData(const QString &key)
QByteArray data() const
int error() const
void result(KJob *job)
QString errorText() const
The KDAV namespace.
KIOCORE_EXPORT StoredTransferJob * storedGet(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
DefaultFlags
HideProgressInfo
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QString section(QChar sep, qsizetype start, qsizetype end, SectionFlags flags) const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
CaseInsensitive
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:13:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.