Newer
Older
#include <QStringList>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QDateTime>
#ifdef QTLIB_UNIX
#include "appimage/update.h"
#endif
UpdateHandler::UpdateHandler(ConfigHandler *configHandler, QObject *parent)
: QObject(parent), configHandler_(configHandler)
{}
configHandler_->setUsrConfigUpdateAvailableItems(updateAvailableItems);
auto installedItems = configHandler_->getUsrConfigInstalledItems();
if (installedItems.isEmpty()) {
emit checkAllFinished();
return;
}
for (const auto &itemKey : installedItems.keys()) {
auto installedItem = installedItems[itemKey].toObject();
auto installType = installedItem["install_type"].toString();
file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename);
file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString() + "/" + filename);
if (file.path().endsWith(".appimage", Qt::CaseInsensitive)) {
if (checkAppImage(file.path())) {
//else if (checkAppImageWithOcsApi(itemKey)) {
// updateMethod = "appimageupdatewithocsapi";
//}
if (!updateMethod.isEmpty()) {
QJsonObject updateAvailableItem;
updateAvailableItem["installed_item"] = itemKey;
updateAvailableItem["update_method"] = updateMethod;
// Use installed item's key as unique key to the update available item
configHandler_->setUsrConfigUpdateAvailableItemsItem(itemKey, updateAvailableItem);
}
auto application = configHandler_->getUsrConfigApplication();
application["update_checked_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigApplication(application);
emit checkAllFinished();
auto updateAvailableItems = configHandler_->getUsrConfigUpdateAvailableItems();
if (!updateAvailableItems.contains(itemKey)) {
return;
}
auto updateMethod = updateAvailableItems[itemKey].toObject()["update_method"].toString();
if (updateMethod == "appimageupdate") {
updateAppImage(itemKey);
//else if (updateMethod == "appimageupdatewithocsapi") {
// updateAppImageWithOcsApi(itemKey);
//}
QString UpdateHandler::describeAppImage(const QString &path) const
{
appimage::update::Updater appImageUpdater(path.toStdString());
std::string description;
if (appImageUpdater.describeAppImage(description)) {
updateInformation = QString::fromStdString(description);
}
return updateInformation;
}
bool UpdateHandler::checkAppImage(const QString &path) const
{
appimage::update::Updater appImageUpdater(path.toStdString());
bool updateAvailable;
if (appImageUpdater.checkForChanges(updateAvailable)) {
return updateAvailable;
}
auto updateAvailableItem = configHandler_->getUsrConfigUpdateAvailableItems()[itemKey].toObject();
auto installedItemKey = updateAvailableItem["installed_item"].toString();
auto installedItems = configHandler_->getUsrConfigInstalledItems();
if (!installedItems.contains(installedItemKey)) {
return;
}
auto installedItem = installedItems[installedItemKey].toObject();
auto filename = installedItem["filename"].toString();
auto installType = installedItem["install_type"].toString();
qtlib::File file(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename);
for (const auto &info : updateInformation.split("\n")) {
if (info.endsWith(".zsync", Qt::CaseInsensitive)) {
newFilename = info.split("|").last().split("/").last().replace(".zsync", "", Qt::CaseInsensitive);
appimage::update::Updater appImageUpdater(file.path().toStdString(), false);
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);
installedItem["filename"] = newFilename;
QJsonArray files;
files.append(QJsonValue(newFilename));
installedItem["files"] = files;
installedItem["installed_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigInstalledItemsItem(installedItemKey, installedItem);
configHandler_->removeUsrConfigUpdateAvailableItemsItem(itemKey);
emit updateFinished(itemKey);