summaryrefslogtreecommitdiffstats
path: root/src/downloadManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/downloadManager.cpp')
-rw-r--r--src/downloadManager.cpp79
1 files changed, 47 insertions, 32 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!";
+}
+*/