e4f69290a2b117ee00887f20d2624d449b7937f1
[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                         #"cancel": self.keyCancel,
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 ok(self):
104                 print "OK"
105                 if (self.wizard[self.currStep]["config"]["screen"] != None):
106                         try: # don't die, if no run() is available
107                                 self.configInstance.run()
108                         except:
109                                 print "Failed to run configInstance"
110                 
111                 if (len(self.wizard[self.currStep]["list"]) > 0):
112                         nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
113                         if nextStep == "end":
114                                 self.currStep = self.numSteps
115                         elif nextStep == "next":
116                                 pass
117                         else:
118                                 self.currStep = int(nextStep) - 1
119
120                 if (self.currStep == self.numSteps): # wizard finished
121                         config.misc.firstrun.value = 0;
122                         config.misc.firstrun.save()
123                         self.session.close()
124                 else:
125                         self.currStep += 1
126                         self.updateValues()
127                         
128                 print "Now: " + str(self.currStep)
129
130         def keyNumberGlobal(self, number):
131                 if (self.wizard[self.currStep]["config"]["screen"] != None):
132                         self.configInstance.keyNumberGlobal(number)
133                 
134         def left(self):
135                 if (self.wizard[self.currStep]["config"]["screen"] != None):
136                         self.configInstance.keyLeft()
137                 print "left"
138         
139         def right(self):
140                 if (self.wizard[self.currStep]["config"]["screen"] != None):
141                         self.configInstance.keyRight()
142                 print "right"
143
144         def up(self):
145                 if (self.wizard[self.currStep]["config"]["screen"] != None):
146                         self["config"].instance.moveSelection(self["config"].instance.moveUp)
147                 elif (len(self.wizard[self.currStep]["list"]) > 0):
148                         self["list"].instance.moveSelection(self["config"].instance.moveUp)
149                 print "up"
150                 
151         def down(self):
152                 if (self.wizard[self.currStep]["config"]["screen"] != None):
153                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
154                 elif (len(self.wizard[self.currStep]["list"]) > 0):
155                         self["list"].instance.moveSelection(self["config"].instance.moveDown)
156                 print "down"
157                 
158         def updateValues(self):
159                 print "Updating values in step " + str(self.currStep)
160                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
161                 self["stepslider"].setValue(self.currStep)
162
163                 self["text"].setText(_(self.wizard[self.currStep]["text"]))
164
165                 if self.wizard[self.currStep]["code"] != "":
166                         print self.wizard[self.currStep]["code"]
167                         exec(self.wizard[self.currStep]["code"])
168                 
169                 self["list"].instance.setZPosition(1)
170                 self.list = []
171                 if (len(self.wizard[self.currStep]["list"]) > 0):
172                         self["list"].instance.setZPosition(2)
173                         for x in self.wizard[self.currStep]["list"]:
174                                 self.list.append((x[0], None))
175                 self["list"].l.setList(self.list)
176
177                 self["config"].instance.setZPosition(1)
178                 if (self.wizard[self.currStep]["config"]["screen"] != None):
179                         if self.wizard[self.currStep]["config"]["type"] == "standalone":
180                                 print "Type is standalone"
181                                 self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
182                         else:
183                                 self["config"].instance.setZPosition(2)
184                                 print self.wizard[self.currStep]["config"]["screen"]
185                                 if self.wizard[self.currStep]["config"]["args"] == None:
186                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
187                                 else:
188                                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
189                                 self["config"].l.setList(self.configInstance["config"].list)
190                                 self.configInstance["config"] = self["config"]
191                 else:
192                         self["config"].l.setList([])
193
194 class WizardManager:
195         def __init__(self):
196                 self.wizards = []
197         
198         def registerWizard(self, wizard):
199                 self.wizards.append(wizard)
200         
201         def getWizards(self):
202                 if config.misc.firstrun.value:
203                         return self.wizards
204                 else:
205                         return []
206
207 wizardManager = WizardManager()