all repos — mgba @ 25a8b125ed8c75a3891f7a1a23ac664c5c5fb598

mGBA Game Boy Advance Emulator

src/platform/qt/LibraryModel.cpp (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  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 "LibraryModel.h"
  7
  8#include <QFontMetrics>
  9
 10#include <mgba-util/vfs.h>
 11
 12using namespace QGBA;
 13
 14Q_DECLARE_METATYPE(mLibraryEntry);
 15
 16QMap<QString, LibraryModel::LibraryHandle*> LibraryModel::s_handles;
 17QMap<QString, LibraryModel::LibraryColumn> LibraryModel::s_columns;
 18
 19LibraryModel::LibraryModel(const QString& path, QObject* parent)
 20	: QAbstractItemModel(parent)
 21{
 22	if (s_columns.empty()) {
 23		s_columns["name"] = {
 24			tr("Name"),
 25			[](const mLibraryEntry& e) -> QString {
 26				if (e.title) {
 27					return QString::fromUtf8(e.title);
 28				}
 29				return QString::fromUtf8(e.filename);
 30			}
 31		};
 32		s_columns["filename"] = {
 33			tr("Filename"),
 34			[](const mLibraryEntry& e) -> QString {
 35				return QString::fromUtf8(e.filename);
 36			}
 37		};
 38		s_columns["size"] = {
 39			tr("Size"),
 40			[](const mLibraryEntry& e) -> QString {
 41				double size = e.filesize;
 42				QString unit = "B";
 43				if (size >= 1024.0) {
 44					size /= 1024.0;
 45					unit = "kiB";
 46				}
 47				if (size >= 1024.0) {
 48					size /= 1024.0;
 49					unit = "MiB";
 50				}
 51				return QString("%0 %1").arg(size, 0, 'f', 1).arg(unit);
 52			},
 53			Qt::AlignRight
 54		};
 55		s_columns["platform"] = {
 56			tr("Platform"),
 57			[](const mLibraryEntry& e) -> QString {
 58				int platform = e.platform;
 59				switch (platform) {
 60#ifdef M_CORE_GBA
 61				case PLATFORM_GBA:
 62					return tr("GBA");
 63#endif
 64#ifdef M_CORE_GB
 65				case PLATFORM_GB:
 66					return tr("GB");
 67#endif
 68#ifdef M_CORE_DS
 69				case PLATFORM_DS:
 70					return tr("DS");
 71#endif
 72				default:
 73					return tr("?");
 74				}
 75			}
 76		};
 77		s_columns["location"] = {
 78			tr("Location"),
 79			[](const mLibraryEntry& e) -> QString {
 80				return QString::fromUtf8(e.base);
 81			}
 82		};
 83		s_columns["crc32"] = {
 84			tr("CRC32"),
 85			[](const mLibraryEntry& e) -> QString {
 86				return QString("%0").arg(e.crc32, 8, 16, QChar('0'));
 87			}
 88		};
 89	}
 90	if (!path.isNull()) {
 91		if (s_handles.contains(path)) {
 92			m_library = s_handles[path];
 93			m_library->ref();
 94		} else {
 95			m_library = new LibraryHandle(mLibraryLoad(path.toUtf8().constData()), path);
 96			if (m_library->library) {
 97				s_handles[path] = m_library;
 98			} else {
 99				delete m_library;
100				m_library = new LibraryHandle(mLibraryCreateEmpty());
101			}
102		}
103	} else {
104		m_library = new LibraryHandle(mLibraryCreateEmpty());
105	}
106	memset(&m_constraints, 0, sizeof(m_constraints));
107	m_constraints.platform = PLATFORM_NONE;
108	m_columns.append(s_columns["name"]);
109	m_columns.append(s_columns["location"]);
110	m_columns.append(s_columns["platform"]);
111	m_columns.append(s_columns["size"]);
112	m_columns.append(s_columns["crc32"]);
113
114	connect(m_library->loader, SIGNAL(directoryLoaded(const QString&)), this, SLOT(directoryLoaded(const QString&)));
115}
116
117LibraryModel::~LibraryModel() {
118	clearConstraints();
119	if (!m_library->deref()) {
120		s_handles.remove(m_library->path);
121		delete m_library;
122	}
123}
124
125void LibraryModel::loadDirectory(const QString& path) {
126	m_queue.append(path);
127	QMetaObject::invokeMethod(m_library->loader, "loadDirectory", Q_ARG(const QString&, path));
128}
129
130bool LibraryModel::entryAt(int row, mLibraryEntry* out) const {
131	mLibraryListing entries;
132	mLibraryListingInit(&entries, 0);
133	if (!mLibraryGetEntries(m_library->library, &entries, 1, row, &m_constraints)) {
134		mLibraryListingDeinit(&entries);
135		return false;
136	}
137	*out = *mLibraryListingGetPointer(&entries, 0);
138	mLibraryListingDeinit(&entries);
139	return true;
140}
141
142VFile* LibraryModel::openVFile(const QModelIndex& index) const {
143	mLibraryEntry entry;
144	if (!entryAt(index.row(), &entry)) {
145		return nullptr;
146	}
147	return mLibraryOpenVFile(m_library->library, &entry);
148}
149
150QString LibraryModel::filename(const QModelIndex& index) const {
151	mLibraryEntry entry;
152	if (!entryAt(index.row(), &entry)) {
153		return QString();
154	}
155	return QString::fromUtf8(entry.filename);
156}
157
158QString LibraryModel::location(const QModelIndex& index) const {
159	mLibraryEntry entry;
160	if (!entryAt(index.row(), &entry)) {
161		return QString();
162	}
163	return QString::fromUtf8(entry.base);
164}
165
166QVariant LibraryModel::data(const QModelIndex& index, int role) const {
167	if (!index.isValid()) {
168		return QVariant();
169	}
170	mLibraryEntry entry;
171	if (!entryAt(index.row(), &entry)) {
172		return QVariant();
173	}
174	if (role == Qt::UserRole) {
175		return QVariant::fromValue(entry);
176	}
177	if (index.column() >= m_columns.count()) {
178		return QVariant();
179	}
180	switch (role) {
181	case Qt::DisplayRole:
182		return m_columns[index.column()].value(entry);
183	case Qt::SizeHintRole: {
184		QFontMetrics fm((QFont()));
185		return fm.size(Qt::TextSingleLine, m_columns[index.column()].value(entry));
186	}
187	case Qt::TextAlignmentRole:
188		return m_columns[index.column()].alignment;
189	default:
190		return QVariant();
191	}
192}
193
194QVariant LibraryModel::headerData(int section, Qt::Orientation orientation, int role) const {
195	if (role != Qt::DisplayRole) {
196		return QAbstractItemModel::headerData(section, orientation, role);
197	}
198	if (orientation == Qt::Horizontal) {
199		if (section >= m_columns.count()) {
200			return QVariant();
201		}
202		return m_columns[section].name;
203	}
204	return section;
205}
206
207QModelIndex LibraryModel::index(int row, int column, const QModelIndex& parent) const {
208	if (parent.isValid()) {
209		return QModelIndex();
210	}
211	return createIndex(row, column, nullptr);
212}
213
214QModelIndex LibraryModel::parent(const QModelIndex&) const {
215	return QModelIndex();
216}
217
218int LibraryModel::columnCount(const QModelIndex& parent) const {
219	if (parent.isValid()) {
220		return 0;
221	}
222	return m_columns.count();
223}
224
225int LibraryModel::rowCount(const QModelIndex& parent) const {
226	if (parent.isValid()) {
227		return 0;
228	}
229	return mLibraryCount(m_library->library, &m_constraints);
230}
231
232void LibraryModel::attachGameDB(const NoIntroDB* gameDB) {
233	mLibraryAttachGameDB(m_library->library, gameDB);
234}
235
236void LibraryModel::constrainBase(const QString& path) {
237	if (m_constraints.base) {
238		free(const_cast<char*>(m_constraints.base));
239	}
240	m_constraints.base = strdup(path.toUtf8().constData());
241}
242
243void LibraryModel::clearConstraints() {
244	if (m_constraints.base) {
245		free(const_cast<char*>(m_constraints.base));
246	}
247	if (m_constraints.filename) {
248		free(const_cast<char*>(m_constraints.filename));
249	}
250	if (m_constraints.title) {
251		free(const_cast<char*>(m_constraints.title));
252	}
253	memset(&m_constraints, 0, sizeof(m_constraints));
254}
255
256void LibraryModel::directoryLoaded(const QString& path) {
257	m_queue.removeOne(path);
258	beginResetModel();
259	endResetModel();
260	if (m_queue.empty()) {
261		emit doneLoading();
262	}
263}
264
265LibraryModel::LibraryColumn::LibraryColumn() {
266}
267
268LibraryModel::LibraryColumn::LibraryColumn(const QString& name, std::function<QString(const mLibraryEntry&)> value, int alignment)
269	: name(name)
270	, value(value)
271	, alignment(alignment)
272{
273}
274
275LibraryModel::LibraryHandle::LibraryHandle(mLibrary* lib, const QString& p)
276	: library(lib)
277	, loader(new LibraryLoader(library))
278	, path(p)
279	, m_ref(1)
280{
281	if (!library) {
282		return;
283	}
284	loader->moveToThread(&m_loaderThread);
285	m_loaderThread.setObjectName("Library Loader Thread");
286	m_loaderThread.start();
287}
288
289LibraryModel::LibraryHandle::~LibraryHandle() {
290	m_loaderThread.quit();
291	m_loaderThread.wait();
292	if (library) {
293		mLibraryDestroy(library);
294	}
295}
296
297void LibraryModel::LibraryHandle::ref() {
298	++m_ref;
299}
300
301bool LibraryModel::LibraryHandle::deref() {
302	--m_ref;
303	return m_ref > 0;
304}
305
306LibraryLoader::LibraryLoader(mLibrary* library, QObject* parent)
307	: QObject(parent)
308	, m_library(library)
309{
310}
311
312void LibraryLoader::loadDirectory(const QString& path) {
313	mLibraryLoadDirectory(m_library, path.toUtf8().constData());
314	emit directoryLoaded(path);
315}