typo fix
[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, InfoBarChannelSelection
7 from Screens.ChannelSelection import ChannelSelection
8 from Screens.InfoBar import InfoBar, MoviePlayer
9 from Screens.MessageBox import MessageBox
10 from Components.config import config, ConfigSubsection, ConfigSelection, ConfigYesNo
11 from operator import attrgetter
12 import inspect
13
14 config.plugins.movieepg = ConfigSubsection()
15 config.plugins.movieepg.show_epg_entry = ConfigSelection(choices=[
16                 ("never", _("Never")),
17                 ("movie", _("Movie Player")),
18                 ("always", _("always")),
19         ], default = "movie"
20 )
21 config.plugins.movieepg.show_servicelist_plugins_in_movieplayer = ConfigYesNo(default = True)
22
23 MODE_OFF = False
24 MODE_ON = True
25 MODE_MOVIEPLAYER = 2
26
27 # 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.
28 # Each of these steps are enclosed in their own sections and their purpose is explained.
29
30 # Step 1: change way used to determine if plugins requiring service list should be shown
31 # Base implementation uses isinstance(self, InfoBarChannelSelection) which we do not want (to implement) for various reasons.
32 # Instead we we check for the instance variable "servicelist".
33 #
34 # Additionally, we inject our "fake" plugin only if the (original?) movie player is running.
35 #
36 # Doing so should not affect behavior if plugin is disabled, though we still overwrite these functions to make things easier to manage internally.
37 def InfoBarPlugins_getPluginList(self, *args, **kwargs):
38         l = []
39         showSlistPlugins = (config.plugins.movieepg.show_servicelist_plugins_in_movieplayer.value and hasattr(self, 'servicelist')) or isinstance(self, InfoBarChannelSelection)
40         for p in plugins.getPlugins(where = PluginDescriptor.WHERE_EXTENSIONSMENU):
41                 args = inspect.getargspec(p.__call__)[0]
42                 if len(args) == 1 or len(args) == 2 and showSlistPlugins:
43                         l.append(p)
44         l.sort(key=attrgetter('weight', 'name')) # sort first by weight, then by name
45
46         # "tranform" into weird internal format
47         l = [((boundFunction(self.getPluginName, p.name), boundFunction(self.runPlugin, p), lambda: True), None, p.name) for p in l]
48
49         # add fake plugin if show_epg_entry set to "always" or "movie" and this is the movie player
50         show_epg_entry = config.plugins.movieepg.show_epg_entry.value
51         if show_epg_entry == "always" or show_epg_entry == "movie" and isinstance(self, InfoBarMoviePlayerSummarySupport):
52                 l.append(((boundFunction(self.getPluginName, "EPG"), boundFunction(self.runPlugin, entry), lambda: True), None, "EPG"))
53
54         return l
55 def InfoBarPlugins_runPlugin(self, plugin, *args, **kwargs):
56         if hasattr(self, 'servicelist'):
57                 plugin(session = self.session, servicelist = self.servicelist)
58         else:
59                 plugin(session = self.session)
60 InfoBarPlugins.getPluginList = InfoBarPlugins_getPluginList
61 InfoBarPlugins.runPlugin = InfoBarPlugins_runPlugin
62
63 # Step 2: Overwrite some ChannelSelection code to be able to interject channel selection
64 #
65 # Doing so should not affect behavior if plugin is disabled, though we still overwrite these functions to make things easier to manage internally.
66 def ChannelSelection_close(self, *args, **kwargs):
67         if hasattr(self, 'secretMovieMode') and self.secretMovieMode != MODE_MOVIEPLAYER:
68                 # handles "plugin" close
69                 self.secretMovieMode = MODE_OFF
70         baseChannelSelection_close(self, *args, **kwargs)
71 baseChannelSelection_close = ChannelSelection.close
72 ChannelSelection.close = ChannelSelection_close
73
74 def ChannelSelection_zap(self, *args, **kwargs):
75         if hasattr(self, 'secretMovieMode') and self.secretMovieMode:
76                 if movieEpgMoviePlayerInstance is not None:
77                         movieEpgMoviePlayerInstance.lastservice = self.getCurrentSelection()
78                         movieEpgMoviePlayerInstance.leavePlayer()
79                 return
80         baseChannelSelection_zap(self, *args, **kwargs)
81 baseChannelSelection_zap = ChannelSelection.zap
82 ChannelSelection.zap = ChannelSelection_zap
83
84 # Step 3: Plugin which allows access to service list from extension menu (and possibly later on from plugin menu)
85 # Absolutely no effect on its own.
86 def entry(session = None, servicelist = None):
87         # XXX: session.current_dialog is the movie player (or infobar if ran from "regular" extension menu)
88         if not session: return
89         if not servicelist:
90                 if InfoBar.instance:
91                         servicelist = InfoBar.instance.servicelist
92                 else:
93                         session.open(MessageBox, _("Unable to access InfoBar!\nEPG not available."), MessageBox.TYPE_ERROR)
94                         return
95
96         if hasattr(servicelist, 'secretMovieMode') and servicelist.secretMovieMode != MODE_MOVIEPLAYER:
97                 servicelist.secretMovieMode = MODE_ON
98         session.execDialog(servicelist)
99
100 # Step 4: Modify standard movie player to keep a reference to the service list (taken from the info bar)
101 # We also save a reference to the movie list here which we use to provide the "standard" close dialog when trying to zap
102 #
103 # Basically no effect on its own.
104 movieEpgMoviePlayerInstance = None
105 def MoviePlayer___init__(self, *args, **kwargs):
106         baseMoviePlayer___init__(self, *args, **kwargs)
107         if InfoBar.instance:
108                 self.servicelist = InfoBar.instance.servicelist
109                 self.servicelist.secretMovieMode = MODE_MOVIEPLAYER
110         global movieEpgMoviePlayerInstance
111         movieEpgMoviePlayerInstance = self
112 baseMoviePlayer___init__ = MoviePlayer.__init__
113 MoviePlayer.__init__ = MoviePlayer___init__
114
115 def MoviePlayer_close(self, *args, **kwargs):
116         global movieEpgMoviePlayerInstance
117         movieEpgMoviePlayerInstance = None
118         self.servicelist.secretMovieMode = MODE_OFF
119         baseMoviePlayer_close(self, *args, **kwargs)
120 baseMoviePlayer_close = MoviePlayer.close
121 MoviePlayer.close = MoviePlayer_close
122
123 # Configuration
124 from MovieEpgSetup import MovieEpgSetup
125
126 def main(session):
127         session.open(MovieEpgSetup)
128
129 def Plugins(**kwargs):
130         return [
131                 PluginDescriptor(
132                         name="Movie-EPG",
133                         description=_("Configure Movie-EPG Plugin"),
134                         where=PluginDescriptor.WHERE_PLUGINMENU,
135                         fnc=main,
136                         needsRestart=True, # XXX: force restart for now as I don't think it will work properly without doing so
137                 ),
138         ]