diff options
author | Tamas Zakor <ztamas@inf.u-szeged.hu> | 2019-05-28 13:54:28 +0200 |
---|---|---|
committer | Tamas Zakor <ztamas@inf.u-szeged.hu> | 2019-07-05 08:57:30 +0200 |
commit | 0884fab3b161b62c1e2bb1bac6bbd74560f06232 (patch) | |
tree | ec07c0b9473c5eb05880cd1783ca11dd5bc09aa0 /src/webenginewidgets/api | |
parent | fbc2700180d97d9c84b079b7c4da9eace19abf23 (diff) |
Add API to change download directory path and file name
Add functions and property to change the download directory and
file name in QWebEngineDownloadItem and QQuickWebEngineDownloadItem
and deprecate the path() and setPath().
Regenerating the uniquifying download filename after
change the download directory.
[ChangeLog][DownloadItem] Add functions and property
to change the download directory and file name in
QWebEngineDownloadItem and QQuickWebEngineDownloadItem
and deprecate the path() and setPath().
Task-number: QTBUG-56978
Change-Id: I6e63da82a187add8bc3206cc80c8bf6865fbdd35
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/webenginewidgets/api')
4 files changed, 101 insertions, 11 deletions
diff --git a/src/webenginewidgets/api/qwebenginedownloaditem.cpp b/src/webenginewidgets/api/qwebenginedownloaditem.cpp index 6df131066..7ce572e2f 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem.cpp +++ b/src/webenginewidgets/api/qwebenginedownloaditem.cpp @@ -43,6 +43,7 @@ #include "profile_adapter.h" #include "qwebengineprofile_p.h" +#include <QDir> #include "QFileInfo" QT_BEGIN_NAMESPACE @@ -508,6 +509,7 @@ QString QWebEngineDownloadItem::mimeType() const } /*! + \obsolete Returns the full target path where data is being downloaded to. The path includes the file name. The default suggested path is the standard download location @@ -517,7 +519,7 @@ QString QWebEngineDownloadItem::mimeType() const QString QWebEngineDownloadItem::path() const { Q_D(const QWebEngineDownloadItem); - return d->downloadPath; + return QDir::cleanPath(QDir(d->downloadDirectory).filePath(d->downloadFileName)); } /*! @@ -534,19 +536,99 @@ void QWebEngineDownloadItem::setPath(QString path) qWarning("Setting the download path is not allowed after the download has been accepted."); return; } + if (QDir(d->downloadDirectory).filePath(d->downloadFileName) != path) { + if (QFileInfo(path).fileName().isEmpty()) { + qWarning("The download path does not include file name."); + return; + } + + if (QFileInfo(path).isDir()) { + qWarning("The download path matches with an already existing directory path."); + return; + } + + if (QFileInfo(path).fileName() == path) { + d->downloadDirectory = QStringLiteral(""); + d->downloadFileName = path; + } else { + d->downloadDirectory = QFileInfo(path).filePath(); + d->downloadFileName = QFileInfo(path).fileName(); + } + } +} + +/*! + \since 5.14 + + Returns the download directory path. +*/ + +QString QWebEngineDownloadItem::downloadDirectory() const +{ + Q_D(const QWebEngineDownloadItem); + return d->downloadDirectory; +} - if (QFileInfo(path).fileName().isEmpty()) { - qWarning("The download path does not include file name."); +/*! + \since 5.14 + + Sets the directory path to download the file to. + + The download directory path can only be set in response to the QWebEngineProfile::downloadRequested() + signal before the download is accepted. Past that point, this function has no effect on the + download item's state. +*/ + +void QWebEngineDownloadItem::setDownloadDirectory(QString directory) +{ + Q_D(QWebEngineDownloadItem); + if (d->downloadState != QWebEngineDownloadItem::DownloadRequested) { + qWarning("Setting the download directory is not allowed after the download has been accepted."); return; - } + } + + if (!directory.isEmpty() && d->downloadDirectory != directory) + d->downloadDirectory = directory; - if (QFileInfo(path).isDir()) { - qWarning("The download path matches with an already existing directory path."); + d->downloadFileName = QFileInfo(d->profile->profileAdapter()->updateDownloadPath(d->downloadId, + d->downloadDirectory, + d->suggestedFileName)).fileName(); +} + +/*! + \since 5.14 + + Returns the suggested file name. +*/ + +QString QWebEngineDownloadItem::downloadFileName() const +{ + Q_D(const QWebEngineDownloadItem); + return d->downloadFileName; +} + +/*! + \since 5.14 + + Sets the file name to download the file to. + + The download file name can only be set in response to the QWebEngineProfile::downloadRequested() + signal before the download is accepted. Past that point, this function has no effect on the + download item's state. +*/ + +void QWebEngineDownloadItem::setDownloadFileName(QString fileName) +{ + Q_D(QWebEngineDownloadItem); + if (d->downloadState != QWebEngineDownloadItem::DownloadRequested) { + qWarning("Setting the download file name is not allowed after the download has been accepted."); return; } - d->downloadPath = path; + if (!fileName.isEmpty()) + d->downloadFileName = fileName; } + /*! \since 5.14 diff --git a/src/webenginewidgets/api/qwebenginedownloaditem.h b/src/webenginewidgets/api/qwebenginedownloaditem.h index 0ae6b2575..169d80553 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem.h +++ b/src/webenginewidgets/api/qwebenginedownloaditem.h @@ -118,8 +118,8 @@ public: qint64 receivedBytes() const; QUrl url() const; QString mimeType() const; - QString path() const; - void setPath(QString path); + QString Q_DECL_DEPRECATED path() const; + void Q_DECL_DEPRECATED setPath(QString path); bool isFinished() const; bool isPaused() const; SavePageFormat savePageFormat() const; @@ -129,6 +129,10 @@ public: QString interruptReasonString() const; bool isSavePageDownload() const; QString suggestedFileName() const; + QString downloadDirectory() const; + void setDownloadDirectory(QString directory); + QString downloadFileName() const; + void setDownloadFileName(QString fileName); QWebEnginePage *page() const; diff --git a/src/webenginewidgets/api/qwebenginedownloaditem_p.h b/src/webenginewidgets/api/qwebenginedownloaditem_p.h index ac5db7dcd..08e478736 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem_p.h +++ b/src/webenginewidgets/api/qwebenginedownloaditem_p.h @@ -79,6 +79,8 @@ public: QString mimeType; bool downloadPaused; QString suggestedFileName; + QString downloadDirectory; + QString downloadFileName; qint64 totalBytes; qint64 receivedBytes; diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index 6e7191355..6e2db533e 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -53,6 +53,7 @@ #include "visited_links_manager_qt.h" #include "web_engine_settings.h" +#include <QDir> #include <QtWebEngineCore/qwebengineurlscheme.h> QT_BEGIN_NAMESPACE @@ -226,7 +227,8 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) itemPrivate->downloadId = info.id; itemPrivate->downloadState = info.accepted ? QWebEngineDownloadItem::DownloadInProgress : QWebEngineDownloadItem::DownloadRequested; - itemPrivate->downloadPath = info.path; + itemPrivate->downloadDirectory = QFileInfo(info.path).path(); + itemPrivate->downloadFileName = QFileInfo(info.path).fileName(); itemPrivate->suggestedFileName = info.suggestedFileName; itemPrivate->mimeType = info.mimeType; itemPrivate->savePageFormat = static_cast<QWebEngineDownloadItem::SavePageFormat>(info.savePageFormat); @@ -245,7 +247,7 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) QWebEngineDownloadItem::DownloadState state = download->state(); - info.path = download->path(); + info.path = QDir(download->downloadDirectory()).filePath(download->downloadFileName()); info.savePageFormat = static_cast<QtWebEngineCore::ProfileAdapterClient::SavePageFormat>( download->savePageFormat()); info.accepted = state != QWebEngineDownloadItem::DownloadCancelled; |