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