Libkdav2

davitemcreatejob.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 "davitemcreatejob.h"
20 
21 #include "davitemfetchjob.h"
22 #include "davmanager.h"
23 #include "daverror.h"
24 #include "davjob.h"
25 
26 #include "libkdav2_debug.h"
27 
28 using namespace KDAV2;
29 
31  : DavJobBase(parent), mItem(item)
32 {
33 }
34 
36 {
37  auto job = DavManager::self()->createCreateJob(mItem.data(), itemUrl(), mItem.contentType().toLatin1());
38  connect(job, &DavJob::result, this, &DavItemCreateJob::davJobFinished);
39 }
40 
42 {
43  return mItem;
44 }
45 
46 QUrl DavItemCreateJob::itemUrl() const
47 {
48  return mItem.url().url();
49 }
50 
51 void DavItemCreateJob::davJobFinished(KJob *job)
52 {
53  auto storedJob = static_cast<DavJob*>(job);
54 
55  if (storedJob->error()) {
56  setErrorFromJob(storedJob, ERR_ITEMCREATE);
57  emitResult();
58  return;
59  }
60 
61  mItem.setUrl(DavUrl(storedJob->url(), mItem.url().protocol()));
62 
63  DavItemFetchJob *fetchJob = new DavItemFetchJob(mItem);
64  connect(fetchJob, &DavItemFetchJob::result, this, &DavItemCreateJob::itemRefreshed);
65  fetchJob->start();
66 }
67 
68 void DavItemCreateJob::itemRefreshed(KJob *job)
69 {
70  if (!job->error()) {
71  DavItemFetchJob *fetchJob = qobject_cast<DavItemFetchJob *>(job);
72  mItem.setEtag(fetchJob->item().etag());
73  }
74  emitResult();
75 }
76 
DavItem item() const
Returns the created DAV item including the correct identifier url and current etag information.
void result(KJob *job)
static DavManager * self()
Returns the global instance of the DAV manager.
Definition: davmanager.cpp:52
void start() override
Starts the job.
A helper class to combine url and protocol of a DAV url.
Definition: davurl.h:35
Protocol protocol() const
Returns the DAV protocol dialect that is used to retrieve the DAV object.
Definition: davurl.cpp:51
QByteArray toLatin1() const const
base class for the jobs used by the resource.
Definition: davjobbase.h:37
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
DavItem item() const
Returns the fetched item including current etag information.
void setEtag(const QString &etag)
Sets the etag of the item.
Definition: davitem.cpp:109
DavItemCreateJob(const DavItem &item, QObject *parent=nullptr)
Creates a new dav item create job.
void setUrl(const DavUrl &url)
Sets the url that identifies the item.
Definition: davitem.cpp:79
void start() override
Starts the job.
DavUrl url() const
Returns the url that identifies the item.
Definition: davitem.cpp:84
DavJob * createCreateJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType)
Returns a preconfigured DAV PUT job with a If-None-Match header.
Definition: davmanager.cpp:105
A helper class to store information about DAV resources.
Definition: davitem.h:51
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
A job that fetches a DAV item from the DAV server.
QByteArray data() const
Returns the raw content data of the item.
Definition: davitem.cpp:104
QString etag() const
Returns the etag of the item.
Definition: davitem.cpp:114
void emitResult()
int error() const
QUrl url() const
Returns the url that identifies the DAV object.
Definition: davurl.cpp:41
QString contentType() const
Returns the content type of the item.
Definition: davitem.cpp:94
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Aug 10 2022 04:09:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.