timer: add name/description, factor out "parse from epg event", fix some cases where...
[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                 
85                 self["actions"] = NumberActionMap(["WizardActions", "NumberActions"],
86                 {
87                         "ok": self.ok,
88                         "back": self.back,
89                         "left": self.left,
90                         "right": self.right,
91                         "up": self.up,
92                         "down": self.down,
93                         "1": self.keyNumberGlobal,
94                         "2": self.keyNumberGlobal,
95                         "3": self.keyNumberGlobal,
96                         "4": self.keyNumberGlobal,
97                         "5": self.keyNumberGlobal,
98                         "6": self.keyNumberGlobal,
99                         "7": self.keyNumberGlobal,
100                         "8": self.keyNumberGlobal,
101                         "9": self.keyNumberGlobal,
102                         "0": self.keyNumberGlobal
103                 }, -1)
104                 
105                 #self["actions"] = HelpableActionMap(self, "OkCancelActions",
106                         #{
107                                 #"ok": (self.ok, _("Close this Screen...")),
108                         #})
109
110         def back(self):
111                 self.currStep -= 1
112                 if self.currStep < 1:
113                         self.currStep = 1
114                 self.updateValues()
115                 
116         def ok(self):
117                 print "OK"
118                 if (self.wizard[self.currStep]["config"]["screen"] != None):
119                         try: # don't die, if no run() is available
120                                 self.configInstance.run()
121                         except:
122                                 print "Failed to run configInstance"
123                 
124                 if (len(self.wizard[self.currStep]["list"]) > 0):
125                         nextStep = self.wizard[self.currStep]["list"][self["list"].l.getCurrentSelectionIndex()][1]
126                         if nextStep == "end":
127                                 self.currStep = self.numSteps
128                         elif nextStep == "next":
129                                 pass
130                         else:
131                                 self.currStep = int(nextStep) - 1
132
133                 if (self.currStep == self.numSteps): # wizard finished
134                         config.misc.firstrun.value = 0;
135                         config.misc.firstrun.save()
136                         self.session.close()
137                 else:
138                         self.currStep += 1
139                         self.updateValues()
140                         
141                 print "Now: " + str(self.currStep)
142
143         def keyNumberGlobal(self, number):
144                 if (self.wizard[self.currStep]["config"]["screen"] != None):
145                         self.configInstance.keyNumberGlobal(number)
146                 
147         def left(self):
148                 if (self.wizard[self.currStep]["config"]["screen"] != None):
149                         self.configInstance.keyLeft()
150                 print "left"
151         
152         def right(self):
153                 if (self.wizard[self.currStep]["config"]["screen"] != None):
154                         self.configInstance.keyRight()
155                 print "right"
156
157         def up(self):
158                 if (self.wizard[self.currStep]["config"]["screen"] != None):
159                         self["config"].instance.moveSelection(self["config"].instance.moveUp)
160                 elif (len(self.wizard[self.currStep]["list"]) > 0):
161                         self["list"].instance.moveSelection(self["list"].instance.moveUp)
162                 print "up"
163                 
164         def down(self):
165                 if (self.wizard[self.currStep]["config"]["screen"] != None):
166                         self["config"].instance.moveSelection(self["config"].instance.moveDown)
167                 elif (len(self.wizard[self.currStep]["list"]) > 0):
168                         self["list"].instance.moveSelection(self["list"].instance.moveDown)
169                 print "down"
170                 
171         def updateValues(self):
172                 print "Updating values in step " + str(self.currStep)
173                 
174                 self.condition = True
175                 exec (self.wizard[self.currStep]["condition"])
176                 if self.condition:
177                         self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
178                         self["stepslider"].setValue(self.currStep)
179                 
180                         print _(self.wizard[self.currStep]["text"])
181                         self["text"].setText(_(self.wizard[self.currStep]["text"]))
182         
183                         if self.wizard[self.currStep]["code"] != "":
184                                 print self.wizard[self.currStep]["code"]
185                                 exec(self.wizard[self.currStep]["code"])
186                         
187                         self["list"].instance.setZPosition(1)
188                         self.list = []
189                         if (len(self.wizard[self.currStep]["list"]) > 0):
190                                 self["list"].instance.setZPosition(2)
191                                 for x in self.wizard[self.currStep]["list"]:
192                                         self.list.append((_(x[0]), None))
193                         self["list"].l.setList(self.list)
194         
195                         self["config"].instance.setZPosition(1)
196                         if (self.wizard[self.currStep]["config"]["screen"] != None):
197                                 if self.wizard[self.currStep]["config"]["type"] == "standalone":
198                                         print "Type is standalone"
199                                         self.session.openWithCallback(self.ok, self.wizard[self.currStep]["config"]["screen"])
200                                 else:
201                                         self["config"].instance.setZPosition(2)
202                                         print self.wizard[self.currStep]["config"]["screen"]
203                                         if self.wizard[self.currStep]["config"]["args"] == None:
204                                                 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"])
205                                         else:
206                                                 self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"]["screen"], eval(self.wizard[self.currStep]["config"]["args"]))
207                                         self["config"].l.setList(self.configInstance["config"].list)
208                                         self.configInstance["config"] = self["config"]
209                         else:
210                                 self["config"].l.setList([])
211                 else: # condition false
212                                 self.currStep += 1
213                                 self.updateValues()
214
215 class WizardManager:
216         def __init__(self):
217                 self.wizards = []
218         
219         def registerWizard(self, wizard):
220                 self.wizards.append(wizard)
221         
222         def getWizards(self):
223                 if config.misc.firstrun.value:
224                         return self.wizards
225                 else:
226                         return []
227
228 wizardManager = WizardManager()