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

UpdateHandler

parent 75544f51
No related branches found
No related tags found
No related merge requests found
......@@ -69,14 +69,14 @@ bool ConfigHandler::setUsrConfigInstalledItems(const QJsonObject &object) const
return usrConfig_.set("installed_items", object);
}
QJsonObject ConfigHandler::getUsrConfigUpdateAvailable() const
QJsonObject ConfigHandler::getUsrConfigUpdateAvailableItems() const
{
return usrConfig_.get("update_available");
return usrConfig_.get("update_available_items");
}
bool ConfigHandler::setUsrConfigUpdateAvailable(const QJsonObject &object) const
bool ConfigHandler::setUsrConfigUpdateAvailableItems(const QJsonObject &object) const
{
return usrConfig_.set("update_available", object);
return usrConfig_.set("update_available_items", object);
}
bool ConfigHandler::setUsrConfigProvidersProvider(const QString &providerKey, const QJsonObject &object) const
......@@ -177,26 +177,24 @@ bool ConfigHandler::removeUsrConfigInstalledItemsItem(const QString &itemKey) co
return setUsrConfigInstalledItems(installedItems);
}
bool ConfigHandler::setUsrConfigUpdateAvailableFile(const QString &fileKey, const QJsonObject &object) const
bool ConfigHandler::setUsrConfigUpdateAvailableItemsItem(const QString &itemKey, const QJsonObject &object) const
{
/* object format
{
"path": "/home/user/.local/bin/example.AppImage",
"filename": "example.AppImage",
"installed_item": "http://example.com/downloads/example.AppImage",
"update_method": "appimageupdate"
}
*/
auto updateAvailable = getUsrConfigUpdateAvailable();
updateAvailable[fileKey] = object;
return setUsrConfigUpdateAvailable(updateAvailable);
auto updateAvailableItems = getUsrConfigUpdateAvailableItems();
updateAvailableItems[itemKey] = object;
return setUsrConfigUpdateAvailableItems(updateAvailableItems);
}
bool ConfigHandler::removeUsrConfigUpdateAvailableFile(const QString &fileKey) const
bool ConfigHandler::removeUsrConfigUpdateAvailableItemsItem(const QString &itemKey) const
{
auto updateAvailable = getUsrConfigUpdateAvailable();
updateAvailable.remove(fileKey);
return setUsrConfigUpdateAvailable(updateAvailable);
auto updateAvailableItems = getUsrConfigUpdateAvailableItems();
updateAvailableItems.remove(itemKey);
return setUsrConfigUpdateAvailableItems(updateAvailableItems);
}
void ConfigHandler::importAppConfigApplication()
......
......@@ -24,8 +24,8 @@ public slots:
bool setUsrConfigCategories(const QJsonObject &object) const;
QJsonObject getUsrConfigInstalledItems() const;
bool setUsrConfigInstalledItems(const QJsonObject &object) const;
QJsonObject getUsrConfigUpdateAvailable() const;
bool setUsrConfigUpdateAvailable(const QJsonObject &object) const;
QJsonObject getUsrConfigUpdateAvailableItems() const;
bool setUsrConfigUpdateAvailableItems(const QJsonObject &object) const;
bool setUsrConfigProvidersProvider(const QString &providerKey, const QJsonObject &object) const;
bool removeUsrConfigProvidersProvider(const QString &providerKey) const;
......@@ -34,8 +34,8 @@ public slots:
bool setUsrConfigCategoriesInstallType(const QString &providerKey, const QString &categoryKey, const QString &installType) const;
bool setUsrConfigInstalledItemsItem(const QString &itemKey, const QJsonObject &object) const;
bool removeUsrConfigInstalledItemsItem(const QString &itemKey) const;
bool setUsrConfigUpdateAvailableFile(const QString &fileKey, const QJsonObject &object) const;
bool removeUsrConfigUpdateAvailableFile(const QString &fileKey) const;
bool setUsrConfigUpdateAvailableItemsItem(const QString &itemKey, const QJsonObject &object) const;
bool removeUsrConfigUpdateAvailableItemsItem(const QString &itemKey) const;
private:
void importAppConfigApplication();
......
......@@ -24,76 +24,67 @@ void UpdateHandler::checkAll()
{
emit checkAllStarted();
auto application = configHandler_->getUsrConfigApplication();
QJsonObject updateAvailableItems;
configHandler_->setUsrConfigUpdateAvailableItems(updateAvailableItems); // Resets data
auto installedItems = configHandler_->getUsrConfigInstalledItems();
if (installedItems.isEmpty() || application.contains("update_checked_at")) {
auto currentDate = QDateTime::currentDateTime();
auto checkedDate = QDateTime::fromMSecsSinceEpoch(application["update_checked_at"].toInt());
if (currentDate.daysTo(checkedDate.addDays(1)) <= 0) {
emit checkAllFinished();
return;
}
if (installedItems.isEmpty()) {
emit checkAllFinished();
return;
}
// Clear data
QJsonObject updateAvailable;
configHandler_->setUsrConfigUpdateAvailable(updateAvailable);
for (const auto &itemKey : installedItems.keys()) {
auto installedItem = installedItems[itemKey].toObject();
auto filename = installedItem["filename"].toString();
auto installType = installedItem["install_type"].toString();
QString destDir = "";
qtlib::File file;
#ifdef QTLIB_UNIX
destDir = configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString();
file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename);
#else
destDir = configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString();
file.setPath(configHandler_->getAppConfigInstallTypes()[installType].toObject()["generic_destination"].toString() + "/" + filename);
#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;
QJsonObject updateAvailableItem;
if (installType == "bin") {
#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)) {}
if (file.path().endsWith(".appimage", Qt::CaseInsensitive)) {
if (checkAppImage(file.path())) {
updateAvailableItem["installed_item"] = itemKey;
updateAvailableItem["update_method"] = QString("appimageupdate");
configHandler_->setUsrConfigUpdateAvailableItemsItem(itemKey, updateAvailableItem);
}
#endif
//else if (checkAppImageWithOcsApi(itemKey)) {}
}
#endif
}
}
auto application = configHandler_->getUsrConfigApplication();
application["update_checked_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigApplication(application);
emit checkAllFinished();
}
void UpdateHandler::update(const QString &fileKey)
void UpdateHandler::update(const QString &itemKey)
{
if (configHandler_->getUsrConfigUpdateAvailable().contains(fileKey)) {
auto updateMethod = configHandler_->getUsrConfigUpdateAvailable()[fileKey].toObject()["update_method"].toString();
auto updateAvailableItems = configHandler_->getUsrConfigUpdateAvailableItems();
if (!updateAvailableItems.contains(itemKey)) {
return;
}
auto updateMethod = updateAvailableItems[itemKey].toObject()["update_method"].toString();
#ifdef QTLIB_UNIX
if (updateMethod == "appimageupdate") {
updateAppImage(fileKey);
}
//else if (updateMethod == "appimageupdatewithocsapi") {
//}
#endif
if (updateMethod == "appimageupdate") {
updateAppImage(itemKey);
}
//else if (updateMethod == "appimageupdatewithocsapi") {}
#endif
}
#ifdef QTLIB_UNIX
......@@ -118,16 +109,25 @@ bool UpdateHandler::checkAppImage(const QString &path) const
return false;
}
void UpdateHandler::updateAppImage(const QString &fileKey)
void UpdateHandler::updateAppImage(const QString &itemKey)
{
auto updateAvailableFile = configHandler_->getUsrConfigUpdateAvailable()[fileKey].toObject();
auto installedItems = configHandler_->getUsrConfigInstalledItems();
auto updateAvailableItem = configHandler_->getUsrConfigUpdateAvailableItems()[itemKey].toObject();
auto installedItemKey = updateAvailableItem["installed_item"].toString();
if (!installedItems.contains(installedItemKey)) {
return;
}
auto installedItem = installedItems[installedItemKey].toObject();
auto filename = installedItem["filename"].toString();
auto installType = installedItem["install_type"].toString();
auto path = updateAvailableFile["path"].toString();
auto filename = updateAvailableFile["filename"].toString();
auto itemKey = updateAvailableFile["installed_item"].toString();
qtlib::File file(configHandler_->getAppConfigInstallTypes()[installType].toObject()["destination"].toString() + "/" + filename);
auto newFilename = filename;
auto updateInformation = describeAppImage(path);
auto updateInformation = describeAppImage(file.path());
for (const auto &info : updateInformation.split("\n")) {
if (info.endsWith(".zsync", Qt::CaseInsensitive)) {
newFilename = info.split("|").last().split("/").last().replace(".zsync", "", Qt::CaseInsensitive);
......@@ -135,48 +135,45 @@ void UpdateHandler::updateAppImage(const QString &fileKey)
}
}
appimage::update::Updater appImageUpdater(path.toStdString(), false);
if (appImageUpdater.start()) {
emit updateStarted(fileKey);
appimage::update::Updater appImageUpdater(file.path().toStdString(), false);
if (!appImageUpdater.start()) {
return;
}
while (!appImageUpdater.isDone()) {
QThread::msleep(100);
double progress;
if (appImageUpdater.progress(progress)) {
emit updateProgress(fileKey, progress * 100);
}
}
emit updateStarted(itemKey);
if (appImageUpdater.hasError()) {
std::string nextMessage;
while (appImageUpdater.nextStatusMessage(nextMessage)) {
qWarning() << QString::fromStdString(nextMessage);
}
while (!appImageUpdater.isDone()) {
QThread::msleep(100);
double progress;
if (appImageUpdater.progress(progress)) {
emit updateProgress(itemKey, progress * 100);
}
}
emit updateFinished(fileKey);
return;
if (appImageUpdater.hasError()) {
std::string nextMessage;
while (appImageUpdater.nextStatusMessage(nextMessage)) {
qWarning() << QString::fromStdString(nextMessage);
}
configHandler_->removeUsrConfigUpdateAvailableFile(fileKey);
emit updateFinished(itemKey);
return;
}
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;
installedItem["installed_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigInstalledItemsItem(itemKey, installedItem);
qtlib::File(path).remove();
}
installedItem["filename"] = newFilename;
QJsonArray files;
files.append(QJsonValue(newFilename));
installedItem["files"] = files;
installedItem["installed_at"] = QDateTime::currentMSecsSinceEpoch();
configHandler_->setUsrConfigInstalledItemsItem(installedItemKey, installedItem);
emit updateFinished(fileKey);
if (newFilename != filename) {
file.remove();
}
configHandler_->removeUsrConfigUpdateAvailableItemsItem(itemKey);
emit updateFinished(itemKey);
}
#endif
......@@ -14,19 +14,19 @@ public:
signals:
void checkAllStarted();
void checkAllFinished();
void updateStarted(QString fileKey);
void updateFinished(QString fileKey);
void updateProgress(QString fileKey, int progress);
void updateStarted(QString itemKey);
void updateFinished(QString itemKey);
void updateProgress(QString itemKey, int progress);
public slots:
void checkAll();
void update(const QString &fileKey);
void update(const QString &itemKey);
private:
#ifdef QTLIB_UNIX
QString describeAppImage(const QString &path) const;
bool checkAppImage(const QString &path) const;
void updateAppImage(const QString &fileKey);
void updateAppImage(const QString &itemKey);
#endif
ConfigHandler *configHandler_;
......
......@@ -211,24 +211,24 @@ void WebSocketServer::updateCheckAllFinished()
sendMessage("", "UpdateHandler::checkAllFinished", data);
}
void WebSocketServer::updateUpdateStarted(QString fileKey)
void WebSocketServer::updateUpdateStarted(QString itemKey)
{
QJsonArray data;
data.append(fileKey);
data.append(itemKey);
sendMessage("", "UpdateHandler::updateStarted", data);
}
void WebSocketServer::updateUpdateFinished(QString fileKey)
void WebSocketServer::updateUpdateFinished(QString itemKey)
{
QJsonArray data;
data.append(fileKey);
data.append(itemKey);
sendMessage("", "UpdateHandler::updateFinished", data);
}
void WebSocketServer::updateUpdateProgress(QString fileKey, int progress)
void WebSocketServer::updateUpdateProgress(QString itemKey, int progress)
{
QJsonArray data;
data.append(fileKey);
data.append(itemKey);
data.append(progress);
sendMessage("", "UpdateHandler::updateProgress", data);
}
......@@ -289,11 +289,11 @@ void WebSocketServer::receiveMessage(const QString &id, const QString &func, con
else if (func == "ConfigHandler::setUsrConfigInstalledItems") {
resultData.append(configHandler_->setUsrConfigInstalledItems(data.at(0).toObject()));
}
else if (func == "ConfigHandler::getUsrConfigUpdateAvailable") {
resultData.append(configHandler_->getUsrConfigUpdateAvailable());
else if (func == "ConfigHandler::getUsrConfigUpdateAvailableItems") {
resultData.append(configHandler_->getUsrConfigUpdateAvailableItems());
}
else if (func == "ConfigHandler::setUsrConfigUpdateAvailable") {
resultData.append(configHandler_->setUsrConfigUpdateAvailable(data.at(0).toObject()));
else if (func == "ConfigHandler::setUsrConfigUpdateAvailableItems") {
resultData.append(configHandler_->setUsrConfigUpdateAvailableItems(data.at(0).toObject()));
}
else if (func == "ConfigHandler::setUsrConfigProvidersProvider") {
resultData.append(configHandler_->setUsrConfigProvidersProvider(data.at(0).toString(), data.at(1).toObject()));
......@@ -316,11 +316,11 @@ void WebSocketServer::receiveMessage(const QString &id, const QString &func, con
else if (func == "ConfigHandler::removeUsrConfigInstalledItemsItem") {
resultData.append(configHandler_->removeUsrConfigInstalledItemsItem(data.at(0).toString()));
}
else if (func == "ConfigHandler::setUsrConfigUpdateAvailableFile") {
resultData.append(configHandler_->setUsrConfigUpdateAvailableFile(data.at(0).toString(), data.at(1).toObject()));
else if (func == "ConfigHandler::setUsrConfigUpdateAvailableItemsItem") {
resultData.append(configHandler_->setUsrConfigUpdateAvailableItemsItem(data.at(0).toString(), data.at(1).toObject()));
}
else if (func == "ConfigHandler::removeUsrConfigUpdateAvailableFile") {
resultData.append(configHandler_->removeUsrConfigUpdateAvailableFile(data.at(0).toString()));
else if (func == "ConfigHandler::removeUsrConfigUpdateAvailableItemsItem") {
resultData.append(configHandler_->removeUsrConfigUpdateAvailableItemsItem(data.at(0).toString()));
}
// SystemHandler
else if (func == "SystemHandler::isUnix") {
......
......@@ -53,9 +53,9 @@ private slots:
void updateCheckAllStarted();
void updateCheckAllFinished();
void updateUpdateStarted(QString fileKey);
void updateUpdateFinished(QString fileKey);
void updateUpdateProgress(QString fileKey, int progress);
void updateUpdateStarted(QString itemKey);
void updateUpdateFinished(QString itemKey);
void updateUpdateProgress(QString itemKey, int 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