Browse Source

Added extra information in downloader class

Ivan Arkhipov 5 years ago
parent
commit
0f82b83808
2 changed files with 48 additions and 5 deletions
  1. 17 3
      src/Legacy/models/downloader.cpp
  2. 31 2
      src/Legacy/models/downloader.h

+ 17 - 3
src/Legacy/models/downloader.cpp

@@ -59,14 +59,24 @@ quint64 Downloader::getBytesDownloaded()
 
 quint64 Downloader::getElapsedTime()
 {
-    return (getBytesTotal() - getBytesDownloaded()) / qMax(quint64(1), getSpeed());
+    return (getBytesTotal() - getBytesDownloaded()) / qMax(quint64(1), getCurrentSpeed());
 }
 
-quint64 Downloader::getSpeed()
+quint64 Downloader::getCurrentSpeed()
 {
     return download_speed;
 }
 
+quint64 Downloader::getAverageSpeed()
+{
+    return average_speed;
+}
+
+Downloader::Status Downloader::getDownloadStatus()
+{
+    return {isStarted(), getPercent(), getBytesTotal(), getBytesDownloaded(), getCurrentSpeed(), getAverageSpeed(), getElapsedTime()};
+}
+
 QString Downloader::getSpeedFormatted(quint64 speed_bytes_per_sec)
 {
     float speed = speed_bytes_per_sec;
@@ -201,9 +211,13 @@ void Downloader::onDownloadProgressChanged(qint64 bytesReceived, qint64 bytesTot
 
     if (downloadTime.elapsed() - timeElapsedBeforeSecond >= 300) {
         download_speed = (bytes_downloaded - bytesReceivedBeforeSecond) * 1000.0 / (downloadTime.elapsed() - timeElapsedBeforeSecond);
+
+        average_speed = (average_speed * speed_update_ticks + download_speed) / (speed_update_ticks + 1);
+        ++speed_update_ticks;
+
         timeElapsedBeforeSecond = downloadTime.elapsed();
         bytesReceivedBeforeSecond = bytes_downloaded;
-        emit progressChanged(this);
+        emit progressChanged(this, getDownloadStatus());
     }
 }
 

+ 31 - 2
src/Legacy/models/downloader.h

@@ -14,6 +14,29 @@ class Downloader : public QObject
     Q_OBJECT
 
 public:
+    struct Status {
+        bool started;
+        double percent;
+
+        quint64 total_bytes;
+        quint64 downloaded_bytes;
+        quint64 current_speed;
+        quint64 average_speed;
+        quint64 elapsed_time;
+    };
+
+    Status operator+(const Status& a, const Status &b) {
+        Status result = {
+            a.started || b.started,
+            (a.percent + b.percent) / 2.0,
+            a.total_bytes + b.total_bytes,
+            a.downloaded_bytes + b.downloaded_bytes,
+            a.average_speed + b.average_speed,
+            qMax(a.elapsed_time, b.elapsed_time)
+        };
+        return result;
+    }
+
     explicit Downloader(QObject *parent = 0);
 private:
     Q_DISABLE_COPY(Downloader)
@@ -30,14 +53,18 @@ public:
     quint64 getBytesTotal();
     quint64 getBytesDownloaded();
     quint64 getElapsedTime();
-    quint64 getSpeed();
+    quint64 getCurrentSpeed();
+    quint64 getAverageSpeed();
+
+    Status getDownloadStatus();
+
     static QString getSizeFormatted(quint64 bytes);
     static QString getSpeedFormatted(quint64 speed_bytes_per_sec);
     static QString getElapsedTimeFormatted(quint64 elapsed_time_secs);
 
 signals:
     void downloadFinished(Downloader* this_downloader_ptr);
-    void progressChanged(Downloader* this_downloader_ptr);
+    void progressChanged(Downloader* this_downloader_ptr, Status status);
 
 public slots:
     void start();
@@ -56,6 +83,8 @@ public:
 private:
     QTime downloadTime;
     quint64 download_speed; // bytes per second
+    quint64 average_speed;
+    quint64 speed_update_ticks;
 
     quint64 bytes_total;
     quint64 bytes_downloaded;