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

UpdateHandler

parent bf1c6cc9
Branches
Tags
No related merge requests found
#include "updatehandler.h" #include "updatehandler.h"
#include <QStringList>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QDateTime>
#ifdef QTLIB_UNIX #ifdef QTLIB_UNIX
#include <thread> #include <thread>
#include <chrono> #include <chrono>
...@@ -7,18 +13,84 @@ ...@@ -7,18 +13,84 @@
#include "appimage/update.h" #include "appimage/update.h"
#endif #endif
#include "qtlib_file.h"
#include "handlers/confighandler.h" #include "handlers/confighandler.h"
UpdateHandler::UpdateHandler(ConfigHandler *configHandler, QObject *parent) UpdateHandler::UpdateHandler(ConfigHandler *configHandler, QObject *parent)
: QObject(parent), configHandler_(configHandler) : QObject(parent), configHandler_(configHandler)
{} {}
QString UpdateHandler::checkAll() const void UpdateHandler::checkAll()
{ {
emit checkAllStarted();
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;
}
// Clear data
QJsonObject updateAvailable;
configHandler_->setUsrConfigUpdateAvailable(updateAvailable);
for (const auto &itemKey : installedItems.keys()) {
auto installedItem = installedItems[itemKey].toObject();
auto installType = installedItem["install_type"].toString();
QString destDir = "";
#ifdef QTLIB_UNIX
destDir = configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString();
#else
destDir = configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString();
#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;
#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)) {}
}
#endif
}
}
}
application["update_checked_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigApplication(application);
emit checkAllFinished();
} }
QString UpdateHandler::update(const QString &path) const void UpdateHandler::update(const QString &fileKey)
{ {
if (configHandler_->getUsrConfigUpdateAvailable().contains(fileKey)) {
auto updateAvailableFile = configHandler_->getUsrConfigUpdateAvailable()[fileKey].toObject();
#ifdef QTLIB_UNIX
if (updateAvailableFile["update_method"].toString() == "appimageupdate") {
updateAppImage(fileKey);
}
#endif
}
} }
#ifdef QTLIB_UNIX #ifdef QTLIB_UNIX
...@@ -43,24 +115,56 @@ bool UpdateHandler::checkAppImage(const QString &path) const ...@@ -43,24 +115,56 @@ bool UpdateHandler::checkAppImage(const QString &path) const
return false; return false;
} }
bool UpdateHandler::updateAppImage(const QString &path) void UpdateHandler::updateAppImage(const QString &fileKey)
{ {
auto updateAvailableFile = configHandler_->getUsrConfigUpdateAvailable()[fileKey].toObject();
auto path = updateAvailableFile["path"].toString();
auto filename = updateAvailableFile["filename"].toString();
auto itemKey = updateAvailableFile["installed_item"].toString();
auto newFilename = filename;
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();
break;
}
}
appimage::update::Updater appImageUpdater(path.toStdString(), false); appimage::update::Updater appImageUpdater(path.toStdString(), false);
if (appImageUpdater.start()) { if (appImageUpdater.start()) {
emit updateStarted(path); emit updateStarted(fileKey);
while (!appImageUpdater.isDone()) { while (!appImageUpdater.isDone()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
double progress; double progress;
if (appImageUpdater.progress(progress)) { if (appImageUpdater.progress(progress)) {
emit updateProgress(path, progress * 100); emit updateProgress(fileKey, progress * 100);
} }
} }
auto newPath = path; // dummy
emit updateFinished(path, newPath);
if (!appImageUpdater.hasError()) { if (!appImageUpdater.hasError()) {
return true; 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;
configHandler_->setUsrConfigInstalledItemsItem(itemKey, installedItem);
qtlib::File(path).remove();
}
configHandler_->removeUsrConfigUpdateAvailableFile(fileKey);
}
emit updateFinished(fileKey);
} }
return false;
} }
#endif #endif
...@@ -14,19 +14,19 @@ public: ...@@ -14,19 +14,19 @@ public:
signals: signals:
void checkAllStarted(); void checkAllStarted();
void checkAllFinished(); void checkAllFinished();
void updateStarted(QString path); void updateStarted(QString fileKey);
void updateFinished(QString path, QString newPath); void updateFinished(QString fileKey);
void updateProgress(QString path, int progress); void updateProgress(QString fileKey, int progress);
public slots: public slots:
bool checkAll() const; void checkAll();
bool update(const QString &path) const; void update(const QString &fileKey);
private: private:
#ifdef QTLIB_UNIX #ifdef QTLIB_UNIX
QString describeAppImage(const QString &path) const; QString describeAppImage(const QString &path) const;
bool checkAppImage(const QString &path) const; bool checkAppImage(const QString &path) const;
bool updateAppImage(const QString &path); void updateAppImage(const QString &fileKey);
#endif #endif
ConfigHandler *configHandler_; ConfigHandler *configHandler_;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment