Skip to content

Commit

Permalink
Added custom file-not-found page
Browse files Browse the repository at this point in the history
When we try to open or navigate a zim that no longer exists, this page is displayed. Contains the url of the requested zim article
  • Loading branch information
ShaopengLin committed Jun 16, 2024
1 parent 6ed46b0 commit 5afaeb2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,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": "We can't find the Zim file corresponding to this page. Please check that your file exists.",
"zim-url": "Zim Url"
}
5 changes: 4 additions & 1 deletion resources/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,8 @@
"clear-filter": "Represents the action of clearing the filters selected for a filter type.",
"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."
"no-videos": "A content type for Zim files that does not contain videos.",
"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-url": "The Zim Url path for the currently displayed tab"
}
26 changes: 25 additions & 1 deletion src/urlschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, qurl);
return;
}
try {
Expand Down Expand Up @@ -176,6 +176,30 @@ UrlSchemeHandler::handleSearchRequest(QWebEngineUrlRequestJob* request)
request->reply("text/html", buffer);
}

void UrlSchemeHandler::replyZimNotFoundPage(QWebEngineUrlRequestJob *request,
const QUrl &url)
{
QBuffer *buffer = new QBuffer;
QString contentHtml = "<section><div><div>"
"<h1>" +
gt("file-not-found-title") +
"</h1>"
"<p>" +
gt("file-not-found-text") +
"</p>"
"<p>" +
gt("zim-url") + ": <b>" + url.url() +
"</b></p>"
"</div></div></section>";

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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/urlschemehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class UrlSchemeHandler : public QWebEngineUrlSchemeHandler
void handleMetaRequest(QWebEngineUrlRequestJob *request);
void handleContentRequest(QWebEngineUrlRequestJob *request);
void handleSearchRequest(QWebEngineUrlRequestJob *request);

void replyZimNotFoundPage(QWebEngineUrlRequestJob *request, const QUrl& url);
};

#endif // URLSCHEMEHANDLER_H

0 comments on commit 5afaeb2

Please sign in to comment.