1/*
2 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "davitemdeletejob.h"
8#include "davjobbase_p.h"
9
10#include "daverror.h"
11#include "davitemfetchjob.h"
12#include "davmanager_p.h"
13
14#include <KIO/DeleteJob>
15#include <KIO/Job>
16
17using namespace KDAV;
18
19namespace KDAV
20{
21class DavItemDeleteJobPrivate : public DavJobBasePrivate
22{
23public:
24 void davJobFinished(KJob *job);
25 void conflictingItemFetched(KJob *job);
26
27 DavItem mItem;
28 DavItem mFreshItem;
29 int mFreshResponseCode = -1;
30};
31}
32
33DavItemDeleteJob::DavItemDeleteJob(const DavItem &item, QObject *parent)
34 : DavJobBase(new DavItemDeleteJobPrivate, parent)
35{
36 Q_D(DavItemDeleteJob);
37 d->mItem = item;
38}
39
40void DavItemDeleteJob::start()
41{
42 Q_D(DavItemDeleteJob);
43 KIO::DeleteJob *job = KIO::del(src: d->mItem.url().url(), flags: KIO::HideProgressInfo | KIO::DefaultFlags);
44 job->addMetaData(QStringLiteral("PropagateHttpHeader"), QStringLiteral("true"));
45 job->addMetaData(QStringLiteral("customHTTPHeader"), QStringLiteral("If-Match: ") + d->mItem.etag());
46 job->addMetaData(QStringLiteral("cookies"), QStringLiteral("none"));
47 job->addMetaData(QStringLiteral("no-auth-prompt"), QStringLiteral("true"));
48
49 connect(sender: job, signal: &KIO::DeleteJob::result, context: this, slot: [d](KJob *job) {
50 d->davJobFinished(job);
51 });
52}
53
54DavItem DavItemDeleteJob::freshItem() const
55{
56 Q_D(const DavItemDeleteJob);
57 return d->mFreshItem;
58}
59
60int DavItemDeleteJob::freshResponseCode() const
61{
62 Q_D(const DavItemDeleteJob);
63 return d->mFreshResponseCode;
64}
65
66void DavItemDeleteJobPrivate::davJobFinished(KJob *job)
67{
68 KIO::DeleteJob *deleteJob = qobject_cast<KIO::DeleteJob *>(object: job);
69
70 if (deleteJob->error() && deleteJob->error() != KIO::ERR_NO_CONTENT) {
71 const int responseCode = deleteJob->queryMetaData(QStringLiteral("responsecode")).isEmpty() //
72 ? 0
73 : deleteJob->queryMetaData(QStringLiteral("responsecode")).toInt();
74
75 if (responseCode != 404 && responseCode != 410) {
76 setLatestResponseCode(responseCode);
77 setError(ERR_ITEMDELETE);
78 setJobErrorText(deleteJob->errorText());
79 setJobError(deleteJob->error());
80 setErrorTextFromDavError();
81 }
82
83 if (q_ptr->hasConflict()) {
84 DavItemFetchJob *fetchJob = new DavItemFetchJob(mItem);
85 QObject::connect(sender: fetchJob, signal: &DavItemFetchJob::result, context: q_ptr, slot: [this](KJob *job) {
86 conflictingItemFetched(job);
87 });
88 fetchJob->start();
89 return;
90 }
91 }
92
93 emitResult();
94}
95
96void DavItemDeleteJobPrivate::conflictingItemFetched(KJob *job)
97{
98 DavItemFetchJob *fetchJob = qobject_cast<DavItemFetchJob *>(object: job);
99 mFreshResponseCode = fetchJob->latestResponseCode();
100
101 if (!job->error()) {
102 mFreshItem = fetchJob->item();
103 }
104
105 emitResult();
106}
107
108#include "moc_davitemdeletejob.cpp"
109

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