fix frontend number query
[enigma2.git] / lib / python / Components / Converter / ServicePosition.py
1 from Converter import Converter
2 from Poll import Poll
3 from enigma import iPlayableService
4 from Components.Element import cached
5
6 class ServicePosition(Converter, Poll, object):
7         TYPE_LENGTH = 0
8         TYPE_POSITION = 1
9         TYPE_REMAINING = 2
10         TYPE_GAUGE = 3
11         TYPE_POSITION_DETAILED = 4
12
13         def __init__(self, type):
14                 Poll.__init__(self)
15                 Converter.__init__(self, type)
16
17                 self.poll_interval = 500
18
19                 if type == "Length":
20                         self.type = self.TYPE_LENGTH
21                 elif type == "Position":
22                         self.type = self.TYPE_POSITION
23                 elif type == "PositionDetailed":
24                         self.type = self.TYPE_POSITION_DETAILED
25                         self.poll_interval = 100
26                 elif type == "Remaining":
27                         self.type = self.TYPE_REMAINING
28                 elif type == "Gauge":
29                         self.type = self.TYPE_GAUGE
30                 else:
31                         raise "type must be {Length|Position|PositionDetaileed|Remaining|Gauge}"
32
33                 self.poll_enabled = self.type != self.TYPE_LENGTH
34
35         def getSeek(self):
36                 s = self.source.service
37                 return s and s.seek()
38
39         @cached
40         def getPosition(self):
41                 seek = self.getSeek()
42                 if seek is None:
43                         return None
44                 pos = seek.getPlayPosition()
45                 if pos[0]:
46                         return 0
47                 return pos[1]
48
49         @cached
50         def getLength(self):
51                 seek = self.getSeek()
52                 if seek is None:
53                         return None
54                 length = seek.getLength()
55                 if length[0]:
56                         return 0
57                 return length[1]
58
59         @cached
60         def getCutlist(self):
61                 service = self.source.service
62                 cue = service and service.cueSheet()
63                 return cue and cue.getCutList()
64
65         @cached
66         def getText(self):
67                 seek = self.getSeek()
68                 if seek is None:
69                         return ""
70                 else:
71                         if self.type == self.TYPE_LENGTH:
72                                 l = self.length
73                         elif self.type in [self.TYPE_POSITION, self.TYPE_POSITION_DETAILED]:
74                                 l = self.position
75                         elif self.type == self.TYPE_REMAINING:
76                                 l = self.length - self.position
77
78                         if self.type != self.TYPE_POSITION_DETAILED:
79                                 l /= 90000
80
81                         if l > 0:
82                                 sign = ""
83                         else:
84                                 l = -l
85                                 sign = "-"
86
87                         if self.type != self.TYPE_POSITION_DETAILED:
88                                 return sign + "%d:%02d" % (l/60, l%60)
89                         else:
90                                 return sign + "%d:%02d:%03d" % ((l/60/90000), (l/90000)%60, (l%90000)/90)
91
92         # range/value are for the Progress renderer
93         range = 10000
94
95         @cached
96         def getValue(self):
97                 pos = self.position
98                 len = self.length
99                 if pos is None or len is None or len <= 0:
100                         return None
101                 return pos * 10000 / len
102
103         position = property(getPosition)
104         length = property(getLength)
105         cutlist = property(getCutlist)
106         text = property(getText)
107         value = property(getValue)
108
109         def changed(self, what):
110                 cutlist_refresh = what[0] != self.CHANGED_SPECIFIC or what[1] in [iPlayableService.evCuesheetChanged]
111                 time_refresh = what[0] == self.CHANGED_POLL or what[0] == self.CHANGED_SPECIFIC and what[1] in [iPlayableService.evCuesheetChanged]
112
113                 if cutlist_refresh:
114                         if self.type == self.TYPE_GAUGE:
115                                 self.downstream_elements.cutlist_changed()
116
117                 if time_refresh:
118                         self.downstream_elements.changed(what)