whitespace cleanup
[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 ok(self):
157                 print "OK"
158                 currStep = self.currStep
159                 
160                 if self.showConfig:
161                         if (self.wizard[currStep]["config"]["screen"] != None):
162                                 # TODO: don't die, if no run() is available
163                                 # there was a try/except here, but i can't see a reason
164                                 # for this. If there is one, please do a more specific check
165                                 # and/or a comment in which situation there is no run()
166                                 self.configInstance.run()
167                 
168                 if self.showList:
169                         if (len(self.wizard[currStep]["list"]) > 0):
170                                 nextStep = self.wizard[currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
171                                 self.currStep = self.getStepWithID(nextStep)
172
173                 if (currStep == self.numSteps): # wizard finished
174                         self.markDone()
175                         self.close()
176                 else:
177                         self.runCode(self.wizard[currStep]["codeafter"])
178                         if self.wizard[currStep]["nextstep"] is not None:
179                                 self.currStep = self.getStepWithID(self.wizard[currStep]["nextstep"])
180                         self.currStep += 1
181                         self.updateValues()
182                         
183                 print "Now: " + str(self.currStep)
184
185         def keyNumberGlobal(self, number):
186                 if (self.wizard[self.currStep]["config"]["screen"] != None):
187                         self.configInstance.keyNumberGlobal(number)
188                 
189         def left(self):
190                 if (self.wizard[self.currStep]["config"]["screen"] != None):
191                         self.configInstance.keyLeft()
192                 print "left"
193         
194         def right(self):
195                 if (self.wizard[self.currStep]["config"]["screen"] != None):
196                         self.configInstance.keyRight()
197                 print "right"
198
199         def up(self):
200                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
201                                 self["config"].instance.moveSelection(self["config"].instance.moveUp)
202                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
203                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
204                 print "up"
205                 
206         def down(self):
207                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
208                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
209                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
210                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
211                 print "down"
212                 
213         def runCode(self, code):
214                 if code != "":
215                         print "code", code
216                         exec(code)
217                 
218         def updateValues(self):
219                 print "Updating values in step " + str(self.currStep)
220                 
221                 self.stepHistory.append(self.currStep)
222                 
223                 if self.configInstance is not None:
224                         del self.configInstance["config"]
225                         self.configInstance.doClose()
226                         self.configInstance = None
227                 
228                 self.condition = True
229                 exec (self.wizard[self.currStep]["condition"])
230                 if self.condition:
231                         if self.showSteps:
232                                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
233                         if self.showStepSlider:
234                                 self["stepslider"].setValue(self.currStep)
235                 
236                         print "wizard text", _(self.wizard[self.currStep]["text"])
237                         self["text"].setText(_(self.wizard[self.currStep]["text"]))
238         
239                         self.runCode(self.wizard[self.currStep]["code"])
240                         
241                         if self.showList:
242                                 self["list"].instance.setZPosition(1)
243                                 self.list = []
244                                 if (len(self.wizard[self.currStep]["list"]) > 0):
245                                         self["list"].instance.setZPosition(2)
246                                         for x in self.wizard[self.currStep]["list"]:
247                                                 self.list.append((_(x[0]), None))
248                                 self["list"].l.setList(self.list)
249                                 self["list"].moveToIndex(0)
250         
251                         if self.showConfig:
252                                 self["config"].instance.setZPosition(1)
253                                 if (self.wizard[self.currStep]["config"]["screen"] != None):
254                                         if self.wizard[self.currStep]["config"]["type"] == "standalone":
255                                                 print "Type is standalone"
256                                                 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
257                                         else:
258                                                 self["config"].instance.setZPosition(2)
259                                                 print "wizard screen", self.wizard[self.currStep]["config"]["screen"]
260                                                 if self.wizard[self.currStep]["config"]["args"] == None:
261                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
262                                                 else:
263                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
264                                                 self["config"].l.setList(self.configInstance["config"].list)
265                                                 self.configInstance["config"].destroy()
266                                                 self.configInstance["config"] = self["config"]
267                                 else:
268                                         self["config"].l.setList([])
269                 else: # condition false
270                                 self.currStep += 1
271                                 self.updateValues()
272
273 class WizardManager:
274         def __init__(self):
275                 self.wizards = []
276         
277         def registerWizard(self, wizard, precondition):
278                 self.wizards.append((wizard, precondition))
279         
280         def getWizards(self):
281                 list = []
282                 for x in self.wizards:
283                         if x[1] == 1: # precondition
284                                 list.append(x[0])
285                 return list
286
287 wizardManager = WizardManager()