Kgapi

fileabstractresumablejob.h
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2020 David Barchiesi <david@barchie.si>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#pragma once
10
11#include "account.h"
12#include "file.h"
13#include "fileabstractdatajob.h"
14#include "kgapidrive_export.h"
15
16namespace KGAPI2
17{
18
19namespace Drive
20{
21
22/**
23 * @headerfile fileabstractresumablejob.h
24 *
25 * @brief Abstract superclass for KGAPI2::Drive::File create or modify jobs
26 * that use chunked uploading of the file's content.
27 *
28 * The Job starts an upload session and data is written in chunks. When the
29 * Job is out of chunks to write it will emit the @p readyWrite() signal and
30 * expect new data to be written via @p write().
31 *
32 * Writing 0 bytes will indicate that the File has been completely transferred
33 * and the Job will close the upload session.
34 *
35 * @see <a href="https://developers.google.com/drive/api/v2/manage-uploads#resumable">Perform a resumable upload</a>
36 * @see readyWrite, write
37 *
38 * @author David Barchiesi <david@barchie.si>
39 * @since 20.12
40 */
41class KGAPIDRIVE_EXPORT FileAbstractResumableJob : public KGAPI2::Drive::FileAbstractDataJob
42{
43 Q_OBJECT
44
45public:
46 /**
47 * @brief Constructs a job that will upload an Untitled file in the
48 * users root folder.
49 *
50 * @param account Account to authenticate the request
51 * @param parent
52 */
53 explicit FileAbstractResumableJob(const AccountPtr &account, QObject *parent = nullptr);
54
55 /**
56 * @brief Constructs a job that will upload a file with its metadata.
57 *
58 * @param metadata File metadata to upload
59 * @param account Account to authenticate the request
60 * @param parent
61 */
62 explicit FileAbstractResumableJob(const FilePtr &metadata, const AccountPtr &account, QObject *parent = nullptr);
63
64 /**
65 * @brief Constructs a job that will upload an Untitled file in the
66 * users root folder with data contained in device.
67 *
68 * @param device Open device to read from
69 * @param account Account to authenticate the request
70 * @param parent
71 */
72 explicit FileAbstractResumableJob(QIODevice *device, const AccountPtr &account, QObject *parent = nullptr);
73
74 /**
75 * @brief Constructs a job that will upload a file with its metadata
76 * with data contained in device.
77 *
78 * @param device Open device to read from
79 * @param metadata File metadata to upload
80 * @param account Account to authenticate the request
81 * @param parent
82 */
83 explicit FileAbstractResumableJob(QIODevice *device, const FilePtr &metadata, const AccountPtr &account, QObject *parent = nullptr);
84
85 /**
86 * @brief Destructor
87 */
89
90 /**
91 * @brief Returns metadata supplied at Job creation or retrieved on
92 * Job completion.
93 */
94 FilePtr metadata() const;
95
96 /**
97 * @brief Sets the total upload size and is required for progress reporting
98 * via the Job::progress() signal.
99 */
100 void setUploadSize(int size);
101
102 /**
103 * @brief This function writes all the bytes in \p data to the upload session.
104 *
105 * The write operation splits written data in chunks that will be
106 * subsequently uploaded in the session.
107 *
108 * Writing an empty \p data will signal the Job that it can complete as no
109 * more data will be written.
110 *
111 * When more data is required the readyWrite() signal is emitted.
112 *
113 * @see readyWrite
114 *
115 * @param data the data to write
116 */
117 void write(const QByteArray &data);
118
119protected:
120 /**
121 * @brief KGAPI2::Job::start implementation
122 */
123 void start() override;
124
125 /**
126 * @brief KGAPI2::Job::handleReply implementation
127 *
128 * @param reply
129 * @param rawData
130 */
131 void handleReply(const QNetworkReply *reply, const QByteArray &rawData) override;
132
133 /**
134 * @brief KGAPI2::Job::dispatchRequest implementation
135 *
136 * @param accessManager
137 * @param request
138 * @param data
139 * @param contentType
140 */
141 void dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType) override;
142
143 /**
144 * @brief Emit readyWrite() signal
145 */
146 void emitReadyWrite();
147
148 /**
149 * @brief Generates url that will be used during upload session start.
150 */
151 virtual QUrl createUrl() = 0;
152
153Q_SIGNALS:
154
155 /**
156 * @brief Emitted when @p job requires more data to proceed. Bytes should
157 * be written via write(). When connecting to this signal always use a
158 * synchronous connection type like Qt::DirectConnection.
159 *
160 * Subclasses should never ever emit this signal directly.
161 *
162 * @param job The job that requires data
163 * @sa readyWrite()
164 *
165 * @note Connecting via Qt::QueuedConnection or any other asynchronous connection
166 * type will potentially crash the job or end it prematurely. Use Qt::DirectConnection
167 *
168 * @see write
169 */
171
172private:
173 class Private;
175 friend class Private;
176
177 Q_PRIVATE_SLOT(d, void _k_uploadProgress(qint64 uploadedBytes, qint64 totalBytes))
178};
179
180} // namespace Drive
181
182} // namespace KGAPI2
Abstract superclass for KGAPI2::Drive::File create or modify jobs that use chunked uploading of the f...
void readyWrite(KGAPI2::Drive::FileAbstractResumableJob *job)
Emitted when job requires more data to proceed.
~FileAbstractResumableJob() override
Destructor.
virtual QUrl createUrl()=0
Generates url that will be used during upload session start.
Q_SCRIPTABLE Q_NOREPLY void start()
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:55:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.