summaryrefslogtreecommitdiff
path: root/src/citra_qt/game_list.cpp
diff options
context:
space:
mode:
authorGravatar archshift2015-08-31 21:35:33 -0700
committerGravatar archshift2015-10-01 19:39:14 -0700
commit6e1bb58ee8ace739615e42cff02f07ee396edb6e (patch)
tree17b5eb9e4319d82e7e0a5683095ad7800941c79c /src/citra_qt/game_list.cpp
parentAdd helper function for creating a readable byte size string. (diff)
downloadyuzu-6e1bb58ee8ace739615e42cff02f07ee396edb6e.tar.gz
yuzu-6e1bb58ee8ace739615e42cff02f07ee396edb6e.tar.xz
yuzu-6e1bb58ee8ace739615e42cff02f07ee396edb6e.zip
Initial implementation of a game list
Diffstat (limited to 'src/citra_qt/game_list.cpp')
-rw-r--r--src/citra_qt/game_list.cpp154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/citra_qt/game_list.cpp b/src/citra_qt/game_list.cpp
new file mode 100644
index 000000000..f90e05374
--- /dev/null
+++ b/src/citra_qt/game_list.cpp
@@ -0,0 +1,154 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <QHeaderView>
6#include <QThreadPool>
7#include <QVBoxLayout>
8
9#include "game_list.h"
10#include "game_list_p.h"
11
12#include "core/loader/loader.h"
13
14#include "common/common_paths.h"
15#include "common/logging/log.h"
16#include "common/string_util.h"
17
18GameList::GameList(QWidget* parent)
19{
20 QVBoxLayout* layout = new QVBoxLayout;
21
22 tree_view = new QTreeView;
23 item_model = new QStandardItemModel(tree_view);
24 tree_view->setModel(item_model);
25
26 tree_view->setAlternatingRowColors(true);
27 tree_view->setSelectionMode(QHeaderView::SingleSelection);
28 tree_view->setSelectionBehavior(QHeaderView::SelectRows);
29 tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
30 tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
31 tree_view->setSortingEnabled(true);
32 tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
33 tree_view->setUniformRowHeights(true);
34
35 item_model->insertColumns(0, COLUMN_COUNT);
36 item_model->setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, "File type");
37 item_model->setHeaderData(COLUMN_NAME, Qt::Horizontal, "Name");
38 item_model->setHeaderData(COLUMN_SIZE, Qt::Horizontal, "Size");
39
40 connect(tree_view, SIGNAL(activated(const QModelIndex&)), this, SLOT(ValidateEntry(const QModelIndex&)));
41
42 // We must register all custom types with the Qt Automoc system so that we are able to use it with
43 // signals/slots. In this case, QList falls under the umbrells of custom types.
44 qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
45
46 layout->addWidget(tree_view);
47 setLayout(layout);
48}
49
50GameList::~GameList()
51{
52 emit ShouldCancelWorker();
53}
54
55void GameList::AddEntry(QList<QStandardItem*> entry_items)
56{
57 item_model->invisibleRootItem()->appendRow(entry_items);
58}
59
60void GameList::ValidateEntry(const QModelIndex& item)
61{
62 // We don't care about the individual QStandardItem that was selected, but its row.
63 int row = item_model->itemFromIndex(item)->row();
64 QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME);
65 QString file_path = child_file->data(GameListItemPath::FullPathRole).toString();
66
67 if (file_path.isEmpty())
68 return;
69 std::string std_file_path = file_path.toStdString();
70 if (!FileUtil::Exists(std_file_path) || FileUtil::IsDirectory(std_file_path))
71 return;
72 emit GameChosen(file_path);
73}
74
75void GameList::DonePopulating()
76{
77 tree_view->setEnabled(true);
78}
79
80void GameList::PopulateAsync(const QString& dir_path, bool deep_scan)
81{
82 if (!FileUtil::Exists(dir_path.toStdString()) || !FileUtil::IsDirectory(dir_path.toStdString())) {
83 LOG_ERROR(Frontend, "Could not find game list folder at %s", dir_path.toLatin1().data());
84 return;
85 }
86
87 tree_view->setEnabled(false);
88 // Delete any rows that might already exist if we're repopulating
89 item_model->removeRows(0, item_model->rowCount());
90
91 emit ShouldCancelWorker();
92 GameListWorker* worker = new GameListWorker(dir_path, deep_scan);
93
94 connect(worker, SIGNAL(EntryReady(QList<QStandardItem*>)), this, SLOT(AddEntry(QList<QStandardItem*>)), Qt::QueuedConnection);
95 connect(worker, SIGNAL(Finished()), this, SLOT(DonePopulating()), Qt::QueuedConnection);
96 // Use DirectConnection here because worker->Cancel() is thread-safe and we want it to cancel without delay.
97 connect(this, SIGNAL(ShouldCancelWorker()), worker, SLOT(Cancel()), Qt::DirectConnection);
98
99 QThreadPool::globalInstance()->start(worker);
100 current_worker = std::move(worker);
101}
102
103void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan)
104{
105 const auto callback = [&](const std::string& directory,
106 const std::string& virtual_name) -> int {
107
108 std::string physical_name = directory + DIR_SEP + virtual_name;
109
110 if (stop_processing)
111 return -1; // A negative return value breaks the callback loop.
112
113 if (deep_scan && FileUtil::IsDirectory(physical_name)) {
114 AddFstEntriesToGameList(physical_name, true);
115 } else {
116 std::string filename_filename, filename_extension;
117 Common::SplitPath(physical_name, nullptr, &filename_filename, &filename_extension);
118
119 Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension);
120 if (guessed_filetype == Loader::FileType::Unknown)
121 return 0;
122 Loader::FileType filetype = Loader::IdentifyFile(physical_name);
123 if (filetype == Loader::FileType::Unknown) {
124 LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str());
125 return 0;
126 }
127 if (guessed_filetype != filetype) {
128 LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str());
129 }
130
131 emit EntryReady({
132 new GameListItem(QString::fromStdString(Loader::GetFileTypeString(filetype))),
133 new GameListItemPath(QString::fromStdString(physical_name)),
134 new GameListItemSize(FileUtil::GetSize(physical_name)),
135 });
136 }
137
138 return 0; // We don't care about the found entries
139 };
140 FileUtil::ScanDirectoryTreeAndCallback(dir_path, callback);
141}
142
143void GameListWorker::run()
144{
145 stop_processing = false;
146 AddFstEntriesToGameList(dir_path.toStdString(), deep_scan);
147 emit Finished();
148}
149
150void GameListWorker::Cancel()
151{
152 disconnect(this, 0, 0, 0);
153 stop_processing = true;
154}