get rid of unneede global keywords
[enigma2.git] / lib / python / Screens / InfoBarGenerics.py
index a85bcc97d0015b1e621ae0889651ae729a632d63..31108c8ad17d8e976e190b610696a0036f542818 100644 (file)
@@ -32,17 +32,18 @@ from Screens.MinuteInput import MinuteInput
 from Screens.TimerSelection import TimerSelection
 from Screens.PictureInPicture import PictureInPicture
 from Screens.SubtitleDisplay import SubtitleDisplay
+from Screens.SleepTimerEdit import SleepTimerEdit
 from ServiceReference import ServiceReference
 
 from Tools import Notifications
-from Tools.Directories import *
+from Tools.Directories import SCOPE_HDD, resolveFilename
 
-#from enigma import eTimer, eDVBVolumecontrol, quitMainloop
-from enigma import *
+from enigma import eTimer, eServiceCenter, eDVBServicePMTHandler, iServiceInformation, \
+       iPlayableService, eServiceReference, eDVBResourceManager, iFrontendInformation, eEPGCache
 
-import time
-import os
-import bisect
+from time import time
+from os import stat as os_stat
+from bisect import insort
 
 # hack alert!
 from Menu import MainMenu, mdom
@@ -209,9 +210,9 @@ class InfoBarNumberZap:
                                serviceIterator = servicelist.getNext()
                                if not serviceIterator.valid(): #check end of list
                                        break
-                               if serviceIterator.flags: #assume normal dvb service have no flags set
-                                       continue
-                               num -= 1;
+                               playable = not (serviceIterator.flags & (eServiceReference.isMarker|eServiceReference.isDirectory))
+                               if playable:
+                                       num -= 1;
                        if not num: #found service with searched number ?
                                return serviceIterator, 0
                return None, num
@@ -220,18 +221,17 @@ class InfoBarNumberZap:
                bouquet = self.servicelist.bouquet_root
                service = None
                serviceHandler = eServiceCenter.getInstance()
-               if bouquet.toString().find('FROM BOUQUET "bouquets.') == -1: #FIXME HACK
+               if not config.usage.multibouquet.value:
                        service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
                else:
                        bouquetlist = serviceHandler.list(bouquet)
                        if not bouquetlist is None:
                                while number:
-                                       bouquet = self.servicelist.appendDVBTypes(bouquetlist.getNext())
+                                       bouquet = bouquetlist.getNext()
                                        if not bouquet.valid(): #check end of list
                                                break
-                                       if (bouquet.flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory:
-                                               continue
-                                       service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
+                                       if bouquet.flags & eServiceReference.isDirectory:
+                                               service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
                if not service is None:
                        if self.servicelist.getRoot() != bouquet: #already in correct bouquet?
                                self.servicelist.clearPath()
@@ -424,7 +424,7 @@ class InfoBarEPG:
                                service = servicelist.getNext()
                                if not service.valid(): #check if end of list
                                        break
-                               if service.flags: #ignore non playable services
+                               if service.flags & (eServiceReference.isDirectory | eServiceReference.isMarker): #ignore non playable services
                                        continue
                                services.append(ServiceReference(service))
                return services
@@ -596,6 +596,7 @@ class InfoBarSeek:
 
                self["SeekActions"] = InfoBarSeekActionMap(self, "InfobarSeekActions", 
                        {
+                               "playpauseService": (self.playpauseService, _("pause")),
                                "pauseService": (self.pauseService, _("pause")),
                                "unPauseService": (self.unPauseService, _("continue")),
                                
@@ -692,6 +693,12 @@ class InfoBarSeek:
                self.checkSkipShowHideLock()
 
                return True
+       
+       def playpauseService(self):
+               if self.seekstate != self.SEEK_STATE_PLAY:
+                       self.unPauseService()
+               else:
+                       self.pauseService()
 
        def pauseService(self):
                if self.seekstate == self.SEEK_STATE_PAUSE:
@@ -856,22 +863,26 @@ class InfoBarPVRState:
        def __init__(self, screen=PVRState):
                self.onPlayStateChanged.append(self.__playStateChanged)
                self.pvrStateDialog = self.session.instantiateDialog(screen)
-               self.onShow.append(self.__mayShow)
+               self.onShow.append(self._mayShow)
                self.onHide.append(self.pvrStateDialog.hide)
-       
-       def __mayShow(self):
-               if self.seekstate != self.SEEK_STATE_PLAY and self.execing:
+
+       def _mayShow(self):
+               if self.execing and self.seekstate != self.SEEK_STATE_PLAY:
                        self.pvrStateDialog.show()
 
        def __playStateChanged(self, state):
                playstateString = state[3]
                self.pvrStateDialog["state"].setText(playstateString)
-               self.__mayShow()
+               self._mayShow()
 
 class InfoBarTimeshiftState(InfoBarPVRState):
        def __init__(self):
                InfoBarPVRState.__init__(self, screen=TimeshiftState)
 
+       def _mayShow(self):
+               if self.execing and self.timeshift_enabled:
+                       self.pvrStateDialog.show()
+
 class InfoBarShowMovies:
 
        # i don't really like this class. 
@@ -953,7 +964,6 @@ class InfoBarTimeshift:
                        print "hu, timeshift already enabled?"
                else:
                        if not ts.startTimeshift():
-                               import time
                                self.timeshift_enabled = 1
 
                                # we remove the "relative time" for now.
@@ -1120,7 +1130,6 @@ class InfoBarPlugins:
        def __init__(self):
                self.addExtension(extension = self.getPluginList, type = InfoBarExtensions.EXTENSION_LIST)
                
-               
        def getPluginName(self, name):
                return name
                
@@ -1133,6 +1142,20 @@ class InfoBarPlugins:
        def runPlugin(self, plugin):
                plugin(session = self.session)
 
+# depends on InfoBarExtensions
+class InfoBarSleepTimer:
+       def __init__(self):
+               self.addExtension((self.getSleepTimerName, self.showSleepTimerSetup, self.available), "1")      
+               
+       def available(self):
+               return True
+
+       def getSleepTimerName(self):
+               return _("Sleep Timer")
+
+       def showSleepTimerSetup(self):
+               self.session.open(SleepTimerEdit)
+
 # depends on InfoBarExtensions
 class InfoBarPiP:
        def __init__(self):
@@ -1227,8 +1250,8 @@ class InfoBarInstantRecord:
                except:
                        pass
 
-               begin = time.time()
-               end = time.time() + 3600 * 10
+               begin = time()
+               end = time() + 3600 * 10
                name = "instant record"
                description = ""
                eventid = None
@@ -1302,20 +1325,32 @@ class InfoBarInstantRecord:
        def inputCallback(self, value):
                if value is not None:
                        print "stopping recording after", int(value), "minutes."
-                       self.recording[self.selectedEntry].end = time.time() + 60 * int(value)
+                       self.recording[self.selectedEntry].end = time() + 60 * int(value)
                        self.session.nav.RecordTimer.timeChanged(self.recording[self.selectedEntry])
 
        def instantRecord(self):
                try:
-                       stat = os.stat(resolveFilename(SCOPE_HDD))
+                       stat = os_stat(resolveFilename(SCOPE_HDD))
                except:
                        self.session.open(MessageBox, _("No HDD found or HDD not initialized!"), MessageBox.TYPE_ERROR)
                        return
 
                if self.isInstantRecordRunning():
-                       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")])
+                       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")])
                else:
-                       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")])
+                       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")])
 
 from Tools.ISO639 import LanguageCodes
 
@@ -1342,9 +1377,8 @@ class InfoBarAudioSelection:
                                language = i.getLanguage()
                                description = i.getDescription()
        
-                               if len(language) == 3:
-                                       if language in LanguageCodes:
-                                               language = LanguageCodes[language][0]
+                               if LanguageCodes.has_key(language):
+                                       language = LanguageCodes[language][0]
        
                                if len(description):
                                        description += " (" + language + ")"
@@ -1405,6 +1439,8 @@ class InfoBarSubserviceSelection:
 
                self.session.nav.event.append(self.checkSubservicesAvail) # we like to get service events
 
+               self.bsel = None
+
        def checkSubservicesAvail(self, ev):
                if ev == iPlayableService.evUpdatedEventInfo:
                        service = self.session.nav.getCurrentService()
@@ -1445,7 +1481,7 @@ class InfoBarSubserviceSelection:
        def subserviceSelection(self):
                service = self.session.nav.getCurrentService()
                subservices = service and service.subServices()
-               
+               self.bouquets = self.servicelist.getBouquetList()
                n = subservices and subservices.getNumberOfSubservices()
                selection = 0
                if n and n > 0:
@@ -1457,13 +1493,22 @@ class InfoBarSubserviceSelection:
                                        selection = x
                                tlist.append((i.getName(), i))
 
-                       tlist = [(_("Quickzap"), "quickzap", service.subServices()), ("--", "")] + tlist
-
-                       keys = ["red", "",  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ] + [""] * n
+                       if self.bouquets and len(self.bouquets):
+                               keys = ["red", "green", "",  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ] + [""] * n
+                               if config.usage.multibouquet.value:
+                                       tlist = [(_("Quickzap"), "quickzap", service.subServices()), (_("Add to bouquet"), "CALLFUNC", self.addSubserviceToBouquetCallback), ("--", "")] + tlist
+                               else:
+                                       tlist = [(_("Quickzap"), "quickzap", service.subServices()), (_("Add to favourites"), "CALLFUNC", self.addSubserviceToBouquetCallback), ("--", "")] + tlist
+                               selection += 3
+                       else:
+                               tlist = [(_("Quickzap"), "quickzap", service.subServices()), ("--", "")] + tlist
+                               keys = ["red", "",  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ] + [""] * n
+                               selection += 2
 
-                       self.session.openWithCallback(self.subserviceSelected, ChoiceBox, title=_("Please select a subservice..."), list = tlist, selection = selection + 2, keys = keys)
+                       self.session.openWithCallback(self.subserviceSelected, ChoiceBox, title=_("Please select a subservice..."), list = tlist, selection = selection, keys = keys)
 
        def subserviceSelected(self, service):
+               del self.bouquets
                if not service is None:
                        if isinstance(service[1], str):
                                if service[1] == "quickzap":
@@ -1475,6 +1520,32 @@ class InfoBarSubserviceSelection:
                                        self.doShow()
                                self.session.nav.playService(service[1])
 
+       def addSubserviceToBouquetCallback(self, service):
+               if len(service) > 1 and isinstance(service[1], eServiceReference):
+                       self.selectedSubservice = service
+                       if self.bouquets is None:
+                               cnt = 0
+                       else:
+                               cnt = len(self.bouquets)
+                       if cnt > 1: # show bouquet list
+                               self.bsel = self.session.openWithCallback(self.bouquetSelClosed, BouquetSelector, self.bouquets, self.addSubserviceToBouquet)
+                       elif cnt == 1: # add to only one existing bouquet
+                               self.addSubserviceToBouquet(self.bouquets[0][1])
+                               self.session.open(MessageBox, _("Service has been added to the favourites."), MessageBox.TYPE_INFO)
+
+       def bouquetSelClosed(self, confirmed):
+               self.bsel = None
+               del self.selectedSubservice
+               if confirmed:
+                       self.session.open(MessageBox, _("Service has been added to the selected bouquet."), MessageBox.TYPE_INFO)
+
+       def addSubserviceToBouquet(self, dest):
+               self.servicelist.addServiceToBouquet(dest, self.selectedSubservice[1])
+               if self.bsel:
+                       self.bsel.close(True)
+               else:
+                       del self.selectedSubservice
+
 class InfoBarAdditionalInfo:
        def __init__(self):
                self["NimA"] = Pixmap()
@@ -1487,8 +1558,8 @@ class InfoBarAdditionalInfo:
                self["ExtensionsAvailable"] = Boolean(fixed=1)
 
                self.session.nav.event.append(self.gotServiceEvent) # we like to get service events
-               res_mgr = eDVBResourceManagerPtr()
-               if eDVBResourceManager.getInstance(res_mgr) == 0:
+               res_mgr = eDVBResourceManager.getInstance()
+               if res_mgr:
                        res_mgr.frontendUseMaskChanged.get().append(self.tunerUseMaskChanged)
 
        def tunerUseMaskChanged(self, mask):
@@ -1502,7 +1573,7 @@ class InfoBarAdditionalInfo:
                        self["NimB_Active"].hide()
 
        def checkTunerState(self, service):
-               info = service.frontendInfo()
+               info = service and service.frontendInfo()
                feNumber = info and info.getFrontendInfo(iFrontendInformation.frontendNumber)
                if feNumber is None:
                        self["NimA"].hide()
@@ -1516,7 +1587,7 @@ class InfoBarAdditionalInfo:
 
        def gotServiceEvent(self, ev):
                service = self.session.nav.getCurrentService()
-               if ev == iPlayableService.evStart:
+               if ev == iPlayableService.evUpdatedInfo or ev == iPlayableService.evEnd:
                        self.checkTunerState(service)
 
 class InfoBarNotifications:
@@ -1535,12 +1606,26 @@ class InfoBarNotifications:
        def checkNotifications(self):
                if len(Notifications.notifications):
                        n = Notifications.notifications[0]
+                       
                        Notifications.notifications = Notifications.notifications[1:]
                        cb = n[0]
+
+                       if n[3].has_key("onSessionOpenCallback"):
+                               n[3]["onSessionOpenCallback"]()
+                               del n[3]["onSessionOpenCallback"]
+
                        if cb is not None:
-                               self.session.openWithCallback(cb, n[1], *n[2], **n[3])
+                               dlg = self.session.openWithCallback(cb, n[1], *n[2], **n[3])
                        else:
-                               self.session.open(n[1], *n[2], **n[3])
+                               dlg = self.session.open(n[1], *n[2], **n[3])
+                       
+                       # remember that this notification is currently active
+                       d = (n[4], dlg)
+                       Notifications.current_notifications.append(d)
+                       dlg.onClose.append(boundFunction(self.__notificationClosed, d))
+
+       def __notificationClosed(self, d):
+               Notifications.current_notifications.remove(d)
 
 class InfoBarServiceNotifications:
        def __init__(self):
@@ -1671,7 +1756,7 @@ class InfoBarCueSheetSupport:
                        return None
 
        def addMark(self, point):
-               bisect.insort(self.cut_list, point)
+               insort(self.cut_list, point)
                self.uploadCuesheet()
 
        def removeMark(self, point):
@@ -1749,14 +1834,15 @@ class InfoBarSubtitleSupport(object):
 
                self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
                        {
-                               iPlayableService.evStart: self.__serviceStarted,
+                               iPlayableService.evEnd: self.__serviceStopped,
                                iPlayableService.evUpdatedInfo: self.__updatedInfo
                        })
-
-       def __serviceStarted(self):
                self.cached_subtitle_checked = False
+
+       def __serviceStopped(self):
                self.subtitle_window.hide()
                self.__subtitles_enabled = False
+               self.cached_subtitle_checked = False
 
        def __updatedInfo(self):
                if not self.cached_subtitle_checked:
@@ -1791,3 +1877,45 @@ class InfoBarSubtitleSupport(object):
 
        subtitles_enabled = property(lambda self: self.__subtitles_enabled, setSubtitlesEnable)
        selected_subtitle = property(lambda self: self.__selected_subtitle, setSelectedSubtitle)
+
+class InfoBarServiceErrorPopupSupport:
+       def __init__(self):
+               self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
+                       {
+                               iPlayableService.evTuneFailed: self.__tuneFailed,
+                               iPlayableService.evStart: self.__serviceStarted
+                       })
+               self.__serviceStarted()
+
+       def __serviceStarted(self):
+               self.last_error = None
+               Notifications.RemovePopup(id = "ZapError")
+
+       def __tuneFailed(self):
+               service = self.session.nav.getCurrentService()
+               info = service and service.info()
+               error = info and info.getInfo(iServiceInformation.sDVBState)
+               
+               if error == self.last_error:
+                       error = None
+               else:
+                       self.last_error = error
+
+               errors = {
+                       eDVBServicePMTHandler.eventNoResources: _("No free tuner!"),
+                       eDVBServicePMTHandler.eventTuneFailed: _("Tune failed!"),
+                       eDVBServicePMTHandler.eventNoPAT: _("No data on transponder!\n(Timeout reading PAT)"),
+                       eDVBServicePMTHandler.eventNoPATEntry: _("Service not found!\n(SID not found in PAT)"),
+                       eDVBServicePMTHandler.eventNoPMT: _("Service invalid!\n(Timeout reading PMT)"),
+                       eDVBServicePMTHandler.eventNewProgramInfo: None,
+                       eDVBServicePMTHandler.eventTuned: None,
+                       eDVBServicePMTHandler.eventSOF: None,
+                       eDVBServicePMTHandler.eventEOF: None
+               }
+
+               error = errors.get(error) #this returns None when the key not exist in the dict
+
+               if error is not None:
+                       Notifications.AddPopup(text = error, type = MessageBox.TYPE_ERROR, timeout = 5, id = "ZapError")
+               else:
+                       Notifications.RemovePopup(id = "ZapError")