feat(cleanup): add a maintenance page for unused and legacy packages

Leftovers pile up on a machine: dependencies that no installed package
requires any more, and installs coming from the AUR or manual builds that the
sync databases know nothing about. The new Nettoyage page lists both (orphans
first, since they are the ones that can be removed in bulk), shows which
provider reported each row, and frees the space held by the caches: paccache
(or pacman -Sc) and flatpak's unused runtimes.

Cleanup is an optional capability of IPackageManager, so providers that only
install packages keep working unchanged.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-09-12 22:17:08 +02:00
1 parent b8f8dc569f
commit 632cdf1dea
10 files changed
+750 -6

No files matched your search

+35 -2
View File
@@ -14,6 +14,7 @@
#include <QHash>
#include <string>
#include <utility>
#include <vector>
#include "package-manager.h"
@@ -54,7 +55,7 @@ private:
class ListWorker : public QThread {
Q_OBJECT
public:
enum Mode { Installed, Updates };
enum Mode { Installed, Updates, Cleanup };
ListWorker(const std::vector<IPackageManager*>& providers, Mode mode);
void run() override;
signals:
@@ -70,8 +71,11 @@ private:
class InstallWorker : public QThread {
Q_OBJECT
public:
enum class Op { Install, Remove, Update, UpdateAll, Refresh };
enum class Op { Install, Remove, Update, UpdateAll, Refresh, RemovePackages, CleanCache };
InstallWorker(IPackageManager* pm, Op op, const std::string& package);
// Bulk variant: removes several packages in one provider call (cleanup
// page) instead of one call per package.
InstallWorker(IPackageManager* pm, Op op, const std::vector<std::string>& packages);
InstallWorker(const std::vector<IPackageManager*>& providers, Op op);
void run() override;
signals:
@@ -84,6 +88,7 @@ private:
std::vector<IPackageManager*> m_providers;
Op m_op;
std::string m_package;
std::vector<std::string> m_packages;
};
class MainWindow : public QMainWindow {
@@ -123,6 +128,12 @@ private slots:
void onUpdateCheckFinished(bool hasUpdate, const QString& latestVersion,
const QString& errorMessage);
void onSelfUpdateFinished(bool success, const QString& message);
void onCleanupScan();
void onCleanupListFinished(std::vector<PackageInfo> results);
void onCleanupSelectionChanged(int row);
void onRemoveCleanupOrphans();
void onRemoveCleanupSelection();
void onCleanCaches();
private:
enum BrowseMode { ModeSearch, ModeInstalled, ModeUpdates };
@@ -135,6 +146,7 @@ private:
NavSearch,
NavInstalled,
NavUpdates,
NavCleanup,
NavHistory,
NavLogs,
NavPlugins,
@@ -148,6 +160,7 @@ private:
PageHistory,
PageLogs,
PagePlugins,
PageCleanup,
};
// When the sudo password is requested: once per session (at launch) or
@@ -183,6 +196,7 @@ private:
void setupHistoryPage();
void setupLogsPage();
void setupPluginsPage();
void setupCleanupPage();
void setupMenuBar();
void updatePageTitle();
void updateStatusBar();
@@ -206,6 +220,13 @@ private:
void loadInstalled();
void loadUpdates();
void refreshHistoryTable();
void loadCleanup();
void populateCleanupTable(const std::vector<PackageInfo>& candidates);
// Runs a bulk removal of the given candidates, one provider at a time, and
// refreshes the cleanup list when the last group is done.
void removeCleanupPackages(const std::vector<PackageInfo>& packages,
const QString& description);
void startNextCleanupRemoval();
void loadLogs();
void applyLogsFilter();
void setupTray();
@@ -276,6 +297,18 @@ private:
// dashboard refreshes do not spam the user.
int m_lastNotifiedUpdates = -1;
// Cleanup page
QTableWidget* m_cleanupTable = nullptr;
QLabel* m_cleanupCount = nullptr;
QPushButton* m_cleanupRemoveOrphansButton = nullptr;
QPushButton* m_cleanupRemoveSelectionButton = nullptr;
QPushButton* m_cleanupCacheButton = nullptr;
std::vector<PackageInfo> m_cleanupCandidates;
// Providers left to process for the bulk removal in progress, with the
// packages each of them owns. They run one after another because pacman
// locks its database.
std::vector<std::pair<IPackageManager*, std::vector<std::string>>> m_pendingRemovals;
// Plugins page
QTableWidget* m_pluginTable = nullptr;
QPushButton* m_pluginToggleButton = nullptr;
+43
View File
@@ -24,6 +24,13 @@ struct PackageInfo {
std::vector<std::string> providers;
bool installed = false;
bool updateAvailable = false;
// Cleanup classification, filled by providers that report maintainable
// leftovers (see IPackageManager::listCleanupCandidates): true when no
// installed package requires this one, so it is safe to bulk-remove.
bool orphan = false;
// Short reason this row shows up as a cleanup candidate (e.g. "Orphelin",
// "Étranger"), empty for regular packages.
std::string cleanupReason;
};
// Merge packages that share the same name (offered by several providers) into
@@ -142,6 +149,42 @@ public:
virtual std::vector<PackageInfo> listUpdates() = 0;
virtual bool refresh(ProgressCallback progressCallback = nullptr) = 0;
// ---- Cleanup (optional) ----
// Providers that can reclaim unused/legacy packages override these; the
// defaults leave providers that only install packages working unchanged.
// Whether the provider can enumerate cleanup candidates at all.
virtual bool canListCleanup() const { return false; }
// Packages a cleanup can reclaim: orphans (nothing installed requires
// them) and legacy leftovers (installed from outside the sync
// repositories, e.g. AUR or manual builds). Rows carry `orphan` and
// `cleanupReason`.
virtual std::vector<PackageInfo> listCleanupCandidates() { return {}; }
// Remove several packages in one operation (a single privileged command
// instead of one per package). Defaults to a sequence of remove() calls.
virtual bool removePackages(const std::vector<std::string>& packages,
ProgressCallback progressCallback = nullptr) {
bool success = true;
for (const auto& name : packages) {
success = remove(name, progressCallback) && success;
}
return success;
}
// Whether cleanCache() actually has something to trim here, so the UI only
// offers the action for providers that own a cache.
virtual bool canCleanCache() const { return false; }
// Free the disk space held by the provider's caches (package cache,
// unused runtimes...). Returns false when the cleanup failed; the reason is
// then in lastError().
virtual bool cleanCache(ProgressCallback progressCallback = nullptr) {
(void)progressCallback;
return false;
}
// Check if the package manager is available on the system
virtual bool isAvailable() const = 0;
+28
View File
@@ -43,6 +43,24 @@ public:
// Exposed as static so the parsing can be unit-tested without invoking pacman.
static std::map<std::string, std::string> parseSyncRepos(const std::string& output);
// Merge `pacman -Qdt` (orphans) and `pacman -Qm` (foreign) output into
// cleanup candidates tagged with their reason: orphans are safe to
// bulk-remove, foreign packages are listed for review only since nothing
// proves they are unused. Exposed as static so it can be unit-tested
// without invoking pacman.
static std::vector<PackageInfo> parseCleanupOutput(const std::string& orphans,
const std::string& foreign);
// `pacman -Rns` command removing every given package together with the
// dependencies it dragged in, or empty when the list is empty. Exposed as
// static so the command can be unit-tested without invoking pacman.
static std::string removePackagesCommand(const std::vector<std::string>& packages);
// Command that trims the package cache, given whether paccache
// (pacman-contrib) is installed on this system. Exposed as static so both
// branches can be unit-tested without invoking pacman.
static std::string cleanCacheCommand(bool paccacheAvailable);
// Detect a sudo authentication failure (wrong or missing password) in the
// output of a privileged command. Exposed as static so it can be
// unit-tested without invoking sudo.
@@ -79,6 +97,16 @@ public:
std::vector<PackageInfo> listUpdates() override;
bool refresh(ProgressCallback progressCallback = nullptr) override;
// Cleanup: pacman is the one provider that knows about packages no longer
// required by anything (-Qdt) and about legacy installs coming from
// outside the sync databases (-Qm).
bool canListCleanup() const override { return true; }
bool canCleanCache() const override { return true; }
std::vector<PackageInfo> listCleanupCandidates() override;
bool removePackages(const std::vector<std::string>& packages,
ProgressCallback progressCallback = nullptr) override;
bool cleanCache(ProgressCallback progressCallback = nullptr) override;
private:
bool isPackageInstalled(const std::string& name);