Skip to content

Commit

Permalink
File-not-found tab now displays zim name and path
Browse files Browse the repository at this point in the history
Remembers the name and paths in addition to the zimId of the zim file and restore on startup. The extra metadata is displayed on the file-not-found tab when the zim file is missing either on startup or during the running of the application.
  • Loading branch information
ShaopengLin committed Jun 27, 2024
1 parent 2cac2da commit bf13512
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 3 deletions.
4 changes: 3 additions & 1 deletion resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 3 additions & 1 deletion resources/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
47 changes: 47 additions & 0 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void KiwixApp::init()
}

restoreTabs();
cleanupRemovedZimBookInfo();
restoreWindowState();
}

Expand Down Expand Up @@ -350,6 +351,20 @@ bool KiwixApp::isCurrentArticleBookmarked()
return false;
}

QPair<QString,QString> 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<QString,QString>(name, path);
}
return QPair<QString,QString>("N/A", "N/A");
}

void KiwixApp::setMonitorDir(const QString &dir) {
m_settingsManager.setMonitorDir(dir);
m_library.setMonitorDirZims(dir, QStringList());
Expand Down Expand Up @@ -577,3 +592,35 @@ void KiwixApp::saveCurrentTabIndex()
{
return mp_session->setValue("currentTabIndex", getTabWidget()->currentIndex());
}

void KiwixApp::addRemovedZimBookInfo(const QList<kiwix::Book> &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);
}
4 changes: 4 additions & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ 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<QString, QString> getRemovedZimBookInfoById(const QString& zimId);
void setMonitorDir(const QString &dir);
bool isCurrentArticleBookmarked();
QString parseStyleFromFile(QString filePath);
void saveListOfOpenTabs();
void saveWindowState();
void restoreWindowState();
void saveCurrentTabIndex();
void addRemovedZimBookInfo(const QList<kiwix::Book>& books);
void cleanupRemovedZimBookInfo();

public slots:
void newTab();
Expand All @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <QtDebug>
#include <QtConcurrent/QtConcurrentRun>
#include <QJsonObject>


class LibraryManipulator: public kiwix::LibraryManipulator {
Expand Down Expand Up @@ -206,11 +207,18 @@ void Library::updateFromDir(QString monitorDir)
needsRefresh |= manager.addBookFromPath(bookPath.toStdString());
}
}

QList<kiwix::Book> 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());
Expand Down
12 changes: 12 additions & 0 deletions src/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZimView*>(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)
Expand All @@ -301,6 +312,7 @@ void TabBar::closeTab(int index)
view->deleteLater();

KiwixApp::instance()->saveListOfOpenTabs();
KiwixApp::instance()->cleanupRemovedZimBookInfo();
}

void TabBar::onCurrentChanged(int index)
Expand Down
1 change: 1 addition & 0 deletions src/tabbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/urlschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ void UrlSchemeHandler::replyZimNotFoundPage(QWebEngineUrlRequestJob *request,
const QString &zimId)
{
QBuffer *buffer = new QBuffer;
auto namePathPair = KiwixApp::instance()->getRemovedZimBookInfoById(zimId);
QString contentHtml = "<section><div>"
"<h1>" +
gt("file-not-found-title") +
Expand All @@ -190,6 +191,12 @@ void UrlSchemeHandler::replyZimNotFoundPage(QWebEngineUrlRequestJob *request,
"<p>" +
gt("zim-id") + ": <b>" + zimId +
"</b></p>"
"<p>" +
gt("zim-name") + ": <b>" + namePathPair.first +
"</b></p>"
"<p>" +
gt("zim-path") + ": <b>" + namePathPair.second +
"</b></p>"
"</div></section>";

buffer->open(QIODevice::WriteOnly);
Expand Down

0 comments on commit bf13512

Please sign in to comment.