immediate take care of changed "visualize rotor movement" option
[enigma2.git] / lib / python / Screens / Wizard.py
1 from Screen import Screen
2
3 import string
4
5 from Screens.HelpMenu import HelpableScreen
6 from Components.config import config
7 from Components.Label import Label
8 from Components.Slider import Slider
9 from Components.ActionMap import NumberActionMap
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
76         def __init__(self, session, showSteps = True, showStepSlider = True, showList = True, showConfig = True):
77                 Screen.__init__(self, session)
78                 HelpableScreen.__init__(self)
79
80                 self.stepHistory = []
81
82                 self.wizard = {}
83                 parser = make_parser()
84                 print "Reading " + self.xmlfile
85                 wizardHandler = self.parseWizard(self.wizard)
86                 parser.setContentHandler(wizardHandler)
87                 parser.parse('/usr/share/enigma2/' + self.xmlfile)
88
89                 self.showSteps = showSteps
90                 self.showStepSlider = showStepSlider
91                 self.showList = showList
92                 self.showConfig = showConfig
93
94                 self.numSteps = len(self.wizard)
95                 self.currStep = 1
96
97                 self["text"] = Label()
98
99                 if showConfig:
100                         self["config"] = ConfigList([])
101
102                 if self.showSteps:
103                         self["step"] = Label()
104                 
105                 if self.showStepSlider:
106                         self["stepslider"] = Slider(1, self.numSteps)
107                 
108                 if self.showList:
109                         self.list = []
110                         self["list"] = MenuList(self.list)
111
112                 self.onShown.append(self.updateValues)
113
114                 self.configInstance = None
115                 
116                 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
117                 {
118                         "ok": self.ok,
119                         "back": self.back,
120                         "left": self.left,
121                         "right": self.right,
122                         "up": self.up,
123                         "down": self.down,
124                         "1": self.keyNumberGlobal,
125                         "2": self.keyNumberGlobal,
126                         "3": self.keyNumberGlobal,
127                         "4": self.keyNumberGlobal,
128                         "5": self.keyNumberGlobal,
129                         "6": self.keyNumberGlobal,
130                         "7": self.keyNumberGlobal,
131                         "8": self.keyNumberGlobal,
132                         "9": self.keyNumberGlobal,
133                         "0": self.keyNumberGlobal
134                 }, -1)
135
136         def back(self):
137                 if len(self.stepHistory) > 1:
138                         self.currStep = self.stepHistory[-2]
139                         self.stepHistory = self.stepHistory[:-2]
140                 if self.currStep < 1:
141                         self.currStep = 1
142                 self.updateValues()
143                 
144         def markDone(self):
145                 pass
146         
147         def getStepWithID(self, id):
148                 count = 0
149                 for x in self.wizard:
150                         if self.wizard[x]["id"] == id:
151                                 return count
152                         count += 1
153                 return 0
154
155         def finished(self, *args, **kwargs):
156                 print "finished"
157                 currStep = self.currStep
158
159                 if self.updateValues not in self.onShown:
160                         self.onShown.append(self.updateValues)
161
162                 if self.showList:
163                         if (len(self.wizard[currStep]["list"]) > 0):
164                                 nextStep = self.wizard[currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
165                                 self.currStep = self.getStepWithID(nextStep)
166
167                 if (currStep == self.numSteps): # wizard finished
168                         self.markDone()
169                         self.close()
170                 else:
171                         self.runCode(self.wizard[currStep]["codeafter"])
172                         if self.wizard[currStep]["nextstep"] is not None:
173                                 self.currStep = self.getStepWithID(self.wizard[currStep]["nextstep"])
174                         self.currStep += 1
175                         self.updateValues()
176
177                 print "Now: " + str(self.currStep)
178
179
180         def ok(self):
181                 print "OK"
182                 currStep = self.currStep
183                 
184                 if self.showConfig:
185                         if (self.wizard[currStep]["config"]["screen"] != None):
186                                 # TODO: don't die, if no run() is available
187                                 # there was a try/except here, but i can't see a reason
188                                 # for this. If there is one, please do a more specific check
189                                 # and/or a comment in which situation there is no run()
190                                 if callable(getattr(self.configInstance, "runAsync", None)):
191                                         self.onShown.remove(self.updateValues)
192                                         self.configInstance.runAsync(self.finished)
193                                         return
194                                 else:
195                                         self.configInstance.run()
196                 self.finished()
197
198         def keyNumberGlobal(self, number):
199                 if (self.wizard[self.currStep]["config"]["screen"] != None):
200                         self.configInstance.keyNumberGlobal(number)
201                 
202         def left(self):
203                 if (self.wizard[self.currStep]["config"]["screen"] != None):
204                         self.configInstance.keyLeft()
205                 print "left"
206         
207         def right(self):
208                 if (self.wizard[self.currStep]["config"]["screen"] != None):
209                         self.configInstance.keyRight()
210                 print "right"
211
212         def up(self):
213                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
214                                 self["config"].instance.moveSelection(self["config"].instance.moveUp)
215                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
216                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
217                 print "up"
218                 
219         def down(self):
220                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
221                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
222                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
223                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
224                 print "down"
225                 
226         def runCode(self, code):
227                 if code != "":
228                         print "code", code
229                         exec(code)
230                 
231         def updateValues(self):
232                 print "Updating values in step " + str(self.currStep)
233                 
234                 self.stepHistory.append(self.currStep)
235                 
236                 if self.configInstance is not None:
237                         del self.configInstance["config"]
238                         self.configInstance.doClose()
239                         self.configInstance = None
240                 
241                 self.condition = True
242                 exec (self.wizard[self.currStep]["condition"])
243                 if self.condition:
244                         if self.showSteps:
245                                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
246                         if self.showStepSlider:
247                                 self["stepslider"].setValue(self.currStep)
248                 
249                         print "wizard text", _(self.wizard[self.currStep]["text"])
250                         self["text"].setText(_(self.wizard[self.currStep]["text"]))
251         
252                         self.runCode(self.wizard[self.currStep]["code"])
253                         
254                         if self.showList:
255                                 self["list"].instance.setZPosition(1)
256                                 self.list = []
257                                 if (len(self.wizard[self.currStep]["list"]) > 0):
258                                         self["list"].instance.setZPosition(2)
259                                         for x in self.wizard[self.currStep]["list"]:
260                                                 self.list.append((_(x[0]), None))
261                                 self["list"].l.setList(self.list)
262                                 self["list"].moveToIndex(0)
263         
264                         if self.showConfig:
265                                 self["config"].instance.setZPosition(1)
266                                 if (self.wizard[self.currStep]["config"]["screen"] != None):
267                                         if self.wizard[self.currStep]["config"]["type"] == "standalone":
268                                                 print "Type is standalone"
269                                                 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
270                                         else:
271                                                 self["config"].instance.setZPosition(2)
272                                                 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
273                                                 if self.wizard[self.currStep]["config"]["args"] == None:
274                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
275                                                 else:
276                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
277                                                 self["config"].l.setList(self.configInstance["config"].list)
278                                                 self.configInstance["config"].destroy()
279                                                 self.configInstance["config"] = self["config"]
280                                 else:
281                                         self["config"].l.setList([])
282                 else: # condition false
283                                 self.currStep += 1
284                                 self.updateValues()
285
286 class WizardManager:
287         def __init__(self):
288                 self.wizards = []
289         
290         def registerWizard(self, wizard, precondition):
291                 self.wizards.append((wizard, precondition))
292         
293         def getWizards(self):
294                 list = []
295                 for x in self.wizards:
296                         if x[1] == 1: # precondition
297                                 list.append(x[0])
298                 return list
299
300 wizardManager = WizardManager()