From 49e9f2d6338cddac72f24aa1cad926ae983a5a96 Mon Sep 17 00:00:00 2001 From: Flavio Date: Thu, 27 Sep 2012 11:52:00 +0200 Subject: [PATCH] Fixed division by zero --- src/downloaditem.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/downloaditem.cpp b/src/downloaditem.cpp index 6baab76..39132eb 100644 --- a/src/downloaditem.cpp +++ b/src/downloaditem.cpp @@ -204,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); } @@ -262,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) @@ -275,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() { -- 2.39.5