summaryrefslogtreecommitdiffstats
path: root/src/webengine/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/webengine/api')
-rw-r--r--src/webengine/api/qquickwebenginedownloaditem.cpp187
-rw-r--r--src/webengine/api/qquickwebenginedownloaditem_p.h17
-rw-r--r--src/webengine/api/qquickwebenginedownloaditem_p_p.h7
-rw-r--r--src/webengine/api/qquickwebengineprofile.cpp46
-rw-r--r--src/webengine/api/qquickwebengineprofile_p.h2
5 files changed, 203 insertions, 56 deletions
diff --git a/src/webengine/api/qquickwebenginedownloaditem.cpp b/src/webengine/api/qquickwebenginedownloaditem.cpp
index 967638b7..6ee00d9a 100644
--- a/src/webengine/api/qquickwebenginedownloaditem.cpp
+++ b/src/webengine/api/qquickwebenginedownloaditem.cpp
@@ -39,12 +39,26 @@
QT_BEGIN_NAMESPACE
+static inline QQuickWebEngineDownloadItem::DownloadState toDownloadState(int state) {
+ switch (state) {
+ case BrowserContextAdapterClient::DownloadInProgress:
+ return QQuickWebEngineDownloadItem::DownloadInProgress;
+ case BrowserContextAdapterClient::DownloadCompleted:
+ return QQuickWebEngineDownloadItem::DownloadCompleted;
+ case BrowserContextAdapterClient::DownloadCancelled:
+ return QQuickWebEngineDownloadItem::DownloadCancelled;
+ case BrowserContextAdapterClient::DownloadInterrupted:
+ return QQuickWebEngineDownloadItem::DownloadInterrupted;
+ default:
+ Q_UNREACHABLE();
+ return QQuickWebEngineDownloadItem::DownloadCancelled;
+ }
+}
+
QQuickWebEngineDownloadItemPrivate::QQuickWebEngineDownloadItemPrivate(QQuickWebEngineProfilePrivate *p)
: profile(p)
- , downloadStarted(false)
, downloadId(-1)
, downloadState(QQuickWebEngineDownloadItem::DownloadCancelled)
- , downloadProgress(0)
{
}
@@ -53,64 +67,189 @@ QQuickWebEngineDownloadItemPrivate::~QQuickWebEngineDownloadItemPrivate()
profile->downloadDestroyed(downloadId);
}
-void QQuickWebEngineDownloadItemPrivate::update(QQuickWebEngineDownloadItem::DownloadState state, int progress)
+/*!
+ \qmltype WebEngineDownloadItem
+ \instantiates QQuickWebEngineDownloadItem
+ \inqmlmodule QtWebEngine 1.1
+ \since QtWebEngine 1.1
+ \brief A WebEngineDownloadItem provides information about a download.
+
+ WebEngineDownloadItem stores the state of a download to be used to manage requested downloads.
+
+ By default the download is rejected unless the user explicitly accepts it with
+ WebEngineDownloadItem::accept().
+*/
+
+void QQuickWebEngineDownloadItemPrivate::update(const BrowserContextAdapterClient::DownloadItemInfo &info)
{
Q_Q(QQuickWebEngineDownloadItem);
- if (state != downloadState) {
- downloadState = state;
- Q_EMIT q->stateChanged();
+
+ updateState(toDownloadState(info.state));
+
+ if (info.receivedBytes != receivedBytes) {
+ receivedBytes = info.receivedBytes;
+ Q_EMIT q->receivedBytesChanged();
+ }
+
+ if (info.totalBytes != totalBytes) {
+ totalBytes = info.totalBytes;
+ Q_EMIT q->totalBytesChanged();
}
- if (progress != downloadProgress) {
- downloadProgress = progress;
- Q_EMIT q->progressChanged();
+}
+
+void QQuickWebEngineDownloadItemPrivate::updateState(QQuickWebEngineDownloadItem::DownloadState newState)
+{
+ Q_Q(QQuickWebEngineDownloadItem);
+
+ if (downloadState != newState) {
+ downloadState = newState;
+ Q_EMIT q->stateChanged();
}
}
+/*!
+ \qmlmethod void WebEngineDownloadItem::accept()
+
+ Accepts the download request, which will start the download.
+
+ \sa WebEngineDownloadItem::cancel()
+*/
+
+void QQuickWebEngineDownloadItem::accept()
+{
+ Q_D(QQuickWebEngineDownloadItem);
+
+ if (d->downloadState != QQuickWebEngineDownloadItem::DownloadRequested)
+ return;
+
+ d->updateState(QQuickWebEngineDownloadItem::DownloadInProgress);
+}
+
+/*!
+ \qmlmethod void WebEngineDownloadItem::cancel()
+
+ Cancels the download.
+*/
+
void QQuickWebEngineDownloadItem::cancel()
{
Q_D(QQuickWebEngineDownloadItem);
- if (d->downloadState == QQuickWebEngineDownloadItem::DownloadCompleted
- || d->downloadState == QQuickWebEngineDownloadItem::DownloadCancelled)
+ QQuickWebEngineDownloadItem::DownloadState state = d->downloadState;
+
+ if (state == QQuickWebEngineDownloadItem::DownloadCompleted
+ || state == QQuickWebEngineDownloadItem::DownloadCancelled)
return;
- d->update(QQuickWebEngineDownloadItem::DownloadCancelled, d->downloadProgress);
+ d->updateState(QQuickWebEngineDownloadItem::DownloadCancelled);
// We directly cancel the download if the user cancels before
// it even started, so no need to notify the profile here.
- if (d->downloadStarted)
+ if (state == QQuickWebEngineDownloadItem::DownloadInProgress)
d->profile->cancelDownload(d->downloadId);
}
-quint32 QQuickWebEngineDownloadItem::id()
+/*!
+ \qmlproperty quint32 WebEngineDownloadItem::id
+
+ The download item's id.
+*/
+
+quint32 QQuickWebEngineDownloadItem::id() const
{
- Q_D(QQuickWebEngineDownloadItem);
+ Q_D(const QQuickWebEngineDownloadItem);
return d->downloadId;
}
-QQuickWebEngineDownloadItem::DownloadState QQuickWebEngineDownloadItem::state()
+/*!
+ \qmlproperty enumeration WebEngineDownloadItem::state
+
+ This property describes the state in which the download is in.
+
+ The state can be one of:
+
+ \table
+
+ \header
+ \li Constant
+ \li Description
+
+ \row
+ \li DownloadRequested
+ \li The download has been requested, but has not been accepted yet.
+
+ \row
+ \li DownloadInProgress
+ \li The download is in progress.
+
+ \row
+ \li DownloadCompleted
+ \li The download completed successfully.
+
+ \row
+ \li DownloadInterrupted
+ \li The download has been interrupted (by the server or because of lost connectivity).
+
+ \endtable
+*/
+
+QQuickWebEngineDownloadItem::DownloadState QQuickWebEngineDownloadItem::state() const
{
- Q_D(QQuickWebEngineDownloadItem);
+ Q_D(const QQuickWebEngineDownloadItem);
return d->downloadState;
}
-int QQuickWebEngineDownloadItem::progress()
+/*!
+ \qmlproperty int WebEngineDownloadItem::totalBytes
+
+ The download's total size in bytes.
+
+ -1 means the total size is unknown.
+*/
+
+qint64 QQuickWebEngineDownloadItem::totalBytes() const
{
- Q_D(QQuickWebEngineDownloadItem);
- return d->downloadProgress;
+ Q_D(const QQuickWebEngineDownloadItem);
+ return d->totalBytes;
}
-QString QQuickWebEngineDownloadItem::path()
+/*!
+ \qmlproperty int WebEngineDownloadItem::receivedBytes
+
+ The download's bytes that have been received so far.
+*/
+
+qint64 QQuickWebEngineDownloadItem::receivedBytes() const
{
- Q_D(QQuickWebEngineDownloadItem);
+ Q_D(const QQuickWebEngineDownloadItem);
+ return d->receivedBytes;
+}
+
+/*!
+ \qmlproperty QString WebEngineDownloadItem::path
+
+ The download item's full target path where it is being downloaded to.
+
+ The path includes the file name. The default suggested path is the standard
+ download location and file name is deduced not to overwrite already existing files.
+
+ The download path can only be set in the \c WebEngineProfile.onDownloadRequested
+ handler before the download is accepted.
+
+ \sa WebEngineProfile::downloadRequested(WebEngineDownloadItem download), WebEngineDownloadItem::accept()
+*/
+
+QString QQuickWebEngineDownloadItem::path() const
+{
+ Q_D(const QQuickWebEngineDownloadItem);
return d->downloadPath;
}
void QQuickWebEngineDownloadItem::setPath(QString path)
{
Q_D(QQuickWebEngineDownloadItem);
- if (d->downloadStarted) {
- qWarning("Setting the download path is not allowed after the download has been started.");
+ if (d->downloadState != QQuickWebEngineDownloadItem::DownloadRequested) {
+ qWarning("Setting the download path is not allowed after the download has been accepted.");
return;
}
if (d->downloadPath != path) {
diff --git a/src/webengine/api/qquickwebenginedownloaditem_p.h b/src/webengine/api/qquickwebenginedownloaditem_p.h
index 2cb140ac..ea12c9c9 100644
--- a/src/webengine/api/qquickwebenginedownloaditem_p.h
+++ b/src/webengine/api/qquickwebenginedownloaditem_p.h
@@ -52,6 +52,7 @@ class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineDownloadItem: public QObject {
public:
~QQuickWebEngineDownloadItem();
enum DownloadState {
+ DownloadRequested,
DownloadInProgress,
DownloadCompleted,
DownloadCancelled,
@@ -61,20 +62,24 @@ public:
Q_PROPERTY(quint32 id READ id() CONSTANT FINAL)
Q_PROPERTY(DownloadState state READ state NOTIFY stateChanged)
- Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
+ Q_PROPERTY(qint64 totalBytes READ totalBytes NOTIFY totalBytesChanged)
+ Q_PROPERTY(qint64 receivedBytes READ receivedBytes NOTIFY receivedBytesChanged)
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
+ Q_INVOKABLE void accept();
Q_INVOKABLE void cancel();
- quint32 id();
- DownloadState state();
- int progress();
- QString path();
+ quint32 id() const;
+ DownloadState state() const;
+ qint64 totalBytes() const;
+ qint64 receivedBytes() const;
+ QString path() const;
void setPath(QString path);
Q_SIGNALS:
void stateChanged();
- void progressChanged();
+ void receivedBytesChanged();
+ void totalBytesChanged();
void pathChanged();
private:
diff --git a/src/webengine/api/qquickwebenginedownloaditem_p_p.h b/src/webengine/api/qquickwebenginedownloaditem_p_p.h
index 6b724b4c..0250a8fa 100644
--- a/src/webengine/api/qquickwebenginedownloaditem_p_p.h
+++ b/src/webengine/api/qquickwebenginedownloaditem_p_p.h
@@ -53,13 +53,14 @@ public:
QQuickWebEngineDownloadItemPrivate(QQuickWebEngineProfilePrivate *p);
~QQuickWebEngineDownloadItemPrivate();
- bool downloadStarted;
quint32 downloadId;
QQuickWebEngineDownloadItem::DownloadState downloadState;
- int downloadProgress;
+ qint64 totalBytes;
+ qint64 receivedBytes;
QString downloadPath;
- void update(QQuickWebEngineDownloadItem::DownloadState state, int progress);
+ void update(const BrowserContextAdapterClient::DownloadItemInfo &info);
+ void updateState(QQuickWebEngineDownloadItem::DownloadState newState);
};
QT_END_NAMESPACE
diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp
index 791f3926..cac30929 100644
--- a/src/webengine/api/qquickwebengineprofile.cpp
+++ b/src/webengine/api/qquickwebengineprofile.cpp
@@ -48,22 +48,6 @@
QT_BEGIN_NAMESPACE
-static inline QQuickWebEngineDownloadItem::DownloadState toDownloadState(int state) {
- switch (state) {
- case BrowserContextAdapterClient::DownloadInProgress:
- return QQuickWebEngineDownloadItem::DownloadInProgress;
- case BrowserContextAdapterClient::DownloadCompleted:
- return QQuickWebEngineDownloadItem::DownloadCompleted;
- case BrowserContextAdapterClient::DownloadCancelled:
- return QQuickWebEngineDownloadItem::DownloadCancelled;
- case BrowserContextAdapterClient::DownloadInterrupted:
- return QQuickWebEngineDownloadItem::DownloadInterrupted;
- default:
- Q_UNREACHABLE();
- return QQuickWebEngineDownloadItem::DownloadCancelled;
- }
-}
-
QQuickWebEngineProfilePrivate::QQuickWebEngineProfilePrivate(BrowserContextAdapter* browserContext, bool ownsContext)
: m_settings(new QQuickWebEngineSettings())
, m_browserContext(browserContext)
@@ -95,7 +79,6 @@ void QQuickWebEngineProfilePrivate::cancelDownload(quint32 downloadId)
void QQuickWebEngineProfilePrivate::downloadDestroyed(quint32 downloadId)
{
m_ongoingDownloads.remove(downloadId);
- cancelDownload(downloadId);
}
void QQuickWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info)
@@ -105,7 +88,7 @@ void QQuickWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info)
Q_ASSERT(!m_ongoingDownloads.contains(info.id));
QQuickWebEngineDownloadItemPrivate *itemPrivate = new QQuickWebEngineDownloadItemPrivate(this);
itemPrivate->downloadId = info.id;
- itemPrivate->downloadState = QQuickWebEngineDownloadItem::DownloadInProgress;
+ itemPrivate->downloadState = QQuickWebEngineDownloadItem::DownloadRequested;
itemPrivate->downloadPath = info.path;
QQuickWebEngineDownloadItem *download = new QQuickWebEngineDownloadItem(itemPrivate, q);
@@ -113,11 +96,12 @@ void QQuickWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info)
m_ongoingDownloads.insert(info.id, download);
QQmlEngine::setObjectOwnership(download, QQmlEngine::JavaScriptOwnership);
- Q_EMIT q->downloadStarted(download);
- download->d_func()->downloadStarted = true;
+ Q_EMIT q->downloadRequested(download);
+ QQuickWebEngineDownloadItem::DownloadState state = download->state();
info.path = download->path();
- info.cancelled = download->state() == QQuickWebEngineDownloadItem::DownloadCancelled;
+ info.cancelled = state == QQuickWebEngineDownloadItem::DownloadCancelled
+ || state == QQuickWebEngineDownloadItem::DownloadRequested;
}
void QQuickWebEngineProfilePrivate::downloadUpdated(const DownloadItemInfo &info)
@@ -134,7 +118,7 @@ void QQuickWebEngineProfilePrivate::downloadUpdated(const DownloadItemInfo &info
return;
}
- download->d_func()->update(toDownloadState(info.state), info.percentComplete);
+ download->d_func()->update(info);
if (info.state != BrowserContextAdapterClient::DownloadInProgress) {
Q_EMIT q->downloadFinished(download);
@@ -156,6 +140,24 @@ void QQuickWebEngineProfilePrivate::downloadUpdated(const DownloadItemInfo &info
belongs to.
*/
+/*!
+ \qmlsignal WebEngineProfile::downloadRequested(WebEngineDownloadItem download)
+
+ This signal is emitted whenever a download has been triggered.
+ The \a download argument holds the state of the download.
+ The \a download has to be explicitly accepted with WebEngineDownloadItem::accept(),
+ else the download will be cancelled by default.
+*/
+
+/*!
+ \qmlsignal WebEngineProfile::downloadFinished(WebEngineDownloadItem download)
+
+ This signal is emitted whenever a download finishes downloading.
+ This can be due to the download finishing successfully, being cancelled or
+ interrupted by lost connectivity for example.
+ The \a download argument holds the state of the finished download instance.
+*/
+
QQuickWebEngineProfile::QQuickWebEngineProfile()
: d_ptr(new QQuickWebEngineProfilePrivate(new BrowserContextAdapter(false), true))
{
diff --git a/src/webengine/api/qquickwebengineprofile_p.h b/src/webengine/api/qquickwebengineprofile_p.h
index 7a2ff08e..f3e0f5c3 100644
--- a/src/webengine/api/qquickwebengineprofile_p.h
+++ b/src/webengine/api/qquickwebengineprofile_p.h
@@ -112,7 +112,7 @@ signals:
void persistentCookiesPolicyChanged();
void httpCacheMaximumSizeChanged();
- void downloadStarted(QQuickWebEngineDownloadItem *download);
+ void downloadRequested(QQuickWebEngineDownloadItem *download);
void downloadFinished(QQuickWebEngineDownloadItem *download);
private: