some work on the 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):
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
69                 self.numSteps = len(self.wizard)
70                 self.currStep = 1
71
72                 self["text"] = Label()
73
74                 self["config"] = ConfigList([])
75
76                 if self.showSteps:
77                         self["step"] = Label()
78                 
79                 if self.showStepSlider:
80                         self["stepslider"] = Slider(1, self.numSteps)
81                 
82                 self.list = []
83                 self["list"] = MenuList(self.list)
84
85                 self.onShown.append(self.updateValues)
86                 
87                 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
88                 {
89                         "ok": self.ok,
90                         "back": self.back,
91                         "left": self.left,
92                         "right": self.right,
93                         "up": self.up,
94                         "down": self.down,
95                         "1": self.keyNumberGlobal,
96                         "2": self.keyNumberGlobal,
97                         "3": self.keyNumberGlobal,
98                         "4": self.keyNumberGlobal,
99                         "5": self.keyNumberGlobal,
100                         "6": self.keyNumberGlobal,
101                         "7": self.keyNumberGlobal,
102                         "8": self.keyNumberGlobal,
103                         "9": self.keyNumberGlobal,
104                         "0": self.keyNumberGlobal
105                 }, -1)
106                 
107                 #self["actions"] = HelpableActionMap(self, "OkCancelActions",
108                         #{
109                                 #"ok": (self.ok, _("Close this Screen...")),
110                         #})
111
112         def back(self):
113                 self.currStep -= 1
114                 if self.currStep < 1:
115                         self.currStep = 1
116                 self.updateValues()
117                 
118         def markDone(self):
119                 pass
120                 
121         def ok(self):
122                 print "OK"
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 (len(self.wizard[self.currStep]["list"]) > 0):
130                         nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
131                         if nextStep == "end":
132                                 self.currStep = self.numSteps
133                         elif nextStep == "next":
134                                 pass
135                         else:
136                                 self.currStep = int(nextStep) - 1
137
138                 if (self.currStep == self.numSteps): # wizard finished
139                         self.markDone()
140                         self.session.close()
141                 else:
142                         self.currStep += 1
143                         self.updateValues()
144                         
145                 print "Now: " + str(self.currStep)
146
147         def keyNumberGlobal(self, number):
148                 if (self.wizard[self.currStep]["config"]["screen"] != None):
149                         self.configInstance.keyNumberGlobal(number)
150                 
151         def left(self):
152                 if (self.wizard[self.currStep]["config"]["screen"] != None):
153                         self.configInstance.keyLeft()
154                 print "left"
155         
156         def right(self):
157                 if (self.wizard[self.currStep]["config"]["screen"] != None):
158                         self.configInstance.keyRight()
159                 print "right"
160
161         def up(self):
162                 if (self.wizard[self.currStep]["config"]["screen"] != None):
163                         self["config"].instance.moveSelection(self["config"].instance.moveUp)
164                 elif (len(self.wizard[self.currStep]["list"]) > 0):
165                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
166                 print "up"
167                 
168         def down(self):
169                 if (self.wizard[self.currStep]["config"]["screen"] != None):
170                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
171                 elif (len(self.wizard[self.currStep]["list"]) > 0):
172                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
173                 print "down"
174                 
175         def updateValues(self):
176                 print "Updating values in step " + str(self.currStep)
177                 
178                 self.condition = True
179                 exec (self.wizard[self.currStep]["condition"])
180                 if self.condition:
181                         if self.showSteps:
182                                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
183                         if self.showStepSlider:
184                                 self["stepslider"].setValue(self.currStep)
185                 
186                         print _(self.wizard[self.currStep]["text"])
187                         self["text"].setText(_(self.wizard[self.currStep]["text"]))
188         
189                         if self.wizard[self.currStep]["code"] != "":
190                                 print self.wizard[self.currStep]["code"]
191                                 exec(self.wizard[self.currStep]["code"])
192                         
193                         self["list"].instance.setZPosition(1)
194                         self.list = []
195                         if (len(self.wizard[self.currStep]["list"]) > 0):
196                                 self["list"].instance.setZPosition(2)
197                                 for x in self.wizard[self.currStep]["list"]:
198                                         self.list.append((_(x[0]), None))
199                         self["list"].l.setList(self.list)
200         
201                         self["config"].instance.setZPosition(1)
202                         if (self.wizard[self.currStep]["config"]["screen"] != None):
203                                 if self.wizard[self.currStep]["config"]["type"] == "standalone":
204                                         print "Type is standalone"
205                                         self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
206                                 else:
207                                         self["config"].instance.setZPosition(2)
208                                         print self.wizard[self.currStep]["config"]["screen"]
209                                         if self.wizard[self.currStep]["config"]["args"] == None:
210                                                 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
211                                         else:
212                                                 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
213                                         self["config"].l.setList(self.configInstance["config"].list)
214                                         self.configInstance["config"] = self["config"]
215                         else:
216                                 self["config"].l.setList([])
217                 else: # condition false
218                                 self.currStep += 1
219                                 self.updateValues()
220
221 class WizardManager:
222         def __init__(self):
223                 self.wizards = []
224         
225         def registerWizard(self, wizard, precondition):
226                 self.wizards.append((wizard, precondition))
227         
228         def getWizards(self):
229                 list = []
230                 for x in self.wizards:
231                         if x[1] == 1: # precondition
232                                 list.append(x[0])
233                 return list
234
235 wizardManager = WizardManager()