From 368fafb6bc2f239b64960af6e969b6804e18d499 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Sun, 28 Jul 2024 13:01:35 -0400 Subject: [PATCH] Added custom file-not-found page Page is displayed when zim file is missing. --- resources/i18n/en.json | 5 ++++- resources/i18n/qqq.json | 5 ++++- src/urlschemehandler.cpp | 27 ++++++++++++++++++++++++++- src/urlschemehandler.h | 2 ++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/resources/i18n/en.json b/resources/i18n/en.json index 62297e2..c4bc04e 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -168,5 +168,8 @@ "no-pictures": "No Pictures", "no-videos": "No Videos", "open-previous-tabs-at-startup": "Open previous tabs at startup", - "preview-book-in-web-browser": "Preview book in web browser" + "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" } diff --git a/resources/i18n/qqq.json b/resources/i18n/qqq.json index c296da9..8b19e0f 100644 --- a/resources/i18n/qqq.json +++ b/resources/i18n/qqq.json @@ -175,5 +175,8 @@ "no-details": "A content type for Zim files representing it only has an introduction.", "no-pictures": "A content type for Zim files that does not contain pictures.", "no-videos": "A content type for Zim files that does not contain videos.", - "path-was-copied": "Tooltip confirming that the download path from settings was copied." + "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." } diff --git a/src/urlschemehandler.cpp b/src/urlschemehandler.cpp index d592ef9..b21b734 100644 --- a/src/urlschemehandler.cpp +++ b/src/urlschemehandler.cpp @@ -44,7 +44,7 @@ UrlSchemeHandler::handleContentRequest(QWebEngineUrlRequestJob *request) try { archive = library->getArchive(zim_id); } catch (std::out_of_range& e) { - request->fail(QWebEngineUrlRequestJob::UrlNotFound); + replyZimNotFoundPage(request, zim_id); return; } try { @@ -176,6 +176,31 @@ UrlSchemeHandler::handleSearchRequest(QWebEngineUrlRequestJob* request) request->reply("text/html", buffer); } +void +UrlSchemeHandler::replyZimNotFoundPage(QWebEngineUrlRequestJob *request, + const QString &zimId) +{ + QBuffer *buffer = new QBuffer; + QString contentHtml = "
" + "

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

" + "

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

" + "

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

" + "
"; + + buffer->open(QIODevice::WriteOnly); + buffer->write(contentHtml.toStdString().c_str()); + buffer->close(); + + connect(request, SIGNAL(destroyed()), buffer, SLOT(deleteLater())); + request->reply("text/html", buffer); +} + void UrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) { diff --git a/src/urlschemehandler.h b/src/urlschemehandler.h index e7e227a..26d80d8 100644 --- a/src/urlschemehandler.h +++ b/src/urlschemehandler.h @@ -13,6 +13,8 @@ private: void handleMetaRequest(QWebEngineUrlRequestJob *request); void handleContentRequest(QWebEngineUrlRequestJob *request); void handleSearchRequest(QWebEngineUrlRequestJob *request); + + void replyZimNotFoundPage(QWebEngineUrlRequestJob *request, const QString& zimId); }; #endif // URLSCHEMEHANDLER_H