Skip to content
Snippets Groups Projects
Commit d0621900 authored by akiraohgaki's avatar akiraohgaki
Browse files

Use AppImageUpdater class

parent a4cbbc99
No related branches found
No related tags found
No related merge requests found
...@@ -3,27 +3,22 @@ ...@@ -3,27 +3,22 @@
#include <QStringList> #include <QStringList>
#include <QJsonValue> #include <QJsonValue>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonObject>
#include <QDateTime> #include <QDateTime>
#include <QThread>
#include <QDebug>
#ifdef QTLIB_UNIX
#include "appimage/update.h"
#endif
#include "qtlib_file.h" #include "qtlib_file.h"
#include "handlers/confighandler.h" #include "handlers/confighandler.h"
#ifdef QTLIB_UNIX
#include "updaters/appimageupdater.h"
#endif
UpdateHandler::UpdateHandler(ConfigHandler *configHandler, QObject *parent) UpdateHandler::UpdateHandler(ConfigHandler *configHandler, QObject *parent)
: QObject(parent), configHandler_(configHandler) : QObject(parent), configHandler_(configHandler)
{} {}
void UpdateHandler::checkAll() void UpdateHandler::checkAll()
{ {
emit checkAllStarted();
// Resets data // Resets data
QJsonObject updateAvailableItems; QJsonObject updateAvailableItems;
configHandler_->setUsrConfigUpdateAvailableItems(updateAvailableItems); configHandler_->setUsrConfigUpdateAvailableItems(updateAvailableItems);
...@@ -31,32 +26,34 @@ void UpdateHandler::checkAll() ...@@ -31,32 +26,34 @@ void UpdateHandler::checkAll()
auto installedItems = configHandler_->getUsrConfigInstalledItems(); auto installedItems = configHandler_->getUsrConfigInstalledItems();
if (installedItems.isEmpty()) { if (installedItems.isEmpty()) {
emit checkAllFinished(); emit checkAllStarted(false);
return; return;
} }
emit checkAllStarted(true);
for (const auto &itemKey : installedItems.keys()) { for (const auto &itemKey : installedItems.keys()) {
auto installedItem = installedItems[itemKey].toObject(); auto installedItem = installedItems[itemKey].toObject();
auto filename = installedItem["filename"].toString(); auto filename = installedItem["filename"].toString();
auto installType = installedItem["install_type"].toString(); auto installType = installedItem["install_type"].toString();
qtlib::File file; QString filePath = "";
#ifdef QTLIB_UNIX #ifdef QTLIB_UNIX
file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename); filePath = configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename;
#else #else
file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString() + "/" + filename); filePath = configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString() + "/" + filename;
#endif #endif
QString updateMethod = ""; QString updateMethod = "";
if (installType == "bin") { if (installType == "bin") {
#ifdef QTLIB_UNIX #ifdef QTLIB_UNIX
if (file.path().endsWith(".appimage", Qt::CaseInsensitive)) { if (filePath.endsWith(".appimage", Qt::CaseInsensitive)) {
if (checkAppImage(file.path())) { if (AppImageUpdater(itemKey, filePath).checkAppImage()) {
updateMethod = "appimageupdate"; updateMethod = "appimageupdate";
} }
//else if (checkAppImageWithOcsApi(itemKey)) { //else if (OcsFileUpdater(url).checkFile()) {
// updateMethod = "appimageupdatewithocsapi"; // updateMethod = "appimageupdate_ocs";
//} //}
} }
#endif #endif
...@@ -75,7 +72,7 @@ void UpdateHandler::checkAll() ...@@ -75,7 +72,7 @@ void UpdateHandler::checkAll()
application["update_checked_at"] = QDateTime::currentMSecsSinceEpoch(); application["update_checked_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigApplication(application); configHandler_->setUsrConfigApplication(application);
emit checkAllFinished(); emit checkAllFinished(true);
} }
void UpdateHandler::update(const QString &itemKey) void UpdateHandler::update(const QString &itemKey)
...@@ -83,6 +80,7 @@ void UpdateHandler::update(const QString &itemKey) ...@@ -83,6 +80,7 @@ void UpdateHandler::update(const QString &itemKey)
auto updateAvailableItems = configHandler_->getUsrConfigUpdateAvailableItems(); auto updateAvailableItems = configHandler_->getUsrConfigUpdateAvailableItems();
if (!updateAvailableItems.contains(itemKey)) { if (!updateAvailableItems.contains(itemKey)) {
emit updateStarted(itemKey, false);
return; return;
} }
...@@ -92,34 +90,49 @@ void UpdateHandler::update(const QString &itemKey) ...@@ -92,34 +90,49 @@ void UpdateHandler::update(const QString &itemKey)
if (updateMethod == "appimageupdate") { if (updateMethod == "appimageupdate") {
updateAppImage(itemKey); updateAppImage(itemKey);
} }
//else if (updateMethod == "appimageupdatewithocsapi") { //else if (updateMethod == "appimageupdate_ocs") {
// updateAppImageWithOcsApi(itemKey); // updateAppImageOcs(itemKey);
//} //}
#endif #endif
} }
#ifdef QTLIB_UNIX #ifdef QTLIB_UNIX
QString UpdateHandler::describeAppImage(const QString &path) const void UpdateHandler::appImageUpdaterFinished(AppImageUpdater *updater)
{ {
appimage::update::Updater appImageUpdater(path.toStdString()); auto itemKey = updater->id();
QString updateInformation;
std::string description; auto metadata = metadataSet_[itemKey].toObject();
if (appImageUpdater.describeAppImage(description)) { metadataSet_.remove(itemKey);
updateInformation = QString::fromStdString(description);
if (!updater->isFinishedWithNoError()) {
emit updateFinished(itemKey, false);
return;
} }
return updateInformation;
}
bool UpdateHandler::checkAppImage(const QString &path) const auto installedItemKey = metadata["installed_item_key"].toString();
{ auto installedItem = metadata["installed_item_obj"].toObject();
appimage::update::Updater appImageUpdater(path.toStdString()); auto newFilename = metadata["new_filename"].toString();
bool updateAvailable; auto filename = installedItem["filename"].toString();
if (appImageUpdater.checkForChanges(updateAvailable)) {
return updateAvailable; installedItem["filename"] = newFilename;
QJsonArray files;
files.append(QJsonValue(newFilename));
installedItem["files"] = files;
installedItem["installed_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigInstalledItemsItem(installedItemKey, installedItem);
if (newFilename != filename) {
qtlib::File(updater->path()).remove();
} }
return false;
configHandler_->removeUsrConfigUpdateAvailableItemsItem(itemKey);
emit updateFinished(itemKey, true);
} }
#endif
#ifdef QTLIB_UNIX
void UpdateHandler::updateAppImage(const QString &itemKey) void UpdateHandler::updateAppImage(const QString &itemKey)
{ {
auto updateAvailableItem = configHandler_->getUsrConfigUpdateAvailableItems()[itemKey].toObject(); auto updateAvailableItem = configHandler_->getUsrConfigUpdateAvailableItems()[itemKey].toObject();
...@@ -127,6 +140,7 @@ void UpdateHandler::updateAppImage(const QString &itemKey) ...@@ -127,6 +140,7 @@ void UpdateHandler::updateAppImage(const QString &itemKey)
auto installedItems = configHandler_->getUsrConfigInstalledItems(); auto installedItems = configHandler_->getUsrConfigInstalledItems();
if (!installedItems.contains(installedItemKey)) { if (!installedItems.contains(installedItemKey)) {
emit updateStarted(itemKey, false);
return; return;
} }
...@@ -134,57 +148,27 @@ void UpdateHandler::updateAppImage(const QString &itemKey) ...@@ -134,57 +148,27 @@ void UpdateHandler::updateAppImage(const QString &itemKey)
auto filename = installedItem["filename"].toString(); auto filename = installedItem["filename"].toString();
auto installType = installedItem["install_type"].toString(); auto installType = installedItem["install_type"].toString();
qtlib::File file(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename); auto filePath = configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename;
auto *updater = new AppImageUpdater(itemKey, filePath, this);
connect(updater, &AppImageUpdater::updateProgress, this, &UpdateHandler::updateProgress);
connect(updater, &AppImageUpdater::finished, this, &UpdateHandler::appImageUpdaterFinished);
QJsonObject metadata;
metadata["installed_item_key"] = installedItemKey;
metadata["installed_item_obj"] = installedItem;
metadata["new_filename"] = filename;
auto newFilename = filename; auto updateInformation = updater->describeAppImage();
auto updateInformation = describeAppImage(file.path());
for (const auto &info : updateInformation.split("\n")) { for (const auto &info : updateInformation.split("\n")) {
if (info.endsWith(".zsync", Qt::CaseInsensitive)) { if (info.endsWith(".zsync", Qt::CaseInsensitive)) {
newFilename = info.split("|").last().split("/").last().replace(".zsync", "", Qt::CaseInsensitive); metadata["new_filename"] = info.split("|").last().split("/").last().replace(".zsync", "", Qt::CaseInsensitive);
break; break;
} }
} }
appimage::update::Updater appImageUpdater(file.path().toStdString(), false); metadataSet_[itemKey] = metadata;
if (!appImageUpdater.start()) {
return;
}
emit updateStarted(itemKey);
while (!appImageUpdater.isDone()) {
QThread::msleep(100);
double progress;
if (appImageUpdater.progress(progress)) {
emit updateProgress(itemKey, progress * 100);
}
}
if (appImageUpdater.hasError()) {
std::string nextMessage;
while (appImageUpdater.nextStatusMessage(nextMessage)) {
qWarning() << QString::fromStdString(nextMessage);
}
emit updateFinished(itemKey);
return;
}
installedItem["filename"] = newFilename;
QJsonArray files;
files.append(QJsonValue(newFilename));
installedItem["files"] = files;
installedItem["installed_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigInstalledItemsItem(installedItemKey, installedItem);
if (newFilename != filename) {
file.remove();
}
configHandler_->removeUsrConfigUpdateAvailableItemsItem(itemKey);
emit updateFinished(itemKey); emit updateStarted(itemKey, true);
updater->updateAppImage();
} }
#endif #endif
#pragma once #pragma once
#include <QObject> #include <QObject>
#include <QJsonObject>
class ConfigHandler; class ConfigHandler;
#ifdef QTLIB_UNIX
class AppImageUpdater;
#endif
class UpdateHandler : public QObject class UpdateHandler : public QObject
{ {
Q_OBJECT Q_OBJECT
...@@ -12,22 +17,26 @@ public: ...@@ -12,22 +17,26 @@ public:
explicit UpdateHandler(ConfigHandler *configHandler, QObject *parent = nullptr); explicit UpdateHandler(ConfigHandler *configHandler, QObject *parent = nullptr);
signals: signals:
void checkAllStarted(); void checkAllStarted(bool status);
void checkAllFinished(); void checkAllFinished(bool status);
void updateStarted(QString itemKey); void updateStarted(QString itemKey, bool status);
void updateFinished(QString itemKey); void updateFinished(QString itemKey, bool status);
void updateProgress(QString itemKey, int progress); void updateProgress(QString itemKey, double progress);
public slots: public slots:
void checkAll(); void checkAll();
void update(const QString &itemKey); void update(const QString &itemKey);
private slots:
#ifdef QTLIB_UNIX
void appImageUpdaterFinished(AppImageUpdater *updater);
#endif
private: private:
#ifdef QTLIB_UNIX #ifdef QTLIB_UNIX
QString describeAppImage(const QString &path) const;
bool checkAppImage(const QString &path) const;
void updateAppImage(const QString &itemKey); void updateAppImage(const QString &itemKey);
#endif #endif
ConfigHandler *configHandler_; ConfigHandler *configHandler_;
QJsonObject metadataSet_;
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment