movieepg: allow zapping from plugins if started from (standard) movie player
[enigma2-plugins.git] / movieepg / src / plugin.py
1 # Plugin definition
2 from Plugins.Plugin import PluginDescriptor
3
4 from Components.PluginComponent import plugins
5 from Tools.BoundFunction import boundFunction
6 from Screens.InfoBarGenerics import InfoBarPlugins, InfoBarMoviePlayerSummarySupport
7 from Screens.ChannelSelection import ChannelSelection
8 from Screens.InfoBar import InfoBar, MoviePlayer
9 import inspect
10
11 MODE_OFF = False
12 MODE_ON = True
13 MODE_MOVIEPLAYER = 2
14
15 # This plugin consists of 4 "steps" which serve their own purpose and if combined allow access to both the service list and plugins requiring the service list from within the movie player.
16 # Each of these steps are enclosed in their own sections and their purpose is explained.
17
18 # Step 1: change way used to determine if plugins requiring service list should be shown
19 # Base implementation uses isinstance(self, InfoBarChannelSelection) which we do not want (to implement) for various reasons.
20 # Instead we we check for the instance variable "servicelist".
21 #
22 # Additionally, we inject our "fake" plugin only if the (original?) movie player is running.
23 #
24 # Doing so should not affect behavior if plugin is disabled, though we still overwrite these functions to make things easier to manage internally.
25 def InfoBarPlugins_getPluginList(self, *args, **kwargs):
26         l = []
27         showSlistPlugins = hasattr(self, 'servicelist')
28         for p in plugins.getPlugins(where = PluginDescriptor.WHERE_EXTENSIONSMENU):
29                 args = inspect.getargspec(p.__call__)[0]
30                 if len(args) == 1 or len(args) == 2 and showSlistPlugins:
31                         l.append(((boundFunction(self.getPluginName, p.name), boundFunction(self.runPlugin, p), lambda: True), None, p.name))
32         # this is the/a movie player, add our fake plugin
33         if isinstance(self, InfoBarMoviePlayerSummarySupport):
34                 l.append(((boundFunction(self.getPluginName, "EPG"), boundFunction(self.runPlugin, entry), lambda: True), None, "EPG"))
35         l.sort(key = lambda e: e[2]) # sort by name
36         return l
37 def InfoBarPlugins_runPlugin(self, plugin, *args, **kwargs):
38         if hasattr(self, 'servicelist'):
39                 plugin(session = self.session, servicelist = self.servicelist)
40         else:
41                 plugin(session = self.session)
42 InfoBarPlugins.getPluginList = InfoBarPlugins_getPluginList
43 InfoBarPlugins.runPlugin = InfoBarPlugins_runPlugin
44
45 # Step 2: Overwrite some ChannelSelection code to be able to interject channel selection
46 #
47 # Doing so should not affect behavior if plugin is disabled, though we still overwrite these functions to make things easier to manage internally.
48 baseChannelSelection_close = None
49 def ChannelSelection_close(self, *args, **kwargs):
50         if hasattr(self, 'secretMovieMode') and self.secretMovieMode != MODE_MOVIEPLAYER:
51                 # handles "plugin" close
52                 self.secretMovieMode = MODE_OFF
53         baseChannelSelection_close(self, *args, **kwargs)
54 baseChannelSelection_close = ChannelSelection.close
55 ChannelSelection.close = ChannelSelection_close
56
57 baseChannelSelection_zap = None
58 def ChannelSelection_zap(self, *args, **kwargs):
59         if hasattr(self, 'secretMovieMode') and self.secretMovieMode:
60                 if movieEpgMoviePlayerInstance is not None:
61                         movieEpgMoviePlayerInstance.lastservice = self.getCurrentSelection()
62                         movieEpgMoviePlayerInstance.leavePlayer()
63                 return
64         baseChannelSelection_zap(self, *args, **kwargs)
65 baseChannelSelection_zap = ChannelSelection.zap
66 ChannelSelection.zap = ChannelSelection_zap
67
68 # Step 3: Plugin which allows access to service list from extension menu (and possibly later on from plugin menu)
69 # Absolutely no effect on its own.
70 def entry(session = None, servicelist = None):
71         # XXX: session.current_dialog is the movie player (or infobar if ran from "regular" extension menu)
72         if not session: return
73         if not servicelist:
74                 servicelist = InfoBar.instance.servicelist
75         if hasattr(servicelist, 'secretMovieMode') and servicelist.secretMovieMode != MODE_MOVIEPLAYER:
76                 servicelist.secretMovieMode = MODE_ON
77         session.execDialog(servicelist)
78
79 # Step 4: Modify standard movie player to keep a reference to the service list (taken from the info bar)
80 # We also save a reference to the movie list here which we use to provide the "standard" close dialog when trying to zap
81 #
82 # Basically no effect on its own.
83 movieEpgMoviePlayerInstance = None
84 baseMoviePlayer___init__ = None
85 def MoviePlayer___init__(self, *args, **kwargs):
86         baseMoviePlayer___init__(self, *args, **kwargs)
87         self.servicelist = InfoBar.instance.servicelist
88         self.servicelist.secretMovieMode = MODE_MOVIEPLAYER
89         global movieEpgMoviePlayerInstance
90         movieEpgMoviePlayerInstance = self
91 baseMoviePlayer___init__ = MoviePlayer.__init__
92 MoviePlayer.__init__ = MoviePlayer___init__
93
94 baseMoviePlayer_close = None
95 def MoviePlayer_close(self, *args, **kwargs):
96         global movieEpgMoviePlayerInstance
97         movieEpgMoviePlayerInstance = None
98         self.servicelist.secretMovieMode = MODE_OFF
99         baseMoviePlayer_close(self, *args, **kwargs)
100 baseMoviePlayer_close = MoviePlayer.close
101 MoviePlayer.close = MoviePlayer_close
102
103 # Autostart
104 def autostart(reason, **kwargs):
105         if reason == 0:
106                 pass # TODO: anything to do here?
107
108 def Plugins(**kwargs):
109         return [
110                 PluginDescriptor(
111                         where = PluginDescriptor.WHERE_AUTOSTART,
112                         fnc = autostart,
113                         needsRestart = True,
114                 ),
115         ]