/
GameList.cpp
538 lines (449 loc) · 18.8 KB
/
GameList.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <QDesktopServices>
#include <QDir>
#include <QErrorMessage>
#include <QFileDialog>
#include <QFileInfo>
#include <QFrame>
#include <QHeaderView>
#include <QKeyEvent>
#include <QMap>
#include <QMenu>
#include <QMessageBox>
#include <QProgressDialog>
#include <QUrl>
#include "Common/FileUtil.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/HW/DVD/DVDInterface.h"
#include "Core/HW/WiiSave.h"
#include "Core/WiiUtils.h"
#include "DiscIO/Blob.h"
#include "DiscIO/Enums.h"
#include "DolphinQt2/Config/PropertiesDialog.h"
#include "DolphinQt2/GameList/GameList.h"
#include "DolphinQt2/GameList/GridProxyModel.h"
#include "DolphinQt2/GameList/ListProxyModel.h"
#include "DolphinQt2/QtUtils/ActionHelper.h"
#include "DolphinQt2/QtUtils/DoubleClickEventFilter.h"
#include "DolphinQt2/Settings.h"
#include "DolphinQt2/WiiUpdate.h"
static bool CompressCB(const std::string&, float, void*);
GameList::GameList(QWidget* parent) : QStackedWidget(parent)
{
m_model = Settings::Instance().GetGameListModel();
m_list_proxy = new ListProxyModel(this);
m_list_proxy->setSortCaseSensitivity(Qt::CaseInsensitive);
m_list_proxy->setSortRole(Qt::InitialSortOrderRole);
m_list_proxy->setSourceModel(m_model);
m_grid_proxy = new GridProxyModel(this);
m_grid_proxy->setSourceModel(m_model);
MakeListView();
MakeGridView();
MakeEmptyView();
connect(m_list, &QTableView::doubleClicked, this, &GameList::GameSelected);
connect(m_grid, &QListView::doubleClicked, this, &GameList::GameSelected);
connect(m_model, &QAbstractItemModel::rowsInserted, this, &GameList::ConsiderViewChange);
connect(m_model, &QAbstractItemModel::rowsRemoved, this, &GameList::ConsiderViewChange);
addWidget(m_list);
addWidget(m_grid);
addWidget(m_empty);
m_prefer_list = Settings::Instance().GetPreferredView();
ConsiderViewChange();
}
void GameList::MakeListView()
{
m_list = new QTableView(this);
m_list->setModel(m_list_proxy);
m_list->setSelectionMode(QAbstractItemView::SingleSelection);
m_list->setSelectionBehavior(QAbstractItemView::SelectRows);
m_list->setAlternatingRowColors(true);
m_list->setShowGrid(false);
m_list->setSortingEnabled(true);
m_list->setCurrentIndex(QModelIndex());
m_list->setContextMenuPolicy(Qt::CustomContextMenu);
m_list->setWordWrap(false);
m_list->verticalHeader()->setDefaultSectionSize(m_list->verticalHeader()->defaultSectionSize() *
1.25);
connect(m_list, &QTableView::customContextMenuRequested, this, &GameList::ShowContextMenu);
connect(m_list->selectionModel(), &QItemSelectionModel::selectionChanged,
[this](const QItemSelection&, const QItemSelection&) {
emit SelectionChanged(GetSelectedGame());
});
QHeaderView* hor_header = m_list->horizontalHeader();
connect(hor_header, &QHeaderView::sortIndicatorChanged, this, &GameList::OnHeaderViewChanged);
connect(hor_header, &QHeaderView::sectionResized, this, &GameList::OnHeaderViewChanged);
connect(hor_header, &QHeaderView::sectionCountChanged, this, &GameList::OnHeaderViewChanged);
connect(hor_header, &QHeaderView::sectionMoved, this, &GameList::OnHeaderViewChanged);
hor_header->setSectionsMovable(true);
if (!Settings::GetQSettings().contains(QStringLiteral("tableheader/state")))
m_list->sortByColumn(GameListModel::COL_TITLE, Qt::AscendingOrder);
hor_header->restoreState(
Settings::GetQSettings().value(QStringLiteral("tableheader/state")).toByteArray());
hor_header->setSectionResizeMode(GameListModel::COL_PLATFORM, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_BANNER, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_TITLE, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_DESCRIPTION, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_MAKER, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_COUNTRY, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_SIZE, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_FILE_NAME, QHeaderView::Interactive);
m_list->setColumnHidden(GameListModel::COL_PLATFORM, !SConfig::GetInstance().m_showSystemColumn);
m_list->setColumnHidden(GameListModel::COL_BANNER, !SConfig::GetInstance().m_showBannerColumn);
m_list->setColumnHidden(GameListModel::COL_TITLE, !SConfig::GetInstance().m_showTitleColumn);
m_list->setColumnHidden(GameListModel::COL_DESCRIPTION,
!SConfig::GetInstance().m_showDescriptionColumn);
m_list->setColumnHidden(GameListModel::COL_MAKER, !SConfig::GetInstance().m_showMakerColumn);
m_list->setColumnHidden(GameListModel::COL_ID, !SConfig::GetInstance().m_showIDColumn);
m_list->setColumnHidden(GameListModel::COL_COUNTRY, !SConfig::GetInstance().m_showRegionColumn);
m_list->setColumnHidden(GameListModel::COL_SIZE, !SConfig::GetInstance().m_showSizeColumn);
m_list->setColumnHidden(GameListModel::COL_FILE_NAME,
!SConfig::GetInstance().m_showFileNameColumn);
m_list->verticalHeader()->hide();
m_list->setFrameStyle(QFrame::NoFrame);
}
void GameList::MakeEmptyView()
{
m_empty = new QLabel(this);
m_empty->setText(tr("Dolphin could not find any GameCube/Wii ISOs or WADs.\n"
"Double-click here to set a games directory..."));
m_empty->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
auto event_filter = new DoubleClickEventFilter{};
m_empty->installEventFilter(event_filter);
connect(event_filter, &DoubleClickEventFilter::doubleClicked, [this] {
auto current_dir = QDir::currentPath();
auto dir = QFileDialog::getExistingDirectory(this, tr("Select a Directory"), current_dir);
if (!dir.isEmpty())
Settings::Instance().AddPath(dir);
});
}
void GameList::MakeGridView()
{
m_grid = new QListView(this);
m_grid->setModel(m_grid_proxy);
m_grid->setViewMode(QListView::IconMode);
m_grid->setResizeMode(QListView::Adjust);
m_grid->setUniformItemSizes(true);
m_grid->setContextMenuPolicy(Qt::CustomContextMenu);
m_grid->setFrameStyle(QFrame::NoFrame);
connect(m_grid, &QTableView::customContextMenuRequested, this, &GameList::ShowContextMenu);
connect(m_grid->selectionModel(), &QItemSelectionModel::selectionChanged,
[this](const QItemSelection&, const QItemSelection&) {
emit SelectionChanged(GetSelectedGame());
});
}
void GameList::ShowContextMenu(const QPoint&)
{
const auto game = GetSelectedGame();
if (!game)
return;
QMenu* menu = new QMenu(this);
DiscIO::Platform platform = game->GetPlatform();
AddAction(menu, tr("&Properties"), this, &GameList::OpenProperties);
AddAction(menu, tr("&Wiki"), this, &GameList::OpenWiki);
menu->addSeparator();
if (platform == DiscIO::Platform::GameCubeDisc || platform == DiscIO::Platform::WiiDisc)
{
AddAction(menu, tr("Set as &default ISO"), this, &GameList::SetDefaultISO);
const auto blob_type = game->GetBlobType();
if (blob_type == DiscIO::BlobType::GCZ)
AddAction(menu, tr("Decompress ISO..."), this, &GameList::CompressISO);
else if (blob_type == DiscIO::BlobType::PLAIN)
AddAction(menu, tr("Compress ISO..."), this, &GameList::CompressISO);
QAction* change_disc = AddAction(menu, tr("Change &Disc"), this, &GameList::ChangeDisc);
connect(&Settings::Instance(), &Settings::EmulationStateChanged, change_disc,
[change_disc] { change_disc->setEnabled(Core::IsRunning()); });
change_disc->setEnabled(Core::IsRunning());
menu->addSeparator();
}
if (platform == DiscIO::Platform::WiiDisc)
{
auto* perform_disc_update = AddAction(menu, tr("Perform System Update"), this, [this] {
WiiUpdate::PerformDiscUpdate(GetSelectedGame()->GetFilePath(), this);
});
perform_disc_update->setEnabled(!Core::IsRunning() || !SConfig::GetInstance().bWii);
}
if (platform == DiscIO::Platform::WiiWAD)
{
QAction* wad_install_action = new QAction(tr("Install to the NAND"), menu);
QAction* wad_uninstall_action = new QAction(tr("Uninstall from the NAND"), menu);
connect(wad_install_action, &QAction::triggered, this, &GameList::InstallWAD);
connect(wad_uninstall_action, &QAction::triggered, this, &GameList::UninstallWAD);
for (QAction* a : {wad_install_action, wad_uninstall_action})
{
a->setEnabled(!Core::IsRunning());
menu->addAction(a);
}
if (!Core::IsRunning())
wad_uninstall_action->setEnabled(WiiUtils::IsTitleInstalled(game->GetTitleID()));
connect(&Settings::Instance(), &Settings::EmulationStateChanged, menu, [=](Core::State state) {
wad_install_action->setEnabled(state == Core::State::Uninitialized);
wad_uninstall_action->setEnabled(state == Core::State::Uninitialized &&
WiiUtils::IsTitleInstalled(game->GetTitleID()));
});
menu->addSeparator();
}
if (platform == DiscIO::Platform::WiiWAD || platform == DiscIO::Platform::WiiDisc)
{
AddAction(menu, tr("Open Wii &save folder"), this, &GameList::OpenSaveFolder);
AddAction(menu, tr("Export Wii save (Experimental)"), this, &GameList::ExportWiiSave);
menu->addSeparator();
}
AddAction(menu, tr("Open &containing folder"), this, &GameList::OpenContainingFolder);
AddAction(menu, tr("Delete File..."), this, &GameList::DeleteFile);
QAction* netplay_host = new QAction(tr("Host with NetPlay"), menu);
connect(netplay_host, &QAction::triggered,
[this, game] { emit NetPlayHost(QString::fromStdString(game->GetUniqueIdentifier())); });
connect(&Settings::Instance(), &Settings::EmulationStateChanged, menu, [=](Core::State state) {
netplay_host->setEnabled(state == Core::State::Uninitialized);
});
netplay_host->setEnabled(!Core::IsRunning());
menu->addAction(netplay_host);
menu->exec(QCursor::pos());
}
void GameList::OpenProperties()
{
PropertiesDialog* properties = new PropertiesDialog(this, *GetSelectedGame());
connect(properties, &PropertiesDialog::OpenGeneralSettings, this, &GameList::OpenGeneralSettings);
properties->show();
}
void GameList::ExportWiiSave()
{
const QString export_dir = QFileDialog::getExistingDirectory(
this, tr("Select Export Directory"), QString::fromStdString(File::GetUserPath(D_USER_IDX)),
QFileDialog::ShowDirsOnly);
if (export_dir.isEmpty())
return;
if (WiiSave::Export(GetSelectedGame()->GetTitleID(), export_dir.toStdString()))
QMessageBox::information(this, tr("Save Export"), tr("Successfully exported save files"));
else
QMessageBox::critical(this, tr("Save Export"), tr("Failed to export save files."));
}
void GameList::OpenWiki()
{
QString game_id = QString::fromStdString(GetSelectedGame()->GetGameID());
QString url = QStringLiteral("https://wiki.dolphin-emu.org/index.php?title=").append(game_id);
QDesktopServices::openUrl(QUrl(url));
}
void GameList::CompressISO()
{
auto file = GetSelectedGame();
const auto original_path = file->GetFilePath();
const bool compressed = (file->GetBlobType() == DiscIO::BlobType::GCZ);
if (!compressed && file->GetPlatform() == DiscIO::Platform::WiiDisc)
{
QMessageBox wii_warning(this);
wii_warning.setIcon(QMessageBox::Warning);
wii_warning.setText(tr("Are you sure?"));
wii_warning.setInformativeText(
tr("Compressing a Wii disc image will irreversibly change the compressed copy by removing "
"padding data. Your disc image will still work. Continue?"));
wii_warning.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
if (wii_warning.exec() == QMessageBox::No)
return;
}
QString dst_path = QFileDialog::getSaveFileName(
this,
compressed ? tr("Select where you want to save the decompressed image") :
tr("Select where you want to save the compressed image"),
QFileInfo(QString::fromStdString(GetSelectedGame()->GetFilePath()))
.dir()
.absoluteFilePath(QString::fromStdString(file->GetGameID()))
.append(compressed ? QStringLiteral(".gcm") : QStringLiteral(".gcz")),
compressed ? tr("Uncompressed GC/Wii images (*.iso *.gcm)") :
tr("Compressed GC/Wii images (*.gcz)"));
if (dst_path.isEmpty())
return;
QProgressDialog progress_dialog(compressed ? tr("Decompressing...") : tr("Compressing..."),
tr("Abort"), 0, 100, this);
progress_dialog.setWindowModality(Qt::WindowModal);
bool good;
if (compressed)
{
good = DiscIO::DecompressBlobToFile(original_path, dst_path.toStdString(), &CompressCB,
&progress_dialog);
}
else
{
good = DiscIO::CompressFileToBlob(original_path, dst_path.toStdString(),
file->GetPlatform() == DiscIO::Platform::WiiDisc ? 1 : 0,
16384, &CompressCB, &progress_dialog);
}
if (good)
{
QMessageBox(QMessageBox::Information, tr("Success!"), tr("Successfully compressed image."),
QMessageBox::Ok, this)
.exec();
}
else
{
QErrorMessage(this).showMessage(tr("Dolphin failed to complete the requested action."));
}
}
void GameList::InstallWAD()
{
QMessageBox result_dialog(this);
const bool success = WiiUtils::InstallWAD(GetSelectedGame()->GetFilePath());
result_dialog.setIcon(success ? QMessageBox::Information : QMessageBox::Critical);
result_dialog.setText(success ? tr("Successfully installed this title to the NAND.") :
tr("Failed to install this title to the NAND."));
result_dialog.exec();
}
void GameList::UninstallWAD()
{
QMessageBox warning_dialog(this);
warning_dialog.setIcon(QMessageBox::Information);
warning_dialog.setText(tr("Uninstalling the WAD will remove the currently installed version of "
"this title from the NAND without deleting its save data. Continue?"));
warning_dialog.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
if (warning_dialog.exec() == QMessageBox::No)
return;
QMessageBox result_dialog(this);
const bool success = WiiUtils::UninstallTitle(GetSelectedGame()->GetTitleID());
result_dialog.setIcon(success ? QMessageBox::Information : QMessageBox::Critical);
result_dialog.setText(success ? tr("Successfully removed this title from the NAND.") :
tr("Failed to remove this title from the NAND."));
result_dialog.exec();
}
void GameList::SetDefaultISO()
{
SConfig::GetInstance().m_strDefaultISO = GetSelectedGame()->GetFilePath();
}
void GameList::OpenContainingFolder()
{
QUrl url = QUrl::fromLocalFile(
QFileInfo(QString::fromStdString(GetSelectedGame()->GetFilePath())).dir().absolutePath());
QDesktopServices::openUrl(url);
}
void GameList::OpenSaveFolder()
{
QUrl url = QUrl::fromLocalFile(QString::fromStdString(GetSelectedGame()->GetWiiFSPath()));
QDesktopServices::openUrl(url);
}
void GameList::DeleteFile()
{
const std::string game = GetSelectedGame()->GetFilePath();
QMessageBox confirm_dialog(this);
confirm_dialog.setIcon(QMessageBox::Warning);
confirm_dialog.setWindowTitle(tr("Confirm"));
confirm_dialog.setText(tr("Are you sure you want to delete this file?"));
confirm_dialog.setInformativeText(tr("This cannot be undone!"));
confirm_dialog.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
if (confirm_dialog.exec() == QMessageBox::Yes)
{
bool deletion_successful = false;
while (!deletion_successful)
{
deletion_successful = File::Delete(game);
if (deletion_successful)
{
m_model->RemoveGame(game);
}
else
{
QMessageBox error_dialog(this);
error_dialog.setIcon(QMessageBox::Critical);
error_dialog.setText(tr("Failed to delete the selected file."));
error_dialog.setInformativeText(tr("Check whether you have the permissions required to "
"delete the file or whether it's still in use."));
error_dialog.setStandardButtons(QMessageBox::Retry | QMessageBox::Abort);
if (error_dialog.exec() == QMessageBox::Abort)
break;
}
}
}
}
void GameList::ChangeDisc()
{
Core::RunAsCPUThread([this] { DVDInterface::ChangeDisc(GetSelectedGame()->GetFilePath()); });
}
std::shared_ptr<const UICommon::GameFile> GameList::GetSelectedGame() const
{
QAbstractItemView* view;
QSortFilterProxyModel* proxy;
if (currentWidget() == m_list)
{
view = m_list;
proxy = m_list_proxy;
}
else
{
view = m_grid;
proxy = m_grid_proxy;
}
QItemSelectionModel* sel_model = view->selectionModel();
if (sel_model->hasSelection())
{
QModelIndex model_index = proxy->mapToSource(sel_model->selectedIndexes()[0]);
return m_model->GetGameFile(model_index.row());
}
return {};
}
void GameList::SetPreferredView(bool list)
{
m_prefer_list = list;
Settings::Instance().SetPreferredView(list);
ConsiderViewChange();
}
void GameList::ConsiderViewChange()
{
if (m_model->rowCount(QModelIndex()) > 0)
{
if (m_prefer_list)
setCurrentWidget(m_list);
else
setCurrentWidget(m_grid);
}
else
{
setCurrentWidget(m_empty);
}
}
void GameList::keyReleaseEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Return && GetSelectedGame() != nullptr)
emit GameSelected();
else
QStackedWidget::keyReleaseEvent(event);
}
void GameList::OnColumnVisibilityToggled(const QString& row, bool visible)
{
static const QMap<QString, int> rowname_to_col_index = {
{tr("Platform"), GameListModel::COL_PLATFORM},
{tr("Banner"), GameListModel::COL_BANNER},
{tr("Title"), GameListModel::COL_TITLE},
{tr("Description"), GameListModel::COL_DESCRIPTION},
{tr("Maker"), GameListModel::COL_MAKER},
{tr("File Name"), GameListModel::COL_FILE_NAME},
{tr("Game ID"), GameListModel::COL_ID},
{tr("Region"), GameListModel::COL_COUNTRY},
{tr("File Size"), GameListModel::COL_SIZE}};
m_list->setColumnHidden(rowname_to_col_index[row], !visible);
}
void GameList::OnGameListVisibilityChanged()
{
m_list_proxy->invalidate();
m_grid_proxy->invalidate();
}
static bool CompressCB(const std::string& text, float percent, void* ptr)
{
if (ptr == nullptr)
return false;
auto* progress_dialog = static_cast<QProgressDialog*>(ptr);
progress_dialog->setValue(percent * 100);
return !progress_dialog->wasCanceled();
}
void GameList::OnHeaderViewChanged()
{
Settings::GetQSettings().setValue(QStringLiteral("tableheader/state"),
m_list->horizontalHeader()->saveState());
}
void GameList::SetSearchTerm(const QString& term)
{
m_model->SetSearchTerm(term);
m_list_proxy->invalidate();
m_grid_proxy->invalidate();
}