| 926 | } |
| 927 | |
| 928 | Folder DBHelper::updateChildrenInfo(qulonglong folderId, QSqlDatabase &db) |
| 929 | { |
| 930 | auto folder = loadFolder(folderId, db); |
| 931 | QList<LibraryItem *> subfolders = DBHelper::getFoldersFromParent(folderId, db, true); |
| 932 | QList<LibraryItem *> comics = DBHelper::getComicsFromParent(folderId, db, true); |
| 933 | |
| 934 | QList<LibraryItem *> updatedSubfolders; |
| 935 | for (auto sf : subfolders) { |
| 936 | updatedSubfolders.append(new Folder(updateChildrenInfo(static_cast<Folder *>(sf)->id, db))); |
| 937 | } |
| 938 | |
| 939 | QString coverHash = ""; |
| 940 | |
| 941 | if (!comics.isEmpty()) { |
| 942 | auto c = static_cast<ComicDB *>(comics[0]); |
| 943 | coverHash = c->info.hash; |
| 944 | } else { |
| 945 | for (auto item : updatedSubfolders) { |
| 946 | auto f = static_cast<Folder *>(item); |
| 947 | auto firstChildHash = f->firstChildHash; |
| 948 | if (!firstChildHash.isEmpty()) { |
| 949 | coverHash = firstChildHash; |
| 950 | break; |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | if (folder.numChildren == subfolders.count() + comics.count() && folder.firstChildHash == coverHash) { |
| 956 | return folder; |
| 957 | } |
| 958 | |
| 959 | folder.numChildren = subfolders.count() + comics.count(); |
| 960 | folder.firstChildHash = coverHash; |
| 961 | |
| 962 | QSqlQuery updateFolderInfo(db); |
| 963 | updateFolderInfo.prepare("UPDATE folder SET " |
| 964 | "numChildren = :numChildren, " |
| 965 | "firstChildHash = :firstChildHash " |
| 966 | "WHERE id = :id "); |
| 967 | updateFolderInfo.bindValue(":numChildren", folder.numChildren); |
| 968 | updateFolderInfo.bindValue(":firstChildHash", folder.firstChildHash); |
| 969 | updateFolderInfo.bindValue(":id", folderId); |
| 970 | updateFolderInfo.exec(); |
| 971 | |
| 972 | qDeleteAll(subfolders); |
| 973 | qDeleteAll(updatedSubfolders); |
| 974 | qDeleteAll(comics); |
| 975 | |
| 976 | return folder; |
| 977 | } |
| 978 | |
| 979 | void DBHelper::updateChildrenInfo(QSqlDatabase &db) |
| 980 | { |