Verified Commit fd519cbb authored by azubieta's avatar azubieta
Browse files

Trigger check updates from the wsServer

parent 7d1db270
...@@ -13,10 +13,17 @@ ...@@ -13,10 +13,17 @@
#include "updaters/appimageupdater.h" #include "updaters/appimageupdater.h"
#endif #endif
#include "updaters/appupdater.h"
UpdateHandler::UpdateHandler(ConfigHandler *configHandler, QObject *parent) UpdateHandler::UpdateHandler(ConfigHandler *configHandler, QObject *parent)
: QObject(parent), configHandler_(configHandler) : QObject(parent), configHandler_(configHandler), appUpdater_(nullptr)
{} {}
void UpdateHandler::setAppFile(const QString &appFile)
{
appFile_ = appFile;
}
void UpdateHandler::checkAll() void UpdateHandler::checkAll()
{ {
// Resets data // Resets data
...@@ -91,6 +98,28 @@ void UpdateHandler::update(const QString &itemKey) ...@@ -91,6 +98,28 @@ void UpdateHandler::update(const QString &itemKey)
#endif #endif
} }
void UpdateHandler::checkAppUpdates()
{
if (appUpdater_ == nullptr)
appUpdater_ = new AppUpdater(appFile_, this);
appUpdater_->setSilentLookup(false);
if (!appUpdater_->isRunning())
appUpdater_->doUpdateLookUp();
}
void UpdateHandler::silentCheckAppUpdates()
{
if (appUpdater_ == nullptr)
appUpdater_ = new AppUpdater(appFile_, this);
appUpdater_->setSilentLookup(true);
if (!appUpdater_->isRunning())
appUpdater_->doUpdateLookUp();
}
#ifdef APP_DESKTOP #ifdef APP_DESKTOP
void UpdateHandler::appImageUpdaterFinished(AppImageUpdater *updater) void UpdateHandler::appImageUpdaterFinished(AppImageUpdater *updater)
{ {
......
...@@ -9,6 +9,8 @@ class ConfigHandler; ...@@ -9,6 +9,8 @@ class ConfigHandler;
class AppImageUpdater; class AppImageUpdater;
#endif #endif
class AppUpdater;
class UpdateHandler : public QObject class UpdateHandler : public QObject
{ {
Q_OBJECT Q_OBJECT
...@@ -16,6 +18,12 @@ class UpdateHandler : public QObject ...@@ -16,6 +18,12 @@ class UpdateHandler : public QObject
public: public:
explicit UpdateHandler(ConfigHandler *configHandler, QObject *parent = nullptr); explicit UpdateHandler(ConfigHandler *configHandler, QObject *parent = nullptr);
/**
* @brief set application file to be used when performing self updates
* @param appFile
*/
void setAppFile(const QString &appFile);
signals: signals:
void checkAllStarted(bool status); void checkAllStarted(bool status);
void checkAllFinished(bool status); void checkAllFinished(bool status);
...@@ -27,6 +35,9 @@ public slots: ...@@ -27,6 +35,9 @@ public slots:
void checkAll(); void checkAll();
void update(const QString &itemKey); void update(const QString &itemKey);
void checkAppUpdates();
void silentCheckAppUpdates();
private slots: private slots:
#ifdef APP_DESKTOP #ifdef APP_DESKTOP
void appImageUpdaterFinished(AppImageUpdater *updater); void appImageUpdaterFinished(AppImageUpdater *updater);
...@@ -37,6 +48,8 @@ private: ...@@ -37,6 +48,8 @@ private:
void updateAppImage(const QString &itemKey); void updateAppImage(const QString &itemKey);
#endif #endif
QString appFile_;
ConfigHandler *configHandler_; ConfigHandler *configHandler_;
QJsonObject metadataSet_; QJsonObject metadataSet_;
AppUpdater * appUpdater_;
}; };
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <QDebug> #include <QDebug>
#include "handlers/confighandler.h" #include "handlers/confighandler.h"
#include "handlers/updatehandler.h"
#include "websockets/websocketserver.h" #include "websockets/websocketserver.h"
#include "updaters/appupdater.h" #include "updaters/appupdater.h"
...@@ -23,6 +24,8 @@ int main(int argc, char *argv[]) ...@@ -23,6 +24,8 @@ int main(int argc, char *argv[])
auto *configHandler = new ConfigHandler(&app); auto *configHandler = new ConfigHandler(&app);
auto appConfigApplication = configHandler->getAppConfigApplication(); auto appConfigApplication = configHandler->getAppConfigApplication();
auto updateHandler = new UpdateHandler(configHandler, &app);
app.setApplicationName(appConfigApplication["name"].toString()); app.setApplicationName(appConfigApplication["name"].toString());
app.setApplicationVersion(appConfigApplication["version"].toString()); app.setApplicationVersion(appConfigApplication["version"].toString());
app.setOrganizationName(appConfigApplication["organization"].toString()); app.setOrganizationName(appConfigApplication["organization"].toString());
...@@ -52,17 +55,14 @@ int main(int argc, char *argv[]) ...@@ -52,17 +55,14 @@ int main(int argc, char *argv[])
clParser.process(app); clParser.process(app);
// Setup AppUpdater
auto appFile = clParser.value(clOptionAppPath); auto appFile = clParser.value(clOptionAppPath);
AppUpdater appUpdater(appFile);
appUpdater.setSilentLookup(true);
appUpdater.doUpdateLookUp();
auto port = clParser.value(clOptionPort).toUShort(); auto port = clParser.value(clOptionPort).toUShort();
// Setup websocket server // Setup websocket server
auto *wsServer = new WebSocketServer(configHandler, appConfigApplication["id"].toString(), port, &app); auto *wsServer = new WebSocketServer(configHandler, appConfigApplication["id"].toString(), port, &app);
QObject::connect(wsServer, &WebSocketServer::stopped, &app, &QGuiApplication::quit); wsServer->setUpdateHandler(updateHandler);
QObject::connect(wsServer, &WebSocketServer::stopped, &app, &QGuiApplication::quit);
if (wsServer->start()) { if (wsServer->start()) {
qInfo() << "Websocket server started at:" << wsServer->serverUrl().toString(); qInfo() << "Websocket server started at:" << wsServer->serverUrl().toString();
...@@ -72,5 +72,8 @@ int main(int argc, char *argv[]) ...@@ -72,5 +72,8 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
updateHandler->setAppFile(appFile);
updateHandler->checkAppUpdates();
return app.exec(); return app.exec();
} }
...@@ -69,7 +69,7 @@ Do you want to download it?</string> ...@@ -69,7 +69,7 @@ Do you want to download it?</string>
<widget class="QWidget" name="progressPage"> <widget class="QWidget" name="progressPage">
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
<widget class="QLabel" name="title"> <widget class="QLabel" name="progressLabel">
<property name="text"> <property name="text">
<string>Downloading update contents</string> <string>Downloading update contents</string>
</property> </property>
......
...@@ -16,6 +16,16 @@ AppUpdateDialog::~AppUpdateDialog() ...@@ -16,6 +16,16 @@ AppUpdateDialog::~AppUpdateDialog()
delete ui; delete ui;
} }
void AppUpdateDialog::showLookingForUpdates()
{
setWindowTitle(tr("Looking for Pling Store Updates"));
ui->progressLabel->setText(tr("Looking for Pling Store Updates"));
ui->progressBar->setRange(0, 0);
ui->stackedWidget->setCurrentWidget(ui->progressPage);
show();
}
void AppUpdateDialog::showUpdateConfirmationMessage() void AppUpdateDialog::showUpdateConfirmationMessage()
{ {
setWindowTitle(tr("Pling Store Update Available")); setWindowTitle(tr("Pling Store Update Available"));
...@@ -60,6 +70,7 @@ void AppUpdateDialog::showProgress(int progress) ...@@ -60,6 +70,7 @@ void AppUpdateDialog::showProgress(int progress)
setWindowTitle(tr("Pling Store Update")); setWindowTitle(tr("Pling Store Update"));
ui->progressBar->setValue(progress); ui->progressBar->setValue(progress);
ui->progressBar->setRange(0, 100);
ui->progressPage->show(); ui->progressPage->show();
ui->stackedWidget->setCurrentWidget(ui->progressPage); ui->stackedWidget->setCurrentWidget(ui->progressPage);
......
...@@ -27,6 +27,8 @@ signals: ...@@ -27,6 +27,8 @@ signals:
void updateRequested(); void updateRequested();
public slots: public slots:
void showLookingForUpdates();
void showUpdateConfirmationMessage(); void showUpdateConfirmationMessage();
void showErrorMessage(const QString &msg); void showErrorMessage(const QString &msg);
......
...@@ -18,6 +18,11 @@ void AppUpdater::setSilentLookup(bool value) ...@@ -18,6 +18,11 @@ void AppUpdater::setSilentLookup(bool value)
silentLookup = value; silentLookup = value;
} }
bool AppUpdater::isRunning()
{
return progressCheckTimer.isActive();
}
void AppUpdater::doUpdateLookUp() void AppUpdater::doUpdateLookUp()
{ {
if (appImagePath.isEmpty()) { if (appImagePath.isEmpty()) {
...@@ -26,7 +31,7 @@ void AppUpdater::doUpdateLookUp() ...@@ -26,7 +31,7 @@ void AppUpdater::doUpdateLookUp()
} }
if (!silentLookup) if (!silentLookup)
updateDialog.show(); updateDialog.showLookingForUpdates();
QtConcurrent::run([=]() { QtConcurrent::run([=]() {
appimage::update::Updater updater(appImagePath.toStdString()); appimage::update::Updater updater(appImagePath.toStdString());
......
#include <QDebug>
#include <QtConcurrent/QtConcurrent>
#include "appimage/update.h"
#include "appupdater.h"
AppUpdater::AppUpdater(const QString &appImagePath, QObject *parent) : QObject(parent), appImagePath(appImagePath), updateHelper(nullptr)
{
connect(this, &AppUpdater::updateAvailable, &updateDialog, &AppUpdateDialog::showUpdateConfirmationMessage);
connect(&updateDialog, &AppUpdateDialog::updateRequested, this, &AppUpdater::doUpdate);
connect(&updateDialog, &AppUpdateDialog::restartRequested, this, &AppUpdater::doRestart);
connect(&updateDialog, &AppUpdateDialog::rejected, this, &AppUpdater::stop);
}
void AppUpdater::setSilentLookup(bool value)
{
silentLookup = value;
}
void AppUpdater::doUpdateLookUp()
{
if (appImagePath.isEmpty()) {
qWarning() << "Self-updates disabled: No app file provided.";
return;
}
if (!silentLookup)
updateDialog.show();
QtConcurrent::run([=]() {
appimage::update::Updater updater(appImagePath.toStdString());
bool updateAvailable; // this is an output parameter!!!
updater.checkForChanges(updateAvailable);
if (updateAvailable) {
qDebug() << "Update available";
emit this->updateAvailable();
}
});
}
void AppUpdater::doUpdate()
{
if (appImagePath.isEmpty()) {
qWarning() << "Self-updates disabled: No app file provided.";
return;
}
if (updateHelper !=nullptr)
delete updateHelper;
updateHelper = new appimage::update::Updater(appImagePath.toStdString());
updateHelper->start();
progressCheckTimer.setInterval(200);
progressCheckTimer.start();
connect(&progressCheckTimer, &QTimer::timeout, this, &AppUpdater::checkUpdateProgress);
}
void AppUpdater::doRestart()
{
QProcess::startDetached("killall", {"pling-store"});
if (updateHelper != nullptr) {
std::string pathToNewFile;
updateHelper->pathToNewFile(pathToNewFile);
if (!pathToNewFile.empty()) {
QString path = QString::fromStdString(pathToNewFile);
QFile::setPermissions(path, QFileDevice::ReadUser | QFileDevice::ExeUser);
QProcess proc;
proc.setEnvironment(getCleanSystemEnvironment());
qDebug() << proc.environment();
if (proc.startDetached(path)) {
updateDialog.accept();
} else
updateDialog.showErrorMessage("Unable to start: " + path);
}
}
}
void AppUpdater::stop()
{
}
void AppUpdater::checkUpdateProgress()
{
using namespace appimage::update;
auto state = updateHelper->state();
switch (state) {
case Updater::INITIALIZED:
break;
case Updater::RUNNING:
double progress;
updateHelper->progress(progress);
updateDialog.showProgress(progress*100);
break;
case Updater::STOPPING:
break;
case Updater::SUCCESS:
updateDialog.showCompletionMessage();
progressCheckTimer.stop();
break;
case Updater::ERROR:
updateDialog.showErrorMessage(tr("Update failed"));
progressCheckTimer.stop();
break;
}
}
QStringList AppUpdater::getCleanSystemEnvironment()
{
QString appDirPath = qgetenv("APPDIR");
QProcessEnvironment systenEnvironemnt = QProcessEnvironment::systemEnvironment();
QProcessEnvironment processEnvironment;
for (QString key: systenEnvironemnt.keys()) {
QString value = systenEnvironemnt.value(key);
QStringList oldValue = value.split(":");
QStringList newVaule;
for (const QString &valueSection: oldValue)
if (!valueSection.contains(appDirPath))
newVaule << valueSection;
if (!newVaule.empty())
processEnvironment.insert(key, newVaule.join(":"));
}
return processEnvironment.toStringList();
// return {"DISPLAY=:0"};
}
...@@ -17,9 +17,9 @@ class AppUpdater : public QObject ...@@ -17,9 +17,9 @@ class AppUpdater : public QObject
public: public:
explicit AppUpdater(const QString &appImagePath, QObject *parent = nullptr); explicit AppUpdater(const QString &appImagePath, QObject *parent = nullptr);
void setSilentLookup(bool value); void setSilentLookup(bool value);
bool isRunning();
signals: signals:
void updateAvailable(); void updateAvailable();
void restartApp(QString appPath); void restartApp(QString appPath);
......
...@@ -25,7 +25,6 @@ WebSocketServer::WebSocketServer(ConfigHandler *configHandler, const QString &se ...@@ -25,7 +25,6 @@ WebSocketServer::WebSocketServer(ConfigHandler *configHandler, const QString &se
systemHandler_ = new SystemHandler(this); systemHandler_ = new SystemHandler(this);
ocsApiHandler_ = new OcsApiHandler(configHandler_, this); ocsApiHandler_ = new OcsApiHandler(configHandler_, this);
itemHandler_ = new ItemHandler(configHandler_, this); itemHandler_ = new ItemHandler(configHandler_, this);
updateHandler_ = new UpdateHandler(configHandler_, this);
desktopThemeHandler_ = new DesktopThemeHandler(this); desktopThemeHandler_ = new DesktopThemeHandler(this);
connect(itemHandler_, &ItemHandler::metadataSetChanged, this, &WebSocketServer::itemHandlerMetadataSetChanged); connect(itemHandler_, &ItemHandler::metadataSetChanged, this, &WebSocketServer::itemHandlerMetadataSetChanged);
...@@ -38,12 +37,6 @@ WebSocketServer::WebSocketServer(ConfigHandler *configHandler, const QString &se ...@@ -38,12 +37,6 @@ WebSocketServer::WebSocketServer(ConfigHandler *configHandler, const QString &se
connect(itemHandler_, &ItemHandler::installFinished, this, &WebSocketServer::itemHandlerInstallFinished); connect(itemHandler_, &ItemHandler::installFinished, this, &WebSocketServer::itemHandlerInstallFinished);
connect(itemHandler_, &ItemHandler::uninstallStarted, this, &WebSocketServer::itemHandlerUninstallStarted); connect(itemHandler_, &ItemHandler::uninstallStarted, this, &WebSocketServer::itemHandlerUninstallStarted);
connect(itemHandler_, &ItemHandler::uninstallFinished, this, &WebSocketServer::itemHandlerUninstallFinished); connect(itemHandler_, &ItemHandler::uninstallFinished, this, &WebSocketServer::itemHandlerUninstallFinished);
connect(updateHandler_, &UpdateHandler::checkAllStarted, this, &WebSocketServer::updateHandlerCheckAllStarted);
connect(updateHandler_, &UpdateHandler::checkAllFinished, this, &WebSocketServer::updateHandlerCheckAllFinished);
connect(updateHandler_, &UpdateHandler::updateStarted, this, &WebSocketServer::updateHandlerUpdateStarted);
connect(updateHandler_, &UpdateHandler::updateFinished, this, &WebSocketServer::updateHandlerUpdateFinished);
connect(updateHandler_, &UpdateHandler::updateProgress, this, &WebSocketServer::updateHandlerUpdateProgress);
} }
WebSocketServer::~WebSocketServer() WebSocketServer::~WebSocketServer()
...@@ -52,6 +45,17 @@ WebSocketServer::~WebSocketServer() ...@@ -52,6 +45,17 @@ WebSocketServer::~WebSocketServer()
wsServer_->deleteLater(); wsServer_->deleteLater();
} }
void WebSocketServer::setUpdateHandler(UpdateHandler *updateHandler)
{
updateHandler_ = updateHandler;
connect(updateHandler_, &UpdateHandler::checkAllStarted, this, &WebSocketServer::updateHandlerCheckAllStarted);
connect(updateHandler_, &UpdateHandler::checkAllFinished, this, &WebSocketServer::updateHandlerCheckAllFinished);
connect(updateHandler_, &UpdateHandler::updateStarted, this, &WebSocketServer::updateHandlerUpdateStarted);
connect(updateHandler_, &UpdateHandler::updateFinished, this, &WebSocketServer::updateHandlerUpdateFinished);
connect(updateHandler_, &UpdateHandler::updateProgress, this, &WebSocketServer::updateHandlerUpdateProgress);
}
bool WebSocketServer::start() bool WebSocketServer::start()
{ {
if (wsServer_->listen(QHostAddress::Any, serverPort_)) { if (wsServer_->listen(QHostAddress::Any, serverPort_)) {
...@@ -382,7 +386,10 @@ void WebSocketServer::receiveMessage(const QString &id, const QString &func, con ...@@ -382,7 +386,10 @@ void WebSocketServer::receiveMessage(const QString &id, const QString &func, con
} }
else if (func == "UpdateHandler::update") { else if (func == "UpdateHandler::update") {
updateHandler_->update(data.at(0).toString()); updateHandler_->update(data.at(0).toString());
} else if (func == "UpdateHandler::checkAppUpdate") {
updateHandler_->checkAppUpdates();
} }
// DesktopThemeHandler // DesktopThemeHandler
else if (func == "DesktopThemeHandler::desktopEnvironment") { else if (func == "DesktopThemeHandler::desktopEnvironment") {
resultData.append(desktopThemeHandler_->desktopEnvironment()); resultData.append(desktopThemeHandler_->desktopEnvironment());
......
...@@ -23,6 +23,7 @@ public: ...@@ -23,6 +23,7 @@ public:
explicit WebSocketServer(ConfigHandler *configHandler, const QString &serverName = QString("WebSocketServer"), quint16 serverPort = 0, QObject *parent = nullptr); explicit WebSocketServer(ConfigHandler *configHandler, const QString &serverName = QString("WebSocketServer"), quint16 serverPort = 0, QObject *parent = nullptr);
~WebSocketServer(); ~WebSocketServer();
void setUpdateHandler(UpdateHandler *updateHandler);
signals: signals:
void started(); void started();
void stopped(); void stopped();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment