#pragma once #include #include #include #include #include #include // 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. // When the user accepts, the app updates itself by pulling + rebuilding // the source repository in the background; 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). Used for self-update. QString repoPath() const; // Whether the source repository is present so a self-update is possible. bool canSelfUpdate() const; // Pulls the latest code from the remote and rebuilds the app in the // background. The current running binary is left untouched; the new // version takes effect at the next launch. Emits selfUpdateFinished. void performSelfUpdate(); 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_repoPath; // source repo dir when found };