The app kept no durable trace of what it ran: LogManager existed but was never instantiated, so only a handful of console-only qCInfo calls survived a session. Wire it up as a process-wide journal that captures each command's raw output next to UI, search, plugin and update events, and funnel every Qt log message into it, so a failure can still be diagnosed after the fact. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
261 lines
11 KiB
C++
261 lines
11 KiB
C++
#pragma once
|
|
|
|
#include <QHash>
|
|
#include <QList>
|
|
#include <QObject>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
|
|
#include <optional>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "history-manager.h"
|
|
#include "package-manager.h"
|
|
#include "pacman-manager.h"
|
|
#include "plugin-manager.h"
|
|
#include "update-manager.h"
|
|
|
|
class QThread;
|
|
class SearchWorker;
|
|
class ListWorker;
|
|
class InstallWorker;
|
|
class PluginShop;
|
|
|
|
// Logic layer: everything the application *does*. It owns the providers
|
|
// (pacman, Flatpak, AUR, ...), the plugin registry, the history, the privileged
|
|
// commands, pacman's database lock and the self-updater — and knows nothing
|
|
// about widgets, dialogs or layouts.
|
|
//
|
|
// MainWindow is only a view on top of it: it builds widgets, calls these
|
|
// methods and renders the signals below. Two rules keep the split honest:
|
|
// - every operation that shells out is asynchronous and reports through
|
|
// signals; the *StartResult answers only say whether the request was
|
|
// accepted;
|
|
// - whatever needs a human (the sudo password, freeing pacman's lock) is
|
|
// decided by the view: the core says what it lacks and the view calls back
|
|
// once the user answered.
|
|
// The layer therefore runs without a display — tests/app-core.test.cpp links no
|
|
// QtWidgets at all.
|
|
class AppCore : public QObject {
|
|
Q_OBJECT
|
|
public:
|
|
// Which listing is being loaded.
|
|
enum class ListMode { Installed, Updates, Cleanup };
|
|
// When the sudo password is requested: once per session (at launch) or
|
|
// before every privileged operation.
|
|
enum class CredentialMode { AtLaunch, EachTime };
|
|
|
|
// Outcome of a request. A refused request carries the reason and flags what
|
|
// the view has to do before replaying it: ask for the sudo password, or
|
|
// free pacman's database lock.
|
|
struct StartResult {
|
|
bool started = false;
|
|
bool needsPassword = false;
|
|
bool locked = false;
|
|
QString error;
|
|
};
|
|
|
|
explicit AppCore(QObject* parent = nullptr);
|
|
~AppCore() override;
|
|
|
|
// ---- Backend handles the view renders ----
|
|
PluginManager* plugins() const { return m_plugins; }
|
|
HistoryManager* history() const { return m_history; }
|
|
UpdateManager* updateManager() const { return m_update; }
|
|
PluginShop* shop() const { return m_shop; }
|
|
std::vector<IPackageManager*> availableProviders() const;
|
|
QStringList availableProviderNames() const;
|
|
bool hasAvailableProvider() const;
|
|
// Provider owning a package id, falling back to the first available one.
|
|
IPackageManager* providerFor(const QString& providerId) const;
|
|
QString version() const;
|
|
int historyCount() const;
|
|
bool busy() const { return m_busy; }
|
|
|
|
// Last completed listings; -1 when a list was never loaded.
|
|
const std::vector<PackageInfo>& installedPackages() const { return m_installedPackages; }
|
|
const std::vector<PackageInfo>& updatePackages() const { return m_updatePackages; }
|
|
const std::vector<PackageInfo>& cleanupCandidates() const { return m_cleanupCandidates; }
|
|
int installedCount() const;
|
|
int updateCount() const;
|
|
|
|
// ---- Listings ----
|
|
StartResult search(const QString& query);
|
|
StartResult loadInstalled();
|
|
StartResult loadUpdates();
|
|
StartResult loadCleanup();
|
|
// Dashboard: loads the installed and update lists back to back, so the
|
|
// counts and the badges are fresh without the view doing any chaining.
|
|
StartResult loadCounts();
|
|
|
|
// ---- Actions ----
|
|
StartResult install(const PackageInfo& package);
|
|
StartResult remove(const PackageInfo& package);
|
|
StartResult update(const PackageInfo& package);
|
|
StartResult updateAll();
|
|
StartResult refresh();
|
|
StartResult cleanCaches();
|
|
// Bulk removal: one command per provider, run one provider at a time
|
|
// (pacman locks its database for the whole operation).
|
|
StartResult removePackages(const std::vector<PackageInfo>& packages,
|
|
const QString& description);
|
|
StartResult installPluginTool(const std::string& pluginId);
|
|
StartResult releaseDatabaseLock();
|
|
|
|
// Replays the request that was waiting for a password or for the database
|
|
// lock to be freed, so the view can retry right after the user answered.
|
|
StartResult retryPendingRequest();
|
|
void cancelPendingRequest();
|
|
bool hasPendingRequest() const { return m_pending.has_value(); }
|
|
|
|
// ---- Credentials (the view prompts, the core holds the state) ----
|
|
CredentialMode credentialMode() const { return m_credentialMode; }
|
|
void setCredentialMode(CredentialMode mode);
|
|
void setSudoPassword(const QString& password);
|
|
void clearSudoPassword();
|
|
bool hasSudoPassword() const;
|
|
|
|
// ---- History, logs, package details ----
|
|
void clearHistory();
|
|
// Full content of /var/log/pacman.log; `error` is filled when it cannot be
|
|
// read (file missing, no permission).
|
|
QString readPackageLog(QString* error = nullptr) const;
|
|
// Full content of the application's own journal (every operation and event,
|
|
// including the raw output of the commands that ran).
|
|
QString readAppLog(QString* error = nullptr) const;
|
|
// Extra pacman information for the details panel (-Qi / -Si), cached per
|
|
// package so selecting rows repeatedly does not re-spawn pacman.
|
|
QString packageDetails(const PackageInfo& package);
|
|
|
|
// ---- Plugins ----
|
|
void setPluginEnabled(const std::string& pluginId, bool enabled);
|
|
void reloadPlugins();
|
|
|
|
// ---- Plugin shop ----
|
|
// Fetches the catalogue the shop offers and installs/removes one of its
|
|
// entries. Installing drops the shared library in the per-user plugin
|
|
// directory and reloads the registry, so the plugin is usable right away.
|
|
void loadPluginCatalog();
|
|
void installShopPlugin(const QString& id);
|
|
void removeShopPlugin(const QString& id);
|
|
|
|
// ---- Application self-update ----
|
|
bool shouldCheckForAppUpdate() const;
|
|
void checkForAppUpdate();
|
|
bool canSelfUpdate() const;
|
|
UpdateManager::UpdateMethod updateMethod() const;
|
|
QString updateMethodDescription() const;
|
|
QString releasePageUrl() const;
|
|
QString binDir() const;
|
|
bool binDirWritable() const;
|
|
// Starts the background rebuild/install. Returns false with `error` filled
|
|
// when the update cannot run (no method available).
|
|
bool applySelfUpdate(const QString& sudoPassword, QString* error);
|
|
|
|
// ---- pacman's database lock ----
|
|
bool databaseLocked() const;
|
|
// What currently holds the lock, so the view can name the process it offers
|
|
// to close (or the stale lock file it offers to drop).
|
|
PacmanManager::LockHolder lockHolder() const;
|
|
QString databaseLockDescription() const;
|
|
|
|
signals:
|
|
// Search results, streamed in batches then once complete.
|
|
void searchChunk(std::vector<PackageInfo> results);
|
|
void searchFinished(std::vector<PackageInfo> results);
|
|
// A listing finished; the view renders it and keeps what it needs.
|
|
void listFinished(AppCore::ListMode mode, std::vector<PackageInfo> results);
|
|
// Progress of a running action: `message` is a log line, `percent` is -1
|
|
// when the tool does not report a real percentage.
|
|
void actionProgress(const QString& message, int percent);
|
|
void actionFinished(const QString& action, const QString& name, bool success,
|
|
const QString& error, bool locked);
|
|
void busyChanged(bool busy);
|
|
void statusMessage(const QString& message);
|
|
void historyChanged();
|
|
void pluginsChanged();
|
|
// The shop's catalogue was fetched (or failed to be), and an install or a
|
|
// removal from it finished.
|
|
void pluginCatalogChanged();
|
|
void shopInstallFinished(const QString& id, bool success, const QString& message);
|
|
void updateCheckFinished(bool hasUpdate, const QString& latestVersion,
|
|
const QString& errorMessage);
|
|
void selfUpdateFinished(bool success, const QString& message);
|
|
|
|
private:
|
|
// Everything the core knows how to run. The pending request is kept so it
|
|
// can be replayed once the password is provided or the lock released.
|
|
struct Request {
|
|
QString action;
|
|
PackageInfo package;
|
|
std::vector<PackageInfo> packages;
|
|
QString description;
|
|
std::string pluginId;
|
|
};
|
|
|
|
StartResult startRequest(const Request& request);
|
|
StartResult startList(ListMode mode);
|
|
StartResult startSearch(const QString& query);
|
|
StartResult startAction(InstallWorker* worker, const Request& request,
|
|
const std::vector<IPackageManager*>& providers);
|
|
void finishAction(const Request& request,
|
|
const std::vector<IPackageManager*>& providers, bool success);
|
|
// Deletes a worker once its thread has really stopped. wait() has to run on
|
|
// the core's thread (a worker cannot wait on itself), so the cleanup is
|
|
// connected with the core as context and therefore queued back here.
|
|
void disposeWorker(QThread* worker);
|
|
// Chains the provider groups of a bulk removal, one after the other.
|
|
void startNextRemovalGroup();
|
|
void setBusy(bool busy);
|
|
|
|
// Everything the status bar and the dialogs get to read: the core decides
|
|
// what happened, the view decides how to show it.
|
|
QString startMessage(const QString& action, const QString& label) const;
|
|
QString successMessage(const QString& action, const QString& name) const;
|
|
QString failureMessage(const QString& action, const QString& name) const;
|
|
QString firstError(const std::vector<IPackageManager*>& providers) const;
|
|
void recordHistory(const QString& action, const QString& name,
|
|
IPackageManager* provider, bool success);
|
|
void invalidateDetails();
|
|
void refreshProviderCache();
|
|
|
|
// Refusal shared by every privileged request: the view is told which of the
|
|
// three doors is closed (busy, password, unavailable provider).
|
|
StartResult credentialGate(const std::vector<IPackageManager*>& providers) const;
|
|
// Per-operation mode: the password is single-use, drop it after a command.
|
|
void clearCredentialsAfterUse();
|
|
|
|
PluginManager* m_plugins = nullptr;
|
|
PluginShop* m_shop = nullptr;
|
|
PacmanManager* m_pacman = nullptr; // non-owning, for sudo credentials and the lock
|
|
HistoryManager* m_history = nullptr;
|
|
UpdateManager* m_update = nullptr;
|
|
QSettings m_settings;
|
|
|
|
CredentialMode m_credentialMode = CredentialMode::EachTime;
|
|
|
|
std::vector<PackageInfo> m_installedPackages;
|
|
std::vector<PackageInfo> m_updatePackages;
|
|
std::vector<PackageInfo> m_cleanupCandidates;
|
|
QHash<QString, QString> m_detailsCache;
|
|
|
|
// One worker of each kind at a time; a second listing request is queued
|
|
// rather than refused, since the dashboard loads two in a row.
|
|
SearchWorker* m_search = nullptr;
|
|
ListWorker* m_list = nullptr;
|
|
QList<ListMode> m_queuedLists;
|
|
InstallWorker* m_action = nullptr;
|
|
|
|
std::optional<Request> m_pending;
|
|
|
|
// Bulk removal state.
|
|
std::vector<std::pair<IPackageManager*, std::vector<std::string>>> m_pendingRemovals;
|
|
bool m_removalSuccess = true;
|
|
QString m_cleanupRemovalDescription;
|
|
|
|
bool m_busy = false;
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(AppCore::ListMode)
|