From 77ee3a2936dd48e7dbaf99ad893f439aebe371df Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 24 May 2024 16:23:03 +0400 Subject: [PATCH] Library maintains "correct" paths of books being downloaded When a book is downloaded via a metalink URL the fake path (with a ".beingdownloadedbykiwix" suffix appended to it) recorded for it in the library could not in general be used to obtain the real name (and, hence, path) of the ZIM file. Now the book path is "corrected" as soon as the file name becomes available. --- src/contentmanager.cpp | 4 +++- src/library.cpp | 17 ++++++++++++++++- src/library.h | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index ce24378..0c8e7e8 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -564,9 +564,11 @@ void ContentManager::updateDownload(QString bookId, const DownloadInfo& download { const auto downloadState = m_downloads.value(bookId); if ( downloadState ) { + const auto downloadPath = downloadInfo["path"].toString(); if ( downloadInfo["status"].toString() == "completed" ) { - downloadCompleted(bookId, downloadInfo["path"].toString()); + downloadCompleted(bookId, downloadPath); } else { + mp_library->updateBookBeingDownloaded(bookId, downloadPath); downloadState->update(downloadInfo); managerModel->updateDownload(bookId); } diff --git a/src/library.cpp b/src/library.cpp index 656a4ec..2a1a633 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -116,7 +116,10 @@ void Library::addBookBeingDownloaded(const kiwix::Book& book, QString downloadDi const QString downloadUrl = QString::fromStdString(book.getUrl()); // XXX: This works if the URL is a direct link to a ZIM file - // XXX: rather than to a torrent or a metalink file + // XXX: rather than to a torrent or a metalink file. In those cases + // XXX: the file name of the download will be discovered after the + // XXX: the metalink or torrent file is downloaded. Then the real + // XXX: file name of a book must be set via updateBookBeingDownloaded(); const QString fileName = downloadUrl.split('/').back(); const QString path = QDir(downloadDir).absoluteFilePath(fileName); @@ -127,6 +130,18 @@ void Library::addBookBeingDownloaded(const kiwix::Book& book, QString downloadDi addBookToLibrary(bookCopy); } +void Library::updateBookBeingDownloaded(const QString& bookId, const QString& bookPath) +{ + const kiwix::Book& book = getBookById(bookId); + const auto bookPseudoPath = pseudoPathOfAFileBeingDownloaded(bookPath.toStdString()); + if ( bookPseudoPath != book.getPath() ) { + kiwix::Book bookCopy(book); + bookCopy.setPath(bookPseudoPath); + mp_library->addOrUpdateBook(bookCopy); + save(); + } +} + bool Library::isBeingDownloadedByUs(QString path) const { const auto fakePath = pseudoPathOfAFileBeingDownloaded(path.toStdString()); diff --git a/src/library.h b/src/library.h index 16c184e..c399389 100644 --- a/src/library.h +++ b/src/library.h @@ -39,6 +39,7 @@ public: void addBookToLibrary(kiwix::Book& book); void addBookBeingDownloaded(const kiwix::Book& book, QString downloadDir); bool isBeingDownloadedByUs(QString path) const; + void updateBookBeingDownloaded(const QString& bookId, const QString& bookPath); void removeBookFromLibraryById(const QString& id); void addBookmark(kiwix::Bookmark& bookmark); void removeBookmark(const QString& zimId, const QString& url);