switch to opkg
[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, ElementError
5
6 class ServicePosition(Poll, Converter, object):
7         TYPE_LENGTH = 0
8         TYPE_POSITION = 1
9         TYPE_REMAINING = 2
10         TYPE_GAUGE = 3
11
12         def __init__(self, type):
13                 Poll.__init__(self)
14                 Converter.__init__(self, type)
15
16                 args = type.split(',')
17                 type = args.pop(0)
18
19                 self.negate = 'Negate' in args
20                 self.detailed = 'Detailed' in args
21                 self.showHours = 'ShowHours' in args
22                 self.showNoSeconds = 'ShowNoSeconds' in args
23
24                 if type == "Length":
25                         self.type = self.TYPE_LENGTH
26                 elif type == "Position":
27                         self.type = self.TYPE_POSITION
28                 elif type == "Remaining":
29                         self.type = self.TYPE_REMAINING
30                 elif type == "Gauge":
31                         self.type = self.TYPE_GAUGE
32                 else:
33                         raise ElementError("type must be {Length|Position|Remaining|Gauge} with optional arguments {Negate|Detailed|ShowHours|ShowNoSeconds} for ServicePosition converter")
34
35                 if self.detailed:
36                         self.poll_interval = 100
37                 elif self.type == self.TYPE_LENGTH:
38                         self.poll_interval = 2000
39                 else:
40                         self.poll_interval = 500
41
42                 self.poll_enabled = True
43
44         def getSeek(self):
45                 s = self.source.service
46                 return s and s.seek()
47
48         @cached
49         def getPosition(self):
50                 seek = self.getSeek()
51                 if seek is None:
52                         return None
53                 pos = seek.getPlayPosition()
54                 if pos[0]:
55                         return 0
56                 return pos[1]
57
58         @cached
59         def getLength(self):
60                 seek = self.getSeek()
61                 if seek is None:
62                         return None
63                 length = seek.getLength()
64                 if length[0]:
65                         return 0
66                 return length[1]
67
68         @cached
69         def getCutlist(self):
70                 service = self.source.service
71                 cue = service and service.cueSheet()
72                 return cue and cue.getCutList()
73
74         @cached
75         def getText(self):
76                 seek = self.getSeek()
77                 if seek is None:
78                         return ""
79                 else:
80                         if self.type == self.TYPE_LENGTH:
81                                 l = self.length
82                         elif self.type == self.TYPE_POSITION:
83                                 l = self.position
84                         elif self.type == self.TYPE_REMAINING:
85                                 l = self.length - self.position
86
87                         if not self.detailed:
88                                 l /= 90000
89
90                         if self.negate: l = -l
91
92                         if l > 0:
93                                 sign = ""
94                         else:
95                                 l = -l
96                                 sign = "-"
97
98                         if not self.detailed:
99                                 if self.showHours:
100                                         if self.showNoSeconds:
101                                                 return sign + "%d:%02d" % (l/3600, l%3600/60)
102                                         else:
103                                                 return sign + "%d:%02d:%02d" % (l/3600, l%3600/60, l%60)
104                                 else:
105                                         if self.showNoSeconds:
106                                                 return sign + "%d" % (l/60)
107                                         else:
108                                                 return sign + "%d:%02d" % (l/60, l%60)
109                         else:
110                                 if self.showHours:
111                                         return sign + "%d:%02d:%02d:%03d" % ((l/3600/90000), (l/90000)%3600/60, (l/90000)%60, (l%90000)/90)
112                                 else:
113                                         return sign + "%d:%02d:%03d" % ((l/60/90000), (l/90000)%60, (l%90000)/90)
114
115         # range/value are for the Progress renderer
116         range = 10000
117
118         @cached
119         def getValue(self):
120                 pos = self.position
121                 len = self.length
122                 if pos is None or len is None or len <= 0:
123                         return None
124                 return pos * 10000 / len
125
126         position = property(getPosition)
127         length = property(getLength)
128         cutlist = property(getCutlist)
129         text = property(getText)
130         value = property(getValue)
131
132         def changed(self, what):
133                 cutlist_refresh = what[0] != self.CHANGED_SPECIFIC or what[1] in (iPlayableService.evCuesheetChanged,)
134                 time_refresh = what[0] == self.CHANGED_POLL or what[0] == self.CHANGED_SPECIFIC and what[1] in (iPlayableService.evCuesheetChanged,)
135
136                 if cutlist_refresh:
137                         if self.type == self.TYPE_GAUGE:
138                                 self.downstream_elements.cutlist_changed()
139
140                 if time_refresh:
141                         self.downstream_elements.changed(what)