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