From 02463cc3cae029df5cb448884132a4249d8b9100 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Sat, 26 Oct 2024 01:55:13 -0400 Subject: [PATCH 1/4] Introduce getZimItem in WebView.cpp Refactor the way to get item from url. --- src/webview.cpp | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/webview.cpp b/src/webview.cpp index de42da42..87257836 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -138,17 +138,33 @@ QMenu* WebView::getHistoryForwardMenu() const return ret; } +namespace +{ + +/** + * @brief Get the Zim Item object corresponding to the given url. + * + * @param url QUrl + * @return zim::Item + * + * @exception throws exception if zimId is invalid, archive doesn't exist, + * entry is invalid or not found, or entry is redirect. + */ +zim::Item getZimItem(const QUrl& url) +{ + const auto app = KiwixApp::instance(); + const auto library = app->getLibrary(); + const auto archive = library->getArchive(getZimIdFromUrl(url)); + const auto entry = getArchiveEntryFromUrl(*archive, url); + return entry.getItem(true); +} + +} + void WebView::saveViewContent() { try { - auto app = KiwixApp::instance(); - auto library = app->getLibrary(); - auto archive = library->getArchive(m_currentZimId); - auto entry = getArchiveEntryFromUrl(*archive, this->url()); - if (entry.isRedirect()) - return; - - auto item = entry.getItem(true); + const auto item = getZimItem(url()); auto mimeType = QByteArray::fromStdString(item.getMimetype()); mimeType = mimeType.split(';')[0]; From 018056ca22513ebcf07415cb48dc70943ddf44b2 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Mon, 28 Oct 2024 14:27:14 -0400 Subject: [PATCH 2/4] Introduce isHTMLContent in webview.cpp Refactor the way to determine Zim MimeType. --- src/webview.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/webview.cpp b/src/webview.cpp index 87257836..17d99f42 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -159,20 +159,25 @@ zim::Item getZimItem(const QUrl& url) return entry.getItem(true); } +bool isHTMLContent(const zim::Item& item) +{ + auto mimeType = QByteArray::fromStdString(item.getMimetype()); + mimeType = mimeType.split(';')[0]; + return mimeType == "text/html"; +} + } void WebView::saveViewContent() { try { const auto item = getZimItem(url()); - auto mimeType = QByteArray::fromStdString(item.getMimetype()); - mimeType = mimeType.split(';')[0]; /* We have to sanitize here, as parsing will start once we pass the file name to either save or download method. */ QString suggestedFileName = QString::fromStdString(kiwix::getSlugifiedFileName(item.getTitle())); - if (mimeType == "text/html") + if (isHTMLContent(item)) page()->save(suggestedFileName + ".pdf"); else page()->download(this->url(), suggestedFileName); From 901aac87c760cfde1dd8299f9dae78894910b790 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Tue, 29 Oct 2024 10:59:31 -0400 Subject: [PATCH 3/4] Introduce askForSaveFilePath in kprofile.cpp Refactor file name retrieval. --- src/kprofile.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/kprofile.cpp b/src/kprofile.cpp index d1297c20..77b4ba72 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -5,6 +5,26 @@ #include #include +QString askForSaveFilePath(const QString& suggestedName) +{ + 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); + + if (fileName.isEmpty()) + return QString(); + + if (!fileName.endsWith(extension)) { + fileName.append(extension); + } + app->savePrevSaveDir(QFileInfo(fileName).absolutePath()); + return fileName; +} + KProfile::KProfile(QObject *parent) : QWebEngineProfile(parent) { @@ -24,27 +44,15 @@ void KProfile::startDownload(QWebEngineDownloadItem* download) void KProfile::startDownload(QWebEngineDownloadRequest* download) #endif { - auto app = KiwixApp::instance(); #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) QString defaultFileName = QUrl(download->path()).fileName(); #else QString defaultFileName = download->downloadFileName(); #endif - QString suggestedPath = app->getPrevSaveDir() + "/" + defaultFileName; - QString extension = defaultFileName.section('.', -1); - QString filter = extension != '.' ? "(*" + extension + ")" : ""; - - QString fileName = QFileDialog::getSaveFileName( - app->getMainWindow(), gt("save-file-as-window-title"), - suggestedPath, filter); - + const QString fileName = askForSaveFilePath(defaultFileName); if (fileName.isEmpty()) { return; } - if (!fileName.endsWith(extension)) { - fileName.append(extension); - } - app->savePrevSaveDir(QFileInfo(fileName).absolutePath()); if (download->isSavePageDownload()) { download->page()->printToPdf(fileName); From 3ebaaaa49ce5642436446551b69e3c514e859d61 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Tue, 29 Oct 2024 22:38:52 -0400 Subject: [PATCH 4/4] Move Save HTML Page to WebView::saveViewContent Due to Qt bug, WebPage::save() cannot be cancelled by the download object in KProfile::startDownload. Mimetype checking and pdf printing moved to be processed independently. --- src/kprofile.cpp | 4 ---- src/webview.cpp | 9 +++++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/kprofile.cpp b/src/kprofile.cpp index 77b4ba72..6659ebbd 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -54,10 +54,6 @@ void KProfile::startDownload(QWebEngineDownloadRequest* download) return; } - if (download->isSavePageDownload()) { - download->page()->printToPdf(fileName); - return; - } #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) download->setPath(fileName); #else diff --git a/src/webview.cpp b/src/webview.cpp index 17d99f42..2004de7d 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -18,6 +18,7 @@ class QMenu; #include zim::Entry getArchiveEntryFromUrl(const zim::Archive& archive, const QUrl& url); +QString askForSaveFilePath(const QString& suggestedName); void WebViewBackMenu::showEvent(QShowEvent *) { @@ -176,9 +177,13 @@ void WebView::saveViewContent() /* We have to sanitize here, as parsing will start once we pass the file name to either save or download method. */ - QString suggestedFileName = QString::fromStdString(kiwix::getSlugifiedFileName(item.getTitle())); + const QString suggestedFileName = QString::fromStdString(kiwix::getSlugifiedFileName(item.getTitle())); if (isHTMLContent(item)) - page()->save(suggestedFileName + ".pdf"); + { + const QString fileName = askForSaveFilePath(suggestedFileName + ".pdf"); + if (!fileName.isEmpty()) + page()->printToPdf(fileName); + } else page()->download(this->url(), suggestedFileName); }