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