diff --git a/include/mgba/core/library.h b/include/mgba/core/library.h index c2a810c7f..1408f24d3 100644 --- a/include/mgba/core/library.h +++ b/include/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; diff --git a/src/core/library.c b/src/core/library.c index 3f20d5731..d230f60a6 100644 --- a/src/core/library.c +++ b/src/core/library.c @@ -392,6 +392,12 @@ size_t mLibraryGetEntries(struct mLibrary* library, struct mLibraryListing* out, 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); diff --git a/src/platform/qt/LibraryModel.cpp b/src/platform/qt/LibraryModel.cpp index 52173c622..bea45c4d6 100644 --- a/src/platform/qt/LibraryModel.cpp +++ b/src/platform/qt/LibraryModel.cpp @@ -99,6 +99,7 @@ LibraryModel::LibraryModel(const QString& path, QObject* parent) } 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(const QString& path, QObject* parent) LibraryModel::~LibraryModel() { clearConstraints(); + mLibraryListingDeinit(&m_listings); if (!m_library->deref()) { s_handles.remove(m_library->path); delete m_library; @@ -124,14 +126,10 @@ void LibraryModel::loadDirectory(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 @@ void LibraryModel::attachGameDB(const NoIntroDB* gameDB) { } void LibraryModel::constrainBase(const QString& path) { + clearConstraints(); if (m_constraints.base) { free(const_cast(m_constraints.base)); } m_constraints.base = strdup(path.toUtf8().constData()); + reload(); } void LibraryModel::clearConstraints() { @@ -247,6 +247,15 @@ void LibraryModel::clearConstraints() { free(const_cast(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) { diff --git a/src/platform/qt/LibraryModel.h b/src/platform/qt/LibraryModel.h index edfca6a56..970b08f87 100644 --- a/src/platform/qt/LibraryModel.h +++ b/src/platform/qt/LibraryModel.h @@ -52,6 +52,7 @@ public slots: void constrainBase(const QString& path); void clearConstraints(); + void reload(); private slots: void directoryLoaded(const QString& path); @@ -86,6 +87,7 @@ private: static QMap s_handles; mLibraryEntry m_constraints; + mLibraryListing m_listings; QStringList m_queue; QList m_columns; diff --git a/src/platform/qt/LibraryView.cpp b/src/platform/qt/LibraryView.cpp index 2fa050abc..44b447e0a 100644 --- a/src/platform/qt/LibraryView.cpp +++ b/src/platform/qt/LibraryView.cpp @@ -24,6 +24,7 @@ LibraryView::LibraryView(QWidget* parent) m_ui.listing->horizontalHeader()->setSectionsMovable(true); m_ui.listing->setModel(&m_model); m_ui.listing->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + m_model.reload(); resizeColumns(); }