#pragma once #include #include #include #include #include #include // One *installation* of a package. The same application can be present several // times over — flatpak installs one copy in the user installation and another // system-wide, or two different remotes or branches of the same id — and acting // on "the package" is then ambiguous: flatpak refuses ("Multiple installed refs // match …"). A provider that can have several fills `instances` so the view can // list them and the user can choose the ones to act on. struct PackageInstance { // What the user reads: "utilisateur · flathub · stable". std::string label; // What the provider needs to act on exactly that one, in its own syntax // ("user:com.example.App/x86_64/stable" for flatpak). The provider is the // only one that understands it, which is why it is never shown and never // rebuilt by the view. std::string target; }; // 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 providers; // Every installation of this package, when the provider can have more than // one (see PackageInstance). Empty means a single installation, which is // what every provider but flatpak reports. std::vector instances; 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& 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 firstIndex; std::vector 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; // The installations of both rows belong to the merged one: that is how a // package installed system-wide *and* per-user still shows the two // copies the user can choose from. for (const PackageInstance& instance : dup.instances) { bool known = false; for (const PackageInstance& existing : first.instances) { if (existing.target == instance.target) { known = true; break; } } if (!known) first.instances.push_back(instance); } 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(*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& packages, const std::vector& 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(std::tolower(static_cast(a[i]))); unsigned char cb = static_cast(std::tolower(static_cast(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(i); } return static_cast(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; // 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 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 listInstalled() = 0; virtual std::vector 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 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& 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; } // ---- Installation from a file (optional) ---- // Providers that can install a package straight from a file — the Flatpak // bundle formats, typically — override these, so the app can act on what // the user drops on the window or opens from the file manager ("Ouvrir // avec PacManager"). A provider only claims the formats it owns. // Whether this provider understands that file (or URL). virtual bool canInstallFile(const std::string& path) const { (void)path; return false; } // Install the package the file describes. Returns false when the install // failed; the reason is then in lastError(). virtual bool installFile(const std::string& path, ProgressCallback progressCallback = nullptr) { (void)path; (void)progressCallback; return false; } // The application a file would install, as this provider names it // internally ("com.example.App" for flatpak): a reference file spells it // out, a bundle embeds it. Empty when the format is not this provider's, // when the file names no single application (a .flatpakrepo only defines a // remote), or when nothing can be read from it. Read without running // anything, so the app can tell the user a file is already installed // instead of starting an install that would do nothing. virtual std::string fileTargetName(const std::string& path) const { (void)path; return {}; } // Whether the provider already has that application, in any of its // installations. A single lookup through the provider's own tool, cheap // enough to run while the user is dropping a file. virtual bool isPackageInstalled(const std::string& packageName) const { (void)packageName; 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 {}; } };