Files
FWS-PacManage/include/update-manager.h
T
BlackAngelTV 40a01b0060
release / release (push) Has been cancelled
tests / build-test (push) Has been cancelled
fix(update): enable self-update for installed app via release tarball
The update dialog offered a disabled button when running from an install prefix without a source checkout, and the version file lagged behind the release tag. Add a tarball download + rebuild + reinstall path to the same prefix (sudo only for system prefixes) and bump the version file to 1.3.1.
2026-09-10 20:08:08 +02:00

88 lines
3.7 KiB
C++

#pragma once
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDateTime>
#include <QString>
#include <QProcess>
// Checks the Gitea repository for new releases and tracks the last check
// state in a JSON file so the app only queries the API once per day.
// Self-update works in two modes:
// - source checkout present (dev): git pull + rebuild in place;
// - installed without the repo (~/.local/bin, /usr/...): download the
// release tarball, build it in /tmp and `cmake --install` to the same
// prefix the running binary was installed to (sudo when needed).
// The running binary stays untouched until the next launch.
class UpdateManager : public QObject {
Q_OBJECT
public:
explicit UpdateManager(QObject* parent = nullptr);
// Current app version, read from the version file at build time.
static QString currentVersion();
// Path of the state file storing the last check date and the latest
// known remote version (e.g. ~/.local/share/PacManager/update-state.json).
static QString stateFilePath();
// True when the last check happened less than 24h ago, so we can skip
// the network call at launch.
bool shouldCheck() const;
// Latest remote version recorded in the state file (empty if never checked).
QString lastKnownRemoteVersion() const;
// Fetches the latest release from the Gitea API. Emits checkFinished.
void checkForUpdates();
// True when the remote version is newer than the local one.
static bool isNewerVersion(const QString& remote, const QString& local);
// Path of the source repository, or empty when the app is installed
// without the repo (e.g. ~/.local/bin).
QString repoPath() const;
// Download URL of the source tarball for the pending update (empty until
// a check completes), plus the human-readable release page.
QString tarballUrl() const;
QString releasePageUrl() const;
// Install prefix the running binary was installed to (e.g. ~/.local,
// /usr/local, /usr). Used to reinstall the fresh build at the same place.
QString installPrefix() const;
// Whether a self-update is possible. True when running from a source
// checkout, or when a tarball URL is known (installed mode).
bool canSelfUpdate() const;
// Rebuilds + reinstalls the app in the background. Pass the sudo password
// when the install prefix is not user-writable (empty = no sudo pipe,
// `sudo` will prompt on its own if needed). Emits selfUpdateFinished.
void performSelfUpdate(const QString& sudoPassword = QString());
signals:
// Emitted after a check completes. hasUpdate is true when a newer
// release exists; errorMessage is non-empty when the request failed.
void checkFinished(bool hasUpdate, const QString& latestVersion,
const QString& errorMessage);
// Emitted when the background self-update completes. success is true
// if the pull + build produced a fresh executable.
void selfUpdateFinished(bool success, const QString& message);
private:
void saveState(const QString& remoteVersion);
void loadState();
void runShell(const QString& script, const QString& workingDir);
QNetworkAccessManager m_network;
QProcess m_process; // runs the git pull + build script
QString m_lastCheckDate; // ISO date of the last successful check
QString m_lastKnownRemoteVersion; // latest version seen on the server
QString m_latestVersion; // version of the pending update
QString m_tarballUrl; // source tarball for the pending update
QString m_releasePageUrl; // human-readable release page
QString m_repoPath; // source repo dir when found
};