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