Merge branch 'master' into dvd_scale_test
[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         oldService = 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.oldService and set.oldService == str(nav.getCurrentService())
29 #                       print "old_service_running", old_service_running
30                         if not old_service_running and set.oldService:
31                                 set.oldService = None
32                                 set.oldRef = None
33                         ssize = set.InfoBarStackSize
34                         stack = set.InfoBarStack
35                         for func in func_list:
36                                 if (func[0] or  # let pass all events to screens not derived from InfoBarBase
37                                         (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)
38                                         (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)
39                                         func[2]()
40 #                               else:
41 #                                       print "ignore event", ev, "for inactive infobar '" + str(self.screen) + "'"
42
43         @staticmethod
44         def setActiveInfoBar(infobar, old_service, old_ref):
45                 set = ServiceEventTracker
46                 set.oldRef = old_ref
47                 set.oldService = old_service and str(old_service)
48                 assert infobar not in set.InfoBarStack, "FATAL: Infobar '" + str(infobar) + "' is already active!"
49                 set.InfoBarStack.append(infobar)
50                 set.InfoBarStackSize += 1
51 #               print "ServiceEventTracker set active '" + str(infobar) + "'"
52
53         @staticmethod
54         def popActiveInfoBar():
55                 set = ServiceEventTracker
56                 stack = set.InfoBarStack
57                 if set.InfoBarStackSize:
58                         set.InfoBarStackSize -= 1
59                         del stack[set.InfoBarStackSize]
60 #                       if set.InfoBarStackSize:
61 #                               print "ServiceEventTracker reset active '" + str(stack[set.InfoBarStackSize-1]) + "'"
62
63         def __init__(self, screen, eventmap):
64                 self.__screen = screen
65                 self.__eventmap = eventmap
66                 self.__passall = not isinstance(screen, InfoBarBase) # let pass all events to screens not derived from InfoBarBase
67                 EventMap = ServiceEventTracker.EventMap
68                 if not len(EventMap):
69                         screen.session.nav.event.append(ServiceEventTracker.event)
70                         ServiceEventTracker.navcore = screen.session.nav
71                 EventMap = EventMap.setdefault
72                 for x in eventmap.iteritems():
73                         EventMap(x[0], []).append((self.__passall, screen, x[1]))
74                 screen.onClose.append(self.__del_event)
75
76         def __del_event(self):
77                 EventMap = ServiceEventTracker.EventMap.setdefault
78                 for x in self.__eventmap.iteritems():
79                         EventMap(x[0], []).remove((self.__passall, self.__screen, x[1]))