all repos — mgba @ c94aff135f2f80a5384b470b70520fd7fcfccf83

mGBA Game Boy Advance Emulator

src/platform/qt/library/LibraryController.cpp (view raw)

  1/* Copyright (c) 2014-2017 waddlesplash
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "LibraryController.h"
  7
  8#include "ConfigController.h"
  9#include "GBAApp.h"
 10#include "LibraryGrid.h"
 11#include "LibraryTree.h"
 12
 13namespace QGBA {
 14
 15LibraryEntry::LibraryEntry(mLibraryEntry* entry)
 16	: entry(entry)
 17	, m_fullpath(QString("%1/%2").arg(entry->base, entry->filename))
 18{
 19}
 20
 21void AbstractGameList::addEntries(QList<LibraryEntryRef> items) {
 22	for (LibraryEntryRef o : items) {
 23		addEntry(o);
 24	}
 25}
 26void AbstractGameList::removeEntries(QList<LibraryEntryRef> items) {
 27	for (LibraryEntryRef o : items) {
 28		removeEntry(o);
 29	}
 30}
 31
 32LibraryController::LibraryController(QWidget* parent, const QString& path, ConfigController* config)
 33	: QStackedWidget(parent)
 34	, m_config(config)
 35{
 36	mLibraryListingInit(&m_listing, 0);
 37
 38	if (!path.isNull()) {
 39		// This can return NULL if the library is already open
 40		m_library = std::shared_ptr<mLibrary>(mLibraryLoad(path.toUtf8().constData()), mLibraryDestroy);
 41	}
 42	if (!m_library) {
 43		m_library = std::shared_ptr<mLibrary>(mLibraryCreateEmpty(), mLibraryDestroy);
 44	}
 45
 46	mLibraryAttachGameDB(m_library.get(), GBAApp::app()->gameDB());
 47
 48	m_libraryTree = new LibraryTree(this);
 49	addWidget(m_libraryTree->widget());
 50
 51	m_libraryGrid = new LibraryGrid(this);
 52	addWidget(m_libraryGrid->widget());
 53
 54	setViewStyle(LibraryStyle::STYLE_LIST);
 55	refresh();
 56}
 57
 58LibraryController::~LibraryController() {
 59	mLibraryListingDeinit(&m_listing);
 60}
 61
 62void LibraryController::setViewStyle(LibraryStyle newStyle) {
 63	if (m_currentStyle == newStyle) {
 64		return;
 65	}
 66	m_currentStyle = newStyle;
 67
 68	AbstractGameList* newCurrentList = nullptr;
 69	if (newStyle == LibraryStyle::STYLE_LIST || newStyle == LibraryStyle::STYLE_TREE) {
 70		newCurrentList = m_libraryTree;
 71	} else {
 72		newCurrentList = m_libraryGrid;
 73	}
 74	newCurrentList->selectEntry(selectedEntry());
 75	newCurrentList->setViewStyle(newStyle);
 76	setCurrentWidget(newCurrentList->widget());
 77	m_currentList = newCurrentList;
 78}
 79
 80void LibraryController::selectEntry(LibraryEntryRef entry) {
 81	if (!m_currentList) {
 82		return;
 83	}
 84	m_currentList->selectEntry(entry);
 85}
 86
 87LibraryEntryRef LibraryController::selectedEntry() {
 88	if (!m_currentList) {
 89		return LibraryEntryRef();
 90	}
 91	return m_currentList->selectedEntry();
 92}
 93
 94VFile* LibraryController::selectedVFile() {
 95	LibraryEntryRef entry = selectedEntry();
 96	if (entry) {
 97		return mLibraryOpenVFile(m_library.get(), entry->entry);
 98	} else {
 99		return nullptr;
100	}
101}
102
103QPair<QString, QString> LibraryController::selectedPath() {
104	LibraryEntryRef e = selectedEntry();
105	return e ? qMakePair(e->base(), e->filename()) : qMakePair<QString, QString>("", "");
106}
107
108void LibraryController::addDirectory(const QString& dir) {
109	// The worker thread temporarily owns the library
110	std::shared_ptr<mLibrary> library = m_library;
111	m_libraryJob = GBAApp::app()->submitWorkerJob(std::bind(&LibraryController::loadDirectory, this, dir), this, [this, library]() {
112		m_libraryJob = -1;
113		refresh();
114	});
115}
116
117void LibraryController::clear() {
118	if (m_libraryJob > 0) {
119		return;
120	}
121
122	mLibraryClear(m_library.get());
123	refresh();
124}
125
126void LibraryController::refresh() {
127	if (m_libraryJob > 0) {
128		return;
129	}
130
131	setDisabled(true);
132
133	QStringList allEntries;
134	QList<LibraryEntryRef> newEntries;
135
136	mLibraryListingClear(&m_listing);
137	mLibraryGetEntries(m_library.get(), &m_listing, 0, 0, nullptr);
138	for (size_t i = 0; i < mLibraryListingSize(&m_listing); i++) {
139		mLibraryEntry* entry = mLibraryListingGetPointer(&m_listing, i);
140		QString fullpath = QString("%1/%2").arg(entry->base, entry->filename);
141		if (m_entries.contains(fullpath)) {
142			m_entries.value(fullpath)->entry = entry;
143		} else {
144			LibraryEntryRef libentry = std::make_shared<LibraryEntry>(entry);
145			m_entries.insert(fullpath, libentry);
146			newEntries.append(libentry);
147		}
148		allEntries.append(fullpath);
149	}
150
151	// Check for entries that were removed
152	QList<LibraryEntryRef> removedEntries;
153	for (QString& path : m_entries.keys()) {
154		if (!allEntries.contains(path)) {
155			removedEntries.append(m_entries.value(path));
156			m_entries.remove(path);
157		}
158	}
159
160	m_libraryTree->addEntries(newEntries);
161	m_libraryGrid->addEntries(newEntries);
162
163	m_libraryTree->removeEntries(removedEntries);
164	m_libraryGrid->removeEntries(removedEntries);
165
166	setDisabled(false);
167	selectLastBootedGame();
168	emit doneLoading();
169}
170
171void LibraryController::selectLastBootedGame() {
172	if (!m_config || m_config->getMRU().isEmpty()) {
173		return;
174	}
175	const QString lastfile = m_config->getMRU().first();
176	if (m_entries.contains(lastfile)) {
177		selectEntry(m_entries.value(lastfile));
178	}
179}
180
181void LibraryController::loadDirectory(const QString& dir) {
182	// This class can get delted during this function (sigh) so we need to hold onto this
183	std::shared_ptr<mLibrary> library = m_library;
184	mLibraryLoadDirectory(library.get(), dir.toUtf8().constData());
185}
186
187}