diff --git a/app/src/handlers/updatehandler.cpp b/app/src/handlers/updatehandler.cpp index d1e11c2fd8f01092e84c02589125e79cf6898302..b113253e5fc04184610213d76fec2dd297e8447f 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 c246fb52e2e9194e82aadb741c9c5fc75382de03..de2e8b20d733e10a0293ed7f5d63e23e8a868f8c 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 d83ab71c591f82f69b7232db526a995632083fb7..03477f72939b60b00f2b1527b6b89f689f032b61 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_; };