Kgapi

filecopyjob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8#include "filecopyjob.h"
9#include "driveservice.h"
10#include "file.h"
11#include "utils.h"
12
13#include <QNetworkReply>
14#include <QNetworkRequest>
15
16using namespace KGAPI2;
17using namespace KGAPI2::Drive;
18
19class Q_DECL_HIDDEN FileCopyJob::Private
20{
21public:
22 Private(FileCopyJob *parent);
23 void processNext();
24
26
27 QList<FilePtr> copies;
28
29private:
30 FileCopyJob *const q;
31};
32
33FileCopyJob::Private::Private(FileCopyJob *parent)
34 : q(parent)
35{
36}
37
38void FileCopyJob::Private::processNext()
39{
40 if (files.isEmpty()) {
41 q->emitFinished();
42 return;
43 }
44
45 const QString fileId = files.cbegin().key();
46 const FilePtr file = files.take(fileId);
47
48 QUrl url = DriveService::copyFileUrl(fileId);
49 q->updateUrl(url);
50
51 QNetworkRequest request(url);
52
53 const QByteArray rawData = File::toJSON(file);
54
55 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
56}
57
58FileCopyJob::FileCopyJob(const QString &sourceFileId, const FilePtr &destinationFile, const AccountPtr &account, QObject *parent)
59 : FileAbstractDataJob(account, parent)
60 , d(new Private(this))
61{
62 d->files.insert(sourceFileId, destinationFile);
63}
64
65FileCopyJob::FileCopyJob(const FilePtr &sourceFile, const FilePtr &destinationFile, const AccountPtr &account, QObject *parent)
66 : FileAbstractDataJob(account, parent)
67 , d(new Private(this))
68{
69 d->files.insert(sourceFile->id(), destinationFile);
70}
71
72FileCopyJob::FileCopyJob(const QMap<QString, FilePtr> &files, const AccountPtr &account, QObject *parent)
73 : FileAbstractDataJob(account, parent)
74 , d(new Private(this))
75{
76 d->files = files;
77}
78
79FileCopyJob::FileCopyJob(const QMap<FilePtr, FilePtr> &files, const AccountPtr &account, QObject *parent)
80 : FileAbstractDataJob(account, parent)
81 , d(new Private(this))
82{
85 for (; iter != iterEnd; ++iter) {
86 d->files.insert(iter.key()->id(), iter.value());
87 }
88}
89
90FileCopyJob::~FileCopyJob()
91{
92 delete d;
93}
94
95FilesList FileCopyJob::files() const
96{
97 return d->copies;
98}
99
100void FileCopyJob::start()
101{
102 d->processNext();
103}
104
105void FileCopyJob::dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType)
106{
107 QNetworkRequest r = request;
109
110 accessManager->post(r, data);
111}
112
113void FileCopyJob::handleReply(const QNetworkReply *reply, const QByteArray &rawData)
114{
115 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
116 ContentType ct = Utils::stringToContentType(contentType);
117 if (ct == KGAPI2::JSON) {
118 d->copies << File::fromJSON(rawData);
119 } else {
121 setErrorString(tr("Invalid response content type"));
122 emitFinished();
123 return;
124 }
125
126 // Enqueue next item or finish
127 d->processNext();
128}
129
130#include "moc_filecopyjob.cpp"
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition job.cpp:401
AccountPtr account() const
Returns account used to authenticate requests.
Definition job.cpp:436
virtual void emitFinished()
Emits Job::finished() signal.
Definition job.cpp:493
void setError(KGAPI2::Error error)
Set job error to error.
Definition job.cpp:386
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition job.cpp:513
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
ContentType
Definition types.h:210
ConstIterator
const_iterator constBegin() const const
const_iterator constEnd() const const
Key key(const T &value, const Key &defaultKey) const const
T value(const Key &key, const T &defaultValue) const const
QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)
QVariant header(QNetworkRequest::KnownHeaders header) const const
void setHeader(KnownHeaders header, const QVariant &value)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.