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

Merge branch 'feature/update' into develop

parents 3dfd4d75 b892be87
No related branches found
No related tags found
No related merge requests found
......@@ -33,6 +33,7 @@ unix:!ios:!android {
QT += dbus
HEADERS += \
$${PWD}/src/updaters/appimageupdater.h \
$${PWD}/src/desktopthemes/kdetheme.h \
$${PWD}/src/desktopthemes/gnometheme.h \
$${PWD}/src/desktopthemes/xfcetheme.h \
......@@ -40,6 +41,7 @@ unix:!ios:!android {
$${PWD}/src/desktopthemes/matetheme.h
SOURCES += \
$${PWD}/src/updaters/appimageupdater.cpp \
$${PWD}/src/desktopthemes/kdetheme.cpp \
$${PWD}/src/desktopthemes/gnometheme.cpp \
$${PWD}/src/desktopthemes/xfcetheme.cpp \
......
......@@ -3,27 +3,22 @@
#include <QStringList>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QDateTime>
#include <QThread>
#include <QDebug>
#ifdef QTLIB_UNIX
#include "appimage/update.h"
#endif
#include "qtlib_file.h"
#include "handlers/confighandler.h"
#ifdef QTLIB_UNIX
#include "updaters/appimageupdater.h"
#endif
UpdateHandler::UpdateHandler(ConfigHandler *configHandler, QObject *parent)
: QObject(parent), configHandler_(configHandler)
{}
void UpdateHandler::checkAll()
{
emit checkAllStarted();
// Resets data
QJsonObject updateAvailableItems;
configHandler_->setUsrConfigUpdateAvailableItems(updateAvailableItems);
......@@ -31,32 +26,34 @@ void UpdateHandler::checkAll()
auto installedItems = configHandler_->getUsrConfigInstalledItems();
if (installedItems.isEmpty()) {
emit checkAllFinished();
emit checkAllStarted(false);
return;
}
emit checkAllStarted(true);
for (const auto &itemKey : installedItems.keys()) {
auto installedItem = installedItems[itemKey].toObject();
auto filename = installedItem["filename"].toString();
auto installType = installedItem["install_type"].toString();
qtlib::File file;
QString filePath = "";
#ifdef QTLIB_UNIX
file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename);
filePath = configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename;
#else
file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString() + "/" + filename);
filePath = configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString() + "/" + filename;
#endif
QString updateMethod = "";
if (installType == "bin") {
#ifdef QTLIB_UNIX
if (file.path().endsWith(".appimage", Qt::CaseInsensitive)) {
if (checkAppImage(file.path())) {
if (filePath.endsWith(".appimage", Qt::CaseInsensitive)) {
if (AppImageUpdater(itemKey, filePath).checkForChanges()) {
updateMethod = "appimageupdate";
}
//else if (checkAppImageWithOcsApi(itemKey)) {
// updateMethod = "appimageupdatewithocsapi";
//else if (OcsFileUpdater(url).checkFile()) {
// updateMethod = "appimageupdate_ocs";
//}
}
#endif
......@@ -75,7 +72,7 @@ void UpdateHandler::checkAll()
application["update_checked_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigApplication(application);
emit checkAllFinished();
emit checkAllFinished(true);
}
void UpdateHandler::update(const QString &itemKey)
......@@ -83,6 +80,7 @@ void UpdateHandler::update(const QString &itemKey)
auto updateAvailableItems = configHandler_->getUsrConfigUpdateAvailableItems();
if (!updateAvailableItems.contains(itemKey)) {
emit updateStarted(itemKey, false);
return;
}
......@@ -92,34 +90,51 @@ void UpdateHandler::update(const QString &itemKey)
if (updateMethod == "appimageupdate") {
updateAppImage(itemKey);
}
//else if (updateMethod == "appimageupdatewithocsapi") {
// updateAppImageWithOcsApi(itemKey);
//else if (updateMethod == "appimageupdate_ocs") {
// updateAppImageOcs(itemKey);
//}
#endif
}
#ifdef QTLIB_UNIX
QString UpdateHandler::describeAppImage(const QString &path) const
void UpdateHandler::appImageUpdaterFinished(AppImageUpdater *updater)
{
appimage::update::Updater appImageUpdater(path.toStdString());
QString updateInformation;
std::string description;
if (appImageUpdater.describeAppImage(description)) {
updateInformation = QString::fromStdString(description);
auto itemKey = updater->id();
auto metadata = metadataSet_[itemKey].toObject();
metadataSet_.remove(itemKey);
if (!updater->isFinishedWithNoError()) {
emit updateFinished(itemKey, false);
updater->deleteLater();
return;
}
return updateInformation;
}
bool UpdateHandler::checkAppImage(const QString &path) const
{
appimage::update::Updater appImageUpdater(path.toStdString());
bool updateAvailable;
if (appImageUpdater.checkForChanges(updateAvailable)) {
return updateAvailable;
auto installedItemKey = metadata["installed_item_key"].toString();
auto installedItem = metadata["installed_item_obj"].toObject();
auto newFilename = metadata["new_filename"].toString();
auto filename = installedItem["filename"].toString();
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);
updater->deleteLater();
}
#endif
#ifdef QTLIB_UNIX
void UpdateHandler::updateAppImage(const QString &itemKey)
{
auto updateAvailableItem = configHandler_->getUsrConfigUpdateAvailableItems()[itemKey].toObject();
......@@ -127,64 +142,35 @@ void UpdateHandler::updateAppImage(const QString &itemKey)
auto installedItems = configHandler_->getUsrConfigInstalledItems();
if (!installedItems.contains(installedItemKey)) {
emit updateStarted(itemKey, false);
return;
}
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 *updater = new AppImageUpdater(itemKey, filePath, this);
connect(updater, &AppImageUpdater::updateProgress, this, &UpdateHandler::updateProgress);
connect(updater, &AppImageUpdater::finished, this, &UpdateHandler::appImageUpdaterFinished);
qtlib::File file(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename);
QJsonObject metadata;
metadata["installed_item_key"] = installedItemKey;
metadata["installed_item_obj"] = installedItem;
metadata["new_filename"] = filename;
auto newFilename = filename;
auto updateInformation = describeAppImage(file.path());
auto updateInformation = updater->describeAppImage();
for (const auto &info : updateInformation.split("\n")) {
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;
}
}
appimage::update::Updater appImageUpdater(file.path().toStdString(), false);
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);
metadataSet_[itemKey] = metadata;
emit updateFinished(itemKey);
emit updateStarted(itemKey, true);
updater->start();
}
#endif
#pragma once
#include <QObject>
#include <QJsonObject>
class ConfigHandler;
#ifdef QTLIB_UNIX
class AppImageUpdater;
#endif
class UpdateHandler : public QObject
{
Q_OBJECT
......@@ -12,22 +17,26 @@ public:
explicit UpdateHandler(ConfigHandler *configHandler, QObject *parent = nullptr);
signals:
void checkAllStarted();
void checkAllFinished();
void updateStarted(QString itemKey);
void updateFinished(QString itemKey);
void updateProgress(QString itemKey, int progress);
void checkAllStarted(bool status);
void checkAllFinished(bool status);
void updateStarted(QString itemKey, bool status);
void updateFinished(QString itemKey, bool status);
void updateProgress(QString itemKey, double progress);
public slots:
void checkAll();
void update(const QString &itemKey);
private slots:
#ifdef QTLIB_UNIX
void appImageUpdaterFinished(AppImageUpdater *updater);
#endif
private:
#ifdef QTLIB_UNIX
QString describeAppImage(const QString &path) const;
bool checkAppImage(const QString &path) const;
void updateAppImage(const QString &itemKey);
#endif
ConfigHandler *configHandler_;
QJsonObject metadataSet_;
};
#include "appimageupdater.h"
#include <QTimer>
#include "appimage/update.h"
AppImageUpdater::AppImageUpdater(const QString &id, const QString &path, QObject *parent)
: QObject(parent), id_(id), path_(path)
{
isFinishedWithNoError_ = false;
errorString_ = "";
updater_ = new appimage::update::Updater(path_.toStdString(), false);
}
AppImageUpdater::~AppImageUpdater()
{
delete updater_;
}
QString AppImageUpdater::id() const
{
return id_;
}
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 = "";
updater_->describeAppImage(description);
return QString::fromStdString(description);
}
bool AppImageUpdater::checkForChanges() const
{
bool updateAvailable = false;
updater_->checkForChanges(updateAvailable);
return updateAvailable;
}
void AppImageUpdater::start()
{
isFinishedWithNoError_ = false;
errorString_ = "";
if (!updater_->start()) {
emit finished(this);
return;
}
auto timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &AppImageUpdater::checkProgress);
connect(this, &AppImageUpdater::finished, timer, &QTimer::stop);
timer->start(100);
}
void AppImageUpdater::checkProgress()
{
if (!updater_->isDone()) {
double progress;
if (updater_->progress(progress)) {
emit updateProgress(id_, progress);
}
return;
}
if (updater_->hasError()) {
std::string message;
while (updater_->nextStatusMessage(message)) {
errorString_ += QString::fromStdString(message) + "\n";
}
emit finished(this);
return;
}
isFinishedWithNoError_ = true;
emit finished(this);
}
#pragma once
#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 checkForChanges() const;
void start();
signals:
void finished(AppImageUpdater *updater);
void updateProgress(QString id, double progress);
private slots:
void checkProgress();
private:
QString id_;
QString path_;
bool isFinishedWithNoError_;
QString errorString_;
appimage::update::Updater *updater_;
};
......@@ -199,33 +199,37 @@ void WebSocketServer::itemUninstallFinished(QJsonObject result)
sendMessage("", "ItemHandler::uninstallFinished", data);
}
void WebSocketServer::updateCheckAllStarted()
void WebSocketServer::updateCheckAllStarted(bool status)
{
QJsonArray data;
data.append(status);
sendMessage("", "UpdateHandler::checkAllStarted", data);
}
void WebSocketServer::updateCheckAllFinished()
void WebSocketServer::updateCheckAllFinished(bool status)
{
QJsonArray data;
data.append(status);
sendMessage("", "UpdateHandler::checkAllFinished", data);
}
void WebSocketServer::updateUpdateStarted(QString itemKey)
void WebSocketServer::updateUpdateStarted(QString itemKey, bool status)
{
QJsonArray data;
data.append(itemKey);
data.append(status);
sendMessage("", "UpdateHandler::updateStarted", data);
}
void WebSocketServer::updateUpdateFinished(QString itemKey)
void WebSocketServer::updateUpdateFinished(QString itemKey, bool status)
{
QJsonArray data;
data.append(itemKey);
data.append(status);
sendMessage("", "UpdateHandler::updateFinished", data);
}
void WebSocketServer::updateUpdateProgress(QString itemKey, int progress)
void WebSocketServer::updateUpdateProgress(QString itemKey, double progress)
{
QJsonArray data;
data.append(itemKey);
......
......@@ -51,11 +51,11 @@ private slots:
void itemUninstallStarted(QJsonObject result);
void itemUninstallFinished(QJsonObject result);
void updateCheckAllStarted();
void updateCheckAllFinished();
void updateUpdateStarted(QString itemKey);
void updateUpdateFinished(QString itemKey);
void updateUpdateProgress(QString itemKey, int progress);
void updateCheckAllStarted(bool status);
void updateCheckAllFinished(bool status);
void updateUpdateStarted(QString itemKey, bool status);
void updateUpdateFinished(QString itemKey, bool status);
void updateUpdateProgress(QString itemKey, double progress);
private:
void receiveMessage(const QString &id, const QString &func, const QJsonArray &data);
......
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