Commit 363bb70b authored by David Barchiesi's avatar David Barchiesi

Add QIODevice API to FileAbstractResumableJob for reading all chunks of a...

Add QIODevice API to FileAbstractResumableJob for reading all chunks of a device instead of asking for data via signal.
parent d0377406
Pipeline #33560 passed with stage
in 22 minutes and 38 seconds
......@@ -92,24 +92,34 @@ void MainWindow::uploadFile()
QFileInfo fileInfo(uploadingFile->fileName());
uploadFile->setTitle(fileInfo.fileName());
KGAPI2::Drive::FileResumableCreateJob *fileCreateJob = new KGAPI2::Drive::FileResumableCreateJob(uploadFile, m_account, this);
KGAPI2::Drive::FileResumableCreateJob *fileCreateJob;
if (m_ui->writeOnSignalRadioButton->isChecked()) {
fileCreateJob = new KGAPI2::Drive::FileResumableCreateJob(uploadFile, m_account, this);
connect(fileCreateJob, &KGAPI2::Drive::FileResumableCreateJob::readyWrite,
this, &MainWindow::slotFileCreateJobReadyWrite);
} else {
fileCreateJob = new KGAPI2::Drive::FileResumableCreateJob(uploadingFile, uploadFile, m_account, this);
}
fileCreateJob->setUploadSize(uploadingFile->size());
connect(fileCreateJob, &KGAPI2::Drive::FileResumableCreateJob::finished,
this, &MainWindow::slotFileCreateJobFinished);
connect(fileCreateJob, &KGAPI2::Drive::FileResumableCreateJob::readyWrite,
this, &MainWindow::slotFileCreateJobReadyWrite);
connect(fileCreateJob, &KGAPI2::Drive::FileResumableCreateJob::progress,
this, &MainWindow::slotFileCreateJobProgress);
m_ui->statusbar->clearMessage();
bytesUploaded = 0;
fileUploadProgressBar = new QProgressBar(m_ui->statusbar);
fileUploadProgressBar->setFormat(QStringLiteral("Sent to Job: %v bytes [%p%]"));
fileUploadProgressBar->setMaximum(uploadingFile->size());
m_ui->statusbar->addWidget(fileUploadProgressBar);
if (m_ui->writeOnSignalRadioButton->isChecked()) {
fileUploadProgressBar = new QProgressBar(m_ui->statusbar);
fileUploadProgressBar->setFormat(QStringLiteral("Sent to Job: %v bytes [%p%]"));
fileUploadProgressBar->setMaximum(uploadingFile->size());
m_ui->statusbar->addWidget(fileUploadProgressBar);
}
jobUploadProgressBar = new QProgressBar(m_ui->statusbar);
jobUploadProgressBar->setFormat(QStringLiteral("Job progress: %v bytes [%p%]"));
m_ui->statusbar->addWidget(jobUploadProgressBar);
setInputsEnabled(false);
}
void MainWindow::slotFileCreateJobFinished(KGAPI2::Job *job)
......@@ -127,11 +137,15 @@ void MainWindow::slotFileCreateJobFinished(KGAPI2::Job *job)
m_ui->statusbar->showMessage(QStringLiteral("Upload complete, id %1, size %2 (uploaded %3), mimeType %4").arg(file->id()).arg(file->fileSize()).arg(bytesUploaded).arg(file->mimeType()));
}
m_ui->statusbar->removeWidget(fileUploadProgressBar);
fileUploadProgressBar->deleteLater();
if (m_ui->writeOnSignalRadioButton->isChecked()) {
m_ui->statusbar->removeWidget(fileUploadProgressBar);
fileUploadProgressBar->deleteLater();
}
m_ui->statusbar->removeWidget(jobUploadProgressBar);
jobUploadProgressBar->deleteLater();
setInputsEnabled(true);
}
......@@ -146,6 +160,8 @@ void MainWindow::slotFileCreateJobReadyWrite(KGAPI2::Drive::FileAbstractResumabl
void MainWindow::slotFileCreateJobProgress(KGAPI2::Job *job, int base, int total)
{
Q_UNUSED(job)
jobUploadProgressBar->setValue(base);
jobUploadProgressBar->setMaximum(total);
}
......@@ -154,5 +170,7 @@ void MainWindow::setInputsEnabled(bool enabled)
{
m_ui->sourceLineEdit->setEnabled(enabled);
m_ui->browseButton->setEnabled(enabled);
m_ui->writeOnSignalRadioButton->setEnabled(enabled);
m_ui->passFileToJobRadioButton->setEnabled(enabled);
m_ui->uploadButton->setEnabled(enabled);
}
......@@ -86,6 +86,23 @@
</property>
</spacer>
</item>
<item>
<widget class="QRadioButton" name="writeOnSignalRadioButton">
<property name="text">
<string>Write on Job signal</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="passFileToJobRadioButton">
<property name="text">
<string>Pass file to Job</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="uploadButton">
<property name="text">
......
......@@ -14,7 +14,6 @@
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QMimeDatabase>
#include <QUrlQuery>
using namespace KGAPI2;
......@@ -31,11 +30,13 @@ class Q_DECL_HIDDEN FileAbstractResumableJob::Private
void startUploadSession();
void uploadChunk(bool lastChunk);
void processNext();
void readFromDevice();
bool isTotalSizeKnown() const;
void _k_uploadProgress(qint64 bytesSent, qint64 totalBytes);
FilePtr metaData;
QIODevice *device = nullptr;
QString sessionPath;
QList<QByteArray> chunks;
......@@ -142,9 +143,13 @@ void FileAbstractResumableJob::Private::processNext()
if (chunks.isEmpty() || chunks.first().size() < ChunkSize) {
qCDebug(KGAPIDebug) << "Chunks empty or not big enough to process, asking for more";
// Warning: an endless loop could be started here is the signal receiver isn't using
// a direct connection.
q->emitReadyWrite();
if (device) {
readFromDevice();
} else {
// Warning: an endless loop could be started here if the signal receiver isn't using
// a direct connection.
q->emitReadyWrite();
}
processNext();
return;
}
......@@ -163,6 +168,18 @@ void FileAbstractResumableJob::Private::processNext()
}
}
void KGAPI2::Drive::FileAbstractResumableJob::Private::readFromDevice()
{
char buf[ChunkSize];
int read = device->read(buf, ChunkSize);
if (read == -1) {
qCWarning(KGAPIDebug) << "Failed reading from device" << device->errorString();
return;
}
qCDebug(KGAPIDebug) << "Read from device bytes" << read;
q->write(QByteArray(buf, read));
}
bool FileAbstractResumableJob::Private::isTotalSizeKnown() const
{
return totalUploadSize != 0;
......@@ -193,6 +210,26 @@ FileAbstractResumableJob::FileAbstractResumableJob(const FilePtr &metadata,
d->metaData = metadata;
}
FileAbstractResumableJob::FileAbstractResumableJob(QIODevice *device,
const AccountPtr &account,
QObject *parent):
FileAbstractDataJob(account, parent),
d(new Private(this))
{
d->device = device;
}
FileAbstractResumableJob::FileAbstractResumableJob(QIODevice *device,
const FilePtr &metadata,
const AccountPtr &account,
QObject *parent):
FileAbstractDataJob(account, parent),
d(new Private(this))
{
d->device = device;
d->metaData = metadata;
}
FileAbstractResumableJob::~FileAbstractResumableJob() = default;
FilePtr FileAbstractResumableJob::metadata() const
......@@ -245,6 +282,9 @@ void FileAbstractResumableJob::write(const QByteArray &data)
void FileAbstractResumableJob::start()
{
if (d->device) {
d->readFromDevice();
}
// Ask for more chunks right away in case
// write() wasn't called before starting
if (d->chunks.isEmpty()) {
......
......@@ -66,6 +66,32 @@ class KGAPIDRIVE_EXPORT FileAbstractResumableJob : public KGAPI2::Drive::FileAbs
const AccountPtr &account,
QObject *parent = nullptr);
/**
* @brief Constructs a job that will upload an Untitled file in the
* users root folder with data contained in device.
*
* @param device Open device to read from
* @param account Account to authenticate the request
* @param parent
*/
explicit FileAbstractResumableJob(QIODevice *device,
const AccountPtr &account,
QObject *parent = nullptr);
/**
* @brief Constructs a job that will upload a file with its metadata
* with data contained in device.
*
* @param device Open device to read from
* @param metadata File metadata to upload
* @param account Account to authenticate the request
* @param parent
*/
explicit FileAbstractResumableJob(QIODevice *device,
const FilePtr &metadata,
const AccountPtr &account,
QObject *parent = nullptr);
/**
* @brief Destructor
*/
......
......@@ -33,6 +33,23 @@ FileResumableCreateJob::FileResumableCreateJob(const FilePtr &metadata,
{
}
FileResumableCreateJob::FileResumableCreateJob(QIODevice *device,
const AccountPtr &account,
QObject *parent):
FileAbstractResumableJob(device, account, parent),
d(new Private)
{
}
FileResumableCreateJob::FileResumableCreateJob(QIODevice *device,
const FilePtr &metadata,
const AccountPtr &account,
QObject *parent):
FileAbstractResumableJob(device, metadata, account, parent),
d(new Private)
{
}
FileResumableCreateJob::~FileResumableCreateJob() = default;
QUrl FileResumableCreateJob::createUrl()
......
......@@ -27,6 +27,10 @@ class KGAPIDRIVE_EXPORT FileResumableCreateJob : public KGAPI2::Drive::FileAbstr
QObject *parent = nullptr);
explicit FileResumableCreateJob(const FilePtr &metadata, const AccountPtr &account,
QObject *parent = nullptr);
explicit FileResumableCreateJob(QIODevice *device, const AccountPtr &account,
QObject *parent = nullptr);
explicit FileResumableCreateJob(QIODevice *device, const FilePtr &metadata,
const AccountPtr &account, QObject *parent = nullptr);
~FileResumableCreateJob() override;
protected:
......
......@@ -44,6 +44,26 @@ FileResumableModifyJob::FileResumableModifyJob(const QString &fileId,
d->fileId = fileId;
}
FileResumableModifyJob::FileResumableModifyJob(QIODevice *device,
const FilePtr &metadata,
const AccountPtr &account,
QObject *parent):
FileAbstractResumableJob(device, metadata, account, parent),
d(new Private)
{
d->fileId = metadata->id();
}
FileResumableModifyJob::FileResumableModifyJob(QIODevice *device,
const QString &fileId,
const AccountPtr &account,
QObject *parent):
FileAbstractResumableJob(device, account, parent),
d(new Private)
{
d->fileId = fileId;
}
FileResumableModifyJob::~FileResumableModifyJob() = default;
bool FileResumableModifyJob::createNewRevision() const
......
......@@ -64,6 +64,10 @@ class KGAPIDRIVE_EXPORT FileResumableModifyJob : public KGAPI2::Drive::FileAbstr
explicit FileResumableModifyJob(const QString &fileId, const AccountPtr &account,
QObject *parent = nullptr);
explicit FileResumableModifyJob(QIODevice *device, const FilePtr &metadata,
const AccountPtr &account, QObject *parent = nullptr);
explicit FileResumableModifyJob(QIODevice *device, const QString &fileId,
const AccountPtr &account, QObject *parent = nullptr);
~FileResumableModifyJob() override;
bool createNewRevision() const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment