epgrefresh: add help based on mphelp
[enigma2-plugins.git] / epgrefresh / src / plugin.py
1 # for localized messages
2 from . import _
3
4 # Config
5 from Components.config import config, ConfigYesNo, ConfigNumber, \
6         ConfigSelection, ConfigSubsection, ConfigClock, ConfigYesNo
7
8 # Calculate default begin/end
9 from time import time, localtime, mktime
10 now = localtime()
11 begin = mktime((
12         now.tm_year, now.tm_mon, now.tm_mday, 20, 15, \
13         0, now.tm_wday, now.tm_yday, now.tm_isdst)
14 )
15 end = mktime((
16         now.tm_year, now.tm_mon, now.tm_mday, 06, 30, \
17         0, now.tm_wday, now.tm_yday, now.tm_isdst)
18 )
19
20 #Configuration
21 config.plugins.epgrefresh = ConfigSubsection()
22 config.plugins.epgrefresh.enabled = ConfigYesNo(default = False)
23 config.plugins.epgrefresh.begin = ConfigClock(default = int(begin))
24 config.plugins.epgrefresh.end = ConfigClock(default = int(end))
25 config.plugins.epgrefresh.interval = ConfigNumber(default = 2)
26 config.plugins.epgrefresh.delay_standby = ConfigNumber(default = 10)
27 config.plugins.epgrefresh.inherit_autotimer = ConfigYesNo(default = False)
28 config.plugins.epgrefresh.afterevent = ConfigYesNo(default = False)
29 config.plugins.epgrefresh.force = ConfigYesNo(default = False)
30 config.plugins.epgrefresh.enablemessage = ConfigYesNo(default = True)
31 config.plugins.epgrefresh.wakeup = ConfigYesNo(default = False)
32 config.plugins.epgrefresh.lastscan = ConfigNumber(default = 0)
33 config.plugins.epgrefresh.parse_autotimer = ConfigYesNo(default = False)
34 config.plugins.epgrefresh.adapter = ConfigSelection(choices = [
35                 ("main", _("Main Picture")),
36                 ("pip", _("Picture in Picture")),
37                 ("pip_hidden", _("Picture in Picture (hidden)")),
38                 ("record", _("Fake recording")),
39         ], default = "main"
40 )
41 config.plugins.epgrefresh.show_in_extensionsmenu = ConfigYesNo(default = False)
42 config.plugins.epgrefresh.show_help = ConfigYesNo(default = True)
43
44 # convert previous parameter
45 config.plugins.epgrefresh.background = ConfigYesNo(default = False)
46 if config.plugins.epgrefresh.background.value:
47         config.plugins.epgrefresh.adapter.value = "pip_hidden"
48         config.plugins.epgrefresh.background.value = False
49         config.plugins.epgrefresh.save()
50
51 del now, begin, end
52
53 #pragma mark - Help
54 def getHelpName():
55         return _("EPGRefresh Help")
56
57 def getHelpText():
58         return (
59                 HelpPage(
60                         _("Welcome to EPGRefresh"),
61                         _("This help screen is supposed to give you a quick look at everything EPGRefresh has to offer.\nYou can abort it at any time by pressing the RED or EXIT button on your remote control or bring it up at a later point by pressing the HELP button from the configuration menu (more on that later).\n\n\nBut you really should consider to take the few minutes it takes to read these help pages.")
62                 ),
63                 HelpPage(
64                         _("The configuration menu"),
65                         _("This is the entry point of EPGRefresh. From this menu you can configure every aspect of the plugin and start a manual refresh of the EPG.\nThe configuration options each have an explaination which is supposed to help you to understand their effects better. Please give reading them a try, they can save you a lot of time.\n\nUsing the YELLOW button you can start a refresh manually and the INFO button brings up the date the last refresh was completed successfully.\nThe BLUE key opens the service editor (next page).")
66                 ),
67                 HelpPage(
68                         _("Editing the service list"),
69                         _("While the screen does not immediately show it, it does have a lot to offer. The topmost line allows you to choose between the editor for channels and bouquets and the following lines contain the channels/bouquets you chose to refresh.\n\nYou can use the BLUE button to add a new entry to the list or the YELLOW button to remove an existing one.\n\n\nFor most people it should be sufficient to add the \"Favourites\" bouquet by selecting \"Bouquets\" in the first line and by adding it using the BLUE button.")
70                 ),
71                 HelpPage(
72                         _("Congratulations"),
73                         _("You now know how to do your first steps in EPGRefresh.\n\nAs a final note I want to hint you at the fact that this plugin will not do anything on it's own without YOU telling it to. So if you want the refresh to happen automatically in the background you need to configure the plugin to do so.\nThis was not done to cause you any inconvenience but rather to give you the freedom of choice.")
74                 ),
75         )
76
77 try:
78         from Plugins.SystemPlugins.MPHelp import registerHelp, HelpPage
79 except ImportError, ie:
80         print "[EPGRefresh] Unable to find MPHelp, help not available!"
81 else:
82         registerHelp(getHelpName, getHelpText, "EPGRefreshHelp")
83 #pragma mark -
84
85 # Plugin
86 from EPGRefresh import epgrefresh
87 from EPGRefreshConfiguration import EPGRefreshConfiguration
88 from EPGRefreshService import EPGRefreshService
89
90 # Plugins
91 from Components.PluginComponent import plugins
92 from Plugins.Plugin import PluginDescriptor
93
94 def standbyQuestionCallback(session, res = None):
95         if res:
96                 from Screens.Standby import Standby
97                 session.open(Standby)
98
99 # Autostart
100 def autostart(reason, **kwargs):
101         if reason == 0 and kwargs.has_key("session"):
102                 session = kwargs["session"]
103                 epgrefresh.session = session
104
105                 if config.plugins.epgrefresh.enabled.value:
106                         if config.plugins.epgrefresh.wakeup.value:
107                                 now = localtime()
108                                 begin = int(mktime(
109                                         (now.tm_year, now.tm_mon, now.tm_mday,
110                                         config.plugins.epgrefresh.begin.value[0],
111                                         config.plugins.epgrefresh.begin.value[1],
112                                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
113                                 ))
114                                 # booted +- 10min from begin of timespan
115                                 if abs(time() - begin) < 600:
116                                         from Screens.MessageBox import MessageBox
117                                         from Tools.Notifications import AddNotificationWithCallback
118                                         from Tools.BoundFunction import boundFunction
119                                         # XXX: we use a notification because this will be suppressed otherwise
120                                         AddNotificationWithCallback(
121                                                 boundFunction(standbyQuestionCallback, session),
122                                                 MessageBox,
123                                                 _("This might have been an automated bootup to refresh the EPG. For this to happen it is recommended to put the receiver to Standby.\nDo you want to do this now?"),
124                                                 timeout = 15
125                                         )
126
127                         epgrefresh.start(session)
128
129         elif reason == 1:
130                 epgrefresh.stop()
131
132 def getNextWakeup():
133         # Return invalid time if not automatically refreshing
134         if not config.plugins.epgrefresh.enabled.value or \
135                 not config.plugins.epgrefresh.wakeup.value:
136
137                 return -1
138
139         now = localtime()
140         begin = int(mktime(
141                 (now.tm_year, now.tm_mon, now.tm_mday,
142                 config.plugins.epgrefresh.begin.value[0],
143                 config.plugins.epgrefresh.begin.value[1],
144                 0, now.tm_wday, now.tm_yday, now.tm_isdst)
145         ))
146         # todays timespan has not yet begun
147         if begin > time():
148                 return begin
149         # otherwise add 1 day
150         return begin+86400
151
152 # Mainfunction
153 def main(session, **kwargs):
154         epgrefresh.stop()
155         session.openWithCallback(
156                 doneConfiguring,
157                 EPGRefreshConfiguration
158         )
159
160 def doneConfiguring(session, **kwargs):
161         if config.plugins.epgrefresh.enabled.value:
162                 epgrefresh.start(session)
163
164 # Eventinfo
165 def eventinfo(session, servicelist, **kwargs):
166         ref = session.nav.getCurrentlyPlayingServiceReference()
167         if not ref:
168                 return
169         sref = ref.toString()
170         # strip all after last :
171         pos = sref.rfind(':')
172         if pos != -1:
173                 sref = sref[:pos+1]
174
175         epgrefresh.services[0].add(EPGRefreshService(str(sref), None))
176
177 # XXX: we need this helper function to identify the descriptor
178 # Extensions menu
179 def extensionsmenu(session, **kwargs):
180         main(session, **kwargs)
181
182 def housekeepingExtensionsmenu(el):
183         if el.value:
184                 plugins.addPlugin(extDescriptor)
185         else:
186                 plugins.removePlugin(extDescriptor)
187
188 config.plugins.epgrefresh.show_in_extensionsmenu.addNotifier(housekeepingExtensionsmenu, initial_call = False, immediate_feedback = True)
189 extDescriptor = PluginDescriptor(name="EPGRefresh", description = _("Automatically refresh EPG"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = extensionsmenu, needsRestart=False)
190
191 def Plugins(**kwargs):
192         # NOTE: this might be a little odd to check this, but a user might expect
193         # the plugin to resume normal operation if installed during runtime, but
194         # this is not given if the plugin is supposed to run in background (as we
195         # won't be handed the session which we need to zap). So in turn we require
196         # a restart if-and only if-we're installed during runtime AND running in
197         # background. To improve the user experience in this situation, we hide
198         # all references to this plugin.
199         needsRestart = config.plugins.epgrefresh.enabled.value and not plugins.firstRun
200         list = [
201                 PluginDescriptor(
202                         name = "EPGRefresh",
203                         where = [
204                                 PluginDescriptor.WHERE_AUTOSTART,
205                                 PluginDescriptor.WHERE_SESSIONSTART
206                         ],
207                         fnc = autostart,
208                         wakeupfnc = getNextWakeup,
209                         needsRestart = needsRestart,
210                 ),
211                 PluginDescriptor(
212                         name = _("add to EPGRefresh"),
213                         where = PluginDescriptor.WHERE_EVENTINFO,
214                         fnc = eventinfo,
215                         needsRestart = needsRestart,
216                 ),
217                 PluginDescriptor(
218                         name = "EPGRefresh",
219                         description = _("Automatically refresh EPG"),
220                         where = PluginDescriptor.WHERE_PLUGINMENU, 
221                         fnc = main,
222                         needsRestart = needsRestart,
223                 ),
224         ]
225         if config.plugins.epgrefresh.show_in_extensionsmenu.value:
226                 extDescriptor.needsRestart = needsRestart
227                 list.append(extDescriptor)
228
229         return list