don't crash if time is *really* invalid
[enigma2.git] / lib / python / Components / ServicePosition.py
index 80e5e3d..ba87e3e 100644 (file)
@@ -1,13 +1,12 @@
-from PerServiceDisplay import *
-from enigma import eTimer
-
-
-from enigma import iPlayableService, iSeekableServicePtr
+from PerServiceDisplay import PerServiceDisplay, PerServiceBase
+from enigma import eTimer, iPlayableService, iSeekableServicePtr, ePositionGauge
+import time
 
 class ServicePosition(PerServiceDisplay):
        TYPE_LENGTH = 0,
        TYPE_POSITION = 1,
 
 class ServicePosition(PerServiceDisplay):
        TYPE_LENGTH = 0,
        TYPE_POSITION = 1,
-       TYPE_REMAINING = 2
+       TYPE_REMAINING = 2,
+       TYPE_RELATIVE = 3
        
        def __init__(self, navcore, type):
                self.updateTimer = eTimer()
        
        def __init__(self, navcore, type):
                self.updateTimer = eTimer()
@@ -18,6 +17,7 @@ class ServicePosition(PerServiceDisplay):
                                iPlayableService.evEnd: self.stopEvent
                        })
                self.type = type
                                iPlayableService.evEnd: self.stopEvent
                        })
                self.type = type
+               self.relative_base = 0
 #              self.setType(type)
 
        def newService(self):
 #              self.setType(type)
 
        def newService(self):
@@ -29,18 +29,19 @@ class ServicePosition(PerServiceDisplay):
                self.updateTimer.start(500)
                self.update()
        
                self.updateTimer.start(500)
                self.update()
        
+       def setRelative(self, rel):
+               self.relative_base = rel
+       
        def get(self, what):
                service = self.navcore.getCurrentService()
        def get(self, what):
                service = self.navcore.getCurrentService()
-               
-               if service != None:
-                       seek = service.seek()
-                       if seek != None:
-                               if what == self.TYPE_LENGTH:
-                                       r = seek.getLength()
-                               elif what == self.TYPE_POSITION:
-                                       r = seek.getPlayPosition()
-                               if not r[0]:
-                                       return r[1] / 90000
+               seek = service and service.seek()
+               if seek != None:
+                       if what == self.TYPE_LENGTH:
+                               r = seek.getLength()
+                       elif what == self.TYPE_POSITION:
+                               r = seek.getPlayPosition()
+                       if not r[0]:
+                               return r[1] / 90000
                
                return -1
        
                
                return -1
        
@@ -51,14 +52,29 @@ class ServicePosition(PerServiceDisplay):
                        seek = service.seek()
 
                if seek is not None:
                        seek = service.seek()
 
                if seek is not None:
-                       if self.type == self.TYPE_LENGTH:
-                               l = self.get(self.TYPE_LENGTH)
-                       elif self.type == self.TYPE_POSITION:
-                               l = self.get(self.TYPE_POSITION)
-                       elif self.type == self.TYPE_REMAINING:
-                               l = self.get(self.TYPE_LENGTH) - self.get(self.TYPE_POSITION)
+                       if self.type != self.TYPE_RELATIVE:
+                               if self.type == self.TYPE_LENGTH:
+                                       l = self.get(self.TYPE_LENGTH)
+                               elif self.type == self.TYPE_POSITION:
+                                       l = self.get(self.TYPE_POSITION)
+                               elif self.type == self.TYPE_REMAINING:
+                                       l = self.get(self.TYPE_LENGTH) - self.get(self.TYPE_POSITION)
                        
                        
-                       self.setText("%d:%02d" % (l/60, l%60))
+                               self.setText("%d:%02d" % (l/60, l%60))
+                       else:
+                               l = self.get(self.TYPE_POSITION)
+                               if l != -1:
+                                       l += self.relative_base
+                                       try:
+                                               t = time.localtime(l)
+                                               timestr = "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
+                                       except ValueError:
+                                               timestr = ""
+                               else:
+                                       timestr = ""
+
+                               self.setText(timestr)
+                               
                        self.updateTimer.start(500)
                else:
                        self.updateTimer.start(10000)
                        self.updateTimer.start(500)
                else:
                        self.updateTimer.start(10000)
@@ -67,3 +83,57 @@ class ServicePosition(PerServiceDisplay):
        def stopEvent(self):
                self.updateTimer.stop()
                self.setText("");
        def stopEvent(self):
                self.updateTimer.stop()
                self.setText("");
+
+class ServicePositionGauge(PerServiceBase):
+       def __init__(self, navcore):
+               PerServiceBase.__init__(self, navcore,
+                       {
+                               iPlayableService.evStart: self.newService,
+                               iPlayableService.evEnd: self.stopEvent,
+                               iPlayableService.evCuesheetChanged: self.newCuesheet
+                       })
+               self.instance = None
+
+       def newService(self):
+               if self.get() is None:  
+                       self.disablePolling()
+               else:
+                       self.enablePolling(interval=500)
+                       self.newCuesheet()
+       
+       def get(self):
+               service = self.navcore.getCurrentService()
+               seek = service and service.seek()
+               if seek is None:
+                       return None
+
+               len = seek.getLength()
+               pos = seek.getPlayPosition()
+               
+               if len[0] or pos[0]:
+                       return (0, 0)
+               return (len[1], pos[1])
+       
+       def poll(self):
+               data = self.get()
+               if data is None:
+                       return
+
+               if self.instance is not None:
+                       self.instance.setLength(data[0])
+                       self.instance.setPosition(data[1])
+               
+       def stopEvent(self):
+               self.disablePolling()
+
+       GUI_WIDGET = ePositionGauge
+       
+       def postWidgetCreate(self, instance):
+               self.newService()
+       
+       def newCuesheet(self):
+               service = self.navcore.getCurrentService()
+               cue = service and service.cueSheet()
+               cutlist = (cue and cue.getCutList()) or [ ]
+               if self.instance is not None:
+                       self.instance.setInOutList(cutlist)