All the business logic now lives in AppCore (providers, workers, history, plugins, sudo and pacman-lock handling), freed from any widget so it runs headless and is testable on its own. The Qt window becomes a thin frame around one file per screen under pages/, wired through a common Page contract and a shared ui-kit. CMake gains the app-core-test target, which links Qt Core only to enforce the headless guarantee. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
235 lines
8.7 KiB
C++
235 lines
8.7 KiB
C++
#include "cleanup-page.h"
|
|
|
|
#include <QColor>
|
|
#include <QFrame>
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QMessageBox>
|
|
#include <QPushButton>
|
|
#include <QStackedWidget>
|
|
#include <QTableWidget>
|
|
#include <QVBoxLayout>
|
|
|
|
#include "ui-kit.h"
|
|
|
|
CleanupPage::CleanupPage(AppCore* core, QWidget* parent) : Page(core, parent) {
|
|
QVBoxLayout* layout = new QVBoxLayout(this);
|
|
layout->setContentsMargins(24, 20, 24, 20);
|
|
layout->setSpacing(12);
|
|
|
|
QFrame* hintCard = ui::card();
|
|
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_table = ui::table({"Paquet", "Version", "Type", "Fournisseur"});
|
|
connect(m_table, &QTableWidget::currentCellChanged, this,
|
|
[this](int currentRow, int, int, int) { onSelectionChanged(currentRow); });
|
|
m_tableStack = ui::withEmptyState(
|
|
m_table,
|
|
"Rien à nettoyer pour le moment.\nLancez une analyse pour vérifier.");
|
|
layout->addWidget(m_tableStack, 1);
|
|
|
|
QHBoxLayout* buttonBar = new QHBoxLayout();
|
|
buttonBar->setSpacing(8);
|
|
|
|
QPushButton* scanButton = new QPushButton("Analyser");
|
|
scanButton->setProperty("variant", "primary");
|
|
scanButton->setIcon(ui::icon("search"));
|
|
scanButton->setToolTip("Rechercher les paquets inutilisés et legacy");
|
|
connect(scanButton, &QPushButton::clicked, this, &CleanupPage::scan);
|
|
buttonBar->addWidget(scanButton);
|
|
|
|
m_removeOrphansButton = new QPushButton("Supprimer les orphelins");
|
|
m_removeOrphansButton->setProperty("variant", "danger");
|
|
m_removeOrphansButton->setIcon(ui::icon("trash"));
|
|
m_removeOrphansButton->setEnabled(false);
|
|
m_removeOrphansButton->setToolTip(
|
|
"Supprimer tous les paquets que plus rien ne requiert");
|
|
connect(m_removeOrphansButton, &QPushButton::clicked, this,
|
|
&CleanupPage::removeOrphans);
|
|
buttonBar->addWidget(m_removeOrphansButton);
|
|
|
|
m_removeSelectionButton = new QPushButton("Supprimer la sélection");
|
|
m_removeSelectionButton->setIcon(ui::icon("trash"));
|
|
m_removeSelectionButton->setEnabled(false);
|
|
connect(m_removeSelectionButton, &QPushButton::clicked, this,
|
|
&CleanupPage::removeSelection);
|
|
buttonBar->addWidget(m_removeSelectionButton);
|
|
|
|
m_cacheButton = new QPushButton("Vider les caches");
|
|
m_cacheButton->setProperty("variant", "warning");
|
|
m_cacheButton->setIcon(ui::icon("broom"));
|
|
m_cacheButton->setToolTip(
|
|
"Purger le cache de paquets (paccache / pacman -Sc) et les runtimes "
|
|
"Flatpak inutilisés");
|
|
connect(m_cacheButton, &QPushButton::clicked, this, &CleanupPage::cleanCaches);
|
|
buttonBar->addWidget(m_cacheButton);
|
|
|
|
buttonBar->addStretch();
|
|
|
|
m_count = ui::countLabel();
|
|
buttonBar->addWidget(m_count);
|
|
|
|
layout->addLayout(buttonBar);
|
|
|
|
// The candidate list arrives through listFinished, which every page shares.
|
|
connect(core, &AppCore::listFinished, this,
|
|
[this](AppCore::ListMode mode, std::vector<PackageInfo> results) {
|
|
if (mode == AppCore::ListMode::Cleanup) populate(results);
|
|
});
|
|
}
|
|
|
|
void CleanupPage::refresh() {
|
|
scan();
|
|
}
|
|
|
|
void CleanupPage::scan() {
|
|
m_table->setRowCount(0);
|
|
m_candidates.clear();
|
|
m_removeOrphansButton->setEnabled(false);
|
|
m_removeSelectionButton->setEnabled(false);
|
|
|
|
start(core()->loadCleanup(), tr("Nettoyage"));
|
|
}
|
|
|
|
void CleanupPage::populate(const std::vector<PackageInfo>& candidates) {
|
|
m_candidates = candidates;
|
|
ui::showEmptyState(m_tableStack, candidates.empty());
|
|
|
|
QFont nameFont;
|
|
nameFont.setWeight(QFont::DemiBold);
|
|
|
|
ui::TableFillBatch batch(m_table);
|
|
m_table->setRowCount(0);
|
|
m_table->setRowCount(static_cast<int>(candidates.size()));
|
|
|
|
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_table->setItem(i, 0, nameItem);
|
|
|
|
m_table->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(ui::icon(pkg.orphan ? "alert" : "package"));
|
|
m_table->setItem(i, 2, typeItem);
|
|
|
|
m_table->setItem(i, 3, new QTableWidgetItem(ui::providersLabel(core()->plugins(), pkg)));
|
|
}
|
|
batch.release();
|
|
|
|
m_count->setText(QString("%1 orphelin(s) · %2 legacy")
|
|
.arg(orphans)
|
|
.arg(static_cast<int>(candidates.size()) - orphans));
|
|
m_removeOrphansButton->setEnabled(orphans > 0);
|
|
m_removeOrphansButton->setText(orphans > 0
|
|
? QString("Supprimer les orphelins (%1)").arg(orphans)
|
|
: QString("Supprimer les orphelins"));
|
|
|
|
if (!candidates.empty()) m_table->selectRow(0);
|
|
onSelectionChanged(m_table->currentRow());
|
|
}
|
|
|
|
void CleanupPage::onSelectionChanged(int row) {
|
|
if (row < 0 || row >= static_cast<int>(m_candidates.size())) {
|
|
m_removeSelectionButton->setEnabled(false);
|
|
m_removeSelectionButton->setToolTip(QString());
|
|
return;
|
|
}
|
|
|
|
const PackageInfo& pkg = m_candidates[row];
|
|
const QString name = QString::fromStdString(pkg.name);
|
|
m_removeSelectionButton->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_removeSelectionButton->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 CleanupPage::removeOrphans() {
|
|
std::vector<PackageInfo> orphans;
|
|
for (const auto& pkg : m_candidates) {
|
|
if (pkg.orphan) orphans.push_back(pkg);
|
|
}
|
|
if (orphans.empty()) {
|
|
emit message(tr("Aucun orphelin à supprimer."));
|
|
return;
|
|
}
|
|
removePackages(orphans, QString("%1 orphelin(s)").arg(orphans.size()));
|
|
}
|
|
|
|
void CleanupPage::removeSelection() {
|
|
const int row = m_table->currentRow();
|
|
if (row < 0 || row >= static_cast<int>(m_candidates.size())) return;
|
|
|
|
// Copied: the candidate list is rebuilt once the removal completes.
|
|
const PackageInfo pkg = m_candidates[row];
|
|
removePackages({pkg}, QString("« %1 »").arg(QString::fromStdString(pkg.name)));
|
|
}
|
|
|
|
void CleanupPage::removePackages(const std::vector<PackageInfo>& packages,
|
|
const QString& description) {
|
|
if (packages.empty()) {
|
|
emit message(tr("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) {
|
|
return;
|
|
}
|
|
|
|
start(core()->removePackages(packages, description), tr("Nettoyage"));
|
|
}
|
|
|
|
void CleanupPage::cleanCaches() {
|
|
// The core owns which providers have a cache; the page only names them in
|
|
// the confirmation it shows before the purge.
|
|
QStringList names;
|
|
for (IPackageManager* pm : core()->availableProviders()) {
|
|
if (pm->canCleanCache()) names << QString::fromStdString(pm->getName());
|
|
}
|
|
if (names.isEmpty()) {
|
|
emit message(tr("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;
|
|
}
|
|
|
|
start(core()->cleanCaches(), tr("Nettoyage des caches"));
|
|
}
|