From fba624a3e2928d5629f01f2b11ebee0d9dee9192 Mon Sep 17 00:00:00 2001
From: Akira Ohgaki <akiraohgaki@gmail.com>
Date: Wed, 29 Nov 2017 19:30:27 +0900
Subject: [PATCH] Fix

---
 app/src/handlers/updatehandler.cpp     | 33 ++++++++++++++++----------
 app/src/main.cpp                       |  2 +-
 app/src/websockets/websocketserver.cpp |  4 ++--
 3 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/app/src/handlers/updatehandler.cpp b/app/src/handlers/updatehandler.cpp
index d752bc8..8d62299 100644
--- a/app/src/handlers/updatehandler.cpp
+++ b/app/src/handlers/updatehandler.cpp
@@ -5,11 +5,10 @@
 #include <QJsonArray>
 #include <QJsonObject>
 #include <QDateTime>
+#include <QThread>
+#include <QDebug>
 
 #ifdef QTLIB_UNIX
-#include <thread>
-#include <chrono>
-
 #include "appimage/update.h"
 #endif
 
@@ -28,11 +27,13 @@ void UpdateHandler::checkAll()
     auto application = configHandler_->getUsrConfigApplication();
     auto installedItems = configHandler_->getUsrConfigInstalledItems();
 
-    if (installedItems.isEmpty()
-            || (application.contains("update_checked_at")
-                && application["update_checked_at"].toInt() + (1000 * 60 * 60 * 24) > QDateTime::currentMSecsSinceEpoch())) {
-        emit checkAllFinished();
-        return;
+    if (installedItems.isEmpty() || application.contains("update_checked_at")) {
+        auto currentDate = QDateTime::currentDateTime();
+        auto checkedDate = QDateTime::fromMSecsSinceEpoch(application["update_checked_at"].toInt());
+        if (currentDate.daysTo(checkedDate.addDays(1)) <= 0) {
+            emit checkAllFinished();
+            return;
+        }
     }
 
     // Clear data
@@ -83,12 +84,14 @@ void UpdateHandler::checkAll()
 void UpdateHandler::update(const QString &fileKey)
 {
     if (configHandler_->getUsrConfigUpdateAvailable().contains(fileKey)) {
-        auto updateAvailableFile = configHandler_->getUsrConfigUpdateAvailable()[fileKey].toObject();
+        auto updateMethod = configHandler_->getUsrConfigUpdateAvailable()[fileKey].toObject()["update_method"].toString();
 
 #ifdef QTLIB_UNIX
-        if (updateAvailableFile["update_method"].toString() == "appimageupdate") {
+        if (updateMethod == "appimageupdate") {
             updateAppImage(fileKey);
         }
+        //else if (updateMethod == "appimageupdatewithocsapi") {
+        //}
 #endif
     }
 }
@@ -127,7 +130,7 @@ void UpdateHandler::updateAppImage(const QString &fileKey)
     auto updateInformation = describeAppImage(path);
     for (const auto &info : updateInformation.split("\n")) {
         if (info.endsWith(".zsync", Qt::CaseInsensitive)) {
-            newFilename = info.replace(".zsync", "", Qt::CaseInsensitive).split("/").last();
+            newFilename = info.split("|").last().split("/").last().replace(".zsync", "", Qt::CaseInsensitive);
             break;
         }
     }
@@ -137,7 +140,7 @@ void UpdateHandler::updateAppImage(const QString &fileKey)
         emit updateStarted(fileKey);
 
         while (!appImageUpdater.isDone()) {
-            std::this_thread::sleep_for(std::chrono::milliseconds(100));
+            QThread::msleep(100);
             double progress;
             if (appImageUpdater.progress(progress)) {
                 emit updateProgress(fileKey, progress * 100);
@@ -163,6 +166,12 @@ void UpdateHandler::updateAppImage(const QString &fileKey)
 
             configHandler_->removeUsrConfigUpdateAvailableFile(fileKey);
         }
+        else {
+            std::string nextMessage;
+            while (appImageUpdater.nextStatusMessage(nextMessage)) {
+                qWarning() << QString::fromStdString(nextMessage);
+            }
+        }
 
         emit updateFinished(fileKey);
     }
diff --git a/app/src/main.cpp b/app/src/main.cpp
index 6bb8c55..8dca232 100644
--- a/app/src/main.cpp
+++ b/app/src/main.cpp
@@ -51,7 +51,7 @@ int main(int argc, char *argv[])
     QObject::connect(wsServer, &WebSocketServer::stopped, &app, &QGuiApplication::quit);
 
     if (wsServer->start()) {
-        qDebug() << "Websocket server started at:" << wsServer->serverUrl().toString();
+        qInfo() << "Websocket server started at:" << wsServer->serverUrl().toString();
     }
     else {
         qCritical() << "Failed to start websocket server:" << wsServer->errorString();
diff --git a/app/src/websockets/websocketserver.cpp b/app/src/websockets/websocketserver.cpp
index 85732e3..6a135b1 100644
--- a/app/src/websockets/websocketserver.cpp
+++ b/app/src/websockets/websocketserver.cpp
@@ -202,13 +202,13 @@ void WebSocketServer::itemUninstallFinished(QJsonObject result)
 void WebSocketServer::updateCheckAllStarted()
 {
     QJsonArray data;
-    sendMessage("", "ItemHandler::checkAllStarted", data);
+    sendMessage("", "UpdateHandler::checkAllStarted", data);
 }
 
 void WebSocketServer::updateCheckAllFinished()
 {
     QJsonArray data;
-    sendMessage("", "ItemHandler::checkAllFinished", data);
+    sendMessage("", "UpdateHandler::checkAllFinished", data);
 }
 
 void WebSocketServer::updateUpdateStarted(QString fileKey)
-- 
GitLab