Files
FWS-PacManage/include/package-manager.h
T
BlackAngelTV d8a70eabf8 feat(ui): merge duplicate packages across providers
When the same package name comes from several providers (pacman, flatpak,
plugins), collapse the rows into one: the Fournisseur column lists every
provider offering it and installed/updateAvailable are combined, while
the first provider stays the routing default. Applies to search and to
the aggregated installed/updates lists.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-08-29 13:55:56 +02:00

112 lines
4.1 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <functional>
#include <map>
// 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;
};
// 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));
}
}
// 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,
std::function<void(const std::string&)> progressCallback = nullptr) = 0;
virtual bool remove(const std::string& packageName,
std::function<void(const std::string&)> progressCallback = nullptr) = 0;
virtual bool update(const std::string& packageName = "",
std::function<void(const std::string&)> progressCallback = nullptr) = 0;
virtual std::vector<PackageInfo> listInstalled() = 0;
virtual std::vector<PackageInfo> listUpdates() = 0;
virtual bool refresh(std::function<void(const std::string&)> progressCallback = nullptr) = 0;
// 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; }
};