all repos — mgba @ 40e9dfcf5c3f512fa332a48d5a478329971b0aca

mGBA Game Boy Advance Emulator

Qt: Improve library usage
Vicki Pfau vi@endrift.com
Thu, 16 Mar 2017 11:45:33 -0700
commit

40e9dfcf5c3f512fa332a48d5a478329971b0aca

parent

45844301f6d628654f3b61450b25735dad18bd65

M include/mgba/core/library.hinclude/mgba/core/library.h

@@ -39,6 +39,7 @@ void mLibraryLoadDirectory(struct mLibrary* library, const char* base);

size_t mLibraryCount(struct mLibrary* library, const struct mLibraryEntry* constraints); size_t mLibraryGetEntries(struct mLibrary* library, struct mLibraryListing* out, size_t numEntries, size_t offset, const struct mLibraryEntry* constraints); +void mLibraryEntryFree(struct mLibraryEntry* entry); struct VFile* mLibraryOpenVFile(struct mLibrary* library, const struct mLibraryEntry* entry); struct NoIntroDB;
M src/core/library.csrc/core/library.c

@@ -392,6 +392,12 @@ }

return mLibraryListingSize(out); } +void mLibraryEntryFree(struct mLibraryEntry* entry) { + free((void*) entry->title); + free((void*) entry->filename); + free((void*) entry->base); +} + struct VFile* mLibraryOpenVFile(struct mLibrary* library, const struct mLibraryEntry* entry) { struct mLibraryListing entries; mLibraryListingInit(&entries, 0);
M src/platform/qt/LibraryModel.cppsrc/platform/qt/LibraryModel.cpp

@@ -99,6 +99,7 @@ }

} else { m_library = new LibraryHandle(mLibraryCreateEmpty()); } + mLibraryListingInit(&m_listings, 0); memset(&m_constraints, 0, sizeof(m_constraints)); m_constraints.platform = PLATFORM_NONE; m_columns.append(s_columns["name"]);

@@ -112,6 +113,7 @@ }

LibraryModel::~LibraryModel() { clearConstraints(); + mLibraryListingDeinit(&m_listings); if (!m_library->deref()) { s_handles.remove(m_library->path); delete m_library;

@@ -124,14 +126,10 @@ QMetaObject::invokeMethod(m_library->loader, "loadDirectory", Q_ARG(const QString&, path));

} bool LibraryModel::entryAt(int row, mLibraryEntry* out) const { - mLibraryListing entries; - mLibraryListingInit(&entries, 0); - if (!mLibraryGetEntries(m_library->library, &entries, 1, row, &m_constraints)) { - mLibraryListingDeinit(&entries); + if (mLibraryListingSize(&m_listings) <= row) { return false; } - *out = *mLibraryListingGetPointer(&entries, 0); - mLibraryListingDeinit(&entries); + *out = *mLibraryListingGetConstPointer(&m_listings, row); return true; }

@@ -230,10 +228,12 @@ mLibraryAttachGameDB(m_library->library, gameDB);

} void LibraryModel::constrainBase(const QString& path) { + clearConstraints(); if (m_constraints.base) { free(const_cast<char*>(m_constraints.base)); } m_constraints.base = strdup(path.toUtf8().constData()); + reload(); } void LibraryModel::clearConstraints() {

@@ -247,6 +247,15 @@ if (m_constraints.title) {

free(const_cast<char*>(m_constraints.title)); } memset(&m_constraints, 0, sizeof(m_constraints)); + size_t i; + for (i = 0; i < mLibraryListingSize(&m_listings); ++i) { + mLibraryEntryFree(mLibraryListingGetPointer(&m_listings, i)); + } + mLibraryListingClear(&m_listings); +} + +void LibraryModel::reload() { + mLibraryGetEntries(m_library->library, &m_listings, 0, 0, m_constraints.base ? &m_constraints : nullptr); } void LibraryModel::directoryLoaded(const QString& path) {
M src/platform/qt/LibraryModel.hsrc/platform/qt/LibraryModel.h

@@ -52,6 +52,7 @@ void loadDirectory(const QString& path);

void constrainBase(const QString& path); void clearConstraints(); + void reload(); private slots: void directoryLoaded(const QString& path);

@@ -86,6 +87,7 @@ LibraryHandle* m_library;

static QMap<QString, LibraryHandle*> s_handles; mLibraryEntry m_constraints; + mLibraryListing m_listings; QStringList m_queue; QList<LibraryColumn> m_columns;
M src/platform/qt/LibraryView.cppsrc/platform/qt/LibraryView.cpp

@@ -24,6 +24,7 @@ connect(m_ui.listing, SIGNAL(activated(const QModelIndex&)), this, SIGNAL(accepted()));

m_ui.listing->horizontalHeader()->setSectionsMovable(true); m_ui.listing->setModel(&m_model); m_ui.listing->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + m_model.reload(); resizeColumns(); }