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 4, 2024
1 parent a35f526 commit a9eaa9d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 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-with": "There was an error in opening the file using the system handler.",
"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();
}

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

KProfile::KProfile(QObject *parent) :
QWebEngineProfile(parent)
Expand All @@ -19,15 +25,43 @@ KProfile::KProfile(QObject *parent) :
}

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void KProfile::startDownload(QWebEngineDownloadItem* download)
void KProfile::openFile(QWebEngineDownloadItem* download)
#else
void KProfile::startDownload(QWebEngineDownloadRequest* download)
void KProfile::openFile(QWebEngineDownloadRequest* download)
#endif
{
QString defaultFileName = download->url().fileName();
QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(),
gt("save-file-as-window-title"), defaultFileName);
QTemporaryFile *tempFile = new QTemporaryFile(QDir::tempPath() + "/XXXXXX." + QFileInfo(defaultFileName).suffix());
if (tempFile->open()) {
QString tempFilePath = tempFile->fileName();
tempFile->close();
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
download->setPath(tempFilePath);
#else
download->setDownloadFileName(tempFilePath);
#endif
connect(download, &QWebEngineDownloadItem::finished, [tempFilePath]() {
if(!QDesktopServices::openUrl(QUrl::fromLocalFile(tempFilePath)))
showInfoBox(gt("error-title"), gt("error-opening-file-with"), KiwixApp::instance()->getMainWindow());
});
download->accept();
} else {
qDebug()<<"tmp file err";
delete tempFile;
download->cancel();
}
}

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void KProfile::saveFile(QWebEngineDownloadItem* download)
#else
void KProfile::saveFile(QWebEngineDownloadRequest* download)
#endif
{
QString defaultFileName = download->url().fileName();
QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(), "Save File", defaultFileName);
if (fileName.isEmpty()) {
download->cancel();
return;
}
QString extension = "." + download->url().url().section('.', -1);
Expand All @@ -38,20 +72,27 @@ void KProfile::startDownload(QWebEngineDownloadRequest* download)
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
download->accept();
}

void KProfile::downloadFinished()
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void KProfile::startDownload(QWebEngineDownloadItem* download)
#else
void KProfile::startDownload(QWebEngineDownloadRequest* download)
#endif
{
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
11 changes: 10 additions & 1 deletion src/kprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class KProfile : public QWebEngineProfile

private:
UrlSchemeHandler m_schemeHandler;
QWebEngineDownloadItem *currentDownloadItem;

signals:
public slots:
Expand All @@ -28,7 +29,15 @@ public slots:
#else
void startDownload(QWebEngineDownloadRequest*);
#endif
void downloadFinished();

private slots:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void saveFile(QWebEngineDownloadItem*);
void openFile(QWebEngineDownloadItem*);
#else
void saveFile(QWebEngineDownloadRequest*);
void openFile(QWebEngineDownloadRequest*);
#endif
};

/**
Expand Down

0 comments on commit a9eaa9d

Please sign in to comment.