nextcloud-desktop/src/libsync/propagateupload.h

170 lines
5.4 KiB
C
Raw Normal View History

/*
* 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
#include "owncloudpropagator.h"
#include "networkjobs.h"
#include <QBuffer>
#include <QFile>
2014-11-11 12:10:46 +03:00
#include <QDebug>
2014-11-10 00:34:07 +03:00
namespace OCC {
2014-11-14 18:40:13 +03:00
class BandwidthManager;
2014-11-14 18:40:13 +03:00
class UploadDevice : public QIODevice {
Q_OBJECT
2014-02-10 16:00:22 +04:00
public:
2015-01-14 17:14:17 +03:00
UploadDevice(BandwidthManager *bwm);
2014-11-14 18:40:13 +03:00
~UploadDevice();
2015-01-14 17:14:17 +03:00
/** Reads the data from the file and opens the device */
bool prepareAndOpen(const QString& fileName, qint64 start, qint64 size);
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 17:14:17 +03:00
// The file data
QByteArray _data;
2015-01-14 17:14:17 +03:00
// Position in the data
qint64 _read;
// Bandwidth manager related
QPointer<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)
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
};
class PUTFileJob : public AbstractNetworkJob {
Q_OBJECT
QScopedPointer<QIODevice> _device;
QMap<QByteArray, QByteArray> _headers;
QString _errorString;
public:
2014-02-17 16:48:56 +04:00
// Takes ownership of the device
explicit PUTFileJob(AccountPtr account, const QString& path, QIODevice *device,
const QMap<QByteArray, QByteArray> &headers, int chunk, QObject* parent = 0)
: AbstractNetworkJob(account, path, parent), _device(device), _headers(headers), _chunk(chunk) {}
~PUTFileJob();
int _chunk;
virtual void start() Q_DECL_OVERRIDE;
virtual bool finished() Q_DECL_OVERRIDE {
emit finishedSignal();
return true;
}
QString errorString() {
return _errorString.isEmpty() ? reply()->errorString() : _errorString;
};
virtual void slotTimeout() Q_DECL_OVERRIDE;
signals:
void finishedSignal();
void uploadProgress(qint64,qint64);
};
2015-03-04 10:42:24 +03:00
/**
* This job implements the assynchronous PUT
* If the server replies to a PUT with a OC-Finish-Poll url, we will query this url until the server
* replies with an etag
* https://github.com/owncloud/core/issues/12097
*/
class PollJob : public AbstractNetworkJob {
Q_OBJECT
SyncJournalDb *_journal;
QString _localPath;
public:
SyncFileItem _item;
// Takes ownership of the device
explicit PollJob(AccountPtr account, const QString &path, const SyncFileItem &item,
SyncJournalDb *journal, const QString &localPath, QObject *parent)
: AbstractNetworkJob(account, path, parent), _journal(journal), _localPath(localPath), _item(item) {}
void start() Q_DECL_OVERRIDE;
bool finished() Q_DECL_OVERRIDE;
void slotTimeout() Q_DECL_OVERRIDE {
// emit finishedSignal(false);
// deleteLater();
2014-07-29 21:51:26 +04:00
qDebug() << Q_FUNC_INFO;
reply()->abort();
}
signals:
void finishedSignal();
};
2014-02-17 16:48:56 +04:00
class PropagateUploadFileQNAM : public PropagateItemJob {
Q_OBJECT
2015-03-04 10:42:24 +03:00
/**
* That's the start chunk that was stored in the database for resuming.
* In the non-resuming case it is 0.
* If we are resuming, this is the first chunk we need to send
*/
int _startChunk;
2015-03-04 10:42:24 +03:00
/**
* This is the next chunk that we need to send. Starting from 0 even if _startChunk != 0
* (In other words, _startChunk + _currentChunk is really the number of the chunk we need to send next)
* (In other words, _currentChunk is the number of chunk that we already sent or start sending)
*/
int _currentChunk;
2015-03-04 10:42:24 +03:00
int _chunkCount; /// Total number of chunks for this file
int _transferId; /// transfer id (part of the url)
QElapsedTimer _duration;
2015-03-04 10:42:24 +03:00
QVector<PUTFileJob*> _jobs; /// network jobs that are currently in transit
bool _finished; // Tells that all the jobs have been finished
public:
PropagateUploadFileQNAM(OwncloudPropagator* propagator,const SyncFileItem& item)
: PropagateItemJob(propagator, item), _startChunk(0), _currentChunk(0), _chunkCount(0), _transferId(0), _finished(false) {}
void start() Q_DECL_OVERRIDE;
private slots:
void slotPutFinished();
void slotPollFinished();
void slotUploadProgress(qint64,qint64);
void abort() Q_DECL_OVERRIDE;
void startNextChunk();
void finalize(const SyncFileItem&);
void slotJobDestroyed(QObject *job);
private:
void startPollJob(const QString& path);
void abortWithError(SyncFileItem::Status status, const QString &error);
2014-02-10 16:00:22 +04:00
};
}