diff --git a/app/src/handlers/confighandler.cpp b/app/src/handlers/confighandler.cpp index c4043f302011b23734f0f98f0c9598e20d4358e5..6ccbf7d0e9dbbd16f819b744feaf24a1b0eb2019 100644 --- a/app/src/handlers/confighandler.cpp +++ b/app/src/handlers/confighandler.cpp @@ -69,14 +69,14 @@ bool ConfigHandler::setUsrConfigInstalledItems(const QJsonObject &object) const return usrConfig_.set("installed_items", object); } -QJsonObject ConfigHandler::getUsrConfigUpdateAvailable() const +QJsonObject ConfigHandler::getUsrConfigUpdateAvailableItems() const { - return usrConfig_.get("update_available"); + return usrConfig_.get("update_available_items"); } -bool ConfigHandler::setUsrConfigUpdateAvailable(const QJsonObject &object) const +bool ConfigHandler::setUsrConfigUpdateAvailableItems(const QJsonObject &object) const { - return usrConfig_.set("update_available", object); + return usrConfig_.set("update_available_items", object); } bool ConfigHandler::setUsrConfigProvidersProvider(const QString &providerKey, const QJsonObject &object) const @@ -177,26 +177,24 @@ bool ConfigHandler::removeUsrConfigInstalledItemsItem(const QString &itemKey) co return setUsrConfigInstalledItems(installedItems); } -bool ConfigHandler::setUsrConfigUpdateAvailableFile(const QString &fileKey, const QJsonObject &object) const +bool ConfigHandler::setUsrConfigUpdateAvailableItemsItem(const QString &itemKey, const QJsonObject &object) const { /* object format { - "path": "/home/user/.local/bin/example.AppImage", - "filename": "example.AppImage", "installed_item": "http://example.com/downloads/example.AppImage", "update_method": "appimageupdate" } */ - auto updateAvailable = getUsrConfigUpdateAvailable(); - updateAvailable[fileKey] = object; - return setUsrConfigUpdateAvailable(updateAvailable); + auto updateAvailableItems = getUsrConfigUpdateAvailableItems(); + updateAvailableItems[itemKey] = object; + return setUsrConfigUpdateAvailableItems(updateAvailableItems); } -bool ConfigHandler::removeUsrConfigUpdateAvailableFile(const QString &fileKey) const +bool ConfigHandler::removeUsrConfigUpdateAvailableItemsItem(const QString &itemKey) const { - auto updateAvailable = getUsrConfigUpdateAvailable(); - updateAvailable.remove(fileKey); - return setUsrConfigUpdateAvailable(updateAvailable); + auto updateAvailableItems = getUsrConfigUpdateAvailableItems(); + updateAvailableItems.remove(itemKey); + return setUsrConfigUpdateAvailableItems(updateAvailableItems); } void ConfigHandler::importAppConfigApplication() diff --git a/app/src/handlers/confighandler.h b/app/src/handlers/confighandler.h index 76aa0e4cac698760f502d54c1952e347ff344912..a1531ffb94c8ce5387b18c2dc541a51818782c71 100644 --- a/app/src/handlers/confighandler.h +++ b/app/src/handlers/confighandler.h @@ -24,8 +24,8 @@ public slots: bool setUsrConfigCategories(const QJsonObject &object) const; QJsonObject getUsrConfigInstalledItems() const; bool setUsrConfigInstalledItems(const QJsonObject &object) const; - QJsonObject getUsrConfigUpdateAvailable() const; - bool setUsrConfigUpdateAvailable(const QJsonObject &object) const; + QJsonObject getUsrConfigUpdateAvailableItems() const; + bool setUsrConfigUpdateAvailableItems(const QJsonObject &object) const; bool setUsrConfigProvidersProvider(const QString &providerKey, const QJsonObject &object) const; bool removeUsrConfigProvidersProvider(const QString &providerKey) const; @@ -34,8 +34,8 @@ public slots: bool setUsrConfigCategoriesInstallType(const QString &providerKey, const QString &categoryKey, const QString &installType) const; bool setUsrConfigInstalledItemsItem(const QString &itemKey, const QJsonObject &object) const; bool removeUsrConfigInstalledItemsItem(const QString &itemKey) const; - bool setUsrConfigUpdateAvailableFile(const QString &fileKey, const QJsonObject &object) const; - bool removeUsrConfigUpdateAvailableFile(const QString &fileKey) const; + bool setUsrConfigUpdateAvailableItemsItem(const QString &itemKey, const QJsonObject &object) const; + bool removeUsrConfigUpdateAvailableItemsItem(const QString &itemKey) const; private: void importAppConfigApplication(); diff --git a/app/src/handlers/updatehandler.cpp b/app/src/handlers/updatehandler.cpp index 28c4114de4981bec37ecedd67b138f28f0bac8eb..fda87f0f83985a5fdbd84bd5763646b2dc9dc05c 100644 --- a/app/src/handlers/updatehandler.cpp +++ b/app/src/handlers/updatehandler.cpp @@ -24,76 +24,67 @@ void UpdateHandler::checkAll() { emit checkAllStarted(); - auto application = configHandler_->getUsrConfigApplication(); + QJsonObject updateAvailableItems; + configHandler_->setUsrConfigUpdateAvailableItems(updateAvailableItems); // Resets data + auto installedItems = configHandler_->getUsrConfigInstalledItems(); - 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; - } + if (installedItems.isEmpty()) { + emit checkAllFinished(); + return; } - // Clear data - QJsonObject updateAvailable; - configHandler_->setUsrConfigUpdateAvailable(updateAvailable); - for (const auto &itemKey : installedItems.keys()) { auto installedItem = installedItems[itemKey].toObject(); + auto filename = installedItem["filename"].toString(); auto installType = installedItem["install_type"].toString(); - QString destDir = ""; + qtlib::File file; #ifdef QTLIB_UNIX - destDir = configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString(); + file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename); #else - destDir = configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString(); + file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString() + "/" + filename); #endif - if (installType == "bin") { - for (const auto &filenameValue : installedItem["files"].toArray()) { - auto filename = filenameValue.toString(); - QString path = destDir + "/" + filename; - // Use file path as unique key for entry in update_available data - auto fileKey = path; - QJsonObject updateAvailableFile; + QJsonObject updateAvailableItem; + if (installType == "bin") { #ifdef QTLIB_UNIX - if (filename.endsWith(".appimage", Qt::CaseInsensitive)) { - if (checkAppImage(path)) { - updateAvailableFile["path"] = path; - updateAvailableFile["filename"] = filename; - updateAvailableFile["installed_item"] = itemKey; - updateAvailableFile["update_method"] = QString("appimageupdate"); - configHandler_->setUsrConfigUpdateAvailableFile(fileKey, updateAvailableFile); - } - //else if (checkAppImageWithOcsApi(itemKey, filename)) {} + if (file.path().endsWith(".appimage", Qt::CaseInsensitive)) { + if (checkAppImage(file.path())) { + updateAvailableItem["installed_item"] = itemKey; + updateAvailableItem["update_method"] = QString("appimageupdate"); + configHandler_->setUsrConfigUpdateAvailableItemsItem(itemKey, updateAvailableItem); } -#endif + //else if (checkAppImageWithOcsApi(itemKey)) {} } +#endif } } + auto application = configHandler_->getUsrConfigApplication(); application["update_checked_at"] = QDateTime::currentMSecsSinceEpoch(); configHandler_->setUsrConfigApplication(application); emit checkAllFinished(); } -void UpdateHandler::update(const QString &fileKey) +void UpdateHandler::update(const QString &itemKey) { - if (configHandler_->getUsrConfigUpdateAvailable().contains(fileKey)) { - auto updateMethod = configHandler_->getUsrConfigUpdateAvailable()[fileKey].toObject()["update_method"].toString(); + auto updateAvailableItems = configHandler_->getUsrConfigUpdateAvailableItems(); + + if (!updateAvailableItems.contains(itemKey)) { + return; + } + + auto updateMethod = updateAvailableItems[itemKey].toObject()["update_method"].toString(); #ifdef QTLIB_UNIX - if (updateMethod == "appimageupdate") { - updateAppImage(fileKey); - } - //else if (updateMethod == "appimageupdatewithocsapi") { - //} -#endif + if (updateMethod == "appimageupdate") { + updateAppImage(itemKey); } + //else if (updateMethod == "appimageupdatewithocsapi") {} +#endif } #ifdef QTLIB_UNIX @@ -118,16 +109,25 @@ bool UpdateHandler::checkAppImage(const QString &path) const return false; } -void UpdateHandler::updateAppImage(const QString &fileKey) +void UpdateHandler::updateAppImage(const QString &itemKey) { - auto updateAvailableFile = configHandler_->getUsrConfigUpdateAvailable()[fileKey].toObject(); + auto installedItems = configHandler_->getUsrConfigInstalledItems(); + auto updateAvailableItem = configHandler_->getUsrConfigUpdateAvailableItems()[itemKey].toObject(); + auto installedItemKey = updateAvailableItem["installed_item"].toString(); + + if (!installedItems.contains(installedItemKey)) { + return; + } + + auto installedItem = installedItems[installedItemKey].toObject(); + + auto filename = installedItem["filename"].toString(); + auto installType = installedItem["install_type"].toString(); - auto path = updateAvailableFile["path"].toString(); - auto filename = updateAvailableFile["filename"].toString(); - auto itemKey = updateAvailableFile["installed_item"].toString(); + qtlib::File file(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename); auto newFilename = filename; - auto updateInformation = describeAppImage(path); + auto updateInformation = describeAppImage(file.path()); for (const auto &info : updateInformation.split("\n")) { if (info.endsWith(".zsync", Qt::CaseInsensitive)) { newFilename = info.split("|").last().split("/").last().replace(".zsync", "", Qt::CaseInsensitive); @@ -135,48 +135,45 @@ void UpdateHandler::updateAppImage(const QString &fileKey) } } - appimage::update::Updater appImageUpdater(path.toStdString(), false); - if (appImageUpdater.start()) { - emit updateStarted(fileKey); + appimage::update::Updater appImageUpdater(file.path().toStdString(), false); + if (!appImageUpdater.start()) { + return; + } - while (!appImageUpdater.isDone()) { - QThread::msleep(100); - double progress; - if (appImageUpdater.progress(progress)) { - emit updateProgress(fileKey, progress * 100); - } - } + emit updateStarted(itemKey); - if (appImageUpdater.hasError()) { - std::string nextMessage; - while (appImageUpdater.nextStatusMessage(nextMessage)) { - qWarning() << QString::fromStdString(nextMessage); - } + while (!appImageUpdater.isDone()) { + QThread::msleep(100); + double progress; + if (appImageUpdater.progress(progress)) { + emit updateProgress(itemKey, progress * 100); + } + } - emit updateFinished(fileKey); - return; + if (appImageUpdater.hasError()) { + std::string nextMessage; + while (appImageUpdater.nextStatusMessage(nextMessage)) { + qWarning() << QString::fromStdString(nextMessage); } - configHandler_->removeUsrConfigUpdateAvailableFile(fileKey); + emit updateFinished(itemKey); + return; + } - if (newFilename != filename) { - auto installedItem = configHandler_->getUsrConfigInstalledItems()[itemKey].toObject(); - QJsonArray files; - for (const auto &file : installedItem["files"].toArray()) { - if (file.toString() == filename) { - files.append(QJsonValue(newFilename)); - } - else { - files.append(file); - } - } - installedItem["files"] = files; - installedItem["installed_at"] = QDateTime::currentMSecsSinceEpoch(); - configHandler_->setUsrConfigInstalledItemsItem(itemKey, installedItem); - qtlib::File(path).remove(); - } + installedItem["filename"] = newFilename; + QJsonArray files; + files.append(QJsonValue(newFilename)); + installedItem["files"] = files; + installedItem["installed_at"] = QDateTime::currentMSecsSinceEpoch(); + + configHandler_->setUsrConfigInstalledItemsItem(installedItemKey, installedItem); - emit updateFinished(fileKey); + if (newFilename != filename) { + file.remove(); } + + configHandler_->removeUsrConfigUpdateAvailableItemsItem(itemKey); + + emit updateFinished(itemKey); } #endif diff --git a/app/src/handlers/updatehandler.h b/app/src/handlers/updatehandler.h index 49dcf29c3137f28d856ea02c614479a536ab5eee..7c6e851a4275bcfce8a8324c6e977398c9a148bb 100644 --- a/app/src/handlers/updatehandler.h +++ b/app/src/handlers/updatehandler.h @@ -14,19 +14,19 @@ public: signals: void checkAllStarted(); void checkAllFinished(); - void updateStarted(QString fileKey); - void updateFinished(QString fileKey); - void updateProgress(QString fileKey, int progress); + void updateStarted(QString itemKey); + void updateFinished(QString itemKey); + void updateProgress(QString itemKey, int progress); public slots: void checkAll(); - void update(const QString &fileKey); + void update(const QString &itemKey); private: #ifdef QTLIB_UNIX QString describeAppImage(const QString &path) const; bool checkAppImage(const QString &path) const; - void updateAppImage(const QString &fileKey); + void updateAppImage(const QString &itemKey); #endif ConfigHandler *configHandler_; diff --git a/app/src/websockets/websocketserver.cpp b/app/src/websockets/websocketserver.cpp index 5917ed39ece5728f67c47d702e491ccd3c9118c0..0380f6f7f6789b58e71040282a3e81e74f33ac2f 100644 --- a/app/src/websockets/websocketserver.cpp +++ b/app/src/websockets/websocketserver.cpp @@ -211,24 +211,24 @@ void WebSocketServer::updateCheckAllFinished() sendMessage("", "UpdateHandler::checkAllFinished", data); } -void WebSocketServer::updateUpdateStarted(QString fileKey) +void WebSocketServer::updateUpdateStarted(QString itemKey) { QJsonArray data; - data.append(fileKey); + data.append(itemKey); sendMessage("", "UpdateHandler::updateStarted", data); } -void WebSocketServer::updateUpdateFinished(QString fileKey) +void WebSocketServer::updateUpdateFinished(QString itemKey) { QJsonArray data; - data.append(fileKey); + data.append(itemKey); sendMessage("", "UpdateHandler::updateFinished", data); } -void WebSocketServer::updateUpdateProgress(QString fileKey, int progress) +void WebSocketServer::updateUpdateProgress(QString itemKey, int progress) { QJsonArray data; - data.append(fileKey); + data.append(itemKey); data.append(progress); sendMessage("", "UpdateHandler::updateProgress", data); } @@ -289,11 +289,11 @@ void WebSocketServer::receiveMessage(const QString &id, const QString &func, con else if (func == "ConfigHandler::setUsrConfigInstalledItems") { resultData.append(configHandler_->setUsrConfigInstalledItems(data.at(0).toObject())); } - else if (func == "ConfigHandler::getUsrConfigUpdateAvailable") { - resultData.append(configHandler_->getUsrConfigUpdateAvailable()); + else if (func == "ConfigHandler::getUsrConfigUpdateAvailableItems") { + resultData.append(configHandler_->getUsrConfigUpdateAvailableItems()); } - else if (func == "ConfigHandler::setUsrConfigUpdateAvailable") { - resultData.append(configHandler_->setUsrConfigUpdateAvailable(data.at(0).toObject())); + else if (func == "ConfigHandler::setUsrConfigUpdateAvailableItems") { + resultData.append(configHandler_->setUsrConfigUpdateAvailableItems(data.at(0).toObject())); } else if (func == "ConfigHandler::setUsrConfigProvidersProvider") { resultData.append(configHandler_->setUsrConfigProvidersProvider(data.at(0).toString(), data.at(1).toObject())); @@ -316,11 +316,11 @@ void WebSocketServer::receiveMessage(const QString &id, const QString &func, con else if (func == "ConfigHandler::removeUsrConfigInstalledItemsItem") { resultData.append(configHandler_->removeUsrConfigInstalledItemsItem(data.at(0).toString())); } - else if (func == "ConfigHandler::setUsrConfigUpdateAvailableFile") { - resultData.append(configHandler_->setUsrConfigUpdateAvailableFile(data.at(0).toString(), data.at(1).toObject())); + else if (func == "ConfigHandler::setUsrConfigUpdateAvailableItemsItem") { + resultData.append(configHandler_->setUsrConfigUpdateAvailableItemsItem(data.at(0).toString(), data.at(1).toObject())); } - else if (func == "ConfigHandler::removeUsrConfigUpdateAvailableFile") { - resultData.append(configHandler_->removeUsrConfigUpdateAvailableFile(data.at(0).toString())); + else if (func == "ConfigHandler::removeUsrConfigUpdateAvailableItemsItem") { + resultData.append(configHandler_->removeUsrConfigUpdateAvailableItemsItem(data.at(0).toString())); } // SystemHandler else if (func == "SystemHandler::isUnix") { diff --git a/app/src/websockets/websocketserver.h b/app/src/websockets/websocketserver.h index e4b9ba63ba038b137640d66f6323ab54db1ae49e..d017743bc2bf5737ba784e8cba81a641f332be09 100644 --- a/app/src/websockets/websocketserver.h +++ b/app/src/websockets/websocketserver.h @@ -53,9 +53,9 @@ private slots: void updateCheckAllStarted(); void updateCheckAllFinished(); - void updateUpdateStarted(QString fileKey); - void updateUpdateFinished(QString fileKey); - void updateUpdateProgress(QString fileKey, int progress); + void updateUpdateStarted(QString itemKey); + void updateUpdateFinished(QString itemKey); + void updateUpdateProgress(QString itemKey, int progress); private: void receiveMessage(const QString &id, const QString &func, const QJsonArray &data);