remove debug
[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                 for x in self.wizard:
139                         if self.wizard[x]["id"] == id:
140                                 return count
141                         count += 1
142                 return 0
143                 
144         def ok(self):
145                 print "OK"
146                 if self.showConfig:
147                         if (self.wizard[self.currStep]["config"]["screen"] != None):
148                                 try: # don't die, if no run() is available
149                                         self.configInstance.run()
150                                 except:
151                                         print "Failed to run configInstance"
152                 
153                 if self.showList:
154                         if (len(self.wizard[self.currStep]["list"]) > 0):
155                                 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
156                                 self.currStep = self.getStepWithID(nextStep)
157
158                 if (self.currStep == self.numSteps): # wizard finished
159                         self.markDone()
160                         self.session.close()
161                 else:
162                         self.runCode(self.wizard[self.currStep]["codeafter"])
163                         self.currStep += 1
164                         self.updateValues()
165                         
166                 print "Now: " + str(self.currStep)
167
168         def keyNumberGlobal(self, number):
169                 if (self.wizard[self.currStep]["config"]["screen"] != None):
170                         self.configInstance.keyNumberGlobal(number)
171                 
172         def left(self):
173                 if (self.wizard[self.currStep]["config"]["screen"] != None):
174                         self.configInstance.keyLeft()
175                 print "left"
176         
177         def right(self):
178                 if (self.wizard[self.currStep]["config"]["screen"] != None):
179                         self.configInstance.keyRight()
180                 print "right"
181
182         def up(self):
183                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
184                                 self["config"].instance.moveSelection(self["config"].instance.moveUp)
185                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
186                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
187                 print "up"
188                 
189         def down(self):
190                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
191                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
192                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
193                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
194                 print "down"
195                 
196         def runCode(self, code):
197                 if code != "":
198                         print code
199                         exec(code)
200                 
201         def updateValues(self):
202                 print "Updating values in step " + str(self.currStep)
203                 
204                 self.condition = True
205                 exec (self.wizard[self.currStep]["condition"])
206                 if self.condition:
207                         if self.showSteps:
208                                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
209                         if self.showStepSlider:
210                                 self["stepslider"].setValue(self.currStep)
211                 
212                         print _(self.wizard[self.currStep]["text"])
213                         self["text"].setText(_(self.wizard[self.currStep]["text"]))
214         
215                         self.runCode(self.wizard[self.currStep]["code"])
216                         
217                         if self.showList:
218                                 self["list"].instance.setZPosition(1)
219                                 self.list = []
220                                 if (len(self.wizard[self.currStep]["list"]) > 0):
221                                         self["list"].instance.setZPosition(2)
222                                         for x in self.wizard[self.currStep]["list"]:
223                                                 self.list.append((_(x[0]), None))
224                                 self["list"].l.setList(self.list)
225         
226                         if self.showConfig:
227                                 self["config"].instance.setZPosition(1)
228                                 if (self.wizard[self.currStep]["config"]["screen"] != None):
229                                         if self.wizard[self.currStep]["config"]["type"] == "standalone":
230                                                 print "Type is standalone"
231                                                 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
232                                         else:
233                                                 self["config"].instance.setZPosition(2)
234                                                 print self.wizard[self.currStep]["config"]["screen"]
235                                                 if self.wizard[self.currStep]["config"]["args"] == None:
236                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
237                                                 else:
238                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
239                                                 self["config"].l.setList(self.configInstance["config"].list)
240                                                 self.configInstance["config"] = self["config"]
241                                 else:
242                                         self["config"].l.setList([])
243                 else: # condition false
244                                 self.currStep += 1
245                                 self.updateValues()
246
247 class WizardManager:
248         def __init__(self):
249                 self.wizards = []
250         
251         def registerWizard(self, wizard, precondition):
252                 self.wizards.append((wizard, precondition))
253         
254         def getWizards(self):
255                 list = []
256                 for x in self.wizards:
257                         if x[1] == 1: # precondition
258                                 list.append(x[0])
259                 return list
260
261 wizardManager = WizardManager()