From d667071e0f4d39b6cb095d7a73b2ba1302e3e9be Mon Sep 17 00:00:00 2001
From: Akira Ohgaki <akiraohgaki@gmail.com>
Date: Wed, 20 Dec 2017 01:04:55 +0900
Subject: [PATCH] Check updater progress

---
 app/src/handlers/updatehandler.cpp   | 12 +++++-
 app/src/updaters/appimageupdater.cpp | 55 +++++++++++++++++-----------
 app/src/updaters/appimageupdater.h   | 15 +++++++-
 3 files changed, 57 insertions(+), 25 deletions(-)

diff --git a/app/src/handlers/updatehandler.cpp b/app/src/handlers/updatehandler.cpp
index d1e11c2..b113253 100644
--- a/app/src/handlers/updatehandler.cpp
+++ b/app/src/handlers/updatehandler.cpp
@@ -4,6 +4,7 @@
 #include <QJsonValue>
 #include <QJsonArray>
 #include <QDateTime>
+//#include <QThread>
 
 #include "qtlib_file.h"
 
@@ -106,6 +107,7 @@ void UpdateHandler::appImageUpdaterFinished(AppImageUpdater *updater)
 
     if (!updater->isFinishedWithNoError()) {
         emit updateFinished(itemKey, false);
+        updater->deleteLater();
         return;
     }
 
@@ -129,6 +131,7 @@ void UpdateHandler::appImageUpdaterFinished(AppImageUpdater *updater)
     configHandler_->removeUsrConfigUpdateAvailableItemsItem(itemKey);
 
     emit updateFinished(itemKey, true);
+    updater->deleteLater();
 }
 #endif
 
@@ -147,11 +150,17 @@ void UpdateHandler::updateAppImage(const QString &itemKey)
     auto installedItem = installedItems[installedItemKey].toObject();
     auto filename = installedItem["filename"].toString();
     auto installType = installedItem["install_type"].toString();
-
     auto filePath = configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename;
+
+    //auto *thread = new QThread(this);
     auto *updater = new AppImageUpdater(itemKey, filePath, this);
+    //updater->moveToThread(thread);
+
+    //connect(thread, &QThread::started, updater, &AppImageUpdater::updateAppImage);
     connect(updater, &AppImageUpdater::updateProgress, this, &UpdateHandler::updateProgress);
     connect(updater, &AppImageUpdater::finished, this, &UpdateHandler::appImageUpdaterFinished);
+    //connect(updater, &AppImageUpdater::finished, thread, &QThread::quit);
+    //connect(thread, &QThread::finished, thread, &QThread::deleteLater);
 
     QJsonObject metadata;
     metadata["installed_item_key"] = installedItemKey;
@@ -170,5 +179,6 @@ void UpdateHandler::updateAppImage(const QString &itemKey)
 
     emit updateStarted(itemKey, true);
     updater->updateAppImage();
+    //thread->start();
 }
 #endif
diff --git a/app/src/updaters/appimageupdater.cpp b/app/src/updaters/appimageupdater.cpp
index c246fb5..de2e8b2 100644
--- a/app/src/updaters/appimageupdater.cpp
+++ b/app/src/updaters/appimageupdater.cpp
@@ -1,6 +1,6 @@
 #include "appimageupdater.h"
 
-#include <QThread>
+#include <QTimer>
 
 #include "appimage/update.h"
 
@@ -9,6 +9,12 @@ AppImageUpdater::AppImageUpdater(const QString &id, const QString &path, QObject
 {
     isFinishedWithNoError_ = false;
     errorString_ = "";
+    updater_ = new appimage::update::Updater(path_.toStdString(), false);
+}
+
+AppImageUpdater::~AppImageUpdater()
+{
+    delete updater_;
 }
 
 QString AppImageUpdater::id() const
@@ -21,19 +27,27 @@ QString AppImageUpdater::path() const
     return path_;
 }
 
+bool AppImageUpdater::isFinishedWithNoError() const
+{
+    return isFinishedWithNoError_;
+}
+
+QString AppImageUpdater::errorString() const
+{
+    return errorString_;
+}
+
 QString AppImageUpdater::describeAppImage() const
 {
     std::string description = "";
-    appimage::update::Updater updater(path_.toStdString());
-    updater.describeAppImage(description);
+    updater_.describeAppImage(description);
     return QString::fromStdString(description);
 }
 
 bool AppImageUpdater::checkAppImage() const
 {
     bool updateAvailable = false;
-    appimage::update::Updater updater(path_.toStdString());
-    updater.checkForChanges(updateAvailable);
+    updater_.checkForChanges(updateAvailable);
     return updateAvailable;
 }
 
@@ -41,24 +55,31 @@ void AppImageUpdater::updateAppImage()
 {
     isFinishedWithNoError_ = false;
     errorString_ = "";
-    appimage::update::Updater updater(path_.toStdString(), false);
 
-    if (!updater.start()) {
+    if (!updater_.start()) {
         emit finished(this);
         return;
     }
 
-    while (!updater.isDone()) {
-        QThread::msleep(100);
+    auto timer = new QTimer(this);
+    connect(timer, &QTimer::timeout, this, &AppImageUpdater::checkUpdaterProgress);
+    connect(this, &AppImageUpdater::finished, timer, &QTimer::stop);
+    timer->start(100);
+}
+
+void AppImageUpdater::checkUpdaterProgress()
+{
+    if (!updater_->isDone()) {
         double progress;
-        if (updater.progress(progress)) {
+        if (updater_.progress(progress)) {
             emit updateProgress(id_, progress);
         }
+        return;
     }
 
-    if (updater.hasError()) {
+    if (updater_.hasError()) {
         std::string message;
-        while (updater.nextStatusMessage(message)) {
+        while (updater_.nextStatusMessage(message)) {
             errorString_ += QString::fromStdString(message) + "\n";
         }
         emit finished(this);
@@ -68,13 +89,3 @@ void AppImageUpdater::updateAppImage()
     isFinishedWithNoError_ = true;
     emit finished(this);
 }
-
-bool AppImageUpdater::isFinishedWithNoError() const
-{
-    return isFinishedWithNoError_;
-}
-
-QString AppImageUpdater::errorString() const
-{
-    return errorString_;
-}
diff --git a/app/src/updaters/appimageupdater.h b/app/src/updaters/appimageupdater.h
index d83ab71..03477f7 100644
--- a/app/src/updaters/appimageupdater.h
+++ b/app/src/updaters/appimageupdater.h
@@ -2,29 +2,40 @@
 
 #include <QObject>
 
+namespace appimage {
+namespace update {
+class Updater;
+}
+}
+
 class AppImageUpdater : public QObject
 {
     Q_OBJECT
 
 public:
     explicit AppImageUpdater(const QString &id, const QString &path, QObject *parent = nullptr);
+    ~AppImageUpdater();
 
     QString id() const;
     QString path() const;
+    bool isFinishedWithNoError() const;
+    QString errorString() const;
 
     QString describeAppImage() const;
     bool checkAppImage() const;
     void updateAppImage();
-    bool isFinishedWithNoError() const;
-    QString errorString() const;
 
 signals:
     void finished(AppImageUpdater *updater);
     void updateProgress(QString id, double progress);
 
+private slots:
+    void checkUpdaterProgress();
+
 private:
     QString id_;
     QString path_;
     bool isFinishedWithNoError_;
     QString errorString_;
+    appimage::update::Updater *updater_;
 };
-- 
GitLab