nicer code
[enigma2.git] / lib / python / Screens / Ipkg.py
1 from Components.ActionMap import ActionMap
2 from Components.Ipkg import IpkgComponent
3 from Components.Label import Label
4 from Components.Slider import Slider
5 from Screens.Screen import Screen
6 from enigma import eTimer
7
8 class Ipkg(Screen):
9         def __init__(self, session, cmdList = []):
10                 Screen.__init__(self, session)
11                 
12                 self.cmdList = cmdList
13                 
14                 self.slider = Slider(0, len(cmdList))
15                 self["slider"] = self.slider
16                 self.activityslider = Slider(0, 100)
17                 self["activityslider"] = self.activityslider
18                 self.status = Label(_("Preparing... Please wait"))
19                 self["status"] = self.status
20                 self.package = Label()
21                 self["package"] = self.package
22                 
23                 self.packages = 0
24                 self.error = 0
25                 
26                 self.activity = 0
27                 self.activityTimer = eTimer()
28                 self.activityTimer.timeout.get().append(self.doActivityTimer)
29                 #self.activityTimer.start(100, False)
30                                 
31                 self.ipkg = IpkgComponent()
32                 self.ipkg.addCallback(self.ipkgCallback)
33                 
34                 self.runningCmd = None
35                 self.runNextCmd()
36                 
37                 self["actions"] = ActionMap(["WizardActions"], 
38                 {
39                         "ok": self.exit, 
40                         "back": self.exit
41                 }, -1)
42                 
43         def runNextCmd(self):
44                 if self.runningCmd is None:
45                         self.runningCmd = 0
46                 else:
47                         self.runningCmd += 1
48                 print len(self.cmdList), self.runningCmd
49                 if len(self.cmdList) - 1 < self.runningCmd:
50                         self.activityslider.setValue(0)
51                         self.slider.setValue(len(self.cmdList))
52                                 
53                         self.package.setText("")
54                         self.status.setText(_("Done - Installed or upgraded %d packages with %d errors") % (self.packages, self.error))
55                         return False
56                 else:
57                         cmd = self.cmdList[self.runningCmd]
58                         self.slider.setValue(self.runningCmd)
59                         self.ipkg.startCmd(cmd[0], args = cmd[1])
60                         self.startActivityTimer()
61                         
62         def doActivityTimer(self):
63                 if not self.ipkg.isRunning():
64                         self.stopActivityTimer()
65                 else:
66                         self.activity += 1
67                         if self.activity == 100:
68                                 self.activity = 0
69                         self.activityslider.setValue(self.activity)
70                 
71         def startActivityTimer(self):
72                 self.activityTimer.start(100, False)
73                 
74         def stopActivityTimer(self):
75                 self.activityTimer.stop()
76                 
77         def ipkgCallback(self, event, param):
78                 if event == IpkgComponent.EVENT_DOWNLOAD:
79                         self.status.setText(_("Downloading"))
80                 elif event == IpkgComponent.EVENT_UPGRADE:
81                         if self.sliderPackages.has_key(param):
82                                 self.slider.setValue(self.sliderPackages[param])
83                         self.package.setText(param)
84                         self.status.setText(_("Upgrading"))
85                         self.packages += 1
86                 elif event == IpkgComponent.EVENT_INSTALL:
87                         self.package.setText(param)
88                         self.status.setText(_("Installing"))
89                         self.packages += 1
90                 elif event == IpkgComponent.EVENT_CONFIGURING:
91                         self.package.setText(param)
92                         self.status.setText(_("Configuring"))
93                 elif event == IpkgComponent.EVENT_ERROR:
94                         self.error += 1
95                 elif event == IpkgComponent.EVENT_DONE:
96                         self.runNextCmd()
97         
98         def exit(self):
99                 if not self.ipkg.isRunning():
100                         self.close()