ba87e3ef680df8438255f6a9a38352115d41a18b
[enigma2.git] / lib / python / Components / ServicePosition.py
1 from PerServiceDisplay import PerServiceDisplay, PerServiceBase
2 from enigma import eTimer, iPlayableService, iSeekableServicePtr, ePositionGauge
3 import time
4
5 class ServicePosition(PerServiceDisplay):
6         TYPE_LENGTH = 0,
7         TYPE_POSITION = 1,
8         TYPE_REMAINING = 2,
9         TYPE_RELATIVE = 3
10         
11         def __init__(self, navcore, type):
12                 self.updateTimer = eTimer()
13                 self.updateTimer.timeout.get().append(self.update)
14                 PerServiceDisplay.__init__(self, navcore,
15                         {
16                                 iPlayableService.evStart: self.newService,
17                                 iPlayableService.evEnd: self.stopEvent
18                         })
19                 self.type = type
20                 self.relative_base = 0
21 #               self.setType(type)
22
23         def newService(self):
24                 self.setType(self.type)
25         
26         def setType(self, type):
27                 self.type = type
28                 
29                 self.updateTimer.start(500)
30                 self.update()
31         
32         def setRelative(self, rel):
33                 self.relative_base = rel
34         
35         def get(self, what):
36                 service = self.navcore.getCurrentService()
37                 seek = service and service.seek()
38                 if seek != None:
39                         if what == self.TYPE_LENGTH:
40                                 r = seek.getLength()
41                         elif what == self.TYPE_POSITION:
42                                 r = seek.getPlayPosition()
43                         if not r[0]:
44                                 return r[1] / 90000
45                 
46                 return -1
47         
48         def update(self):
49                 seek = None
50                 service = self.navcore.getCurrentService()
51                 if service != None:
52                         seek = service.seek()
53
54                 if seek is not None:
55                         if self.type != self.TYPE_RELATIVE:
56                                 if self.type == self.TYPE_LENGTH:
57                                         l = self.get(self.TYPE_LENGTH)
58                                 elif self.type == self.TYPE_POSITION:
59                                         l = self.get(self.TYPE_POSITION)
60                                 elif self.type == self.TYPE_REMAINING:
61                                         l = self.get(self.TYPE_LENGTH) - self.get(self.TYPE_POSITION)
62                         
63                                 self.setText("%d:%02d" % (l/60, l%60))
64                         else:
65                                 l = self.get(self.TYPE_POSITION)
66                                 if l != -1:
67                                         l += self.relative_base
68                                         try:
69                                                 t = time.localtime(l)
70                                                 timestr = "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
71                                         except ValueError:
72                                                 timestr = ""
73                                 else:
74                                         timestr = ""
75
76                                 self.setText(timestr)
77                                 
78                         self.updateTimer.start(500)
79                 else:
80                         self.updateTimer.start(10000)
81                         self.setText("-:--")
82         
83         def stopEvent(self):
84                 self.updateTimer.stop()
85                 self.setText("");
86
87 class ServicePositionGauge(PerServiceBase):
88         def __init__(self, navcore):
89                 PerServiceBase.__init__(self, navcore,
90                         {
91                                 iPlayableService.evStart: self.newService,
92                                 iPlayableService.evEnd: self.stopEvent,
93                                 iPlayableService.evCuesheetChanged: self.newCuesheet
94                         })
95                 self.instance = None
96
97         def newService(self):
98                 if self.get() is None:  
99                         self.disablePolling()
100                 else:
101                         self.enablePolling(interval=500)
102                         self.newCuesheet()
103         
104         def get(self):
105                 service = self.navcore.getCurrentService()
106                 seek = service and service.seek()
107                 if seek is None:
108                         return None
109
110                 len = seek.getLength()
111                 pos = seek.getPlayPosition()
112                 
113                 if len[0] or pos[0]:
114                         return (0, 0)
115                 return (len[1], pos[1])
116         
117         def poll(self):
118                 data = self.get()
119                 if data is None:
120                         return
121
122                 if self.instance is not None:
123                         self.instance.setLength(data[0])
124                         self.instance.setPosition(data[1])
125                 
126         def stopEvent(self):
127                 self.disablePolling()
128
129         GUI_WIDGET = ePositionGauge
130         
131         def postWidgetCreate(self, instance):
132                 self.newService()
133         
134         def newCuesheet(self):
135                 service = self.navcore.getCurrentService()
136                 cue = service and service.cueSheet()
137                 cutlist = (cue and cue.getCutList()) or [ ]
138                 if self.instance is not None:
139                         self.instance.setInOutList(cutlist)