diff options
author | Joerg Bornemann <joerg.bornemann@theqtcompany.com> | 2015-07-03 16:09:46 +0200 |
---|---|---|
committer | Joerg Bornemann <joerg.bornemann@theqtcompany.com> | 2015-07-06 16:53:16 +0000 |
commit | 934d63d395684626cdfc2fc1170d7424955e7cd7 (patch) | |
tree | d31438d66725ce121f67030ce4999c1ea52d3041 /src | |
parent | 9b70ebd22d58a89ca605f1f7ea602148b183055f (diff) |
add Download*ToDisk web actions
Add the web actions DownloadLinkToDisk, DownloadImageToDisk and
DownloadMediaToDisk.
Change-Id: If6c9c8a3f4b95ad3032cff71ffc53a48be26e083
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/core/web_contents_adapter.cpp | 14 | ||||
-rw-r--r-- | src/core/web_contents_adapter.h | 1 | ||||
-rw-r--r-- | src/core/web_contents_adapter_client.h | 1 | ||||
-rw-r--r-- | src/core/web_contents_view_qt.cpp | 1 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebenginepage.cpp | 21 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebenginepage.h | 3 | ||||
-rw-r--r-- | src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc | 3 |
7 files changed, 44 insertions, 0 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index bf4f027ce..4a4e7198e 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -58,6 +58,7 @@ #include "content/browser/renderer_host/render_view_host_impl.h" #include "content/browser/web_contents/web_contents_impl.h" #include "content/public/browser/child_process_security_policy.h" +#include <content/public/browser/download_manager.h> #include "content/public/browser/host_zoom_map.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/render_view_host.h" @@ -756,6 +757,19 @@ void WebContentsAdapter::updateWebPreferences(const content::WebPreferences & we d->webContents->GetRenderViewHost()->UpdateWebkitPreferences(webPreferences); } +void WebContentsAdapter::download(const QUrl &url, const QString &suggestedFileName) +{ + content::BrowserContext *bctx = webContents()->GetBrowserContext(); + content::DownloadManager *dlm = content::BrowserContext::GetDownloadManager(bctx); + if (!dlm) + return; + + scoped_ptr<content::DownloadUrlParameters> params( + content::DownloadUrlParameters::FromWebContents(webContents(), toGurl(url))); + params->set_suggested_name(toString16(suggestedFileName)); + dlm->DownloadUrl(params.Pass()); +} + void WebContentsAdapter::copyImageAt(const QPoint &location) { Q_D(WebContentsAdapter); diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h index 2e65b86c1..db8449478 100644 --- a/src/core/web_contents_adapter.h +++ b/src/core/web_contents_adapter.h @@ -111,6 +111,7 @@ public: quint64 findText(const QString &subString, bool caseSensitively, bool findBackward); void stopFinding(); void updateWebPreferences(const content::WebPreferences &webPreferences); + void download(const QUrl &url, const QString &suggestedFileName); // Must match blink::WebMediaPlayerAction::Type. enum MediaPlayerAction { diff --git a/src/core/web_contents_adapter_client.h b/src/core/web_contents_adapter_client.h index 05a31c0cd..9bc7698a5 100644 --- a/src/core/web_contents_adapter_client.h +++ b/src/core/web_contents_adapter_client.h @@ -114,6 +114,7 @@ public: MediaType mediaType; bool hasImageContent; uint mediaFlags; + QString suggestedFileName; // Some likely candidates for future additions as we add support for the related actions: // bool isImageBlocked; // bool isEditable; diff --git a/src/core/web_contents_view_qt.cpp b/src/core/web_contents_view_qt.cpp index 340d7b33e..004800d5a 100644 --- a/src/core/web_contents_view_qt.cpp +++ b/src/core/web_contents_view_qt.cpp @@ -142,6 +142,7 @@ static WebEngineContextMenuData fromParams(const content::ContextMenuParams &par ret.mediaType = (WebEngineContextMenuData::MediaType)params.media_type; ret.hasImageContent = params.has_image_contents; ret.mediaFlags = params.media_flags; + ret.suggestedFileName = toQt(params.suggested_filename.data()); return ret; } diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp index ad007dc87..1de94bc6b 100644 --- a/src/webenginewidgets/api/qwebenginepage.cpp +++ b/src/webenginewidgets/api/qwebenginepage.cpp @@ -528,12 +528,18 @@ QAction *QWebEnginePage::action(WebAction action) const case CopyLinkToClipboard: text = tr("Copy Link URL"); break; + case DownloadLinkToDisk: + text = tr("Save Link..."); + break; case CopyImageToClipboard: text = tr("Copy Image"); break; case CopyImageUrlToClipboard: text = tr("Copy Image URL"); break; + case DownloadImageToDisk: + text = tr("Save Image"); + break; case CopyMediaUrlToClipboard: text = tr("Copy Media URL"); break; @@ -549,6 +555,9 @@ QAction *QWebEnginePage::action(WebAction action) const case ToggleMediaMute: text = tr("Toggle Mute"); break; + case DownloadMediaToDisk: + text = tr("Download Media"); + break; default: break; } @@ -636,6 +645,10 @@ void QWebEnginePage::triggerAction(WebAction action, bool) qApp->clipboard()->setMimeData(data); } break; + case DownloadLinkToDisk: + if (d->m_menuData.linkUrl.isValid()) + d->adapter->download(d->m_menuData.linkUrl, d->m_menuData.suggestedFileName); + break; case CopyImageToClipboard: if (d->m_menuData.hasImageContent && (d->m_menuData.mediaType == WebEngineContextMenuData::MediaTypeImage || @@ -658,6 +671,11 @@ void QWebEnginePage::triggerAction(WebAction action, bool) qApp->clipboard()->setMimeData(data); } break; + case DownloadImageToDisk: + case DownloadMediaToDisk: + if (d->m_menuData.mediaUrl.isValid()) + d->adapter->download(d->m_menuData.mediaUrl, d->m_menuData.suggestedFileName); + break; case CopyMediaUrlToClipboard: if (d->m_menuData.mediaUrl.isValid() && (d->m_menuData.mediaType == WebEngineContextMenuData::MediaTypeAudio || @@ -848,6 +866,7 @@ QMenu *QWebEnginePage::createStandardContextMenu() action = QWebEnginePage::action(OpenLinkInThisWindow); action->setText(tr("Follow Link")); menu->addAction(action); + menu->addAction(QWebEnginePage::action(DownloadLinkToDisk)); } if (contextMenuData.selectedText.isEmpty()) { action = new QAction(QIcon::fromTheme(QStringLiteral("go-previous")), tr("&Back"), menu); @@ -873,6 +892,7 @@ QMenu *QWebEnginePage::createStandardContextMenu() if (contextMenuData.mediaUrl.isValid()) { switch (contextMenuData.mediaType) { case WebEngineContextMenuData::MediaTypeImage: + menu->addAction(QWebEnginePage::action(DownloadImageToDisk)); menu->addAction(QWebEnginePage::action(CopyImageUrlToClipboard)); // no break case WebEngineContextMenuData::MediaTypeCanvas: @@ -880,6 +900,7 @@ QMenu *QWebEnginePage::createStandardContextMenu() break; case WebEngineContextMenuData::MediaTypeAudio: case WebEngineContextMenuData::MediaTypeVideo: + menu->addAction(QWebEnginePage::action(DownloadMediaToDisk)); menu->addAction(QWebEnginePage::action(CopyMediaUrlToClipboard)); menu->addAction(QWebEnginePage::action(ToggleMediaPlayPause)); menu->addAction(QWebEnginePage::action(ToggleMediaLoop)); diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h index 16b33a196..f4d0cd18d 100644 --- a/src/webenginewidgets/api/qwebenginepage.h +++ b/src/webenginewidgets/api/qwebenginepage.h @@ -92,15 +92,18 @@ public: OpenLinkInNewWindow, OpenLinkInNewTab, CopyLinkToClipboard, + DownloadLinkToDisk, CopyImageToClipboard, CopyImageUrlToClipboard, + DownloadImageToDisk, CopyMediaUrlToClipboard, ToggleMediaControls, ToggleMediaLoop, ToggleMediaPlayPause, ToggleMediaMute, + DownloadMediaToDisk, WebActionCount }; diff --git a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc index 3c47d97df..29e5d60ce 100644 --- a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc +++ b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc @@ -110,6 +110,9 @@ \value ToggleMediaLoop Toggles whether the hovered audio or video should loop on completetion or not. (Added in Qt 5.6) \value ToggleMediaPlayPause Toggles the play/pause state of the hovered audio or video element. (Added in Qt 5.6) \value ToggleMediaMute Mutes or unmutes the hovered audio or video element. (Added in Qt 5.6) + \value DownloadLinkToDisk Download the current link to the disk. (Added in Qt 5.6) + \value DownloadImageToDisk Download the highlighted image to the disk. (Added in Qt 5.6) + \value DownloadMediaToDisk Download the hovered audio or video to the disk. (Added in Qt 5.6) \omitvalue WebActionCount |