summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2012-12-25 14:39:29 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2012-12-25 14:39:29 +0100
commit522e1cc7388e39f9065f5894def3fbd0b260aa2f (patch)
treeb01c0da662a955f1bf0ec2c3fbfaa44a01c25036
parentLess cluttered indicator. (diff)
downloadoldgen-zmusicuploader-522e1cc7388e39f9065f5894def3fbd0b260aa2f.tar.xz
oldgen-zmusicuploader-522e1cc7388e39f9065f5894def3fbd0b260aa2f.zip
Fix time display.
-rw-r--r--Uploader.cpp39
-rw-r--r--Uploader.h5
2 files changed, 39 insertions, 5 deletions
diff --git a/Uploader.cpp b/Uploader.cpp
index 21322f2..271d026 100644
--- a/Uploader.cpp
+++ b/Uploader.cpp
@@ -17,7 +17,10 @@ Uploader::Uploader(const QString &ftpServer, const QString &ftpPath, const QStri
m_cdCommand(-1),
m_latestCommand(-1),
m_latestPut(-1),
- m_currentFile(0)
+ m_currentFile(0),
+ m_totalBytes(0),
+ m_totalBytesLeft(0),
+ m_currentSize(0)
{
connect(&m_ftp, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
connect(&m_ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(commandFinished(int,bool)));
@@ -78,6 +81,10 @@ void Uploader::commandFinished(int id, bool error)
emit done();
return;
}
+ foreach (const QFileInfo &info, m_fileList.values())
+ m_totalBytes += info.size();
+ m_totalBytesLeft = m_totalBytes;
+ m_totalRateCalculator.reset();
m_listPosition = m_fileList.constBegin();
uploadNextFile();
} else if (id == m_latestPut) {
@@ -103,8 +110,10 @@ void Uploader::uploadNextFile()
emit done();
return;
}
+ m_totalBytesLeft -= m_currentSize;
++m_listPositionNumber;
cerr << "[" << m_listPositionNumber << " of " << m_fileList.count() << "] Uploading " << m_listPosition.value().absoluteFilePath().toStdString() << endl;
+ m_currentSize = m_listPosition.value().size();
m_currentFile = new QFile(m_listPosition.value().absoluteFilePath());
m_currentFile->open(QIODevice::ReadOnly);
XorEncrypter *xorEncrypter = new XorEncrypter(m_secretKey, m_currentFile);
@@ -112,16 +121,36 @@ void Uploader::uploadNextFile()
m_latestPut = m_ftp.put(xorEncrypter, m_listPosition.key());
++m_listPosition;
}
+QString Uploader::formatTime(const qint64 time) const
+{
+ const qint64 hours = time / 1000 / (60 * 60);
+ const qint64 minutes = time / 1000 % (60 * 60) / 60;
+ const qint64 seconds = time / 1000 % (60 * 60) % 60;
+ return QString("%1:%2:%3").arg(hours, 2, 10, QChar('0')).arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0'));
+}
void Uploader::dataTransferProgress(qint64 done, qint64 total)
{
m_rateCalculator.updateTotalBytes(done);
+ m_totalRateCalculator.updateTotalBytes(m_totalBytes - m_totalBytesLeft + done);
const double rate = m_rateCalculator.rate(25000);
- qint64 time;
- if (done == total)
+ qint64 time, totalTime;
+ if (done == total) {
time = m_rateCalculator.elapsed();
- else
+ totalTime = m_totalRateCalculator.elapsed();
+ } else {
time = (total - done) / rate;
+ totalTime = (m_totalBytesLeft - done) / m_totalRateCalculator.rate();
+ }
if (rate < 0 || time < 0)
return;
- cerr << "\r\033[2K" << static_cast<qint64>(round(done / 1024.0)) << "/" << static_cast<qint64>(round(total / 1024.0)) << " kB | " << static_cast<qint64>(round(rate * 1024.0 / 1000.0)) << " kB/s | ETA: " << time / 1000 / 60 << ":" << time / 1000 % 60;
+ cerr << "\r\033[2K"
+ << static_cast<qint64>(round(done / 1024.0))
+ << "/"
+ << static_cast<qint64>(round(total / 1024.0))
+ << " kB | "
+ << static_cast<qint64>(round(rate * 1024.0 / 1000.0))
+ << " kB/s | File ETA: "
+ << formatTime(time).toStdString()
+ << " | Total ETA: "
+ << formatTime(totalTime).toStdString();
}
diff --git a/Uploader.h b/Uploader.h
index e4e9551..bf35bb6 100644
--- a/Uploader.h
+++ b/Uploader.h
@@ -18,6 +18,7 @@ public:
private:
void uploadNextFile();
+ QString formatTime(const qint64 time) const;
QHash<const QString, QFileInfo> m_fileList;
QHash<const QString, QFileInfo>::const_iterator m_listPosition;
int m_listPositionNumber;
@@ -32,6 +33,10 @@ private:
int m_latestPut;
QFile *m_currentFile;
RateCalculator m_rateCalculator;
+ RateCalculator m_totalRateCalculator;
+ qint64 m_totalBytes;
+ qint64 m_totalBytesLeft;
+ qint64 m_currentSize;
private slots:
void stateChanged(int state);