diff options
Diffstat (limited to 'src/core/download_manager_delegate_qt.cpp')
-rw-r--r-- | src/core/download_manager_delegate_qt.cpp | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp index 857af2e18..2cbfd121b 100644 --- a/src/core/download_manager_delegate_qt.cpp +++ b/src/core/download_manager_delegate_qt.cpp @@ -56,6 +56,7 @@ #include "browser_context_adapter_client.h" #include "browser_context_qt.h" #include "type_conversion.h" +#include "web_contents_delegate_qt.h" #include "qtwebenginecoreglobal.h" namespace QtWebEngineCore { @@ -74,6 +75,7 @@ DownloadManagerDelegateQt::DownloadManagerDelegateQt(BrowserContextAdapter *cont : m_contextAdapter(contextAdapter) , m_currentId(0) , m_weakPtrFactory(this) + , m_downloadType(BrowserContextAdapterClient::Attachment) { Q_ASSERT(m_contextAdapter); } @@ -115,6 +117,11 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(content::DownloadItem* i QString suggestedFilename = toQt(item->GetSuggestedFilename()); QString mimeTypeString = toQt(item->GetMimeType()); + bool isAttachment = net::HttpContentDisposition(item->GetContentDisposition(), std::string()).is_attachment(); + + if (!isAttachment || !BrowserContextAdapterClient::UserRequested) + m_downloadType = BrowserContextAdapterClient::DownloadAttribute; + if (suggestedFilename.isEmpty()) suggestedFilename = toQt(net::HttpContentDisposition(item->GetContentDisposition(), std::string()).filename()); @@ -157,7 +164,8 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(content::DownloadItem* i mimeTypeString, suggestedFilePath, BrowserContextAdapterClient::UnknownSavePageFormat, - false /* accepted */ + false /* accepted */, + m_downloadType }; Q_FOREACH (BrowserContextAdapterClient *client, clients) { @@ -211,12 +219,30 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content if (clients.isEmpty()) return; - const QString suggestedFileName - = QFileInfo(toQt(suggested_path.AsUTF8Unsafe())).completeBaseName() - + QStringLiteral(".mhtml"); - const QDir defaultDownloadDirectory - = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); - const QString suggestedFilePath = defaultDownloadDirectory.absoluteFilePath(suggestedFileName); + WebContentsDelegateQt *contentsDelegate = static_cast<WebContentsDelegateQt *>( + web_contents->GetDelegate()); + const SavePageInfo &spi = contentsDelegate->savePageInfo(); + + bool acceptedByDefault = false; + QString suggestedFilePath = spi.requestedFilePath; + if (suggestedFilePath.isEmpty()) { + suggestedFilePath = QFileInfo(toQt(suggested_path.AsUTF8Unsafe())).completeBaseName() + + QStringLiteral(".mhtml"); + } else { + acceptedByDefault = true; + } + if (QFileInfo(suggestedFilePath).isRelative()) { + const QDir downloadDir(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)); + suggestedFilePath = downloadDir.absoluteFilePath(suggestedFilePath); + } + + BrowserContextAdapterClient::SavePageFormat suggestedSaveFormat + = static_cast<BrowserContextAdapterClient::SavePageFormat>(spi.requestedFormat); + if (suggestedSaveFormat == BrowserContextAdapterClient::UnknownSavePageFormat) + suggestedSaveFormat = BrowserContextAdapterClient::MimeHtmlSaveFormat; + + // Clear the delegate's SavePageInfo. It's only valid for the page currently being saved. + contentsDelegate->setSavePageInfo(SavePageInfo()); BrowserContextAdapterClient::DownloadItemInfo info = { m_currentId + 1, @@ -226,8 +252,9 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content 0, /* receivedBytes */ QStringLiteral("application/x-mimearchive"), suggestedFilePath, - BrowserContextAdapterClient::MimeHtmlSaveFormat, - false /* accepted */ + suggestedSaveFormat, + acceptedByDefault, + BrowserContextAdapterClient::SavePage }; Q_FOREACH (BrowserContextAdapterClient *client, clients) { @@ -263,7 +290,8 @@ void DownloadManagerDelegateQt::OnDownloadUpdated(content::DownloadItem *downloa toQt(download->GetMimeType()), QString(), BrowserContextAdapterClient::UnknownSavePageFormat, - true /* accepted */ + true /* accepted */, + m_downloadType }; Q_FOREACH (BrowserContextAdapterClient *client, clients) { |