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

+416 -4
View File
@@ -92,22 +92,34 @@ void ListWorker::run() {
pkg.provider = pm->getName();
results.push_back(pkg);
}
} else {
} else if (m_mode == Updates) {
if (!pm->canUpdate()) continue;
for (auto& pkg : pm->listUpdates()) {
pkg.provider = pm->getName();
results.push_back(pkg);
}
} else {
if (!pm->canListCleanup()) continue;
for (auto& pkg : pm->listCleanupCandidates()) {
pkg.provider = pm->getName();
results.push_back(pkg);
}
}
}
// One row per package name, whatever the number of providers offering it.
mergeProviderDuplicates(results);
// Cleanup candidates stay as reported: each row is tied to the provider
// that must remove it, and merging would hide which one reported it.
if (m_mode != Cleanup) mergeProviderDuplicates(results);
emit finished(results);
}
InstallWorker::InstallWorker(IPackageManager* pm, Op op, const std::string& package)
: m_providers{pm}, m_op(op), m_package(package) {}
InstallWorker::InstallWorker(IPackageManager* pm, Op op,
const std::vector<std::string>& packages)
: m_providers{pm}, m_op(op), m_packages(packages) {}
InstallWorker::InstallWorker(const std::vector<IPackageManager*>& providers, Op op)
: m_providers(providers), m_op(op) {}
@@ -134,6 +146,10 @@ void InstallWorker::run() {
case Op::Update: success = pm->update(m_package, cb) && success; break;
case Op::UpdateAll: success = pm->update("", cb) && success; break;
case Op::Refresh: success = pm->refresh(cb) && success; break;
case Op::RemovePackages:
success = pm->removePackages(m_packages, cb) && success;
break;
case Op::CleanCache: success = pm->cleanCache(cb) && success; break;
}
}
emit finished(success);
@@ -304,6 +320,7 @@ void MainWindow::setupUI() {
setupHistoryPage();
setupLogsPage();
setupPluginsPage();
setupCleanupPage();
contentLayout->addWidget(m_pages, 1);
rootLayout->addWidget(content, 1);
@@ -539,6 +556,8 @@ void MainWindow::setupSidebar() {
addNavButton(NavSearch, "search", "Recherche", "Rechercher des paquets");
addNavButton(NavInstalled, "package", "Installés", "Voir les paquets installés");
addNavButton(NavUpdates, "arrow-up", "Mises à jour", "Voir les mises à jour disponibles");
addNavButton(NavCleanup, "broom", "Nettoyage",
"Purger les paquets inutilisés ou legacy et vider les caches");
layout->addSpacing(8);
addNavButton(NavHistory, "history", "Historique", "Historique des actions");
addNavButton(NavLogs, "logs", "Logs", "Logs pacman");
@@ -633,6 +652,14 @@ void MainWindow::setupDashboardPage() {
connect(btnUpdateAll, &QPushButton::clicked, this, &MainWindow::onUpdateAll);
actionsRow->addWidget(btnUpdateAll);
QPushButton* btnCleanup = new QPushButton("Nettoyer le système");
btnCleanup->setIcon(loadIcon("broom"));
btnCleanup->setToolTip("Purger les paquets inutilisés et vider les caches");
connect(btnCleanup, &QPushButton::clicked, this, [this]() {
m_navGroup->button(NavCleanup)->click();
});
actionsRow->addWidget(btnCleanup);
QPushButton* btnRefresh = new QPushButton("Rafraîchir les sources");
btnRefresh->setProperty("variant", "info");
btnRefresh->setIcon(loadIcon("refresh"));
@@ -886,7 +913,8 @@ void MainWindow::setupHistoryPage() {
filterRow->addWidget(filterLabel);
m_historyFilter = new QComboBox();
m_historyFilter->addItems({"Toutes les actions", "Installations", "Suppressions", "Mises à jour"});
m_historyFilter->addItems({"Toutes les actions", "Installations", "Suppressions",
"Mises à jour", "Nettoyages"});
connect(m_historyFilter, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &MainWindow::onHistoryFilterChanged);
filterRow->addWidget(m_historyFilter);
@@ -1058,6 +1086,103 @@ void MainWindow::setupPluginsPage() {
m_pages->addWidget(page);
}
// Maintenance page: lists what a cleanup can reclaim (orphans and legacy
// packages reported by every provider) and frees the disk space the providers
// hold in their caches. Nothing runs until the user asks for it: the candidate
// list is only built on demand, since both queries cost a pacman call.
void MainWindow::setupCleanupPage() {
QWidget* page = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(page);
layout->setContentsMargins(24, 20, 24, 20);
layout->setSpacing(12);
QFrame* hintCard = makeCard();
QVBoxLayout* hintLayout = new QVBoxLayout(hintCard);
hintLayout->setContentsMargins(18, 16, 18, 16);
hintLayout->setSpacing(4);
QLabel* hintTitle = new QLabel("PAQUETS INUTILISÉS ET LEGACY");
hintTitle->setObjectName("cardTitle");
hintLayout->addWidget(hintTitle);
QLabel* hint = new QLabel(
"Les <b>orphelins</b> ont été installés comme dépendances et ne sont "
"plus requis par aucun paquet : ils peuvent être supprimés sans risque. "
"Les paquets <b>étrangers</b> viennent de l'AUR ou d'une compilation "
"manuelle : vérifiez-les un par un avant de les supprimer.");
hint->setTextFormat(Qt::RichText);
hint->setWordWrap(true);
hint->setStyleSheet("font-size: 12px; color: #7a8299;");
hintLayout->addWidget(hint);
layout->addWidget(hintCard);
m_cleanupTable = new QTableWidget(0, 4);
m_cleanupTable->setHorizontalHeaderLabels(
{"Paquet", "Version", "Type", "Fournisseur"});
m_cleanupTable->verticalHeader()->setVisible(false);
m_cleanupTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_cleanupTable->setSelectionMode(QAbstractItemView::SingleSelection);
m_cleanupTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_cleanupTable->setShowGrid(false);
m_cleanupTable->setAlternatingRowColors(true);
m_cleanupTable->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_cleanupTable->verticalHeader()->setDefaultSectionSize(36);
m_cleanupTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
m_cleanupTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
m_cleanupTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
m_cleanupTable->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
connect(m_cleanupTable, &QTableWidget::currentCellChanged, this,
[this](int currentRow, int, int, int) { onCleanupSelectionChanged(currentRow); });
layout->addWidget(m_cleanupTable, 1);
QHBoxLayout* buttonBar = new QHBoxLayout();
buttonBar->setSpacing(8);
QPushButton* scanButton = new QPushButton("Analyser");
scanButton->setProperty("variant", "primary");
scanButton->setIcon(loadIcon("search"));
scanButton->setToolTip("Rechercher les paquets inutilisés et legacy");
connect(scanButton, &QPushButton::clicked, this, &MainWindow::onCleanupScan);
buttonBar->addWidget(scanButton);
m_cleanupRemoveOrphansButton = new QPushButton("Supprimer les orphelins");
m_cleanupRemoveOrphansButton->setProperty("variant", "danger");
m_cleanupRemoveOrphansButton->setIcon(loadIcon("trash"));
m_cleanupRemoveOrphansButton->setEnabled(false);
m_cleanupRemoveOrphansButton->setToolTip(
"Supprimer tous les paquets que plus rien ne requiert");
connect(m_cleanupRemoveOrphansButton, &QPushButton::clicked,
this, &MainWindow::onRemoveCleanupOrphans);
buttonBar->addWidget(m_cleanupRemoveOrphansButton);
m_cleanupRemoveSelectionButton = new QPushButton("Supprimer la sélection");
m_cleanupRemoveSelectionButton->setIcon(loadIcon("trash"));
m_cleanupRemoveSelectionButton->setEnabled(false);
connect(m_cleanupRemoveSelectionButton, &QPushButton::clicked,
this, &MainWindow::onRemoveCleanupSelection);
buttonBar->addWidget(m_cleanupRemoveSelectionButton);
m_cleanupCacheButton = new QPushButton("Vider les caches");
m_cleanupCacheButton->setProperty("variant", "warning");
m_cleanupCacheButton->setIcon(loadIcon("broom"));
m_cleanupCacheButton->setToolTip(
"Purger le cache de paquets (paccache / pacman -Sc) et les runtimes "
"Flatpak inutilisés");
connect(m_cleanupCacheButton, &QPushButton::clicked, this, &MainWindow::onCleanCaches);
buttonBar->addWidget(m_cleanupCacheButton);
buttonBar->addStretch();
m_cleanupCount = new QLabel("");
m_cleanupCount->setStyleSheet("font-size: 12px; color: #7a8299; font-weight: 600;");
buttonBar->addWidget(m_cleanupCount);
layout->addLayout(buttonBar);
m_pages->addWidget(page);
}
void MainWindow::setupMenuBar() {
QMenuBar* menuBar = this->menuBar();
menuBar->setStyleSheet(
@@ -1094,6 +1219,8 @@ void MainWindow::setupMenuBar() {
[this]() { m_navGroup->button(NavInstalled)->click(); });
toolsMenu->addAction("Mises à jour", this,
[this]() { m_navGroup->button(NavUpdates)->click(); });
toolsMenu->addAction("Nettoyage", this,
[this]() { m_navGroup->button(NavCleanup)->click(); });
toolsMenu->addAction("Historique", this,
[this]() { m_navGroup->button(NavHistory)->click(); });
toolsMenu->addAction("Logs pacman", this,
@@ -1312,6 +1439,11 @@ void MainWindow::updatePageTitle() {
m_headerTitle->setText("Mises à jour disponibles");
m_headerSubtitle->setText("Maintenez votre système à jour");
break;
case NavCleanup:
m_headerTitle->setText("Nettoyage");
m_headerSubtitle->setText(
"Purger les paquets inutilisés ou legacy et libérer de l'espace");
break;
case NavHistory:
m_headerTitle->setText("Historique");
m_headerSubtitle->setText("Journal de toutes vos actions");
@@ -1350,6 +1482,10 @@ void MainWindow::onNavChanged(int id) {
setBrowseMode(ModeUpdates);
loadUpdates();
break;
case NavCleanup:
m_pages->setCurrentIndex(PageCleanup);
loadCleanup();
break;
case NavHistory:
m_pages->setCurrentIndex(PageHistory);
refreshHistoryTable();
@@ -1856,6 +1992,276 @@ void MainWindow::onRefresh() {
worker->start();
}
void MainWindow::onCleanupScan() {
loadCleanup();
}
void MainWindow::loadCleanup() {
std::vector<IPackageManager*> providers = m_pluginManager->availableManagers();
if (providers.empty()) {
statusBar()->showMessage("Aucun gestionnaire de paquets disponible.");
return;
}
setBusy(true);
statusBar()->showMessage("Analyse des paquets inutilisés et legacy...");
m_cleanupTable->setRowCount(0);
m_cleanupCandidates.clear();
m_cleanupRemoveOrphansButton->setEnabled(false);
m_cleanupRemoveSelectionButton->setEnabled(false);
auto* worker = new ListWorker(providers, ListWorker::Cleanup);
connect(worker, &ListWorker::finished, this, &MainWindow::onCleanupListFinished);
connect(worker, &ListWorker::finished, this, [worker] { worker->wait(); worker->deleteLater(); });
worker->start();
}
void MainWindow::onCleanupListFinished(std::vector<PackageInfo> results) {
setBusy(false);
populateCleanupTable(results);
int orphans = 0;
for (const auto& pkg : results) {
if (pkg.orphan) ++orphans;
}
statusBar()->showMessage(
QString("%1 orphelin(s) et %2 paquet(s) legacy détecté(s)")
.arg(orphans).arg(static_cast<int>(results.size()) - orphans));
}
void MainWindow::populateCleanupTable(const std::vector<PackageInfo>& candidates) {
m_cleanupCandidates = candidates;
m_cleanupTable->setRowCount(0);
m_cleanupTable->setRowCount(static_cast<int>(candidates.size()));
QFont nameFont;
nameFont.setWeight(QFont::DemiBold);
int orphans = 0;
for (int i = 0; i < static_cast<int>(candidates.size()); ++i) {
const PackageInfo& pkg = candidates[i];
if (pkg.orphan) ++orphans;
QTableWidgetItem* nameItem = new QTableWidgetItem(QString::fromStdString(pkg.name));
nameItem->setFont(nameFont);
m_cleanupTable->setItem(i, 0, nameItem);
m_cleanupTable->setItem(i, 1,
new QTableWidgetItem(QString::fromStdString(pkg.version)));
QTableWidgetItem* typeItem =
new QTableWidgetItem(QString::fromStdString(pkg.cleanupReason));
typeItem->setForeground(QColor(pkg.orphan ? "#e0af68" : "#7a8299"));
typeItem->setIcon(loadIcon(pkg.orphan ? "alert" : "package"));
m_cleanupTable->setItem(i, 2, typeItem);
m_cleanupTable->setItem(i, 3,
new QTableWidgetItem(providersLabel(m_pluginManager, pkg)));
}
m_cleanupCount->setText(QString("%1 orphelin(s) · %2 legacy")
.arg(orphans).arg(static_cast<int>(candidates.size()) - orphans));
m_cleanupRemoveOrphansButton->setEnabled(orphans > 0);
m_cleanupRemoveOrphansButton->setText(orphans > 0
? QString("Supprimer les orphelins (%1)").arg(orphans)
: QString("Supprimer les orphelins"));
if (!candidates.empty()) m_cleanupTable->selectRow(0);
onCleanupSelectionChanged(m_cleanupTable->currentRow());
}
void MainWindow::onCleanupSelectionChanged(int row) {
if (row < 0 || row >= static_cast<int>(m_cleanupCandidates.size())) {
m_cleanupRemoveSelectionButton->setEnabled(false);
m_cleanupRemoveSelectionButton->setToolTip(QString());
return;
}
const PackageInfo& pkg = m_cleanupCandidates[row];
const QString name = QString::fromStdString(pkg.name);
m_cleanupRemoveSelectionButton->setEnabled(true);
// A legacy package is only ever removed one by one and with an explicit
// warning: nothing on the system proves it is unused.
m_cleanupRemoveSelectionButton->setToolTip(
pkg.orphan
? QString("Supprimer « %1 » (orphelin)").arg(name)
: QString("Supprimer « %1 » (%2) — à vérifier : rien ne prouve "
"qu'il est inutilisé")
.arg(name, QString::fromStdString(pkg.cleanupReason)));
}
void MainWindow::onRemoveCleanupOrphans() {
std::vector<PackageInfo> orphans;
for (const auto& pkg : m_cleanupCandidates) {
if (pkg.orphan) orphans.push_back(pkg);
}
if (orphans.empty()) {
statusBar()->showMessage("Aucun orphelin à supprimer.");
return;
}
removeCleanupPackages(orphans, QString("%1 orphelin(s)").arg(orphans.size()));
}
void MainWindow::onRemoveCleanupSelection() {
const int row = m_cleanupTable->currentRow();
if (row < 0 || row >= static_cast<int>(m_cleanupCandidates.size())) return;
// Copied: the candidate list is rebuilt once the removal completes.
const PackageInfo pkg = m_cleanupCandidates[row];
removeCleanupPackages({pkg},
QString("« %1 »").arg(QString::fromStdString(pkg.name)));
}
// Groups the candidates per provider (each provider then gets a single removal
// command), confirms once, and hands over to the sequential runner below.
void MainWindow::removeCleanupPackages(const std::vector<PackageInfo>& packages,
const QString& description) {
m_pendingRemovals.clear();
for (const auto& pkg : packages) {
IPackageManager* provider = providerFor(pkg.provider);
if (!provider || !provider->canRemove()) continue;
auto group = std::find_if(
m_pendingRemovals.begin(), m_pendingRemovals.end(),
[provider](const auto& entry) { return entry.first == provider; });
if (group == m_pendingRemovals.end()) {
m_pendingRemovals.push_back({provider, {pkg.name}});
} else {
group->second.push_back(pkg.name);
}
}
if (m_pendingRemovals.empty()) {
statusBar()->showMessage("Aucun paquet sélectionné n'est supprimable.");
return;
}
if (QMessageBox::question(this, "Nettoyage",
QString("Supprimer %1 ?\n\nCette action est irréversible.")
.arg(description)) != QMessageBox::Yes) {
m_pendingRemovals.clear();
return;
}
std::vector<IPackageManager*> providers;
for (const auto& entry : m_pendingRemovals) providers.push_back(entry.first);
if (!tryAcquireOp()) {
m_pendingRemovals.clear();
return;
}
if (!ensureCredentials(providers)) {
setBusy(false);
m_pendingRemovals.clear();
return;
}
startNextCleanupRemoval();
}
// Removes the next provider's group of packages, then chains into the
// following one. Two removals never run at once: pacman locks its database for
// the whole operation.
void MainWindow::startNextCleanupRemoval() {
if (m_pendingRemovals.empty()) {
setBusy(false);
clearCredentialsAfterUse();
m_detailsCache.clear();
loadCleanup();
refreshDashboard();
return;
}
const auto job = m_pendingRemovals.front();
m_pendingRemovals.erase(m_pendingRemovals.begin());
IPackageManager* provider = job.first;
const std::vector<std::string> packages = job.second;
statusBar()->showMessage(
QString("Suppression de %1 paquet(s)...").arg(packages.size()));
auto* worker = new InstallWorker(provider, InstallWorker::Op::RemovePackages, packages);
connect(worker, &InstallWorker::progress, this, [this](const QString& msg) {
statusBar()->showMessage(msg);
});
connect(worker, &InstallWorker::progressValue, this, &MainWindow::onInstallProgress);
connect(worker, &InstallWorker::finished, this,
[this, provider, packages](bool success) {
if (success) {
statusBar()->showMessage(
QString("%1 paquet(s) supprimé(s)").arg(packages.size()));
} else {
statusBar()->showMessage("Échec du nettoyage");
showProviderError("Échec du nettoyage", provider);
}
// One history row per removed package keeps the install/remove filters
// of the History page meaningful.
for (const auto& name : packages) {
m_historyManager->record({"", "remove", name, provider->getName(), success});
}
startNextCleanupRemoval();
});
connect(worker, &InstallWorker::finished, this, [worker] { worker->wait(); worker->deleteLater(); });
worker->start();
}
void MainWindow::onCleanCaches() {
std::vector<IPackageManager*> providers;
QStringList names;
for (IPackageManager* pm : m_pluginManager->availableManagers()) {
if (!pm->canCleanCache()) continue;
providers.push_back(pm);
names << QString::fromStdString(pm->getName());
}
if (providers.empty()) {
statusBar()->showMessage("Aucun cache à nettoyer sur ce système.");
return;
}
if (QMessageBox::question(this, "Nettoyage",
QString("Vider les caches de %1 ?\n\npacman ne conserve que la "
"dernière version de chaque paquet téléchargé et Flatpak "
"supprime les runtimes qu'aucune application n'utilise "
"plus.").arg(names.join(", "))) != QMessageBox::Yes) {
return;
}
if (!tryAcquireOp()) return;
if (!ensureCredentials(providers)) {
setBusy(false);
return;
}
statusBar()->showMessage("Nettoyage des caches...");
auto* worker = new InstallWorker(providers, InstallWorker::Op::CleanCache);
connect(worker, &InstallWorker::progress, this, [this](const QString& msg) {
statusBar()->showMessage(msg);
});
connect(worker, &InstallWorker::progressValue, this, &MainWindow::onInstallProgress);
connect(worker, &InstallWorker::finished, this, [this, providers](bool success) {
setBusy(false);
if (success) {
statusBar()->showMessage("Caches nettoyés");
} else {
statusBar()->showMessage("Échec du nettoyage des caches");
for (IPackageManager* pm : providers) {
if (!pm->lastError().empty()) {
showProviderError("Échec du nettoyage des caches", pm);
break;
}
}
}
for (IPackageManager* pm : providers) {
m_historyManager->record({"", "cleanup", "caches", pm->getName(), success});
}
clearCredentialsAfterUse();
refreshDashboard();
});
connect(worker, &InstallWorker::finished, this, [worker] { worker->wait(); worker->deleteLater(); });
worker->start();
}
void MainWindow::performInstall(const std::string& package) {
IPackageManager* provider = providerFor(m_selectedPackage.provider);
if (!provider || !provider->canInstall()) return;
@@ -1999,7 +2405,8 @@ void MainWindow::refreshHistoryTable() {
bool match = filter == 0
|| (filter == 1 && entry.action == "install")
|| (filter == 2 && entry.action == "remove")
|| (filter == 3 && entry.action == "update");
|| (filter == 3 && entry.action == "update")
|| (filter == 4 && entry.action == "cleanup");
if (match) entries.push_back(entry);
}
@@ -2016,6 +2423,9 @@ void MainWindow::refreshHistoryTable() {
} else if (entry.action == "remove") {
actionText = "Suppression";
actionColor = "#f7768e";
} else if (entry.action == "cleanup") {
actionText = "Nettoyage";
actionColor = "#56b6c2";
} else {
actionText = "Mise à jour";
actionColor = "#e0af68";
@@ -2175,6 +2585,8 @@ void MainWindow::refreshRecentActivity() {
actionText = "Installation"; actionBg = "#12301f"; actionFg = "#3fb950";
} else if (entry.action == "remove") {
actionText = "Suppression"; actionBg = "#3a1a1a"; actionFg = "#f7768e";
} else if (entry.action == "cleanup") {
actionText = "Nettoyage"; actionBg = "#12333a"; actionFg = "#56b6c2";
} else {
actionText = "Mise à jour"; actionBg = "#3d2f0f"; actionFg = "#e0af68";
}