game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_library_controller.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// game_library_controller.h
4// ==============================================================================
5// Controller for game library operations - UI-agnostic business logic
6// This separates game management from UI rendering concerns
7// ==============================================================================
8
9#pragma once
10
11#include <functional>
12#include <memory>
13#include <optional>
14#include <string>
15#include <vector>
16
17// Forward declarations to avoid header dependencies
18class Game;
19class GameLibrary;
20enum class GamePlatform;
21enum class GameStatus;
22enum class GameRunner;
23enum class GameSource;
24struct PlaySession;
25
26namespace core
27{
28
29// ==============================================================================
30// Event Types - UI subscribes to these for updates
31// ==============================================================================
32
34{
35 std::string gameId;
36 std::string gameName;
37};
38
40{
41 std::string gameId;
42 std::string gameName;
43};
44
46{
47 std::string gameId;
48 std::string fieldChanged; // Empty means full update
49};
50
52{
53 size_t gameCount;
54};
55
56// ==============================================================================
57// Filter & Sort Options
58// ==============================================================================
59
68
74
76{
77 std::string searchQuery;
78 std::optional<GamePlatform> platform;
79 std::optional<GameStatus> status;
80 std::optional<GameRunner> runner;
81 std::optional<GameSource> source;
82 std::optional<std::string> collection;
83 bool favoritesOnly = false;
84 bool installedOnly = false;
85 bool notInstalledOnly = false; // Show only uninstalled games (Steam games not installed)
86};
87
93
94// ==============================================================================
95// GameLibraryController
96// ==============================================================================
97// Provides UI-agnostic interface for game library operations.
98// The Qt UI layer uses this instead of directly accessing GameLibrary.
99// ==============================================================================
100
102{
103 public:
104 // Callback types for event notifications
105 using OnGameAdded = std::function<void(const GameAddedEvent&)>;
106 using OnGameRemoved = std::function<void(const GameRemovedEvent&)>;
107 using OnGameUpdated = std::function<void(const GameUpdatedEvent&)>;
108 using OnLibraryLoaded = std::function<void(const LibraryLoadedEvent&)>;
109 using OnFilteredListChanged = std::function<void(const std::vector<const Game*>&)>;
110
113
114 // Non-copyable, movable
119
120 // =========================================================================
121 // Library Operations
122 // =========================================================================
123
124 /// Load library from disk
125 void loadLibrary();
126
127 /// Save library to disk
128 void saveLibrary();
129
130 /// Get total game count
131 size_t getGameCount() const;
132
133 /// Get a game by ID (returns nullptr if not found)
134 const Game* getGame(const std::string& gameId) const;
135
136 /// Get mutable game for editing (returns nullptr if not found).
137 /// WARNING: the returned pointer is only safe to use immediately on the
138 /// same thread as the call and must not be stored across event-loop yields.
139 /// Prefer executeWithGame() for cross-thread or long-lived access.
140 Game* getGameForEdit(const std::string& gameId);
141
142 /// Execute an update function on a game while holding the library's
143 /// internal lock. Safe to call from any thread.
144 /// The updater receives a mutable Game& valid only for its duration.
145 void executeWithGame(const std::string& gameId, const std::function<void(Game&)>& updater);
146
147 // =========================================================================
148 // CRUD Operations
149 // =========================================================================
150
151 /// Add a new game to the library
152 /// Returns the assigned game ID, or empty string on failure
153 std::string addGame(const Game& game);
154
155 /// Update an existing game
156 /// Returns true on success
157 bool updateGame(const std::string& gameId, const Game& updatedGame);
158
159 /// Remove a game from the library
160 /// Returns true on success
161 bool removeGame(const std::string& gameId);
162
163 /// Duplicate a game with a new name
164 /// Returns the new game's ID, or empty string on failure
165 std::string duplicateGame(const std::string& sourceGameId, const std::string& newName);
166
167 // =========================================================================
168 // Filtering & Sorting
169 // =========================================================================
170
171 /// Set filter options and refresh filtered list
172 void setFilter(const FilterOptions& options);
173
174 /// Get current filter options (returns copy for thread safety)
175 FilterOptions getFilter() const;
176
177 /// Set sort options and refresh filtered list
178 void setSort(const SortOptions& options);
179
180 /// Get current sort options (returns copy for thread safety)
181 SortOptions getSort() const;
182
183 /// Get the currently filtered and sorted game list (returns copy for thread safety)
184 std::vector<const Game*> getFilteredGames() const;
185
186 /// Force refresh of filtered list (e.g., after external game state change)
187 void refreshFilteredList();
188
189 // =========================================================================
190 // Collections
191 // =========================================================================
192
193 /// Get all collection names
194 std::vector<std::string> getCollections() const;
195
196 /// Add a new collection
197 bool addCollection(const std::string& name);
198
199 /// Remove a collection
200 bool removeCollection(const std::string& name);
201
202 /// Rename a collection
203 bool renameCollection(const std::string& oldName, const std::string& newName);
204
205 /// Add a game to a collection
206 bool addGameToCollection(const std::string& gameId, const std::string& collection);
207
208 /// Remove a game from a collection
209 bool removeGameFromCollection(const std::string& gameId, const std::string& collection);
210
211 // =========================================================================
212 // Favorites
213 // =========================================================================
214
215 /// Toggle favorite status
216 void toggleFavorite(const std::string& gameId);
217
218 /// Check if a game is favorited
219 bool isFavorite(const std::string& gameId) const;
220
221 // =========================================================================
222 // Statistics & Queries
223 // =========================================================================
224
225 /// Get all games (non-const for process tracking)
226 std::vector<Game*> getAllGames();
227
228 /// Get recently played games
229 std::vector<const Game*> getRecentlyPlayed(int count = 10) const;
230
231 /// Get recent play sessions for a game from the database
232 std::vector<PlaySession> getRecentSessions(const std::string& gameId, int limit = 20) const;
233
234 /// Get games by platform
235 std::vector<const Game*> getGamesByPlatform(GamePlatform platform) const;
236
237 /// Get games by runner
238 std::vector<const Game*> getGamesByRunner(GameRunner runner) const;
239
240 /// Search games by name (substring match)
241 std::vector<const Game*> searchGames(const std::string& query) const;
242
243 // =========================================================================
244 // Image Discovery
245 // =========================================================================
246
247 /// Discover and update image paths for games that are missing cover art
248 /// This should be called in a background thread
249 /// Returns number of games updated
251
252 /// Discover images for a specific game (by Steam App ID)
253 /// Returns true if images were found and updated
254 bool discoverImagesForGame(const std::string& gameId);
255
256 /// Refresh images for games with stale/missing image files
257 /// Checks if stored paths still exist and re-discovers if not
258 /// This should be called in a background thread
259 /// Returns number of games refreshed
260 int refreshStaleImages();
261
262 // =========================================================================
263 // Event Subscriptions
264 // =========================================================================
265
266 void setOnGameAdded(OnGameAdded callback);
267 void setOnGameRemoved(OnGameRemoved callback);
268 void setOnGameUpdated(OnGameUpdated callback);
269 void setOnLibraryLoaded(OnLibraryLoaded callback);
271
272 private:
273 class Impl;
274 std::unique_ptr<Impl> impl_;
275};
276
277} // namespace core
Central database for managing all games in the launcher.
Complete representation of a game in the library.
Definition game.h:767
void setOnGameRemoved(OnGameRemoved callback)
bool updateGame(const std::string &gameId, const Game &updatedGame)
Update an existing game Returns true on success.
std::vector< PlaySession > getRecentSessions(const std::string &gameId, int limit=20) const
Get recent play sessions for a game from the database.
void setOnFilteredListChanged(OnFilteredListChanged callback)
std::string addGame(const Game &game)
Add a new game to the library Returns the assigned game ID, or empty string on failure.
std::vector< const Game * > getGamesByRunner(GameRunner runner) const
Get games by runner.
std::string duplicateGame(const std::string &sourceGameId, const std::string &newName)
Duplicate a game with a new name Returns the new game's ID, or empty string on failure.
std::vector< std::string > getCollections() const
Get all collection names.
GameLibraryController(GameLibraryController &&) noexcept
int discoverMissingImages()
Discover and update image paths for games that are missing cover art This should be called in a backg...
bool removeGame(const std::string &gameId)
Remove a game from the library Returns true on success.
bool discoverImagesForGame(const std::string &gameId)
Discover images for a specific game (by Steam App ID) Returns true if images were found and updated.
FilterOptions getFilter() const
Get current filter options (returns copy for thread safety).
void refreshFilteredList()
Force refresh of filtered list (e.g., after external game state change).
const Game * getGame(const std::string &gameId) const
Get a game by ID (returns nullptr if not found).
void loadLibrary()
Load library from disk.
Game * getGameForEdit(const std::string &gameId)
Get mutable game for editing (returns nullptr if not found).
bool removeGameFromCollection(const std::string &gameId, const std::string &collection)
Remove a game from a collection.
std::vector< const Game * > searchGames(const std::string &query) const
Search games by name (substring match).
bool addCollection(const std::string &name)
Add a new collection.
void setSort(const SortOptions &options)
Set sort options and refresh filtered list.
GameLibraryController(const GameLibraryController &)=delete
void toggleFavorite(const std::string &gameId)
Toggle favorite status.
std::function< void(const GameRemovedEvent &)> OnGameRemoved
void setOnGameAdded(OnGameAdded callback)
void setFilter(const FilterOptions &options)
Set filter options and refresh filtered list.
bool renameCollection(const std::string &oldName, const std::string &newName)
Rename a collection.
std::vector< const Game * > getFilteredGames() const
Get the currently filtered and sorted game list (returns copy for thread safety).
std::function< void(const GameUpdatedEvent &)> OnGameUpdated
void executeWithGame(const std::string &gameId, const std::function< void(Game &)> &updater)
Execute an update function on a game while holding the library's internal lock.
void saveLibrary()
Save library to disk.
std::vector< Game * > getAllGames()
Get all games (non-const for process tracking).
std::function< void(const GameAddedEvent &)> OnGameAdded
size_t getGameCount() const
Get total game count.
std::vector< const Game * > getRecentlyPlayed(int count=10) const
Get recently played games.
bool removeCollection(const std::string &name)
Remove a collection.
std::function< void(const LibraryLoadedEvent &)> OnLibraryLoaded
std::function< void(const std::vector< const Game * > &)> OnFilteredListChanged
GameLibraryController & operator=(const GameLibraryController &)=delete
void setOnLibraryLoaded(OnLibraryLoaded callback)
int refreshStaleImages()
Refresh images for games with stale/missing image files Checks if stored paths still exist and re-dis...
SortOptions getSort() const
Get current sort options (returns copy for thread safety).
bool addGameToCollection(const std::string &gameId, const std::string &collection)
Add a game to a collection.
void setOnGameUpdated(OnGameUpdated callback)
std::vector< const Game * > getGamesByPlatform(GamePlatform platform) const
Get games by platform.
bool isFavorite(const std::string &gameId) const
Check if a game is favorited.
GameSource
Where the game was acquired from (store/service/local).
Definition game.h:39
GamePlatform
Platform/runner type for executing the game.
Definition game.h:61
GameStatus
Current installation and execution status of the game.
Definition game.h:97
GameRunner
Runner/compatibility layer for executing the game.
Definition game.h:81
Record of a single game play session.
Definition game.h:630
std::optional< GameStatus > status
std::optional< GameRunner > runner
std::optional< std::string > collection
std::optional< GameSource > source