fix caching
[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         
12         def __init__(self, type):
13                 Poll.__init__(self)
14                 Converter.__init__(self, type)
15                 if type == "Length":
16                         self.type = self.TYPE_LENGTH
17                 elif type == "Position":
18                         self.type = self.TYPE_POSITION
19                 elif type == "Remaining":
20                         self.type = self.TYPE_REMAINING
21                 elif type == "Gauge":
22                         self.type = self.TYPE_GAUGE
23
24                 self.poll_interval = 500
25                 self.poll_enabled = self.type != self.TYPE_LENGTH
26         
27         def getSeek(self):
28                 s = self.source.service
29                 return s and s.seek()
30         
31         @cached
32         def getPosition(self):
33                 seek = self.getSeek()
34                 if seek is None:
35                         return None
36                 pos = seek.getPlayPosition()
37                 if pos[0]:
38                         return 0
39                 return pos[1]
40         
41         @cached
42         def getLength(self):
43                 seek = self.getSeek()
44                 if seek is None:
45                         return None
46                 length = seek.getLength()
47                 if length[0]:
48                         return 0
49                 return length[1]
50         
51         @cached
52         def getCutlist(self):
53                 service = self.source.service
54                 cue = service and service.cueSheet()
55                 return cue and cue.getCutList()
56         
57         @cached
58         def getText(self):
59                 seek = self.getSeek()
60                 if seek is None:
61                         return ""
62                 else:
63                         if self.type == self.TYPE_LENGTH:
64                                 l = self.length
65                         elif self.type == self.TYPE_POSITION:
66                                 l = self.position
67                         elif self.type == self.TYPE_REMAINING:
68                                 l = self.length - self.position
69                         
70                         l /= 90000
71                         return "%d:%02d" % (l/60, l%60)
72
73         position = property(getPosition)
74         length = property(getLength)
75         cutlist = property(getCutlist)
76         text = property(getText)
77         
78         def changed(self, what):
79                 cutlist_refresh = what[0] != self.CHANGED_SPECIFIC or what[1] in [iPlayableService.evCuesheetChanged]
80                 time_refresh = what[0] == self.CHANGED_POLL or what[0] == self.CHANGED_SPECIFIC and what[1] in [iPlayableService.evCuesheetChanged]
81                 
82                 if cutlist_refresh:
83                         if self.type == self.TYPE_GAUGE:
84                                 self.downstream_elements.cutlist_changed()
85
86                 if time_refresh:
87                         self.downstream_elements.changed(what)