Calculate the speed in FileTransferJob if we know the size

KJob doesn't do this for us so we have to do the math
ourselves.
This commit is contained in:
Àlex Fiestas 2014-03-03 21:12:06 +01:00
parent 39589434bd
commit 82a686dbe5
2 changed files with 21 additions and 3 deletions

View file

@ -37,7 +37,9 @@ FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, int si
mOrigin = origin; mOrigin = origin;
mSize = size; mSize = size;
mWritten = 0; mWritten = 0;
m_speedBytes = 0;
mDeviceName = i18nc("Device name that will appear on the jobs", "KDE-Connect"); mDeviceName = i18nc("Device name that will appear on the jobs", "KDE-Connect");
qDebug() << "SIZE: " << mSize;
kDebug(kdeconnect_kded()) << "FileTransferJob Downloading payload to" << destination; kDebug(kdeconnect_kded()) << "FileTransferJob Downloading payload to" << destination;
} }
@ -49,7 +51,9 @@ void FileTransferJob::openFinished(KJob* job)
void FileTransferJob::start() void FileTransferJob::start()
{ {
//kDebug(kdeconnect_kded()) << "FileTransferJob start"; //kDebug(kdeconnect_kded()) << "FileTransferJob start";
setTotalAmount(Bytes, mSize);
setProcessedAmount(Bytes, 0);
m_time = QTime::currentTime();
description(this, i18n("Receiving file over KDE-Connect"), description(this, i18n("Receiving file over KDE-Connect"),
QPair<QString, QString>(i18nc("File transfer origin", "From"), QPair<QString, QString>(i18nc("File transfer origin", "From"),
QString(mDeviceName)), QString(mDeviceName)),
@ -84,12 +88,23 @@ void FileTransferJob::readyRead()
int bytes = qMin(qint64(4096), mOrigin->bytesAvailable()); int bytes = qMin(qint64(4096), mOrigin->bytesAvailable());
QByteArray data = mOrigin->read(bytes); QByteArray data = mOrigin->read(bytes);
mDestination->write(data); mDestination->write(data);
mWritten += bytes; mWritten += data.size();
setProcessedAmount(Bytes, mWritten);
//kDebug(kdeconnect_kded()) << "readyRead" << mSize << mWritten << bytes; //kDebug(kdeconnect_kded()) << "readyRead" << mSize << mWritten << bytes;
if (mSize > -1) { if (mSize > -1) {
setPercent((mWritten*100)/mSize); //If a least 1 second has passed since last update
int secondsSinceLastTime = m_time.secsTo(QTime::currentTime());
if (secondsSinceLastTime > 0 && m_speedBytes > 0) {
float speed = (mWritten - m_speedBytes) / secondsSinceLastTime;
emitSpeed(speed);
m_time = QTime::currentTime();
m_speedBytes = mWritten;
} else if(m_speedBytes == 0) {
m_speedBytes = mWritten;
}
} }
if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached

View file

@ -22,6 +22,7 @@
#define FILETRANSFERJOB_H #define FILETRANSFERJOB_H
#include <QIODevice> #include <QIODevice>
#include <QTime>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <KJob> #include <KJob>
@ -50,6 +51,8 @@ private:
QSharedPointer<QIODevice> mOrigin; QSharedPointer<QIODevice> mOrigin;
KIO::FileJob* mDestination; KIO::FileJob* mDestination;
QString mDeviceName; QString mDeviceName;
QTime m_time;
qulonglong m_speedBytes;
int mSize; int mSize;
int mWritten; int mWritten;