game_list_p.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <utility>
  7. #include <QImage>
  8. #include <QRunnable>
  9. #include <QStandardItem>
  10. #include <QString>
  11. #include "common/string_util.h"
  12. #include "yuzu/util/util.h"
  13. /**
  14. * Gets the default icon (for games without valid SMDH)
  15. * @param large If true, returns large icon (48x48), otherwise returns small icon (24x24)
  16. * @return QPixmap default icon
  17. */
  18. static QPixmap GetDefaultIcon(bool large) {
  19. int size = large ? 48 : 24;
  20. QPixmap icon(size, size);
  21. icon.fill(Qt::transparent);
  22. return icon;
  23. }
  24. class GameListItem : public QStandardItem {
  25. public:
  26. GameListItem() = default;
  27. explicit GameListItem(const QString& string) : QStandardItem(string) {}
  28. };
  29. /**
  30. * A specialization of GameListItem for path values.
  31. * This class ensures that for every full path value it holds, a correct string representation
  32. * of just the filename (with no extension) will be displayed to the user.
  33. * If this class receives valid SMDH data, it will also display game icons and titles.
  34. */
  35. class GameListItemPath : public GameListItem {
  36. public:
  37. static const int FullPathRole = Qt::UserRole + 1;
  38. static const int TitleRole = Qt::UserRole + 2;
  39. static const int ProgramIdRole = Qt::UserRole + 3;
  40. GameListItemPath() = default;
  41. GameListItemPath(const QString& game_path, const std::vector<u8>& smdh_data, u64 program_id) {
  42. setData(game_path, FullPathRole);
  43. setData(qulonglong(program_id), ProgramIdRole);
  44. }
  45. QVariant data(int role) const override {
  46. if (role == Qt::DisplayRole) {
  47. std::string filename;
  48. Common::SplitPath(data(FullPathRole).toString().toStdString(), nullptr, &filename,
  49. nullptr);
  50. QString title = data(TitleRole).toString();
  51. return QString::fromStdString(filename) + (title.isEmpty() ? "" : "\n " + title);
  52. } else {
  53. return GameListItem::data(role);
  54. }
  55. }
  56. };
  57. /**
  58. * A specialization of GameListItem for size values.
  59. * This class ensures that for every numerical size value it holds (in bytes), a correct
  60. * human-readable string representation will be displayed to the user.
  61. */
  62. class GameListItemSize : public GameListItem {
  63. public:
  64. static const int SizeRole = Qt::UserRole + 1;
  65. GameListItemSize() = default;
  66. explicit GameListItemSize(const qulonglong size_bytes) {
  67. setData(size_bytes, SizeRole);
  68. }
  69. void setData(const QVariant& value, int role) override {
  70. // By specializing setData for SizeRole, we can ensure that the numerical and string
  71. // representations of the data are always accurate and in the correct format.
  72. if (role == SizeRole) {
  73. qulonglong size_bytes = value.toULongLong();
  74. GameListItem::setData(ReadableByteSize(size_bytes), Qt::DisplayRole);
  75. GameListItem::setData(value, SizeRole);
  76. } else {
  77. GameListItem::setData(value, role);
  78. }
  79. }
  80. /**
  81. * This operator is, in practice, only used by the TreeView sorting systems.
  82. * Override it so that it will correctly sort by numerical value instead of by string
  83. * representation.
  84. */
  85. bool operator<(const QStandardItem& other) const override {
  86. return data(SizeRole).toULongLong() < other.data(SizeRole).toULongLong();
  87. }
  88. };
  89. /**
  90. * Asynchronous worker object for populating the game list.
  91. * Communicates with other threads through Qt's signal/slot system.
  92. */
  93. class GameListWorker : public QObject, public QRunnable {
  94. Q_OBJECT
  95. public:
  96. GameListWorker(QString dir_path, bool deep_scan)
  97. : dir_path(std::move(dir_path)), deep_scan(deep_scan) {}
  98. public slots:
  99. /// Starts the processing of directory tree information.
  100. void run() override;
  101. /// Tells the worker that it should no longer continue processing. Thread-safe.
  102. void Cancel();
  103. signals:
  104. /**
  105. * The `EntryReady` signal is emitted once an entry has been prepared and is ready
  106. * to be added to the game list.
  107. * @param entry_items a list with `QStandardItem`s that make up the columns of the new entry.
  108. */
  109. void EntryReady(QList<QStandardItem*> entry_items);
  110. /**
  111. * After the worker has traversed the game directory looking for entries, this signal is emmited
  112. * with a list of folders that should be watched for changes as well.
  113. */
  114. void Finished(QStringList watch_list);
  115. private:
  116. QStringList watch_list;
  117. QString dir_path;
  118. bool deep_scan;
  119. std::atomic_bool stop_processing;
  120. void AddFstEntriesToGameList(const std::string& dir_path, unsigned int recursion = 0);
  121. };