diff --git a/resources/i18n/en.json b/resources/i18n/en.json index c4bc04e7..2a918429 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -171,5 +171,7 @@ "preview-book-in-web-browser": "Preview book in web browser", "file-not-found-title": "ZIM File Not Found", "file-not-found-text": "ZIM file doesn't exist or is not readable", - "zim-id": "ZIM Id" + "zim-id": "ZIM Id", + "zim-name": "ZIM Name", + "zim-path": "ZIM File Path" } diff --git a/resources/i18n/qqq.json b/resources/i18n/qqq.json index 8b19e0ff..909b260c 100644 --- a/resources/i18n/qqq.json +++ b/resources/i18n/qqq.json @@ -178,5 +178,7 @@ "path-was-copied": "Tooltip confirming that the download path from settings was copied.", "file-not-found-title": "Error title text displayed when the desktop application cannot find the Zim file needed to display the web page.", "file-not-found-text": "Error description text for when the desktop application cannot find the Zim file needed to display the web page.", - "zim-id": "The term for the unique identifier of a zim file." + "zim-id": "The term for the unique identifier of a zim file.", + "zim-name": "The term for the name of a Zim file", + "zim-path": "The term for the path of the Zim File" } diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index b32f57da..cbbe1ab1 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -124,6 +124,7 @@ void KiwixApp::init() } restoreTabs(); + cleanupRemovedZimBookInfo(); restoreWindowState(); } @@ -350,6 +351,20 @@ bool KiwixApp::isCurrentArticleBookmarked() return false; } +QPair KiwixApp::getRemovedZimBookInfoById(const QString & zimId) +{ + QMutexLocker locker(&m_updateBookInfoMutex); + auto bookInfoMap = mp_session->value("removedZimBookInfo", QVariantMap{}).toMap(); + if (bookInfoMap.contains(zimId)) + { + QJsonObject obj = bookInfoMap.value(zimId).toJsonObject(); + auto name = obj.value("name").toString(); + auto path = obj.value("path").toString(); + return QPair(name, path); + } + return QPair("N/A", "N/A"); +} + void KiwixApp::setMonitorDir(const QString &dir) { m_settingsManager.setMonitorDir(dir); m_library.setMonitorDirZims(dir, QStringList()); @@ -577,3 +592,35 @@ void KiwixApp::saveCurrentTabIndex() { return mp_session->setValue("currentTabIndex", getTabWidget()->currentIndex()); } + +void KiwixApp::addRemovedZimBookInfo(const QList &books) +{ + QMutexLocker locker(&m_updateBookInfoMutex); + auto bookInfoMap = mp_session->value("removedZimBookInfo", QVariantMap{}).toMap(); + for (auto& book : books) + { + auto id = QString::fromStdString(book.getId()); + auto name = QString::fromStdString(book.getName()); + auto path = QString::fromStdString(book.getPath()); + bookInfoMap[id] = QJsonObject{{"name", name}, {"path", path}}; + } + mp_session->setValue("removedZimBookInfo", bookInfoMap); +} + +/** + * @brief Removes only the book infos that has no trace left in the app. + * + */ +void KiwixApp::cleanupRemovedZimBookInfo() +{ + QMutexLocker locker(&m_updateBookInfoMutex); + auto bookInfoMap = mp_session->value("removedZimBookInfo", QVariantMap{}).toMap(); + auto existingZimIds = getTabWidget()->getTabZimIds(); + + for (const auto& zimId : bookInfoMap.keys()) + { + if (!existingZimIds.contains(zimId)) + bookInfoMap.remove(zimId); + } + mp_session->setValue("removedZimBookInfo", bookInfoMap); +} diff --git a/src/kiwixapp.h b/src/kiwixapp.h index bcecd0a2..24b9633e 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -85,6 +85,7 @@ class KiwixApp : public QtSingleApplication kiwix::Server* getLocalServer() { return &m_server; } SettingsManager* getSettingsManager() { return &m_settingsManager; }; QString getText(const QString &key) { return m_translation.getText(key); }; + QPair getRemovedZimBookInfoById(const QString& zimId); void setMonitorDir(const QString &dir); bool isCurrentArticleBookmarked(); QString parseStyleFromFile(QString filePath); @@ -92,6 +93,8 @@ class KiwixApp : public QtSingleApplication void saveWindowState(); void restoreWindowState(); void saveCurrentTabIndex(); + void addRemovedZimBookInfo(const QList& books); + void cleanupRemovedZimBookInfo(); public slots: void newTab(); @@ -113,6 +116,7 @@ public slots: KProfile m_profile; QString m_libraryDirectory; Library m_library; + QMutex m_updateBookInfoMutex; kiwix::Downloader* mp_downloader; ContentManager* mp_manager; MainWindow* mp_mainWindow; diff --git a/src/library.cpp b/src/library.cpp index 656a4ecb..5fcdf42b 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -6,6 +6,7 @@ #include #include +#include class LibraryManipulator: public kiwix::LibraryManipulator { @@ -206,11 +207,18 @@ void Library::updateFromDir(QString monitorDir) needsRefresh |= manager.addBookFromPath(bookPath.toStdString()); } } + + QList removedBookList; for (auto bookPath : removedZims) { try { - removeBookFromLibraryById(QString::fromStdString(mp_library->getBookByPath(bookPath.toStdString()).getId())); + auto& book = mp_library->getBookByPath(bookPath.toStdString()); + removedBookList.push_back(book); + removeBookFromLibraryById(QString::fromStdString(book.getId())); } catch (...) {} } + if (!removedBookList.isEmpty()) + KiwixApp::instance()->addRemovedZimBookInfo(std::move(removedBookList)); + if (needsRefresh) { emit(booksChanged()); setMonitorDirZims(monitorDir, newDir.values()); diff --git a/src/tabbar.cpp b/src/tabbar.cpp index f5456cd1..48d9edf5 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -282,6 +282,17 @@ QStringList TabBar::getTabUrls() const { return idList; } +QStringList TabBar::getTabZimIds() const +{ + QStringList idList; + for (int index = 0; index <= mp_stackedWidget->count(); index++) + { + if (ZimView* zv = qobject_cast(mp_stackedWidget->widget(index))) + idList.push_back(zv->getWebView()->zimId()); + } + return idList; +} + void TabBar::closeTab(int index) { // The first and last tabs (i.e. the library tab and the + (new tab) button) @@ -301,6 +312,7 @@ void TabBar::closeTab(int index) view->deleteLater(); KiwixApp::instance()->saveListOfOpenTabs(); + KiwixApp::instance()->cleanupRemovedZimBookInfo(); } void TabBar::onCurrentChanged(int index) diff --git a/src/tabbar.h b/src/tabbar.h index 26d5b111..2875713f 100644 --- a/src/tabbar.h +++ b/src/tabbar.h @@ -50,6 +50,7 @@ class TabBar : public QTabBar void openFindInPageBar(); void closeTabsByZimId(const QString &id); QStringList getTabUrls() const; + QStringList getTabZimIds() const; protected: void mousePressEvent(QMouseEvent *event); diff --git a/src/urlschemehandler.cpp b/src/urlschemehandler.cpp index 305bdab0..f1eeb703 100644 --- a/src/urlschemehandler.cpp +++ b/src/urlschemehandler.cpp @@ -180,6 +180,7 @@ void UrlSchemeHandler::replyZimNotFoundPage(QWebEngineUrlRequestJob *request, const QString &zimId) { QBuffer *buffer = new QBuffer; + auto namePathPair = KiwixApp::instance()->getRemovedZimBookInfoById(zimId); QString contentHtml = "
" "

" + gt("file-not-found-title") + @@ -190,6 +191,12 @@ void UrlSchemeHandler::replyZimNotFoundPage(QWebEngineUrlRequestJob *request, "

" + gt("zim-id") + ": " + zimId + "

" + "

" + + gt("zim-name") + ": " + namePathPair.first + + "

" + "

" + + gt("zim-path") + ": " + namePathPair.second + + "

" "

"; buffer->open(QIODevice::WriteOnly);