From 0f20780db00950f48899b698d05a33c45f3e1d14 Mon Sep 17 00:00:00 2001
From: Florian Bruhin <git@the-compiler.org>
Date: Tue, 27 Jun 2017 16:56:59 +0200
Subject: [PATCH] Add method for triggering downloads

The method download(const QUrl &url, const QString &suggestedFileName)
already exists in WebContentsAdapter. It is now accessible through a
corresponding method in QWebEnginePage.

[ChangeLog][QtWebEngineWidgets][QWebEnginePage] New QWebEnginePage::download()
method to start custom downloads for a web page.

Task-number: QTBUG-55944
Change-Id: I185ec69c1668b377a129bc9fc09963278e52de1e
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
---
 src/webenginewidgets/api/qwebenginepage.cpp   | 19 +++++++++++++++++++
 src/webenginewidgets/api/qwebenginepage.h     |  1 +
 .../api/qwebengineprofile.cpp                 |  2 +-
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp
index 5b7fa9df2..34728834e 100644
--- a/src/webenginewidgets/api/qwebenginepage.cpp
+++ b/src/webenginewidgets/api/qwebenginepage.cpp
@@ -1810,6 +1810,25 @@ WebEngineSettings *QWebEnginePagePrivate::webEngineSettings() const
     return settings->d_func();
 }
 
+/*!
+    \since 5.10
+    Downloads the resource from the location given by \a url to a local file.
+
+    If \a filename is given, it is used as the suggested file name.
+    If it is relative, the file is saved in the standard download location with
+    the given name.
+    If it is a null or empty QString, the default file name is used.
+
+    This will emit QWebEngineProfile::downloadRequested() after the download
+    has started.
+*/
+
+void QWebEnginePage::download(const QUrl& url, const QString& filename)
+{
+    Q_D(QWebEnginePage);
+    d->adapter->download(url, filename);
+}
+
 void QWebEnginePage::load(const QUrl& url)
 {
     Q_D(QWebEnginePage);
diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h
index 292075827..295527e74 100644
--- a/src/webenginewidgets/api/qwebenginepage.h
+++ b/src/webenginewidgets/api/qwebenginepage.h
@@ -252,6 +252,7 @@ public:
 
     void load(const QUrl &url);
     void load(const QWebEngineHttpRequest &request);
+    void download(const QUrl &url, const QString &filename = QString());
     void setHtml(const QString &html, const QUrl &baseUrl = QUrl());
     void setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl());
 
diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp
index 73998030c..f3608471c 100644
--- a/src/webenginewidgets/api/qwebengineprofile.cpp
+++ b/src/webenginewidgets/api/qwebengineprofile.cpp
@@ -142,7 +142,7 @@ using QtWebEngineCore::BrowserContextAdapter;
   will be deleted immediately after the signal emission.
   This signal cannot be used with a queued connection.
 
-  \sa QWebEngineDownloadItem
+  \sa QWebEngineDownloadItem, QWebEnginePage::download()
 */
 
 QWebEngineProfilePrivate::QWebEngineProfilePrivate(QSharedPointer<BrowserContextAdapter> browserContext)
-- 
GitLab