diff --git a/YACReaderLibrary/library_creator.cpp b/YACReaderLibrary/library_creator.cpp index 663c77ec..8a8281aa 100644 --- a/YACReaderLibrary/library_creator.cpp +++ b/YACReaderLibrary/library_creator.cpp @@ -228,11 +228,13 @@ void LibraryCreator::run() if (partialUpdate) { auto folder = DBHelper::updateChildrenInfo(folderDestinationModelIndex.data(FolderModel::IdRole).toULongLong(), _database); DBHelper::propagateFolderUpdatesToParent(folder, _database); - } else + } else { DBHelper::updateChildrenInfo(_database); - - _database.commit(); + cleanup(_database, _target); + } } + + _database.commit(); _database.close(); } @@ -270,6 +272,38 @@ void LibraryCreator::cancel() stopRunning = true; } +void LibraryCreator::cleanup(QSqlDatabase &db, const QString &target) +{ + QDir coversDir(target + "/covers/"); + if (!coversDir.exists()) { + return; + } + + // delete from comic_info all the comics that don't have a comic associated from the comic table + QSqlQuery infoToDeleteQuery(db); + infoToDeleteQuery.prepare("SELECT ci.id, ci.hash FROM comic_info ci WHERE ci.id NOT IN (SELECT c.comicInfoId FROM comic c)"); + + if (!infoToDeleteQuery.exec()) { + QLOG_ERROR() << "Error getting comics to delete"; + return; + } + + while (infoToDeleteQuery.next()) { + QString hash = infoToDeleteQuery.value(1).toString(); + QString cover = hash + ".jpg"; + + auto fullPath = coversDir.absoluteFilePath(cover); + QFile::remove(fullPath); + } + + QSqlQuery deleteQuery(db); + deleteQuery.prepare("DELETE FROM comic_info WHERE id NOT IN (SELECT comicInfoId FROM comic)"); + if (!deleteQuery.exec()) { + QLOG_ERROR() << "Error purging info from comic_info"; + return; + } +} + // retorna el id del ultimo de los folders qulonglong LibraryCreator::insertFolders() { diff --git a/YACReaderLibrary/library_creator.h b/YACReaderLibrary/library_creator.h index 84d82ea8..0d7c3757 100644 --- a/YACReaderLibrary/library_creator.h +++ b/YACReaderLibrary/library_creator.h @@ -59,6 +59,7 @@ private: QModelIndex folderDestinationModelIndex; QSettings *settings; bool checkModifiedDatesOnUpdate; + void cleanup(QSqlDatabase &db, const QString &target); signals: void finished();