@@ -22,7 +22,7 @@ BookService::BookService(IBookMetadataHelper* bookMetadataHelper,
2222{
2323 // Book cover generated
2424 connect (m_bookMetadataHelper, &IBookMetadataHelper::bookCoverGenerated,
25- this , &BookService::storeBookCover );
25+ this , &BookService::assignBookCoverToBook );
2626
2727 // Fetch changes timer
2828 m_fetchChangesTimer.setInterval (m_fetchChangedInterval);
@@ -32,11 +32,11 @@ BookService::BookService(IBookMetadataHelper* bookMetadataHelper,
3232 // Getting books finished
3333 connect (m_bookStorageManager,
3434 &IBookStorageManager::loadingRemoteBooksFinished, this ,
35- &BookService::mergeLibraries );
35+ &BookService::updateLibrary );
3636
3737 // Downloading book finished
3838 connect (m_bookStorageManager, &IBookStorageManager::finishedDownloadingBook,
39- this , &BookService::updateDownloadedBook );
39+ this , &BookService::processDownloadedBook );
4040}
4141
4242BookOperationStatus BookService::addBook (const QString& filePath)
@@ -146,8 +146,9 @@ BookOperationStatus BookService::updateBook(const Book& newBook)
146146 return BookOperationStatus::BookDoesNotExist;
147147 }
148148
149- // Manually handle changes to current page to prevent "lastModified" being
150- // updated on a simple page update
149+ // Manually handle changes to "current page" because we don't want
150+ // "lastModified" to be updated on a "current page" change, since this would
151+ // break the whole updating mechanism.
151152 book->setCurrentPage (newBook.getCurrentPage ());
152153
153154 if (*book != newBook)
@@ -164,8 +165,8 @@ BookOperationStatus BookService::updateBook(const Book& newBook)
164165 return BookOperationStatus::Success;
165166}
166167
167- BookOperationStatus BookService::addTag (const QUuid& uuid,
168- const domain::entities::Tag& tag)
168+ BookOperationStatus BookService::addTagToBook (const QUuid& uuid,
169+ const domain::entities::Tag& tag)
169170{
170171 auto * book = getBook (uuid);
171172 if (book == nullptr )
@@ -193,8 +194,8 @@ BookOperationStatus BookService::addTag(const QUuid& uuid,
193194 return BookOperationStatus::Success;
194195}
195196
196- BookOperationStatus BookService::removeTag (const QUuid& bookUuid,
197- const QUuid& tagUuid)
197+ BookOperationStatus BookService::removeTagFromBook (const QUuid& bookUuid,
198+ const QUuid& tagUuid)
198199{
199200 auto * book = getBook (bookUuid);
200201 if (book == nullptr )
@@ -222,9 +223,9 @@ BookOperationStatus BookService::removeTag(const QUuid& bookUuid,
222223 return BookOperationStatus::Success;
223224}
224225
225- BookOperationStatus BookService::renameTag (const QUuid& bookUuid,
226- const QUuid& tagUuid,
227- const QString& newName)
226+ BookOperationStatus BookService::renameTagOfBook (const QUuid& bookUuid,
227+ const QUuid& tagUuid,
228+ const QString& newName)
228229{
229230 auto * book = getBook (bookUuid);
230231 if (book == nullptr )
@@ -327,7 +328,7 @@ BookOperationStatus BookService::saveBookToFile(const QUuid& uuid,
327328 return BookOperationStatus::Success;
328329}
329330
330- bool BookService::refreshLastOpened (const QUuid& uuid)
331+ bool BookService::refreshLastOpenedDateOfBook (const QUuid& uuid)
331332{
332333 auto * book = getBook (uuid);
333334 if (book == nullptr )
@@ -342,8 +343,8 @@ bool BookService::refreshLastOpened(const QUuid& uuid)
342343 return true ;
343344}
344345
345- void BookService::updateDownloadedBook (const QUuid& uuid,
346- const QString& filePath)
346+ void BookService::processDownloadedBook (const QUuid& uuid,
347+ const QString& filePath)
347348{
348349 auto * book = getBook (uuid);
349350
@@ -388,7 +389,7 @@ void BookService::clearUserData()
388389 emit bookClearingEnded ();
389390}
390391
391- void BookService::storeBookCover (const QPixmap* pixmap)
392+ void BookService::assignBookCoverToBook (const QPixmap* pixmap)
392393{
393394 int index = m_books.size () - 1 ;
394395 auto & book = m_books.at (index);
@@ -397,9 +398,11 @@ void BookService::storeBookCover(const QPixmap* pixmap)
397398 emit bookCoverGenerated (index);
398399}
399400
400- void BookService::mergeLibraries (
401- const std::vector<domain::entities::Book>& books)
401+ void BookService::updateLibrary (const std::vector<Book>& books)
402402{
403+ // The remote library is the library fetched from the server and the local
404+ // library is the library on the client's PC. On startup we need to make
405+ // sure that both libraries are synchronized.
403406 mergeRemoteLibraryIntoLocalLibrary (books);
404407 mergeLocalLibraryIntoRemoteLibrary (books);
405408}
@@ -416,6 +419,7 @@ void BookService::mergeRemoteLibraryIntoLocalLibrary(
416419 continue ;
417420 }
418421
422+ // Add the remote book to the local library if it does not exist
419423 emit bookInsertionStarted (m_books.size ());
420424 m_books.emplace_back (remoteBook);
421425 emit bookInsertionEnded ();
@@ -439,71 +443,73 @@ void BookService::mergeLocalLibraryIntoRemoteLibrary(
439443 }
440444}
441445
442- void BookService::mergeBooks (Book& original , const Book& mergee )
446+ void BookService::mergeBooks (Book& localBook , const Book& remoteBook )
443447{
444- auto lastOpenedStatus = mergeCurrentPage (original, mergee );
445- auto lastModifiedStatus = mergeBookData (original, mergee );
448+ auto lastOpenedStatus = mergeCurrentPage (localBook, remoteBook );
449+ auto lastModifiedStatus = mergeBookData (localBook, remoteBook );
446450
447451
448- if (lastOpenedStatus.updateLocalLibrary ||
449- lastModifiedStatus.updateLocalLibrary )
452+ if (lastOpenedStatus.localLibraryOutdated ||
453+ lastModifiedStatus.localLibraryOutdated )
450454 {
451- m_bookStorageManager->updateBookLocally (original );
455+ m_bookStorageManager->updateBookLocally (localBook );
452456
453457 // Update UI
454- auto index = getBookIndex (original .getUuid ());
458+ auto index = getBookIndex (localBook .getUuid ());
455459 emit dataChanged (index);
456460 }
457461
458- if (lastOpenedStatus.updateDatabase || lastModifiedStatus.updateDatabase )
462+ if (lastOpenedStatus.remoteLibraryOutdated ||
463+ lastModifiedStatus.remoteLibraryOutdated )
459464 {
460- m_bookStorageManager->updateBookRemotely (original );
465+ m_bookStorageManager->updateBookRemotely (localBook );
461466 }
462467}
463468
464- MergeStatus BookService::mergeCurrentPage (domain::entities:: Book& original ,
465- const domain::entities:: Book& mergee )
469+ MergeStatus BookService::mergeCurrentPage (Book& localBook ,
470+ const Book& remoteBook )
466471{
467- // Take the current time in seconds, so there are no ms mismatches
468- auto mergeeLastOpened = mergee .getLastOpened ().toSecsSinceEpoch ();
469- auto originalLastOpened = original .getLastOpened ().toSecsSinceEpoch ();
472+ // Take the current time in seconds, so that there are no ms mismatches
473+ auto localLastOpened = localBook .getLastOpened ().toSecsSinceEpoch ();
474+ auto remoteLastOpened = remoteBook .getLastOpened ().toSecsSinceEpoch ();
470475
471- if (mergeeLastOpened == originalLastOpened)
476+ // There are no "current page" differences between the local and remote book
477+ if (remoteLastOpened == localLastOpened)
472478 return {};
473479
474- if (mergeeLastOpened > originalLastOpened )
480+ if (remoteLastOpened > localLastOpened )
475481 {
476- original .setCurrentPage (mergee .getCurrentPage ());
477- original .setLastOpened (mergee .getLastOpened ());
482+ localBook .setCurrentPage (remoteBook .getCurrentPage ());
483+ localBook .setLastOpened (remoteBook .getLastOpened ());
478484
479- return MergeStatus { .updateLocalLibrary = true };
485+ return MergeStatus { .localLibraryOutdated = true };
480486 }
481487
482- return MergeStatus { .updateDatabase = true };
488+ return MergeStatus { .remoteLibraryOutdated = true };
483489}
484490
485- MergeStatus BookService::mergeBookData (domain::entities::Book& original,
486- const domain::entities::Book& mergee)
491+ MergeStatus BookService::mergeBookData (Book& localBook, const Book& remoteBook)
487492{
488- // Take the current time in seconds, so there are no ms mismatches
489- auto mergeeLastModified = mergee .getLastModified ().toSecsSinceEpoch ();
490- auto originalLastModified = original .getLastModified ().toSecsSinceEpoch ();
493+ // Take the current time in seconds, so that there are no ms mismatches
494+ auto localLastModified = localBook .getLastModified ().toSecsSinceEpoch ();
495+ auto remoteLastModified = remoteBook .getLastModified ().toSecsSinceEpoch ();
491496
492- if (mergeeLastModified == originalLastModified)
497+ // There are no data differences between the local and remote book
498+ if (remoteLastModified == localLastModified)
493499 return {};
494500
495- if (mergeeLastModified > originalLastModified )
501+ if (remoteLastModified > localLastModified )
496502 {
497503 // Save the file path since its overwritten during the update
498504 // operation
499- auto localBookFilePath = original .getFilePath ();
500- original .update (mergee );
501- original .setFilePath (localBookFilePath);
505+ auto localBookFilePath = localBook .getFilePath ();
506+ localBook .update (remoteBook );
507+ localBook .setFilePath (localBookFilePath);
502508
503- return MergeStatus { .updateLocalLibrary = true };
509+ return MergeStatus { .localLibraryOutdated = true };
504510 }
505511
506- return MergeStatus { .updateDatabase = true };
512+ return MergeStatus { .remoteLibraryOutdated = true };
507513}
508514
509515} // namespace application::services
0 commit comments