properly handle tags combined with other prerequisites
[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
19         def __init__(self, screen, eventmap):
20                 self.__eventmap = eventmap
21                 self.screen = screen
22                 screen.session.nav.event.append(self.__event)
23                 screen.onClose.append(self.__del_event)
24
25         def __del_event(self):
26                 self.screen.session.nav.event.remove(self.__event)
27
28         def __event(self, ev):
29                 set = ServiceEventTracker
30                 screen = self.screen
31                 nav = screen.session.nav
32                 cur_ref = nav.getCurrentlyPlayingServiceReference()
33                 old_service_running = set.oldRef and cur_ref and cur_ref == set.oldRef and set.oldService and set.oldService == str(nav.getCurrentService())
34                 if not old_service_running:
35                         set.oldService = None
36                         set.oldRef = None
37 #               print "old_service_running", old_service_running
38                 if ev in self.__eventmap:
39                         ssize = set.InfoBarStackSize
40                         stack = set.InfoBarStack
41                         if (not isinstance(screen, InfoBarBase) or # let pass all events to screens not derived from InfoBarBase
42                                 (not old_service_running and ssize and stack[ssize-1] == screen) or # let pass events from currently running service just to current active screen (derived from InfoBarBase)
43                                 (old_service_running and ssize > 1 and stack[ssize-2] == screen)): # let pass events from old running service just to previous active screen (derived from InfoBarBase)
44                                 self.__eventmap[ev]()
45 #                       else:
46 #                               print "ignore event", ev, "for inactive infobar '" + str(self.screen) + "'"
47
48         @staticmethod
49         def setActiveInfoBar(infobar, old_service, old_ref):
50                 set = ServiceEventTracker
51                 set.oldRef = old_ref
52                 set.oldService = old_service and str(old_service)
53                 assert infobar not in set.InfoBarStack, "FATAL: Infobar '" + str(infobar) + "' is already active!"
54                 set.InfoBarStack.append(infobar)
55                 set.InfoBarStackSize += 1
56 #               print "ServiceEventTracker set active '" + str(infobar) + "'"
57
58         @staticmethod
59         def popActiveInfoBar():
60                 set = ServiceEventTracker
61                 stack = set.InfoBarStack
62                 if set.InfoBarStackSize:
63                         set.InfoBarStackSize -= 1
64                         del stack[set.InfoBarStackSize]
65 #                       if set.InfoBarStackSize:
66 #                               print "ServiceEventTracker reset active '" + str(stack[set.InfoBarStackSize-1]) + "'"