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