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