From 60effd4c8447a3950389bff4d5c4248bfb679794 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Mon, 3 Jan 2022 17:29:09 +0530 Subject: [PATCH 1/3] Add exception handling for book modification while kiwix-desktop is running A message box is shown if the file couldn't be opened and the entry is removed from library. --- resources/i18n/en.json | 4 +++- src/contentmanager.cpp | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/resources/i18n/en.json b/resources/i18n/en.json index 01fe48f..80ecaa4 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -121,5 +121,7 @@ "open-link-in-web-browser":"Open link in web browser", "download-dir-dialog-title":"Are you sure you want to change the download directory?", "download-dir-dialog-msg":"The new download directory path will be:\n{{DIRECTORY}}", - "invalid-port":"Invalid port" + "invalid-port":"Invalid port", + "zim-open-fail-title":"Invalid file", + "zim-open-fail-text":"The ZIM file {{ZIM}} cannot be open properly. It will be removed from your library." } diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 00138ec..d5d3145 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -10,6 +10,7 @@ #include #include #include +#include ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent) : QObject(parent), @@ -120,7 +121,25 @@ QStringList ContentManager::getBookInfos(QString id, const QStringList &keys) void ContentManager::openBook(const QString &id) { QUrl url("zim://"+id+".zim/"); - KiwixApp::instance()->openUrl(url, true); + try { + KiwixApp::instance()->openUrl(url, true); + } catch (const std::exception& e) { + auto tabBar = KiwixApp::instance()->getTabWidget(); + tabBar->closeTab(1); + auto text = gt("zim-open-fail-text"); + text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getPath())); + QMessageBox msgBox( + QMessageBox::Warning, //Icon + gt("zim-open-fail-title"), //Title + text, //Text + QMessageBox::Ok //Buttons + ); + msgBox.exec(); + mp_library->removeBookFromLibraryById(id); + tabBar->setCurrentIndex(0); + emit(booksChanged()); + return; + } } #define ADD_V(KEY, METH) {if(key==KEY) {values.append(QString::fromStdString((d->METH()))); continue;}} From f14d633fd3ce7bc73149c52a5fa845ce6b58c75e Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Tue, 4 Jan 2022 13:33:41 +0530 Subject: [PATCH 2/3] Add check to not open zim file (from command line arguments) if it is invalid --- src/kiwixapp.cpp | 10 +++++++++- src/library.cpp | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index 70c7f26..ce3cadd 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -176,7 +176,15 @@ void KiwixApp::openZimFile(const QString &zimfile) try { zimId = m_library.openBookFromPath(validZimFile); } catch (const std::exception& e) { - showMessage("Cannot open " + validZimFile + ": \n" + e.what()); + auto text = gt("zim-open-fail-text"); + text = text.replace("{{ZIM}}", validZimFile); + QMessageBox msgBox( + QMessageBox::Warning, //Icon + gt("zim-open-fail-title"), //Title + text, //Text + QMessageBox::Ok //Buttons + ); + msgBox.exec(); return; } openUrl(QUrl("zim://"+zimId+".zim/")); diff --git a/src/library.cpp b/src/library.cpp index ffe0504..8ec5298 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -48,6 +48,9 @@ QString Library::openBookFromPath(const QString &zimPath) kiwix::Manager manager(&m_library); auto id = manager.addBookFromPathAndGetId(zimPath.toStdString()); + if (id == "") { + throw std::invalid_argument("invalid zim file"); + } save(); emit(booksChanged()); return QString::fromStdString(id); From 90afbf3f92df9d05524fe2b595b7eeab35d3ff25 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Sun, 16 Jan 2022 17:39:15 +0530 Subject: [PATCH 3/3] Update showMessage() This removes the previous default error message. KiwixApp::showMessage() now accepts title, text and MessageBox icon --- resources/i18n/en.json | 1 + src/contentmanager.cpp | 9 ++------- src/kiwixapp.cpp | 22 +++++++++++----------- src/kiwixapp.h | 3 ++- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/resources/i18n/en.json b/resources/i18n/en.json index 80ecaa4..55f3955 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -15,6 +15,7 @@ "home-page":"Home page", "main-menu":"Main menu", "print":"Print", + "print-page-error": "An error has occured while printing.", "new-tab":"New tab", "close-tab":"Close tab", "close":"Close", diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index d5d3145..06d3f66 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -128,13 +128,8 @@ void ContentManager::openBook(const QString &id) tabBar->closeTab(1); auto text = gt("zim-open-fail-text"); text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getPath())); - QMessageBox msgBox( - QMessageBox::Warning, //Icon - gt("zim-open-fail-title"), //Title - text, //Text - QMessageBox::Ok //Buttons - ); - msgBox.exec(); + auto title = gt("zim-open-fail-title"); + KiwixApp::instance()->showMessage(text, title, QMessageBox::Warning); mp_library->removeBookFromLibraryById(id); tabBar->setCurrentIndex(0); emit(booksChanged()); diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index ce3cadd..c5f3e88 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -178,13 +178,7 @@ void KiwixApp::openZimFile(const QString &zimfile) } catch (const std::exception& e) { auto text = gt("zim-open-fail-text"); text = text.replace("{{ZIM}}", validZimFile); - QMessageBox msgBox( - QMessageBox::Warning, //Icon - gt("zim-open-fail-title"), //Title - text, //Text - QMessageBox::Ok //Buttons - ); - msgBox.exec(); + showMessage(text, gt("zim-open-fail-title"), QMessageBox::Warning); return; } openUrl(QUrl("zim://"+zimId+".zim/")); @@ -204,7 +198,7 @@ void KiwixApp::printPage() return; webview->page()->print(printer, [=](bool success) { if (!success) { - showMessage("An error has occured while printing."); + showMessage(gt("print-page-error"), gt("error-title"), QMessageBox::Critical); } delete printer; }); @@ -235,13 +229,19 @@ void KiwixApp::openRandomUrl(bool newTab) url.setPath("/" + QString::fromStdString(entry.getPath())); openUrl(url, newTab); } catch ( const kiwix::NoEntry& ) { - showMessage(gt("random-article-error")); + showMessage(gt("random-article-error"), gt("error-title"), QMessageBox::Information); } } -void KiwixApp::showMessage(const QString &message) +void KiwixApp::showMessage(const QString &message, const QString &title, const enum QMessageBox::Icon &icon) { - mp_errorDialog->showMessage(message); + QMessageBox msgBox( + icon, //Icon + title, //Title + message, //Text + QMessageBox::Ok //Buttons + ); + msgBox.exec(); } QAction *KiwixApp::getAction(KiwixApp::Actions action) diff --git a/src/kiwixapp.h b/src/kiwixapp.h index d7a727a..38664d8 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -66,7 +67,7 @@ public: void openRandomUrl(bool newTab=true); - void showMessage(const QString& message); + void showMessage(const QString& message, const QString& title, const enum QMessageBox::Icon& icon); KProfile* getProfile() { return &m_profile; } Library* getLibrary() { return &m_library; }