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 url 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 21, 2024
1 parent 8668054 commit c482195
Show file tree
Hide file tree
Showing 8 changed files with 89 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 @@ -170,5 +170,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 @@ -177,5 +177,7 @@
"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-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"
}
12 changes: 11 additions & 1 deletion src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,13 @@ void KiwixApp::restoreTabs()
mp_session = new QSettings(dir.filePath("kiwix-desktop.session"),
QSettings::defaultFormat(), this);
QStringList tabsToOpen = mp_session->value("reopenTabList").toStringList();

BookInfoMap tabInfoMap = mp_session->value("tabBookInfoMap").value<BookInfoMap>();

/* Restart a new session to prevent duplicate records in openURL */
saveListOfOpenTabs();
saveTabBookInfoMap();

getTabWidget()->setBookInfoMap(std::move(tabInfoMap));
if (m_settingsManager.getReopenTab())
{
for (const auto &zimUrl : tabsToOpen)
Expand Down Expand Up @@ -577,3 +581,9 @@ void KiwixApp::saveCurrentTabIndex()
{
return mp_session->setValue("currentTabIndex", getTabWidget()->currentIndex());
}

void KiwixApp::saveTabBookInfoMap()
{
qInfo() << getTabWidget()->getBookInfoMap() << "\n";
return mp_session->setValue("tabBookInfoMap", getTabWidget()->getBookInfoMap());
}
1 change: 1 addition & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class KiwixApp : public QtSingleApplication
void saveWindowState();
void restoreWindowState();
void saveCurrentTabIndex();
void saveTabBookInfoMap();

public slots:
void newTab();
Expand Down
31 changes: 31 additions & 0 deletions src/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ int TabBar::realTabCount() const
return count() < 1 ? 0 : count() - 1;
}

void TabBar::removeBookInfoById(const QString& zimId)
{
/* No need to remove yet if we still have a bookmark with this id. */
auto& bookmarks = KiwixApp::instance()->getLibrary()->getBookmarks(false);
for (auto& bookmark : bookmarks)
{
if (QString::fromStdString(bookmark.getBookId()) == zimId)
return;
}

/* No need to remove yet if we still have this zimId opened. */
for (int index = 0; index <= mp_stackedWidget->count(); index++)
{
if (ZimView* zv = qobject_cast<ZimView*>(mp_stackedWidget->widget(index)))
if (zv->getWebView()->zimId() == zimId)
return;
}
m_bookInfoMap.remove(zimId);
}

void TabBar::moveToNextTab()
{
const int index = currentIndex();
Expand Down Expand Up @@ -176,6 +196,8 @@ void TabBar::openUrl(const QUrl& url, bool newTab)
}
QUITIFNULL(webView);
webView->setUrl(url);

KiwixApp::instance()->saveTabBookInfoMap();
}

void TabBar::setTitleOf(const QString& title, ZimView* tab)
Expand Down Expand Up @@ -282,6 +304,11 @@ QStringList TabBar::getTabUrls() const {
return idList;
}

BookInfo TabBar::getBookInfoById(const QString& zimId) const
{
return m_bookInfoMap.contains(zimId) ? m_bookInfoMap[zimId].toJsonObject() : BookInfo{};
}

void TabBar::closeTab(int index)
{
// The first and last tabs (i.e. the library tab and the + (new tab) button)
Expand All @@ -298,8 +325,12 @@ void TabBar::closeTab(int index)
view->setParent(nullptr);
removeTab(index);
view->close();

if (ZimView* zv = qobject_cast<ZimView*>(view))
removeBookInfoById(zv->getWebView()->zimId());
view->deleteLater();

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

Expand Down
25 changes: 25 additions & 0 deletions src/tabbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@
#include "fullscreenwindow.h"
#include <QMouseEvent>
#include <QWebEngineFullScreenRequest>
#include <QJsonObject>

struct BookInfo
{
QString name = "N/A";
QString path = "N/A";

BookInfo() {};
BookInfo(const QString &name, const QString &path)
: name{name}, path{path} {};
BookInfo(const QJsonObject &json)
: name{json.value("name").toString()},
path{json.value("path").toString()} {};

QJsonObject toJson() const { return {{"name", name}, {"path", path}}; };
};

using BookInfoMap = QVariantMap;

class TabBar : public QTabBar
{
Expand Down Expand Up @@ -41,6 +59,8 @@ class TabBar : public QTabBar
// Redirect call to sub-webView
void setTitleOf(const QString& title, ZimView* tab=nullptr);
void setIconOf(const QIcon& icon, ZimView* tab=nullptr);
void addBookInfoById(const QString& zimId, const BookInfo& info) { m_bookInfoMap[zimId] = info.toJson(); };
void setBookInfoMap(const BookInfoMap& map) { m_bookInfoMap = map; };
QString currentZimId();

void triggerWebPageAction(QWebEnginePage::WebAction action, ZimView* widget=nullptr);
Expand All @@ -50,6 +70,9 @@ class TabBar : public QTabBar
void openFindInPageBar();
void closeTabsByZimId(const QString &id);
QStringList getTabUrls() const;
BookInfoMap getBookInfoMap() const { return m_bookInfoMap; };
BookInfo getBookInfoById(const QString& zimId) const;
bool isIdInBookInfoMap(const QString& zimId) const { return m_bookInfoMap.contains(zimId); };

protected:
void mousePressEvent(QMouseEvent *event);
Expand All @@ -74,10 +97,12 @@ public slots:
private:
QStackedWidget* mp_stackedWidget;
QScopedPointer<FullScreenWindow> m_fullScreenWindow;
BookInfoMap m_bookInfoMap;

// The "+" (new tab) button is implemented as a tab (that is always placed at the end).
// This function returns the count of real tabs.
int realTabCount() const;
void removeBookInfoById(const QString& zimId);

private slots:
void onTabMoved(int from, int to);
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;
BookInfo info = KiwixApp::instance()->getTabWidget()->getBookInfoById(zimId);
QString contentHtml = "<section><div><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>" + info.name +
"</b></p>"
"<p>" +
gt("zim-path") + ": <b>" + info.path +
"</b></p>"
"</div></div></section>";

buffer->open(QIODevice::WriteOnly);
Expand Down
8 changes: 8 additions & 0 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,17 @@ void WebView::onUrlChanged(const QUrl& url) {
m_currentZimId = zimId;
emit zimIdChanged(m_currentZimId);
std::shared_ptr<zim::Archive> archive;
auto tabWidget = app->getTabWidget();
try {
archive = app->getLibrary()->getArchive(m_currentZimId);
auto book = app->getLibrary()->getBookById(m_currentZimId);

BookInfo info {book.getName().c_str(), book.getPath().c_str()};
tabWidget->addBookInfoById(m_currentZimId, info);
} catch (std::out_of_range& e) {
/* If there is already a record, we do not need to overwrite it. */
if (!tabWidget->isIdInBookInfoMap(m_currentZimId))
tabWidget->addBookInfoById(m_currentZimId, BookInfo{});
return;
}
auto zoomFactor = app->getSettingsManager()->getZoomFactorByZimId(zimId);
Expand Down

0 comments on commit c482195

Please sign in to comment.