From 6e6631ea46a30b3a727f3b7b2b0fcbd4644d85c6 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Wed, 16 Mar 2011 00:36:20 +0100 Subject: update progress now calls updateProgressBar(percent, speed) speed in bytes/sec atm, adapt your sites... --- src/downloadManager.cpp | 79 +++++++++++++++++++++++++++------------------ src/downloadManager.h | 4 ++- src/fbgui.cpp | 2 +- src/javascriptInterface.cpp | 7 ++-- src/javascriptInterface.h | 2 +- 5 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/downloadManager.cpp b/src/downloadManager.cpp index f7a6372..e17b459 100644 --- a/src/downloadManager.cpp +++ b/src/downloadManager.cpp @@ -81,17 +81,13 @@ void downloadManager::startNextDownload() return; } + /* Send the request for the file */ QNetworkRequest request(url); currentDownload = qnam->get(request); - if (currentDownload->error() != QNetworkReply::NoError) - { - if (debug) qDebug() << "Network reply error, skipping download..."; - return; - } - lastProgress = 0; currentProgress = 0; dip = true; + dltime.start(); QObject::connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReady())); QObject::connect(currentDownload, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); @@ -108,16 +104,28 @@ void downloadManager::downloadReady() // ---------------------------------------------------------------------------------------- void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) { + // "fix" for the weird bytesTotal = -1 initial reading... if (bytesIn > bytesTotal) return; - + /* Calculate current speed */ + double speed = bytesIn * 1000 / dltime.elapsed(); + QString unit; + if (speed < 1024) { + unit = "bytes/sec"; + } else if (speed < 1024*1024) { + speed /= 1024; + unit = "KB/s"; + } else { + speed /= 1024*1024; + unit = "MB/s"; + } + if (debug) qDebug() << "speed: " << speed << unit; /* Update progress only if difference higher than the updateInterval setting */ - int tmp = ((bytesIn * 100) / bytesTotal); - if (tmp > 0) - currentProgress = tmp; + currentProgress = ((bytesIn * 100) / bytesTotal); if (currentProgress - lastProgress >= updateInterval){ lastProgress = currentProgress; - emit updateProgress(currentDownload->url().toString(), currentProgress); + + emit updateProgress(currentProgress, speed); if (debug) qDebug() << "Download progress of " << currentDownload->url().toString() << ": " << bytesIn << "/" << bytesTotal << "(" << currentProgress << "\%)"; } @@ -126,27 +134,12 @@ void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) // ---------------------------------------------------------------------------------------- void downloadManager::downloadFinished() { - /* Header filename fetching & renaming - const QByteArray cd = "Content-Disposition"; - QByteArray cdc = currentDownload->rawHeader(cd); - int x = cdc.indexOf("filename=\"") + 10; - cdc.remove(0, x).chop(1); - if (!cdc.isEmpty()) - currentTargetFilename = cdc; - else - currentTargetFilename = QString("download.\%1").arg(downloaded); - QString tmp = outfile.fileName(); - tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); - qDebug() << "Trying to rename " << tmp << " to --> " << currentTargetFilename; - - if (outfile.rename(downloadPath + "/" + currentTargetFilename)) { - if (debug) qDebug() << "Renamed file!"; - } - else { - if (debug) qDebug() << "Failure to rename file!"; - } - */ + if (currentDownload->error()) + if (debug) qDebug() << "Download of" << currentDownload->url().toString() + << "failed with status code: " << currentDownload->error(); + // TODO Handle errors. + if (debug) qDebug() << "NetworkCode: " << currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); outfile.close(); currentDownload->deleteLater(); @@ -162,5 +155,27 @@ void downloadManager::downloadFinished() } startNextDownload(); } -// ---------------------------------------------------------------------------------------- +/* ---------------------------------------------------------------------------------------- +* + ** dead code: Header filename fetching & renaming ** + +const QByteArray cd = "Content-Disposition"; +QByteArray cdc = currentDownload->rawHeader(cd); +int x = cdc.indexOf("filename=\"") + 10; +cdc.remove(0, x).chop(1); +if (!cdc.isEmpty()) + currentTargetFilename = cdc; +else + currentTargetFilename = QString("download.\%1").arg(downloaded); +QString tmp = outfile.fileName(); +tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); +qDebug() << "Trying to rename " << tmp << " to --> " << currentTargetFilename; + +if (outfile.rename(downloadPath + "/" + currentTargetFilename)) { + if (debug) qDebug() << "Renamed file!"; +} +else { + if (debug) qDebug() << "Failure to rename file!"; +} +*/ diff --git a/src/downloadManager.h b/src/downloadManager.h index c556cb3..6c96633 100644 --- a/src/downloadManager.h +++ b/src/downloadManager.h @@ -23,6 +23,7 @@ #include #include #include +#include extern bool debug; extern QUrl baseURL; @@ -46,6 +47,7 @@ private: QNetworkReply* currentDownload; QFile outfile; QDir downloadDir; + QTime dltime; bool dip; int currentProgress, lastProgress; @@ -54,7 +56,7 @@ private: signals: void finished(); - void updateProgress(QString current, int i); + void updateProgress(int percent, double speed); void downloadQueueEmpty(); public slots: diff --git a/src/fbgui.cpp b/src/fbgui.cpp index e74db26..5a2d985 100644 --- a/src/fbgui.cpp +++ b/src/fbgui.cpp @@ -34,7 +34,7 @@ fbgui::fbgui() /* Init Download Manager */ downloadManager* dm = new downloadManager(); QObject::connect(jsi, SIGNAL(requestFile(QString&)), dm, SLOT(downloadFile(QString&))); - QObject::connect(dm, SIGNAL(updateProgress(QString, int)), jsi, SLOT(updateProgressBar(QString, int))); + QObject::connect(dm, SIGNAL(updateProgress(int, double)), jsi, SLOT(updateProgressBar(int, double))); QObject::connect(dm, SIGNAL(downloadQueueEmpty()), jsi, SLOT(callbackOnDlQueueFinished())); setAttribute(Qt::WA_QuitOnClose, true); diff --git a/src/javascriptInterface.cpp b/src/javascriptInterface.cpp index d57ce44..399f863 100644 --- a/src/javascriptInterface.cpp +++ b/src/javascriptInterface.cpp @@ -33,10 +33,10 @@ void javascriptInterface::startDownload(QString filename) emit requestFile(filename); } //------------------------------------------------------------------------------------------------------- -void javascriptInterface::updateProgressBar(QString current, int i) +void javascriptInterface::updateProgressBar(int percent, double speed) { - if (i == 0) return; - QString code = QString("updateProgress('\%1', \%2)").arg(current).arg(i); + if (percent == 0) return; + QString code = QString("updateProgress('\%1', \%2)").arg(percent).arg(speed); if (debug) qDebug() << "To JS: " << code; _parent->evaluateJavaScript(code); } @@ -57,4 +57,3 @@ void javascriptInterface::quit() if (debug) qDebug() << "Quit signal."; emit quitFbgui(); } - diff --git a/src/javascriptInterface.h b/src/javascriptInterface.h index 6ee113b..beb8205 100644 --- a/src/javascriptInterface.h +++ b/src/javascriptInterface.h @@ -39,7 +39,7 @@ public slots: void startDownload(QString filename); void setCallbackOnDlQueueFinished(QString fctOnDownloadsFinished); void callbackOnDlQueueFinished(); - void updateProgressBar(QString current, int i); + void updateProgressBar(int percent, double speed); QString getSysInfo(QString info); void quit(); -- cgit v1.2.3-55-g7522