Files
FWS-PacManage/app/main-window.h
T
BlackAngelTV 5a6aaae244 refactor(tree): give the logic layer and the shell a folder each
src/ and include/ are what is left: the controller the screens delegate
to, its workers and its data model (core/), and the window and entry
point that hold them together (app/). With those moved, src/ and
include/ are empty and go away — a header is now always next to its
source, in a folder named after what it does.
2026-09-29 21:26:04 +02:00

246 lines
9.9 KiB
C++

#pragma once
#include <QElapsedTimer>
#include <QHash>
#include <QMainWindow>
#include <QPalette>
#include <QString>
#include <QStringList>
#include <QSystemTrayIcon>
#include <vector>
#include "app-core.h"
#include "package-manager.h"
class QAction;
class QButtonGroup;
class QDragEnterEvent;
class QDragMoveEvent;
class QDropEvent;
class QFrame;
class QLabel;
class QMimeData;
class QProgressBar;
class QPushButton;
class QStackedWidget;
class QSystemTrayIcon;
class QWidget;
class BrowsePage;
class CleanupPage;
class DashboardPage;
class HistoryPage;
class LogsPage;
class Page;
class PluginsPage;
class SettingsPage;
// The window is only a frame: it owns the sidebar, the header, the status bar,
// the menu, the tray and every dialog, and it stacks one screen per file from
// pages/ inside it. No package-manager command, no privileged operation and no
// pacman query lives here — see include/app-core.h for the logic and
// pages/page.h for the contract a screen honours.
//
// Adding a screen means writing one Page subclass under pages/, registering it
// in setupUI() and giving it a sidebar entry: nothing else in this file has to
// learn about it.
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
~MainWindow();
// Colours the stylesheet cannot reach, applied in main() before any widget
// exists. See the definition for what leaves a light band around a menu.
static QPalette appPalette();
// Installs the Flatpak files handed over by the desktop: dropped on the
// window, or opened from the file manager ("Ouvrir avec PacManager", which
// passes them on the command line). One install runs at a time; the rest
// wait in line. Paths the core does not recognise are reported and skipped.
void installLocalFiles(const QStringList& paths);
protected:
// Dragging a .flatpakref / .flatpakrepo / .flatpak onto the window is the
// same request as opening it from the file manager.
void dragEnterEvent(QDragEnterEvent* event) override;
void dragMoveEvent(QDragMoveEvent* event) override;
void dropEvent(QDropEvent* event) override;
// Language switch: Qt asks every widget to retranslate itself.
void changeEvent(QEvent* event) override;
private slots:
void onNavChanged(int id);
void onRefresh();
void onUpdateAll();
void onTrayActivated(QSystemTrayIcon::ActivationReason reason);
void onCheckForUpdates();
// Ticks once a second while an operation runs: what it does when the
// provider has printed nothing for a while is described in onActivityTick().
void onActivityTick();
// ---- What a page asks the frame to do ----
// A page hands over a request so the frame can walk the user through what
// the core needs (sudo password, freeing pacman's lock) and replay it.
void onStartRequested(const AppCore::StartResult& result, const QString& title);
void onPageMessage(const QString& text);
void onOpenPage(const QString& pageId);
void onOpenPackage(const QString& name);
// ---- What the core reports back ----
void onCoreListFinished(AppCore::ListMode mode, std::vector<PackageInfo> results);
void onCoreActionProgress(const QString& message, int percent);
void onCoreActionFinished(const QString& action, const QString& name, bool success,
const QString& error, bool locked);
void onCoreBusyChanged(bool busy);
void onCoreStatusMessage(const QString& message);
void onCoreHistoryChanged();
void onCoreUpdateCheckFinished(bool hasUpdate, const QString& latestVersion,
const QString& errorMessage);
void onCoreSelfUpdateFinished(bool success, const QString& message);
private:
// Sidebar entries, in display order. Named so the sidebar, the dashboard
// shortcuts, the menu bar and the keyboard shortcuts cannot drift apart
// when a page is inserted.
enum NavPage {
NavDashboard = 0,
NavSearch,
NavInstalled,
NavUpdates,
NavCleanup,
NavHistory,
NavLogs,
NavPlugins,
NavSettings,
};
void setupUI();
void setupSidebar();
void setupHeader();
void setupPages();
void setupMenuBar();
void setupTray();
void setupShortcuts();
// Wires a screen: the frame answers its requests and shows the page it asks
// for. Called once per screen by setupPages().
void registerPage(const QString& id, int navId, Page* page);
// Shows the settings screen, which has no sidebar entry of its own.
void openSettingsPage();
// Screen currently displayed, and the screens themselves.
Page* currentPage() const;
void updatePageTitle();
void updateStatusBar();
// Mirrors the pending update count on the sidebar entry, so the page that
// needs attention is visible from wherever the user is.
void setUpdatesBadge(int count);
// Last count given to the badge, kept so a language switch can rebuild the
// entry's label without having to re-query the core.
int m_updatesBadgeCount = 0;
void notifyUpdatesAvailable(int count);
// Runs a request and, when the core refused it, walks the user through what
// it needs before replaying it.
void handleStartResult(const AppCore::StartResult& result, const QString& title);
// Asks for the sudo password and hands it to the core. Returns false when
// the user cancelled.
bool promptForSudoPassword();
// Names the process holding pacman's database and offers to close it — or to
// drop a stale lock — so the pending action can resume.
void handleDatabaseLock(const QString& title);
// Lock-aware failure reporting: a locked database opens the recovery
// dialog, anything else is shown as-is.
void showActionError(const QString& title, const QString& error, bool locked);
void applyCredentialMode(AppCore::CredentialMode mode);
// Applies the mode and re-checks the matching menu entry, so the menu bar
// and the Settings screen can never disagree about the current mode.
void onCredentialModeRequested(bool atLaunch);
// Re-applies the language and puts the text back in the widgets, so
// switching language on the Settings screen takes effect at once.
void onLanguageChanged(const QString& code);
// Keeps the window's drop target in step with the preference.
void onDragDropEnabledChanged(bool enabled);
// Puts every string the frame owns back into its widget. Called on the
// LanguageChange event, so it is what makes the language setting visible
// without restarting.
void retranslateUI();
// Records that the running operation just said something: the step is kept
// for the heartbeat and the "nothing printed" clock restarts.
void noteOperationOutput(const QString& step);
// The files a drag carries that the core can install; empty when the drop is
// nothing the app handles (which is also how dragEnterEvent refuses it).
QStringList installableDroppedFiles(const QMimeData* mimeData) const;
// Takes the next queued file and asks the core to install it. A core busy
// with another operation leaves it queued: onCoreActionFinished calls back.
void startNextFileInstall();
// Logic layer: it owns the providers, the workers, the history, the
// credentials and the self-updater. The frame never talks to them directly.
AppCore* m_core = nullptr;
// Sidebar
QFrame* m_sidebar = nullptr;
QButtonGroup* m_navGroup = nullptr;
QPushButton* m_refreshButton = nullptr;
QPushButton* m_navUpdatesButton = nullptr;
QLabel* m_versionLabel = nullptr;
// Header
QWidget* m_headerWidget = nullptr;
QLabel* m_headerTitle = nullptr;
QLabel* m_headerSubtitle = nullptr;
QPushButton* m_headerUpdateAllButton = nullptr;
// Screens, stacked in sidebar order.
QStackedWidget* m_pages = nullptr;
QHash<QString, Page*> m_pageById; // "search" -> screen
QHash<QString, int> m_navIdByPage; // "search" -> NavSearch
DashboardPage* m_dashboard = nullptr;
BrowsePage* m_browse = nullptr;
CleanupPage* m_cleanup = nullptr;
HistoryPage* m_history = nullptr;
LogsPage* m_logs = nullptr;
PluginsPage* m_plugins = nullptr;
SettingsPage* m_settings = nullptr;
// Sidebar entries, by nav id, so the language can be re-applied to them
// (and the updates badge found) without rebuilding the sidebar.
QHash<int, QPushButton*> m_navButtons;
// Progress of the running operation, anchored in the status bar: the bar
// itself (indeterminate until a provider reports a real percentage) and the
// value next to it.
QProgressBar* m_installProgressBar = nullptr;
QLabel* m_progressLabel = nullptr;
// Highest percentage reported by the current operation, and a counter that
// invalidates the "hide the completed bar" timer when a new one starts.
int m_progressValue = 0;
int m_progressGeneration = 0;
// What the window knows about the operation it is waiting on. A download
// that takes minutes can print nothing at all for minutes, and a window
// that only repeats the same sentence looks broken: the timer re-states the
// current step with the elapsed time, and the journal notes the wait.
QTimer* m_activityTimer = nullptr;
QElapsedTimer m_operationClock;
QString m_operationStep;
qint64 m_lastOutputAtMs = 0;
qint64 m_lastWaitNoteMs = 0;
// Files waiting to be installed (dropped or opened), and whether one is
// being installed right now.
QStringList m_pendingFileInstalls;
bool m_fileInstallRunning = false;
// Tray (optional: hidden when the desktop has no system tray)
QSystemTrayIcon* m_tray = nullptr;
// Last update count already announced in a tray notification, so repeated
// dashboard refreshes do not spam the user.
int m_lastNotifiedUpdates = -1;
// Menu
QAction* m_credLaunchAction = nullptr;
QAction* m_credEachTimeAction = nullptr;
};