Merge branch 'obi/master'
[enigma2.git] / lib / python / Components / PerServiceDisplay.py
1 from GUIComponent import GUIComponent
2 from VariableText import VariableText
3 from VariableValue import VariableValue
4
5 from enigma import iPlayableService
6 from enigma import eLabel, eSlider, eTimer
7
8
9 class PerServiceBase(object):
10         EventMap = { }
11
12         @staticmethod
13         def event(ev):
14                 func_list = PerServiceBase.EventMap.setdefault(ev, [])
15                 for func in func_list:
16                         if func[0]: # with_event
17                                 func[1](ev)
18                         else:
19                                 func[1]()
20
21         def __init__(self, navcore, eventmap, with_event=False):
22                 self.navcore = navcore
23                 self.eventmap = eventmap
24                 self.poll_timer = eTimer()
25                 self.with_event = with_event
26
27                 self.poll_timer.callback.append(self.poll)
28
29                 EventMap = PerServiceBase.EventMap
30                 if not len(EventMap):
31                         self.navcore.event.append(PerServiceBase.event)
32
33                 EventMap = EventMap.setdefault
34                 for x in eventmap.iteritems():
35                         EventMap(x[0], []).append((with_event, x[1]))
36
37                 # start with stopped state, so simulate that
38                 evEndEntry = eventmap.get(iPlayableService.evEnd)
39                 if evEndEntry:
40                         if with_event:
41                                 evEndEntry(iPlayableService.evEnd)
42                         else:
43                                 evEndEntry()
44
45         def destroy(self):
46                 EventMap = PerServiceBase.EventMap.setdefault
47                 for x in self.eventmap.iteritems():
48                         EventMap(x[0], []).remove((self.with_event, x[1]))
49
50         def enablePolling(self, interval=60000):
51                 if interval:
52                         self.poll_timer.start(interval)
53                 else:
54                         self.poll_timer.stop()
55         
56         def disablePolling(self):
57                 self.enablePolling(interval=0)
58
59         def poll(self):
60                 pass
61
62 class PerServiceDisplay(PerServiceBase, VariableText, GUIComponent):
63         """Mixin for building components which display something which changes on navigation events, for example "service name" """
64         def __init__(self, navcore, eventmap):
65                 GUIComponent.__init__(self)
66                 VariableText.__init__(self)
67                 PerServiceBase.__init__(self, navcore, eventmap)
68
69         def destroy(self):
70                 PerServiceBase.destroy(self)
71                 GUIComponent.destroy(self)
72
73         GUI_WIDGET = eLabel
74
75 class PerServiceDisplayProgress(PerServiceBase, VariableValue, GUIComponent):
76         def __init__(self, navcore, eventmap):
77                 GUIComponent.__init__(self)
78                 VariableValue.__init__(self)
79                 PerServiceBase.__init__(self, navcore, eventmap)
80                 self.eventmap = eventmap
81                 self.navcore = navcore
82                 self.navcore.event.append(self.event)
83
84                 # start with stopped state, so simulate that
85                 self.event(iPlayableService.evEnd)
86
87         GUI_WIDGET = eSlider
88
89         def destroy(self):
90                 PerServiceBase.destroy(self)
91                 GUIComponent.destroy(self)