@@ -0,0 +1,204 @@
2+ * This file is part of system-settings
4+ * Copyright (C) 2016 Canonical Ltd.
6+ * This program is free software: you can redistribute it and/or modify it
7+ * under the terms of the GNU General Public License version 3, as published
8+ * by the Free Software Foundation.
10+ * This program is distributed in the hope that it will be useful, but
11+ * WITHOUT ANY WARRANTY; without even the implied warranties of
12+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13+ * PURPOSE. See the GNU General Public License for more details.
15+ * You should have received a copy of the GNU General Public License along
16+ * with this program. If not, see <http://www.gnu.org/licenses/>.
20+import Ubuntu.DownloadManager 1.2
24+ property var updateModel
25+ property alias downloads: downloadManager.downloads
26+ property var udm: DownloadManager {
28+ onDownloadFinished: {
29+ updateModel.setInstalled(download.metadata.custom.identifier,
30+ download.metadata.custom.revision);
33+ updateModel.pauseUpdate(download.metadata.custom.identifier,
34+ download.metadata.custom.revision)
37+ updateModel.resumeUpdate(download.metadata.custom.identifier,
38+ download.metadata.custom.revision)
40+ onDownloadCanceled: {
41+ updateModel.cancelUpdate(download.metadata.custom.identifier,
42+ download.metadata.custom.revision)
45+ updateModel.setError(download.metadata.custom.identifier,
46+ download.metadata.custom.revision,
47+ download.errorMessage)
49+ onDownloadsChanged: restoreDownloads()
50+ Component.onCompleted: restoreDownloads()
53+ function downloadAll() {
57+ function restoreDownloads() {
59+ for (var i = 0; i<downloads.length; i++) {
62+ /* We only bind those signals here, that the UDM does not
63+ receive, i.e. processing and progressChanged. */
64+ dl.progressChanged.connect(onDownloadProgress.bind(dl));
65+ dl.processing.connect(onDownloadProcessing.bind(dl));
71+ function resumeDownload(click) {
72+ var download = getDownloadFor(click);
73+ if (download && !download.downloading && !download.isCompleted) {
78+ function pauseDownload(click) {
79+ var download = getDownloadFor(click);
80+ if (download && download.downloading) {
85+ // Return download for a click update.
86+ function getDownloadFor(click) {
90+ for (var i = 0; i<downloads.length; i++) {
92+ if (dl.errorMessage || dl.isCompleted) {
93+ // Ignore failed and completed downloads.
97+ cust = downloads[i].metadata.custom;
98+ if (cust.identifier === click.identifier && cust.revision === click.revision) {
105+ function hasExistingDownload(click) {
106+ var existingDownload = getDownloadFor(click);
107+ return (existingDownload !== null &&
108+ !existingDownload.errorMessage &&
109+ !existingDownload.isCompleted);
112+ function retryDownload(click) {
113+ return createDownload(click, true);
116+ function createDownload(click, useSignedUrl) {
117+ if (hasExistingDownload(click)) {
120+ var downloadUrl = click.downloadUrl;
122+ "command": click.command,
123+ "title": click.title,
124+ "showInIndicator": false
126+ var metadataObj = mdt.createObject(root, metadata);
127+ metadataObj.custom = {
128+ "identifier": click.identifier,
129+ "package-name": click.identifier,
130+ "revision": click.revision,
135+ if (!click.signedDownloadUrl) {
136+ console.warn('The signed download URL was empty.');
138+ downloadUrl = click.signedDownloadUrl;
140+ // If we're using an unsigned URL, we need to provide this header.
141+ hdrs["X-Click-Token"] = click.token;
144+ var singleDownloadObj = sdl.createObject(root, {
146+ "hash": click.downloadHash,
147+ "algorithm": "sha512",
149+ "metadata": metadataObj,
150+ "revision": click.revision,
151+ "identifier": click.identifier
153+ singleDownloadObj.download(downloadUrl);
154+ return singleDownloadObj;
157+ function onDownloadProgress(progress) {
158+ updateModel.setProgress(this.metadata.custom.identifier,
159+ this.metadata.custom.revision,
163+ function onDownloadProcessing() {
164+ updateModel.processUpdate(this.metadata.custom.identifier,
165+ this.metadata.custom.revision);
168+ /* If a update's model has a downloadId, check if UDM knows it. If not,
169+ treat this as a failure. Workaround for lp:1603770. */
170+ function assertDownloadExist(click) {
171+ if (!getDownloadFor(click)) {
172+ updateModel.setError(
173+ click.identifier, click.revision,
174+ i18n.tr("Installation failed")
183+ objectName: "singleDownload"
184+ property bool _bound: true
186+ onDownloadIdChanged: {
187+ updateModel.queueUpdate(metadata.custom.identifier,
188+ metadata.custom.revision,
192+ updateModel.startUpdate(metadata.custom.identifier,
193+ metadata.custom.revision);
195+ onProgressChanged: onDownloadProgress.call(download)
196+ onProcessing: onDownloadProcessing.call(download)