From 9b56ced7ee1d20fd1f5fd469bbda6bb08a456f76 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Sun, 28 Jul 2024 12:53:31 -0400 Subject: [PATCH] Add Directory Monitoring handling to File-Not-Found page Directory Monitoring can remove zims from the library instance. We now only remove zims that are not opened by the user. There could be the case where library instance is directly modified inside kiwix-desktop, filesystem, etc that causes us to lose the zim information. We only display N/A in this case for path and name on the page. --- src/contentmanager.cpp | 3 ++- src/kiwixapp.cpp | 4 ++-- src/urlschemehandler.cpp | 12 +++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index f2bd5fd8d..0f9a0a80c 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -906,5 +906,6 @@ void ContentManager::updateLibraryFromDir(QString monitorDir) void ContentManager::handleDisappearedZimFile(QString bookId) { - mp_library->removeBookFromLibraryById(bookId); + if (!KiwixApp::instance()->getTabWidget()->getTabZimIds().contains(bookId)) + mp_library->removeBookFromLibraryById(bookId); } diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index 878824ff0..2df37e89a 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -106,10 +106,10 @@ void KiwixApp::init() mp_manager->asyncUpdateLibraryFromDir(monitorDir); }); - setupDirectoryMonitoring(); - + /* Restore Tabs before directory monitoring to ensure we know what tabs user had. */ restoreTabs(); restoreWindowState(); + setupDirectoryMonitoring(); } void KiwixApp::setupDirectoryMonitoring() diff --git a/src/urlschemehandler.cpp b/src/urlschemehandler.cpp index aed0e2c0e..22c050e1d 100644 --- a/src/urlschemehandler.cpp +++ b/src/urlschemehandler.cpp @@ -181,9 +181,15 @@ UrlSchemeHandler::replyZimNotFoundPage(QWebEngineUrlRequestJob *request, const QString &zimId) { QBuffer *buffer = new QBuffer; - auto& book = KiwixApp::instance()->getLibrary()->getBookById(zimId); - QString path = QString::fromStdString(book.getPath()); - QString name = QString::fromStdString(book.getName()); + QString path = "N/A", name = "N/A"; + try + { + auto& book = KiwixApp::instance()->getLibrary()->getBookById(zimId); + path = QString::fromStdString(book.getPath()); + name = QString::fromStdString(book.getName()); + } + catch (...) { /* Blank */ } + QString contentHtml = "
" "

" + gt("file-not-found-title") +