2014-02-06 14:50:16 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@owncloud.com>
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-02-18 14:52:38 +04:00
|
|
|
#include "owncloudpropagator.h"
|
2014-02-06 14:50:16 +04:00
|
|
|
#include "owncloudpropagator_p.h"
|
|
|
|
#include "networkjobs.h"
|
|
|
|
|
2014-02-18 14:52:38 +04:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QFile>
|
2014-02-06 14:50:16 +04:00
|
|
|
|
|
|
|
namespace Mirall {
|
|
|
|
|
2014-02-10 16:00:22 +04:00
|
|
|
class ChunkBlock {
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ChunkBlock() : _state(NotTransfered) { }
|
|
|
|
enum State {
|
|
|
|
CHUNK_SUCCESS,
|
|
|
|
NotTransfered, /* never tried to transfer */
|
|
|
|
Transfered, /* transfer currently running */
|
|
|
|
TransferFailed, /* transfer tried but failed */
|
|
|
|
TransferSuccess, /* block transfer succeeded. */
|
|
|
|
Fail
|
|
|
|
};
|
|
|
|
|
|
|
|
int _sequenceNo;
|
|
|
|
int64_t _start;
|
|
|
|
int64_t _size;
|
|
|
|
|
|
|
|
State _state;
|
|
|
|
int _httpResultCode;
|
|
|
|
QString _httpErrorMsg;
|
|
|
|
QString _etag;
|
|
|
|
QBuffer *_buffer;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
class PUTFileJob : public AbstractNetworkJob {
|
|
|
|
Q_OBJECT
|
2014-09-29 12:30:39 +04:00
|
|
|
QSharedPointer<QIODevice> _device;
|
2014-02-06 14:50:16 +04:00
|
|
|
QMap<QByteArray, QByteArray> _headers;
|
2014-04-30 19:54:14 +04:00
|
|
|
QString _errorString;
|
2014-02-06 14:50:16 +04:00
|
|
|
|
|
|
|
public:
|
2014-02-17 16:48:56 +04:00
|
|
|
// Takes ownership of the device
|
2014-02-10 16:00:22 +04:00
|
|
|
explicit PUTFileJob(Account* account, const QString& path, QIODevice *device,
|
2014-09-15 19:55:55 +04:00
|
|
|
const QMap<QByteArray, QByteArray> &headers, int chunk, QObject* parent = 0)
|
|
|
|
: AbstractNetworkJob(account, path, parent), _device(device), _headers(headers), _chunk(chunk) {}
|
|
|
|
|
|
|
|
int _chunk;
|
2014-02-06 14:50:16 +04:00
|
|
|
|
|
|
|
virtual void start();
|
|
|
|
|
2014-04-14 17:08:43 +04:00
|
|
|
virtual bool finished() {
|
2014-02-06 14:50:16 +04:00
|
|
|
emit finishedSignal();
|
2014-04-14 17:08:43 +04:00
|
|
|
return true;
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|
|
|
|
|
2014-04-30 19:54:14 +04:00
|
|
|
QString errorString() {
|
|
|
|
return _errorString.isEmpty() ? reply()->errorString() : _errorString;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void slotTimeout();
|
|
|
|
|
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
signals:
|
|
|
|
void finishedSignal();
|
2014-03-14 16:03:16 +04:00
|
|
|
void uploadProgress(qint64,qint64);
|
2014-02-06 14:50:16 +04:00
|
|
|
};
|
|
|
|
|
2014-07-25 15:30:48 +04:00
|
|
|
class PollJob : public AbstractNetworkJob {
|
|
|
|
Q_OBJECT
|
2014-07-28 14:12:52 +04:00
|
|
|
SyncJournalDb *_journal;
|
|
|
|
QString _localPath;
|
2014-07-25 15:30:48 +04:00
|
|
|
public:
|
2014-07-28 14:12:52 +04:00
|
|
|
SyncFileItem _item;
|
2014-07-25 15:30:48 +04:00
|
|
|
// Takes ownership of the device
|
2014-07-29 17:51:22 +04:00
|
|
|
explicit PollJob(Account* account, const QString &path, const SyncFileItem &item,
|
2014-07-28 14:12:52 +04:00
|
|
|
SyncJournalDb *journal, const QString &localPath, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent), _journal(journal), _localPath(localPath), _item(item) {}
|
2014-07-25 15:30:48 +04:00
|
|
|
|
2014-07-28 14:12:52 +04:00
|
|
|
void start() Q_DECL_OVERRIDE;
|
|
|
|
bool finished() Q_DECL_OVERRIDE;
|
|
|
|
void slotTimeout() Q_DECL_OVERRIDE {
|
2014-07-25 15:30:48 +04:00
|
|
|
// emit finishedSignal(false);
|
|
|
|
// deleteLater();
|
2014-07-29 21:51:26 +04:00
|
|
|
qDebug() << Q_FUNC_INFO;
|
2014-07-25 15:30:48 +04:00
|
|
|
reply()->abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
2014-07-28 14:12:52 +04:00
|
|
|
void finishedSignal();
|
2014-07-25 15:30:48 +04:00
|
|
|
};
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
class PropagateUploadFileQNAM : public PropagateItemJob {
|
|
|
|
Q_OBJECT
|
2014-02-13 17:02:05 +04:00
|
|
|
QFile *_file;
|
|
|
|
int _startChunk;
|
|
|
|
int _currentChunk;
|
|
|
|
int _chunkCount;
|
|
|
|
int _transferId;
|
2014-03-26 20:58:32 +04:00
|
|
|
QElapsedTimer _duration;
|
2014-09-15 19:55:55 +04:00
|
|
|
QVector<PUTFileJob*> _jobs;
|
|
|
|
bool _finished;
|
2014-02-06 14:50:16 +04:00
|
|
|
public:
|
2014-02-13 17:02:05 +04:00
|
|
|
PropagateUploadFileQNAM(OwncloudPropagator* propagator,const SyncFileItem& item)
|
2014-09-15 19:55:55 +04:00
|
|
|
: PropagateItemJob(propagator, item), _startChunk(0), _currentChunk(0), _chunkCount(0), _transferId(0), _finished(false) {}
|
2014-02-06 14:50:16 +04:00
|
|
|
void start();
|
|
|
|
private slots:
|
|
|
|
void slotPutFinished();
|
2014-07-28 14:12:52 +04:00
|
|
|
void slotPollFinished();
|
2014-03-14 16:03:16 +04:00
|
|
|
void slotUploadProgress(qint64,qint64);
|
2014-02-06 17:52:56 +04:00
|
|
|
void abort();
|
2014-02-13 17:02:05 +04:00
|
|
|
void startNextChunk();
|
2014-07-15 19:52:01 +04:00
|
|
|
void finalize(const SyncFileItem&);
|
2014-09-15 19:55:55 +04:00
|
|
|
void slotJobDestroyed(QObject *job);
|
2014-07-25 15:30:48 +04:00
|
|
|
private:
|
|
|
|
void startPollJob(const QString& path);
|
2014-02-10 16:00:22 +04:00
|
|
|
};
|
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
class GETFileJob : public AbstractNetworkJob {
|
|
|
|
Q_OBJECT
|
2014-07-18 14:02:57 +04:00
|
|
|
QFile* _device;
|
2014-02-17 16:48:56 +04:00
|
|
|
QMap<QByteArray, QByteArray> _headers;
|
2014-03-19 18:19:09 +04:00
|
|
|
QString _errorString;
|
2014-03-20 16:26:40 +04:00
|
|
|
QByteArray _expectedEtagForResume;
|
2014-07-18 14:02:57 +04:00
|
|
|
quint64 _resumeStart;
|
2014-04-22 14:34:03 +04:00
|
|
|
SyncFileItem::Status _errorStatus;
|
2014-06-03 13:50:13 +04:00
|
|
|
QUrl _directDownloadUrl;
|
|
|
|
QByteArray _etag;
|
2014-09-29 12:30:39 +04:00
|
|
|
bool _bandwidthLimited; // if _bandwidthQuota will be used
|
|
|
|
bool _bandwidthChoked; // if download is paused (won't read on readyRead())
|
|
|
|
qint64 _bandwidthQuota;
|
|
|
|
BandwidthManager *_bandwidthManager;
|
|
|
|
bool _hasEmittedFinishedSignal;
|
2014-02-17 16:48:56 +04:00
|
|
|
public:
|
2014-03-19 18:19:09 +04:00
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
// DOES NOT take owncership of the device.
|
2014-07-18 14:02:57 +04:00
|
|
|
explicit GETFileJob(Account* account, const QString& path, QFile *device,
|
2014-03-20 16:26:40 +04:00
|
|
|
const QMap<QByteArray, QByteArray> &headers, QByteArray expectedEtagForResume,
|
2014-07-18 18:59:29 +04:00
|
|
|
quint64 resumeStart, QObject* parent = 0);
|
2014-06-03 13:50:13 +04:00
|
|
|
// For directDownloadUrl:
|
2014-07-18 18:59:29 +04:00
|
|
|
explicit GETFileJob(Account* account, const QUrl& url, QFile *device,
|
2014-06-03 13:50:13 +04:00
|
|
|
const QMap<QByteArray, QByteArray> &headers,
|
|
|
|
QObject* parent = 0);
|
2014-09-29 12:30:39 +04:00
|
|
|
virtual ~GETFileJob() {
|
|
|
|
if (_bandwidthManager) {
|
|
|
|
_bandwidthManager->unregisterDownloadJob(this);
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
|
|
|
|
virtual void start();
|
2014-04-14 17:08:43 +04:00
|
|
|
virtual bool finished() {
|
2014-09-29 12:30:39 +04:00
|
|
|
if (reply()->bytesAvailable()) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Not all read yet because of bandwidth limits";
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (_bandwidthManager) {
|
|
|
|
_bandwidthManager->unregisterDownloadJob(this);
|
|
|
|
}
|
|
|
|
if (!_hasEmittedFinishedSignal) {
|
|
|
|
emit finishedSignal();
|
|
|
|
}
|
|
|
|
_hasEmittedFinishedSignal = true;
|
|
|
|
return true; // discard
|
|
|
|
}
|
2014-02-17 16:48:56 +04:00
|
|
|
}
|
|
|
|
|
2014-09-29 12:30:39 +04:00
|
|
|
void setBandwidthManager(BandwidthManager *bwm);
|
|
|
|
void setChoked(bool c);
|
|
|
|
void setBandwidthLimited(bool b);
|
|
|
|
void giveBandwidthQuota(qint64 q);
|
|
|
|
|
2014-03-19 18:19:09 +04:00
|
|
|
QString errorString() {
|
|
|
|
return _errorString.isEmpty() ? reply()->errorString() : _errorString;
|
|
|
|
};
|
|
|
|
|
2014-04-22 14:34:03 +04:00
|
|
|
SyncFileItem::Status errorStatus() { return _errorStatus; }
|
|
|
|
|
2014-04-30 19:54:14 +04:00
|
|
|
virtual void slotTimeout();
|
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
QByteArray &etag() { return _etag; }
|
|
|
|
|
2014-04-30 19:54:14 +04:00
|
|
|
|
2014-02-17 16:48:56 +04:00
|
|
|
signals:
|
|
|
|
void finishedSignal();
|
2014-03-14 16:03:16 +04:00
|
|
|
void downloadProgress(qint64,qint64);
|
2014-02-17 16:48:56 +04:00
|
|
|
private slots:
|
|
|
|
void slotReadyRead();
|
2014-03-20 16:26:40 +04:00
|
|
|
void slotMetaDataChanged();
|
2014-02-17 16:48:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class PropagateDownloadFileQNAM : public PropagateItemJob {
|
|
|
|
Q_OBJECT
|
|
|
|
QPointer<GETFileJob> _job;
|
|
|
|
|
|
|
|
// QFile *_file;
|
|
|
|
QFile _tmpFile;
|
2014-03-14 16:03:16 +04:00
|
|
|
quint64 _startSize;
|
2014-02-17 16:48:56 +04:00
|
|
|
public:
|
|
|
|
PropagateDownloadFileQNAM(OwncloudPropagator* propagator,const SyncFileItem& item)
|
2014-03-14 16:03:16 +04:00
|
|
|
: PropagateItemJob(propagator, item), _startSize(0) {}
|
2014-02-17 16:48:56 +04:00
|
|
|
void start();
|
|
|
|
private slots:
|
|
|
|
void slotGetFinished();
|
|
|
|
void abort();
|
|
|
|
void downloadFinished();
|
2014-03-14 16:03:16 +04:00
|
|
|
void slotDownloadProgress(qint64,qint64);
|
2014-02-17 16:48:56 +04:00
|
|
|
};
|
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|