From 24fa7f342675cc607c1794a29c132a97b7450bda Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Mon, 3 Jun 2024 19:08:20 +0530 Subject: [PATCH 1/6] KiwixConfirmBox is now KiwixMessageBox It started as a simple yes/no box but is now being used as an InfoBox, ConfirmBox and a customized MessageBox (in further commits) - this sounds better! --- kiwix-desktop.pro | 6 ++--- src/contentmanager.cpp | 2 +- src/downloadmanagement.cpp | 2 +- ...iwixconfirmbox.cpp => kiwixmessagebox.cpp} | 14 ++++++------ src/{kiwixconfirmbox.h => kiwixmessagebox.h} | 22 +++++++++---------- ui/{kiwixconfirmbox.ui => kiwixmessagebox.ui} | 4 ++-- 6 files changed, 25 insertions(+), 25 deletions(-) rename src/{kiwixconfirmbox.cpp => kiwixmessagebox.cpp} (77%) rename src/{kiwixconfirmbox.h => kiwixmessagebox.h} (71%) rename ui/{kiwixconfirmbox.ui => kiwixmessagebox.ui} (97%) diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index 8d370af09..d9be13a28 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -55,7 +55,7 @@ SOURCES += \ src/findinpagebar.cpp \ src/flowlayout.cpp \ src/kiwixchoicebox.cpp \ - src/kiwixconfirmbox.cpp \ + src/kiwixmessagebox.cpp \ src/kiwixlineedit.cpp \ src/kiwixlistwidget.cpp \ src/kiwixloader.cpp \ @@ -105,7 +105,7 @@ HEADERS += \ src/findinpagebar.h \ src/flowlayout.h \ src/kiwixchoicebox.h \ - src/kiwixconfirmbox.h \ + src/kiwixmessagebox.h \ src/kiwixlineedit.h \ src/kiwixlistwidget.h \ src/kiwixloader.h \ @@ -151,7 +151,7 @@ FORMS += \ src/contentmanagerview.ui \ src/findinpagebar.ui \ ui/kiwixchoicebox.ui \ - ui/kiwixconfirmbox.ui \ + ui/kiwixmessagebox.ui \ ui/mainwindow.ui \ ui/about.ui \ src/contentmanagerside.ui \ diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 2fc6d3a90..b682c6773 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -17,7 +17,7 @@ #include "node.h" #include "rownode.h" #include "descriptionnode.h" -#include "kiwixconfirmbox.h" +#include "kiwixmessagebox.h" #include #include "contentmanagerheader.h" #include diff --git a/src/downloadmanagement.cpp b/src/downloadmanagement.cpp index 52b4e20b6..16a4f7cdc 100644 --- a/src/downloadmanagement.cpp +++ b/src/downloadmanagement.cpp @@ -1,7 +1,7 @@ #include "downloadmanagement.h" #include "kiwixapp.h" -#include "kiwixconfirmbox.h" +#include "kiwixmessagebox.h" #include #include diff --git a/src/kiwixconfirmbox.cpp b/src/kiwixmessagebox.cpp similarity index 77% rename from src/kiwixconfirmbox.cpp rename to src/kiwixmessagebox.cpp index 8811bc6aa..0b1719be5 100644 --- a/src/kiwixconfirmbox.cpp +++ b/src/kiwixmessagebox.cpp @@ -1,11 +1,11 @@ -#include "kiwixconfirmbox.h" -#include "ui_kiwixconfirmbox.h" +#include "kiwixmessagebox.h" +#include "ui_kiwixmessagebox.h" #include #include "kiwixapp.h" -KiwixConfirmBox::KiwixConfirmBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent) : +KiwixMessageBox::KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent) : QDialog(parent), m_confirmTitle(confirmTitle), m_confirmText(confirmText), - ui(new Ui::kiwixconfirmbox) + ui(new Ui::kiwixmessagebox) { ui->setupUi(this); setWindowFlag(Qt::FramelessWindowHint, true); @@ -32,16 +32,16 @@ KiwixConfirmBox::KiwixConfirmBox(QString confirmTitle, QString confirmText, bool } } -KiwixConfirmBox::~KiwixConfirmBox() +KiwixMessageBox::~KiwixMessageBox() { delete ui; } void showInfoBox(QString title, QString text, QWidget *parent) { - KiwixConfirmBox *dialog = new KiwixConfirmBox(title, text, true, parent); + KiwixMessageBox *dialog = new KiwixMessageBox(title, text, true, parent); dialog->show(); - QObject::connect(dialog, &KiwixConfirmBox::okClicked, [=]() { + QObject::connect(dialog, &KiwixMessageBox::okClicked, [=]() { dialog->deleteLater(); }); } diff --git a/src/kiwixconfirmbox.h b/src/kiwixmessagebox.h similarity index 71% rename from src/kiwixconfirmbox.h rename to src/kiwixmessagebox.h index f8f35c43c..41e195a94 100644 --- a/src/kiwixconfirmbox.h +++ b/src/kiwixmessagebox.h @@ -1,21 +1,21 @@ -#ifndef KIWIXCONFIRMBOX_H -#define KIWIXCONFIRMBOX_H +#ifndef KIWIXMESSAGEBOX_H +#define KIWIXMESSAGEBOX_H #include #include namespace Ui { -class kiwixconfirmbox; +class kiwixmessagebox; } -class KiwixConfirmBox : public QDialog +class KiwixMessageBox : public QDialog { Q_OBJECT public: - KiwixConfirmBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr); - ~KiwixConfirmBox(); + KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr); + ~KiwixMessageBox(); signals: void yesClicked(); @@ -25,7 +25,7 @@ class KiwixConfirmBox : public QDialog private: QString m_confirmTitle; QString m_confirmText; - Ui::kiwixconfirmbox *ui; + Ui::kiwixmessagebox *ui; }; @@ -35,13 +35,13 @@ template void showConfirmBox(QString title, QString text, QWidget *parent, YesAction yesAction) { - KiwixConfirmBox *dialog = new KiwixConfirmBox(title, text, false, parent); + KiwixMessageBox *dialog = new KiwixMessageBox(title, text, false, parent); dialog->show(); - QObject::connect(dialog, &KiwixConfirmBox::yesClicked, [=]() { + QObject::connect(dialog, &KiwixMessageBox::yesClicked, [=]() { yesAction(); dialog->deleteLater(); }); - QObject::connect(dialog, &KiwixConfirmBox::noClicked, [=]() { + QObject::connect(dialog, &KiwixMessageBox::noClicked, [=]() { dialog->deleteLater(); }); } @@ -66,4 +66,4 @@ inline void showErrorBox(const KiwixAppError& err, QWidget *parent = nullptr) showInfoBox(err.summary(), err.details(), parent); } -#endif // KIWIXCONFIRMBOX_H +#endif // KIWIXMESSAGEBOX_H diff --git a/ui/kiwixconfirmbox.ui b/ui/kiwixmessagebox.ui similarity index 97% rename from ui/kiwixconfirmbox.ui rename to ui/kiwixmessagebox.ui index a561970e7..d2e9a2d40 100644 --- a/ui/kiwixconfirmbox.ui +++ b/ui/kiwixmessagebox.ui @@ -1,7 +1,7 @@ - kiwixconfirmbox - + kiwixmessagebox + 0 From df2d29c8ab8e6e32a408df9615c09839cff5ef16 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Mon, 3 Jun 2024 19:31:16 +0530 Subject: [PATCH 2/6] Introduce showKiwixMessageBox() Added a function to show custom choices instead of yes/no. The chosen option is returned A closeButton is also added to the dialog. --- .../css/{confirmBox.css => messageBox.css} | 7 + resources/style.qrc | 2 +- src/kiwixmessagebox.cpp | 28 +++- src/kiwixmessagebox.h | 13 +- ui/kiwixmessagebox.ui | 122 +++++++++++++----- 5 files changed, 132 insertions(+), 40 deletions(-) rename resources/css/{confirmBox.css => messageBox.css} (83%) diff --git a/resources/css/confirmBox.css b/resources/css/messageBox.css similarity index 83% rename from resources/css/confirmBox.css rename to resources/css/messageBox.css index 85b0aedb3..af4c48bf1 100644 --- a/resources/css/confirmBox.css +++ b/resources/css/messageBox.css @@ -29,4 +29,11 @@ QPushButton:hover { color: white; } +#closeButton { + border: 0; +} + +#closeButton:hover { + background-color: transparent; +} diff --git a/resources/style.qrc b/resources/style.qrc index bb0bef4fb..864bb7fba 100644 --- a/resources/style.qrc +++ b/resources/style.qrc @@ -3,7 +3,7 @@ css/style.css css/popup.css css/localServer.css - css/confirmBox.css + css/messageBox.css css/contentmanagerside.css css/choiceBox.css diff --git a/src/kiwixmessagebox.cpp b/src/kiwixmessagebox.cpp index 0b1719be5..09944cc95 100644 --- a/src/kiwixmessagebox.cpp +++ b/src/kiwixmessagebox.cpp @@ -3,26 +3,36 @@ #include #include "kiwixapp.h" -KiwixMessageBox::KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent) : +KiwixMessageBox::KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent, + QString leftAction, QString rightAction) : QDialog(parent), m_confirmTitle(confirmTitle), m_confirmText(confirmText), ui(new Ui::kiwixmessagebox) { ui->setupUi(this); setWindowFlag(Qt::FramelessWindowHint, true); - setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/confirmBox.css")); + setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/messageBox.css")); connect(ui->yesButton, &QPushButton::clicked, [=]() { emit yesClicked(); + m_result = YesClicked; + accept(); }); connect(ui->noButton, &QPushButton::clicked, [=]() { emit noClicked(); + m_result = NoClicked; + reject(); }); connect(ui->okButton, &QPushButton::clicked, [=]() { emit okClicked(); + m_result = OkClicked; + }); + connect(ui->closeButton, &QPushButton::clicked, [=]() { + this->close(); + m_result = CloseClicked; }); ui->confirmText->setText(confirmText); ui->confirmTitle->setText(confirmTitle); - ui->yesButton->setText(gt("yes")); - ui->noButton->setText(gt("no")); + ui->yesButton->setText(leftAction); + ui->noButton->setText(rightAction); ui->okButton->setText(gt("ok")); ui->okButton->hide(); if (okDialog) { @@ -45,3 +55,13 @@ void showInfoBox(QString title, QString text, QWidget *parent) dialog->deleteLater(); }); } + +KiwixMessageBox::Result showKiwixMessageBox(QString title, QString text, QWidget *parent, QString leftTitle, QString rightTitle) +{ + KiwixMessageBox *dialog = new KiwixMessageBox(title, text, false, parent, leftTitle, rightTitle); + QObject::connect(dialog, &KiwixMessageBox::finished, [=]() { + dialog->deleteLater(); + }); + return dialog->execDialog(); +} + diff --git a/src/kiwixmessagebox.h b/src/kiwixmessagebox.h index 41e195a94..bee76a563 100644 --- a/src/kiwixmessagebox.h +++ b/src/kiwixmessagebox.h @@ -2,6 +2,7 @@ #define KIWIXMESSAGEBOX_H #include +#include "kiwixapp.h" #include @@ -14,8 +15,16 @@ class KiwixMessageBox : public QDialog Q_OBJECT public: - KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr); + KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr, + QString leftAction = gt("yes"), QString rightAction = gt("no")); ~KiwixMessageBox(); + enum Result { + YesClicked, + NoClicked, + OkClicked, + CloseClicked + }; + Result execDialog() { QDialog::exec(); return m_result; } signals: void yesClicked(); @@ -26,10 +35,12 @@ class KiwixMessageBox : public QDialog QString m_confirmTitle; QString m_confirmText; Ui::kiwixmessagebox *ui; + Result m_result; }; void showInfoBox(QString title, QString text, QWidget *parent = nullptr); +KiwixMessageBox::Result showKiwixMessageBox(QString title, QString text, QWidget *parent, QString leftTitle, QString rightTitle); template void showConfirmBox(QString title, QString text, QWidget *parent, diff --git a/ui/kiwixmessagebox.ui b/ui/kiwixmessagebox.ui index d2e9a2d40..957b2e3a8 100644 --- a/ui/kiwixmessagebox.ui +++ b/ui/kiwixmessagebox.ui @@ -26,39 +26,7 @@ Dialog - - - - - 0 - 0 - - - - - 600 - 16777215 - - - - Would you like to confirm doing xyz? - - - - - - - - 0 - 0 - - - - Confirm title - - - - + @@ -90,6 +58,9 @@ 0 + + PointingHandCursor + Yes @@ -112,6 +83,9 @@ 0 + + PointingHandCursor + Ok @@ -134,6 +108,9 @@ 0 + + PointingHandCursor + No @@ -144,8 +121,85 @@ + + + + + 0 + 0 + + + + + 600 + 16777215 + + + + Would you like to confirm doing xyz? + + + + + + + + + + 0 + 0 + + + + Confirm title + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + + 20 + 16777215 + + + + PointingHandCursor + + + + + + + :/icons/close.svg:/icons/close.svg + + + + + - + + + From f6cb2b92da6d16bfb44a43dc225cd8e475152129 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 6 Nov 2024 13:14:53 +0400 Subject: [PATCH 3/6] Fixed a compilation error when using Qt 6 --- src/kprofile.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kprofile.cpp b/src/kprofile.cpp index 6659ebbd2..a952a7700 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -17,7 +17,7 @@ QString askForSaveFilePath(const QString& suggestedName) if (fileName.isEmpty()) return QString(); - + if (!fileName.endsWith(extension)) { fileName.append(extension); } @@ -62,7 +62,7 @@ void KProfile::startDownload(QWebEngineDownloadRequest* download) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) connect(download, &QWebEngineDownloadItem::finished, this, &KProfile::downloadFinished); #else - connect(download, &QWebEngineDownloadRequest::isFinished, this, &KProfile::downloadFinished); + connect(download, &QWebEngineDownloadRequest::isFinishedChanged, this, &KProfile::downloadFinished); #endif download->accept(); } From 1480944668084b9a6c54ac53b58e568e014dde09 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 6 Nov 2024 12:27:52 +0400 Subject: [PATCH 4/6] Cleaner handling of Qt API change QWebEngineDownloadItem of Qt 5 was replaced with QWebEngineDownloadRequest in Qt 6. Besides the API of QWebEngineDownloadItem has slightly changed in Qt 5.14. This commit reduces the pollution of the code by conditional compilation needed to support all those versions of Qt. --- src/kprofile.cpp | 51 +++++++++++++++++++++++++++++++++--------------- src/kprofile.h | 8 +++----- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/kprofile.cpp b/src/kprofile.cpp index a952a7700..5859ffd31 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -5,6 +5,12 @@ #include #include +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + #define DownloadFinishedSignal WebEngineDownloadType::finished +#else + #define DownloadFinishedSignal WebEngineDownloadType::isFinishedChanged +#endif + QString askForSaveFilePath(const QString& suggestedName) { const auto app = KiwixApp::instance(); @@ -38,32 +44,45 @@ KProfile::KProfile(QObject *parent) : #endif } -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) -void KProfile::startDownload(QWebEngineDownloadItem* download) +namespace +{ + +void setDownloadFilePath(WebEngineDownloadType* download, QString filePath) +{ +#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) + // WebEngineDownloadType is QWebEngineDownloadItem + // and no QWebEngineDownloadItem::setDownloadFileName() yet + download->setPath(filePath); #else -void KProfile::startDownload(QWebEngineDownloadRequest* download) + // Same API for QWebEngineDownloadItem and QWebEngineDownloadRequest + download->setDownloadFileName(filePath); #endif +} + +QString getDownloadFilePath(WebEngineDownloadType* download) { -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) - QString defaultFileName = QUrl(download->path()).fileName(); +#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) + // WebEngineDownloadType is QWebEngineDownloadItem + // and no QWebEngineDownloadItem::downloadFileName() yet + return QUrl(download->path()).fileName(); #else - QString defaultFileName = download->downloadFileName(); + // Same API for QWebEngineDownloadItem and QWebEngineDownloadRequest + return download->downloadFileName(); #endif +} + +} // unnamed namespace + +void KProfile::startDownload(WebEngineDownloadType* download) +{ + const QString defaultFileName = getDownloadFilePath(download); const QString fileName = askForSaveFilePath(defaultFileName); if (fileName.isEmpty()) { return; } -#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::isFinishedChanged, this, &KProfile::downloadFinished); -#endif + setDownloadFilePath(download, fileName); + connect(download, &DownloadFinishedSignal, this, &KProfile::downloadFinished); download->accept(); } diff --git a/src/kprofile.h b/src/kprofile.h index 98e75a90a..bbe78fa7b 100644 --- a/src/kprofile.h +++ b/src/kprofile.h @@ -5,8 +5,10 @@ #include #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #include +typedef QWebEngineDownloadItem WebEngineDownloadType; #else #include +typedef QWebEngineDownloadRequest WebEngineDownloadType; #endif #include "urlschemehandler.h" @@ -22,12 +24,8 @@ class KProfile : public QWebEngineProfile 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(); }; From 33702e85865852fcb6b5003751424794980b1e1a Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 6 Nov 2024 12:56:47 +0400 Subject: [PATCH 5/6] Better "Download Finished" message box --- resources/i18n/en.json | 1 + resources/i18n/qqq.json | 1 + src/kprofile.cpp | 9 +++++---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/resources/i18n/en.json b/resources/i18n/en.json index b884f1293..c779de636 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -45,6 +45,7 @@ "donate-to-support-kiwix":"Donate to support Kiwix", "exit":"Exit", "save-file-as-window-title":"Save File as", + "download-finished": "Download Finished", "download-finished-message":"The document has been downloaded.", "file":"File", "edit":"Edit", diff --git a/resources/i18n/qqq.json b/resources/i18n/qqq.json index 2fdc8c7b8..e7fb7347c 100644 --- a/resources/i18n/qqq.json +++ b/resources/i18n/qqq.json @@ -53,6 +53,7 @@ "donate-to-support-kiwix": "Represents the action of donating to support the Kiwix Organization.", "exit": "Represents the action of exiting the desktop application", "save-file-as-window-title": "Title text of the window prompting user to save as a new file.", + "download-finished": "Title of the message box announcing the completion of a download", "download-finished-message": "Text displayed when the download action has been completed", "file": "{{Identical|File}}", "edit": "{{Identical|Edit}}", diff --git a/src/kprofile.cpp b/src/kprofile.cpp index 5859ffd31..90e3afe37 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -2,8 +2,8 @@ #include "kiwixapp.h" #include -#include #include +#include "kiwixmessagebox.h" #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #define DownloadFinishedSignal WebEngineDownloadType::finished @@ -88,9 +88,10 @@ void KProfile::startDownload(WebEngineDownloadType* download) void KProfile::downloadFinished() { - QMessageBox msgBox; - msgBox.setText(gt("download-finished-message")); - msgBox.exec(); + showInfoBox(gt("download-finished"), + gt("download-finished-message"), + KiwixApp::instance()->getMainWindow() + ); } void ExternalReqInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) From 7e04b42c210056f8b324d7be0de2fd3b09aab082 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 6 Nov 2024 13:12:40 +0400 Subject: [PATCH 6/6] Open/Save functionality for downloaded files --- resources/i18n/en.json | 6 +++++- resources/i18n/qqq.json | 6 +++++- src/kiwixmessagebox.cpp | 1 - src/kprofile.cpp | 46 ++++++++++++++++++++++++++++++++++++++++- src/kprofile.h | 6 ++++-- 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/resources/i18n/en.json b/resources/i18n/en.json index c779de636..48663c585 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", @@ -174,5 +176,7 @@ "scroll-next-tab": "Scroll to next tab", "scroll-previous-tab": "Scroll to previous tab", "kiwix-search": "Kiwix search", - "search-options": "Search Options" + "search-options": "Search Options", + "save-or-open": "Save or Open file", + "save-or-open-text": "What should Kiwix do with this file?" } diff --git a/resources/i18n/qqq.json b/resources/i18n/qqq.json index e7fb7347c..8b7bbaed6 100644 --- a/resources/i18n/qqq.json +++ b/resources/i18n/qqq.json @@ -17,6 +17,7 @@ "error-downloader-launch-message": "Error text displayed to elaborate on the effect of the downloader failure.", "error-launch-server-message": "Error text displayed when Kiwix server failed to launch.", "error-archive": "Error text displayed when the retrieving the archive of a ZIM file failed.", + "error-opening-file": "Error text displayed when opening a downloaded file with an external application fails.", "open-zim": "Caption text for file explorer window used to choose a ZIM file.", "local-kiwix-server": "Describes a server hosted locally that contains the local kiwix library.", "random-article": "A randomly chosen article in a ZIM file.", @@ -31,6 +32,7 @@ "reopen-closed-tab": "Represents the action of reopen a tab that has just been closed.", "browse-library": "Represents the action of browsing the local kiwix library.", "open-file": "Represents the action of opening a file from the local file system.", + "save-file": "Represents the action of saving a file to the local file system.", "open-recent": "Represents the action of choosing to open a file from those recently opened.", "search-article": "Represents the action of Searching for a article in the current ZIM file.", "search-in-library": "Represents the action of Searching for a text in the local Kiwix library.", @@ -182,5 +184,7 @@ "scroll-next-tab": "Represents the action of scrolling to the next tab of the current tab which toward the end of the tab bar.", "scroll-previous-tab": "Represents the action of scrolling to the previous tab of the current tab which toward the start of the tab bar.", "kiwix-search": "Title text for a list of search results, which notes to the user those are from Kiwix's Search Engine", - "search-options": "The term for a collection of additional search filtering options to help users narrow down search results." + "search-options": "The term for a collection of additional search filtering options to help users narrow down search results.", + "save-or-open": "Title of the message box allowing to choose whether a remote resource should be saved to disk or opened with a respective application", + "save-or-open-text": "Text of the message box allowing to choose whether a remote resource should be saved to disk or opened with a respective application" } diff --git a/src/kiwixmessagebox.cpp b/src/kiwixmessagebox.cpp index 09944cc95..2de9c8928 100644 --- a/src/kiwixmessagebox.cpp +++ b/src/kiwixmessagebox.cpp @@ -64,4 +64,3 @@ KiwixMessageBox::Result showKiwixMessageBox(QString title, QString text, QWidget }); return dialog->execDialog(); } - diff --git a/src/kprofile.cpp b/src/kprofile.cpp index 90e3afe37..6146ab913 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -3,6 +3,8 @@ #include "kiwixapp.h" #include #include +#include +#include #include "kiwixmessagebox.h" #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) @@ -73,11 +75,32 @@ QString getDownloadFilePath(WebEngineDownloadType* download) } // unnamed namespace -void KProfile::startDownload(WebEngineDownloadType* download) +void KProfile::openFile(WebEngineDownloadType* download) +{ + const QString defaultFileName = getDownloadFilePath(download); + 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, &DownloadFinishedSignal, [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) { const QString defaultFileName = getDownloadFilePath(download); const QString fileName = askForSaveFilePath(defaultFileName); if (fileName.isEmpty()) { + download->cancel(); return; } @@ -94,6 +117,27 @@ void KProfile::downloadFinished() ); } +void KProfile::startDownload(WebEngineDownloadType* download) +{ + const 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) { const QString reqUrl = info.requestUrl().toString(); diff --git a/src/kprofile.h b/src/kprofile.h index bbe78fa7b..42a9d6273 100644 --- a/src/kprofile.h +++ b/src/kprofile.h @@ -23,10 +23,12 @@ class KProfile : public QWebEngineProfile UrlSchemeHandler m_schemeHandler; signals: -public slots: - void startDownload(WebEngineDownloadType*); +private slots: + void startDownload(WebEngineDownloadType*); void downloadFinished(); + void saveFile(WebEngineDownloadType*); + void openFile(WebEngineDownloadType*); }; /**