码迷,mamicode.com
首页 > 系统相关 > 详细

如何使用Ubuntu SDK中的Download Manager来下载文件

时间:2015-05-05 16:43:51      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

对于一下应用来说,我们需要使用网路上的一下文件,并下载它们。那么我们怎么在QML应用中来下载文件呢?我们在SDK API的网页中,我们发现有一个叫做Download Manager的API。我们可以使用SingleDownloadDownloadManager来下载一个或多个文件。


首先,我们来创建一个简单的Download应用。这里我们使用“QML app with Simple UI (qmlproject)”。对于大家不熟悉Download Manager的开发者来说,我们可以使用如下的方法得到它更加详细的接口:


$qmlplugindump Ubuntu.DownloadManager 0.1

通过上面的命令,我们可以得到如下的输出:


import QtQuick.tooling 1.1

// This file describes the plugin-supplied types contained in the library.
// It is used for QML tooling purposes only.
//
// This file was auto-generated by:
// 'qmlplugindump Ubuntu.DownloadManager 0.1'

Module {
    Component {
        name: "Ubuntu::DownloadManager::DownloadError"
        prototype: "QObject"
        exports: ["Error 0.1"]
        exportMetaObjectRevisions: [0]
        Property { name: "type"; type: "string"; isReadonly: true }
        Property { name: "message"; type: "string"; isReadonly: true }
    }
    Component {
        name: "Ubuntu::DownloadManager::Metadata"
        prototype: "QObject"
        exports: ["Metadata 0.1"]
        exportMetaObjectRevisions: [0]
        Property { name: "title"; type: "string" }
        Property { name: "showInIndicator"; type: "bool" }
        Property { name: "deflate"; type: "bool" }
        Property { name: "extract"; type: "bool" }
        Signal { name: "showIndicatorChanged" }
    }
    Component {
        name: "Ubuntu::DownloadManager::SingleDownload"
        prototype: "QObject"
        exports: ["SingleDownload 0.1"]
        exportMetaObjectRevisions: [0]
        Property { name: "autoStart"; type: "bool" }
        Property { name: "errorMessage"; type: "string"; isReadonly: true }
        Property { name: "isCompleted"; type: "bool"; isReadonly: true }
        Property { name: "downloadInProgress"; type: "bool"; isReadonly: true }
        Property { name: "allowMobileDownload"; type: "bool" }
        Property { name: "throttle"; type: "qulonglong" }
        Property { name: "progress"; type: "int"; isReadonly: true }
        Property { name: "downloading"; type: "bool"; isReadonly: true }
        Property { name: "downloadId"; type: "string"; isReadonly: true }
        Property { name: "headers"; type: "QVariantMap" }
        Property { name: "metadata"; type: "Ubuntu::DownloadManager::Metadata"; isPointer: true }
        Signal {
            name: "canceled"
            Parameter { name: "success"; type: "bool" }
        }
        Signal {
            name: "finished"
            Parameter { name: "path"; type: "string" }
        }
        Signal {
            name: "paused"
            Parameter { name: "success"; type: "bool" }
        }
        Signal {
            name: "processing"
            Parameter { name: "path"; type: "string" }
        }
        Signal {
            name: "progressReceived"
            Parameter { name: "received"; type: "qulonglong" }
            Parameter { name: "total"; type: "qulonglong" }
        }
        Signal {
            name: "resumed"
            Parameter { name: "success"; type: "bool" }
        }
        Signal {
            name: "started"
            Parameter { name: "success"; type: "bool" }
        }
        Signal {
            name: "errorFound"
            Parameter { name: "error"; type: "DownloadError&" }
        }
        Signal { name: "errorChanged" }
        Method {
            name: "registerError"
            Parameter { name: "error"; type: "Error"; isPointer: true }
        }
        Method {
            name: "bindDownload"
            Parameter { name: "download"; type: "Download"; isPointer: true }
        }
        Method {
            name: "unbindDownload"
            Parameter { name: "download"; type: "Download"; isPointer: true }
        }
        Method {
            name: "onFinished"
            Parameter { name: "path"; type: "string" }
        }
        Method {
            name: "onProgress"
            Parameter { name: "received"; type: "qulonglong" }
            Parameter { name: "total"; type: "qulonglong" }
        }
        Method {
            name: "onPaused"
            Parameter { name: "wasPaused"; type: "bool" }
        }
        Method {
            name: "onResumed"
            Parameter { name: "wasResumed"; type: "bool" }
        }
        Method {
            name: "onStarted"
            Parameter { name: "wasStarted"; type: "bool" }
        }
        Method {
            name: "onCanceled"
            Parameter { name: "wasCanceled"; type: "bool" }
        }
        Method { name: "start" }
        Method { name: "pause" }
        Method { name: "resume" }
        Method { name: "cancel" }
        Method {
            name: "download"
            Parameter { name: "url"; type: "string" }
        }
    }
    Component {
        name: "Ubuntu::DownloadManager::UbuntuDownloadManager"
        prototype: "QObject"
        exports: ["DownloadManager 0.1"]
        exportMetaObjectRevisions: [0]
        Property { name: "autoStart"; type: "bool" }
        Property { name: "cleanDownloads"; type: "bool" }
        Property { name: "errorMessage"; type: "string"; isReadonly: true }
        Property { name: "downloads"; type: "QVariantList"; isReadonly: true }
        Signal { name: "errorChanged" }
        Method {
            name: "download"
            Parameter { name: "url"; type: "string" }
        }
    }
}

在今天的例子里,我们将使用SingleDownload来下载我们所需要的图片。我们可以看到,SingleDownload有“finished”信号,我们可以通过这个信号来得到文件下载完毕的通知,并在“path”中得到我们所下载文件的路径。当然我们也可以使用其它的信号来得到下载的更多的信息。


我们来修改我们的“Main.qml”如下:


import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.DownloadManager 0.1

/*!
    \brief MainView with a Label and Button elements.
*/

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "download.liu-xiao-guo"

    /*
     This property enables the application to change orientation
     when the device is rotated. The default is false.
    */
    //automaticOrientation: true

    // Removes the old toolbar and enables new features of the new header.
    useDeprecatedToolbar: false

    width: units.gu(50)
    height: units.gu(75)

    Page {
        title: i18n.tr("Download")

        Rectangle {
            width: parent.width
            height: units.gu(20)
            TextField {
                id: text
                placeholderText: "File URL to download..."
                height: units.gu(5)
                anchors {
                    left: parent.left
                    right: button.left
                    rightMargin: units.gu(2)
                }

                text: "http://bbs.unpcn.com/attachment.aspx?attachmentid=3820584"
            }

            Button {
                id: button
                text: "Download"
                height: 50
                anchors.right: parent.right
                anchors.verticalCenter: text.verticalCenter
                onClicked: {
                    single.download(text.text);
                }
            }

            TextField {
                id: downloaded
                placeholderText: "Downloaded file address"
                height: 100
                anchors {
                    left: parent.left
                    right: parent.right
                    top: button.bottom
                    rightMargin: units.gu(2)
                    topMargin: units.gu(1)
                }
            }

            ProgressBar {
                id: progress
                minimumValue: 0
                maximumValue: 100
                value: single.progress
                anchors {
                    left: parent.left
                    right: parent.right
                    bottom: parent.bottom
                }

                SingleDownload {
                    id: single

                    onFinished: {
                        downloaded.text = path;
                        console.log("downloaded path: " + path);
                    }
                }
            }

            Image {
                anchors.top: progress.bottom
                source: downloaded.text
            }
        }
    }
}


界面非常简单,我们在上面可以输入我们想要的URL文件地址,在下面显示下载后的文件的路径。我们可以点击“Download”按钮来下载我们所需要的文件。为了说明问题,我们在下面使用了一个Image来显示下载的图片:


技术分享


所有的源码在:git clone https://gitcafe.com/ubuntu/download.git


如何使用Ubuntu SDK中的Download Manager来下载文件

标签:

原文地址:http://blog.csdn.net/ubuntutouch/article/details/45501223

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!