diff --git a/resources/i18n/en.json b/resources/i18n/en.json index 3be13d0b..ddd65054 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -48,6 +48,8 @@ "save-file-as-window-title":"Save File as", "download-finished": "Download Finished", "download-finished-message":"The document has been downloaded.", + "article-download-finished": "Article saved", + "article-download-finished-message":"The article \"{{TITLE}}\" has been saved at:\n{{PATH}}.", "file":"File", "edit":"Edit", "view":"View", @@ -111,7 +113,7 @@ "details":"Full article", "yes":"yes", "no":"no", - "ok":"ok", + "ok":"OK", "no-filter":"no filter", "open-link-in-web-browser":"Open link in web browser", "copy-link":"Copy link", @@ -172,7 +174,7 @@ "import-reading-list": "Import reading list", "import-reading-list-error": "An error has occured during import of the reading list.", "disable-sandbox": "Kiwix has been launched from a network drive. This is known to cause compatibility issues with the browsing sandboxing. As a result, the sandbox will be disabled. Do you want to continue?", - "save-page-as": "Save As...", + "save-page-as": "Save article", "portable-disabled-tooltip": "Function disabled in portable mode", "scroll-next-tab": "Scroll to next tab", "scroll-previous-tab": "Scroll to previous tab", diff --git a/src/kiwixmessagebox.cpp b/src/kiwixmessagebox.cpp index 2114021a..2d48ac84 100644 --- a/src/kiwixmessagebox.cpp +++ b/src/kiwixmessagebox.cpp @@ -30,6 +30,8 @@ KiwixMessageBox::KiwixMessageBox(QString confirmTitle, QString confirmText, bool m_result = CloseClicked; }); ui->confirmText->setText(confirmText); + ui->confirmText->setWordWrap(true); + ui->confirmText->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Minimum); ui->confirmTitle->setText(confirmTitle); ui->yesButton->setText(leftAction); ui->noButton->setText(rightAction); diff --git a/src/kprofile.cpp b/src/kprofile.cpp index 0d1e65c0..7c08090e 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -24,32 +24,61 @@ QWebEngineScript getScript(QString filename, return script; } +QString getExtensionFromFilter(const QString &filter) { + const QRegularExpression re("\\(\\*\\.(\\w+)\\)"); + const QRegularExpressionMatch match = re.match(filter); + + if (match.hasMatch()) { + return "." + match.captured(1); + } + return QString(); +} + } #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #define DownloadFinishedSignal WebEngineDownloadType::finished + #define DownloadInProgress QWebEngineDownloadItem::DownloadInProgress #else #define DownloadFinishedSignal WebEngineDownloadType::isFinishedChanged + #define DownloadInProgress QWebEngineDownloadRequest::DownloadInProgress #endif -QString askForSaveFilePath(const QString& suggestedName) +QString askForSaveFilePath(const QString &suggestedName, const QString &filter) { + const auto app = KiwixApp::instance(); + const QString suggestedPath = app->getPrevSaveDir() + "/" + suggestedName; + QString selectedFilter; + QString fileName = QFileDialog::getSaveFileName( + app->getMainWindow(), gt("save-file-as-window-title"), suggestedPath, + filter,&selectedFilter); + + if (fileName.isEmpty()) + return QString(); + + const QString extension = getExtensionFromFilter(selectedFilter); + if (!fileName.endsWith(extension)) { + fileName.append(extension); + } + app->savePrevSaveDir(QFileInfo(fileName).absolutePath()); + return fileName; +} + +void downloadFinished() { - const auto app = KiwixApp::instance(); - const QString suggestedPath = app->getPrevSaveDir() + "/" + suggestedName; - const QString extension = suggestedName.section(".", -1); - const QString filter = extension.isEmpty() ? "" : "(*." + extension + ")"; - QString fileName = QFileDialog::getSaveFileName( - app->getMainWindow(), gt("save-file-as-window-title"), - suggestedPath, filter); + showInfoBox(gt("download-finished"), + gt("download-finished-message"), + KiwixApp::instance()->getMainWindow() + ); +} - if (fileName.isEmpty()) - return QString(); +void articleDownloadFinished(const QString &title,const QString &filePath) +{ + QString finishedMessage = gt("article-download-finished-message"); + finishedMessage = finishedMessage.replace("{{TITLE}}", title); + finishedMessage = finishedMessage.replace("{{PATH}}", filePath); - if (!fileName.endsWith(extension)) { - fileName.append(extension); - } - app->savePrevSaveDir(QFileInfo(fileName).absolutePath()); - return fileName; + showInfoBox(gt("article-download-finished"), finishedMessage, + KiwixApp::instance()->getMainWindow()); } KProfile::KProfile(QObject *parent) : @@ -121,27 +150,30 @@ void KProfile::openFile(WebEngineDownloadType* download) void KProfile::saveFile(WebEngineDownloadType* download) { const QString defaultFileName = getDownloadFilePath(download); - const QString fileName = askForSaveFilePath(defaultFileName); + const QString extension = defaultFileName.section(".", -1); + const QString filter = extension.isEmpty() ? "" : "(*." + extension + ")"; + const QString fileName = askForSaveFilePath(defaultFileName, filter); if (fileName.isEmpty()) { download->cancel(); return; } setDownloadFilePath(download, fileName); - connect(download, &DownloadFinishedSignal, this, &KProfile::downloadFinished); + connect(download, &DownloadFinishedSignal, &downloadFinished); download->accept(); } -void KProfile::downloadFinished() -{ - showInfoBox(gt("download-finished"), - gt("download-finished-message"), - KiwixApp::instance()->getMainWindow() - ); -} - void KProfile::startDownload(WebEngineDownloadType* download) { + if (download->state() == DownloadInProgress) { + connect(download, &DownloadFinishedSignal, [download]() { + const QString fullPath = QDir(download->downloadDirectory()) + .filePath(download->downloadFileName()); + articleDownloadFinished(download->page()->title(), fullPath); + }); + return; + } + const auto res = showKiwixMessageBox( gt("save-or-open"), gt("save-or-open-text"), diff --git a/src/kprofile.h b/src/kprofile.h index 42a9d627..6ee75450 100644 --- a/src/kprofile.h +++ b/src/kprofile.h @@ -26,7 +26,6 @@ class KProfile : public QWebEngineProfile private slots: void startDownload(WebEngineDownloadType*); - void downloadFinished(); void saveFile(WebEngineDownloadType*); void openFile(WebEngineDownloadType*); }; diff --git a/src/webview.cpp b/src/webview.cpp index 19cd84e1..aadc1fc2 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -23,7 +23,8 @@ class QMenu; #include "tableofcontentbar.h" zim::Entry getArchiveEntryFromUrl(const zim::Archive& archive, const QUrl& url); -QString askForSaveFilePath(const QString& suggestedName); +QString askForSaveFilePath(const QString& suggestedName , const QString& filters); +void articleDownloadFinished(const QString &title, const QString &filePath); void WebViewBackMenu::showEvent(QShowEvent *) { @@ -88,6 +89,11 @@ WebView::WebView(QWidget *parent) connect(this->page(), &QWebEnginePage::linkHovered, this, [=] (const QString& url) { m_linkHovered = url; }); + connect(page(), &QWebEnginePage::pdfPrintingFinished, + [=](const QString &filePath,const bool success) { + if (success) + articleDownloadFinished(page()->title(), filePath); + }); /* In Qt 5.12, the zoom factor is not correctly passed after a fulltext search * Bug Report: https://bugreports.qt.io/browse/QTBUG-51851 @@ -199,9 +205,17 @@ void WebView::saveViewContent() const QString suggestedFileName = QString::fromStdString(kiwix::getSlugifiedFileName(item.getTitle())); if (isHTMLContent(item)) { - const QString fileName = askForSaveFilePath(suggestedFileName + ".pdf"); - if (!fileName.isEmpty()) - page()->printToPdf(fileName); + const QString filters = QString("PDF Document (*.pdf)") + QString(";;") + QString("(Web Page, complete (*.html)"); + const QString fileName = askForSaveFilePath(suggestedFileName + ".pdf", filters); + + if (!fileName.isEmpty()) { + const QString extension = fileName.section(".", -1); + if (extension == "pdf") { + page()->printToPdf(fileName); + } else { + page()->save(fileName); + } + } } else page()->download(this->url(), suggestedFileName); @@ -344,7 +358,6 @@ QMenu* WebView::createStandardContextMenu() { KiwixApp::instance()->getTabWidget()->triggerWebPageAction(QWebEnginePage::Forward); }); - menu->addAction(app->getAction(KiwixApp::SavePageAsAction)); return menu; }