1 /* Copyright (c) 2014-2017 waddlesplash
2  * Copyright (c) 2014-2021 Jeffrey Pfau
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #pragma once
8 
9 #include <memory>
10 
11 #include <QAtomicInteger>
12 #include <QHash>
13 #include <QList>
14 #include <QStackedWidget>
15 
16 #include <mgba/core/library.h>
17 
18 namespace QGBA {
19 
20 // Predefinitions
21 class LibraryGrid;
22 class LibraryTree;
23 class ConfigController;
24 
25 enum class LibraryStyle {
26 	STYLE_LIST = 0,
27 	STYLE_TREE,
28 	STYLE_GRID,
29 	STYLE_ICON
30 };
31 
32 class AbstractGameList {
33 public:
34 	virtual mLibraryEntry* selectedEntry() = 0;
35 	virtual void selectEntry(mLibraryEntry* game) = 0;
36 
37 	virtual void setViewStyle(LibraryStyle newStyle) = 0;
38 
39 	virtual void addEntry(mLibraryEntry* item) = 0;
40 	virtual void addEntries(QList<mLibraryEntry*> items);
41 
42 	virtual void removeEntry(mLibraryEntry* item) = 0;
43 	virtual void removeEntries(QList<mLibraryEntry*> items);
44 
45 	virtual QWidget* widget() = 0;
46 };
47 
48 class LibraryController final : public QStackedWidget {
49 Q_OBJECT
50 
51 public:
52 	LibraryController(QWidget* parent = nullptr, const QString& path = QString(),
53 	    ConfigController* config = nullptr);
54 	~LibraryController();
55 
viewStyle()56 	LibraryStyle viewStyle() const { return m_currentStyle; }
57 	void setViewStyle(LibraryStyle newStyle);
58 
59 	void selectEntry(mLibraryEntry* entry);
60 	mLibraryEntry* selectedEntry();
61 	VFile* selectedVFile();
62 	QPair<QString, QString> selectedPath();
63 
64 	void selectLastBootedGame();
65 
66 	void addDirectory(const QString& dir, bool recursive = true);
67 
68 public slots:
69 	void clear();
70 
71 signals:
72 	void startGame();
73 	void doneLoading();
74 
75 private slots:
76 	void refresh();
77 
78 private:
79 	void loadDirectory(const QString&, bool recursive = true); // Called on separate thread
80 	void freeLibrary();
81 
82 	ConfigController* m_config = nullptr;
83 	std::shared_ptr<mLibrary> m_library;
84 	QAtomicInteger<qint64> m_libraryJob = -1;
85 	mLibraryListing m_listing;
86 	QHash<QString, mLibraryEntry*> m_entries;
87 
88 	LibraryStyle m_currentStyle;
89 	AbstractGameList* m_currentList = nullptr;
90 
91 	std::unique_ptr<LibraryGrid> m_libraryGrid;
92 	std::unique_ptr<LibraryTree> m_libraryTree;
93 };
94 
95 }
96