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
veloman-yunkan committed Nov 6, 2024
1 parent 33702e8 commit 7e04b42
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 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 @@ -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?"
}
6 changes: 5 additions & 1 deletion resources/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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.",
Expand Down Expand Up @@ -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"
}
1 change: 0 additions & 1 deletion src/kiwixmessagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,3 @@ KiwixMessageBox::Result showKiwixMessageBox(QString title, QString text, QWidget
});
return dialog->execDialog();
}

46 changes: 45 additions & 1 deletion src/kprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "kiwixapp.h"
#include <QFileDialog>
#include <QWebEngineSettings>
#include <QDesktopServices>
#include <QTemporaryFile>
#include "kiwixmessagebox.h"

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/kprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
};

/**
Expand Down

0 comments on commit 7e04b42

Please sign in to comment.