35280cf981492b2bf08bac4439b6e2243aa012d9
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / ServicePosition.py
1 from PerServiceDisplay import *
2 from enigma import eTimer
3
4 from enigma import iPlayableService, iSeekableServicePtr, ePositionGauge
5
6 class ServicePosition(PerServiceDisplay):
7         TYPE_LENGTH = 0,
8         TYPE_POSITION = 1,
9         TYPE_REMAINING = 2
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.setType(type)
21
22         def newService(self):
23                 self.setType(self.type)
24         
25         def setType(self, type):
26                 self.type = type
27                 
28                 self.updateTimer.start(500)
29                 self.update()
30         
31         def get(self, what):
32                 service = self.navcore.getCurrentService()
33                 
34                 if service != None:
35                         seek = service.seek()
36                         if seek != None:
37                                 if what == self.TYPE_LENGTH:
38                                         r = seek.getLength()
39                                 elif what == self.TYPE_POSITION:
40                                         r = seek.getPlayPosition()
41                                 if not r[0]:
42                                         return r[1] / 90000
43                 
44                 return -1
45         
46         def update(self):
47                 seek = None
48                 service = self.navcore.getCurrentService()
49                 if service != None:
50                         seek = service.seek()
51
52                 if seek is not None:
53                         if self.type == self.TYPE_LENGTH:
54                                 l = self.get(self.TYPE_LENGTH)
55                         elif self.type == self.TYPE_POSITION:
56                                 l = self.get(self.TYPE_POSITION)
57                         elif self.type == self.TYPE_REMAINING:
58                                 l = self.get(self.TYPE_LENGTH) - self.get(self.TYPE_POSITION)
59                         
60                         self.setText("%d:%02d" % (l/60, l%60))
61                         self.updateTimer.start(500)
62                 else:
63                         self.updateTimer.start(10000)
64                         self.setText("-:--")
65         
66         def stopEvent(self):
67                 self.updateTimer.stop()
68                 self.setText("");
69
70 class ServicePositionGauge(PerServiceBase):
71         def __init__(self, navcore):
72                 PerServiceBase.__init__(self, navcore,
73                         {
74                                 iPlayableService.evStart: self.newService,
75                                 iPlayableService.evEnd: self.stopEvent
76                         })
77
78         def newService(self):
79                 if self.get() is None:  
80                         self.disablePolling()
81                 else:
82                         self.enablePolling(interval=500)
83         
84         def get(self):
85                 service = self.navcore.getCurrentService()
86                 if service is None:
87                         return None
88                 seek = service.seek()
89                 if seek is None:
90                         return None
91
92                 len = seek.getLength()
93                 pos = seek.getPlayPosition()
94                 
95                 if len[0] or pos[0]:
96                         return (0, 0)
97                 return (len[1], pos[1])
98         
99         def poll(self):
100                 data = self.get()
101                 if data is None:
102                         return
103                 self.instance.setLength(data[0])
104                 self.instance.setPosition(data[1])
105                 
106         def stopEvent(self):
107                 self.disablePolling()
108
109         def GUIcreate(self, parent):
110                 self.instance = ePositionGauge(parent)
111         
112         def GUIdelete(self):
113                 self.instance = None