KDAV

davitemcreatejob.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 "davitemcreatejob.h"
8#include "davjobbase_p.h"
9
10#include "daverror.h"
11#include "davitemfetchjob.h"
12
13#include <KIO/DavJob>
14#include <KIO/StoredTransferJob>
15
16using namespace KDAV;
17
18namespace KDAV
19{
20class DavItemCreateJobPrivate : public DavJobBasePrivate
21{
22public:
23 void davJobFinished(KJob *job);
24 void itemRefreshed(KJob *job);
25
26 DavItem mItem;
27 int mRedirectCount = 0;
28
29 Q_DECLARE_PUBLIC(DavItemCreateJob)
30};
31}
32
34 : DavJobBase(new DavItemCreateJobPrivate, parent)
35{
37 d->mItem = item;
38}
39
41{
43 QString headers = QStringLiteral("Content-Type: ");
44 headers += d->mItem.contentType();
45 headers += QLatin1String("\r\n");
46 headers += QLatin1String("If-None-Match: *");
47
48 KIO::StoredTransferJob *job = KIO::storedPut(d->mItem.data(), itemUrl(), -1, KIO::HideProgressInfo | KIO::DefaultFlags);
49 job->addMetaData(QStringLiteral("PropagateHttpHeader"), QStringLiteral("true"));
50 job->addMetaData(QStringLiteral("customHTTPHeader"), headers);
51 job->addMetaData(QStringLiteral("cookies"), QStringLiteral("none"));
52 job->addMetaData(QStringLiteral("no-auth-prompt"), QStringLiteral("true"));
54
55 connect(job, &KIO::StoredTransferJob::result, this, [d](KJob *job) {
56 d->davJobFinished(job);
57 });
58}
59
61{
62 Q_D(const DavItemCreateJob);
63 return d->mItem;
64}
65
66QUrl DavItemCreateJob::itemUrl() const
67{
68 Q_D(const DavItemCreateJob);
69 return d->mItem.url().url();
70}
71
72void DavItemCreateJobPrivate::davJobFinished(KJob *job)
73{
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 if (storedJob->error()) {
79 setLatestResponseCode(responseCode);
80 setError(ERR_ITEMCREATE);
81 setJobErrorText(storedJob->errorText());
82 setJobError(storedJob->error());
83 setErrorTextFromDavError();
84
85 emitResult();
86 return;
87 }
88
89 // The 'Location:' HTTP header is used to indicate the new URL
90 const QStringList allHeaders = storedJob->queryMetaData(QStringLiteral("HTTP-Headers")).split(QLatin1Char('\n'));
92 for (const QString &header : allHeaders) {
93 if (header.startsWith(QLatin1String("location:"), Qt::CaseInsensitive)) {
94 location = header.section(QLatin1Char(' '), 1);
95 }
96 }
97
98 QUrl url;
99 if (location.isEmpty()) {
100 url = storedJob->url();
101 } else if (location.startsWith(QLatin1Char('/'))) {
102 url = storedJob->url();
103 url.setPath(location, QUrl::TolerantMode);
104 } else {
105 url = QUrl::fromUserInput(location);
106 }
107
108 if (responseCode == 301 || responseCode == 302 || responseCode == 307 || responseCode == 308) {
109 if (mRedirectCount > 4) {
110 setLatestResponseCode(responseCode);
111 setError(DavItemCreateJob::UserDefinedError + responseCode);
112 emitResult();
113 } else {
114 QUrl _itemUrl(url);
115 _itemUrl.setUserInfo(q->itemUrl().userInfo());
116 mItem.setUrl(DavUrl(_itemUrl, mItem.url().protocol()));
117
118 ++mRedirectCount;
119 q->start();
120 }
121
122 return;
123 }
124
125 url.setUserInfo(q->itemUrl().userInfo());
126 mItem.setUrl(DavUrl(url, mItem.url().protocol()));
127
128 DavItemFetchJob *fetchJob = new DavItemFetchJob(mItem);
129 QObject::connect(fetchJob, &DavItemFetchJob::result, q, [this](KJob *job) {
130 itemRefreshed(job);
131 });
132 fetchJob->start();
133}
134
135void DavItemCreateJobPrivate::itemRefreshed(KJob *job)
136{
137 if (!job->error()) {
138 DavItemFetchJob *fetchJob = qobject_cast<DavItemFetchJob *>(job);
139 mItem.setEtag(fetchJob->item().etag());
140 }
141 emitResult();
142}
143
144#include "moc_davitemcreatejob.cpp"
A job to create a DAV item on the DAV server.
DavItem item() const
Returns the created DAV item including the correct identifier URL and current ETag information.
DavItemCreateJob(const DavItem &item, QObject *parent=nullptr)
Creates a new DAV item create job.
void start() override
Starts the job.
A job that fetches a DAV item from the DAV server.
DavItem item() const
Returns the fetched item including current ETag information.
void start() override
Starts the job.
A helper class to store information about DAV resources.
Definition davitem.h:39
DavUrl url() const
Returns the URL that identifies the item.
Definition davitem.cpp:47
void setEtag(const QString &etag)
Sets the etag of the item.
Definition davitem.cpp:72
void setUrl(const DavUrl &url)
Sets the url that identifies the item.
Definition davitem.cpp:42
QString etag() const
Returns the ETag of the item.
Definition davitem.cpp:77
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
Protocol protocol() const
Returns the DAV protocol dialect that is used to retrieve the DAV object.
Definition davurl.cpp:55
void addMetaData(const QMap< QString, QString > &values)
QString queryMetaData(const QString &key)
void setRedirectionHandlingEnabled(bool handle)
const QUrl & url() const
int error() const
void result(KJob *job)
QString errorText() const
The KDAV namespace.
KIOCORE_EXPORT StoredTransferJob * storedPut(const QByteArray &arr, const QUrl &url, int permissions, JobFlags flags=DefaultFlags)
DefaultFlags
HideProgressInfo
QVariant location(const QVariant &res)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
CaseInsensitive
TolerantMode
QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, UserInputResolutionOptions options)
void setPath(const QString &path, ParsingMode mode)
void setUserInfo(const QString &userInfo, ParsingMode mode)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:05:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.