Files
FWS-PacManage/include/package-manager.h
T
BlackAngelTV 632cdf1dea 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>
2026-09-12 22:17:08 +02:00

202 lines
8.1 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <functional>
#include <map>
#include <algorithm>
#include <cctype>
// Information about a single package
struct PackageInfo {
std::string name;
std::string version;
std::string description;
std::string repository;
std::string size;
std::string installedSize;
// Id of the provider that owns this package (e.g. "pacman", "flatpak").
// Filled by the aggregating workers so operations can be routed to the
// right manager without the user picking one.
std::string provider;
// Every provider offering this package, after duplicate rows have been
// merged (used for display). The routing id stays in `provider`.
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
// a single row. The first occurrence keeps its metadata and provider id (so
// operations keep a deterministic default); later occurrences are folded in:
// installed/updateAvailable become true if any provider reports them, and
// their provider id is added to pkg.providers for display.
inline void mergeProviderDuplicates(std::vector<PackageInfo>& packages) {
// Every tagged package advertises at least its own provider.
for (auto& pkg : packages) {
if (pkg.providers.empty() && !pkg.provider.empty()) {
pkg.providers.push_back(pkg.provider);
}
}
std::map<std::string, std::size_t> firstIndex;
std::vector<std::size_t> toDrop;
for (std::size_t i = 0; i < packages.size(); ++i) {
const std::string& name = packages[i].name;
if (name.empty()) continue;
auto it = firstIndex.find(name);
if (it == firstIndex.end()) {
firstIndex.emplace(name, i);
continue;
}
PackageInfo& first = packages[it->second];
const PackageInfo& dup = packages[i];
first.installed = first.installed || dup.installed;
first.updateAvailable = first.updateAvailable || dup.updateAvailable;
if (!dup.provider.empty()) {
bool known = false;
for (const auto& p : first.providers) {
if (p == dup.provider) {
known = true;
break;
}
}
if (!known) first.providers.push_back(dup.provider);
}
toDrop.push_back(i);
}
for (auto it = toDrop.rbegin(); it != toDrop.rend(); ++it) {
packages.erase(packages.begin() + static_cast<std::ptrdiff_t>(*it));
}
}
// Sort aggregated results by provider, then by name (case-insensitive), so
// packages from the same manager stay grouped. Providers are ordered by their
// position in providerOrder (query order); providers absent from it sort last
// and are compared by id.
inline void sortByProviderThenName(std::vector<PackageInfo>& packages,
const std::vector<std::string>& providerOrder = {}) {
auto ciLess = [](const std::string& a, const std::string& b) {
std::size_t i = 0;
for (; i < a.size() && i < b.size(); ++i) {
unsigned char ca =
static_cast<unsigned char>(std::tolower(static_cast<unsigned char>(a[i])));
unsigned char cb =
static_cast<unsigned char>(std::tolower(static_cast<unsigned char>(b[i])));
if (ca != cb) return ca < cb;
}
return a.size() < b.size();
};
auto rank = [&](const std::string& id) {
for (std::size_t i = 0; i < providerOrder.size(); ++i) {
if (providerOrder[i] == id) return static_cast<int>(i);
}
return static_cast<int>(providerOrder.size());
};
std::stable_sort(packages.begin(), packages.end(),
[&](const PackageInfo& a, const PackageInfo& b) {
int ra = rank(a.provider);
int rb = rank(b.provider);
if (ra != rb) return ra < rb;
if (a.provider != b.provider) return a.provider < b.provider;
return ciLess(a.name, b.name);
});
}
// Progress reported during an operation: a human-readable message plus an
// optional percentage (0-100) when the tool exposes real progress, or -1 when
// the exact position is unknown. -1 lets the UI keep an indeterminate bar.
using ProgressCallback = std::function<void(const std::string& message, int percent)>;
// Abstract interface for the package manager
class IPackageManager {
public:
virtual ~IPackageManager() = default;
// Identity
virtual std::string getName() const = 0;
virtual std::string getVersion() const = 0;
virtual std::string getDescription() const = 0;
// Capabilities
virtual bool canSearch() const = 0;
virtual bool canInstall() const = 0;
virtual bool canRemove() const = 0;
virtual bool canUpdate() const = 0;
virtual bool canListInstalled() const = 0;
// Operations
virtual std::vector<PackageInfo> search(const std::string& query) = 0;
virtual bool install(const std::string& packageName,
ProgressCallback progressCallback = nullptr) = 0;
virtual bool remove(const std::string& packageName,
ProgressCallback progressCallback = nullptr) = 0;
virtual bool update(const std::string& packageName = "",
ProgressCallback progressCallback = nullptr) = 0;
virtual std::vector<PackageInfo> listInstalled() = 0;
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;
// Whether privileged operations need a password (e.g. sudo for pacman).
// Providers that run per-user (e.g. flatpak) can keep the default.
virtual bool requiresPassword() const { return false; }
// Last error reported by the most recent failed operation, if any. Used
// to surface the real command output to the user instead of a generic
// failure message.
virtual std::string lastError() const { return {}; }
};