2014-02-06 14:50:16 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@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-18 14:52:38 +04:00
|
|
|
#include "owncloudpropagator.h"
|
2014-02-06 14:50:16 +04:00
|
|
|
#include "networkjobs.h"
|
|
|
|
|
2014-02-18 14:52:38 +04:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QFile>
|
2014-11-11 12:10:46 +03:00
|
|
|
#include <QDebug>
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-11-14 18:40:13 +03:00
|
|
|
class BandwidthManager;
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2014-11-14 18:40:13 +03:00
|
|
|
class UploadDevice : public QIODevice {
|
|
|
|
Q_OBJECT
|
2014-02-10 16:00:22 +04:00
|
|
|
public:
|
2014-11-14 18:40:13 +03:00
|
|
|
UploadDevice(QIODevice *file, qint64 start, qint64 size, BandwidthManager *bwm);
|
|
|
|
~UploadDevice();
|
2015-01-14 14:48:38 +03:00
|
|
|
bool open(OpenMode mode) Q_DECL_OVERRIDE;
|
2015-01-14 13:54:04 +03:00
|
|
|
qint64 writeData(const char* , qint64 ) Q_DECL_OVERRIDE;
|
|
|
|
qint64 readData(char* data, qint64 maxlen) Q_DECL_OVERRIDE;
|
|
|
|
bool atEnd() const Q_DECL_OVERRIDE;
|
|
|
|
qint64 size() const Q_DECL_OVERRIDE;
|
|
|
|
qint64 bytesAvailable() const Q_DECL_OVERRIDE;
|
|
|
|
bool isSequential() const Q_DECL_OVERRIDE;
|
|
|
|
bool seek ( qint64 pos ) Q_DECL_OVERRIDE;
|
2014-02-10 16:00:22 +04:00
|
|
|
|
2014-11-14 18:40:13 +03:00
|
|
|
void setBandwidthLimited(bool);
|
|
|
|
bool isBandwidthLimited() { return _bandwidthLimited; }
|
|
|
|
void setChoked(bool);
|
|
|
|
bool isChoked() { return _choked; }
|
|
|
|
void giveBandwidthQuota(qint64 bwq);
|
|
|
|
private:
|
2015-01-14 14:48:38 +03:00
|
|
|
|
|
|
|
// Used before opening
|
|
|
|
QPointer<QIODevice> _file;
|
|
|
|
qint64 _size;
|
|
|
|
qint64 _start;
|
|
|
|
|
|
|
|
// Used after opening, in order to read
|
|
|
|
QByteArray _data;
|
|
|
|
qint64 _read;
|
|
|
|
|
|
|
|
// Bandith manager related
|
|
|
|
BandwidthManager* _bandwidthManager;
|
|
|
|
qint64 _bandwidthQuota;
|
|
|
|
qint64 _readWithProgress;
|
2014-11-14 18:40:13 +03:00
|
|
|
bool _bandwidthLimited; // if _bandwidthQuota will be used
|
|
|
|
bool _choked; // if upload is paused (readData() will return 0)
|
2015-01-14 14:48:38 +03:00
|
|
|
friend class BandwidthManager;
|
2014-11-14 18:40:13 +03:00
|
|
|
protected slots:
|
|
|
|
void slotJobUploadProgress(qint64 sent, qint64 t);
|
2014-02-10 16:00:22 +04:00
|
|
|
};
|
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
class PUTFileJob : public AbstractNetworkJob {
|
|
|
|
Q_OBJECT
|
2015-01-14 13:16:54 +03:00
|
|
|
QScopedPointer<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-12-18 14:09:48 +03:00
|
|
|
explicit PUTFileJob(AccountPtr 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
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual void start() Q_DECL_OVERRIDE;
|
2014-02-06 14:50:16 +04:00
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual bool finished() Q_DECL_OVERRIDE {
|
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;
|
|
|
|
};
|
|
|
|
|
2014-07-10 01:22:28 +04:00
|
|
|
virtual void slotTimeout() Q_DECL_OVERRIDE;
|
2014-04-30 19:54:14 +04:00
|
|
|
|
|
|
|
|
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-12-18 14:09:48 +03:00
|
|
|
explicit PollJob(AccountPtr 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
|
|
|
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;
|
2015-01-14 14:48:38 +03:00
|
|
|
bool _finished; // Tells that all the jobs have been 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-07-10 01:22:28 +04:00
|
|
|
void start() Q_DECL_OVERRIDE;
|
2014-02-06 14:50:16 +04:00
|
|
|
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-07-10 01:22:28 +04:00
|
|
|
void abort() Q_DECL_OVERRIDE;
|
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);
|
2015-01-14 14:48:38 +03:00
|
|
|
void abortWithError(SyncFileItem::Status status, const QString &error);
|
2014-02-10 16:00:22 +04:00
|
|
|
};
|
|
|
|
|
2014-02-06 14:50:16 +04:00
|
|
|
}
|
2014-11-11 14:16:14 +03:00
|
|
|
|