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 14, 2024
1 parent f708fc5 commit c1943bd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
6 changes: 5 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 Down Expand Up @@ -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?"
}
1 change: 0 additions & 1 deletion src/kiwixmessagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

73 changes: 52 additions & 21 deletions src/kprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <QFileDialog>
#include <QMessageBox>
#include <QWebEngineSettings>
#include <QDesktopServices>
#include <QFileDialog>
#include <QMessageBox>
#include <QTemporaryFile>
#include "kiwixmessagebox.h"

KProfile::KProfile(QObject *parent) :
QWebEngineProfile(parent)
Expand All @@ -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)
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 c1943bd

Please sign in to comment.