110 lines
4.8 KiB
C++
110 lines
4.8 KiB
C++
#pragma once
|
|
|
|
#include <QObject>
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkReply>
|
|
#include <QDateTime>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#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;
|
|
|
|
// Prebuilt binary archive attached to the release
|
|
// (PacManager-<version>-x86_64.tar.gz), when the release workflow
|
|
// uploaded one. Preferred over the source tarball: no compiler needed.
|
|
QString prebuiltUrl() const;
|
|
|
|
// True when the pending update can be applied by unpacking a prebuilt
|
|
// archive instead of recompiling from source.
|
|
bool hasPrebuiltUpdate() 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());
|
|
|
|
// Pre-flight: missing tools that would make the source path fail
|
|
// (e.g. "cmake", "g++"). Empty when a prebuilt archive is available
|
|
// (no compiler needed) or every tool is present.
|
|
QStringList missingSelfUpdateTools() const;
|
|
|
|
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);
|
|
void performPrebuiltUpdate(const QString& sudoPassword);
|
|
void performTarballUpdate(const QString& sudoPassword);
|
|
QString installCommand(const QString& quotedPassword,
|
|
const QString& action) const;
|
|
static QString quoteShell(const QString& s);
|
|
static QString quotedPassword(const QString& sudoPassword);
|
|
|
|
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_prebuiltUrl; // prebuilt binary archive, when attached
|
|
QString m_releasePageUrl; // human-readable release page
|
|
QString m_repoPath; // source repo dir when found
|
|
}; |