summaryrefslogtreecommitdiffstats
path: root/src/core/download_manager_delegate_qt.cpp
diff options
context:
space:
mode:
authorJoerg Bornemann <joerg.bornemann@theqtcompany.com>2015-11-16 17:51:01 +0100
committerSimon Hausmann <simon.hausmann@theqtcompany.com>2015-12-11 06:37:32 +0000
commit9d18f8d02b73c2c9310b772c972ea1e0d1356102 (patch)
tree7cc8c93950db0797916146620c6fb2e56ad0acc4 /src/core/download_manager_delegate_qt.cpp
parent222faea4af492f3286e9adca9c3d57a0f553a52a (diff)
Add web action for saving the current web page
Add the possibility to save web pages as single HTML file, complete HTML (with resource directory) or MHTML archive. Change-Id: Ic7e7cfda9432f3534c13350a6369d79bb17fd8b3 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Diffstat (limited to 'src/core/download_manager_delegate_qt.cpp')
-rw-r--r--src/core/download_manager_delegate_qt.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp
index e9af98fd8..b6de27ca8 100644
--- a/src/core/download_manager_delegate_qt.cpp
+++ b/src/core/download_manager_delegate_qt.cpp
@@ -61,9 +61,15 @@ ASSERT_ENUMS_MATCH(content::DownloadItem::COMPLETE, BrowserContextAdapterClient:
ASSERT_ENUMS_MATCH(content::DownloadItem::CANCELLED, BrowserContextAdapterClient::DownloadCancelled)
ASSERT_ENUMS_MATCH(content::DownloadItem::INTERRUPTED, BrowserContextAdapterClient::DownloadInterrupted)
+ASSERT_ENUMS_MATCH(content::SAVE_PAGE_TYPE_UNKNOWN, BrowserContextAdapterClient::UnknownSavePageFormat)
+ASSERT_ENUMS_MATCH(content::SAVE_PAGE_TYPE_AS_ONLY_HTML, BrowserContextAdapterClient::SingleHtmlSaveFormat)
+ASSERT_ENUMS_MATCH(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, BrowserContextAdapterClient::CompleteHtmlSaveFormat)
+ASSERT_ENUMS_MATCH(content::SAVE_PAGE_TYPE_AS_MHTML, BrowserContextAdapterClient::MimeHtmlSaveFormat)
+
DownloadManagerDelegateQt::DownloadManagerDelegateQt(BrowserContextAdapter *contextAdapter)
: m_contextAdapter(contextAdapter)
, m_currentId(0)
+ , m_weakPtrFactory(this)
{
Q_ASSERT(m_contextAdapter);
}
@@ -140,6 +146,7 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(content::DownloadItem* i
item->GetTotalBytes(),
item->GetReceivedBytes(),
suggestedFilePath,
+ BrowserContextAdapterClient::UnknownSavePageFormat,
false /* accepted */
};
@@ -181,6 +188,57 @@ void DownloadManagerDelegateQt::GetSaveDir(content::BrowserContext* browser_cont
*skip_dir_check = true;
}
+void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_contents,
+ const base::FilePath &suggested_path,
+ const base::FilePath::StringType &default_extension,
+ bool can_save_as_complete,
+ const content::SavePackagePathPickedCallback &callback)
+{
+ Q_UNUSED(default_extension);
+ Q_UNUSED(can_save_as_complete);
+
+ QList<BrowserContextAdapterClient*> clients = m_contextAdapter->clients();
+ 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);
+
+ BrowserContextAdapterClient::DownloadItemInfo info = {
+ m_currentId + 1,
+ toQt(web_contents->GetURL()),
+ content::DownloadItem::IN_PROGRESS,
+ 0, /* totalBytes */
+ 0, /* receivedBytes */
+ suggestedFilePath,
+ BrowserContextAdapterClient::MimeHtmlSaveFormat,
+ false /* accepted */
+ };
+
+ Q_FOREACH (BrowserContextAdapterClient *client, clients) {
+ client->downloadRequested(info);
+ if (info.accepted)
+ break;
+ }
+
+ if (!info.accepted)
+ return;
+
+ callback.Run(toFilePath(info.path), static_cast<content::SavePageType>(info.savePageFormat),
+ base::Bind(&DownloadManagerDelegateQt::savePackageDownloadCreated,
+ m_weakPtrFactory.GetWeakPtr()));
+}
+
+void DownloadManagerDelegateQt::savePackageDownloadCreated(content::DownloadItem *item)
+{
+ OnDownloadUpdated(item);
+ item->AddObserver(this);
+}
+
void DownloadManagerDelegateQt::OnDownloadUpdated(content::DownloadItem *download)
{
QList<BrowserContextAdapterClient*> clients = m_contextAdapter->clients();
@@ -192,6 +250,7 @@ void DownloadManagerDelegateQt::OnDownloadUpdated(content::DownloadItem *downloa
download->GetTotalBytes(),
download->GetReceivedBytes(),
QString(),
+ BrowserContextAdapterClient::UnknownSavePageFormat,
true /* accepted */
};