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