Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
"save-file-as-window-title":"Save File as",
"download-finished": "Download Finished",
"download-finished-message":"The document has been downloaded.",
"article-download-finished": "Article saved",
"article-download-finished-message":"The article \"{{TITLE}}\" has been saved at:\n{{PATH}}.",
"file":"File",
"edit":"Edit",
"view":"View",
Expand Down Expand Up @@ -111,7 +113,7 @@
"details":"Full article",
"yes":"yes",
"no":"no",
"ok":"ok",
"ok":"OK",
"no-filter":"no filter",
"open-link-in-web-browser":"Open link in web browser",
"copy-link":"Copy link",
Expand Down Expand Up @@ -172,7 +174,7 @@
"import-reading-list": "Import reading list",
"import-reading-list-error": "An error has occured during import of the reading list.",
"disable-sandbox": "Kiwix has been launched from a network drive. This is known to cause compatibility issues with the browsing sandboxing. As a result, the sandbox will be disabled. Do you want to continue?",
"save-page-as": "Save As...",
"save-page-as": "Save article",
"portable-disabled-tooltip": "Function disabled in portable mode",
"scroll-next-tab": "Scroll to next tab",
"scroll-previous-tab": "Scroll to previous tab",
Expand Down
2 changes: 2 additions & 0 deletions src/kiwixmessagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ KiwixMessageBox::KiwixMessageBox(QString confirmTitle, QString confirmText, bool
m_result = CloseClicked;
});
ui->confirmText->setText(confirmText);
ui->confirmText->setWordWrap(true);
ui->confirmText->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Minimum);
ui->confirmTitle->setText(confirmTitle);
ui->yesButton->setText(leftAction);
ui->noButton->setText(rightAction);
Expand Down
82 changes: 57 additions & 25 deletions src/kprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,61 @@ QWebEngineScript getScript(QString filename,
return script;
}

QString getExtensionFromFilter(const QString &filter) {
const QRegularExpression re("\\(\\*\\.(\\w+)\\)");
const QRegularExpressionMatch match = re.match(filter);

if (match.hasMatch()) {
return "." + match.captured(1);
}
return QString();
}

}

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

QString askForSaveFilePath(const QString& suggestedName)
QString askForSaveFilePath(const QString &suggestedName, const QString &filter) {
const auto app = KiwixApp::instance();
const QString suggestedPath = app->getPrevSaveDir() + "/" + suggestedName;
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(
app->getMainWindow(), gt("save-file-as-window-title"), suggestedPath,
filter,&selectedFilter);

if (fileName.isEmpty())
return QString();

const QString extension = getExtensionFromFilter(selectedFilter);
if (!fileName.endsWith(extension)) {
fileName.append(extension);
}
app->savePrevSaveDir(QFileInfo(fileName).absolutePath());
return fileName;
}

void downloadFinished()
{
const auto app = KiwixApp::instance();
const QString suggestedPath = app->getPrevSaveDir() + "/" + suggestedName;
const QString extension = suggestedName.section(".", -1);
const QString filter = extension.isEmpty() ? "" : "(*." + extension + ")";
QString fileName = QFileDialog::getSaveFileName(
app->getMainWindow(), gt("save-file-as-window-title"),
suggestedPath, filter);
showInfoBox(gt("download-finished"),
gt("download-finished-message"),
KiwixApp::instance()->getMainWindow()
);
}

if (fileName.isEmpty())
return QString();
void articleDownloadFinished(const QString &title,const QString &filePath)
{
QString finishedMessage = gt("article-download-finished-message");
finishedMessage = finishedMessage.replace("{{TITLE}}", title);
finishedMessage = finishedMessage.replace("{{PATH}}", filePath);

if (!fileName.endsWith(extension)) {
fileName.append(extension);
}
app->savePrevSaveDir(QFileInfo(fileName).absolutePath());
return fileName;
showInfoBox(gt("article-download-finished"), finishedMessage,
KiwixApp::instance()->getMainWindow());
}

KProfile::KProfile(QObject *parent) :
Expand Down Expand Up @@ -121,27 +150,30 @@ void KProfile::openFile(WebEngineDownloadType* download)
void KProfile::saveFile(WebEngineDownloadType* download)
{
const QString defaultFileName = getDownloadFilePath(download);
const QString fileName = askForSaveFilePath(defaultFileName);
const QString extension = defaultFileName.section(".", -1);
const QString filter = extension.isEmpty() ? "" : "(*." + extension + ")";
const QString fileName = askForSaveFilePath(defaultFileName, filter);
if (fileName.isEmpty()) {
download->cancel();
return;
}

setDownloadFilePath(download, fileName);
connect(download, &DownloadFinishedSignal, this, &KProfile::downloadFinished);
connect(download, &DownloadFinishedSignal, &downloadFinished);
download->accept();
}

void KProfile::downloadFinished()
{
showInfoBox(gt("download-finished"),
gt("download-finished-message"),
KiwixApp::instance()->getMainWindow()
);
}

void KProfile::startDownload(WebEngineDownloadType* download)
{
if (download->state() == DownloadInProgress) {
connect(download, &DownloadFinishedSignal, [download]() {
const QString fullPath = QDir(download->downloadDirectory())
.filePath(download->downloadFileName());
articleDownloadFinished(download->page()->title(), fullPath);
});
return;
}

const auto res = showKiwixMessageBox(
gt("save-or-open"),
gt("save-or-open-text"),
Expand Down
1 change: 0 additions & 1 deletion src/kprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class KProfile : public QWebEngineProfile

private slots:
void startDownload(WebEngineDownloadType*);
void downloadFinished();
void saveFile(WebEngineDownloadType*);
void openFile(WebEngineDownloadType*);
};
Expand Down
23 changes: 18 additions & 5 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class QMenu;
#include "tableofcontentbar.h"

zim::Entry getArchiveEntryFromUrl(const zim::Archive& archive, const QUrl& url);
QString askForSaveFilePath(const QString& suggestedName);
QString askForSaveFilePath(const QString& suggestedName , const QString& filters);
void articleDownloadFinished(const QString &title, const QString &filePath);

void WebViewBackMenu::showEvent(QShowEvent *)
{
Expand Down Expand Up @@ -88,6 +89,11 @@ WebView::WebView(QWidget *parent)
connect(this->page(), &QWebEnginePage::linkHovered, this, [=] (const QString& url) {
m_linkHovered = url;
});
connect(page(), &QWebEnginePage::pdfPrintingFinished,
[=](const QString &filePath,const bool success) {
if (success)
articleDownloadFinished(page()->title(), filePath);
});

/* In Qt 5.12, the zoom factor is not correctly passed after a fulltext search
* Bug Report: https://bugreports.qt.io/browse/QTBUG-51851
Expand Down Expand Up @@ -199,9 +205,17 @@ void WebView::saveViewContent()
const QString suggestedFileName = QString::fromStdString(kiwix::getSlugifiedFileName(item.getTitle()));
if (isHTMLContent(item))
{
const QString fileName = askForSaveFilePath(suggestedFileName + ".pdf");
if (!fileName.isEmpty())
page()->printToPdf(fileName);
const QString filters = QString("PDF Document (*.pdf)") + QString(";;") + QString("(Web Page, complete (*.html)");
const QString fileName = askForSaveFilePath(suggestedFileName + ".pdf", filters);

if (!fileName.isEmpty()) {
const QString extension = fileName.section(".", -1);
if (extension == "pdf") {
page()->printToPdf(fileName);
} else {
page()->save(fileName);
}
}
}
else
page()->download(this->url(), suggestedFileName);
Expand Down Expand Up @@ -344,7 +358,6 @@ QMenu* WebView::createStandardContextMenu() {
KiwixApp::instance()->getTabWidget()->triggerWebPageAction(QWebEnginePage::Forward);
});

menu->addAction(app->getAction(KiwixApp::SavePageAsAction));
return menu;
}

Expand Down