From 2b780e23a4ba49d260a0b989f3368eacedd643b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Tue, 25 Aug 2020 19:01:43 +0200 Subject: [PATCH] Make `updateFromRemoteClient` return more updated comics So they can be sent back to the client. --- YACReaderLibrary/db_helper.cpp | 48 ++++++++++++++++++++++++++-------- YACReaderLibrary/db_helper.h | 2 +- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/YACReaderLibrary/db_helper.cpp b/YACReaderLibrary/db_helper.cpp index a81bd916..47fdc68c 100644 --- a/YACReaderLibrary/db_helper.cpp +++ b/YACReaderLibrary/db_helper.cpp @@ -827,10 +827,15 @@ void DBHelper::updateFromRemoteClientWithHash(const ComicInfo &comicInfo) } } -void DBHelper::updateFromRemoteClient(const QMap> &comics) +QMap> DBHelper::updateFromRemoteClient(const QMap> &comics) { + QMap> moreRecentComics; + foreach (qulonglong libraryId, comics.keys()) { + QList libraryMoreRecentComics; + QString libraryPath = DBHelper::getLibraries().getPath(libraryId); + QString connectionName = ""; { QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary"); @@ -850,37 +855,58 @@ void DBHelper::updateFromRemoteClient(const QMap> & ComicDB comic = DBHelper::loadComic(comicInfo.id, db); if (comic.info.hash == comicInfo.hash) { - if (comicInfo.currentPage > 0) { - comic.info.currentPage = comicInfo.currentPage; + bool isMoreRecent = false; - if (comic.info.currentPage == comic.info.numPages) - comic.info.read = true; - - comic.info.hasBeenOpened = true; - - if (comic.info.lastTimeOpened.toULongLong() < comicInfo.lastTimeOpened.toULongLong()) - comic.info.lastTimeOpened = comicInfo.lastTimeOpened; + //completion takes precedence over lastTimeOpened, if we just want to synchronize the lastest status we should use only lastTimeOpened + if ((comic.info.currentPage > 1 && comic.info.currentPage > comicInfo.currentPage) || comic.info.hasBeenOpened || (comic.info.read && !comicInfo.read)) { + isMoreRecent = true; } + if (comic.info.lastTimeOpened.toULongLong() > 0 && comicInfo.lastTimeOpened.toULongLong() == 0) { + isMoreRecent = true; + } + + comic.info.currentPage = qMax(comic.info.currentPage, comicInfo.currentPage); + + if (comic.info.currentPage == comic.info.numPages) + comic.info.read = true; + + comic.info.read = comic.info.read || comicInfo.read; + + comic.info.hasBeenOpened = comic.info.hasBeenOpened || comicInfo.currentPage > 0; + + if (comic.info.lastTimeOpened.toULongLong() < comicInfo.lastTimeOpened.toULongLong() && comicInfo.lastTimeOpened.toULongLong() > 0) + comic.info.lastTimeOpened = comicInfo.lastTimeOpened; + if (comicInfo.rating > 0) comic.info.rating = comicInfo.rating; updateComicInfo.bindValue(":read", comic.info.read ? 1 : 0); updateComicInfo.bindValue(":currentPage", comic.info.currentPage); updateComicInfo.bindValue(":hasBeenOpened", comic.info.hasBeenOpened ? 1 : 0); - updateComicInfo.bindValue(":lastTimeOpened", QDateTime::currentMSecsSinceEpoch() / 1000); + updateComicInfo.bindValue(":lastTimeOpened", comic.info.lastTimeOpened); updateComicInfo.bindValue(":id", comic.info.id); updateComicInfo.bindValue(":rating", comic.info.rating); updateComicInfo.exec(); + + if (isMoreRecent) { + libraryMoreRecentComics.append(comic); + } } } + if (!libraryMoreRecentComics.isEmpty()) { + moreRecentComics[libraryId] = libraryMoreRecentComics; + } + db.commit(); connectionName = db.connectionName(); } QSqlDatabase::removeDatabase(connectionName); } + + return moreRecentComics; } void DBHelper::updateFromRemoteClientWithHash(const QList &comics) diff --git a/YACReaderLibrary/db_helper.h b/YACReaderLibrary/db_helper.h index fb61b854..3b11f41f 100644 --- a/YACReaderLibrary/db_helper.h +++ b/YACReaderLibrary/db_helper.h @@ -72,7 +72,7 @@ public: static void updateFromRemoteClient(qulonglong libraryId, const ComicInfo &comicInfo); static void updateFromRemoteClientWithHash(const ComicInfo &comicInfo); static void updateReadingRemoteProgress(const ComicInfo &comicInfo, QSqlDatabase &db); - static void updateFromRemoteClient(const QMap> &comics); + static QMap> updateFromRemoteClient(const QMap> &comics); static void updateFromRemoteClientWithHash(const QList &comics); static void renameLabel(qulonglong id, const QString &name, QSqlDatabase &db); static void renameList(qulonglong id, const QString &name, QSqlDatabase &db);