df0870afb5619d07c076a42af5d3cc1a148f4fdf
[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                                 "instantRecord": self.instantRecord
337                         })
338                 self["okbutton"] = Button("mainMenu", [self.mainMenu])
339                 
340                 self["CurrentTime"] = Clock()
341                 
342                 self["ServiceName"] = ServiceName(self.session.nav)
343                 
344                 self["Event_Now"] = EventInfo(self.session.nav, EventInfo.Now)
345                 self["Event_Next"] = EventInfo(self.session.nav, EventInfo.Next)
346
347                 self["Event_Now_Duration"] = EventInfo(self.session.nav, EventInfo.Now_Duration)
348                 self["Event_Next_Duration"] = EventInfo(self.session.nav, EventInfo.Next_Duration)
349                 
350                 self.recording = 0
351         
352         def mainMenu(self):
353                 print "loading mainmenu XML..."
354                 menu = mdom.childNodes[0]
355                 assert menu.tagName == "menu", "root element in menu must be 'menu'!"
356                 self.session.open(Menu, menu, menu.childNodes)
357
358         def switchChannel(self):        
359                 self.session.execDialog(self.servicelist)
360
361         def     zapUp(self):
362                 self.servicelist.zapUp()
363
364         def     zapDown(self):
365                 self.servicelist.zapDown()
366                 
367         def instantRecord(self):
368                 if self.recording:
369                         self.session.nav.endRecording()
370                         self.recording = 0
371                 else:
372                         self.session.nav.recordService(self.session.nav.getCurrentlyPlayingServiceReference())
373                         self.recording = 1
374
375 # a clock display dialog
376 class clockDisplay(Screen):
377         def okbutton(self):
378                 self.session.close()
379         
380         def __init__(self, session, clock):
381                 Screen.__init__(self, session)
382                 self["theClock"] = clock
383                 b = Button("bye")
384                 b.onClick = [ self.okbutton ]
385                 self["okbutton"] = b
386                 self["title"] = Header("clock dialog: here you see the current uhrzeit!")
387
388 class serviceScan(Screen):
389         def ok(self):
390                 print "ok"
391                 if self["scan"].isDone():
392                         self.close()
393         
394         def cancel(self):
395                 print "cancel not yet implemented ;)"
396         
397         def __init__(self, session):
398                 Screen.__init__(self, session)
399                 
400                 self["scan_progress"] = ProgressBar()
401                 self["scan_state"] = Label("scan state")
402                 self["scan"] = ServiceScan(self["scan_progress"], self["scan_state"])
403
404                 self["actions"] = ActionMap(["OkCancelActions"], 
405                         {
406                                 "ok": self.ok,
407                                 "cancel": self.cancel
408                         })
409