fix bluescreen when wizard ends
[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.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.stepHistory = []
80
81                 self.wizard = {}
82                 parser = make_parser()
83                 print "Reading " + self.xmlfile
84                 wizardHandler = self.parseWizard(self.wizard)
85                 parser.setContentHandler(wizardHandler)
86                 parser.parse('/usr/share/enigma2/' + self.xmlfile)
87
88                 self.showSteps = showSteps
89                 self.showStepSlider = showStepSlider
90                 self.showList = showList
91                 self.showConfig = showConfig
92
93                 self.numSteps = len(self.wizard)
94                 self.currStep = 1
95
96                 self["text"] = Label()
97
98                 if showConfig:
99                         self["config"] = ConfigList([])
100
101                 if self.showSteps:
102                         self["step"] = Label()
103                 
104                 if self.showStepSlider:
105                         self["stepslider"] = Slider(1, self.numSteps)
106                 
107                 if self.showList:
108                         self.list = []
109                         self["list"] = MenuList(self.list)
110
111                 self.onShown.append(self.updateValues)
112                 
113                 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
114                 {
115                         "ok": self.ok,
116                         "back": self.back,
117                         "left": self.left,
118                         "right": self.right,
119                         "up": self.up,
120                         "down": self.down,
121                         "1": self.keyNumberGlobal,
122                         "2": self.keyNumberGlobal,
123                         "3": self.keyNumberGlobal,
124                         "4": self.keyNumberGlobal,
125                         "5": self.keyNumberGlobal,
126                         "6": self.keyNumberGlobal,
127                         "7": self.keyNumberGlobal,
128                         "8": self.keyNumberGlobal,
129                         "9": self.keyNumberGlobal,
130                         "0": self.keyNumberGlobal
131                 }, -1)
132
133         def back(self):
134                 if len(self.stepHistory) > 1:
135                         self.currStep = self.stepHistory[-2]
136                         self.stepHistory = self.stepHistory[:-2]
137                 if self.currStep < 1:
138                         self.currStep = 1
139                 self.updateValues()
140                 
141         def markDone(self):
142                 pass
143         
144         def getStepWithID(self, id):
145                 count = 0
146                 for x in self.wizard:
147                         if self.wizard[x]["id"] == id:
148                                 return count
149                         count += 1
150                 return 0
151                 
152         def ok(self):
153                 print "OK"
154                 currStep = self.currStep
155                 if self.showConfig:
156                         if (self.wizard[currStep]["config"]["screen"] != None):
157                                 try: # don't die, if no run() is available
158                                         self.configInstance.run()
159                                 except:
160                                         print "Failed to run configInstance"
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         def keyNumberGlobal(self, number):
180                 if (self.wizard[self.currStep]["config"]["screen"] != None):
181                         self.configInstance.keyNumberGlobal(number)
182                 
183         def left(self):
184                 if (self.wizard[self.currStep]["config"]["screen"] != None):
185                         self.configInstance.keyLeft()
186                 print "left"
187         
188         def right(self):
189                 if (self.wizard[self.currStep]["config"]["screen"] != None):
190                         self.configInstance.keyRight()
191                 print "right"
192
193         def up(self):
194                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
195                                 self["config"].instance.moveSelection(self["config"].instance.moveUp)
196                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
197                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
198                 print "up"
199                 
200         def down(self):
201                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
202                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
203                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
204                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
205                 print "down"
206                 
207         def runCode(self, code):
208                 if code != "":
209                         print "code", code
210                         exec(code)
211                 
212         def updateValues(self):
213                 print "Updating values in step " + str(self.currStep)
214                 
215                 self.stepHistory.append(self.currStep)
216                 
217                 self.condition = True
218                 exec (self.wizard[self.currStep]["condition"])
219                 if self.condition:
220                         if self.showSteps:
221                                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
222                         if self.showStepSlider:
223                                 self["stepslider"].setValue(self.currStep)
224                 
225                         print "wizard text", _(self.wizard[self.currStep]["text"])
226                         self["text"].setText(_(self.wizard[self.currStep]["text"]))
227         
228                         self.runCode(self.wizard[self.currStep]["code"])
229                         
230                         if self.showList:
231                                 self["list"].instance.setZPosition(1)
232                                 self.list = []
233                                 if (len(self.wizard[self.currStep]["list"]) > 0):
234                                         self["list"].instance.setZPosition(2)
235                                         for x in self.wizard[self.currStep]["list"]:
236                                                 self.list.append((_(x[0]), None))
237                                 self["list"].l.setList(self.list)
238         
239                         if self.showConfig:
240                                 self["config"].instance.setZPosition(1)
241                                 if (self.wizard[self.currStep]["config"]["screen"] != None):
242                                         if self.wizard[self.currStep]["config"]["type"] == "standalone":
243                                                 print "Type is standalone"
244                                                 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
245                                         else:
246                                                 self["config"].instance.setZPosition(2)
247                                                 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
248                                                 if self.wizard[self.currStep]["config"]["args"] == None:
249                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
250                                                 else:
251                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
252                                                 self["config"].l.setList(self.configInstance["config"].list)
253                                                 self.configInstance["config"] = self["config"]
254                                 else:
255                                         self["config"].l.setList([])
256                 else: # condition false
257                                 self.currStep += 1
258                                 self.updateValues()
259
260 class WizardManager:
261         def __init__(self):
262                 self.wizards = []
263         
264         def registerWizard(self, wizard, precondition):
265                 self.wizards.append((wizard, precondition))
266         
267         def getWizards(self):
268                 list = []
269                 for x in self.wizards:
270                         if x[1] == 1: # precondition
271                                 list.append(x[0])
272                 return list
273
274 wizardManager = WizardManager()