Merge branch 'folder_covers_propagation' into develop

This commit is contained in:
Luis Ángel San Martín 2022-08-31 18:59:00 +02:00
commit cf400e39cf
7 changed files with 92 additions and 34 deletions

View File

@ -547,7 +547,8 @@ void FolderModel::deleteFolder(const QModelIndex &mi)
{ {
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
DBHelper::removeFromDB(&f, db); DBHelper::removeFromDB(&f, db);
DBHelper::updateChildrenInfo(item->parent()->id, db); auto folder = DBHelper::updateChildrenInfo(item->parent()->id, db);
DBHelper::propagateFolderUpdatesToParent(folder, db);
connectionName = db.connectionName(); connectionName = db.connectionName();
} }
QSqlDatabase::removeDatabase(connectionName); QSqlDatabase::removeDatabase(connectionName);
@ -560,7 +561,8 @@ void FolderModel::updateFolderChildrenInfo(qulonglong folderId)
QString connectionName = ""; QString connectionName = "";
{ {
QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
DBHelper::updateChildrenInfo(folderId, db); auto folder = DBHelper::updateChildrenInfo(folderId, db);
DBHelper::propagateFolderUpdatesToParent(folder, db);
connectionName = db.connectionName(); connectionName = db.connectionName();
} }
QSqlDatabase::removeDatabase(connectionName); QSqlDatabase::removeDatabase(connectionName);

View File

@ -727,8 +727,57 @@ void DBHelper::update(const Folder &folder, QSqlDatabase &db)
updateFolderInfo.exec(); updateFolderInfo.exec();
} }
void DBHelper::updateChildrenInfo(const Folder &folder, QSqlDatabase &db) void DBHelper::propagateFolderUpdatesToParent(const Folder &folder, QSqlDatabase &db)
{ {
auto currentParentId = folder.parentId;
auto currentId = folder.id;
while (currentParentId != 1) {
auto f = loadFolder(currentParentId, db);
currentParentId = f.parentId;
currentId = f.id;
}
if (currentId != folder.id) {
updateChildrenInfo(currentId, db);
}
}
Folder DBHelper::updateChildrenInfo(qulonglong folderId, QSqlDatabase &db)
{
auto folder = loadFolder(folderId, db);
QList<LibraryItem *> subitems;
QList<LibraryItem *> subfolders = DBHelper::getFoldersFromParent(folderId, db, false);
QList<LibraryItem *> comics = DBHelper::getComicsFromParent(folderId, db, false);
QList<LibraryItem *> updatedSubfolders;
for (auto sf : subfolders) {
updatedSubfolders.append(new Folder(updateChildrenInfo(static_cast<Folder *>(sf)->id, db)));
}
subitems.append(updatedSubfolders);
subitems.append(comics);
std::sort(subitems.begin(), subitems.end(), naturalSortLessThanCILibraryItem);
QString coverHash = "";
for (auto item : subitems) {
if (item->isDir()) {
auto f = static_cast<Folder *>(item);
auto firstChildHash = f->getFirstChildHash();
if (!firstChildHash.isEmpty()) {
coverHash = firstChildHash;
break;
}
} else {
auto c = static_cast<ComicDB *>(item);
coverHash = c->info.hash;
break;
}
}
folder.setNumChildren(subfolders.count() + comics.count());
folder.setFirstChildHash(coverHash);
QSqlQuery updateFolderInfo(db); QSqlQuery updateFolderInfo(db);
updateFolderInfo.prepare("UPDATE folder SET " updateFolderInfo.prepare("UPDATE folder SET "
"numChildren = :numChildren, " "numChildren = :numChildren, "
@ -736,39 +785,30 @@ void DBHelper::updateChildrenInfo(const Folder &folder, QSqlDatabase &db)
"WHERE id = :id "); "WHERE id = :id ");
updateFolderInfo.bindValue(":numChildren", folder.getNumChildren()); updateFolderInfo.bindValue(":numChildren", folder.getNumChildren());
updateFolderInfo.bindValue(":firstChildHash", folder.getFirstChildHash()); updateFolderInfo.bindValue(":firstChildHash", folder.getFirstChildHash());
updateFolderInfo.bindValue(":id", folder.id);
updateFolderInfo.exec();
}
void DBHelper::updateChildrenInfo(qulonglong folderId, QSqlDatabase &db)
{
QList<LibraryItem *> subfolders = DBHelper::getFoldersFromParent(folderId, db, false);
QList<LibraryItem *> comics = DBHelper::getComicsFromParent(folderId, db, true);
ComicDB *firstComic = NULL;
if (comics.count() > 0)
firstComic = static_cast<ComicDB *>(comics.first());
QSqlQuery updateFolderInfo(db);
updateFolderInfo.prepare("UPDATE folder SET "
"numChildren = :numChildren, "
"firstChildHash = :firstChildHash "
"WHERE id = :id ");
updateFolderInfo.bindValue(":numChildren", subfolders.count() + comics.count());
updateFolderInfo.bindValue(":firstChildHash", firstComic != NULL ? firstComic->info.hash : "");
updateFolderInfo.bindValue(":id", folderId); updateFolderInfo.bindValue(":id", folderId);
updateFolderInfo.exec(); updateFolderInfo.exec();
qDeleteAll(subfolders);
qDeleteAll(updatedSubfolders);
qDeleteAll(comics);
return folder;
} }
void DBHelper::updateChildrenInfo(QSqlDatabase &db) void DBHelper::updateChildrenInfo(QSqlDatabase &db)
{ {
QElapsedTimer timer;
timer.start();
QSqlQuery selectQuery(db); // TODO check QSqlQuery selectQuery(db); // TODO check
selectQuery.prepare("SELECT id FROM folder"); selectQuery.prepare("SELECT id FROM folder f WHERE f.parentId = 1");
selectQuery.exec(); selectQuery.exec();
while (selectQuery.next()) { while (selectQuery.next()) {
DBHelper::updateChildrenInfo(selectQuery.value(0).toULongLong(), db); DBHelper::updateChildrenInfo(selectQuery.value(0).toULongLong(), db);
} }
qDebug() << timer.elapsed();
} }
void DBHelper::updateProgress(qulonglong libraryId, const ComicInfo &comicInfo) void DBHelper::updateProgress(qulonglong libraryId, const ComicInfo &comicInfo)

View File

@ -66,8 +66,8 @@ public:
static void update(ComicInfo *comicInfo, QSqlDatabase &db); static void update(ComicInfo *comicInfo, QSqlDatabase &db);
static void updateRead(ComicInfo *comicInfo, QSqlDatabase &db); static void updateRead(ComicInfo *comicInfo, QSqlDatabase &db);
static void update(const Folder &folder, QSqlDatabase &db); static void update(const Folder &folder, QSqlDatabase &db);
static void updateChildrenInfo(const Folder &folder, QSqlDatabase &db); static void propagateFolderUpdatesToParent(const Folder &folder, QSqlDatabase &db);
static void updateChildrenInfo(qulonglong folderId, QSqlDatabase &db); static Folder updateChildrenInfo(qulonglong folderId, QSqlDatabase &db);
static void updateChildrenInfo(QSqlDatabase &db); static void updateChildrenInfo(QSqlDatabase &db);
static void updateProgress(qulonglong libraryId, const ComicInfo &comicInfo); static void updateProgress(qulonglong libraryId, const ComicInfo &comicInfo);
static void setComicAsReading(qulonglong libraryId, const ComicInfo &comicInfo); static void setComicAsReading(qulonglong libraryId, const ComicInfo &comicInfo);

View File

@ -185,9 +185,10 @@ void LibraryCreator::run()
update(QDir(_source)); update(QDir(_source));
} }
if (partialUpdate) if (partialUpdate) {
DBHelper::updateChildrenInfo(folderDestinationModelIndex.data(FolderModel::IdRole).toULongLong(), _database); auto folder = DBHelper::updateChildrenInfo(folderDestinationModelIndex.data(FolderModel::IdRole).toULongLong(), _database);
else DBHelper::propagateFolderUpdatesToParent(folder, _database);
} else
DBHelper::updateChildrenInfo(_database); DBHelper::updateChildrenInfo(_database);
_database.commit(); _database.commit();
@ -199,9 +200,9 @@ void LibraryCreator::run()
// si estabamos en modo creación, se está añadiendo una librería que ya existía y se ha actualizado antes de añadirse. // si estabamos en modo creación, se está añadiendo una librería que ya existía y se ha actualizado antes de añadirse.
if (!partialUpdate) { if (!partialUpdate) {
if (!creation) { if (!creation) {
emit(updated()); emit updated();
} else { } else {
emit(created()); emit created();
} }
} }
QLOG_INFO() << "Update library END"; QLOG_INFO() << "Update library END";
@ -377,16 +378,20 @@ void LibraryCreator::update(QDir dirS)
bool updated; bool updated;
int i, j; int i, j;
for (i = 0, j = 0; (i < lenghtS) || (j < lenghtD);) { for (i = 0, j = 0; (i < lenghtS) || (j < lenghtD);) {
if (stopRunning) if (stopRunning) {
qDeleteAll(listD);
return; return;
}
updated = false; updated = false;
if (i >= lenghtS) // finished source files/dirs if (i >= lenghtS) // finished source files/dirs
{ {
// QLOG_WARN() << "finished source files/dirs" << dirS.absolutePath(); // QLOG_WARN() << "finished source files/dirs" << dirS.absolutePath();
// delete listD //from j // delete listD //from j
for (; j < lenghtD; j++) { for (; j < lenghtD; j++) {
if (stopRunning) if (stopRunning) {
qDeleteAll(listD);
return; return;
}
DBHelper::removeFromDB(listD.at(j), (_database)); DBHelper::removeFromDB(listD.at(j), (_database));
} }
updated = true; updated = true;
@ -396,8 +401,10 @@ void LibraryCreator::update(QDir dirS)
// QLOG_WARN() << "finished library files/dirs" << dirS.absolutePath(); // QLOG_WARN() << "finished library files/dirs" << dirS.absolutePath();
// create listS //from i // create listS //from i
for (; i < lenghtS; i++) { for (; i < lenghtS; i++) {
if (stopRunning) if (stopRunning) {
qDeleteAll(listD);
return; return;
}
QFileInfo fileInfoS = listS.at(i); QFileInfo fileInfoS = listS.at(i);
if (fileInfoS.isDir()) // create folder if (fileInfoS.isDir()) // create folder
{ {
@ -548,4 +555,6 @@ void LibraryCreator::update(QDir dirS)
} }
} }
} }
qDeleteAll(listD);
} }

View File

@ -308,5 +308,7 @@ void FolderController::service(HttpRequest &request, HttpResponse &response)
t.setVariable("page", QString("%1").arg(page + 1)); t.setVariable("page", QString("%1").arg(page + 1));
t.setVariable("pages", QString("%1").arg(numPages)); t.setVariable("pages", QString("%1").arg(numPages));
qDeleteAll(folderContent);
response.write(t.toUtf8(), true); response.write(t.toUtf8(), true);
} }

View File

@ -63,6 +63,8 @@ void FolderContentControllerV2::serviceContent(const int &library, const qulongl
} }
} }
qDeleteAll(folderContent);
QJsonDocument output(items); QJsonDocument output(items);
response.write(output.toJson(QJsonDocument::Compact)); response.write(output.toJson(QJsonDocument::Compact));

View File

@ -34,6 +34,9 @@ Folder &Folder::operator=(const Folder &other)
this->finished = other.finished; this->finished = other.finished;
this->completed = other.completed; this->completed = other.completed;
this->manga = other.manga; this->manga = other.manga;
this->numChildren = other.numChildren;
this->firstChildHash = other.firstChildHash;
this->customImage = other.customImage;
return *this; return *this;
} }