pluginsort: initial checkin
[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
43 # convert previous parameter
44 config.plugins.epgrefresh.background = ConfigYesNo(default = False)
45 if config.plugins.epgrefresh.background.value:
46         config.plugins.epgrefresh.adapter.value = "pip_hidden"
47         config.plugins.epgrefresh.background.value = False
48         config.plugins.epgrefresh.save()
49
50 del now, begin, end
51
52 # Plugin
53 from EPGRefresh import epgrefresh
54 from EPGRefreshConfiguration import EPGRefreshConfiguration
55 from EPGRefreshService import EPGRefreshService
56
57 # Plugin
58 from Components.PluginComponent import plugins
59 from Plugins.Plugin import PluginDescriptor
60
61 def standbyQuestionCallback(session, res = None):
62         if res:
63                 from Screens.Standby import Standby
64                 session.open(Standby)
65
66 # Autostart
67 def autostart(reason, **kwargs):
68         if reason == 0 and kwargs.has_key("session"):
69                 session = kwargs["session"]
70                 epgrefresh.session = session
71
72                 if config.plugins.epgrefresh.enabled.value:
73                         if config.plugins.epgrefresh.wakeup.value:
74                                 now = localtime()
75                                 begin = int(mktime(
76                                         (now.tm_year, now.tm_mon, now.tm_mday,
77                                         config.plugins.epgrefresh.begin.value[0],
78                                         config.plugins.epgrefresh.begin.value[1],
79                                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
80                                 ))
81                                 # booted +- 10min from begin of timespan
82                                 if abs(time() - begin) < 600:
83                                         from Screens.MessageBox import MessageBox
84                                         from Tools.Notifications import AddNotificationWithCallback
85                                         from Tools.BoundFunction import boundFunction
86                                         # XXX: we use a notification because this will be suppressed otherwise
87                                         AddNotificationWithCallback(
88                                                 boundFunction(standbyQuestionCallback, session),
89                                                 MessageBox,
90                                                 _("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?"),
91                                                 timeout = 15
92                                         )
93
94                         epgrefresh.start(session)
95
96         elif reason == 1:
97                 epgrefresh.stop()
98
99 def getNextWakeup():
100         # Return invalid time if not automatically refreshing
101         if not config.plugins.epgrefresh.enabled.value or \
102                 not config.plugins.epgrefresh.wakeup.value:
103
104                 return -1
105
106         now = localtime()
107         begin = int(mktime(
108                 (now.tm_year, now.tm_mon, now.tm_mday,
109                 config.plugins.epgrefresh.begin.value[0],
110                 config.plugins.epgrefresh.begin.value[1],
111                 0, now.tm_wday, now.tm_yday, now.tm_isdst)
112         ))
113         # todays timespan has not yet begun
114         if begin > time():
115                 return begin
116         # otherwise add 1 day
117         return begin+86400
118
119 # Mainfunction
120 def main(session, **kwargs):
121         epgrefresh.stop()
122         session.openWithCallback(
123                 doneConfiguring,
124                 EPGRefreshConfiguration
125         )
126
127 def doneConfiguring(session, **kwargs):
128         if config.plugins.epgrefresh.enabled.value:
129                 epgrefresh.start(session)
130
131 # Eventinfo
132 def eventinfo(session, servicelist, **kwargs):
133         ref = session.nav.getCurrentlyPlayingServiceReference()
134         if not ref:
135                 return
136         sref = ref.toString()
137         # strip all after last :
138         pos = sref.rfind(':')
139         if pos != -1:
140                 sref = sref[:pos+1]
141
142         epgrefresh.services[0].add(EPGRefreshService(str(sref), None))
143
144 # XXX: we need this helper function to identify the descriptor
145 # Extensions menu
146 def extensionsmenu(session, **kwargs):
147         main(session, **kwargs)
148
149 def housekeepingExtensionsmenu(el):
150         if el.value:
151                 plugins.addPlugin(extDescriptor)
152         else:
153                 plugins.removePlugin(extDescriptor)
154
155 config.plugins.epgrefresh.show_in_extensionsmenu.addNotifier(housekeepingExtensionsmenu, initial_call = False, immediate_feedback = True)
156 extDescriptor = PluginDescriptor(name="EPGRefresh", description = _("Automatically refresh EPG"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = extensionsmenu, needsRestart=False)
157
158 def Plugins(**kwargs):
159         # NOTE: this might be a little odd to check this, but a user might expect
160         # the plugin to resume normal operation if installed during runtime, but
161         # this is not given if the plugin is supposed to run in background (as we
162         # won't be handed the session which we need to zap). So in turn we require
163         # a restart if-and only if-we're installed during runtime AND running in
164         # background. To improve the user experience in this situation, we hide
165         # all references to this plugin.
166         needsRestart = config.plugins.epgrefresh.enabled.value and not plugins.firstRun
167         list = [
168                 PluginDescriptor(
169                         name = "EPGRefresh",
170                         where = [
171                                 PluginDescriptor.WHERE_AUTOSTART,
172                                 PluginDescriptor.WHERE_SESSIONSTART
173                         ],
174                         fnc = autostart,
175                         wakeupfnc = getNextWakeup,
176                         needsRestart = needsRestart,
177                 ),
178                 PluginDescriptor(
179                         name = _("add to EPGRefresh"),
180                         where = PluginDescriptor.WHERE_EVENTINFO,
181                         fnc = eventinfo,
182                         needsRestart = needsRestart,
183                 ),
184                 PluginDescriptor(
185                         name = "EPGRefresh",
186                         description = _("Automatically refresh EPG"),
187                         where = PluginDescriptor.WHERE_PLUGINMENU, 
188                         fnc = main,
189                         needsRestart = needsRestart,
190                 ),
191         ]
192         if config.plugins.epgrefresh.show_in_extensionsmenu.value:
193                 extDescriptor.needsRestart = needsRestart
194                 list.append(extDescriptor)
195
196         return list