Qt: Improve library usage

This commit is contained in:
Vicki Pfau 2017-03-16 11:45:33 -07:00
parent 45844301f6
commit 40e9dfcf5c
5 changed files with 25 additions and 6 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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<char*>(m_constraints.base));
}
m_constraints.base = strdup(path.toUtf8().constData());
reload();
}
void LibraryModel::clearConstraints() {
@ -247,6 +247,15 @@ void LibraryModel::clearConstraints() {
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) {

View File

@ -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<QString, LibraryHandle*> s_handles;
mLibraryEntry m_constraints;
mLibraryListing m_listings;
QStringList m_queue;
QList<LibraryColumn> m_columns;

View File

@ -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();
}