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 #include "davmanager_p.h"
12
13 #include <KIO/DavJob>
14 #include <KIO/Job>
15
16 using namespace KDAV;
17 namespace KDAV
18 {
19 class DavItemFetchJobPrivate : public DavJobBasePrivate
20 {
21 public:
22 void davJobFinished(KJob *job);
23
24 DavUrl mUrl;
25 DavItem mItem;
26 };
27 }
28
etagFromHeaders(const QString & headers)29 static QString etagFromHeaders(const QString &headers)
30 {
31 const QStringList allHeaders = headers.split(QLatin1Char('\n'));
32
33 QString etag;
34 for (const QString &header : allHeaders) {
35 if (header.startsWith(QLatin1String("etag:"), Qt::CaseInsensitive)) {
36 etag = header.section(QLatin1Char(' '), 1);
37 }
38 }
39
40 return etag;
41 }
42
DavItemFetchJob(const DavItem & item,QObject * parent)43 DavItemFetchJob::DavItemFetchJob(const DavItem &item, QObject *parent)
44 : DavJobBase(new DavItemFetchJobPrivate, parent)
45 {
46 Q_D(DavItemFetchJob);
47 d->mItem = item;
48 }
49
start()50 void DavItemFetchJob::start()
51 {
52 Q_D(DavItemFetchJob);
53 KIO::StoredTransferJob *job = KIO::storedGet(d->mItem.url().url(), KIO::Reload, KIO::HideProgressInfo | KIO::DefaultFlags);
54 job->addMetaData(QStringLiteral("PropagateHttpHeader"), QStringLiteral("true"));
55 // Work around a strange bug in Zimbra (seen at least on CE 5.0.18) : if the user-agent
56 // contains "Mozilla", some strange debug data is displayed in the shared calendars.
57 // This kinda mess up the events parsing...
58 job->addMetaData(QStringLiteral("UserAgent"), QStringLiteral("KDE DAV groupware client"));
59 job->addMetaData(QStringLiteral("cookies"), QStringLiteral("none"));
60 job->addMetaData(QStringLiteral("no-auth-prompt"), QStringLiteral("true"));
61
62 connect(job, &KIO::StoredTransferJob::result, this, [d](KJob *job) {
63 d->davJobFinished(job);
64 });
65 }
66
item() const67 DavItem DavItemFetchJob::item() const
68 {
69 Q_D(const DavItemFetchJob);
70 return d->mItem;
71 }
72
davJobFinished(KJob * job)73 void DavItemFetchJobPrivate::davJobFinished(KJob *job)
74 {
75 KIO::StoredTransferJob *storedJob = qobject_cast<KIO::StoredTransferJob *>(job);
76 const QString responseCodeStr = storedJob->queryMetaData(QStringLiteral("responsecode"));
77 const int responseCode = responseCodeStr.isEmpty() ? 0 : responseCodeStr.toInt();
78
79 setLatestResponseCode(responseCode);
80
81 if (storedJob->error()) {
82 setLatestResponseCode(responseCode);
83 setError(ERR_PROBLEM_WITH_REQUEST);
84 setJobErrorText(storedJob->errorText());
85 setJobError(storedJob->error());
86 setErrorTextFromDavError();
87 } else {
88 mItem.setData(storedJob->data());
89 mItem.setContentType(storedJob->queryMetaData(QStringLiteral("content-type")));
90 mItem.setEtag(etagFromHeaders(storedJob->queryMetaData(QStringLiteral("HTTP-Headers"))));
91 }
92
93 emitResult();
94 }
95