diff --git a/include/update-manager.h b/include/update-manager.h index 912da0e..db60058 100644 --- a/include/update-manager.h +++ b/include/update-manager.h @@ -9,9 +9,12 @@ // 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. +// 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: @@ -38,16 +41,26 @@ public: 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. + // without the repo (e.g. ~/.local/bin). QString repoPath() const; - // Whether the source repository is present so a self-update is possible. + // 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; - // 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(); + // 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 @@ -69,5 +82,7 @@ private: 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 }; \ No newline at end of file diff --git a/src/main-window.cpp b/src/main-window.cpp index 44c9e19..d5e6ce0 100644 --- a/src/main-window.cpp +++ b/src/main-window.cpp @@ -5,10 +5,13 @@ #include #include #include +#include +#include #include #include #include #include +#include #include #include #include @@ -2158,9 +2161,10 @@ void MainWindow::onUpdateCheckFinished(bool hasUpdate, const QString& latestVers return; } - // Ask the user whether to update now or skip. When the source repo is - // available the update runs in the background (git pull + build); the - // running binary stays untouched until the next launch. + // Ask the user whether to update now or skip. The update runs in the + // background (git pull + rebuild from a checkout, or tarball download + + // rebuild + reinstall when installed); the running binary stays + // untouched until the next launch. QMessageBox box(this); box.setWindowTitle("Mise à jour disponible"); box.setIcon(QMessageBox::Information); @@ -2170,25 +2174,60 @@ void MainWindow::onUpdateCheckFinished(bool hasUpdate, const QString& latestVers .arg(latestVersion, UpdateManager::currentVersion())); QPushButton* updateBtn = box.addButton("Mettre à jour", QMessageBox::AcceptRole); - QPushButton* skipBtn = box.addButton("Plus tard", QMessageBox::RejectRole); + box.addButton("Plus tard", QMessageBox::RejectRole); if (!m_updateManager->canSelfUpdate()) { - // No source checkout: only point to the releases page. + // No source checkout and no tarball URL: fall back to the releases + // page instead of a disabled button. + updateBtn->setText("Ouvrir la page des releases"); box.setInformativeText( "Le dépôt source n'est pas disponible sur ce système.\n" "Téléchargez la nouvelle version depuis la page des releases."); - updateBtn->setEnabled(false); + } else if (m_updateManager->repoPath().isEmpty()) { + // Installed mode: tarball download + local build + reinstall to the + // same prefix (sudo only for system prefixes). + box.setInformativeText( + QString("La mise à jour va télécharger les sources, les compiler " + "et réinstaller l'application dans %1.\n" + "Redémarrez PacManager ensuite pour utiliser la nouvelle version.") + .arg(m_updateManager->installPrefix())); } box.exec(); - if (box.clickedButton() == updateBtn) { - statusBar()->showMessage("Mise à jour en cours en arrière-plan..."); - connect(m_updateManager, &UpdateManager::selfUpdateFinished, - this, &MainWindow::onSelfUpdateFinished); - m_updateManager->performSelfUpdate(); + if (box.clickedButton() != updateBtn) { + // "Plus tard" (or closing the dialog) simply skips the update. + return; } - // "Plus tard" (or closing the dialog) simply skips the update. + + if (!m_updateManager->canSelfUpdate()) { + QDesktopServices::openUrl(QUrl(m_updateManager->releasePageUrl())); + return; + } + + // System prefixes (/usr, /usr/local) need sudo for `cmake --install`. + // Prompt once for the password; user prefixes install without sudo. + QString sudoPassword; + const QString prefix = m_updateManager->installPrefix(); + const bool prefixWritable = + QFileInfo(prefix).isWritable() || QFileInfo(prefix + "/bin").isWritable(); + if (m_updateManager->repoPath().isEmpty() && !prefixWritable + && m_pacman && m_pacman->isAvailable()) { + bool ok = false; + sudoPassword = QInputDialog::getText( + this, "Mise à jour", + QString("Mot de passe sudo pour installer dans %1 :").arg(prefix), + QLineEdit::Password, QString(), &ok); + if (!ok) { + statusBar()->showMessage("Mise à jour annulée."); + return; + } + } + + statusBar()->showMessage("Mise à jour en cours en arrière-plan..."); + connect(m_updateManager, &UpdateManager::selfUpdateFinished, + this, &MainWindow::onSelfUpdateFinished); + m_updateManager->performSelfUpdate(sudoPassword); } // Called when the background git pull + build finishes. Reports the result in diff --git a/src/update-manager.cpp b/src/update-manager.cpp index 0743270..4890bf7 100644 --- a/src/update-manager.cpp +++ b/src/update-manager.cpp @@ -21,16 +21,18 @@ static const char* kRepoUrl = UpdateManager::UpdateManager(QObject* parent) : QObject(parent) { loadState(); - // The running executable is expected to live in the build/ directory when - // launched from a source checkout. Walk up two levels to find the repo. - // When installed to ~/.local/bin there is no source tree, so update - // becomes impossible and the dialog simply points to the releases page. - QDir dir(QCoreApplication::applicationDirPath()); - if (dir.dirName() == QLatin1String("build")) { - dir.cdUp(); - if (dir.exists(QLatin1String("CMakeLists.txt"))) { + // Detect a source checkout around the running executable so the dev + // workflow (git pull + rebuild in place) keeps working. Supported + // layouts: /build/PacManager and /build//PacManager. + const QDir appDir(QCoreApplication::applicationDirPath()); + QDir dir = appDir; + for (int i = 0; i < 3; ++i) { + if (dir.exists(QLatin1String("CMakeLists.txt")) && + dir.exists(QLatin1String("src/main.cpp"))) { m_repoPath = dir.absolutePath(); + break; } + if (!dir.cdUp()) break; } connect(&m_process, &QProcess::finished, this, [this](int exitCode) { @@ -87,22 +89,42 @@ QString UpdateManager::repoPath() const { return m_repoPath; } +QString UpdateManager::tarballUrl() const { + return m_tarballUrl; +} + +QString UpdateManager::releasePageUrl() const { + return m_releasePageUrl; +} + +QString UpdateManager::installPrefix() const { + // Infer the install prefix from the running binary location: + // /bin/PacManager -> . Falls back to ~/.local. + QDir dir(QCoreApplication::applicationDirPath()); + if (dir.dirName() == QLatin1String("bin")) { + dir.cdUp(); + return dir.absolutePath(); + } + return QDir::homePath() + "/.local"; +} + bool UpdateManager::canSelfUpdate() const { - if (m_repoPath.isEmpty()) return false; + if (!m_repoPath.isEmpty()) { + QDir repo(m_repoPath); + if (!repo.exists(QLatin1String(".git"))) return !m_tarballUrl.isEmpty(); - QDir repo(m_repoPath); - if (!repo.exists(QLatin1String(".git"))) return false; - - // The remote must point to the same FWS repo, otherwise we would build - // someone else's code on the user's machine. - QFile config(m_repoPath + "/.git/config"); - if (config.open(QIODevice::ReadOnly | QIODevice::Text)) { - const QString content = QString::fromUtf8(config.readAll()); - if (content.contains(QLatin1String("FWS/FWS-PacManage"))) { - return true; + // The remote must point to the same FWS repo, otherwise we would build + // someone else's code on the user's machine. + QFile config(m_repoPath + "/.git/config"); + if (config.open(QIODevice::ReadOnly | QIODevice::Text)) { + const QString content = QString::fromUtf8(config.readAll()); + if (content.contains(QLatin1String("FWS/FWS-PacManage"))) { + return true; + } } } - return false; + // Installed mode: a tarball download + local build needs no git checkout. + return !m_tarballUrl.isEmpty(); } void UpdateManager::loadState() { @@ -167,7 +189,10 @@ void UpdateManager::checkForUpdates() { } // Gitea returns the release tag (e.g. "V1.2.1") as `tag_name`. - QString tag = doc.object().value("tag_name").toString().trimmed(); + // Keep the tarball + release page so an installed app (no git + // checkout) can still self-update by downloading the sources. + const QJsonObject obj = doc.object(); + QString tag = obj.value("tag_name").toString().trimmed(); if (tag.isEmpty()) { emit checkFinished(false, QString(), QStringLiteral("Aucune release trouvée sur le serveur.")); @@ -179,6 +204,23 @@ void UpdateManager::checkForUpdates() { tag = tag.mid(1); } + // Prefer an attached source archive; fall back to the auto-generated + // tarball Gitea always provides (releases here ship no assets). + QString tarball = obj.value("tarball_url").toString().trimmed(); + const QJsonArray assets = obj.value("assets").toArray(); + for (const QJsonValue& v : assets) { + const QString dl = v.toObject().value("browser_download_url").toString().trimmed(); + if (dl.endsWith(QLatin1String(".tar.gz")) || dl.endsWith(QLatin1String(".zip"))) { + tarball = dl; + break; + } + } + m_tarballUrl = tarball; + m_releasePageUrl = obj.value("html_url").toString().trimmed(); + if (m_releasePageUrl.isEmpty()) { + m_releasePageUrl = QStringLiteral("https://gitea.nocode-var.tech/FWS/FWS-PacManage/releases"); + } + saveState(tag); bool hasUpdate = isNewerVersion(tag, currentVersion()); @@ -212,27 +254,103 @@ bool UpdateManager::isNewerVersion(const QString& remote, const QString& local) return false; } -// Pulls the latest source and rebuilds the app in the background. The script -// exits with a non-zero code on failure so the caller can report it. The new -// binary replaces the old one only when the build completes — the currently -// running process is untouched and picks it up on its next launch. -void UpdateManager::performSelfUpdate() { - if (m_repoPath.isEmpty()) { +// Rebuilds + reinstalls the app in the background. +// - Source checkout present: git pull + rebuild in place. +// - Installed without the repo: download the release tarball to /tmp, +// build it there and `cmake --install` to the prefix the running binary +// came from (sudo when that prefix is not user-writable). +// The new binary takes effect at the next launch; the running process is +// untouched. Exits non-zero on failure so the caller can report it. +void UpdateManager::performSelfUpdate(const QString& sudoPassword) { + if (!m_repoPath.isEmpty()) { + QString script = QStringLiteral( + "set -e\n" + "echo 'Mise à jour de PacManager...'\n" + "git pull --ff-only origin main 2>&1 || git pull --ff-only 2>&1\n" + "if [ -d build ]; then\n" + " cmake --build build -j$(nproc) 2>&1\n" + "else\n" + " cmake -B build -DCMAKE_BUILD_TYPE=Release . 2>&1\n" + " cmake --build build -j$(nproc) 2>&1\n" + "fi\n" + "cmake --install build 2>&1\n" + "echo 'Mise à jour terminée. Redémarrez PacManager pour utiliser la nouvelle version.'"); + + runShell(script, m_repoPath); + return; + } + + if (m_tarballUrl.isEmpty() || m_latestVersion.isEmpty()) { emit selfUpdateFinished(false, QStringLiteral("Dépôt source introuvable.")); return; } + // Quote the password for the shell pipe; never log it. + QString quoted; + { + QString p = sudoPassword; + p.replace(QLatin1String("'"), QLatin1String("'\\''")); + quoted = QStringLiteral("'") + p + QStringLiteral("'"); + } + const bool useSudoPipe = !sudoPassword.isEmpty(); + + // Installed mode: fetch the release tarball, compile in /tmp, reinstall + // to the same prefix. `cmake --install` needs sudo only for system + // prefixes; ~/.local stays a plain user install. QString script = QStringLiteral( "set -e\n" "echo 'Mise à jour de PacManager...'\n" - "git pull --ff-only origin main 2>&1 || git pull --ff-only 2>&1\n" - "if [ -d build ]; then\n" - " cmake --build build -j$(nproc) 2>&1\n" + "command -v cmake >/dev/null || { echo 'cmake est requis pour la mise à jour (pacman -S cmake)'; exit 1; }\n" + "WORK=$(mktemp -d /tmp/pacmanager-update-XXXXXX)\n" + "trap 'rm -rf \"$WORK\"' EXIT\n" + "TARBALL_URL=%1\n" + "VERSION=%2\n" + "PREFIX=%3\n" + "ARCHIVE=\"$WORK/src.tar.gz\"\n" + "if command -v curl >/dev/null; then curl -fsSL -o \"$ARCHIVE\" \"$TARBALL_URL\" 2>&1;\n" + "elif command -v wget >/dev/null; then wget -O \"$ARCHIVE\" \"$TARBALL_URL\" 2>&1;\n" + "else echo 'curl ou wget est requis pour télécharger la mise à jour'; exit 1; fi\n" + "if ! tar xzf \"$ARCHIVE\" -C \"$WORK\" 2>/dev/null; then\n" + " command -v unzip >/dev/null || { echo 'unzip est requis pour cette archive'; exit 1; }\n" + " unzip -q \"$ARCHIVE\" -d \"$WORK\" 2>&1 || { echo 'Archive invalide'; exit 1; }\n" + "fi\n" + // NOTE: single %h here — QString::arg() leaves %h untouched (only + // %1..%99 are placeholders), so find receives the correct -printf. + "SRC=$(find \"$WORK\" -maxdepth 2 -name CMakeLists.txt -printf '%h\\n' | head -n 1)\n" + "[ -n \"$SRC\" ] || { echo 'Archive invalide: CMakeLists.txt introuvable'; exit 1; }\n" + "cmake -S \"$SRC\" -B \"$WORK/build\" -DCMAKE_BUILD_TYPE=Release 2>&1\n" + "cmake --build \"$WORK/build\" -j$(nproc) 2>&1\n" + "if [ -w \"$PREFIX\" ] || [ -w \"$PREFIX/bin\" ]; then\n" + " cmake --install \"$WORK/build\" --prefix \"$PREFIX\" 2>&1\n" "else\n" - " cmake -B build -DCMAKE_BUILD_TYPE=Release . 2>&1\n" - " cmake --build build -j$(nproc) 2>&1\n" + "%4" "fi\n" "echo 'Mise à jour terminée. Redémarrez PacManager pour utiliser la nouvelle version.'"); - runShell(script, m_repoPath); + // Install step: pipe the sudo password when provided, otherwise let sudo + // prompt on its own terminal (graphical sudo may pop up its dialog). + QString installStep; + if (useSudoPipe) { + // NOTE: single %s — QString::arg() only substitutes %1..%99, so + // printf keeps its format specifier intact. + installStep = QStringLiteral( + " printf '%s\\n' %1 | sudo -S cmake --install \"$WORK/build\" --prefix \"$PREFIX\" 2>&1\n") + .arg(quoted); + } else { + installStep = QStringLiteral( + " sudo cmake --install \"$WORK/build\" --prefix \"$PREFIX\" 2>&1\n"); + } + + auto quoteShell = [](const QString& s) { + QString q = s; + q.replace(QLatin1String("'"), QLatin1String("'\\''")); + return QStringLiteral("'") + q + QStringLiteral("'"); + }; + + script = script.arg(quoteShell(m_tarballUrl)) + .arg(quoteShell(m_latestVersion)) + .arg(quoteShell(installPrefix())) + .arg(installStep); + + runShell(script, QDir::homePath()); } \ No newline at end of file diff --git a/version b/version index cb174d5..6261a05 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.2.1 \ No newline at end of file +1.3.1 \ No newline at end of file