Assign covers to folders from subfolders & comics

Before this commit folder with no direct comics (only subfolders) didn't have a cover image to be displayed. Now updating the children info  is done recursively and subfolders are taken into account.

This affects the iOS client remote browser so far, but it is also needed for the future browser update (display folders as a grid instead of using EmptyFolderWidget)
This commit is contained in:
Luis Ángel San Martín 2022-08-31 18:58:25 +02:00
parent 0fcf9d0cba
commit 8a6ec5fcc6
4 changed files with 72 additions and 15 deletions

View File

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

View File

@ -727,35 +727,88 @@ void DBHelper::update(const Folder &folder, QSqlDatabase &db)
updateFolderInfo.exec();
}
void DBHelper::updateChildrenInfo(qulonglong folderId, QSqlDatabase &db)
void DBHelper::propagateFolderUpdatesToParent(const Folder &folder, QSqlDatabase &db)
{
QList<LibraryItem *> subfolders = DBHelper::getFoldersFromParent(folderId, db, false);
QList<LibraryItem *> comics = DBHelper::getComicsFromParent(folderId, db, true);
auto currentParentId = folder.parentId;
auto currentId = folder.id;
while (currentParentId != 1) {
auto f = loadFolder(currentParentId, db);
currentParentId = f.parentId;
currentId = f.id;
}
ComicDB *firstComic = NULL;
if (comics.count() > 0)
firstComic = static_cast<ComicDB *>(comics.first());
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);
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(":numChildren", folder.getNumChildren());
updateFolderInfo.bindValue(":firstChildHash", folder.getFirstChildHash());
updateFolderInfo.bindValue(":id", folderId);
updateFolderInfo.exec();
qDeleteAll(subfolders);
qDeleteAll(updatedSubfolders);
qDeleteAll(comics);
return folder;
}
void DBHelper::updateChildrenInfo(QSqlDatabase &db)
{
QElapsedTimer timer;
timer.start();
QSqlQuery selectQuery(db); // TODO check
selectQuery.prepare("SELECT id FROM folder");
selectQuery.prepare("SELECT id FROM folder f WHERE f.parentId = 1");
selectQuery.exec();
while (selectQuery.next()) {
DBHelper::updateChildrenInfo(selectQuery.value(0).toULongLong(), db);
}
qDebug() << timer.elapsed();
}
void DBHelper::updateProgress(qulonglong libraryId, const ComicInfo &comicInfo)

View File

@ -66,7 +66,8 @@ public:
static void update(ComicInfo *comicInfo, QSqlDatabase &db);
static void updateRead(ComicInfo *comicInfo, QSqlDatabase &db);
static void update(const Folder &folder, QSqlDatabase &db);
static void updateChildrenInfo(qulonglong folderId, QSqlDatabase &db);
static void propagateFolderUpdatesToParent(const Folder &folder, QSqlDatabase &db);
static Folder updateChildrenInfo(qulonglong folderId, QSqlDatabase &db);
static void updateChildrenInfo(QSqlDatabase &db);
static void updateProgress(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));
}
if (partialUpdate)
DBHelper::updateChildrenInfo(folderDestinationModelIndex.data(FolderModel::IdRole).toULongLong(), _database);
else
if (partialUpdate) {
auto folder = DBHelper::updateChildrenInfo(folderDestinationModelIndex.data(FolderModel::IdRole).toULongLong(), _database);
DBHelper::propagateFolderUpdatesToParent(folder, _database);
} else
DBHelper::updateChildrenInfo(_database);
_database.commit();