game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_action_factory.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/game_action_factory.cpp
4// ==============================================================================
5// Shared factory implementation. Consolidated ~250 LoC of near-identical
6// per-game action construction that used to exist in both MenuBarManager's
7// Game menu and MainWindow's game-list context menu.
8//
9// Mutation handling: the factory calls libraryController directly for
10// writes (Quick Settings toggle, favorite flip, collection add/remove,
11// Configure dialog accept). After each mutation it emits the appropriate
12// signal so callers can drive global refresh cascades without knowing
13// what the factory did internally.
14// ==============================================================================
15
17
24#include "models/game.h"
25#include "theme_manager.h"
26
27#include <QAction>
28#include <QDesktopServices>
29#include <QInputDialog>
30#include <QKeySequence>
31#include <QLineEdit>
32#include <QMenu>
33#include <QMessageBox>
34#include <QProcess>
35#include <QString>
36#include <QUrl>
37#include <QWidget>
38
39#include <algorithm>
40#include <filesystem>
41#include <vector>
42
43namespace
44{
45
46// Open a steam:// URL via the steam executable directly — avoids KIO
47// protocol handler issues. Matches the helper in menubar_manager.cpp.
48void openSteamUrl(const QString& steamUrl)
49{
50 QProcess::startDetached("steam", {steamUrl});
51}
52
53} // namespace
54
55// ==============================================================================
56// Impl
57// ==============================================================================
58
60{
61 public:
62 Impl(QWidget* parentWidget, core::GameLibraryController* libCtrl,
63 core::LaunchController* launchCtrl, DetailsPanelManager* detailsMgr,
64 GameThumbnailCache* imgCache, ThemeManager* themeMgr)
65 : dialogParent(parentWidget), libraryController(libCtrl), launchController(launchCtrl),
66 detailsPanelManager(detailsMgr), imageCache(imgCache), themeManager(themeMgr)
67 {
68 }
69
70 QWidget* dialogParent = nullptr;
76
77 // Common helper: mutate a runnerConfig bool flag and emit
78 // gameConfigUpdated. Factored out so Quick Settings stay DRY.
79 void toggleRunnerConfigFlag(GameActionFactory* q, const std::string& gameId,
80 bool GameRunnerConfig::* field, bool newValue)
81 {
82 Game* g = libraryController->getGameForEdit(gameId);
83 if (!g)
84 return;
85 g->runnerConfig.*field = newValue;
86 libraryController->updateGame(gameId, *g);
88 detailsPanelManager->update(gameId);
89 emit q->gameConfigUpdated(gameId);
90 }
91};
92
93// ==============================================================================
94// Ctor / dtor
95// ==============================================================================
96
98 core::GameLibraryController* libraryController,
99 core::LaunchController* launchController,
100 DetailsPanelManager* detailsPanelManager,
101 GameThumbnailCache* imageCache, ThemeManager* themeManager,
102 QObject* qparent)
103 : QObject(qparent),
104 impl_(std::make_unique<Impl>(dialogParent, libraryController, launchController,
105 detailsPanelManager, imageCache, themeManager))
106{
107}
108
110
111// ==============================================================================
112// Launch / Stop / Install
113// ==============================================================================
114
115void GameActionFactory::addLaunchActions(QMenu* menu, const std::string& gameId, const Game* game)
116{
117 if (!menu || !game)
118 return;
119
120 const bool isRunning =
121 impl_->launchController && impl_->launchController->isGameRunning(gameId);
122 const bool isInstalled = game->isInstalled();
123 const bool isSteamGame = game->source == GameSource::Steam && game->metadata.steamAppId > 0;
124
125 if (isRunning)
126 {
127 auto* action = menu->addAction(tr("\u25A0 Stop"));
128 connect(action, &QAction::triggered, this,
129 [this, gameId]() { emit stopRequested(gameId); });
130 }
131 else if (isInstalled)
132 {
133 auto* action = menu->addAction(tr("\u25B6 Launch"));
134 action->setShortcut(QKeySequence(Qt::Key_Return));
135 connect(action, &QAction::triggered, this,
136 [this, gameId]() { emit launchRequested(gameId); });
137 }
138 else if (isSteamGame)
139 {
140 const int steamAppId = game->metadata.steamAppId;
141 auto* action = menu->addAction(tr("\u21E9 Install via Steam"));
142 connect(action, &QAction::triggered, this, [steamAppId]()
143 { openSteamUrl(QStringLiteral("steam://install/%1").arg(steamAppId)); });
144 }
145}
146
147// ==============================================================================
148// Configure
149// ==============================================================================
150
151void GameActionFactory::addConfigureAction(QMenu* menu, const std::string& gameId)
152{
153 if (!menu)
154 return;
155 auto* action = menu->addAction(tr("\u2699 Configure..."));
156 action->setShortcut(QKeySequence(QStringLiteral("Ctrl+G")));
157 connect(action, &QAction::triggered, this,
158 [this, gameId]()
159 {
160 Game* g = impl_->libraryController->getGameForEdit(gameId);
161 if (!g)
162 return;
163 GameConfigDialog dialog(g, impl_->dialogParent);
164 if (dialog.exec() == QDialog::Accepted)
165 {
166 impl_->libraryController->updateGame(gameId, *g);
167 if (impl_->detailsPanelManager)
168 impl_->detailsPanelManager->update(gameId);
169 emit gameConfigUpdated(gameId);
170 }
171 });
172}
173
174// ==============================================================================
175// Quick Settings submenu
176// ==============================================================================
177
178void GameActionFactory::addQuickSettingsSubmenu(QMenu* menu, const std::string& gameId,
179 const Game* game, bool includeMoreSettings)
180{
181 if (!menu || !game)
182 return;
183 auto* submenu = menu->addMenu(tr("\u2699 Quick Settings"));
184 impl_->themeManager->styleMenu(submenu);
185
186 auto addToggle = [this, submenu, gameId](const QString& label, bool GameRunnerConfig::* field,
187 bool currentValue)
188 {
189 auto* action = submenu->addAction(label);
190 action->setCheckable(true);
191 action->setChecked(currentValue);
192 connect(action, &QAction::triggered, this, [this, gameId, field](bool checked)
193 { impl_->toggleRunnerConfigFlag(this, gameId, field, checked); });
194 };
195
196 addToggle(tr("MangoHud"), &GameRunnerConfig::enableMangoHud, game->runnerConfig.enableMangoHud);
197 addToggle(tr("GameMode"), &GameRunnerConfig::enableGameMode, game->runnerConfig.enableGameMode);
198 addToggle(tr("Gamescope"), &GameRunnerConfig::enableGamescope,
200 submenu->addSeparator();
201 addToggle(tr("VkBasalt"), &GameRunnerConfig::enableVkBasalt, game->runnerConfig.enableVkBasalt);
202
203 if (includeMoreSettings)
204 {
205 submenu->addSeparator();
206 auto* more = submenu->addAction(tr("More settings..."));
207 connect(more, &QAction::triggered, this,
208 [this, gameId]()
209 {
210 Game* g = impl_->libraryController->getGameForEdit(gameId);
211 if (!g)
212 return;
213 GameConfigDialog dialog(g, impl_->dialogParent);
214 if (dialog.exec() == QDialog::Accepted)
215 {
216 impl_->libraryController->updateGame(gameId, *g);
217 if (impl_->detailsPanelManager)
218 impl_->detailsPanelManager->update(gameId);
219 emit gameConfigUpdated(gameId);
220 }
221 });
222 }
223}
224
225// ==============================================================================
226// Favorite
227// ==============================================================================
228
229void GameActionFactory::addFavoriteAction(QMenu* menu, const std::string& gameId, const Game* game)
230{
231 if (!menu || !game)
232 return;
233 const bool isFav = game->isFavorite;
234 auto* action =
235 menu->addAction(isFav ? tr("\u2605 Remove from Favorites") : tr("\u2605 Add to Favorites"));
236 action->setShortcut(QKeySequence(Qt::Key_F));
237 connect(action, &QAction::triggered, this,
238 [this, gameId, isFav]()
239 {
240 Game* g = impl_->libraryController->getGameForEdit(gameId);
241 if (!g)
242 return;
243 g->isFavorite = !isFav;
244 impl_->libraryController->updateGame(gameId, *g);
245 emit favoriteChanged(gameId);
246 });
247}
248
249// ==============================================================================
250// Collections submenu
251// ==============================================================================
252
253void GameActionFactory::addCollectionsSubmenu(QMenu* menu, const std::string& gameId,
254 const Game* game)
255{
256 if (!menu || !game)
257 return;
258 auto* submenu = menu->addMenu(tr("Co&llections"));
259 impl_->themeManager->styleMenu(submenu);
260
261 auto collections = impl_->libraryController->getCollections();
262 std::vector<std::string> gameCollections = game->collections;
263
264 for (const auto& collection : collections)
265 {
266 const bool inCollection = std::find(gameCollections.begin(), gameCollections.end(),
267 collection) != gameCollections.end();
268 auto* action = submenu->addAction(QString::fromStdString(collection));
269 action->setCheckable(true);
270 action->setChecked(inCollection);
271 connect(action, &QAction::triggered, this,
272 [this, gameId, collection, inCollection]()
273 {
274 if (inCollection)
275 impl_->libraryController->removeGameFromCollection(gameId, collection);
276 else
277 impl_->libraryController->addGameToCollection(gameId, collection);
278 emit gameListChanged();
279 });
280 }
281 if (!collections.empty())
282 submenu->addSeparator();
283
284 auto* newColl = submenu->addAction(tr("Create New..."));
285 connect(newColl, &QAction::triggered, this,
286 [this, gameId]()
287 {
288 bool ok = false;
289 QString name = QInputDialog::getText(impl_->dialogParent, tr("Create Collection"),
290 tr("Enter collection name:"),
291 QLineEdit::Normal, QString(), &ok);
292 if (!ok || name.isEmpty())
293 return;
294 std::string nameStr = name.toStdString();
295 impl_->libraryController->addCollection(nameStr);
296 impl_->libraryController->addGameToCollection(gameId, nameStr);
297 emit gameListChanged();
298 });
299}
300
301// ==============================================================================
302// Change Artwork
303// ==============================================================================
304
305void GameActionFactory::addChangeArtworkAction(QMenu* menu, const std::string& gameId,
306 const Game* game)
307{
308 if (!menu || !game)
309 return;
310 if (game->metadata.steamAppId <= 0)
311 return;
312 const int appId = game->metadata.steamAppId;
313 auto* action = menu->addAction(tr("Change &Artwork..."));
314 connect(action, &QAction::triggered, this,
315 [this, appId, gameId]()
316 {
317 const Game* g = impl_->libraryController->getGame(gameId);
318 if (!g)
319 return;
320 SteamGridDBSelectorDialog dialog(impl_->dialogParent);
321 dialog.setGame(QString::fromStdString(g->name), appId);
322 if (dialog.exec() == QDialog::Accepted && dialog.hasSelection())
323 {
324 if (impl_->imageCache)
325 impl_->imageCache->clearAll();
326 if (impl_->detailsPanelManager)
327 impl_->detailsPanelManager->update(gameId);
328 emit artworkChanged(gameId);
329 }
330 });
331}
332
333// ==============================================================================
334// ProtonDB
335// ==============================================================================
336
337void GameActionFactory::addProtonDBAction(QMenu* menu, const Game* game)
338{
339 if (!menu || !game || game->metadata.steamAppId <= 0)
340 return;
341 const int appId = game->metadata.steamAppId;
342 auto* action = menu->addAction(tr("View on &ProtonDB"));
343 connect(action, &QAction::triggered, this,
344 [appId]()
345 {
346 QDesktopServices::openUrl(
347 QUrl(QStringLiteral("https://www.protondb.com/app/%1").arg(appId)));
348 });
349}
350
351// ==============================================================================
352// Open folder
353// ==============================================================================
354
355void GameActionFactory::addOpenFolderAction(QMenu* menu, const Game* game)
356{
357 if (!menu || !game)
358 return;
359 if (!game->isInstalled() || game->runnerConfig.executablePath.empty())
360 return;
361 const std::filesystem::path gamePath(game->runnerConfig.executablePath);
362 const std::string folder = gamePath.parent_path().string();
363 auto* action = menu->addAction(tr("Open Game &Folder"));
364 connect(action, &QAction::triggered, this, [folder]()
365 { QDesktopServices::openUrl(QUrl::fromLocalFile(QString::fromStdString(folder))); });
366}
367
368// ==============================================================================
369// Steam — Manage / Store
370// ==============================================================================
371
372void GameActionFactory::addSteamActions(QMenu* menu, const Game* game)
373{
374 if (!menu || !game)
375 return;
376 const bool isSteamGame = game->source == GameSource::Steam && game->metadata.steamAppId > 0;
377 if (!isSteamGame)
378 return;
379
380 const int appId = game->metadata.steamAppId;
381 if (game->isInstalled())
382 {
383 auto* manage = menu->addAction(tr("Manage in &Steam..."));
384 connect(manage, &QAction::triggered, this, [appId]()
385 { openSteamUrl(QStringLiteral("steam://nav/games/details/%1").arg(appId)); });
386 }
387 auto* store = menu->addAction(tr("View on Steam Sto&re"));
388 connect(store, &QAction::triggered, this,
389 [appId]()
390 {
391 QDesktopServices::openUrl(
392 QUrl(QStringLiteral("https://store.steampowered.com/app/%1").arg(appId)));
393 });
394}
395
396// ==============================================================================
397// Remove from Library
398// ==============================================================================
399
400void GameActionFactory::addRemoveAction(QMenu* menu, const std::string& gameId)
401{
402 if (!menu)
403 return;
404 auto* action = menu->addAction(tr("\u2715 Remove from Library"));
405 action->setShortcut(QKeySequence::Delete);
406 connect(action, &QAction::triggered, this,
407 [this, gameId]()
408 {
409 auto reply = QMessageBox::question(
410 impl_->dialogParent, tr("Confirm Removal"),
411 tr("Are you sure you want to remove this game from the library?"),
412 QMessageBox::Yes | QMessageBox::No);
413 if (reply == QMessageBox::Yes)
414 emit removeGameRequested(gameId);
415 });
416}
void toggleRunnerConfigFlag(GameActionFactory *q, const std::string &gameId, bool GameRunnerConfig::*field, bool newValue)
Impl(QWidget *parentWidget, core::GameLibraryController *libCtrl, core::LaunchController *launchCtrl, DetailsPanelManager *detailsMgr, GameThumbnailCache *imgCache, ThemeManager *themeMgr)
GameThumbnailCache * imageCache
DetailsPanelManager * detailsPanelManager
core::LaunchController * launchController
core::GameLibraryController * libraryController
void addQuickSettingsSubmenu(QMenu *menu, const std::string &gameId, const Game *game, bool includeMoreSettings)
"Quick Settings" submenu with MangoHud / GameMode / Gamescope / VkBasalt toggles.
~GameActionFactory() override
void addCollectionsSubmenu(QMenu *menu, const std::string &gameId, const Game *game)
"Collections" submenu listing every collection as a checkmarked action, plus "Create New....
void gameConfigUpdated(const std::string &gameId)
Configure dialog closed Accepted OR a Quick Setting was toggled.
GameActionFactory(QWidget *dialogParent, core::GameLibraryController *libraryController, core::LaunchController *launchController, DetailsPanelManager *detailsPanelManager, GameThumbnailCache *imageCache, ThemeManager *themeManager, QObject *qparent=nullptr)
void removeGameRequested(const std::string &gameId)
Remove from Library confirmed.
void favoriteChanged(const std::string &gameId)
Favorite state flipped. Caller should refresh the list.
void addChangeArtworkAction(QMenu *menu, const std::string &gameId, const Game *game)
"Change Artwork..." — opens SteamGridDBSelectorDialog if the game has a Steam App ID.
void addFavoriteAction(QMenu *menu, const std::string &gameId, const Game *game)
Favorite toggle (text flips between Add/Remove based on current state).
void addConfigureAction(QMenu *menu, const std::string &gameId)
Configure... — opens GameConfigDialog.
void addRemoveAction(QMenu *menu, const std::string &gameId)
"Remove from Library" with confirmation QMessageBox.
void artworkChanged(const std::string &gameId)
Artwork was changed via SteamGridDBSelectorDialog.
void stopRequested(const std::string &gameId)
The Stop action was triggered. Caller calls launchController->kill.
void launchRequested(const std::string &gameId)
The Launch action was triggered. Caller runs the launch pipeline.
void addProtonDBAction(QMenu *menu, const Game *game)
"View on ProtonDB" — opens https://www.protondb.com/app/{appId} in the default browser.
void addOpenFolderAction(QMenu *menu, const Game *game)
"Open Game Folder" — opens the executable's parent directory in the file manager.
void addSteamActions(QMenu *menu, const Game *game)
Steam actions — "Manage in Steam..." (if installed) and "View on Steam Store" (always for Steam games...
void addLaunchActions(QMenu *menu, const std::string &gameId, const Game *game)
Launch (if installed + not running), Stop (if running), or Install via Steam (if Steam game + not ins...
void gameListChanged()
Collections (add / remove / create+add) changed for gameId.
Complete representation of a game in the library.
Definition game.h:767
GameMetadata metadata
Descriptive information and media.
Definition game.h:785
std::vector< std::string > collections
Collections this game belongs to.
Definition game.h:804
bool isFavorite
Is this marked as a favorite game?
Definition game.h:803
std::string name
Display name shown in UI.
Definition game.h:774
GameRunnerConfig runnerConfig
How to launch and run the game.
Definition game.h:784
bool isInstalled() const
Check if game is currently installed.
Definition game.h:944
GameSource source
Where the game was acquired from (Steam, GOG, Local, etc.).
Definition game.h:775
void setGame(const QString &gameName, int steamAppId=0)
@ Steam
Steam store/platform.
Definition game.h:41
static void openSteamUrl(const QString &steamUrl)
int steamAppId
Steam App ID (0 if not a Steam game).
Definition game.h:575
Configuration for how to launch and run a game.
Definition game.h:138
bool enableVkBasalt
Enable VkBasalt shader injection.
Definition game.h:188
std::string executablePath
Full path to game executable.
Definition game.h:141
bool enableMangoHud
Enable MangoHud performance overlay.
Definition game.h:184
bool enableGameMode
Enable GameMode CPU governor optimization.
Definition game.h:186
bool enableGamescope
Enable Gamescope compositor.
Definition game.h:194