- new automatic scan - scans everything the NIM can handle
[vuplus_dvbapp] / lib / python / Screens / Wizard.py
1 from Screen import Screen
2
3 import string
4
5 from Screens.HelpMenu import HelpableScreen
6 from Components.Label import Label
7 from Components.Slider import Slider
8 from Components.ActionMap import HelpableActionMap, NumberActionMap
9 from Components.Pixmap import *
10 from Components.MenuList import MenuList
11 from Components.ConfigList import ConfigList
12
13 from xml.sax import make_parser
14 from xml.sax.handler import ContentHandler
15
16 class Wizard(Screen, HelpableScreen):
17
18         class parseWizard(ContentHandler):
19                 def __init__(self, wizard):
20                         self.isPointsElement, self.isReboundsElement = 0, 0
21                         self.wizard = wizard
22                         self.currContent = ""
23                         self.lastStep = 0
24                 
25                 def startElement(self, name, attrs):
26                         print "startElement", name
27                         self.currContent = name
28                         if (name == "step"):
29                                 self.lastStep += 1
30                                 if attrs.has_key('id'):
31                                         id = str(attrs.get('id'))
32                                 else:
33                                         id = ""
34                                 if attrs.has_key('nextstep'):
35                                         nextstep = str(attrs.get('nextstep'))
36                                 else:
37                                         nextstep = None
38                                 self.wizard[self.lastStep] = {"id": id, "condition": "", "text": "", "list": [], "config": {"screen": None, "args": None, "type": "" }, "code": "", "codeafter": "", "nextstep": nextstep}
39                         elif (name == "text"):
40                                 self.wizard[self.lastStep]["text"] = string.replace(str(attrs.get('value')), "\\n", "\n")
41                         elif (name == "listentry"):
42                                 self.wizard[self.lastStep]["list"].append((str(attrs.get('caption')), str(attrs.get('step'))))
43                         elif (name == "config"):
44                                 exec "from Screens." + str(attrs.get('module')) + " import *"
45                                 self.wizard[self.lastStep]["config"]["screen"] = eval(str(attrs.get('screen')))
46                                 if (attrs.has_key('args')):
47                                         print "has args"
48                                         self.wizard[self.lastStep]["config"]["args"] = str(attrs.get('args'))
49                                 self.wizard[self.lastStep]["config"]["type"] = str(attrs.get('type'))
50                         elif (name == "code"):
51                                 if attrs.has_key('pos') and str(attrs.get('pos')) == "after":
52                                         self.codeafter = True
53                                 else:
54                                         self.codeafter = False
55                         elif (name == "condition"):
56                                 pass
57                 def endElement(self, name):
58                         self.currContent = ""
59                         if name == 'code':
60                                 if self.codeafter:
61                                         self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"].strip()
62                                 else:
63                                         self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
64                         elif name == 'condition':
65                                 self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"].strip()
66                                                                 
67                 def characters(self, ch):
68                         if self.currContent == "code":
69                                 if self.codeafter:
70                                         self.wizard[self.lastStep]["codeafter"] = self.wizard[self.lastStep]["codeafter"] + ch
71                                 else:
72                                         self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
73                         elif self.currContent == "condition":
74                                  self.wizard[self.lastStep]["condition"] = self.wizard[self.lastStep]["condition"] + ch
75         def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
76                 Screen.__init__(self, session)
77                 HelpableScreen.__init__(self)
78
79                 self.wizard = {}
80                 parser = make_parser()
81                 print "Reading " + self.xmlfile
82                 wizardHandler = self.parseWizard(self.wizard)
83                 parser.setContentHandler(wizardHandler)
84                 parser.parse('/usr/share/enigma2/' + self.xmlfile)
85
86                 self.showSteps = showSteps
87                 self.showStepSlider = showStepSlider
88                 self.showList = showList
89                 self.showConfig = showConfig
90
91                 self.numSteps = len(self.wizard)
92                 self.currStep = 1
93
94                 self["text"] = Label()
95
96                 if showConfig:
97                         self["config"] = ConfigList([])
98
99                 if self.showSteps:
100                         self["step"] = Label()
101                 
102                 if self.showStepSlider:
103                         self["stepslider"] = Slider(1, self.numSteps)
104                 
105                 if self.showList:
106                         self.list = []
107                         self["list"] = MenuList(self.list)
108
109                 self.onShown.append(self.updateValues)
110                 
111                 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
112                 {
113                         "ok": self.ok,
114                         "back": self.back,
115                         "left": self.left,
116                         "right": self.right,
117                         "up": self.up,
118                         "down": self.down,
119                         "1": self.keyNumberGlobal,
120                         "2": self.keyNumberGlobal,
121                         "3": self.keyNumberGlobal,
122                         "4": self.keyNumberGlobal,
123                         "5": self.keyNumberGlobal,
124                         "6": self.keyNumberGlobal,
125                         "7": self.keyNumberGlobal,
126                         "8": self.keyNumberGlobal,
127                         "9": self.keyNumberGlobal,
128                         "0": self.keyNumberGlobal
129                 }, -1)
130
131         def back(self):
132                 self.currStep -= 1
133                 if self.currStep < 1:
134                         self.currStep = 1
135                 self.updateValues()
136                 
137         def markDone(self):
138                 pass
139         
140         def getStepWithID(self, id):
141                 count = 0
142                 for x in self.wizard:
143                         if self.wizard[x]["id"] == id:
144                                 return count
145                         count += 1
146                 return 0
147                 
148         def ok(self):
149                 print "OK"
150                 if self.showConfig:
151                         if (self.wizard[self.currStep]["config"]["screen"] != None):
152                                 try: # don't die, if no run() is available
153                                         self.configInstance.run()
154                                 except:
155                                         print "Failed to run configInstance"
156                 
157                 if self.showList:
158                         if (len(self.wizard[self.currStep]["list"]) > 0):
159                                 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
160                                 self.currStep = self.getStepWithID(nextStep)
161
162                 if (self.currStep == self.numSteps): # wizard finished
163                         self.markDone()
164                         self.session.close()
165                 else:
166                         self.runCode(self.wizard[self.currStep]["codeafter"])
167                         if self.wizard[self.currStep]["nextstep"] is not None:
168                                 self.currStep = self.getStepWithID(self.wizard[self.currStep]["nextstep"])
169                         self.currStep += 1
170                         self.updateValues()
171                         
172                 print "Now: " + str(self.currStep)
173
174         def keyNumberGlobal(self, number):
175                 if (self.wizard[self.currStep]["config"]["screen"] != None):
176                         self.configInstance.keyNumberGlobal(number)
177                 
178         def left(self):
179                 if (self.wizard[self.currStep]["config"]["screen"] != None):
180                         self.configInstance.keyLeft()
181                 print "left"
182         
183         def right(self):
184                 if (self.wizard[self.currStep]["config"]["screen"] != None):
185                         self.configInstance.keyRight()
186                 print "right"
187
188         def up(self):
189                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
190                                 self["config"].instance.moveSelection(self["config"].instance.moveUp)
191                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
192                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
193                 print "up"
194                 
195         def down(self):
196                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
197                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
198                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
199                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
200                 print "down"
201                 
202         def runCode(self, code):
203                 if code != "":
204                         print "code", code
205                         exec(code)
206                 
207         def updateValues(self):
208                 print "Updating values in step " + str(self.currStep)
209                 
210                 self.condition = True
211                 exec (self.wizard[self.currStep]["condition"])
212                 if self.condition:
213                         if self.showSteps:
214                                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
215                         if self.showStepSlider:
216                                 self["stepslider"].setValue(self.currStep)
217                 
218                         print "wizard text", _(self.wizard[self.currStep]["text"])
219                         self["text"].setText(_(self.wizard[self.currStep]["text"]))
220         
221                         self.runCode(self.wizard[self.currStep]["code"])
222                         
223                         if self.showList:
224                                 self["list"].instance.setZPosition(1)
225                                 self.list = []
226                                 if (len(self.wizard[self.currStep]["list"]) > 0):
227                                         self["list"].instance.setZPosition(2)
228                                         for x in self.wizard[self.currStep]["list"]:
229                                                 self.list.append((_(x[0]), None))
230                                 self["list"].l.setList(self.list)
231         
232                         if self.showConfig:
233                                 self["config"].instance.setZPosition(1)
234                                 if (self.wizard[self.currStep]["config"]["screen"] != None):
235                                         if self.wizard[self.currStep]["config"]["type"] == "standalone":
236                                                 print "Type is standalone"
237                                                 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
238                                         else:
239                                                 self["config"].instance.setZPosition(2)
240                                                 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
241                                                 if self.wizard[self.currStep]["config"]["args"] == None:
242                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
243                                                 else:
244                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
245                                                 self["config"].l.setList(self.configInstance["config"].list)
246                                                 self.configInstance["config"] = self["config"]
247                                 else:
248                                         self["config"].l.setList([])
249                 else: # condition false
250                                 self.currStep += 1
251                                 self.updateValues()
252
253 class WizardManager:
254         def __init__(self):
255                 self.wizards = []
256         
257         def registerWizard(self, wizard, precondition):
258                 self.wizards.append((wizard, precondition))
259         
260         def getWizards(self):
261                 list = []
262                 for x in self.wizards:
263                         if x[1] == 1: # precondition
264                                 list.append(x[0])
265                 return list
266
267 wizardManager = WizardManager()