- fix storage of servicelist (cannot be stored as a component)
[enigma2.git] / screens.py
1 from components import *
2 import sys
3 from enigma import quitMainloop
4
5 import xml.dom.minidom
6 from xml.dom import EMPTY_NAMESPACE
7 from skin import elementsWithTag
8
9
10 # some screens
11 def doGlobal(screen):
12         screen["clock"] = Clock()
13
14 class Screen(dict, HTMLSkin, GUISkin):
15         """ bla """
16
17         def __init__(self, session):
18                 self.skinName = self.__class__.__name__
19                 self.session = session
20                 GUISkin.__init__(self)
21                 
22         def execBegin(self):
23 #               assert self.session == None, "a screen can only exec one per time"
24 #               self.session = session
25                 for (name, val) in self.items():
26                         val.execBegin()
27         
28         def execEnd(self):
29                 for (name, val) in self.items():
30                         val.execEnd()
31 #               assert self.session != None, "execEnd on non-execing screen!"
32 #               self.session = None
33         
34         # never call this directly - it will be called from the session!
35         def doClose(self):
36                 GUISkin.close(self)
37                 
38                 del self.session
39                 for (name, val) in self.items():
40                         print "%s -> %d" % (name, sys.getrefcount(val))
41                         del self[name]
42         
43         def close(self, retval=None):
44                 self.session.close()
45
46
47 mdom = xml.dom.minidom.parseString(
48         """
49         <menu text="Mainmenu" title="the real Mainmenu">
50                 <item text="Standby debug">quitMainloop()</item>
51                 <item text="Automatic Scan">self.openDialog(serviceScan)</item>
52
53                 <item text="TV-Mode">self.setModeTV()</item>
54                 <item text="Radio-Mode">self.setModeRadio()</item>
55                 <item text="File-Mode">self.setModeFile()</item>
56                 <item text="Scart">self.openDialog(ScartLoopThrough)</item>
57                 <item text="Timer"></item>
58                 <menu text="Setup">
59                         <menu text="Service Organising">
60                                 <item text="New Bouquets"></item>
61                                 <item text="Add to Bouquets"></item>
62                                 <item text="Edit Bouquets"></item>
63                         </menu>
64                         <menu text="Service Searching">
65                                 <item text="Satelliteconfig"></item>
66                                 <item text="Satfinder"></item>
67                                 <item text="Rotor Control"></item>
68                                 <item text="Edit Transponder"></item>
69                                 <item text="Automatic Scan">self.openDialog(serviceScan)</item>
70                                 <item text="Automatic 'Multisat' Scan"></item>
71                                 <item text="Manual Scan"></item>
72                         </menu>
73                         <menu text="System">
74                                 <item text="Time Date"></item>
75                                 <item text="Video Audio"></item>
76                                 <item text="UHF Modulator"></item>
77                                 <item text="Harddisk"></item>
78                                 <item text="Keyboard"></item>
79                                 <item text="OSD">self.openDialog(configOSD)</item>
80                                 <item text="Language"></item>
81                                 <item text="LCD"></item>
82                         </menu>
83                         <item text="Common Interface"></item>
84                         <item text="Parental Control"></item>
85                         <item text="Expert"></item>
86                 </menu>
87                 <item text="Games"></item>
88                 <item text="Information"></item>
89                 <menu text="Standby">
90                         <item text="PowerOff"></item>
91                         <item text="Restart"></item>
92                         <item text="Standby"></item>
93                         <item text="Sleep Timer">self.goSetup()</item>
94                 </menu>
95         </menu>""")
96
97 def getText(nodelist):
98         rc = ""
99         for node in nodelist:
100                 if node.nodeType == node.TEXT_NODE:
101                         rc = rc + node.data
102         return rc
103
104 def getValbyAttr(x, attr):
105         for p in range(x.attributes.length):
106                 a = x.attributes.item(p)
107                 attrib = str(a.name)
108                 value = str(a.value)
109                 if attrib == attr:
110                         return value
111                         
112         return ""
113
114 class boundFunction:
115         def __init__(self, fnc, *args):
116                 self.fnc = fnc
117                 self.args = args
118         def __call__(self):
119                 self.fnc(*self.args)
120
121 class configOSD(Screen):
122         #this needs focus handling - so not useable
123
124         def okbuttonClick(self):
125                 self.close
126  
127         def __init__(self, session):
128                 Screen.__init__(self, session)
129
130                 self["actions"] = ActionMap(["OkCancelActions"], 
131                         {
132                                 "ok": self.okbuttonClick,
133                                 "cancel": self.close
134                         })
135
136                 self["okbutton"] = Button("Save")
137
138                 self["txt_alpha"] = Label("Alpha:")
139                 self["sld_alpha"] = ProgressBar()
140                 self["sld_alpha"].setValue(50)
141
142                 self["txt_brightness"] = Label("Brightness:")
143                 self["sld_brightness"] = ProgressBar()
144                 self["sld_brightness"].setValue(50)
145
146                 self["txt_gamma"] = Label("Contrast:")
147                 self["sld_gamma"] = ProgressBar()
148                 self["sld_gamma"].setValue(50)
149
150
151 class ScartLoopThrough(Screen):
152         def __init__(self, session):
153                 Screen.__init__(self, session)
154
155                 self["actions"] = ActionMap(["OkCancelActions"], 
156                         {
157                                 "cancel": self.close
158                         })
159
160 class ConfigMenu(Screen):
161         #create a generic class for view/edit settings
162         #all stuff come from xml file
163         #configtype / datasource / validate-call / ...
164
165         def __init__(self, session):
166                 Screen.__init__(self, session)
167
168                 self["actions"] = ActionMap(["OkCancelActions"], 
169                         {
170                                 #"ok": self.okbuttonClick,
171                                 "cancel": self.close
172                         })
173
174 class configTest(Screen):
175
176         def __init__(self, session):
177                 Screen.__init__(self, session)
178                 
179
180                 self["config"] = ConfigList(
181                         [
182                                 configEntry("HKEY_LOCAL_ENIGMA/IMPORTANT/USER_ANNOYING_STUFF/SDTV/FLASHES/GREEN"),
183                                 configEntry("HKEY_LOCAL_ENIGMA/IMPORTANT/USER_ANNOYING_STUFF/HDTV/FLASHES/GREEN"),
184                         ])
185
186                 self["actions"] = ActionMap(["OkCancelActions"], 
187                         {
188                                 "ok": self["config"].toggle,
189                                 "cancel": self.close
190                         })
191                 
192
193 class Menu(Screen):
194         #add file load functions for the xml-file
195         #remove old code (i.e. goScan / goClock...)
196
197         def openDialog(self, dialog):
198                 self.session.open(dialog)
199
200         def goSetup(self):
201                 self.session.open(configTest)
202         
203         def setModeTV(self):
204                 print "set Mode to TV"
205                 pass
206
207         def setModeRadio(self):
208                 print "set Mode to Radio"
209                 pass
210
211         def setModeFile(self):
212                 print "set Mode to File"
213                 pass
214
215         def goScan(self):
216                 self.session.open(serviceScan)
217         
218         def goClock(self):
219                 self.session.open(clockDisplay, Clock())
220
221         def okbuttonClick(self):
222                 selection = self["menu"].getCurrent()
223                 selection[1]()
224
225         def evalText(self, text):
226                 eval(text)
227                 
228         def nothing(self):                                                                                                                                      #dummy
229                 pass
230
231         def addMenu(self, destList, node):
232                 MenuTitle = getValbyAttr(node, "text")
233                 if MenuTitle != "":                                                                                                                                     #check for title
234                         a = boundFunction(self.session.open, Menu, node, node.childNodes)
235                         #TODO add check if !empty(node.childNodes)
236                         destList.append((MenuTitle, a))
237                 
238         def addItem(self, destList, node):
239                 ItemText = getValbyAttr(node, "text")
240                 if ItemText != "":                                                                                                                                      #check for name
241                         b = getText(node.childNodes)
242                         if b != "":                                                                                                                                                             #check for function
243                                 destList.append((ItemText,boundFunction(self.evalText,b)))
244                         else:
245                                 destList.append((ItemText,self.nothing))                                #use dummy as function
246
247         def __init__(self, session, parent, childNode):
248                 Screen.__init__(self, session)
249                 
250                 list = []
251
252                 for x in childNode:                                                     #walk through the actual nodelist
253                         if x.nodeType != xml.dom.minidom.Element.nodeType:
254                             continue
255                         elif x.tagName == 'item':
256                                 self.addItem(list, x)
257                         elif x.tagName == 'menu':
258                                 self.addMenu(list, x)
259
260                 self["menu"] = MenuList(list)   
261                                                         
262                 self["actions"] = ActionMap(["OkCancelActions"], 
263                         {
264                                 "ok": self.okbuttonClick,
265                                 "cancel": self.close
266                         })
267                 
268                 a = getValbyAttr(parent, "title")
269                 if a == "":                                                                                                             #if empty use name
270                         a = getValbyAttr(parent, "text")
271                 self["title"] = Header(a)
272
273 class channelSelection(Screen):
274         def __init__(self, session):
275                 Screen.__init__(self, session)
276
277                 self["key_red"] = Button("red")
278                 self["key_green"] = Button("green")
279                 self["key_yellow"] = Button("yellow")
280                 self["key_blue"] = Button("blue")
281                 
282                 self["list"] = ServiceList()
283                 self["list"].setRoot(eServiceReference("""1:0:1:0:0:0:0:0:0:0:(provider=="ARD") && (type == 1)"""))
284                 
285                 #self["okbutton"] = Button("ok", [self.channelSelected])
286                 
287                 class ChannelActionMap(ActionMap):
288                         def action(self, contexts, action):
289                                 if action[:7] == "bouquet":
290                                         print "setting root to " + action[8:]
291                                         self.csel["list"].setRoot(eServiceReference("1:0:1:0:0:0:0:0:0:0:" + action[8:]))
292                                 else:
293                                         ActionMap.action(self, contexts, action)
294
295                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions"], 
296                         {
297                                 "cancel": self.close,
298                                 "ok": self.channelSelected,
299                                 "mark": self.doMark
300                         })
301                 self["actions"].csel = self
302
303         def doMark(self):
304                 ref = self["list"].getCurrent()
305                 if self["list"].isMarked(ref):
306                         self["list"].removeMarked(ref)
307                 else:
308                         self["list"].addMarked(ref)
309                 
310         def channelSelected(self):
311                 self.session.nav.playService(self["list"].getCurrent())
312                 self.close()
313
314         #called from infoBar
315         def zapUp(self):
316                 self["list"].moveUp()
317                 self.session.nav.playService(self["list"].getCurrent())
318
319         def zapDown(self):
320                 self["list"].moveDown()
321                 self.session.nav.playService(self["list"].getCurrent())
322
323 class infoBar(Screen):
324         def __init__(self, session):
325                 Screen.__init__(self, session)
326
327                 #instantiate forever
328                 self.servicelist = self.session.instantiateDialog(channelSelection)
329                 
330                 self["actions"] = ActionMap( [ "InfobarActions" ], 
331                         {
332                                 "switchChannel": self.switchChannel,
333                                 "mainMenu": self.mainMenu,
334                                 "zapUp": self.zapUp,
335                                 "zapDown": self.zapDown
336                         })
337                 self["okbutton"] = Button("mainMenu", [self.mainMenu])
338                 
339                 self["CurrentTime"] = Clock()
340                 
341                 self["ServiceName"] = ServiceName(self.session.nav)
342                 
343                 self["Event_Now"] = EventInfo(self.session.nav, EventInfo.Now)
344                 self["Event_Next"] = EventInfo(self.session.nav, EventInfo.Next)
345
346                 self["Event_Now_Duration"] = EventInfo(self.session.nav, EventInfo.Now_Duration)
347                 self["Event_Next_Duration"] = EventInfo(self.session.nav, EventInfo.Next_Duration)
348         
349         def mainMenu(self):
350                 print "loading mainmenu XML..."
351                 menu = mdom.childNodes[0]
352                 assert menu.tagName == "menu", "root element in menu must be 'menu'!"
353                 self.session.open(Menu, menu, menu.childNodes)
354
355         def switchChannel(self):        
356                 self.session.execDialog(self.servicelist)
357
358         def     zapUp(self):
359                 self.servicelist.zapUp()
360
361         def     zapDown(self):
362                 self.servicelist.zapDown()
363
364 # a clock display dialog
365 class clockDisplay(Screen):
366         def okbutton(self):
367                 self.session.close()
368         
369         def __init__(self, session, clock):
370                 Screen.__init__(self, session)
371                 self["theClock"] = clock
372                 b = Button("bye")
373                 b.onClick = [ self.okbutton ]
374                 self["okbutton"] = b
375                 self["title"] = Header("clock dialog: here you see the current uhrzeit!")
376
377 class serviceScan(Screen):
378         def ok(self):
379                 print "ok"
380                 if self["scan"].isDone():
381                         self.close()
382         
383         def cancel(self):
384                 print "cancel not yet implemented ;)"
385         
386         def __init__(self, session):
387                 Screen.__init__(self, session)
388                 
389                 self["scan_progress"] = ProgressBar()
390                 self["scan_state"] = Label("scan state")
391                 self["scan"] = ServiceScan(self["scan_progress"], self["scan_state"])
392
393                 self["actions"] = ActionMap(["OkCancelActions"], 
394                         {
395                                 "ok": self.ok,
396                                 "cancel": self.cancel
397                         })
398