4.3.1r21
[enigma2.git] / usr / lib / enigma2 / python / Plugins / Extensions / Browser / Downloads.py
1 from Components.Task import Task, Job, JobManager
2 from Tools.Downloader import downloadWithProgress
3 from Tools import Notifications
4
5 Notifications.notificationQueue.registerDomain("DownloadManager", _("Download Manager"), Notifications.ICON_DEFAULT)
6
7
8 class DownloadJob(Job):
9         def __init__(self, url, file, title, agent=None):
10                 Job.__init__(self, title)
11                 DownloadTask(self, url, file, agent)
12
13 class DownloadTask(Task):
14         def __init__(self, job, url, fileName, agent=None):
15                 print "[DownloadTask] url='%s', fileName='%s'" %(url, fileName)
16                 Task.__init__(self, job, ("download task"))
17                 self.agent = agent
18                 self.end = 100
19                 self.url = url
20                 self.local = fileName
21
22         def prepare(self):
23                 self.error = None
24
25         def run(self, callback):
26                 self.callback = callback
27                 if self.agent is not None:
28                         self.download = downloadWithProgress(self.url,self.local,agent=self.agent)
29                 else:
30                         self.download = downloadWithProgress(self.url,self.local)
31                 self.download.addProgress(self.http_progress)
32                 self.download.start().addCallback(self.http_finished).addErrback(self.http_failed)
33
34         def http_progress(self, recvbytes, totalbytes):
35                 self.progress = int(self.end*recvbytes/float(totalbytes))
36
37         def http_finished(self, string=""):
38                 print "[DownloadTask].http_finished " + str(string)
39                 Task.processFinished(self, 0)
40
41         def http_failed(self, failure_instance=None, error_message=""):
42                 print "[DownloadTask].http_failed"
43                 if error_message == "" and failure_instance is not None:
44                         error_message = failure_instance.getErrorMessage()
45                         print "[DownloadTask].http_failed " + error_message
46                         Task.processFinished(self, 1)
47
48 downloadManager = JobManager(domain="DownloadManager")
49 downloadManager.in_background = True