menu.xml and setup.xml are now translatable
[enigma2.git] / lib / python / Screens / InfoBarGenerics.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap, HelpableActionMap
3 from Components.ActionMap import NumberActionMap
4 from Components.Label import Label
5 from Components.config import configfile, configsequencearg
6 from Components.config import config, configElement, ConfigSubsection, configSequence
7 from ChannelSelection import ChannelSelection
8
9 from Components.ServiceName import ServiceName
10 from Components.EventInfo import EventInfo
11
12 from ServiceReference import ServiceReference
13 from EpgSelection import EPGSelection
14
15 from Screens.MessageBox import MessageBox
16 from Screens.Volume import Volume
17 from Screens.Mute import Mute
18 from Screens.Standby import Standby
19 from Screens.EventView import EventView
20
21 #from enigma import eTimer, eDVBVolumecontrol, quitMainloop
22 from enigma import *
23
24 import time
25 import os
26
27 # hack alert!
28 from Menu import MainMenu, mdom
29
30 class InfoBarVolumeControl:
31         """Volume control, handles volUp, volDown, volMute actions and display 
32         a corresponding dialog"""
33         def __init__(self):
34                 config.audio = ConfigSubsection()
35                 config.audio.volume = configElement("config.audio.volume", configSequence, [5], configsequencearg.get("INTEGER", (0, 100)))
36
37                 self["VolumeActions"] = ActionMap( ["InfobarVolumeActions"] ,
38                         {
39                                 "volumeUp": self.volUp,
40                                 "volumeDown": self.volDown,
41                                 "volumeMute": self.volMute,
42                         })
43
44                 self.volumeDialog = self.session.instantiateDialog(Volume)
45                 self.muteDialog = self.session.instantiateDialog(Mute)
46
47                 self.hideVolTimer = eTimer()
48                 self.hideVolTimer.timeout.get().append(self.volHide)
49
50                 vol = config.audio.volume.value[0]
51                 self.volumeDialog.setValue(vol)
52                 eDVBVolumecontrol.getInstance().setVolume(vol, vol)
53         
54         def volSave(self):
55                 config.audio.volume.value = eDVBVolumecontrol.getInstance().getVolume()
56                 config.audio.volume.save()
57                 
58         def     volUp(self):
59                 eDVBVolumecontrol.getInstance().volumeUp()
60                 self.volumeDialog.instance.show()
61                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
62                 self.volSave()
63                 self.hideVolTimer.start(3000)
64
65         def     volDown(self):
66                 eDVBVolumecontrol.getInstance().volumeDown()
67                 self.volumeDialog.instance.show()
68                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
69                 self.volSave()
70                 self.hideVolTimer.start(3000)
71                 
72         def volHide(self):
73                 self.volumeDialog.instance.hide()
74
75         def     volMute(self):
76                 eDVBVolumecontrol.getInstance().volumeToggleMute()
77                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
78                 
79                 if (eDVBVolumecontrol.getInstance().isMuted()):
80                         self.muteDialog.instance.show()
81                 else:
82                         self.muteDialog.instance.hide()
83
84 class InfoBarShowHide:
85         """ InfoBar show/hide control, accepts toggleShow and hide actions, might start
86         fancy animations. """
87         STATE_HIDDEN = 0
88         STATE_HIDING = 1
89         STATE_SHOWING = 2
90         STATE_SHOWN = 3
91         
92         def __init__(self):
93                 self["ShowHideActions"] = ActionMap( ["InfobarShowHideActions"] ,
94                         {
95                                 "toggleShow": self.toggleShow,
96                                 "hide": self.hide,
97                         })
98
99                 self.state = self.STATE_HIDDEN
100                 
101                 self.hideTimer = eTimer()
102                 self.hideTimer.timeout.get().append(self.doTimerHide)
103                 #self.hideTimer.start(1000)
104
105         def hide(self): 
106                 self.instance.hide()
107
108         def doTimerHide(self):
109                 if self.state == self.STATE_SHOWN:
110                         self.instance.hide()
111                         self.state = self.STATE_HIDDEN
112
113         def toggleShow(self):
114                 if self.state == self.STATE_SHOWN:
115                         self.instance.hide()
116                         #pls check animation support, sorry
117 #                       self.startHide()
118                         self.state = self.STATE_HIDDEN
119                 else:
120                         self.instance.show()
121 #                       self.startShow()
122                         self.state = self.STATE_SHOWN
123                         #TODO: make it customizable
124                         self.hideTimer.start(5000)
125
126         def startShow(self):
127                 self.instance.m_animation.startMoveAnimation(ePoint(0, 600), ePoint(0, 380), 100)
128                 self.state = self.STATE_SHOWN
129         
130         def startHide(self):
131                 self.instance.m_animation.startMoveAnimation(ePoint(0, 380), ePoint(0, 600), 100)
132                 self.state = self.STATE_HIDDEN
133
134 class NumberZap(Screen):
135         def quit(self):
136                 self.Timer.stop()
137                 self.close(0)
138
139         def keyOK(self):
140                 self.Timer.stop()
141                 self.close(int(self["number"].getText()))
142
143         def keyNumberGlobal(self, number):
144                 self.Timer.start(3000)          #reset timer
145                 self.field = self.field + str(number)
146                 self["number"].setText(self.field)
147                 if len(self.field) >= 4:
148                         self.keyOK()
149
150         def __init__(self, session, number):
151                 Screen.__init__(self, session)
152                 self.field = str(number)
153                 
154                 self["number"] = Label(self.field)
155
156                 self["actions"] = NumberActionMap( [ "SetupActions" ], 
157                         {
158                                 "cancel": self.quit,
159                                 "ok": self.keyOK,
160                                 "1": self.keyNumberGlobal,
161                                 "2": self.keyNumberGlobal,
162                                 "3": self.keyNumberGlobal,
163                                 "4": self.keyNumberGlobal,
164                                 "5": self.keyNumberGlobal,
165                                 "6": self.keyNumberGlobal,
166                                 "7": self.keyNumberGlobal,
167                                 "8": self.keyNumberGlobal,
168                                 "9": self.keyNumberGlobal,
169                                 "0": self.keyNumberGlobal
170                         })
171
172                 self.Timer = eTimer()
173                 self.Timer.timeout.get().append(self.keyOK)
174                 self.Timer.start(3000)
175
176 class InfoBarPowerKey:
177         """ PowerKey stuff - handles the powerkey press and powerkey release actions"""
178         
179         def __init__(self):
180                 self.powerKeyTimer = eTimer()
181                 self.powerKeyTimer.timeout.get().append(self.powertimer)
182                 self["PowerKeyActions"] = ActionMap( ["PowerKeyActions"],
183                         {
184                                 "powerdown": self.powerdown,
185                                 "powerup": self.powerup,
186                         })
187
188         def powertimer(self):   
189                 print "PowerOff - Now!"
190                 self.quit()
191         
192         def powerdown(self):
193                 self.standbyblocked = 0
194                 self.powerKeyTimer.start(3000)
195
196         def powerup(self):
197                 self.powerKeyTimer.stop()
198                 if self.standbyblocked == 0:
199                         self.standbyblocked = 1
200                         self.session.open(Standby, self)
201
202         def quit(self):
203                 quitMainloop(0)
204
205 class InfoBarNumberZap:
206         """ Handles an initial number for NumberZapping """
207         def __init__(self):
208                 self["NumberZapActions"] = NumberActionMap( [ "NumberZapActions"],
209                         {
210                                 "1": self.keyNumberGlobal,
211                                 "2": self.keyNumberGlobal,
212                                 "3": self.keyNumberGlobal,
213                                 "4": self.keyNumberGlobal,
214                                 "5": self.keyNumberGlobal,
215                                 "6": self.keyNumberGlobal,
216                                 "7": self.keyNumberGlobal,
217                                 "8": self.keyNumberGlobal,
218                                 "9": self.keyNumberGlobal,
219                                 "0": self.keyNumberGlobal,
220                         })
221
222         def keyNumberGlobal(self, number):
223 #               print "You pressed number " + str(number)
224                 self.session.openWithCallback(self.numberEntered, NumberZap, number)
225
226         def numberEntered(self, retval):
227 #               print self.servicelist
228                 if retval > 0:
229                         self.servicelist.zapToNumber(retval)
230
231 class InfoBarChannelSelection:
232         """ ChannelSelection - handles the channelSelection dialog and the initial 
233         channelChange actions which open the channelSelection dialog """
234         def __init__(self):
235                 #instantiate forever
236                 self.servicelist = self.session.instantiateDialog(ChannelSelection)
237
238                 self["ChannelSelectActions"] = HelpableActionMap(self, "InfobarChannelSelection",
239                         {
240                                 "switchChannelUp": self.switchChannelUp,
241                                 "switchChannelDown": self.switchChannelDown,
242                                 "zapUp": (self.zapUp, _("next channel")),
243                                 "zapDown": (self.zapDown, _("previous channel")),
244                         })
245                         
246         def switchChannelUp(self):      
247                 self.servicelist.moveUp()
248                 self.session.execDialog(self.servicelist)
249
250         def switchChannelDown(self):    
251                 self.servicelist.moveDown()
252                 self.session.execDialog(self.servicelist)
253
254         def     zapUp(self):
255                 self.servicelist.moveUp()
256                 self.servicelist.zap()
257
258         def     zapDown(self):
259                 self.servicelist.moveDown()
260                 self.servicelist.zap()
261
262 class InfoBarMenu:
263         """ Handles a menu action, to open the (main) menu """
264         def __init__(self):
265                 self["MenuActions"] = ActionMap( [ "InfobarMenuActions" ], 
266                         {
267                                 "mainMenu": self.mainMenu,
268                         })
269
270         def mainMenu(self):
271                 print "loading mainmenu XML..."
272                 menu = mdom.childNodes[0]
273                 assert menu.tagName == "menu", "root element in menu must be 'menu'!"
274                 self.session.open(MainMenu, menu, menu.childNodes)
275
276 class InfoBarEPG:
277         """ EPG - Opens an EPG list when the showEPGList action fires """
278         def __init__(self):
279                 self["EPGActions"] = HelpableActionMap(self, "InfobarEPGActions", 
280                         {
281                                 "showEPGList": (self.showEPGList, _("show EPG...")),
282                         })
283
284         def showEPGList(self):
285                 ref=self.session.nav.getCurrentlyPlayingServiceReference()
286                 ptr=eEPGCache.getInstance()
287                 if ptr.startTimeQuery(ref) != -1:
288                         self.session.open(EPGSelection, ref)
289                 else: # try to show now/next
290                         print 'no epg for service', ref.toString()
291                         try:
292                                 self.epglist = [ ]
293                                 service = self.session.nav.getCurrentService()
294                                 info = service.info()
295                                 ptr=info.getEvent(0)
296                                 if ptr:
297                                         self.epglist.append(ptr)
298                                 ptr=info.getEvent(1)
299                                 if ptr:
300                                         self.epglist.append(ptr)
301                                 if len(self.epglist) > 0:
302                                         self.session.open(EventView, self.epglist[0], ServiceReference(ref), self.eventViewCallback)
303                         except:
304                                 pass
305
306         def eventViewCallback(self, setEvent, val): #used for now/next displaying
307                 if len(self.epglist) > 1:
308                         tmp = self.epglist[0]
309                         self.epglist[0]=self.epglist[1]
310                         self.epglist[1]=tmp
311                         setEvent(self.epglist[0])
312
313 class InfoBarEvent:
314         """provides a current/next event info display"""
315         def __init__(self):
316                 self["Event_Now_StartTime"] = EventInfo(self.session.nav, EventInfo.Now_StartTime)
317                 self["Event_Next_StartTime"] = EventInfo(self.session.nav, EventInfo.Next_StartTime)
318                                 
319                 self["Event_Now"] = EventInfo(self.session.nav, EventInfo.Now)
320                 self["Event_Next"] = EventInfo(self.session.nav, EventInfo.Next)
321
322                 self["Event_Now_Duration"] = EventInfo(self.session.nav, EventInfo.Now_Duration)
323                 self["Event_Next_Duration"] = EventInfo(self.session.nav, EventInfo.Next_Duration)
324
325 class InfoBarServiceName:
326         def __init__(self):
327                 self["ServiceName"] = ServiceName(self.session.nav)
328
329 class InfoBarPVR:
330         """handles PVR specific actions like seeking, pause"""
331         def __init__(self):
332                 self["PVRActions"] = ActionMap( [ "InfobarPVRActions" ], 
333                         {
334                                 "pauseService": self.pauseService,
335                                 "unPauseService": self.unPauseService,
336                                 
337                                 "seekFwd": self.seekFwd,
338                                 "seekBack": self.seekBack,
339                         })
340                 
341         def pauseService(self):
342                 self.session.nav.pause(1)
343                 
344         def unPauseService(self):
345                 self.session.nav.pause(0)
346         
347         def doSeek(self, dir, seektime):
348                 service = self.session.nav.getCurrentService()
349                 if service is None:
350                         return
351                 
352                 seekable = service.seek()
353                 if seekable is None:
354                         return
355                 seekable.seekRelative(dir, 90 * seektime)
356
357         def seekFwd(self):
358                 self.doSeek(+1, 60000)
359         
360         def seekBack(self):
361                 self.doSeek(-1, 60000)
362
363 class InfoBarInstantRecord:
364         """Instant Record - handles the instantRecord action in order to 
365         start/stop instant records"""
366         def __init__(self):
367                 self["InstnantRecordActions"] = ActionMap( [ "InfobarInstantRecord" ],
368                         {
369                                 "instantRecord": self.instantRecord,
370                         })
371                 self.recording = None
372
373         def stopCurrentRecording(self): 
374                 self.session.nav.RecordTimer.removeEntry(self.recording)
375                 self.recording = None
376         
377         def startInstantRecording(self):
378                 serviceref = self.session.nav.getCurrentlyPlayingServiceReference()
379                         
380                 # try to get event info
381                 epg = None
382                 try:
383                         service = self.session.nav.getCurrentService()
384                         info = service.info()
385                         ev = info.getEvent(0)
386                         epg = ev
387                 except:
388                         pass
389                 
390                 # fix me, description. 
391                 self.recording = self.session.nav.recordWithTimer(time.time(), time.time() + 3600, serviceref, epg, "instant record")
392                 self.recording.dontSave = True
393
394         def recordQuestionCallback(self, answer):
395                 if answer == False:
396                         return
397                 
398                 if self.recording != None:
399                         self.stopCurrentRecording()
400                 else:
401                         self.startInstantRecording()
402
403         def instantRecord(self):
404                 try:
405                         stat = os.stat("/hdd/movies")
406                 except:
407                         self.session.open(MessageBox, "No HDD found!")
408                         return
409         
410                 if self.recording != None:
411                         self.session.openWithCallback(self.recordQuestionCallback, MessageBox, "Do you want to stop the current\n(instant) recording?")
412                 else:
413                         self.session.openWithCallback(self.recordQuestionCallback, MessageBox, "Start recording?")
414
415 from Screens.AudioSelection import AudioSelection
416
417 class InfoBarAudioSelection:
418         def __init__(self):
419                 self["AudioSelectionAction"] = ActionMap( [ "InfobarAudioSelectionActions" ], 
420                         {
421                                 "audioSelection": self.audioSelection,
422                         })
423
424         def audioSelection(self):
425                 service = self.session.nav.getCurrentService()
426                 audio = service.audioTracks()
427                 n = audio.getNumberOfTracks()
428                 if n > 0:
429                         self.session.open(AudioSelection, audio)