Verified Commit fd519cbb authored by azubieta's avatar azubieta

Trigger check updates from the wsServer

parent 7d1db270
......@@ -13,10 +13,17 @@
#include "updaters/appimageupdater.h"
#endif
#include "updaters/appupdater.h"
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()
{
// Resets data
......@@ -91,6 +98,28 @@ void UpdateHandler::update(const QString &itemKey)
#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
void UpdateHandler::appImageUpdaterFinished(AppImageUpdater *updater)
{
......
......@@ -9,6 +9,8 @@ class ConfigHandler;
class AppImageUpdater;
#endif
class AppUpdater;
class UpdateHandler : public QObject
{
Q_OBJECT
......@@ -16,6 +18,12 @@ class UpdateHandler : public QObject
public:
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:
void checkAllStarted(bool status);
void checkAllFinished(bool status);
......@@ -27,6 +35,9 @@ public slots:
void checkAll();
void update(const QString &itemKey);
void checkAppUpdates();
void silentCheckAppUpdates();
private slots:
#ifdef APP_DESKTOP
void appImageUpdaterFinished(AppImageUpdater *updater);
......@@ -37,6 +48,8 @@ private:
void updateAppImage(const QString &itemKey);
#endif
QString appFile_;
ConfigHandler *configHandler_;
QJsonObject metadataSet_;
AppUpdater * appUpdater_;
};
......@@ -9,6 +9,7 @@
#include <QDebug>
#include "handlers/confighandler.h"
#include "handlers/updatehandler.h"
#include "websockets/websocketserver.h"
#include "updaters/appupdater.h"
......@@ -23,6 +24,8 @@ int main(int argc, char *argv[])
auto *configHandler = new ConfigHandler(&app);
auto appConfigApplication = configHandler->getAppConfigApplication();
auto updateHandler = new UpdateHandler(configHandler, &app);
app.setApplicationName(appConfigApplication["name"].toString());
app.setApplicationVersion(appConfigApplication["version"].toString());
app.setOrganizationName(appConfigApplication["organization"].toString());
......@@ -52,17 +55,14 @@ int main(int argc, char *argv[])
clParser.process(app);
// Setup AppUpdater
auto appFile = clParser.value(clOptionAppPath);
AppUpdater appUpdater(appFile);
appUpdater.setSilentLookup(true);
appUpdater.doUpdateLookUp();
auto port = clParser.value(clOptionPort).toUShort();
// Setup websocket server
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()) {
qInfo() << "Websocket server started at:" << wsServer->serverUrl().toString();
......@@ -72,5 +72,8 @@ int main(int argc, char *argv[])
return 1;
}
updateHandler->setAppFile(appFile);
updateHandler->checkAppUpdates();
return app.exec();
}
......@@ -69,7 +69,7 @@ Do you want to download it?</string>
<widget class="QWidget" name="progressPage">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="title">
<widget class="QLabel" name="progressLabel">
<property name="text">
<string>Downloading update contents</string>
</property>
......
......@@ -16,6 +16,16 @@ AppUpdateDialog::~AppUpdateDialog()
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()
{
setWindowTitle(tr("Pling Store Update Available"));
......@@ -60,6 +70,7 @@ void AppUpdateDialog::showProgress(int progress)
setWindowTitle(tr("Pling Store Update"));
ui->progressBar->setValue(progress);
ui->progressBar->setRange(0, 100);
ui->progressPage->show();
ui->stackedWidget->setCurrentWidget(ui->progressPage);
......
......@@ -27,6 +27,8 @@ signals:
void updateRequested();
public slots:
void showLookingForUpdates();
void showUpdateConfirmationMessage();
void showErrorMessage(const QString &msg);
......
......@@ -18,6 +18,11 @@ void AppUpdater::setSilentLookup(bool value)
silentLookup = value;
}
bool AppUpdater::isRunning()
{
return progressCheckTimer.isActive();
}
void AppUpdater::doUpdateLookUp()
{
if (appImagePath.isEmpty()) {
......@@ -26,7 +31,7 @@ void AppUpdater::doUpdateLookUp()
}
if (!silentLookup)
updateDialog.show();
updateDialog.showLookingForUpdates();
QtConcurrent::run([=]() {
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
public:
explicit AppUpdater(const QString &appImagePath, QObject *parent = nullptr);
void setSilentLookup(bool value);
bool isRunning();
signals:
void updateAvailable();
void restartApp(QString appPath);
......
......@@ -25,7 +25,6 @@ WebSocketServer::WebSocketServer(ConfigHandler *configHandler, const QString &se
systemHandler_ = new SystemHandler(this);
ocsApiHandler_ = new OcsApiHandler(configHandler_, this);
itemHandler_ = new ItemHandler(configHandler_, this);
updateHandler_ = new UpdateHandler(configHandler_, this);
desktopThemeHandler_ = new DesktopThemeHandler(this);
connect(itemHandler_, &ItemHandler::metadataSetChanged, this, &WebSocketServer::itemHandlerMetadataSetChanged);
......@@ -38,12 +37,6 @@ WebSocketServer::WebSocketServer(ConfigHandler *configHandler, const QString &se
connect(itemHandler_, &ItemHandler::installFinished, this, &WebSocketServer::itemHandlerInstallFinished);
connect(itemHandler_, &ItemHandler::uninstallStarted, this, &WebSocketServer::itemHandlerUninstallStarted);
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()
......@@ -52,6 +45,17 @@ WebSocketServer::~WebSocketServer()
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()
{
if (wsServer_->listen(QHostAddress::Any, serverPort_)) {
......@@ -382,7 +386,10 @@ void WebSocketServer::receiveMessage(const QString &id, const QString &func, con
}
else if (func == "UpdateHandler::update") {
updateHandler_->update(data.at(0).toString());
} else if (func == "UpdateHandler::checkAppUpdate") {
updateHandler_->checkAppUpdates();
}
// DesktopThemeHandler
else if (func == "DesktopThemeHandler::desktopEnvironment") {
resultData.append(desktopThemeHandler_->desktopEnvironment());
......
......@@ -23,6 +23,7 @@ public:
explicit WebSocketServer(ConfigHandler *configHandler, const QString &serverName = QString("WebSocketServer"), quint16 serverPort = 0, QObject *parent = nullptr);
~WebSocketServer();
void setUpdateHandler(UpdateHandler *updateHandler);
signals:
void started();
void stopped();
......
Markdown is supported
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