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