Kgapi

fileabstractmodifyjob.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
9#include "fileabstractmodifyjob.h"
10#include "file.h"
11#include "utils.h"
12
13#include <QNetworkReply>
14#include <QNetworkRequest>
15#include <QUrlQuery>
16
17using namespace KGAPI2;
18using namespace KGAPI2::Drive;
19
20class Q_DECL_HIDDEN FileAbstractModifyJob::Private
21{
22public:
23 Private(FileAbstractModifyJob *parent);
24 void processNext();
25
26 QStringList filesIds;
27
28 bool supportsAllDrives = true;
29
30private:
31 FileAbstractModifyJob *const q;
32};
33
34FileAbstractModifyJob::Private::Private(FileAbstractModifyJob *parent)
35 : q(parent)
36{
37}
38
39void FileAbstractModifyJob::Private::processNext()
40{
41 if (filesIds.isEmpty()) {
42 q->emitFinished();
43 return;
44 }
45
46 const QString fileId = filesIds.takeFirst();
47 QUrl url = q->url(fileId);
48
49 QUrlQuery query(url);
50 query.addQueryItem(QStringLiteral("supportsAllDrives"), Utils::bool2Str(supportsAllDrives));
51 url.setQuery(query);
52
53 QNetworkRequest request(url);
54 request.setHeader(QNetworkRequest::ContentLengthHeader, 0);
55
56 q->enqueueRequest(request);
57}
58
59FileAbstractModifyJob::FileAbstractModifyJob(const QString &fileId, const AccountPtr &account, QObject *parent)
61 , d(new Private(this))
62{
63 d->filesIds << fileId;
64}
65
66FileAbstractModifyJob::FileAbstractModifyJob(const QStringList &filesIds, const AccountPtr &account, QObject *parent)
67 : ModifyJob(account, parent)
68 , d(new Private(this))
69{
70 d->filesIds << filesIds;
71}
72
73FileAbstractModifyJob::FileAbstractModifyJob(const FilePtr &file, const AccountPtr &account, QObject *parent)
74 : ModifyJob(account, parent)
75 , d(new Private(this))
76{
77 d->filesIds << file->id();
78}
79
80FileAbstractModifyJob::FileAbstractModifyJob(const FilesList &files, const AccountPtr &account, QObject *parent)
81 : ModifyJob(account, parent)
82 , d(new Private(this))
83{
84 for (const FilePtr &file : std::as_const(files)) {
85 d->filesIds << file->id();
86 }
87}
88
89FileAbstractModifyJob::~FileAbstractModifyJob()
90{
91 delete d;
92}
93
94void FileAbstractModifyJob::start()
95{
96 d->processNext();
97}
98
99bool FileAbstractModifyJob::supportsAllDrives() const
100{
101 return d->supportsAllDrives;
102}
103
104void FileAbstractModifyJob::setSupportsAllDrives(bool supportsAllDrives)
105{
106 d->supportsAllDrives = supportsAllDrives;
107}
108
109ObjectsList FileAbstractModifyJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
110{
111 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
112 ContentType ct = Utils::stringToContentType(contentType);
114 if (ct == KGAPI2::JSON) {
115 items << File::fromJSON(rawData);
116 } else {
118 setErrorString(tr("Invalid response content type"));
119 emitFinished();
120 }
121
122 d->processNext();
123
124 return items;
125}
126
127#include "moc_fileabstractmodifyjob.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
Abstract superclass for all jobs that somehow modify resources on Google.
Definition modifyjob.h:25
ObjectsList items() const
Definition modifyjob.cpp:43
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
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
bool isEmpty() const const
value_type takeFirst()
QVariant header(QNetworkRequest::KnownHeaders header) const const
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
void setQuery(const QString &query, ParsingMode mode)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:59:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.