Files
FWS-PacManage/pages/cleanup-page.cpp
T
BlackAngelTV 459a8f045e feat(i18n): wrap the interface and the journal wording in tr()
Every label, tooltip, empty state and status message was a bare
QStringLiteral, so nothing could be translated. They now go through tr()
(QCoreApplication::translate from a free helper, QT_TRANSLATE_NOOP where
the code already had it), which is what the catalogues are keyed on.

The journal follows the same rule as the interface: an event, an error and
the two outcomes of a command read in the chosen language. What the tools
print themselves is left alone — that is their output, not our wording —
and so are the qC* developer traces, which keep their technical English.
2026-09-29 21:17:49 +02:00

235 lines
8.8 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(tr("PAQUETS INUTILISÉS ET LEGACY"));
hintTitle->setObjectName("cardTitle");
hintLayout->addWidget(hintTitle);
QLabel* hint = new QLabel(
tr("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: #94a3b8;");
hintLayout->addWidget(hint);
layout->addWidget(hintCard);
m_table = ui::table({tr("Paquet"), tr("Version"), tr("Type"), tr("Fournisseur")});
connect(m_table, &QTableWidget::currentCellChanged, this,
[this](int currentRow, int, int, int) { onSelectionChanged(currentRow); });
m_tableStack = ui::withEmptyState(
m_table,
tr("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(tr("Analyser"));
scanButton->setProperty("variant", "primary");
scanButton->setIcon(ui::icon("search"));
scanButton->setToolTip(tr("Rechercher les paquets inutilisés et legacy"));
connect(scanButton, &QPushButton::clicked, this, &CleanupPage::scan);
buttonBar->addWidget(scanButton);
m_removeOrphansButton = new QPushButton(tr("Supprimer les orphelins"));
m_removeOrphansButton->setProperty("variant", "danger");
m_removeOrphansButton->setIcon(ui::icon("trash"));
m_removeOrphansButton->setEnabled(false);
m_removeOrphansButton->setToolTip(
tr("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(tr("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(tr("Vider les caches"));
m_cacheButton->setProperty("variant", "warning");
m_cacheButton->setIcon(ui::icon("broom"));
m_cacheButton->setToolTip(
tr("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 ? "#fbbf24" : "#94a3b8"));
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(tr("%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(tr("Supprimer les orphelins (%1)")).arg(orphans)
: QString(tr("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(tr("Supprimer « %1 » (orphelin)")).arg(name)
: QString(tr("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(tr("%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(tr("« %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, tr("Nettoyage"),
QString(tr("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, tr("Nettoyage"),
QString(tr("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"));
}