all repos — mgba @ 45844301f6d628654f3b61450b25735dad18bd65

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