Skip to content

Commit

Permalink
library fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
biscuitcakes committed Oct 10, 2024
1 parent 9df7bf1 commit 2af2ad5
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 81 deletions.
2 changes: 2 additions & 0 deletions include/firelight/library/user_library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ namespace firelight::library {
virtual std::vector<WatchedDirectory> getWatchedDirectories() = 0;

virtual bool addWatchedDirectory(const WatchedDirectory &directory) = 0;

virtual bool updateWatchedDirectory(const WatchedDirectory &directory) = 0;
};
}
2 changes: 1 addition & 1 deletion src/app/library/library_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LibraryScanner::LibraryScanner(
// directory_watcher_.addPath(
// QString::fromStdString(default_rom_path_.string()));

m_scanDirectoryModel = new firelight::gui::LibraryPathModel(*lib_database);
// m_scanDirectoryModel = new firelight::gui::LibraryPathModel(*lib_database);
refreshDirectories();

connect(m_scanDirectoryModel, &QAbstractListModel::dataChanged,
Expand Down
14 changes: 12 additions & 2 deletions src/app/library/library_scanner2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ namespace firelight::library {
continue;
}

if (fileInfo.size() > 1024 * 1024 * 1024) {
spdlog::info("Skipping file {} because it's too large",
fileInfo.filePath().toStdString());
continue;
}

auto extension = fileInfo.suffix().toLower();
if (extension == "zip" || extension == "7z" || extension == "tar" ||
extension == "rar") {
Expand Down Expand Up @@ -113,7 +119,9 @@ namespace firelight::library {
// printf("-- content hash: %s\n",
// romFile.getContentHash().toStdString().c_str());

m_library.addRomFile(romFile);
if (romFile.isValid()) {
m_library.addRomFile(romFile);
}

delete[] buffer;
}
Expand Down Expand Up @@ -143,7 +151,9 @@ namespace firelight::library {
}

auto romFile = RomFile(fileInfo.filePath());
m_library.addRomFile(romFile);
if (romFile.isValid()) {
m_library.addRomFile(romFile);
}

// metadata scan:
// for each entry
Expand Down
11 changes: 6 additions & 5 deletions src/app/library/rom_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ namespace firelight::library {
RomFile::RomFile(const QString &path) : m_filePath(path) {
m_suffix = path.right(path.length() - (path.lastIndexOf('.') + 1)).toLower();

QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
return;
}

m_platformId = PlatformMetadata::getIdFromFileExtension(m_suffix.toStdString());
if (m_platformId == PlatformMetadata::PLATFORM_ID_UNKNOWN) {
return;
// TODO: check for other stuff later.
}

QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
return;
}

const auto bytes = file.readAll();
file.close();

Expand Down
15 changes: 15 additions & 0 deletions src/app/library/sqlite_user_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ namespace firelight::library {
return true;
}

bool SqliteUserLibrary::updateWatchedDirectory(const WatchedDirectory &directory) {
QSqlQuery q(getDatabase());
q.prepare("UPDATE watched_directoriesv1 SET path = :path WHERE id = :id");
q.bindValue(":path", directory.path);
q.bindValue(":id", directory.id);

if (!q.exec()) {
spdlog::error("Failed to update watched directory: {}",
q.lastError().text().toStdString());
return false;
}

return true;
}

QSqlDatabase SqliteUserLibrary::getDatabase() const {
const auto name =
DATABASE_PREFIX +
Expand Down
2 changes: 2 additions & 0 deletions src/app/library/sqlite_user_library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace firelight::library {

bool addWatchedDirectory(const WatchedDirectory &directory) override;

bool updateWatchedDirectory(const WatchedDirectory &directory) override;

signals:
void romFileAdded(int id, const QString &contentHash);

Expand Down
113 changes: 57 additions & 56 deletions src/gui/library_path_model.cpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,73 @@
#include "library_path_model.hpp"

namespace firelight::gui {
LibraryPathModel::LibraryPathModel(db::ILibraryDatabase &libraryDatabase)
: m_libraryDatabase(libraryDatabase) {
// m_settings = std::make_unique<QSettings>();
m_items = m_libraryDatabase.getAllLibraryContentDirectories();
}
#include <firelight/library/user_library.hpp>

int LibraryPathModel::rowCount(const QModelIndex &parent) const {
return m_items.size();
}
namespace firelight::gui {
LibraryPathModel::LibraryPathModel(library::IUserLibrary &userLibrary)
: m_userLibrary(userLibrary) {
// m_settings = std::make_unique<QSettings>();
m_items = m_userLibrary.getWatchedDirectories();
}

QVariant LibraryPathModel::data(const QModelIndex &index, int role) const {
if (role < Qt::UserRole || index.row() >= m_items.size()) {
return {};
int LibraryPathModel::rowCount(const QModelIndex &parent) const {
return m_items.size();
}

auto item = m_items.at(index.row());
QVariant LibraryPathModel::data(const QModelIndex &index, int role) const {
if (role < Qt::UserRole || index.row() >= m_items.size()) {
return {};
}

switch (role) {
case LocalFilename: {
auto t = QString::fromStdString(item.path);
auto val = QUrl::fromUserInput(t);
return val;
}
case Path: {
return QString::fromStdString(item.path);
}
case NumGameFiles:
return item.numGameFiles;
default:
return QVariant{};
}
}
auto item = m_items.at(index.row());

bool LibraryPathModel::setData(const QModelIndex &index, const QVariant &value,
int role) {
if (role < Qt::UserRole || index.row() >= m_items.size()) {
return false;
switch (role) {
case LocalFilename: {
auto val = QUrl::fromUserInput(item.path);
return val;
}
case Path: {
return item.path;
}
case NumGameFiles:
return item.numContentFiles;
default:
return QVariant{};
}
}

auto &item = m_items.at(index.row());
switch (role) {
case Path:
item.path = QUrl(value.toString()).toLocalFile().toStdString();
m_libraryDatabase.updateLibraryContentDirectory(item);
bool LibraryPathModel::setData(const QModelIndex &index, const QVariant &value,
int role) {
if (role < Qt::UserRole || index.row() >= m_items.size()) {
return false;
}

auto &item = m_items.at(index.row());
switch (role) {
case Path:
item.path = QUrl(value.toString()).toLocalFile();
m_userLibrary.updateWatchedDirectory(item);

emit dataChanged(index, index, {Path, LocalFilename});
return true;
case NumGameFiles:
item.numGameFiles = value.toInt();
emit dataChanged(index, index, {Path, LocalFilename});
return true;
case NumGameFiles:
item.numContentFiles = value.toInt();

emit dataChanged(index, index, {role});
return true;
default:
return false;
emit dataChanged(index, index, {role});
return true;
default:
return false;
}
}
}

QHash<int, QByteArray> LibraryPathModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[Path] = "path";
roles[LocalFilename] = "local_filename";
roles[NumGameFiles] = "num_game_files";
return roles;
}
QHash<int, QByteArray> LibraryPathModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[Path] = "path";
roles[LocalFilename] = "local_filename";
roles[NumGameFiles] = "num_game_files";
return roles;
}

Qt::ItemFlags LibraryPathModel::flags(const QModelIndex &index) const {
return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
}
Qt::ItemFlags LibraryPathModel::flags(const QModelIndex &index) const {
return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
}
} // namespace firelight::gui
31 changes: 16 additions & 15 deletions src/gui/library_path_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@
#include <QUrl>
#include <firelight/library_database.hpp>
#include <qsettings.h>
#include <firelight/library/user_library.hpp>

namespace firelight::gui {
class LibraryPathModel : public QAbstractListModel {
Q_OBJECT
class LibraryPathModel : public QAbstractListModel {
Q_OBJECT

std::vector<db::LibraryContentDirectory> m_items;
std::unique_ptr<QSettings> m_settings;
db::ILibraryDatabase &m_libraryDatabase;
std::vector<library::WatchedDirectory> m_items;
std::unique_ptr<QSettings> m_settings;
library::IUserLibrary &m_userLibrary;

public:
enum Roles { Path = Qt::UserRole + 1, LocalFilename, NumGameFiles };
public:
enum Roles { Path = Qt::UserRole + 1, LocalFilename, NumGameFiles };

LibraryPathModel(db::ILibraryDatabase &libraryDatabase);
explicit LibraryPathModel(library::IUserLibrary &userLibrary);

int rowCount(const QModelIndex &parent) const override;
int rowCount(const QModelIndex &parent) const override;

QVariant data(const QModelIndex &index, int role) const override;
QVariant data(const QModelIndex &index, int role) const override;

bool setData(const QModelIndex &index, const QVariant &value,
int role) override;
bool setData(const QModelIndex &index, const QVariant &value,
int role) override;

QHash<int, QByteArray> roleNames() const override;
QHash<int, QByteArray> roleNames() const override;

Qt::ItemFlags flags(const QModelIndex &index) const override;
};
Qt::ItemFlags flags(const QModelIndex &index) const override;
};
} // namespace firelight::gui
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ int main(int argc, char *argv[]) {
QObject::connect(&libScanner2, &firelight::library::LibraryScanner2::scanFinished,
&entryListModel, &firelight::library::EntryListModel::reset);

firelight::gui::LibraryPathModel libraryPathModel(userLibrary);

firelight::gui::PlatformListModel platformListModel;
firelight::shop::ShopItemModel shopItemModel(contentDatabase);

Expand Down Expand Up @@ -246,8 +248,7 @@ int main(int argc, char *argv[]) {
engine.rootContext()->setContextProperty("library_manager", &libraryManager);
engine.rootContext()->setContextProperty("library_database",
&libraryDatabase);
engine.rootContext()->setContextProperty("library_scan_path_model",
libraryManager.scanDirectoryModel());
engine.rootContext()->setContextProperty("library_scan_path_model", &libraryPathModel);
engine.rootContext()->setContextProperty("controller_model",
&controllerListModel);
engine.rootContext()->setContextProperty("controller_manager",
Expand Down

0 comments on commit 2af2ad5

Please sign in to comment.