diff options
Diffstat (limited to 'Uploader.cpp')
-rw-r--r-- | Uploader.cpp | 39 |
1 files changed, 34 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(); } |