game_list.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QHeaderView>
  5. #include <QThreadPool>
  6. #include <QVBoxLayout>
  7. #include "game_list.h"
  8. #include "game_list_p.h"
  9. #include "core/loader/loader.h"
  10. #include "common/common_paths.h"
  11. #include "common/logging/log.h"
  12. #include "common/string_util.h"
  13. GameList::GameList(QWidget* parent)
  14. {
  15. QVBoxLayout* layout = new QVBoxLayout;
  16. tree_view = new QTreeView;
  17. item_model = new QStandardItemModel(tree_view);
  18. tree_view->setModel(item_model);
  19. tree_view->setAlternatingRowColors(true);
  20. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  21. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  22. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  23. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  24. tree_view->setSortingEnabled(true);
  25. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  26. tree_view->setUniformRowHeights(true);
  27. item_model->insertColumns(0, COLUMN_COUNT);
  28. item_model->setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, "File type");
  29. item_model->setHeaderData(COLUMN_NAME, Qt::Horizontal, "Name");
  30. item_model->setHeaderData(COLUMN_SIZE, Qt::Horizontal, "Size");
  31. connect(tree_view, SIGNAL(activated(const QModelIndex&)), this, SLOT(ValidateEntry(const QModelIndex&)));
  32. // We must register all custom types with the Qt Automoc system so that we are able to use it with
  33. // signals/slots. In this case, QList falls under the umbrells of custom types.
  34. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  35. layout->addWidget(tree_view);
  36. setLayout(layout);
  37. }
  38. GameList::~GameList()
  39. {
  40. emit ShouldCancelWorker();
  41. }
  42. void GameList::AddEntry(QList<QStandardItem*> entry_items)
  43. {
  44. item_model->invisibleRootItem()->appendRow(entry_items);
  45. }
  46. void GameList::ValidateEntry(const QModelIndex& item)
  47. {
  48. // We don't care about the individual QStandardItem that was selected, but its row.
  49. int row = item_model->itemFromIndex(item)->row();
  50. QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME);
  51. QString file_path = child_file->data(GameListItemPath::FullPathRole).toString();
  52. if (file_path.isEmpty())
  53. return;
  54. std::string std_file_path(file_path.toLocal8Bit());
  55. if (!FileUtil::Exists(std_file_path) || FileUtil::IsDirectory(std_file_path))
  56. return;
  57. emit GameChosen(file_path);
  58. }
  59. void GameList::DonePopulating()
  60. {
  61. tree_view->setEnabled(true);
  62. }
  63. void GameList::PopulateAsync(const QString& dir_path, bool deep_scan)
  64. {
  65. if (!FileUtil::Exists(dir_path.toStdString()) || !FileUtil::IsDirectory(dir_path.toStdString())) {
  66. LOG_ERROR(Frontend, "Could not find game list folder at %s", dir_path.toLocal8Bit().data());
  67. return;
  68. }
  69. tree_view->setEnabled(false);
  70. // Delete any rows that might already exist if we're repopulating
  71. item_model->removeRows(0, item_model->rowCount());
  72. emit ShouldCancelWorker();
  73. GameListWorker* worker = new GameListWorker(dir_path, deep_scan);
  74. connect(worker, SIGNAL(EntryReady(QList<QStandardItem*>)), this, SLOT(AddEntry(QList<QStandardItem*>)), Qt::QueuedConnection);
  75. connect(worker, SIGNAL(Finished()), this, SLOT(DonePopulating()), Qt::QueuedConnection);
  76. // Use DirectConnection here because worker->Cancel() is thread-safe and we want it to cancel without delay.
  77. connect(this, SIGNAL(ShouldCancelWorker()), worker, SLOT(Cancel()), Qt::DirectConnection);
  78. QThreadPool::globalInstance()->start(worker);
  79. current_worker = std::move(worker);
  80. }
  81. void GameList::SaveInterfaceLayout(QSettings& settings)
  82. {
  83. settings.beginGroup("UILayout");
  84. settings.setValue("gameListHeaderState", tree_view->header()->saveState());
  85. settings.endGroup();
  86. }
  87. void GameList::LoadInterfaceLayout(QSettings& settings)
  88. {
  89. auto header = tree_view->header();
  90. settings.beginGroup("UILayout");
  91. header->restoreState(settings.value("gameListHeaderState").toByteArray());
  92. settings.endGroup();
  93. item_model->sort(header->sortIndicatorSection(), header->sortIndicatorOrder());
  94. }
  95. void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan)
  96. {
  97. const auto callback = [&](unsigned* num_entries_out,
  98. const std::string& directory,
  99. const std::string& virtual_name) -> bool {
  100. std::string physical_name = directory + DIR_SEP + virtual_name;
  101. if (stop_processing)
  102. return false; // Breaks the callback loop.
  103. if (deep_scan && FileUtil::IsDirectory(physical_name)) {
  104. AddFstEntriesToGameList(physical_name, true);
  105. } else {
  106. std::string filename_filename, filename_extension;
  107. Common::SplitPath(physical_name, nullptr, &filename_filename, &filename_extension);
  108. Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension);
  109. if (guessed_filetype == Loader::FileType::Unknown)
  110. return true;
  111. Loader::FileType filetype = Loader::IdentifyFile(physical_name);
  112. if (filetype == Loader::FileType::Unknown) {
  113. LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str());
  114. return true;
  115. }
  116. if (guessed_filetype != filetype) {
  117. LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str());
  118. }
  119. emit EntryReady({
  120. new GameListItem(QString::fromStdString(Loader::GetFileTypeString(filetype))),
  121. new GameListItemPath(QString::fromLocal8Bit(physical_name.c_str())),
  122. new GameListItemSize(FileUtil::GetSize(physical_name)),
  123. });
  124. }
  125. return true;
  126. };
  127. FileUtil::ForeachDirectoryEntry(nullptr, dir_path, callback);
  128. }
  129. void GameListWorker::run()
  130. {
  131. stop_processing = false;
  132. AddFstEntriesToGameList(dir_path.toStdString(), deep_scan);
  133. emit Finished();
  134. }
  135. void GameListWorker::Cancel()
  136. {
  137. disconnect(this, 0, 0, 0);
  138. stop_processing = true;
  139. }