f2516c4b56d02237c2b92ec28453e39ecb0cc0fd
[enigma2.git] / lib / python / Screens / InfoBarGenerics.py
1 from ChannelSelection import ChannelSelection, BouquetSelector
2
3 from Components.ActionMap import ActionMap, HelpableActionMap
4 from Components.ActionMap import NumberActionMap
5 from Components.BlinkingPixmap import BlinkingPixmapConditional
6 from Components.Harddisk import harddiskmanager
7 from Components.Input import Input
8 from Components.Label import *
9 from Components.Pixmap import Pixmap, PixmapConditional
10 from Components.PluginComponent import plugins
11 from Components.ProgressBar import *
12 from Components.ServiceEventTracker import ServiceEventTracker
13 from Components.Sources.CurrentService import CurrentService
14 from Components.Sources.EventInfo import EventInfo
15 from Components.Sources.FrontendStatus import FrontendStatus
16 from Components.Sources.Boolean import Boolean
17 from Components.Sources.Clock import Clock
18 from Components.TimerList import TimerEntryComponent
19 from Components.config import config, configElement, ConfigSubsection, configSequence, configElementBoolean, configSelection, configElement_nonSave, getConfigListEntry
20 from Components.config import configfile, configsequencearg
21
22 from EpgSelection import EPGSelection
23 from Plugins.Plugin import PluginDescriptor
24
25 from Screen import Screen
26 from Screens.ChoiceBox import ChoiceBox
27 from Screens.Dish import Dish
28 from Screens.EventView import EventViewEPGSelect, EventViewSimple
29 from Screens.InputBox import InputBox
30 from Screens.MessageBox import MessageBox
31 from Screens.MinuteInput import MinuteInput
32 from Screens.TimerSelection import TimerSelection
33 from Screens.PictureInPicture import PictureInPicture
34 from Screens.SubtitleDisplay import SubtitleDisplay
35 from ServiceReference import ServiceReference
36
37 from Tools import Notifications
38 from Tools.Directories import *
39
40 #from enigma import eTimer, eDVBVolumecontrol, quitMainloop
41 from enigma import *
42
43 import time
44 import os
45 import bisect
46
47 from Components.config import config, currentConfigSelectionElement
48
49 # hack alert!
50 from Menu import MainMenu, mdom
51
52 class InfoBarDish:
53         def __init__(self):
54                 self.dishDialog = self.session.instantiateDialog(Dish)
55                 self.onLayoutFinish.append(self.dishDialog.show)
56
57 class InfoBarShowHide:
58         """ InfoBar show/hide control, accepts toggleShow and hide actions, might start
59         fancy animations. """
60         STATE_HIDDEN = 0
61         STATE_HIDING = 1
62         STATE_SHOWING = 2
63         STATE_SHOWN = 3
64         
65         def __init__(self):
66                 self["ShowHideActions"] = ActionMap( ["InfobarShowHideActions"] ,
67                         {
68                                 "toggleShow": self.toggleShow,
69                                 "hide": self.hide,
70                         })
71
72                 self.__state = self.STATE_SHOWN
73                 self.__locked = 0
74                 
75                 self.onExecBegin.append(self.show)
76                 
77                 self.hideTimer = eTimer()
78                 self.hideTimer.timeout.get().append(self.doTimerHide)
79                 self.hideTimer.start(5000, True)
80                 
81                 self.onShow.append(self.__onShow)
82                 self.onHide.append(self.__onHide)
83
84         def __onShow(self):
85                 self.__state = self.STATE_SHOWN
86                 self.startHideTimer()
87         
88         def startHideTimer(self):
89                 if self.__state == self.STATE_SHOWN and not self.__locked:
90                         self.hideTimer.start(5000, True)
91
92         def __onHide(self):
93                 self.__state = self.STATE_HIDDEN
94
95         def doShow(self):
96                 self.show()
97                 self.startHideTimer()
98
99         def doTimerHide(self):
100                 self.hideTimer.stop()
101                 if self.__state == self.STATE_SHOWN:
102                         self.hide()
103
104         def toggleShow(self):
105                 if self.__state == self.STATE_SHOWN:
106                         self.hide()
107                         self.hideTimer.stop()
108                 elif self.__state == self.STATE_HIDDEN:
109                         self.show()
110
111         def lockShow(self):
112                 self.__locked = self.__locked + 1
113                 if self.execing:
114                         self.show()
115                         self.hideTimer.stop()
116         
117         def unlockShow(self):
118                 self.__locked = self.__locked - 1
119                 if self.execing:
120                         self.startHideTimer()
121
122 #       def startShow(self):
123 #               self.instance.m_animation.startMoveAnimation(ePoint(0, 600), ePoint(0, 380), 100)
124 #               self.__state = self.STATE_SHOWN
125 #       
126 #       def startHide(self):
127 #               self.instance.m_animation.startMoveAnimation(ePoint(0, 380), ePoint(0, 600), 100)
128 #               self.__state = self.STATE_HIDDEN
129
130 class NumberZap(Screen):
131         def quit(self):
132                 self.Timer.stop()
133                 self.close(0)
134
135         def keyOK(self):
136                 self.Timer.stop()
137                 self.close(int(self["number"].getText()))
138
139         def keyNumberGlobal(self, number):
140                 self.Timer.start(3000, True)            #reset timer
141                 self.field = self.field + str(number)
142                 self["number"].setText(self.field)
143                 if len(self.field) >= 4:
144                         self.keyOK()
145
146         def __init__(self, session, number):
147                 Screen.__init__(self, session)
148                 self.field = str(number)
149
150                 self["channel"] = Label(_("Channel:"))
151
152                 self["number"] = Label(self.field)
153
154                 self["actions"] = NumberActionMap( [ "SetupActions" ], 
155                         {
156                                 "cancel": self.quit,
157                                 "ok": self.keyOK,
158                                 "1": self.keyNumberGlobal,
159                                 "2": self.keyNumberGlobal,
160                                 "3": self.keyNumberGlobal,
161                                 "4": self.keyNumberGlobal,
162                                 "5": self.keyNumberGlobal,
163                                 "6": self.keyNumberGlobal,
164                                 "7": self.keyNumberGlobal,
165                                 "8": self.keyNumberGlobal,
166                                 "9": self.keyNumberGlobal,
167                                 "0": self.keyNumberGlobal
168                         })
169
170                 self.Timer = eTimer()
171                 self.Timer.timeout.get().append(self.keyOK)
172                 self.Timer.start(3000, True)
173
174 class InfoBarNumberZap:
175         """ Handles an initial number for NumberZapping """
176         def __init__(self):
177                 self["NumberActions"] = NumberActionMap( [ "NumberActions"],
178                         {
179                                 "1": self.keyNumberGlobal,
180                                 "2": self.keyNumberGlobal,
181                                 "3": self.keyNumberGlobal,
182                                 "4": self.keyNumberGlobal,
183                                 "5": self.keyNumberGlobal,
184                                 "6": self.keyNumberGlobal,
185                                 "7": self.keyNumberGlobal,
186                                 "8": self.keyNumberGlobal,
187                                 "9": self.keyNumberGlobal,
188                                 "0": self.keyNumberGlobal,
189                         })
190
191         def keyNumberGlobal(self, number):
192 #               print "You pressed number " + str(number)
193                 if number == 0:
194                         self.servicelist.recallPrevService()
195                         self.doShow()
196                 else:
197                         self.session.openWithCallback(self.numberEntered, NumberZap, number)
198
199         def numberEntered(self, retval):
200 #               print self.servicelist
201                 if retval > 0:
202                         self.zapToNumber(retval)
203
204         def searchNumberHelper(self, serviceHandler, num, bouquet):
205                 servicelist = serviceHandler.list(bouquet)
206                 if not servicelist is None:
207                         while num:
208                                 serviceIterator = servicelist.getNext()
209                                 if not serviceIterator.valid(): #check end of list
210                                         break
211                                 if serviceIterator.flags: #assume normal dvb service have no flags set
212                                         continue
213                                 num -= 1;
214                         if not num: #found service with searched number ?
215                                 return serviceIterator, 0
216                 return None, num
217
218         def zapToNumber(self, number):
219                 bouquet = self.servicelist.bouquet_root
220                 service = None
221                 serviceHandler = eServiceCenter.getInstance()
222                 if bouquet.toString().find('FROM BOUQUET "bouquets.') == -1: #FIXME HACK
223                         service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
224                 else:
225                         bouquetlist = serviceHandler.list(bouquet)
226                         if not bouquetlist is None:
227                                 while number:
228                                         bouquet = self.servicelist.appendDVBTypes(bouquetlist.getNext())
229                                         if not bouquet.valid(): #check end of list
230                                                 break
231                                         if (bouquet.flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory:
232                                                 continue
233                                         service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
234                 if not service is None:
235                         if self.servicelist.getRoot() != bouquet: #already in correct bouquet?
236                                 self.servicelist.clearPath()
237                                 if self.servicelist.bouquet_root != bouquet:
238                                         self.servicelist.enterPath(self.servicelist.bouquet_root)
239                                 self.servicelist.enterPath(bouquet)
240                         self.servicelist.setCurrentSelection(service) #select the service in servicelist
241                         self.servicelist.zap()
242
243 config.misc.initialchannelselection = configElementBoolean("config.misc.initialchannelselection", 1);
244
245 class InfoBarChannelSelection:
246         """ ChannelSelection - handles the channelSelection dialog and the initial 
247         channelChange actions which open the channelSelection dialog """
248         def __init__(self):
249                 #instantiate forever
250                 self.servicelist = self.session.instantiateDialog(ChannelSelection)
251                 
252                 if config.misc.initialchannelselection.value == 1:
253                         self.onShown.append(self.firstRun)
254
255                 self["ChannelSelectActions"] = HelpableActionMap(self, "InfobarChannelSelection",
256                         {
257                                 "switchChannelUp": (self.switchChannelUp, _("open servicelist(up)")),
258                                 "switchChannelDown": (self.switchChannelDown, _("open servicelist(down)")),
259                                 "zapUp": (self.zapUp, _("previous channel")),
260                                 "zapDown": (self.zapDown, _("next channel")),
261                                 "historyBack": (self.historyBack, _("previous channel in history")),
262                                 "historyNext": (self.historyNext, _("next channel in history")),
263                                 "openServiceList": (self.openServiceList, _("open servicelist")),
264                         })
265
266         def showTvChannelList(self, zap=False):
267                 self.servicelist.setModeTv()
268                 if zap:
269                         self.servicelist.zap()
270                 self.session.execDialog(self.servicelist)
271
272         def showRadioChannelList(self, zap=False):
273                 self.servicelist.setModeRadio()
274                 if zap:
275                         self.servicelist.zap()
276                 self.session.execDialog(self.servicelist)
277
278         def firstRun(self):
279                 self.onShown.remove(self.firstRun)
280                 config.misc.initialchannelselection.value = 0
281                 config.misc.initialchannelselection.save()
282                 self.switchChannelDown()
283
284         def historyBack(self):
285                 self.servicelist.historyBack()
286
287         def historyNext(self):
288                 self.servicelist.historyNext()
289
290         def switchChannelUp(self):
291                 self.servicelist.moveUp()
292                 self.session.execDialog(self.servicelist)
293
294         def switchChannelDown(self):
295                 self.servicelist.moveDown()
296                 self.session.execDialog(self.servicelist)
297         
298         def openServiceList(self):
299                 self.session.execDialog(self.servicelist)
300
301         def zapUp(self):
302                 if self.servicelist.inBouquet():
303                         prev = self.servicelist.getCurrentSelection()
304                         if prev:
305                                 prev = prev.toString()
306                                 while True:
307                                         if currentConfigSelectionElement(config.usage.quickzap_bouquet_change) == "yes":
308                                                 if self.servicelist.atBegin():
309                                                         self.servicelist.prevBouquet()
310                                         self.servicelist.moveUp()
311                                         cur = self.servicelist.getCurrentSelection()
312                                         if not cur or (not (cur.flags & 64)) or cur.toString() == prev:
313                                                 break
314                 else:
315                         self.servicelist.moveUp()
316                 self.servicelist.zap()
317                 self.doShow()
318
319         def zapDown(self):
320                 if self.servicelist.inBouquet():
321                         prev = self.servicelist.getCurrentSelection()
322                         if prev:
323                                 prev = prev.toString()
324                                 while True:
325                                         if currentConfigSelectionElement(config.usage.quickzap_bouquet_change) == "yes" and self.servicelist.atEnd():
326                                                 self.servicelist.nextBouquet()
327                                         else:
328                                                 self.servicelist.moveDown()
329                                         cur = self.servicelist.getCurrentSelection()
330                                         if not cur or (not (cur.flags & 64)) or cur.toString() == prev:
331                                                 break
332                 else:
333                         self.servicelist.moveDown()
334                 self.servicelist.zap()
335                 self.doShow()
336
337 class InfoBarMenu:
338         """ Handles a menu action, to open the (main) menu """
339         def __init__(self):
340                 self["MenuActions"] = HelpableActionMap(self, "InfobarMenuActions", 
341                         {
342                                 "mainMenu": (self.mainMenu, _("Enter main menu...")),
343                         })
344
345         def mainMenu(self):
346                 print "loading mainmenu XML..."
347                 menu = mdom.childNodes[0]
348                 assert menu.tagName == "menu", "root element in menu must be 'menu'!"
349                 self.session.open(MainMenu, menu, menu.childNodes)
350
351 class InfoBarSimpleEventView:
352         """ Opens the Eventview for now/next """
353         def __init__(self):
354                 self["EPGActions"] = HelpableActionMap(self, "InfobarEPGActions",
355                         {
356                                 "showEventInfo": (self.openEventView, _("show event details")),
357                         })
358
359         def openEventView(self):
360                 self.epglist = [ ]
361                 service = self.session.nav.getCurrentService()
362                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
363                 info = service.info()
364                 ptr=info.getEvent(0)
365                 if ptr:
366                         self.epglist.append(ptr)
367                 ptr=info.getEvent(1)
368                 if ptr:
369                         self.epglist.append(ptr)
370                 if len(self.epglist) > 0:
371                         self.session.open(EventViewSimple, self.epglist[0], ServiceReference(ref), self.eventViewCallback)
372
373         def eventViewCallback(self, setEvent, setService, val): #used for now/next displaying
374                 if len(self.epglist) > 1:
375                         tmp = self.epglist[0]
376                         self.epglist[0]=self.epglist[1]
377                         self.epglist[1]=tmp
378                         setEvent(self.epglist[0])
379
380 class InfoBarEPG:
381         """ EPG - Opens an EPG list when the showEPGList action fires """
382         def __init__(self):
383                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
384                         {
385                                 iPlayableService.evUpdatedEventInfo: self.__evEventInfoChanged,
386                         })
387
388                 self.is_now_next = False
389                 self.dlg_stack = [ ]
390                 self.bouquetSel = None
391                 self.eventView = None
392                 self["EPGActions"] = HelpableActionMap(self, "InfobarEPGActions", 
393                         {
394                                 "showEventInfo": (self.openEventView, _("show EPG...")),
395                         })
396
397         def zapToService(self, service):
398                 if not service is None:
399                         if self.servicelist.getRoot() != self.epg_bouquet: #already in correct bouquet?
400                                 self.servicelist.clearPath()
401                                 if self.servicelist.bouquet_root != self.epg_bouquet:
402                                         self.servicelist.enterPath(self.servicelist.bouquet_root)
403                                 self.servicelist.enterPath(self.epg_bouquet)
404                         self.servicelist.setCurrentSelection(service) #select the service in servicelist
405                         self.servicelist.zap()
406
407         def getBouquetServices(self, bouquet):
408                 services = [ ]
409                 servicelist = eServiceCenter.getInstance().list(bouquet)
410                 if not servicelist is None:
411                         while True:
412                                 service = servicelist.getNext()
413                                 if not service.valid(): #check if end of list
414                                         break
415                                 if service.flags: #ignore non playable services
416                                         continue
417                                 services.append(ServiceReference(service))
418                 return services
419
420         def openBouquetEPG(self, bouquet, withCallback=True):
421                 services = self.getBouquetServices(bouquet)
422                 if len(services):
423                         self.epg_bouquet = bouquet
424                         if withCallback:
425                                 self.dlg_stack.append(self.session.openWithCallback(self.closed, EPGSelection, services, self.zapToService, None, self.changeBouquetCB))
426                         else:
427                                 self.session.open(EPGSelection, services, self.zapToService, None, self.changeBouquetCB)
428
429         def changeBouquetCB(self, direction, epg):
430                 if self.bouquetSel:
431                         if direction > 0:
432                                 self.bouquetSel.down()
433                         else:
434                                 self.bouquetSel.up()
435                         bouquet = self.bouquetSel.getCurrent()
436                         services = self.getBouquetServices(bouquet)
437                         if len(services):
438                                 self.epg_bouquet = bouquet
439                                 epg.setServices(services)
440
441         def closed(self, ret=False):
442                 closedScreen = self.dlg_stack.pop()
443                 if self.bouquetSel and closedScreen == self.bouquetSel:
444                         self.bouquetSel = None
445                 elif self.eventView and closedScreen == self.eventView:
446                         self.eventView = None
447                 if ret:
448                         dlgs=len(self.dlg_stack)
449                         if dlgs > 0:
450                                 self.dlg_stack[dlgs-1].close(dlgs > 1)
451
452         def openMultiServiceEPG(self, withCallback=True):
453                 bouquets = self.servicelist.getBouquetList()
454                 if bouquets is None:
455                         cnt = 0
456                 else:
457                         cnt = len(bouquets)
458                 if cnt > 1: # show bouquet list
459                         if withCallback:
460                                 self.bouquetSel = self.session.openWithCallback(self.closed, BouquetSelector, bouquets, self.openBouquetEPG, enableWrapAround=True)
461                                 self.dlg_stack.append(self.bouquetSel)
462                         else:
463                                 self.bouquetSel = self.session.open(BouquetSelector, bouquets, self.openBouquetEPG, enableWrapAround=True)
464                 elif cnt == 1: 
465                         self.openBouquetEPG(bouquets[0][1], withCallback)
466
467         def openSingleServiceEPG(self):
468                 ref=self.session.nav.getCurrentlyPlayingServiceReference()
469                 self.session.open(EPGSelection, ref)
470
471         def openSimilarList(self, eventid, refstr):
472                 self.session.open(EPGSelection, refstr, None, eventid)
473
474         def getNowNext(self):
475                 self.epglist = [ ]
476                 service = self.session.nav.getCurrentService()
477                 info = service and service.info()
478                 ptr = info and info.getEvent(0)
479                 if ptr:
480                         self.epglist.append(ptr)
481                 ptr = info and info.getEvent(1)
482                 if ptr:
483                         self.epglist.append(ptr)
484
485         def __evEventInfoChanged(self):
486                 if self.is_now_next and len(self.dlg_stack) == 1:
487                         self.getNowNext()
488                         assert self.eventView
489                         if len(self.epglist):
490                                 self.eventView.setEvent(self.epglist[0])
491
492         def openEventView(self):
493                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
494                 self.getNowNext()
495                 if len(self.epglist) == 0:
496                         self.is_now_next = False
497                         epg = eEPGCache.getInstance()
498                         ptr = ref and ref.valid() and epg.lookupEventTime(ref, -1)
499                         if ptr:
500                                 self.epglist.append(ptr)
501                                 ptr = epg.lookupEventTime(ref, ptr.getBeginTime(), +1)
502                                 if ptr:
503                                         self.epglist.append(ptr)
504                 else:
505                         self.is_now_next = True
506                 if len(self.epglist) > 0:
507                         self.eventView = self.session.openWithCallback(self.closed, EventViewEPGSelect, self.epglist[0], ServiceReference(ref), self.eventViewCallback, self.openSingleServiceEPG, self.openMultiServiceEPG, self.openSimilarList)
508                         self.dlg_stack.append(self.eventView)
509                 else:
510                         print "no epg for the service avail.. so we show multiepg instead of eventinfo"
511                         self.openMultiServiceEPG(False)
512
513         def eventViewCallback(self, setEvent, setService, val): #used for now/next displaying
514                 if len(self.epglist) > 1:
515                         tmp = self.epglist[0]
516                         self.epglist[0]=self.epglist[1]
517                         self.epglist[1]=tmp
518                         setEvent(self.epglist[0])
519
520 class InfoBarTuner:
521         """provides a snr/agc/ber display"""
522         def __init__(self):
523                 self["FrontendStatus"] = FrontendStatus(service_source = self.session.nav.getCurrentService)
524
525 class InfoBarEvent:
526         """provides a current/next event info display"""
527         def __init__(self):
528                 self["Event_Now"] = EventInfo(self.session.nav, EventInfo.NOW)
529                 self["Event_Next"] = EventInfo(self.session.nav, EventInfo.NEXT)
530
531 class InfoBarServiceName:
532         def __init__(self):
533                 self["CurrentService"] = CurrentService(self.session.nav)
534
535 class InfoBarSeek:
536         """handles actions like seeking, pause"""
537         
538         # ispause, isff, issm
539         SEEK_STATE_PLAY = (0, 0, 0, ">")
540         SEEK_STATE_PAUSE = (1, 0, 0, "||")
541         SEEK_STATE_FF_2X = (0, 2, 0, ">> 2x")
542         SEEK_STATE_FF_4X = (0, 4, 0, ">> 4x")
543         SEEK_STATE_FF_8X = (0, 8, 0, ">> 8x")
544         SEEK_STATE_FF_32X = (0, 32, 0, ">> 32x")
545         SEEK_STATE_FF_64X = (0, 64, 0, ">> 64x")
546         SEEK_STATE_FF_128X = (0, 128, 0, ">> 128x")
547         
548         SEEK_STATE_BACK_16X = (0, -16, 0, "<< 16x")
549         SEEK_STATE_BACK_32X = (0, -32, 0, "<< 32x")
550         SEEK_STATE_BACK_64X = (0, -64, 0, "<< 64x")
551         SEEK_STATE_BACK_128X = (0, -128, 0, "<< 128x")
552         
553         SEEK_STATE_SM_HALF = (0, 0, 2, "/2")
554         SEEK_STATE_SM_QUARTER = (0, 0, 4, "/4")
555         SEEK_STATE_SM_EIGHTH = (0, 0, 8, "/8")
556         
557         def __init__(self):
558                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
559                         {
560                                 iPlayableService.evSeekableStatusChanged: self.__seekableStatusChanged,
561                                 iPlayableService.evStart: self.__serviceStarted,
562                                 
563                                 iPlayableService.evEOF: self.__evEOF,
564                                 iPlayableService.evSOF: self.__evSOF,
565                         })
566
567                 class InfoBarSeekActionMap(HelpableActionMap):
568                         def __init__(self, screen, *args, **kwargs):
569                                 HelpableActionMap.__init__(self, screen, *args, **kwargs)
570                                 self.screen = screen
571                                 
572                         def action(self, contexts, action):
573                                 if action[:5] == "seek:":
574                                         time = int(action[5:])
575                                         self.screen.seekRelative(time * 90000)
576                                         return 1
577                                 else:
578                                         return HelpableActionMap.action(self, contexts, action)
579
580                 self["SeekActions"] = InfoBarSeekActionMap(self, "InfobarSeekActions", 
581                         {
582                                 "pauseService": (self.pauseService, _("pause")),
583                                 "unPauseService": (self.unPauseService, _("continue")),
584                                 
585                                 "seekFwd": (self.seekFwd, _("skip forward")),
586                                 "seekFwdDown": self.seekFwdDown,
587                                 "seekFwdUp": self.seekFwdUp,
588                                 "seekBack": (self.seekBack, _("skip backward")),
589                                 "seekBackDown": self.seekBackDown,
590                                 "seekBackUp": self.seekBackUp,
591                         }, prio=-1)
592                         # give them a little more priority to win over color buttons
593
594                 self.seekstate = self.SEEK_STATE_PLAY
595                 self.onClose.append(self.delTimer)
596                 
597                 self.fwdtimer = False
598                 self.fwdKeyTimer = eTimer()
599                 self.fwdKeyTimer.timeout.get().append(self.fwdTimerFire)
600
601                 self.rwdtimer = False
602                 self.rwdKeyTimer = eTimer()
603                 self.rwdKeyTimer.timeout.get().append(self.rwdTimerFire)
604                 
605                 self.onPlayStateChanged = [ ]
606                 
607                 self.lockedBecauseOfSkipping = False
608         
609         def up(self):
610                 pass
611         
612         def down(self):
613                 pass
614         
615         def delTimer(self):
616                 del self.fwdKeyTimer
617                 del self.rwdKeyTimer
618         
619         def getSeek(self):
620                 service = self.session.nav.getCurrentService()
621                 if service is None:
622                         return None
623
624                 seek = service.seek()
625
626                 if seek is None or not seek.isCurrentlySeekable():
627                         return None
628                 
629                 return seek
630         
631         def isSeekable(self):
632                 if self.getSeek() is None:
633                         return False
634                 return True
635
636         def __seekableStatusChanged(self):
637                 print "seekable status changed!"
638                 if not self.isSeekable():
639                         self["SeekActions"].setEnabled(False)
640                         print "not seekable, return to play"
641                         self.setSeekState(self.SEEK_STATE_PLAY)
642                 else:
643                         self["SeekActions"].setEnabled(True)
644                         print "seekable"
645
646         def __serviceStarted(self):
647                 self.seekstate = self.SEEK_STATE_PLAY
648
649         def setSeekState(self, state):
650                 service = self.session.nav.getCurrentService()
651                 
652                 if service is None:
653                         return False
654                 
655                 if not self.isSeekable():
656                         if state not in [self.SEEK_STATE_PLAY, self.SEEK_STATE_PAUSE]:
657                                 state = self.SEEK_STATE_PLAY
658                 
659                 pauseable = service.pause()
660
661                 if pauseable is None:
662                         print "not pauseable."
663                         state = self.SEEK_STATE_PLAY
664                 
665                 oldstate = self.seekstate
666                 self.seekstate = state
667                 
668                 for i in range(3):
669                         if oldstate[i] != self.seekstate[i]:
670                                 (self.session.nav.pause, pauseable.setFastForward, pauseable.setSlowMotion)[i](self.seekstate[i])
671
672                 for c in self.onPlayStateChanged:
673                         c(self.seekstate)
674                 
675                 self.checkSkipShowHideLock()
676
677                 return True
678
679         def pauseService(self):
680                 if self.seekstate == self.SEEK_STATE_PAUSE:
681                         print "pause, but in fact unpause"
682                         self.unPauseService()
683                 else:
684                         if self.seekstate == self.SEEK_STATE_PLAY:
685                                 print "yes, playing."
686                         else:
687                                 print "no", self.seekstate
688                         print "pause"
689                         self.setSeekState(self.SEEK_STATE_PAUSE);
690                 
691         def unPauseService(self):
692                 print "unpause"
693                 if self.seekstate == self.SEEK_STATE_PLAY:
694                         return 0
695                 self.setSeekState(self.SEEK_STATE_PLAY)
696         
697         def doSeek(self, seektime):
698                 print "doseek", seektime
699                 service = self.session.nav.getCurrentService()
700                 if service is None:
701                         return
702                 
703                 seekable = self.getSeek()
704                 if seekable is None:
705                         return
706                 
707                 seekable.seekTo(90 * seektime)
708
709         def seekFwdDown(self):
710                 print "start fwd timer"
711                 self.fwdtimer = True
712                 self.fwdKeyTimer.start(1000)
713
714         def seekBackDown(self):
715                 print "start rewind timer"
716                 self.rwdtimer = True
717                 self.rwdKeyTimer.start(1000)
718
719         def seekFwdUp(self):
720                 print "seekFwdUp"
721                 if self.fwdtimer:
722                         self.fwdKeyTimer.stop()
723                         self.fwdtimer = False
724                         self.seekFwd()
725
726         def seekFwd(self):
727                 lookup = {
728                                 self.SEEK_STATE_PLAY: self.SEEK_STATE_FF_2X,
729                                 self.SEEK_STATE_PAUSE: self.SEEK_STATE_SM_EIGHTH,
730                                 self.SEEK_STATE_FF_2X: self.SEEK_STATE_FF_4X,
731                                 self.SEEK_STATE_FF_4X: self.SEEK_STATE_FF_8X,
732                                 self.SEEK_STATE_FF_8X: self.SEEK_STATE_FF_32X,
733                                 self.SEEK_STATE_FF_32X: self.SEEK_STATE_FF_64X,
734                                 self.SEEK_STATE_FF_64X: self.SEEK_STATE_FF_128X,
735                                 self.SEEK_STATE_FF_128X: self.SEEK_STATE_FF_128X,
736                                 self.SEEK_STATE_BACK_16X: self.SEEK_STATE_PLAY,
737                                 self.SEEK_STATE_BACK_32X: self.SEEK_STATE_BACK_16X,
738                                 self.SEEK_STATE_BACK_64X: self.SEEK_STATE_BACK_32X,
739                                 self.SEEK_STATE_BACK_128X: self.SEEK_STATE_BACK_64X,
740                                 self.SEEK_STATE_SM_HALF: self.SEEK_STATE_SM_HALF,
741                                 self.SEEK_STATE_SM_QUARTER: self.SEEK_STATE_SM_HALF,
742                                 self.SEEK_STATE_SM_EIGHTH: self.SEEK_STATE_SM_QUARTER
743                         }
744                 self.setSeekState(lookup[self.seekstate])
745         
746         def seekBackUp(self):
747                 print "seekBackUp"
748                 if self.rwdtimer:
749                         self.rwdKeyTimer.stop()
750                         self.rwdtimer = False
751                         self.seekBack()
752                 
753         def seekBack(self):
754                 lookup = {
755                                 self.SEEK_STATE_PLAY: self.SEEK_STATE_BACK_16X,
756                                 self.SEEK_STATE_PAUSE: self.SEEK_STATE_PAUSE,
757                                 self.SEEK_STATE_FF_2X: self.SEEK_STATE_PLAY,
758                                 self.SEEK_STATE_FF_4X: self.SEEK_STATE_FF_2X,
759                                 self.SEEK_STATE_FF_8X: self.SEEK_STATE_FF_4X,
760                                 self.SEEK_STATE_FF_32X: self.SEEK_STATE_FF_8X,
761                                 self.SEEK_STATE_FF_64X: self.SEEK_STATE_FF_32X,
762                                 self.SEEK_STATE_FF_128X: self.SEEK_STATE_FF_64X,
763                                 self.SEEK_STATE_BACK_16X: self.SEEK_STATE_BACK_32X,
764                                 self.SEEK_STATE_BACK_32X: self.SEEK_STATE_BACK_64X,
765                                 self.SEEK_STATE_BACK_64X: self.SEEK_STATE_BACK_128X,
766                                 self.SEEK_STATE_BACK_128X: self.SEEK_STATE_BACK_128X,
767                                 self.SEEK_STATE_SM_HALF: self.SEEK_STATE_SM_QUARTER,
768                                 self.SEEK_STATE_SM_QUARTER: self.SEEK_STATE_SM_EIGHTH,
769                                 self.SEEK_STATE_SM_EIGHTH: self.SEEK_STATE_PAUSE
770                         }
771                 self.setSeekState(lookup[self.seekstate])
772                 
773                 if self.seekstate == self.SEEK_STATE_PAUSE:
774                         seekable = self.getSeek()
775                         if seekable is not None:
776                                 seekable.seekRelative(-1, 3)
777
778         def fwdTimerFire(self):
779                 print "Display seek fwd"
780                 self.fwdKeyTimer.stop()
781                 self.fwdtimer = False
782                 self.session.openWithCallback(self.fwdSeekTo, MinuteInput)
783                 
784         def fwdSeekTo(self, minutes):
785                 print "Seek", minutes, "minutes forward"
786                 if minutes != 0:
787                         seekable = self.getSeek()
788                         if seekable is not None:
789                                 seekable.seekRelative(1, minutes * 60 * 90000)
790         
791         def rwdTimerFire(self):
792                 print "rwdTimerFire"
793                 self.rwdKeyTimer.stop()
794                 self.rwdtimer = False
795                 self.session.openWithCallback(self.rwdSeekTo, MinuteInput)
796         
797         def rwdSeekTo(self, minutes):
798                 print "rwdSeekTo"
799                 self.fwdSeekTo(0 - minutes)
800         
801         def checkSkipShowHideLock(self):
802                 wantlock = self.seekstate != self.SEEK_STATE_PLAY
803                 
804                 if self.lockedBecauseOfSkipping and not wantlock:
805                         self.unlockShow()
806                         self.lockedBecauseOfSkipping = False
807                 
808                 if wantlock and not self.lockedBecauseOfSkipping:
809                         self.lockShow()
810                         self.lockedBecauseOfSkipping = True
811
812         def __evEOF(self):
813                 if self.seekstate != self.SEEK_STATE_PLAY:
814                         self.setSeekState(self.SEEK_STATE_PAUSE)
815                         # HACK
816                         #self.getSeek().seekRelative(1, -90000)
817                         self.setSeekState(self.SEEK_STATE_PLAY)
818                 else:
819                         self.setSeekState(self.SEEK_STATE_PAUSE)
820         
821         def __evSOF(self):
822                 self.setSeekState(self.SEEK_STATE_PLAY)
823                 self.doSeek(0)
824
825         def seekRelative(self, diff):
826                 seekable = self.getSeek()
827                 if seekable is not None:
828                         seekable.seekRelative(1, diff)
829
830 from Screens.PVRState import PVRState, TimeshiftState
831
832 class InfoBarPVRState:
833         def __init__(self, screen=PVRState):
834                 self.onPlayStateChanged.append(self.__playStateChanged)
835                 self.pvrStateDialog = self.session.instantiateDialog(screen)
836                 self.onShow.append(self.__mayShow)
837                 self.onHide.append(self.pvrStateDialog.hide)
838         
839         def __mayShow(self):
840                 if self.seekstate != self.SEEK_STATE_PLAY and self.execing:
841                         self.pvrStateDialog.show()
842
843         def __playStateChanged(self, state):
844                 playstateString = state[3]
845                 self.pvrStateDialog["state"].setText(playstateString)
846                 self.__mayShow()
847
848 class InfoBarTimeshiftState(InfoBarPVRState):
849         def __init__(self):
850                 InfoBarPVRState.__init__(self, screen=TimeshiftState)
851
852 class InfoBarShowMovies:
853
854         # i don't really like this class. 
855         # it calls a not further specified "movie list" on up/down/movieList,
856         # so this is not more than an action map
857         def __init__(self):
858                 self["MovieListActions"] = HelpableActionMap(self, "InfobarMovieListActions", 
859                         {
860                                 "movieList": (self.showMovies, "movie list"),
861                                 "up": (self.showMovies, "movie list"),
862                                 "down": (self.showMovies, "movie list")
863                         })
864
865 # InfoBarTimeshift requires InfoBarSeek, instantiated BEFORE!
866
867 # Hrmf.
868 #
869 # Timeshift works the following way:
870 #                                         demux0   demux1                    "TimeshiftActions" "TimeshiftActivateActions" "SeekActions"
871 # - normal playback                       TUNER    unused      PLAY               enable                disable              disable
872 # - user presses "yellow" button.         TUNER    record      PAUSE              enable                disable              enable
873 # - user presess pause again              FILE     record      PLAY               enable                disable              enable
874 # - user fast forwards                    FILE     record      FF                 enable                disable              enable
875 # - end of timeshift buffer reached       TUNER    record      PLAY               enable                enable               disable
876 # - user backwards                        FILE     record      BACK  # !!         enable                disable              enable
877 #
878
879 # in other words:
880 # - when a service is playing, pressing the "timeshiftStart" button ("yellow") enables recording ("enables timeshift"),
881 # freezes the picture (to indicate timeshift), sets timeshiftMode ("activates timeshift")
882 # now, the service becomes seekable, so "SeekActions" are enabled, "TimeshiftEnableActions" are disabled.
883 # - the user can now PVR around
884 # - if it hits the end, the service goes into live mode ("deactivates timeshift", it's of course still "enabled")
885 # the service looses it's "seekable" state. It can still be paused, but just to activate timeshift right
886 # after!
887 # the seek actions will be disabled, but the timeshiftActivateActions will be enabled
888 # - if the user rewinds, or press pause, timeshift will be activated again
889
890 # note that a timeshift can be enabled ("recording") and
891 # activated (currently time-shifting).
892
893 class InfoBarTimeshift:
894         def __init__(self):
895                 self["TimeshiftActions"] = HelpableActionMap(self, "InfobarTimeshiftActions", 
896                         {
897                                 "timeshiftStart": (self.startTimeshift, _("start timeshift")),  # the "yellow key"
898                                 "timeshiftStop": (self.stopTimeshift, _("stop timeshift"))      # currently undefined :), probably 'TV'
899                         }, prio=1)
900                 self["TimeshiftActivateActions"] = ActionMap(["InfobarTimeshiftActivateActions"],
901                         {
902                                 "timeshiftActivateEnd": self.activateTimeshiftEnd, # something like "pause key"
903                                 "timeshiftActivateEndAndPause": self.activateTimeshiftEndAndPause  # something like "backward key"
904                         }, prio=-1) # priority over record
905
906                 self.timeshift_enabled = 0
907                 self.timeshift_state = 0
908                 self.ts_pause_timer = eTimer()
909                 self.ts_pause_timer.timeout.get().append(self.pauseService)
910
911                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
912                         {
913                                 iPlayableService.evStart: self.__serviceStarted,
914                                 iPlayableService.evSeekableStatusChanged: self.__seekableStatusChanged
915                         })
916         
917         def getTimeshift(self):
918                 service = self.session.nav.getCurrentService()
919                 return service and service.timeshift()
920
921         def startTimeshift(self):
922                 print "enable timeshift"
923                 ts = self.getTimeshift()
924                 if ts is None:
925 #                       self.session.open(MessageBox, _("Timeshift not possible!"), MessageBox.TYPE_ERROR)
926 #                       print "no ts interface"
927                         return 0;
928                 
929                 if self.timeshift_enabled:
930                         print "hu, timeshift already enabled?"
931                 else:
932                         if not ts.startTimeshift():
933                                 import time
934                                 self.timeshift_enabled = 1
935
936                                 # we remove the "relative time" for now.
937                                 #self.pvrStateDialog["timeshift"].setRelative(time.time())
938                                         
939                                 # PAUSE.
940                                 self.setSeekState(self.SEEK_STATE_PAUSE)
941                                 
942                                 # enable the "TimeshiftEnableActions", which will override
943                                 # the startTimeshift actions
944                                 self.__seekableStatusChanged()
945                         else:
946                                 print "timeshift failed"
947
948         def stopTimeshift(self):
949                 if not self.timeshift_enabled:
950                         return 0
951                 print "disable timeshift"
952                 ts = self.getTimeshift()
953                 if ts is None:
954                         return 0
955                 self.session.openWithCallback(self.stopTimeshiftConfirmed, MessageBox, _("Stop Timeshift?"), MessageBox.TYPE_YESNO)
956
957         def stopTimeshiftConfirmed(self, confirmed):
958                 if not confirmed:
959                         return
960
961                 ts = self.getTimeshift()
962                 if ts is None:
963                         return
964
965                 ts.stopTimeshift()
966                 self.timeshift_enabled = 0
967
968                 # disable actions
969                 self.__seekableStatusChanged()
970         
971         # activates timeshift, and seeks to (almost) the end
972         def activateTimeshiftEnd(self):
973                 ts = self.getTimeshift()
974                 
975                 if ts is None:
976                         return
977                 
978                 if ts.isTimeshiftActive():
979                         print "!! activate timeshift called - but shouldn't this be a normal pause?"
980                         self.pauseService()
981                 else:
982                         self.setSeekState(self.SEEK_STATE_PLAY)
983                         ts.activateTimeshift()
984                         self.seekRelative(0)
985         
986         # same as activateTimeshiftEnd, but pauses afterwards.
987         def activateTimeshiftEndAndPause(self):
988                 state = self.seekstate
989                 self.activateTimeshiftEnd()
990                 
991                 # well, this is "andPause", but it could be pressed from pause,
992                 # when pausing on the (fake-)"live" picture, so an un-pause
993                 # is perfectly ok.
994                 
995                 print "now, pauseService"
996                 if state == self.SEEK_STATE_PLAY:
997                         print "is PLAYING, start pause timer"
998                         self.ts_pause_timer.start(200, 1)
999                 else:
1000                         print "unpause"
1001                         self.unPauseService()
1002         
1003         def __seekableStatusChanged(self):
1004                 enabled = False
1005                 
1006                 print "self.isSeekable", self.isSeekable()
1007                 print "self.timeshift_enabled", self.timeshift_enabled
1008                 
1009                 # when this service is not seekable, but timeshift
1010                 # is enabled, this means we can activate
1011                 # the timeshift
1012                 if not self.isSeekable() and self.timeshift_enabled:
1013                         enabled = True
1014
1015                 print "timeshift activate:", enabled
1016                 self["TimeshiftActivateActions"].setEnabled(enabled)
1017
1018         def __serviceStarted(self):
1019                 self.timeshift_enabled = False
1020                 self.__seekableStatusChanged()
1021
1022 from Screens.PiPSetup import PiPSetup
1023
1024 class InfoBarExtensions:
1025         EXTENSION_SINGLE = 0
1026         EXTENSION_LIST = 1
1027         
1028         def __init__(self):
1029                 self.list = []
1030                 
1031                 self["InstantExtensionsActions"] = HelpableActionMap(self, "InfobarExtensions",
1032                         {
1033                                 "extensions": (self.showExtensionSelection, _("view extensions...")),
1034                         })
1035
1036         def addExtension(self, extension, key = None, type = EXTENSION_SINGLE):
1037                 self.list.append((type, extension, key))
1038                 
1039         def updateExtension(self, extension, key = None):
1040                 self.extensionsList.append(extension)
1041                 if key is not None:
1042                         if self.extensionKeys.has_key(key):
1043                                 key = None
1044                 
1045                 if key is None:
1046                         for x in self.availableKeys:
1047                                 if not self.extensionKeys.has_key(x):
1048                                         key = x
1049                                         break
1050
1051                 if key is not None:
1052                         self.extensionKeys[key] = len(self.extensionsList) - 1
1053                         
1054         def updateExtensions(self):
1055                 self.extensionsList = []
1056                 self.availableKeys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "red", "green", "yellow", "blue" ]
1057                 self.extensionKeys = {}
1058                 for x in self.list:
1059                         if x[0] == self.EXTENSION_SINGLE:
1060                                 self.updateExtension(x[1], x[2])
1061                         else:
1062                                 for y in x[1]():
1063                                         self.updateExtension(y[0], y[1])
1064
1065
1066         def showExtensionSelection(self):
1067                 self.updateExtensions()
1068                 extensionsList = self.extensionsList[:]
1069                 keys = []
1070                 list = []
1071                 for x in self.availableKeys:
1072                         if self.extensionKeys.has_key(x):
1073                                 entry = self.extensionKeys[x]
1074                                 extension = self.extensionsList[entry]
1075                                 if extension[2]():
1076                                         name = str(extension[0]())
1077                                         list.append((extension[0](), extension))
1078                                         keys.append(x)
1079                                         extensionsList.remove(extension)
1080                                 else:
1081                                         extensionsList.remove(extension)
1082                 for x in extensionsList:
1083                         list.append((x[0](), x))
1084                 keys += [""] * len(extensionsList)
1085                 self.session.openWithCallback(self.extensionCallback, ChoiceBox, title=_("Please choose an extension..."), list = list, keys = keys)
1086
1087         def extensionCallback(self, answer):
1088                 if answer is not None:
1089                         answer[1][1]()
1090
1091 from Tools.BoundFunction import boundFunction
1092
1093 # depends on InfoBarExtensions and InfoBarSubtitleSupport
1094 class InfoBarSubtitles:
1095         def __init__(self):
1096                 self.addExtension((self.getDisableSubtitleName, self.disableSubtitles, self.subtitlesEnabled), "4")
1097                 self.addExtension(extension = self.getSubtitleList, type = InfoBarExtensions.EXTENSION_LIST)
1098                 
1099         def getDisableSubtitleName(self):
1100                 return _("Disable subtitles")
1101
1102         def getSubtitleList(self):
1103                 list = []
1104                 s = self.getCurrentServiceSubtitle()
1105                 l = s and s.getSubtitleList() or [ ]
1106
1107                 for x in l:
1108                         list.append(((boundFunction(self.getSubtitleEntryName, x[0]), boundFunction(self.enableSubtitle, x[1]), lambda: True), None))
1109                 return list
1110         
1111         def getSubtitleEntryName(self, name):
1112                 return "Enable Subtitles: " + name
1113
1114         def enableSubtitle(self, subtitles):
1115                 print "enable subitles", subtitles
1116                 self.selected_subtitle = subtitles
1117                 self.subtitles_enabled = True
1118                 
1119         def subtitlesEnabled(self):
1120                 return self.subtitles_enabled
1121                 
1122         def disableSubtitles(self):
1123                 self.subtitles_enabled = False
1124
1125 # depends on InfoBarExtensions
1126 class InfoBarPiP:
1127         def __init__(self):
1128                 self.session.pipshown = False
1129                 
1130                 self.addExtension((self.getShowHideName, self.showPiP, self.available), "1")
1131                 self.addExtension((self.getMoveName, self.movePiP, self.pipShown), "2")
1132                 self.addExtension((self.getSwapName, self.swapPiP, self.pipShown), "3")
1133
1134         
1135         def available(self):
1136                 return True
1137         
1138         def pipShown(self):
1139                 return self.session.pipshown
1140         
1141         def getShowHideName(self):
1142                 if self.session.pipshown:
1143                         return _("Disable Picture in Picture")
1144                 else:
1145                         return _("Activate Picture in Picture")
1146                 
1147         def getSwapName(self):
1148                 return _("Swap Services")
1149                 
1150         def getMoveName(self):
1151                 return _("Move Picture in Picture")
1152         
1153         def showPiP(self):
1154                 if self.session.pipshown:
1155                         del self.session.pip
1156                         self.session.pipshown = False
1157                 else:
1158                         self.session.pip = self.session.instantiateDialog(PictureInPicture)
1159                         newservice = self.session.nav.getCurrentlyPlayingServiceReference()
1160                         if self.session.pip.playService(newservice):
1161                                 self.session.pipshown = True
1162                                 self.session.pip.servicePath = self.servicelist.getCurrentServicePath()
1163                         else:
1164                                 self.session.pipshown = False
1165                                 del self.session.pip
1166                         self.session.nav.playService(newservice)
1167         
1168         def swapPiP(self):
1169                 swapservice = self.session.nav.getCurrentlyPlayingServiceReference()
1170                 if self.session.pip.servicePath:
1171                         servicepath = self.servicelist.getCurrentServicePath()
1172                         ref=servicepath[len(servicepath)-1]
1173                         pipref=self.session.pip.getCurrentService()
1174                         self.session.pip.playService(swapservice)
1175                         self.servicelist.setCurrentServicePath(self.session.pip.servicePath)
1176                         if pipref.toString() != ref.toString(): # is a subservice ?
1177                                 self.session.nav.stopService() # stop portal
1178                                 self.session.nav.playService(pipref) # start subservice
1179                         self.session.pip.servicePath=servicepath
1180         
1181         def movePiP(self):
1182                 self.session.open(PiPSetup, pip = self.session.pip)
1183
1184 from RecordTimer import parseEvent
1185
1186 class InfoBarInstantRecord:
1187         """Instant Record - handles the instantRecord action in order to 
1188         start/stop instant records"""
1189         def __init__(self):
1190                 self["InstantRecordActions"] = HelpableActionMap(self, "InfobarInstantRecord",
1191                         {
1192                                 "instantRecord": (self.instantRecord, _("Instant Record...")),
1193                         })
1194                 self.recording = []
1195                 self["BlinkingPoint"] = BlinkingPixmapConditional()
1196                 self["BlinkingPoint"].hide()
1197                 self["BlinkingPoint"].setConnect(self.session.nav.RecordTimer.isRecording)
1198
1199         def stopCurrentRecording(self, entry = -1):     
1200                 if entry is not None and entry != -1:
1201                         self.session.nav.RecordTimer.removeEntry(self.recording[entry])
1202                         self.recording.remove(self.recording[entry])
1203
1204         def startInstantRecording(self, limitEvent = False):
1205                 serviceref = self.session.nav.getCurrentlyPlayingServiceReference()
1206                 
1207                 # try to get event info
1208                 event = None
1209                 try:
1210                         service = self.session.nav.getCurrentService()
1211                         epg = eEPGCache.getInstance()
1212                         event = epg.lookupEventTime(serviceref, -1, 0)
1213                         if event is None:
1214                                 info = service.info()
1215                                 ev = info.getEvent(0)
1216                                 event = ev
1217                 except:
1218                         pass
1219
1220                 begin = time.time()
1221                 end = time.time() + 3600 * 10
1222                 name = "instant record"
1223                 description = ""
1224                 eventid = None
1225                 
1226                 if event is not None:
1227                         curEvent = parseEvent(event)
1228                         name = curEvent[2]
1229                         description = curEvent[3]
1230                         eventid = curEvent[4]
1231                         if limitEvent:
1232                                 end = curEvent[1]
1233                 else:
1234                         if limitEvent:
1235                                 self.session.open(MessageBox, _("No event info found, recording indefinitely."), MessageBox.TYPE_INFO)
1236                                 
1237                 data = (begin, end, name, description, eventid)
1238                 
1239                 recording = self.session.nav.recordWithTimer(serviceref, *data)
1240                 recording.dontSave = True
1241                 self.recording.append(recording)
1242                 
1243                 #self["BlinkingPoint"].setConnect(lambda: self.recording.isRunning())
1244                 
1245         def isInstantRecordRunning(self):
1246                 print "self.recording:", self.recording
1247                 if len(self.recording) > 0:
1248                         for x in self.recording:
1249                                 if x.isRunning():
1250                                         return True
1251                 return False
1252
1253         def recordQuestionCallback(self, answer):
1254                 print "pre:\n", self.recording
1255                 
1256                 if answer is None or answer[1] == "no":
1257                         return
1258                 list = []
1259                 recording = self.recording[:]
1260                 for x in recording:
1261                         if not x in self.session.nav.RecordTimer.timer_list:
1262                                 self.recording.remove(x)
1263                         elif x.dontSave and x.isRunning():
1264                                 list.append(TimerEntryComponent(x, False))              
1265
1266                 if answer[1] == "changeduration":
1267                         if len(self.recording) == 1:
1268                                 self.changeDuration(0)
1269                         else:
1270                                 self.session.openWithCallback(self.changeDuration, TimerSelection, list)
1271                 elif answer[1] == "stop":
1272                         if len(self.recording) == 1:
1273                                 self.stopCurrentRecording(0)
1274                         else:
1275                                 self.session.openWithCallback(self.stopCurrentRecording, TimerSelection, list)
1276                 if answer[1] == "indefinitely" or answer[1] == "manualduration" or answer[1] == "event":
1277                         limitEvent = False
1278                         if answer[1] == "event":
1279                                 limitEvent = True
1280                         if answer[1] == "manualduration":
1281                                 self.selectedEntry = len(self.recording)
1282                                 self.session.openWithCallback(self.inputCallback, InputBox, title=_("How many minutes do you want to record?"), text="5", maxSize=False, type=Input.NUMBER)
1283                         self.startInstantRecording(limitEvent = limitEvent)
1284                         
1285                 print "after:\n", self.recording
1286
1287         def changeDuration(self, entry):
1288                 if entry is not None:
1289                         self.selectedEntry = entry
1290                         self.session.openWithCallback(self.inputCallback, InputBox, title=_("How many minutes do you want to record?"), text="5", maxSize=False, type=Input.NUMBER)
1291
1292         def inputCallback(self, value):
1293                 if value is not None:
1294                         print "stopping recording after", int(value), "minutes."
1295                         self.recording[self.selectedEntry].end = time.time() + 60 * int(value)
1296                         self.session.nav.RecordTimer.timeChanged(self.recording[self.selectedEntry])
1297
1298         def instantRecord(self):
1299                 try:
1300                         stat = os.stat(resolveFilename(SCOPE_HDD))
1301                 except:
1302                         self.session.open(MessageBox, _("No HDD found or HDD not initialized!"), MessageBox.TYPE_ERROR)
1303                         return
1304
1305                 if self.isInstantRecordRunning():
1306                         self.session.openWithCallback(self.recordQuestionCallback, ChoiceBox, title=_("A recording is currently running.\nWhat do you want to do?"), list=[(_("stop recording"), "stop"), (_("change recording (duration)"), "changeduration"), (_("add recording (indefinitely)"), "indefinitely"), (_("add recording (stop after current event)"), "event"), (_("add recording (enter recording duration)"), "manualduration"), (_("do nothing"), "no")])
1307                 else:
1308                         self.session.openWithCallback(self.recordQuestionCallback, ChoiceBox, title=_("Start recording?"), list=[(_("add recording (indefinitely)"), "indefinitely"), (_("add recording (stop after current event)"), "event"), (_("add recording (enter recording duration)"), "manualduration"),(_("don't record"), "no")])
1309
1310 from Tools.ISO639 import LanguageCodes
1311
1312 class InfoBarAudioSelection:
1313         def __init__(self):
1314                 self["AudioSelectionAction"] = HelpableActionMap(self, "InfobarAudioSelectionActions", 
1315                         {
1316                                 "audioSelection": (self.audioSelection, _("Audio Options...")),
1317                         })
1318
1319         def audioSelection(self):
1320                 service = self.session.nav.getCurrentService()
1321                 audio = service and service.audioTracks()
1322                 self.audioTracks = audio
1323                 n = audio and audio.getNumberOfTracks() or 0
1324                 keys = [ "red", "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] + [""]*n
1325                 tlist = []
1326                 print "tlist:", tlist
1327                 if n > 0:
1328                         self.audioChannel = service.audioChannel()
1329
1330                         for x in range(n):
1331                                 i = audio.getTrackInfo(x)
1332                                 language = i.getLanguage()
1333                                 description = i.getDescription()
1334         
1335                                 if len(language) == 3:
1336                                         if language in LanguageCodes:
1337                                                 language = LanguageCodes[language][0]
1338         
1339                                 if len(description):
1340                                         description += " (" + language + ")"
1341                                 else:
1342                                         description = language
1343         
1344                                 tlist.append((description, x))
1345                         
1346                         selectedAudio = tlist[0][1]
1347                         tlist.sort(lambda x,y : cmp(x[0], y[0]))
1348
1349                         selection = 2
1350                         for x in tlist:
1351                                 if x[1] != selectedAudio:
1352                                         selection += 1
1353                                 else:
1354                                         break
1355
1356                         tlist = [([_("Left"), _("Stereo"), _("Right")][self.audioChannel.getCurrentChannel()], "mode"), ("--", "")] + tlist
1357                         self.session.openWithCallback(self.audioSelected, ChoiceBox, title=_("Select audio track"), list = tlist, selection = selection, keys = keys)
1358                 else:
1359                         del self.audioTracks
1360
1361         def audioSelected(self, audio):
1362                 if audio is not None:
1363                         if isinstance(audio[1], str):
1364                                 if audio[1] == "mode":
1365                                         keys = ["red", "green", "yellow"]
1366                                         selection = self.audioChannel.getCurrentChannel()
1367                                         tlist = [(_("left"), 0), (_("stereo"), 1), (_("right"), 2)]
1368                                         self.session.openWithCallback(self.modeSelected, ChoiceBox, title=_("Select audio mode"), list = tlist, selection = selection, keys = keys)
1369                         else:
1370                                 del self.audioChannel
1371                                 if self.session.nav.getCurrentService().audioTracks().getNumberOfTracks() > audio[1]:
1372                                         self.audioTracks.selectTrack(audio[1])
1373                 else:
1374                         del self.audioChannel
1375                 del self.audioTracks
1376
1377         def modeSelected(self, mode):
1378                 if mode is not None:
1379                         self.audioChannel.selectChannel(mode[1])
1380                 del self.audioChannel
1381
1382                 
1383 class InfoBarSubserviceSelection:
1384         def __init__(self):
1385                 self["SubserviceSelectionAction"] = HelpableActionMap(self, "InfobarSubserviceSelectionActions",
1386                         {
1387                                 "subserviceSelection": (self.subserviceSelection, _("Subservice list...")),
1388                         })
1389
1390                 self["SubserviceQuickzapAction"] = HelpableActionMap(self, "InfobarSubserviceQuickzapActions",
1391                         {
1392                                 "nextSubservice": (self.nextSubservice, _("Switch to next subservice")),
1393                                 "prevSubservice": (self.prevSubservice, _("Switch to previous subservice"))
1394                         }, -1)
1395                 self["SubserviceQuickzapAction"].setEnabled(False)
1396
1397                 self.session.nav.event.append(self.checkSubservicesAvail) # we like to get service events
1398
1399         def checkSubservicesAvail(self, ev):
1400                 if ev == iPlayableService.evUpdatedEventInfo:
1401                         service = self.session.nav.getCurrentService()
1402                         subservices = service and service.subServices()
1403                         if not subservices or subservices.getNumberOfSubservices() == 0:
1404                                 self["SubserviceQuickzapAction"].setEnabled(False)
1405
1406         def nextSubservice(self):
1407                 self.changeSubservice(+1)
1408
1409         def prevSubservice(self):
1410                 self.changeSubservice(-1)
1411
1412         def changeSubservice(self, direction):
1413                 service = self.session.nav.getCurrentService()
1414                 subservices = service and service.subServices()
1415                 n = subservices and subservices.getNumberOfSubservices()
1416                 if n and n > 0:
1417                         selection = -1
1418                         ref = self.session.nav.getCurrentlyPlayingServiceReference()
1419                         for x in range(n):
1420                                 if subservices.getSubservice(x).toString() == ref.toString():
1421                                         selection = x
1422                         if selection != -1:
1423                                 selection += direction
1424                                 if selection >= n:
1425                                         selection=0
1426                                 elif selection < 0:
1427                                         selection=n-1
1428                                 newservice = subservices.getSubservice(selection)
1429                                 if newservice.valid():
1430                                         del subservices
1431                                         del service
1432                                         self.session.nav.playService(newservice)
1433
1434         def subserviceSelection(self):
1435                 service = self.session.nav.getCurrentService()
1436                 subservices = service and service.subServices()
1437                 
1438                 n = subservices and subservices.getNumberOfSubservices()
1439                 selection = 0
1440                 if n and n > 0:
1441                         ref = self.session.nav.getCurrentlyPlayingServiceReference()
1442                         tlist = []
1443                         for x in range(n):
1444                                 i = subservices.getSubservice(x)
1445                                 if i.toString() == ref.toString():
1446                                         selection = x
1447                                 tlist.append((i.getName(), i))
1448
1449                         tlist = [(_("Quickzap"), "quickzap", service.subServices()), ("--", "")] + tlist
1450
1451                         keys = ["red", "",  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ] + [""] * n
1452
1453                         self.session.openWithCallback(self.subserviceSelected, ChoiceBox, title=_("Please select a subservice..."), list = tlist, selection = selection + 2, keys = keys)
1454
1455         def subserviceSelected(self, service):
1456                 if not service is None:
1457                         if isinstance(service[1], str):
1458                                 if service[1] == "quickzap":
1459                                         from Screens.SubservicesQuickzap import SubservicesQuickzap
1460                                         self.session.open(SubservicesQuickzap, service[2])
1461                         else:
1462                                 self["SubserviceQuickzapAction"].setEnabled(True)
1463                                 self.session.nav.playService(service[1])
1464
1465 class InfoBarAdditionalInfo:
1466         def __init__(self):
1467                 self["NimA"] = Pixmap()
1468                 self["NimB"] = Pixmap()
1469                 self["NimA_Active"] = Pixmap()
1470                 self["NimB_Active"] = Pixmap()
1471
1472                 self["RecordingPossible"] = Boolean(fixed=harddiskmanager.HDDCount() > 0)
1473                 self["TimeshiftPossible"] = self["RecordingPossible"]
1474                 self["ExtensionsAvailable"] = Boolean(fixed=1)
1475
1476                 self.session.nav.event.append(self.gotServiceEvent) # we like to get service events
1477                 res_mgr = eDVBResourceManagerPtr()
1478                 if eDVBResourceManager.getInstance(res_mgr) == 0:
1479                         res_mgr.frontendUseMaskChanged.get().append(self.tunerUseMaskChanged)
1480
1481         def tunerUseMaskChanged(self, mask):
1482                 if mask&1:
1483                         self["NimA_Active"].show()
1484                 else:
1485                         self["NimA_Active"].hide()
1486                 if mask&2:
1487                         self["NimB_Active"].show()
1488                 else:
1489                         self["NimB_Active"].hide()
1490
1491         def checkTunerState(self, service):
1492                 info = service.frontendInfo()
1493                 feNumber = info and info.getFrontendInfo(iFrontendInformation.frontendNumber)
1494                 if feNumber is None:
1495                         self["NimA"].hide()
1496                         self["NimB"].hide()
1497                 elif feNumber == 0:
1498                         self["NimB"].hide()
1499                         self["NimA"].show()
1500                 elif feNumber == 1:
1501                         self["NimA"].hide()
1502                         self["NimB"].show()
1503
1504         def gotServiceEvent(self, ev):
1505                 service = self.session.nav.getCurrentService()
1506                 if ev == iPlayableService.evStart:
1507                         self.checkTunerState(service)
1508
1509 class InfoBarNotifications:
1510         def __init__(self):
1511                 self.onExecBegin.append(self.checkNotifications)
1512                 Notifications.notificationAdded.append(self.checkNotificationsIfExecing)
1513                 self.onClose.append(self.__removeNotification)
1514         
1515         def __removeNotification(self):
1516                 Notifications.notificationAdded.remove(self.checkNotificationsIfExecing)
1517         
1518         def checkNotificationsIfExecing(self):
1519                 if self.execing:
1520                         self.checkNotifications()
1521
1522         def checkNotifications(self):
1523                 if len(Notifications.notifications):
1524                         n = Notifications.notifications[0]
1525                         Notifications.notifications = Notifications.notifications[1:]
1526                         cb = n[0]
1527                         if cb is not None:
1528                                 self.session.openWithCallback(cb, n[1], *n[2], **n[3])
1529                         else:
1530                                 self.session.open(n[1], *n[2], **n[3])
1531
1532 class InfoBarServiceNotifications:
1533         def __init__(self):
1534                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
1535                         {
1536                                 iPlayableService.evEnd: self.serviceHasEnded
1537                         })
1538
1539         def serviceHasEnded(self):
1540                 print "service end!"
1541
1542                 try:
1543                         self.setSeekState(self.SEEK_STATE_PLAY)
1544                 except:
1545                         pass
1546
1547 class InfoBarCueSheetSupport:
1548         CUT_TYPE_IN = 0
1549         CUT_TYPE_OUT = 1
1550         CUT_TYPE_MARK = 2
1551         CUT_TYPE_LAST = 3
1552         
1553         ENABLE_RESUME_SUPPORT = False
1554         
1555         def __init__(self):
1556                 self["CueSheetActions"] = HelpableActionMap(self, "InfobarCueSheetActions", 
1557                         {
1558                                 "jumpPreviousMark": (self.jumpPreviousMark, "jump to next marked position"),
1559                                 "jumpNextMark": (self.jumpNextMark, "jump to previous marked position"),
1560                                 "toggleMark": (self.toggleMark, "toggle a cut mark at the current position")
1561                         }, prio=1) 
1562                 
1563                 self.cut_list = [ ]
1564                 self.is_closing = False
1565                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
1566                         {
1567                                 iPlayableService.evStart: self.__serviceStarted,
1568                         })
1569
1570         def __serviceStarted(self):
1571                 if self.is_closing:
1572                         return
1573                 print "new service started! trying to download cuts!"
1574                 self.downloadCuesheet()
1575                 
1576                 if self.ENABLE_RESUME_SUPPORT:
1577                         last = None
1578                         
1579                         for (pts, what) in self.cut_list:
1580                                 if what == self.CUT_TYPE_LAST:
1581                                         last = pts
1582                         
1583                         if last is not None:
1584                                 self.resume_point = last
1585                                 Notifications.AddNotificationWithCallback(self.playLastCB, MessageBox, _("Do you want to resume this playback?"), timeout=10)
1586
1587         def playLastCB(self, answer):
1588                 if answer == True:
1589                         seekable = self.__getSeekable()
1590                         if seekable is not None:
1591                                 seekable.seekTo(self.resume_point)
1592
1593         def __getSeekable(self):
1594                 service = self.session.nav.getCurrentService()
1595                 if service is None:
1596                         return None
1597                 return service.seek()
1598
1599         def cueGetCurrentPosition(self):
1600                 seek = self.__getSeekable()
1601                 if seek is None:
1602                         return None
1603                 r = seek.getPlayPosition()
1604                 if r[0]:
1605                         return None
1606                 return long(r[1])
1607
1608         def jumpPreviousNextMark(self, cmp, alternative=None):
1609                 current_pos = self.cueGetCurrentPosition()
1610                 if current_pos is None:
1611                         return
1612                 mark = self.getNearestCutPoint(current_pos, cmp=cmp)
1613                 if mark is not None:
1614                         pts = mark[0]
1615                 elif alternative is not None:
1616                         pts = alternative
1617                 else:
1618                         return
1619
1620                 seekable = self.__getSeekable()
1621                 if seekable is not None:
1622                         seekable.seekTo(pts)
1623
1624         def jumpPreviousMark(self):
1625                 # we add 2 seconds, so if the play position is <2s after
1626                 # the mark, the mark before will be used
1627                 self.jumpPreviousNextMark(lambda x: -x-5*90000, alternative=0)
1628
1629         def jumpNextMark(self):
1630                 self.jumpPreviousNextMark(lambda x: x)
1631
1632         def getNearestCutPoint(self, pts, cmp=abs):
1633                 # can be optimized
1634                 nearest = None
1635                 for cp in self.cut_list:
1636                         diff = cmp(cp[0] - pts)
1637                         if diff >= 0 and (nearest is None or cmp(nearest[0] - pts) > diff):
1638                                 nearest = cp
1639                 return nearest
1640
1641         def toggleMark(self, onlyremove=False, onlyadd=False, tolerance=5*90000, onlyreturn=False):
1642                 current_pos = self.cueGetCurrentPosition()
1643                 if current_pos is None:
1644                         print "not seekable"
1645                         return
1646                 
1647                 nearest_cutpoint = self.getNearestCutPoint(current_pos)
1648                 
1649                 if nearest_cutpoint is not None and abs(nearest_cutpoint[0] - current_pos) < tolerance:
1650                         if onlyreturn:
1651                                 return nearest_cutpoint
1652                         if not onlyadd:
1653                                 self.removeMark(nearest_cutpoint)
1654                 elif not onlyremove and not onlyreturn:
1655                         self.addMark((current_pos, self.CUT_TYPE_MARK))
1656                 
1657                 if onlyreturn:
1658                         return None
1659
1660         def addMark(self, point):
1661                 bisect.insort(self.cut_list, point)
1662                 self.uploadCuesheet()
1663
1664         def removeMark(self, point):
1665                 self.cut_list.remove(point)
1666                 self.uploadCuesheet()
1667
1668         def __getCuesheet(self):
1669                 service = self.session.nav.getCurrentService()
1670                 if service is None:
1671                         return None
1672                 return service.cueSheet()
1673
1674         def uploadCuesheet(self):
1675                 cue = self.__getCuesheet()
1676
1677                 if cue is None:
1678                         print "upload failed, no cuesheet interface"
1679                         return
1680                 cue.setCutList(self.cut_list)
1681
1682         def downloadCuesheet(self):
1683                 cue = self.__getCuesheet()
1684
1685                 if cue is None:
1686                         print "upload failed, no cuesheet interface"
1687                         return
1688                 self.cut_list = cue.getCutList()
1689
1690 class InfoBarSummary(Screen):
1691         skin = """
1692         <screen position="0,0" size="132,64">
1693                 <widget source="CurrentTime" render="Label" position="50,46" size="82,18" font="Regular;16" >
1694                         <convert type="ClockToText">WithSeconds</convert>
1695                 </widget>
1696                 <widget source="CurrentService" render="Label" position="0,4" size="132,42" font="Regular;18" >
1697                         <convert type="ServiceName">Name</convert>
1698                 </widget>
1699         </screen>"""
1700
1701         def __init__(self, session, parent):
1702                 Screen.__init__(self, session)
1703                 self["CurrentService"] = CurrentService(self.session.nav)
1704                 self["CurrentTime"] = Clock()
1705
1706 class InfoBarSummarySupport:
1707         def __init__(self):
1708                 pass
1709         
1710         def createSummary(self):
1711                 return InfoBarSummary
1712
1713 class InfoBarTeletextPlugin:
1714         def __init__(self):
1715                 self.teletext_plugin = None
1716                 
1717                 for p in plugins.getPlugins(PluginDescriptor.WHERE_TELETEXT):
1718                         self.teletext_plugin = p
1719                 
1720                 if self.teletext_plugin is not None:
1721                         self["TeletextActions"] = HelpableActionMap(self, "InfobarTeletextActions",
1722                                 {
1723                                         "startTeletext": (self.startTeletext, _("View teletext..."))
1724                                 })
1725                 else:
1726                         print "no teletext plugin found!"
1727
1728         def startTeletext(self):
1729                 self.teletext_plugin(session=self.session, service=self.session.nav.getCurrentService())
1730
1731 class InfoBarSubtitleSupport(object):
1732         def __init__(self):
1733                 object.__init__(self)
1734                 self.subtitle_window = self.session.instantiateDialog(SubtitleDisplay)
1735                 self.__subtitles_enabled = False
1736
1737                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
1738                         {
1739                                 iPlayableService.evStart: self.__serviceStarted,
1740                         })
1741
1742         def __serviceStarted(self):
1743                 # reenable if it was enabled
1744                 r = self.__subtitles_enabled
1745                 self.__subtitles_enabled = False
1746                 self.__selected_subtitle = None
1747                 self.setSubtitlesEnable(r)
1748
1749         def getCurrentServiceSubtitle(self):
1750                 service = self.session.nav.getCurrentService()
1751                 return service and service.subtitle()
1752         
1753         def setSubtitlesEnable(self, enable=True):
1754                 subtitle = self.getCurrentServiceSubtitle()
1755                 if enable and self.__selected_subtitle:
1756                         if subtitle and not self.__subtitles_enabled:
1757                                 subtitle.enableSubtitles(self.subtitle_window.instance, self.selected_subtitle)
1758                                 self.subtitle_window.show()
1759                                 self.__subtitles_enabled = True
1760                 else:
1761                         if subtitle:
1762                                 subtitle.disableSubtitles(self.subtitle_window.instance)
1763
1764                         self.subtitle_window.hide()
1765                         self.__subtitles_enabled = False
1766
1767         def setSelectedSubtitle(self, subtitle):
1768                 if self.__selected_subtitle != subtitle and self.subtitles_enabled:
1769                         # kick
1770                         self.__selected_subtitle = subtitle
1771                         self.__serviceStarted()
1772                 else:
1773                         self.__selected_subtitle = subtitle
1774
1775         subtitles_enabled = property(lambda self: self.__subtitles_enabled, setSubtitlesEnable)
1776         selected_subtitle = property(lambda self: self.__selected_subtitle, setSelectedSubtitle)