all repos — mgba @ 849f80e7a1959923c9d3d47191d0a162f0887bb9

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