From ecfe13cade3980797805c79cd5ddf76df2343ca9 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sat, 3 Feb 2024 10:04:25 -0500 Subject: [PATCH] sagasu: Fix import path list function It can now quickly import path lists from ResLogger (or another source that fits such format) --- sagasu/src/hashdatabase.cpp | 46 ++++++++++++++++++++++++++++++++++++- sagasu/src/mainwindow.cpp | 20 +++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/sagasu/src/hashdatabase.cpp b/sagasu/src/hashdatabase.cpp index ef7f923..aeaf2f1 100644 --- a/sagasu/src/hashdatabase.cpp +++ b/sagasu/src/hashdatabase.cpp @@ -4,6 +4,7 @@ #include "hashdatabase.h" #include +#include #include #include @@ -99,10 +100,53 @@ void HashDatabase::importFileList(const QString &path) QFile file(path); file.open(QIODevice::ReadOnly); + QVariantList folderNames, folderHashes; + QVariantList fileNames, fileHashes; + + m_db.transaction(); + + QSqlQuery folderQuery; + folderQuery.prepare( + QStringLiteral("REPLACE INTO folder_hashes (hash, name) " + "VALUES (?, ?)")); + + QSqlQuery fileQuery; + fileQuery.prepare( + QStringLiteral("REPLACE INTO file_hashes (hash, name) " + "VALUES (?, ?)")); + QTextStream stream(&file); + stream.readLine(); // skip header while (!stream.atEnd()) { - addFile(stream.readLine()); + const QStringList parts = stream.readLine().split(QLatin1Char(',')); + + const QString &folderHash = parts[1]; + const QString &fileHash = parts[2]; + const QString &path = parts[4]; + + QString filename; + QString foldername; + if (path.contains(QStringLiteral("/"))) { + int lastSlash = path.lastIndexOf(QStringLiteral("/")); + filename = path.sliced(lastSlash + 1, path.length() - lastSlash - 1); + foldername = path.left(lastSlash); + } else { + filename = path; + } + + // execBatch is too slow as the QSQLITE doesn't support batch operations + if (!foldername.isEmpty()) { + folderQuery.bindValue(0, folderHash.toUInt()); + folderQuery.bindValue(1, foldername); + folderQuery.exec(); + } + + fileQuery.bindValue(0, fileHash.toUInt()); + fileQuery.bindValue(1, filename); + fileQuery.exec(); } + + m_db.commit(); } #include "moc_hashdatabase.cpp" \ No newline at end of file diff --git a/sagasu/src/mainwindow.cpp b/sagasu/src/mainwindow.cpp index e3e4852..d8116af 100644 --- a/sagasu/src/mainwindow.cpp +++ b/sagasu/src/mainwindow.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "cmppart.h" #include "exdpart.h" @@ -119,11 +120,28 @@ void MainWindow::refreshParts(const QString &path) void MainWindow::setupFileMenu(QMenu *menu) { - auto openList = menu->addAction(QStringLiteral("Import path list...")); + auto openList = menu->addAction(QStringLiteral("Import Path List...")); openList->setIcon(QIcon::fromTheme(QStringLiteral("document-open"))); connect(openList, &QAction::triggered, [this] { auto fileName = QFileDialog::getOpenFileName(nullptr, QStringLiteral("Open Path List"), QStringLiteral("~")); + QMessageBox::warning(this, + QStringLiteral("Import Warning"), + QStringLiteral("Depending on the size of the import, this process usually takes a few minutes. The program may freeze. Please " + "keep it open until the operation is finished."), + QMessageBox::Ok, + QMessageBox::Ok); + + m_database.importFileList(fileName); + + QMessageBox::information(this, QStringLiteral("Import Complete"), QStringLiteral("Successfully imported path list!"), QMessageBox::Ok, QMessageBox::Ok); + }); + + auto downloadList = menu->addAction(QStringLiteral("Download Path List...")); + downloadList->setIcon(QIcon::fromTheme(QStringLiteral("document-open"))); + connect(downloadList, &QAction::triggered, [this] { + auto fileName = QFileDialog::getOpenFileName(nullptr, QStringLiteral("Open Path List"), QStringLiteral("~")); + m_database.importFileList(fileName); }); }