Implement XML scanning for a folder

This commit is contained in:
Luis Ángel San Martín 2022-10-29 11:07:40 +02:00
parent 3e2bda7cf1
commit 76642737fd
2 changed files with 70 additions and 25 deletions

View File

@ -6,6 +6,7 @@
#include "initial_comic_info_extractor.h"
#include "xml_info_parser.h"
#include "yacreader_global.h"
#include "folder_item.h"
#include "QsLog.h"
@ -23,6 +24,21 @@ void XMLInfoLibraryScanner::scanLibrary(const QString &source, const QString &ta
this->stopRunning = false;
partialUpdate = false;
start();
}
void XMLInfoLibraryScanner::scanFolder(const QString &source, const QString &target, const QString &folder, const QModelIndex &dest)
{
this->source = source;
this->target = target;
this->stopRunning = false;
partialUpdate = true;
folderDestinationModelIndex = dest;
start();
}
@ -52,36 +68,29 @@ void XMLInfoLibraryScanner::run()
database.transaction();
QSqlQuery comicsInfo("SELECT * FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id)", database);
comicsInfo.exec();
if (!partialUpdate) {
QSqlQuery comicsInfo("SELECT * FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id)", database);
comicsInfo.exec();
QSqlRecord record = comicsInfo.record();
updateFromSQLQuery(database, comicsInfo);
} else {
if (folderDestinationModelIndex.isValid()) {
YACReader::iterate(folderDestinationModelIndex, folderDestinationModelIndex.model(), [&](const QModelIndex &idx) {
if (stopRunning) {
return false;
}
int id = record.indexOf("id");
// int parentIdIndex = record.indexOf("parentId");
int fileNameIndex = record.indexOf("fileName");
int pathIndex = record.indexOf("path");
auto item = static_cast<FolderItem *>(idx.internalPointer());
while (comicsInfo.next()) {
if (this->stopRunning) {
break;
}
/* currentItem.id = selectQuery.value(id).toULongLong();
currentItem.parentId = parentId; //selectQuery.value(parentId).toULongLong();
currentItem.name = selectQuery.value(fileName).toString(); */
auto fileName = comicsInfo.value(fileNameIndex).toString();
auto path = comicsInfo.value(pathIndex).toString();
QSqlQuery comicsInfo(database);
comicsInfo.prepare("SELECT * FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) WHERE c.parentId = :parentId");
comicsInfo.bindValue(":parentId", item->id);
comicsInfo.exec();
emit comicScanned(path, fileName);
updateFromSQLQuery(database, comicsInfo);
auto info = DBHelper::getComicInfoFromQuery(comicsInfo, "comicInfoId");
InitialComicInfoExtractor ie(QDir::cleanPath(this->source + path), "None");
ie.extract();
if (parseXMLIntoInfo(ie.getXMLInfoRawData(), info)) {
DBHelper::update(&info, database);
return true;
});
}
}
@ -96,3 +105,33 @@ void XMLInfoLibraryScanner::stop()
{
stopRunning = true;
}
void XMLInfoLibraryScanner::updateFromSQLQuery(QSqlDatabase &db, QSqlQuery &query)
{
QSqlRecord record = query.record();
int id = record.indexOf("id");
// int parentIdIndex = record.indexOf("parentId");
int fileNameIndex = record.indexOf("fileName");
int pathIndex = record.indexOf("path");
while (query.next()) {
if (this->stopRunning) {
break;
}
auto fileName = query.value(fileNameIndex).toString();
auto path = query.value(pathIndex).toString();
emit comicScanned(path, fileName);
auto info = DBHelper::getComicInfoFromQuery(query, "comicInfoId");
InitialComicInfoExtractor ie(QDir::cleanPath(this->source + path), "None", info.coverPage.toInt(), true);
ie.extract();
if (parseXMLIntoInfo(ie.getXMLInfoRawData(), info)) {
DBHelper::update(&info, db);
}
}
}

View File

@ -2,6 +2,7 @@
#define XMLINFOLIBRARYSCANNER_H
#include <QtCore>
#include <QSqlQuery>
namespace YACReader {
@ -11,6 +12,7 @@ class XMLInfoLibraryScanner : public QThread
public:
XMLInfoLibraryScanner();
void scanLibrary(const QString &source, const QString &target);
void scanFolder(const QString &source, const QString &target, const QString &folder, const QModelIndex &dest);
protected:
void run() override;
@ -25,6 +27,10 @@ private:
QString source;
QString target;
bool stopRunning;
bool partialUpdate;
QModelIndex folderDestinationModelIndex;
void updateFromSQLQuery(QSqlDatabase &db, QSqlQuery &query);
};
}