Merge branch 'obi/master'
[enigma2.git] / lib / python / Components / ServiceEventTracker.py
1 class InfoBarBase:
2         def __init__(self, steal_current_service = False):
3                 if steal_current_service:
4                         ServiceEventTracker.setActiveInfoBar(self, None, None)
5                 else:
6                         nav = self.session.nav
7                         ServiceEventTracker.setActiveInfoBar(self, not steal_current_service and nav.getCurrentService(), nav.getCurrentlyPlayingServiceReference())
8                 self.onClose.append(self.__close)
9
10         def __close(self):
11                 ServiceEventTracker.popActiveInfoBar()
12
13 class ServiceEventTracker:
14         """Tracks service events into a screen"""
15         InfoBarStack = [ ]
16         InfoBarStackSize = 0
17         oldServiceStr = None
18         EventMap = { }
19         navcore = None
20
21         @staticmethod
22         def event(evt):
23                 set = ServiceEventTracker
24                 func_list = set.EventMap.setdefault(evt, [])
25                 if func_list:
26                         nav = set.navcore
27                         cur_ref = nav.getCurrentlyPlayingServiceReference()
28                         old_service_running = set.oldRef and cur_ref and cur_ref == set.oldRef and set.oldServiceStr == nav.getCurrentService().getPtrString()
29                         if not old_service_running and set.oldServiceStr:
30                                 set.oldServiceStr = None
31                                 set.oldRef = None
32                         ssize = set.InfoBarStackSize
33                         stack = set.InfoBarStack
34                         for func in func_list:
35                                 if (func[0] or  # let pass all events to screens not derived from InfoBarBase
36                                         (not old_service_running and stack[ssize-1] == func[1]) or # let pass events from currently running service just to current active screen (derived from InfoBarBase)
37                                         (old_service_running and ssize > 1 and stack[ssize-2] == func[1])): # let pass events from old running service just to previous active screen (derived from InfoBarBase)
38                                         func[2]()
39
40         @staticmethod
41         def setActiveInfoBar(infobar, old_service, old_ref):
42                 set = ServiceEventTracker
43                 set.oldRef = old_ref
44                 set.oldServiceStr = old_service and old_service.getPtrString()
45                 assert infobar not in set.InfoBarStack, "FATAL: Infobar '" + str(infobar) + "' is already active!"
46                 set.InfoBarStack.append(infobar)
47                 set.InfoBarStackSize += 1
48 #               print "ServiceEventTracker set active '" + str(infobar) + "'"
49
50         @staticmethod
51         def popActiveInfoBar():
52                 set = ServiceEventTracker
53                 stack = set.InfoBarStack
54                 if set.InfoBarStackSize:
55                         nav = set.navcore
56                         set.InfoBarStackSize -= 1
57                         del stack[set.InfoBarStackSize]
58                         old_service = nav.getCurrentService()
59                         set.oldServiceStr = old_service and old_service.getPtrString()
60                         set.oldRef = nav.getCurrentlyPlayingServiceReference()
61 #                       if set.InfoBarStackSize:
62 #                               print "ServiceEventTracker reset active '" + str(stack[set.InfoBarStackSize-1]) + "'"
63
64         def __init__(self, screen, eventmap):
65                 self.__screen = screen
66                 self.__eventmap = eventmap
67                 self.__passall = not isinstance(screen, InfoBarBase) # let pass all events to screens not derived from InfoBarBase
68                 EventMap = ServiceEventTracker.EventMap
69                 if not len(EventMap):
70                         screen.session.nav.event.append(ServiceEventTracker.event)
71                         ServiceEventTracker.navcore = screen.session.nav
72                 EventMap = EventMap.setdefault
73                 for x in eventmap.iteritems():
74                         EventMap(x[0], []).append((self.__passall, screen, x[1]))
75                 screen.onClose.append(self.__del_event)
76
77         def __del_event(self):
78                 EventMap = ServiceEventTracker.EventMap.setdefault
79                 for x in self.__eventmap.iteritems():
80                         EventMap(x[0], []).remove((self.__passall, self.__screen, x[1]))