game_list.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QFileInfo>
  5. #include <QHeaderView>
  6. #include <QMenu>
  7. #include <QThreadPool>
  8. #include <QVBoxLayout>
  9. #include "common/common_paths.h"
  10. #include "common/logging/log.h"
  11. #include "common/string_util.h"
  12. #include "core/loader/loader.h"
  13. #include "game_list.h"
  14. #include "game_list_p.h"
  15. #include "ui_settings.h"
  16. GameList::GameList(QWidget* parent) : QWidget{parent} {
  17. QVBoxLayout* layout = new QVBoxLayout;
  18. tree_view = new QTreeView;
  19. item_model = new QStandardItemModel(tree_view);
  20. tree_view->setModel(item_model);
  21. tree_view->setAlternatingRowColors(true);
  22. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  23. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  24. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  25. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  26. tree_view->setSortingEnabled(true);
  27. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  28. tree_view->setUniformRowHeights(true);
  29. tree_view->setContextMenuPolicy(Qt::CustomContextMenu);
  30. item_model->insertColumns(0, COLUMN_COUNT);
  31. item_model->setHeaderData(COLUMN_NAME, Qt::Horizontal, "Name");
  32. item_model->setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, "File type");
  33. item_model->setHeaderData(COLUMN_SIZE, Qt::Horizontal, "Size");
  34. connect(tree_view, &QTreeView::activated, this, &GameList::ValidateEntry);
  35. connect(tree_view, &QTreeView::customContextMenuRequested, this, &GameList::PopupContextMenu);
  36. // We must register all custom types with the Qt Automoc system so that we are able to use it
  37. // with signals/slots. In this case, QList falls under the umbrells of custom types.
  38. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  39. layout->addWidget(tree_view);
  40. setLayout(layout);
  41. }
  42. GameList::~GameList() {
  43. emit ShouldCancelWorker();
  44. }
  45. void GameList::AddEntry(const QList<QStandardItem*>& entry_items) {
  46. item_model->invisibleRootItem()->appendRow(entry_items);
  47. }
  48. void GameList::ValidateEntry(const QModelIndex& item) {
  49. // We don't care about the individual QStandardItem that was selected, but its row.
  50. int row = item_model->itemFromIndex(item)->row();
  51. QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME);
  52. QString file_path = child_file->data(GameListItemPath::FullPathRole).toString();
  53. if (file_path.isEmpty())
  54. return;
  55. std::string std_file_path(file_path.toStdString());
  56. if (!FileUtil::Exists(std_file_path) || FileUtil::IsDirectory(std_file_path))
  57. return;
  58. emit GameChosen(file_path);
  59. }
  60. void GameList::DonePopulating() {
  61. tree_view->setEnabled(true);
  62. }
  63. void GameList::PopupContextMenu(const QPoint& menu_location) {
  64. QModelIndex item = tree_view->indexAt(menu_location);
  65. if (!item.isValid())
  66. return;
  67. int row = item_model->itemFromIndex(item)->row();
  68. QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME);
  69. u64 program_id = child_file->data(GameListItemPath::ProgramIdRole).toULongLong();
  70. QMenu context_menu;
  71. QAction* open_save_location = context_menu.addAction(tr("Open Save Data Location"));
  72. open_save_location->setEnabled(program_id != 0);
  73. connect(open_save_location, &QAction::triggered,
  74. [&]() { emit OpenSaveFolderRequested(program_id); });
  75. context_menu.exec(tree_view->viewport()->mapToGlobal(menu_location));
  76. }
  77. void GameList::PopulateAsync(const QString& dir_path, bool deep_scan) {
  78. if (!FileUtil::Exists(dir_path.toStdString()) ||
  79. !FileUtil::IsDirectory(dir_path.toStdString())) {
  80. LOG_ERROR(Frontend, "Could not find game list folder at %s", dir_path.toLocal8Bit().data());
  81. return;
  82. }
  83. tree_view->setEnabled(false);
  84. // Delete any rows that might already exist if we're repopulating
  85. item_model->removeRows(0, item_model->rowCount());
  86. emit ShouldCancelWorker();
  87. GameListWorker* worker = new GameListWorker(dir_path, deep_scan);
  88. connect(worker, &GameListWorker::EntryReady, this, &GameList::AddEntry, Qt::QueuedConnection);
  89. connect(worker, &GameListWorker::Finished, this, &GameList::DonePopulating,
  90. Qt::QueuedConnection);
  91. // Use DirectConnection here because worker->Cancel() is thread-safe and we want it to cancel
  92. // without delay.
  93. connect(this, &GameList::ShouldCancelWorker, worker, &GameListWorker::Cancel,
  94. Qt::DirectConnection);
  95. QThreadPool::globalInstance()->start(worker);
  96. current_worker = std::move(worker);
  97. }
  98. void GameList::SaveInterfaceLayout() {
  99. UISettings::values.gamelist_header_state = tree_view->header()->saveState();
  100. }
  101. void GameList::LoadInterfaceLayout() {
  102. auto header = tree_view->header();
  103. if (!header->restoreState(UISettings::values.gamelist_header_state)) {
  104. // We are using the name column to display icons and titles
  105. // so make it as large as possible as default.
  106. header->resizeSection(COLUMN_NAME, header->width());
  107. }
  108. item_model->sort(header->sortIndicatorSection(), header->sortIndicatorOrder());
  109. }
  110. const QStringList GameList::supported_file_extensions = {"3ds", "3dsx", "elf", "axf",
  111. "cci", "cxi", "app"};
  112. static bool HasSupportedFileExtension(const std::string& file_name) {
  113. QFileInfo file = QFileInfo(file_name.c_str());
  114. return GameList::supported_file_extensions.contains(file.completeSuffix(), Qt::CaseInsensitive);
  115. }
  116. void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, unsigned int recursion) {
  117. const auto callback = [this, recursion](unsigned* num_entries_out, const std::string& directory,
  118. const std::string& virtual_name) -> bool {
  119. std::string physical_name = directory + DIR_SEP + virtual_name;
  120. if (stop_processing)
  121. return false; // Breaks the callback loop.
  122. if (!FileUtil::IsDirectory(physical_name) && HasSupportedFileExtension(physical_name)) {
  123. std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(physical_name);
  124. if (!loader)
  125. return true;
  126. std::vector<u8> smdh;
  127. loader->ReadIcon(smdh);
  128. u64 program_id = 0;
  129. loader->ReadProgramId(program_id);
  130. emit EntryReady({
  131. new GameListItemPath(QString::fromStdString(physical_name), smdh, program_id),
  132. new GameListItem(
  133. QString::fromStdString(Loader::GetFileTypeString(loader->GetFileType()))),
  134. new GameListItemSize(FileUtil::GetSize(physical_name)),
  135. });
  136. } else if (recursion > 0) {
  137. AddFstEntriesToGameList(physical_name, recursion - 1);
  138. }
  139. return true;
  140. };
  141. FileUtil::ForeachDirectoryEntry(nullptr, dir_path, callback);
  142. }
  143. void GameListWorker::run() {
  144. stop_processing = false;
  145. AddFstEntriesToGameList(dir_path.toStdString(), deep_scan ? 256 : 0);
  146. emit Finished();
  147. }
  148. void GameListWorker::Cancel() {
  149. disconnect(this, nullptr, nullptr, nullptr);
  150. stop_processing = true;
  151. }