| 1243 | } |
| 1244 | |
| 1245 | void DBHelper::updateFromRemoteClientWithHash(const QList<ComicInfo> &comics) |
| 1246 | { |
| 1247 | const YACReaderLibraries libraries = DBHelper::getLibraries(); |
| 1248 | const QStringList names = libraries.getNames(); |
| 1249 | |
| 1250 | for (const auto &name : names) { |
| 1251 | QString libraryPath = DBHelper::getLibraries().getPath(libraries.getId(name)); |
| 1252 | QString connectionName = ""; |
| 1253 | { |
| 1254 | QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath)); |
| 1255 | |
| 1256 | if (!db.isValid()) { |
| 1257 | QLOG_ERROR() << "updateFromRemoteClientWithHash: could not open database for library" << libraryPath; |
| 1258 | continue; |
| 1259 | } |
| 1260 | |
| 1261 | db.transaction(); |
| 1262 | |
| 1263 | QSqlQuery updateComicInfo(db); |
| 1264 | updateComicInfo.prepare("UPDATE comic_info SET " |
| 1265 | "read = :read, " |
| 1266 | "currentPage = :currentPage, " |
| 1267 | "hasBeenOpened = :hasBeenOpened, " |
| 1268 | "lastTimeOpened = :lastTimeOpened, " |
| 1269 | "rating = :rating" |
| 1270 | " WHERE id = :id "); |
| 1271 | |
| 1272 | for (const auto &comicInfo : comics) { |
| 1273 | ComicInfo info = loadComicInfo(comicInfo.hash, db); |
| 1274 | |
| 1275 | if (!info.existOnDb) { |
| 1276 | continue; |
| 1277 | } |
| 1278 | |
| 1279 | if (comicInfo.currentPage > 0) { |
| 1280 | info.currentPage = comicInfo.currentPage; |
| 1281 | |
| 1282 | if (info.currentPage == info.numPages) |
| 1283 | info.read = true; |
| 1284 | |
| 1285 | info.hasBeenOpened = true; |
| 1286 | |
| 1287 | if (info.lastTimeOpened.toULongLong() < comicInfo.lastTimeOpened.toULongLong()) |
| 1288 | info.lastTimeOpened = comicInfo.lastTimeOpened; |
| 1289 | } |
| 1290 | |
| 1291 | if (comicInfo.rating > 0) { |
| 1292 | info.rating = comicInfo.rating; |
| 1293 | } |
| 1294 | |
| 1295 | updateComicInfo.bindValue(":read", info.read ? 1 : 0); |
| 1296 | updateComicInfo.bindValue(":currentPage", info.currentPage); |
| 1297 | updateComicInfo.bindValue(":hasBeenOpened", info.hasBeenOpened ? 1 : 0); |
| 1298 | updateComicInfo.bindValue(":lastTimeOpened", QDateTime::currentSecsSinceEpoch()); |
| 1299 | updateComicInfo.bindValue(":id", info.id); |
| 1300 | updateComicInfo.bindValue(":rating", info.rating); |
| 1301 | updateComicInfo.exec(); |
| 1302 | } |