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