fix progressbar, slider
[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                                         t = time.localtime(l)
69                                         timestr = "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
70                                 else:
71                                         timestr = ""
72
73                                 self.setText(timestr)
74                                 
75                         self.updateTimer.start(500)
76                 else:
77                         self.updateTimer.start(10000)
78                         self.setText("-:--")
79         
80         def stopEvent(self):
81                 self.updateTimer.stop()
82                 self.setText("");
83
84 class ServicePositionGauge(PerServiceBase):
85         def __init__(self, navcore):
86                 PerServiceBase.__init__(self, navcore,
87                         {
88                                 iPlayableService.evStart: self.newService,
89                                 iPlayableService.evEnd: self.stopEvent,
90                                 iPlayableService.evCuesheetChanged: self.newCuesheet
91                         })
92                 self.instance = None
93
94         def newService(self):
95                 if self.get() is None:  
96                         self.disablePolling()
97                 else:
98                         self.enablePolling(interval=500)
99                         self.newCuesheet()
100         
101         def get(self):
102                 service = self.navcore.getCurrentService()
103                 seek = service and service.seek()
104                 if seek is None:
105                         return None
106
107                 len = seek.getLength()
108                 pos = seek.getPlayPosition()
109                 
110                 if len[0] or pos[0]:
111                         return (0, 0)
112                 return (len[1], pos[1])
113         
114         def poll(self):
115                 data = self.get()
116                 if data is None:
117                         return
118
119                 if self.instance is not None:
120                         self.instance.setLength(data[0])
121                         self.instance.setPosition(data[1])
122                 
123         def stopEvent(self):
124                 self.disablePolling()
125
126         GUI_WIDGET = ePositionGauge
127         
128         def postWidgetCreate(self, instance):
129                 self.newService()
130         
131         def newCuesheet(self):
132                 service = self.navcore.getCurrentService()
133                 cue = service and service.cueSheet()
134                 cutlist = (cue and cue.getCutList()) or [ ]
135                 if self.instance is not None:
136                         self.instance.setInOutList(cutlist)