Rationale

A Download represents a single url to be downloaded by the ubuntu download manager.

Interface

The following defines the public interface exposed by the DownloadManager.

Methods

totalSize(OUT UInt64 size)

progress(OUT UInt64 received)

metadata(OUT Dict<String, Variant> metadata)

setThrottle(IN UInt64 speed)

throttle(OUT UInt64 speed)

allowGSMDownload(IN Bool isAllowed)

isGSMDownloadAllowed(OUT Bool isAllowed)

start

pause

resume

cancel

Signals

started(OUT Bool started)

paused(OUT Bool paused)

resumed(OUT Bool resumed)

canceled(OUT Bool canceled)

hashing(OUT String localPath)

processing(OUT String localPath)

finished(OUT String localPath)

error(OUT String errorMsg)

progress(OUT UInt64 received, OUT UInt64 total)

DBus API XML Definition

The following is an XML definition of the DBus interface that can be used to generate adapters in different languages:

   1 <node>
   2   <interface name="com.canonical.applications.Download">
   3 
   4     <method name="totalSize">
   5         <arg name="total" type="t" direction="out"/>
   6     </method>
   7 
   8     <method name="progress">
   9         <arg name="received" type="t" direction="out"/>
  10     </method>
  11 
  12     <method name="metadata">
  13         <arg name="data" type="a{sv}" direction="out" />
  14     </method>
  15 
  16     <method name="setThrottle">
  17         <arg name="speed" type="t" direction="in"/>
  18     </method>
  19 
  20     <method name="throttle">
  21         <arg name="speed" type="t" direction="out"/>
  22     </method>
  23 
  24     <method name="allowGSMDownload">
  25         <arg name="allowed" type="b" direction="in"/>
  26     </method>
  27 
  28     <method name="isGSMDownloadAllowed">
  29         <arg name="allowed" type="b" direction="out"/>
  30     </method>
  31 
  32     <method name="start" />
  33     <method name="pause" />
  34     <method name="resume" />
  35     <method name="cancel" />
  36 
  37     <signal name="started">
  38         <arg name="success" type="b" direction="out"/>
  39     </signal>
  40 
  41     <signal name="paused">
  42         <arg name="success" type="b" direction="out"/>
  43     </signal>
  44 
  45     <signal name="resumed">
  46         <arg name="success" type="b" direction="out"/>
  47     </signal>
  48 
  49     <signal name="canceled">
  50         <arg name="success" type="b" direction="out"/>
  51     </signal>
  52 
  53     <signal name="hashing">
  54         <arg name="path" type="s" direction="out"/>
  55     </signal>
  56 
  57     <signal name="processing">
  58         <arg name="path" type="s" direction="out"/>
  59     </signal>
  60 
  61     <signal name="finished">
  62         <arg name="path" type="s" direction="out"/>
  63     </signal>
  64 
  65     <signal name="error">
  66         <arg name="error" type="s" direction="out"/>
  67     </signal>
  68 
  69     <signal name="progress">
  70         <arg name="received" type="t" direction="out"/>
  71         <arg name="total" type="t" direction="out"/>
  72     </signal>
  73 
  74  </interface>
  75 </node>

Code Examples

Python

Small script that shows how to use the DownloadManager to create a single download, track its process and finish the script.

   1 import gobject
   2 import dbus
   3 from dbus.mainloop.glib import DBusGMainLoop
   4 
   5 DBusGMainLoop(set_as_default=True)
   6 
   7 MANAGER_PATH = '/'
   8 MANAGER_IFACE = 'com.canonical.applications.DownloaderManager'
   9 DOWNLOAD_IFACE = 'com.canonical.applications.Download'
  10 IMAGE_FILE = 'http://i.imgur.com/y51njgu.jpg'
  11 
  12 
  13 def download_created(path):
  14     """Deal with the download created signal."""
  15     print 'Download created in %s' % path
  16 
  17 
  18 def finished_callback(path, loop):
  19     """Deal with the finis signal."""
  20     print 'Download performed in "%s"' % path
  21     loop.quit()
  22 
  23 
  24 def progress_callback(total, progress):
  25     """Deal with the progress signals."""
  26     print 'Progress is %s/%s' % (progress, total)
  27 
  28 if __name__ == '__main__':
  29 
  30     bus = dbus.SessionBus()
  31     loop = gobject.MainLoop()
  32     manager = bus.get_object('com.canonical.applications.Downloader',
  33             MANAGER_PATH)
  34     manager_dev_iface = dbus.Interface(manager, dbus_interface=MANAGER_IFACE)
  35 
  36     # ensure that download created works
  37     manager_dev_iface.connect_to_signal('downloadCreated', download_created)
  38 
  39     down_path = manager_dev_iface.createDownload((IMAGE_FILE, "", "", {}, {}))
  40 
  41     download = bus.get_object('com.canonical.applications.Downloader',
  42             down_path)
  43 
  44     download_dev_iface = dbus.Interface(download, dbus_interface=DOWNLOAD_IFACE)
  45 
  46     # connect to signals
  47     download_dev_iface.connect_to_signal('progress', progress_callback)
  48     download_dev_iface.connect_to_signal('finished',
  49             lambda path: finished_callback(path, loop))
  50 
  51     download_dev_iface.start()
  52 
  53     loop.run()

DownloadService/Download (last edited 2013-11-07 13:45:30 by 65)