From 0884fab3b161b62c1e2bb1bac6bbd74560f06232 Mon Sep 17 00:00:00 2001 From: Tamas Zakor Date: Tue, 28 May 2019 13:54:28 +0200 Subject: 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 --- src/webengine/api/qquickwebenginedownloaditem.cpp | 111 ++++++++++++++++++++- src/webengine/api/qquickwebenginedownloaditem_p.h | 14 ++- .../api/qquickwebenginedownloaditem_p_p.h | 2 + src/webengine/api/qquickwebengineprofile.cpp | 7 +- 4 files changed, 126 insertions(+), 8 deletions(-) (limited to 'src/webengine/api') diff --git a/src/webengine/api/qquickwebenginedownloaditem.cpp b/src/webengine/api/qquickwebenginedownloaditem.cpp index 2b3e50a72..f9b305e88 100644 --- a/src/webengine/api/qquickwebenginedownloaditem.cpp +++ b/src/webengine/api/qquickwebenginedownloaditem.cpp @@ -43,6 +43,7 @@ #include "profile_adapter.h" #include "qquickwebengineprofile_p.h" +#include #include "QFileInfo" using QtWebEngineCore::ProfileAdapterClient; @@ -417,6 +418,7 @@ QString QQuickWebEngineDownloadItem::mimeType() const /*! \qmlproperty string WebEngineDownloadItem::path + \obsolete Holds the full target path where data is being downloaded to. @@ -433,7 +435,7 @@ QString QQuickWebEngineDownloadItem::mimeType() const QString QQuickWebEngineDownloadItem::path() const { Q_D(const QQuickWebEngineDownloadItem); - return d->downloadPath; + return QDir::cleanPath(QDir(d->downloadDirectory).filePath(d->downloadFileName)); } void QQuickWebEngineDownloadItem::setPath(QString path) @@ -443,7 +445,7 @@ void QQuickWebEngineDownloadItem::setPath(QString path) qWarning("Setting the download path is not allowed after the download has been accepted."); return; } - if (d->downloadPath != path) { + if (QDir(d->downloadDirectory).filePath(d->downloadFileName) != path) { if (QFileInfo(path).fileName().isEmpty()) { qWarning("The download path does not include file name."); return; @@ -454,10 +456,113 @@ void QQuickWebEngineDownloadItem::setPath(QString path) return; } - d->downloadPath = path; + QString newDirectory; + QString newFileName; + + if (QFileInfo(path).fileName() == path) { + newDirectory = QStringLiteral(""); + newFileName = path; + } else { + newDirectory = QFileInfo(path).filePath(); + newFileName = QFileInfo(path).fileName(); + } + + if (d->downloadDirectory != newDirectory) { + d->downloadDirectory = newDirectory; + Q_EMIT pathChanged(); + Q_EMIT downloadDirectoryChanged(); + } + + if (d->downloadFileName != newFileName) { + d->downloadFileName = newFileName; + Q_EMIT pathChanged(); + Q_EMIT downloadFileNameChanged(); + } + } +} + +/*! + \qmlproperty string WebEngineDownloadItem::downloadDirectory + \since QtWebEngine 1.10 + + Holds the full target path without file name where data is being downloaded to. + + The download directory can only be set in the + \l{WebEngineProfile::downloadRequested}{downloadRequested} handler before + the download is accepted. + + \sa WebEngineProfile::downloadRequested(), accept() +*/ + +QString QQuickWebEngineDownloadItem::downloadDirectory() const +{ + Q_D(const QQuickWebEngineDownloadItem); + return d->downloadDirectory; +} + +void QQuickWebEngineDownloadItem::setDownloadDirectory(QString directory) +{ + Q_D(QQuickWebEngineDownloadItem); + if (d->downloadState != QQuickWebEngineDownloadItem::DownloadRequested) { + qWarning("Setting the download directory is not allowed after the download has been accepted."); + return; + } + + QString changeDirectory = d->downloadDirectory; + if (!directory.isEmpty() && changeDirectory != directory) { + changeDirectory = directory; + + if (d->downloadDirectory != changeDirectory) { + d->downloadDirectory = changeDirectory; + Q_EMIT pathChanged(); + Q_EMIT downloadDirectoryChanged(); + } + + QString newFileName = QFileInfo(d->profile->d_ptr->profileAdapter()->updateDownloadPath(d->downloadId, + d->downloadDirectory, + d->suggestedFileName)).fileName(); + if (d->downloadFileName != newFileName) { + d->downloadFileName = newFileName; + Q_EMIT pathChanged(); + Q_EMIT downloadFileNameChanged(); + } + } +} + +/*! + \qmlproperty string WebEngineDownloadItem::downloadFileName + \since QtWebEngine 1.10 + + Holds the name of the file to which data is being downloaded. + + The download file name can only be set in the + \l{WebEngineProfile::downloadRequested}{downloadRequested} handler before + the download is accepted. + + \sa WebEngineProfile::downloadRequested(), accept() +*/ + +QString QQuickWebEngineDownloadItem::downloadFileName() const +{ + Q_D(const QQuickWebEngineDownloadItem); + return d->downloadFileName; +} + +void QQuickWebEngineDownloadItem::setDownloadFileName(QString fileName) +{ + Q_D(QQuickWebEngineDownloadItem); + if (d->downloadState != QQuickWebEngineDownloadItem::DownloadRequested) { + qWarning("Setting the download file name is not allowed after the download has been accepted."); + return; + } + + if (d->downloadFileName != fileName && !fileName.isEmpty()) { + d->downloadFileName = fileName; Q_EMIT pathChanged(); + Q_EMIT downloadFileNameChanged(); } } + /*! \qmlproperty string WebEngineDownloadItem::suggestedFileName \since QtWebEngine 1.10 diff --git a/src/webengine/api/qquickwebenginedownloaditem_p.h b/src/webengine/api/qquickwebenginedownloaditem_p.h index 613b0173d..cef99e534 100644 --- a/src/webengine/api/qquickwebenginedownloaditem_p.h +++ b/src/webengine/api/qquickwebenginedownloaditem_p.h @@ -139,6 +139,8 @@ public: Q_PROPERTY(QQuickWebEngineView *view READ view CONSTANT REVISION 7 FINAL) Q_PROPERTY(QUrl url READ url CONSTANT REVISION 8 FINAL) Q_PROPERTY(QString suggestedFileName READ suggestedFileName CONSTANT REVISION 8 FINAL) + Q_PROPERTY(QString downloadDirectory READ downloadDirectory WRITE setDownloadDirectory NOTIFY downloadDirectoryChanged REVISION 8 FINAL) + Q_PROPERTY(QString downloadFileName READ downloadFileName WRITE setDownloadFileName NOTIFY downloadFileNameChanged REVISION 8 FINAL) Q_INVOKABLE void accept(); Q_INVOKABLE void cancel(); @@ -150,8 +152,8 @@ public: qint64 totalBytes() const; qint64 receivedBytes() const; QString mimeType() const; - QString path() const; - void setPath(QString path); + QString Q_DECL_DEPRECATED path() const; + void Q_DECL_DEPRECATED setPath(QString path); SavePageFormat savePageFormat() const; void setSavePageFormat(SavePageFormat format); DownloadType Q_DECL_DEPRECATED type() const; @@ -163,6 +165,10 @@ public: QQuickWebEngineView *view() const; QUrl url() const; QString suggestedFileName() const; + QString downloadDirectory() const; + void setDownloadDirectory(QString directory); + QString downloadFileName() const; + void setDownloadFileName(QString fileName); Q_SIGNALS: void stateChanged(); @@ -170,11 +176,13 @@ Q_SIGNALS: void receivedBytesChanged(); void totalBytesChanged(); Q_REVISION(1) void mimeTypeChanged(); - void pathChanged(); + void Q_DECL_DEPRECATED pathChanged(); Q_REVISION(3) void typeChanged(); Q_REVISION(4) void interruptReasonChanged(); Q_REVISION(5) void isFinishedChanged(); Q_REVISION(5) void isPausedChanged(); + Q_REVISION(8) void downloadDirectoryChanged(); + Q_REVISION(8) void downloadFileNameChanged(); private: QQuickWebEngineDownloadItem(QQuickWebEngineDownloadItemPrivate*, QObject *parent = 0); diff --git a/src/webengine/api/qquickwebenginedownloaditem_p_p.h b/src/webengine/api/qquickwebenginedownloaditem_p_p.h index e4d90d8ef..51deee18b 100644 --- a/src/webengine/api/qquickwebenginedownloaditem_p_p.h +++ b/src/webengine/api/qquickwebenginedownloaditem_p_p.h @@ -84,6 +84,8 @@ public: QQuickWebEngineView *view; QUrl downloadUrl; QString suggestedFileName; + QString downloadDirectory; + QString downloadFileName; void update(const QtWebEngineCore::ProfileAdapterClient::DownloadItemInfo &info); void updateState(QQuickWebEngineDownloadItem::DownloadState newState); diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp index 2dbd0e94b..57434e296 100644 --- a/src/webengine/api/qquickwebengineprofile.cpp +++ b/src/webengine/api/qquickwebengineprofile.cpp @@ -48,6 +48,8 @@ #include "qwebenginecookiestore.h" #include "qwebenginenotification.h" +#include +#include #include #include "profile_adapter.h" @@ -242,7 +244,8 @@ void QQuickWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) itemPrivate->downloadState = QQuickWebEngineDownloadItem::DownloadRequested; itemPrivate->totalBytes = info.totalBytes; itemPrivate->mimeType = info.mimeType; - itemPrivate->downloadPath = info.path; + itemPrivate->downloadDirectory = QFileInfo(info.path).path(); + itemPrivate->downloadFileName = QFileInfo(info.path).fileName(); itemPrivate->suggestedFileName = info.suggestedFileName; itemPrivate->savePageFormat = static_cast( info.savePageFormat); @@ -261,7 +264,7 @@ void QQuickWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) Q_EMIT q->downloadRequested(download); QQuickWebEngineDownloadItem::DownloadState state = download->state(); - info.path = download->path(); + info.path = QDir(download->downloadDirectory()).filePath(download->downloadFileName()); info.savePageFormat = itemPrivate->savePageFormat; info.accepted = state != QQuickWebEngineDownloadItem::DownloadCancelled && state != QQuickWebEngineDownloadItem::DownloadRequested; -- cgit v1.2.3