1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include <QDir>
#include <QIcon>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QThreadPool>
#include "common/logging.h"
#include "common/settings.h"
#include "core/core.h"
#include "core/file_sys/patch_manager.h"
#include "core/file_sys/registered_cache.h"
#include "core/hle/service/filesystem/filesystem.h"
#include "qt_common/config/uisettings.h"
#include "qt_common/qt_common.h"
#include "qt_common/util/game.h"
#include "qt_common/game_list/game_list_p.h"
#include "qt_common/game_list/model.h"
#include "qt_common/game_list/worker.h"
GameListModel::GameListModel(std::shared_ptr<FileSys::VfsFilesystem> vfs_,
FileSys::ManualContentProvider* provider_,
const PlayTime::PlayTimeManager& play_time_manager_,
Core::System& system_, QObject* parent)
: QStandardItemModel{parent}, vfs{std::move(vfs_)}, provider{provider_},
play_time_manager{play_time_manager_}, system{system_} {
watcher = new QFileSystemWatcher(this);
external_watcher = new QFileSystemWatcher(this);
connect(watcher, &QFileSystemWatcher::directoryChanged, this,
&GameListModel::RefreshGameDirectory);
connect(external_watcher, &QFileSystemWatcher::directoryChanged, this,
&GameListModel::RefreshExternalContent);
ResetExternalWatcher();
insertColumns(0, COLUMN_COUNT);
RetranslateUI();
setSortRole(GameListItemPath::SortRole);
}
GameListModel::~GameListModel() {
StopWorker();
}
void GameListModel::SetScanEnabled(bool enabled) {
scan_enabled = enabled;
if (!enabled) {
StopWorker();
}
}
void GameListModel::PopulateAsync(QVector<UISettings::GameDir>& game_dirs) {
if (!scan_enabled || system.IsPoweredOn()) {
return;
}
emit PopulatingStarted();
StopWorker();
removeRows(0, rowCount());
current_worker = std::make_unique<GameListWorker>(vfs, provider, game_dirs,
play_time_manager, system);
connect(current_worker.get(), &GameListWorker::DataAvailable, this, &GameListModel::WorkerEvent,
Qt::QueuedConnection);
QThreadPool::globalInstance()->start(current_worker.get());
}
void GameListModel::WorkerEvent() {
if (current_worker) {
current_worker->ProcessEvents(this);
}
}
void GameListModel::AddDirEntry(GameListDir* entry_items) {
if (m_flat) {
delete entry_items;
return;
}
invisibleRootItem()->appendRow(entry_items);
}
void GameListModel::AddEntry(const QList<QStandardItem*>& entry_items, GameListDir* parent) {
if (m_flat) {
invisibleRootItem()->appendRow(entry_items);
} else {
parent->appendRow(entry_items);
}
}
void GameListModel::DonePopulating(const QStringList& watch_list) {
emit ShowList(!IsEmpty());
if (!m_flat) {
invisibleRootItem()->appendRow(new GameListAddDir());
invisibleRootItem()->insertRow(0, new GameListFavorites());
for (const auto id : std::as_const(UISettings::values.favorited_ids)) {
AddFavorite(id);
}
}
emit PopulatingCompleted(watch_list);
}
bool GameListModel::IsEmpty() const {
for (int i = 0; i < rowCount(); i++) {
const QStandardItem* child = invisibleRootItem()->child(i);
const auto type = static_cast<GameListItemType>(child->type());
if (!child->hasChildren() &&
(type == GameListItemType::SdmcDir || type == GameListItemType::UserNandDir ||
type == GameListItemType::SysNandDir)) {
invisibleRootItem()->removeRow(child->row());
i--;
}
}
return !invisibleRootItem()->hasChildren();
}
void GameListModel::ToggleFavorite(u64 program_id) {
if (!UISettings::values.favorited_ids.contains(program_id)) {
UISettings::values.favorited_ids.append(program_id);
AddFavorite(program_id);
} else {
UISettings::values.favorited_ids.removeOne(program_id);
RemoveFavorite(program_id);
}
emit SaveConfig();
}
void GameListModel::AddFavorite(u64 program_id) {
auto* favorites_row = item(0);
for (int i = 1; i < rowCount() - 1; i++) {
const auto* folder = item(i);
for (int j = 0; j < folder->rowCount(); j++) {
if (folder->child(j)->data(GameListItemPath::ProgramIdRole).toULongLong() ==
program_id) {
QList<QStandardItem*> list;
for (int k = 0; k < COLUMN_COUNT; k++) {
list.append(folder->child(j, k)->clone());
}
list[0]->setData(folder->child(j)->data(GameListItem::SortRole),
GameListItem::SortRole);
list[0]->setText(folder->child(j)->data(Qt::DisplayRole).toString());
favorites_row->appendRow(list);
return;
}
}
}
}
void GameListModel::RemoveFavorite(u64 program_id) {
auto* favorites_row = item(0);
for (int i = 0; i < favorites_row->rowCount(); i++) {
const auto* game = favorites_row->child(i);
if (game->data(GameListItemPath::ProgramIdRole).toULongLong() == program_id) {
favorites_row->removeRow(i);
return;
}
}
}
void GameListModel::StopWorker() {
if (!current_worker) {
return;
}
// The worker may still be inside ProcessEvents further up the stack (a nested event loop
// can trigger a repopulate), so stop it now but defer freeing it to the event loop.
current_worker->disconnect();
current_worker->Stop();
current_worker.release()->deleteLater();
}
void GameListModel::Repopulate() {
// CreateFactories replaces the content providers a running game reads from. Stopping
// emulation repopulates the list anyway, so skip watcher-driven reloads until then.
if (!scan_enabled || QtCommon::system->IsPoweredOn()) {
LOG_INFO(Frontend, "Emulation is running, deferring game list reload.");
return;
}
StopWorker();
QtCommon::system->GetFileSystemController().CreateFactories(*QtCommon::vfs);
PopulateAsync(UISettings::values.game_dirs);
}
void GameListModel::RefreshGameDirectory() {
ResetExternalWatcher();
if (!UISettings::values.game_dirs.empty() && current_worker != nullptr) {
LOG_INFO(Frontend, "Change detected in the games directory. Reloading game list.");
Repopulate();
}
}
void GameListModel::RefreshExternalContent() {
if (!UISettings::values.game_dirs.empty() && current_worker != nullptr) {
LOG_INFO(Frontend, "External content directory changed. Clearing metadata cache.");
QtCommon::Game::ResetMetadata(false);
Repopulate();
}
}
void GameListModel::ResetExternalWatcher() {
auto watch_dirs = external_watcher->directories();
if (!watch_dirs.isEmpty()) {
external_watcher->removePaths(watch_dirs);
}
for (const std::string& dir : Settings::values.external_content_dirs) {
external_watcher->addPath(QString::fromStdString(dir));
}
}
void GameListModel::RetranslateUI() {
setHeaderData(COLUMN_NAME, Qt::Horizontal, tr("Name"));
setHeaderData(COLUMN_ADD_ONS, Qt::Horizontal, tr("Add-ons"));
setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, tr("File type"));
setHeaderData(COLUMN_SIZE, Qt::Horizontal, tr("Size"));
setHeaderData(COLUMN_PLAY_TIME, Qt::Horizontal, tr("Play time"));
}
QFileSystemWatcher* GameListModel::GetWatcher() const {
return watcher;
}
void GameListModel::SetFlat(bool flat) {
m_flat = flat;
}
|