]> git.sur5r.net Git - minitube/blobdiff - src/downloaditem.cpp
Imported Upstream version 2.0
[minitube] / src / downloaditem.cpp
index 05995370968b5fa1b07aed5298e0c6a2971f5342..0ff2396bd90f1583264c33771009a363d6ff613f 100644 (file)
@@ -42,7 +42,7 @@ DownloadItem::~DownloadItem() {
 }
 
 void DownloadItem::start() {
-    m_reply = The::http()->simpleGet(m_url);
+    m_reply = The::http()->request(m_url);
     init();
 }
 
@@ -71,6 +71,7 @@ void DownloadItem::init() {
             this, SLOT(requestFinished()));
 
     // start timer for the download estimation
+    m_totalTime = 0;
     m_downloadTime.start();
     speedCheckTimer->start();
 
@@ -158,7 +159,7 @@ void DownloadItem::metaDataChanged() {
     QVariant locationHeader = m_reply->header(QNetworkRequest::LocationHeader);
     if (locationHeader.isValid()) {
         m_url = locationHeader.toUrl();
-        qDebug() << "Redirecting to" << m_url;
+        // qDebug() << "Redirecting to" << m_url;
         tryAgain();
         return;
     }
@@ -203,7 +204,10 @@ void DownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
             m_status = Downloading;
             emit statusChanged();
         } else {
-            int bufferPercent = bytesReceived * 100 / qMax(bufferSize, neededBytes);
+            int bytes = qMax(bufferSize, neededBytes);
+            int bufferPercent = 0;
+            if (bytes > 0)
+                bufferPercent = bytesReceived * 100 / bytes;
             emit bufferProgress(bufferPercent);
         }
 
@@ -261,7 +265,10 @@ double DownloadItem::remainingTime() const {
     if (m_finishedDownloading)
         return -1.0;
 
-    double timeRemaining = ((double)(bytesTotal() - bytesReceived())) / currentSpeed();
+    double speed = currentSpeed();
+    double timeRemaining = 0.0;
+    if (speed > 0.0)
+        timeRemaining = ((double)(bytesTotal() - bytesReceived())) / speed;
 
     // When downloading the eta should never be 0
     if (timeRemaining == 0)
@@ -274,7 +281,11 @@ double DownloadItem::currentSpeed() const {
     if (m_finishedDownloading)
         return -1.0;
 
-    return m_bytesReceived * 1000.0 / m_downloadTime.elapsed();
+    int elapsed = m_downloadTime.elapsed();
+    double speed = -1.0;
+    if (elapsed > 0)
+        speed = m_bytesReceived * 1000.0 / elapsed;
+    return speed;
 }
 
 void DownloadItem::requestFinished() {
@@ -298,6 +309,7 @@ void DownloadItem::requestFinished() {
     }
     m_file.close();
     m_status = Finished;
+    m_totalTime = m_downloadTime.elapsed() / 1000.0;
     emit statusChanged();
     emit finished();
     m_reply->deleteLater();
@@ -333,14 +345,15 @@ QString DownloadItem::formattedSpeed(double speed) {
     return QString(QLatin1String("%1 %2")).arg(speedInt).arg(unit);
 }
 
-QString DownloadItem::formattedTime(double timeRemaining) {
+QString DownloadItem::formattedTime(double timeRemaining, bool remaining) {
     QString timeRemainingString = tr("seconds");
     if (timeRemaining > 60) {
         timeRemaining = timeRemaining / 60;
         timeRemainingString = tr("minutes");
     }
     timeRemaining = floor(timeRemaining);
-    return tr("%4 %5 remaining")
+    QString msg = remaining ? tr("%4 %5 remaining") : "%4 %5";
+        return msg
             .arg(timeRemaining)
             .arg(timeRemainingString);
 }