all repos — mgba @ 9b6fc4482555e1a9e06b088604ebaabbc8325e2b

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
 32LibraryLoaderThread::LibraryLoaderThread(QObject* parent)
 33	: QThread(parent)
 34{
 35}
 36
 37void LibraryLoaderThread::run() {
 38	mLibraryLoadDirectory(m_library, m_directory.toUtf8().constData());
 39	m_directory = QString();
 40}
 41
 42LibraryController::LibraryController(QWidget* parent, const QString& path, ConfigController* config)
 43	: QStackedWidget(parent)
 44	, m_config(config)
 45{
 46	mLibraryListingInit(&m_listing, 0);
 47
 48	if (!path.isNull()) {
 49		// This can return NULL if the library is already open
 50		m_library = mLibraryLoad(path.toUtf8().constData());
 51	}
 52	if (!m_library) {
 53		m_library = mLibraryCreateEmpty();
 54	}
 55
 56	mLibraryAttachGameDB(m_library, GBAApp::app()->gameDB());
 57
 58	m_libraryTree = new LibraryTree(this);
 59	addWidget(m_libraryTree->widget());
 60
 61	m_libraryGrid = new LibraryGrid(this);
 62	addWidget(m_libraryGrid->widget());
 63
 64	connect(&m_loaderThread, &QThread::finished, this, &LibraryController::refresh, Qt::QueuedConnection);
 65
 66	setViewStyle(LibraryStyle::STYLE_LIST);
 67	refresh();
 68}
 69
 70LibraryController::~LibraryController() {
 71	mLibraryListingDeinit(&m_listing);
 72
 73	if (m_loaderThread.isRunning()) {
 74		m_loaderThread.wait();
 75	}
 76	if (!m_loaderThread.isRunning() && m_loaderThread.m_library) {
 77		m_library = m_loaderThread.m_library;
 78		m_loaderThread.m_library = nullptr;
 79	}
 80	if (m_library) {
 81		mLibraryDestroy(m_library);
 82	}
 83}
 84
 85void LibraryController::setViewStyle(LibraryStyle newStyle) {
 86	m_currentStyle = newStyle;
 87
 88	AbstractGameList* newCurrentList = nullptr;
 89	if (newStyle == LibraryStyle::STYLE_LIST || newStyle == LibraryStyle::STYLE_TREE) {
 90		newCurrentList = m_libraryTree;
 91	} else {
 92		newCurrentList = m_libraryGrid;
 93	}
 94	newCurrentList->selectEntry(selectedEntry());
 95	newCurrentList->setViewStyle(newStyle);
 96	setCurrentWidget(newCurrentList->widget());
 97	m_currentList = newCurrentList;
 98}
 99
100void LibraryController::selectEntry(LibraryEntryRef entry) {
101	if (!m_currentList) {
102		return;
103	}
104	m_currentList->selectEntry(entry);
105}
106
107LibraryEntryRef LibraryController::selectedEntry() {
108	if (!m_currentList) {
109		return LibraryEntryRef();
110	}
111	return m_currentList->selectedEntry();
112}
113
114VFile* LibraryController::selectedVFile() {
115	LibraryEntryRef entry = selectedEntry();
116	if (entry) {
117		return mLibraryOpenVFile(m_library, entry->entry);
118	} else {
119		return nullptr;
120	}
121}
122
123QPair<QString, QString> LibraryController::selectedPath() {
124	LibraryEntryRef e = selectedEntry();
125	return e ? qMakePair(e->base(), e->filename()) : qMakePair<QString, QString>("", "");
126}
127
128void LibraryController::addDirectory(const QString& dir) {
129	m_loaderThread.m_directory = dir;
130	m_loaderThread.m_library = m_library;
131	// The m_loaderThread temporarily owns the library
132	m_library = nullptr;
133	m_loaderThread.start();
134}
135
136void LibraryController::clear() {
137	if (!m_library) {
138		if (!m_loaderThread.isRunning() && m_loaderThread.m_library) {
139			m_library = m_loaderThread.m_library;
140			m_loaderThread.m_library = nullptr;
141		} else {
142			return;
143		}
144	}
145
146	mLibraryClear(m_library);
147	refresh();
148}
149
150void LibraryController::refresh() {
151	if (!m_library) {
152		if (!m_loaderThread.isRunning() && m_loaderThread.m_library) {
153			m_library = m_loaderThread.m_library;
154			m_loaderThread.m_library = nullptr;
155		} else {
156			return;
157		}
158	}
159
160	setDisabled(true);
161
162	QStringList allEntries;
163	QList<LibraryEntryRef> newEntries;
164
165	mLibraryListingClear(&m_listing);
166	mLibraryGetEntries(m_library, &m_listing, 0, 0, nullptr);
167	for (size_t i = 0; i < mLibraryListingSize(&m_listing); i++) {
168		mLibraryEntry* entry = mLibraryListingGetPointer(&m_listing, i);
169		QString fullpath = QString("%1/%2").arg(entry->base, entry->filename);
170		if (m_entries.contains(fullpath)) {
171			m_entries.value(fullpath)->entry = entry;
172		} else {
173			LibraryEntryRef libentry = std::make_shared<LibraryEntry>(entry);
174			m_entries.insert(fullpath, libentry);
175			newEntries.append(libentry);
176		}
177		allEntries.append(fullpath);
178	}
179
180	// Check for entries that were removed
181	QList<LibraryEntryRef> removedEntries;
182	for (QString& path : m_entries.keys()) {
183		if (!allEntries.contains(path)) {
184			removedEntries.append(m_entries.value(path));
185			m_entries.remove(path);
186		}
187	}
188
189	m_libraryTree->addEntries(newEntries);
190	m_libraryGrid->addEntries(newEntries);
191
192	m_libraryTree->removeEntries(removedEntries);
193	m_libraryGrid->removeEntries(removedEntries);
194
195	setDisabled(false);
196	selectLastBootedGame();
197	emit doneLoading();
198}
199
200void LibraryController::selectLastBootedGame() {
201	if (!m_config || m_config->getMRU().isEmpty()) {
202		return;
203	}
204	const QString lastfile = m_config->getMRU().first();
205	if (m_entries.contains(lastfile)) {
206		selectEntry(m_entries.value(lastfile));
207	}
208}
209
210}