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(sep: QLatin1Char('\n'));
31
32 QString etag;
33 for (const QString &header : allHeaders) {
34 if (header.startsWith(s: QLatin1String("etag:"), cs: Qt::CaseInsensitive)) {
35 etag = header.section(asep: QLatin1Char(' '), astart: 1);
36 }
37 }
38
39 return etag;
40}
41
42DavItemFetchJob::DavItemFetchJob(const DavItem &item, QObject *parent)
43 : DavJobBase(new DavItemFetchJobPrivate, parent)
44{
45 Q_D(DavItemFetchJob);
46 d->mItem = item;
47}
48
49void DavItemFetchJob::start()
50{
51 Q_D(DavItemFetchJob);
52 KIO::StoredTransferJob *job = KIO::storedGet(url: d->mItem.url().url(), reload: KIO::Reload, flags: 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(sender: job, signal: &KIO::StoredTransferJob::result, context: this, slot: [d](KJob *job) {
62 d->davJobFinished(job);
63 });
64}
65
66DavItem DavItemFetchJob::item() const
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 *>(object: 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(headers: storedJob->queryMetaData(QStringLiteral("HTTP-Headers"))));
90 }
91
92 emitResult();
93}
94
95#include "moc_davitemfetchjob.cpp"
96

source code of kdav/src/common/davitemfetchjob.cpp