diff --git a/resources/i18n/en.json b/resources/i18n/en.json index 01fe48f..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", @@ -121,5 +122,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..06d3f66 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,20 @@ 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())); + auto title = gt("zim-open-fail-title"); + KiwixApp::instance()->showMessage(text, title, QMessageBox::Warning); + 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;}} diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index 70c7f26..c5f3e88 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -176,7 +176,9 @@ 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); + showMessage(text, gt("zim-open-fail-title"), QMessageBox::Warning); return; } openUrl(QUrl("zim://"+zimId+".zim/")); @@ -196,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; }); @@ -227,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; } 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);