Libkdav2

davitemmodifyjob.cpp
1 /*
2  Copyright (c) 2010 Tobias Koenig <[email protected]>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 #include "davitemmodifyjob.h"
20 
21 #include "davitemfetchjob.h"
22 #include "davmanager.h"
23 #include "daverror.h"
24 #include "davjob.h"
25 
26 using namespace KDAV2;
27 
29  : DavJobBase(parent), mItem(item), mFreshResponseCode(0)
30 {
31 }
32 
34 {
35  auto job = DavManager::self()->createModifyJob(mItem.data(), itemUrl(), mItem.contentType().toUtf8(), mItem.etag().toUtf8());
36  connect(job, &DavJob::result, this, &DavItemModifyJob::davJobFinished);
37 }
38 
40 {
41  return mItem;
42 }
43 
45 {
46  return mFreshItem;
47 }
48 
50 {
51  return mFreshResponseCode;
52 }
53 
54 QUrl DavItemModifyJob::itemUrl() const
55 {
56  return mItem.url().url();
57 }
58 
59 void DavItemModifyJob::davJobFinished(KJob *job)
60 {
61  auto storedJob = static_cast<DavJob*>(job);
62 
63  if (storedJob->error()) {
64  setErrorFromJob(storedJob, ERR_ITEMMODIFY);
65 
66  if (hasConflict()) {
67  DavItemFetchJob *fetchJob = new DavItemFetchJob(mItem);
68  connect(fetchJob, &DavItemFetchJob::result, this, &DavItemModifyJob::conflictingItemFetched);
69  fetchJob->start();
70  } else {
71  emitResult();
72  }
73 
74  return;
75  }
76 
77  const auto location = storedJob->getLocationHeader();
78  QUrl url;
79  if (location.isEmpty()) {
80  url = storedJob->url();
81  } else if (location.startsWith(QLatin1Char('/'))) {
82  url = storedJob->url();
83  url.setPath(location, QUrl::TolerantMode);
84  } else {
85  url = QUrl::fromUserInput(location);
86  }
87 
88  url.setUserInfo(itemUrl().userInfo());
89  mItem.setUrl(DavUrl(url, mItem.url().protocol()));
90 
91  DavItemFetchJob *fetchJob = new DavItemFetchJob(mItem);
92  connect(fetchJob, &DavItemFetchJob::result, this, &DavItemModifyJob::itemRefreshed);
93  fetchJob->start();
94 }
95 
96 void DavItemModifyJob::itemRefreshed(KJob *job)
97 {
98  if (!job->error()) {
99  DavItemFetchJob *fetchJob = qobject_cast<DavItemFetchJob *>(job);
100  mItem.setEtag(fetchJob->item().etag());
101  } else {
102  mItem.setEtag(QString());
103  }
104  emitResult();
105 }
106 
107 void DavItemModifyJob::conflictingItemFetched(KJob *job)
108 {
109  DavItemFetchJob *fetchJob = qobject_cast<DavItemFetchJob *>(job);
110  mFreshResponseCode = fetchJob->latestHttpStatusCode();
111 
112  if (!job->error()) {
113  mFreshItem = fetchJob->item();
114  }
115 
116  emitResult();
117 }
118 
QString url(QUrl::FormattingOptions options) const const
QUrl url() const
Returns the url that identifies the DAV object.
Definition: davurl.cpp:41
unsigned int latestHttpStatusCode() const
Get the latest http status code.
Definition: davjobbase.cpp:39
void emitResult()
QString etag() const
Returns the etag of the item.
Definition: davitem.cpp:114
static DavManager * self()
Returns the global instance of the DAV manager.
Definition: davmanager.cpp:52
base class for the jobs used by the resource.
Definition: davjobbase.h:37
void setEtag(const QString &etag)
Sets the etag of the item.
Definition: davitem.cpp:109
int freshResponseCode() const
Returns the http response code we got when fetching the fresh item.
bool hasConflict() const
Check if the job failed because of a conflict.
Definition: davjobbase.cpp:77
DavItemModifyJob(const DavItem &item, QObject *parent=nullptr)
Creates a new dav item modify job.
TolerantMode
void start() override
Starts the job.
QUrl fromUserInput(const QString &userInput)
A helper class to combine url and protocol of a DAV url.
Definition: davurl.h:35
DavItem freshItem() const
Returns the item that triggered the conflict, if any.
QByteArray data() const
Returns the raw content data of the item.
Definition: davitem.cpp:104
void start() override
Starts the job.
void setPath(const QString &path, QUrl::ParsingMode mode)
DavUrl url() const
Returns the url that identifies the item.
Definition: davitem.cpp:84
void setUrl(const DavUrl &url)
Sets the url that identifies the item.
Definition: davitem.cpp:79
DavJob * createModifyJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType, const QByteArray &etag)
Returns a preconfigured DAV PUT job with a If-Match header, that matches the.
Definition: davmanager.cpp:112
A helper class to store information about DAV resources.
Definition: davitem.h:51
DavItem item() const
Returns the modified item including the updated etag information.
DavItem item() const
Returns the fetched item including current etag information.
QString contentType() const
Returns the content type of the item.
Definition: davitem.cpp:94
Protocol protocol() const
Returns the DAV protocol dialect that is used to retrieve the DAV object.
Definition: davurl.cpp:51
void result(KJob *job)
void setErrorFromJob(DavJob *, ErrorNumber jobErrorCode=ERR_PROBLEM_WITH_REQUEST)
Set the error of this job from a failed DavJob (executed by this job).
Definition: davjobbase.cpp:99
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
void setUserInfo(const QString &userInfo, QUrl::ParsingMode mode)
A job that fetches a DAV item from the DAV server.
int error() const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 23:10:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.