some code cleanup
[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
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 WelcomeWizard(Screen, HelpableScreen):
18
19         skin = """
20                 <screen position="0,0" size="720,560" title="Welcome..." flags="wfNoBorder" >
21                         <widget name="text" position="50,100" size="440,200" font="Arial;23" />
22                         <widget name="list" position="50,300" size="440,200" />
23                         <widget name="config" position="50,300" zPosition="100" size="440,200" />                       
24                         <widget name="step" position="50,50" size="440,25" font="Arial;23" />
25                         <widget name="stepslider" position="50,500" zPosition="1" size="440,20" backgroundColor="dark" />
26                         <widget name="rc" pixmap="/usr/share/enigma2/rc.png" position="500,600" size="154,475" transparent="1" alphatest="on"/>
27                         <widget name="arrowdown" pixmap="/usr/share/enigma2/arrowdown.png" position="0,0" zPosition="1" size="37,70" transparent="1" alphatest="on"/>
28                         <widget name="arrowup" pixmap="/usr/share/enigma2/arrowup.png" position="-100,-100" zPosition="1" size="37,70" transparent="1" alphatest="on"/>
29                 </screen>"""
30
31         class parseWizard(ContentHandler):
32                 def __init__(self, wizard):
33                         self.isPointsElement, self.isReboundsElement = 0, 0
34                         self.wizard = wizard
35                         self.currContent = ""
36                 
37                 def startElement(self, name, attrs):
38                         self.currContent = name
39                         if (name == "step"):
40                                 self.lastStep = int(attrs.get('number'))
41                                 self.wizard[self.lastStep] = {"text": "", "list": [], "config": None, "code": ""}
42                         elif (name == "text"):
43                                 self.wizard[self.lastStep]["text"] = str(attrs.get('value'))
44                         elif (name == "listentry"):
45                                 self.wizard[self.lastStep]["list"].append(str(attrs.get('caption')))
46                         elif (name == "config"):
47                                 exec "from Screens." + str(attrs.get('module')) + " import *"
48                                 self.wizard[self.lastStep]["config"] = eval(str(attrs.get('screen')))
49                                 
50                 def endElement(self, name):
51                         self.currContent = ""
52                         if name == 'code':
53                                 self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"].strip()
54                                 
55                 def characters(self, ch):
56                         if self.currContent == "code":
57                                  self.wizard[self.lastStep]["code"] = self.wizard[self.lastStep]["code"] + ch
58                                 
59         def __init__(self, session):
60                 self.skin = WelcomeWizard.skin
61
62                 Screen.__init__(self, session)
63                 HelpableScreen.__init__(self)
64
65                 self.wizard = {}
66                 parser = make_parser()
67                 print "Reading startwizard.xml"
68                 wizardHandler = self.parseWizard(self.wizard)
69                 parser.setContentHandler(wizardHandler)
70                 parser.parse('/usr/share/enigma2/startwizard.xml')
71                 
72                 self.numSteps = len(self.wizard)
73                 self.currStep = 1
74
75                 self["text"] = Label()
76                 self["rc"] = MovingPixmap()
77                 self["arrowdown"] = MovingPixmap()
78                 self["arrowup"] = MovingPixmap()
79
80                 self["config"] = ConfigList([])
81
82                 self["step"] = Label()
83                                 
84                 self["stepslider"] = Slider(1, self.numSteps)
85                 
86                 self.list = []
87                 self["list"] = MenuList(self.list)
88
89                 self.updateValues()
90                 
91                 self["actions"] = HelpableActionMap(self, "OkCancelActions",
92                         {
93                                 "ok": (self.ok, _("Close this Screen...")),
94                         })
95
96         def updateValues(self):
97                 self["step"].setText(_("Step ") + str(self.currStep) + "/" + str(self.numSteps))
98                 self["stepslider"].setValue(self.currStep)
99
100                 self["text"].setText(self.wizard[self.currStep]["text"])
101                 
102                 self.list = []
103                 if (len(self.wizard[self.currStep]["list"]) > 0):
104                         for x in self.wizard[self.currStep]["list"]:
105                                 self.list.append((x, None))
106                 self["list"].l.setList(self.list)
107                 
108                 if (self.wizard[self.currStep]["config"] != None):
109                         self.configInstance = self.session.instantiateDialog(self.wizard[self.currStep]["config"])
110                         self["config"].l.setList(self.configInstance["config"].list)
111                 else:
112                         self["config"].l.setList([])
113
114                 if self.wizard[self.currStep]["code"] != "":
115                         print self.wizard[self.currStep]["code"]
116                         exec(self.wizard[self.currStep]["code"])
117                         
118         def ok(self):
119                 if (self.currStep == self.numSteps): # wizard finished
120                         config.misc.firstrun.value = 0;
121                         config.misc.firstrun.save()
122                         self.session.close()
123                 else:
124                         self.currStep += 1
125                         self.updateValues()
126
127 def listActiveWizards():
128         wizards = [ ]
129
130         if config.misc.firstrun.value:
131                 wizards.append(WelcomeWizard)
132         
133         return wizards