diff --git a/resources/i18n/en.json b/resources/i18n/en.json index beff86f29..5b99af8f8 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -9,6 +9,7 @@ "error-downloader-launch-message":"Impossible to launch downloader, Kiwix-desktop will start but all download functions will not working!", "error-launch-server-message":"An error has occured!", "error-archive":"Cannot get the archive", + "error-opening-file": "There was an error opening the file.", "open-zim":"Open ZIM File", "local-kiwix-server":"Local Kiwix Server", "random-article":"Random Article", @@ -23,6 +24,7 @@ "reopen-closed-tab":"Reopen closed tab", "browse-library":"Browse library", "open-file":"Open file", + "save-file": "Save file", "open-recent":"Open recent", "search-article":"Search article", "search-in-library":"Search in library", @@ -167,5 +169,7 @@ "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", + "save-or-open": "Save or Open file", + "save-or-open-text": "What should Kiwix do with this file?" } diff --git a/src/kiwixmessagebox.cpp b/src/kiwixmessagebox.cpp index 56592b275..1e6431c7b 100644 --- a/src/kiwixmessagebox.cpp +++ b/src/kiwixmessagebox.cpp @@ -61,4 +61,3 @@ KiwixMessageBox::Result showKiwixMessageBox(QString title, QString text, QWidget KiwixMessageBox *dialog = new KiwixMessageBox(title, text, false, parent, leftTitle, rightTitle); return dialog->execDialog(); } - diff --git a/src/kprofile.cpp b/src/kprofile.cpp index fa09dc5c9..93e002103 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -4,6 +4,11 @@ #include #include #include +#include +#include +#include +#include +#include "kiwixmessagebox.h" KProfile::KProfile(QObject *parent) : QWebEngineProfile(parent) @@ -18,40 +23,66 @@ KProfile::KProfile(QObject *parent) : #endif } -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) -void KProfile::startDownload(QWebEngineDownloadItem* download) -#else -void KProfile::startDownload(QWebEngineDownloadRequest* download) -#endif +namespace { + void setDownloadFilePath(KProfile::WebEngineDownloadType* download, QString filePath) + { + #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) + download->setPath(filePath); + #else + download->setDownloadFileName(filePath); + #endif + } +} + +void KProfile::openFile(WebEngineDownloadType* download) +{ + QString defaultFileName = download->url().fileName(); + QTemporaryFile tempFile(QDir::tempPath() + "/XXXXXX." + QFileInfo(defaultFileName).suffix()); + tempFile.setAutoRemove(false); + if (tempFile.open()) { + QString tempFilePath = tempFile.fileName(); + tempFile.close(); + setDownloadFilePath(download, tempFilePath); + connect(download, &WebEngineDownloadType::finished, [tempFilePath]() { + if(!QDesktopServices::openUrl(QUrl::fromLocalFile(tempFilePath))) + showInfoBox(gt("error-title"), gt("error-opening-file"), KiwixApp::instance()->getMainWindow()); + }); + download->accept(); + } else { + qDebug()<<"tmp file err"; + download->cancel(); + } +} + +void KProfile::saveFile(WebEngineDownloadType* download) { QString defaultFileName = download->url().fileName(); - QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(), - gt("save-file-as-window-title"), defaultFileName); + QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(), "Save File", defaultFileName); if (fileName.isEmpty()) { + download->cancel(); return; } QString extension = "." + download->url().url().section('.', -1); if (!fileName.endsWith(extension)) { fileName.append(extension); } -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) - download->setPath(fileName); -#else - download->setDownloadFileName(fileName); -#endif -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - connect(download, &QWebEngineDownloadItem::finished, this, &KProfile::downloadFinished); -#else - connect(download, &QWebEngineDownloadRequest::isFinished, this, &KProfile::downloadFinished); -#endif + setDownloadFilePath(download, fileName); download->accept(); } -void KProfile::downloadFinished() +void KProfile::startDownload(WebEngineDownloadType* download) { - QMessageBox msgBox; - msgBox.setText(gt("download-finished-message")); - msgBox.exec(); + auto res = showKiwixMessageBox(gt("save-or-open"), gt("save-or-open-text"), + KiwixApp::instance()->getMainWindow(), gt("save-file"), gt("open-file")); + if (res == KiwixMessageBox::YesClicked) { + saveFile(download); + return; + } + if (res == KiwixMessageBox::NoClicked) { + openFile(download); + return; + } + download->cancel(); } void ExternalReqInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) diff --git a/src/kprofile.h b/src/kprofile.h index 98e75a90a..c63569abd 100644 --- a/src/kprofile.h +++ b/src/kprofile.h @@ -16,19 +16,21 @@ class KProfile : public QWebEngineProfile Q_OBJECT public: KProfile(QObject *parent = nullptr); - +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + typedef QWebEngineDownloadItem WebEngineDownloadType; +#else + typedef QWebEngineDownloadRequest WebEngineDownloadType; +#endif private: UrlSchemeHandler m_schemeHandler; signals: public slots: + void startDownload(WebEngineDownloadType*); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - void startDownload(QWebEngineDownloadItem*); -#else - void startDownload(QWebEngineDownloadRequest*); -#endif - void downloadFinished(); +private slots: + void saveFile(WebEngineDownloadType*); + void openFile(WebEngineDownloadType*); }; /**