add ability to remove list and config from a wizard
[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": ""}
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                 #self["actions"] = HelpableActionMap(self, "OkCancelActions",
112                         #{
113                                 #"ok": (self.ok, _("Close this Screen...")),
114                         #})
115
116         def back(self):
117                 self.currStep -= 1
118                 if self.currStep < 1:
119                         self.currStep = 1
120                 self.updateValues()
121                 
122         def markDone(self):
123                 pass
124                 
125         def ok(self):
126                 print "OK"
127                 if self.showConfig:
128                         if (self.wizard[self.currStep]["config"]["screen"] != None):
129                                 try: # don't die, if no run() is available
130                                         self.configInstance.run()
131                                 except:
132                                         print "Failed to run configInstance"
133                 
134                 if self.showList:
135                         if (len(self.wizard[self.currStep]["list"]) > 0):
136                                 nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
137                                 if nextStep == "end":
138                                         self.currStep = self.numSteps
139                                 elif nextStep == "next":
140                                         pass
141                                 else:
142                                         self.currStep = int(nextStep) - 1
143
144                 if (self.currStep == self.numSteps): # wizard finished
145                         self.markDone()
146                         self.session.close()
147                 else:
148                         self.currStep += 1
149                         self.updateValues()
150                         
151                 print "Now: " + str(self.currStep)
152
153         def keyNumberGlobal(self, number):
154                 if (self.wizard[self.currStep]["config"]["screen"] != None):
155                         self.configInstance.keyNumberGlobal(number)
156                 
157         def left(self):
158                 if (self.wizard[self.currStep]["config"]["screen"] != None):
159                         self.configInstance.keyLeft()
160                 print "left"
161         
162         def right(self):
163                 if (self.wizard[self.currStep]["config"]["screen"] != None):
164                         self.configInstance.keyRight()
165                 print "right"
166
167         def up(self):
168                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
169                                 self["config"].instance.moveSelection(self["config"].instance.moveUp)
170                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
171                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
172                 print "up"
173                 
174         def down(self):
175                 if (self.showConfig and self.wizard[self.currStep]["config"]["screen"] != None):
176                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
177                 elif (self.showList and len(self.wizard[self.currStep]["list"]) > 0):
178                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
179                 print "down"
180                 
181         def updateValues(self):
182                 print "Updating values in step " + str(self.currStep)
183                 
184                 self.condition = True
185                 exec (self.wizard[self.currStep]["condition"])
186                 if self.condition:
187                         if self.showSteps:
188                                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
189                         if self.showStepSlider:
190                                 self["stepslider"].setValue(self.currStep)
191                 
192                         print _(self.wizard[self.currStep]["text"])
193                         self["text"].setText(_(self.wizard[self.currStep]["text"]))
194         
195                         if self.wizard[self.currStep]["code"] != "":
196                                 print self.wizard[self.currStep]["code"]
197                                 exec(self.wizard[self.currStep]["code"])
198                         
199                         if self.showList:
200                                 self["list"].instance.setZPosition(1)
201                                 self.list = []
202                                 if (len(self.wizard[self.currStep]["list"]) > 0):
203                                         self["list"].instance.setZPosition(2)
204                                         for x in self.wizard[self.currStep]["list"]:
205                                                 self.list.append((_(x[0]), None))
206                                 self["list"].l.setList(self.list)
207         
208                         if self.showConfig:
209                                 self["config"].instance.setZPosition(1)
210                                 if (self.wizard[self.currStep]["config"]["screen"] != None):
211                                         if self.wizard[self.currStep]["config"]["type"] == "standalone":
212                                                 print "Type is standalone"
213                                                 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
214                                         else:
215                                                 self["config"].instance.setZPosition(2)
216                                                 print self.wizard[self.currStep]["config"]["screen"]
217                                                 if self.wizard[self.currStep]["config"]["args"] == None:
218                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
219                                                 else:
220                                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
221                                                 self["config"].l.setList(self.configInstance["config"].list)
222                                                 self.configInstance["config"] = self["config"]
223                                 else:
224                                         self["config"].l.setList([])
225                 else: # condition false
226                                 self.currStep += 1
227                                 self.updateValues()
228
229 class WizardManager:
230         def __init__(self):
231                 self.wizards = []
232         
233         def registerWizard(self, wizard, precondition):
234                 self.wizards.append((wizard, precondition))
235         
236         def getWizards(self):
237                 list = []
238                 for x in self.wizards:
239                         if x[1] == 1: # precondition
240                                 list.append(x[0])
241                 return list
242
243 wizardManager = WizardManager()