Skip to content

Commit

Permalink
Open/Save functionality for downloaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
juuz0 committed Jun 16, 2024
1 parent 4a0451a commit 6d82235
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 31 deletions.
7 changes: 6 additions & 1 deletion resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -45,6 +47,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",
Expand Down Expand Up @@ -167,5 +170,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?"
}
1 change: 0 additions & 1 deletion src/kiwixmessagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,3 @@ KiwixMessageBox::Result showKiwixMessageBox(QString title, QString text, QWidget
dialog->setAttribute(Qt::WA_DeleteOnClose);
return dialog->execDialog();
}

81 changes: 59 additions & 22 deletions src/kprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

#include "kiwixapp.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QWebEngineSettings>
#include <QDesktopServices>
#include <QTemporaryFile>
#include "kiwixmessagebox.h"

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#define DownloadFinishedSignal WebEngineDownloadType::finished
#else
#define DownloadFinishedSignal WebEngineDownloadType::isFinishedChanged
#endif

KProfile::KProfile(QObject *parent) :
QWebEngineProfile(parent)
Expand All @@ -18,40 +26,69 @@ 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();
QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(),
gt("save-file-as-window-title"), defaultFileName);
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)
{
QString defaultFileName = download->url().fileName();
QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(), gt("save-file-as-window-title"), 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);
connect(download, &DownloadFinishedSignal, [=]() {
showInfoBox(gt("download-finished"), gt("download-finished-message"), KiwixApp::instance()->getMainWindow());
});
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)
Expand Down
16 changes: 9 additions & 7 deletions src/kprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
};

/**
Expand Down

0 comments on commit 6d82235

Please sign in to comment.