stupid workaround for something that is not really a problem
[enigma2-plugins.git] / epgrefresh / src / plugin.py
1 # for localized messages
2 from . import _
3
4 # Config
5 from Components.config import config, ConfigEnableDisable, ConfigNumber, \
6         ConfigSubsection, ConfigClock
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 config.plugins.epgrefresh = ConfigSubsection()
21 config.plugins.epgrefresh.enabled = ConfigEnableDisable(default = False)
22 config.plugins.epgrefresh.begin = ConfigClock(default = int(begin))
23 config.plugins.epgrefresh.end = ConfigClock(default = int(end))
24 config.plugins.epgrefresh.interval = ConfigNumber(default = 2)
25 config.plugins.epgrefresh.delay_standby = ConfigNumber(default = 10)
26 config.plugins.epgrefresh.inherit_autotimer = ConfigEnableDisable(default = False)
27 config.plugins.epgrefresh.afterevent = ConfigEnableDisable(default = False)
28 config.plugins.epgrefresh.force = ConfigEnableDisable(default = False)
29 config.plugins.epgrefresh.wakeup = ConfigEnableDisable(default = False)
30 config.plugins.epgrefresh.lastscan = ConfigNumber(default = 0)
31
32 del now, begin, end
33
34 # Plugin
35 from EPGRefresh import epgrefresh
36 from EPGRefreshConfiguration import EPGRefreshConfiguration
37 from EPGRefreshService import EPGRefreshService
38
39 # Plugin definition
40 from Plugins.Plugin import PluginDescriptor
41
42 def standbyQuestionCallback(session, res = None):
43         if res:
44                 from Screens.Standby import Standby
45                 session.open(Standby)
46
47 # Autostart
48 def autostart(reason, **kwargs):
49         if config.plugins.epgrefresh.enabled.value and reason == 0 \
50                 and kwargs.has_key("session"):
51
52                 session = kwargs["session"]
53                 if config.plugins.epgrefresh.wakeup.value:
54                         now = localtime()
55                         begin = int(mktime(
56                                 (now.tm_year, now.tm_mon, now.tm_mday,
57                                 config.plugins.epgrefresh.begin.value[0],
58                                 config.plugins.epgrefresh.begin.value[1],
59                                 0, now.tm_wday, now.tm_yday, now.tm_isdst)
60                         ))
61                         # booted +- 10min from begin of timespan
62                         if abs(time() - begin) < 600:
63                                 from Screens.MessageBox import MessageBox
64                                 from Tools.Notifications import AddNotificationWithCallback
65                                 from Tools.BoundFunction import boundFunction
66                                 # XXX: we use a notification because this will be suppressed otherwise
67                                 AddNotificationWithCallback(
68                                         boundFunction(standbyQuestionCallback, session),
69                                         MessageBox,
70                                         _("This might have been an automated bootup to refresh the EPG. For this to happen it is recommmended to put the receiver to Standby.\nDo you want to do this now?"),
71                                         timeout = 15
72                                 )
73
74                 epgrefresh.start(session)
75
76         elif reason == 1:
77                 epgrefresh.stop()
78
79 def getNextWakeup():
80         # Return invalid time if not automatically refreshing
81         if not config.plugins.epgrefresh.enabled.value or \
82                 not config.plugins.epgrefresh.wakeup.value:
83
84                 return -1
85
86         now = localtime()
87         begin = int(mktime(
88                 (now.tm_year, now.tm_mon, now.tm_mday,
89                 config.plugins.epgrefresh.begin.value[0],
90                 config.plugins.epgrefresh.begin.value[1],
91                 0, now.tm_wday, now.tm_yday, now.tm_isdst)
92         ))
93         # todays timespan has not yet begun
94         if begin > time():
95                 return begin
96         # otherwise add 1 day
97         return begin+86400
98
99 # Mainfunction
100 def main(session, **kwargs):
101         epgrefresh.stop()
102         session.openWithCallback(
103                 doneConfiguring,
104                 EPGRefreshConfiguration
105         )
106
107 def doneConfiguring(session, **kwargs):
108         if config.plugins.epgrefresh.enabled.value:
109                 epgrefresh.start(session)
110
111 # Eventinfo
112 def eventinfo(session, servicelist, **kwargs):
113         ref = session.nav.getCurrentlyPlayingServiceReference()
114         if not ref:
115                 return
116         sref = ref.toString()
117         # strip all after last :
118         pos = sref.rfind(':')
119         if pos != -1:
120                 sref = sref[:pos+1]
121
122         epgrefresh.services[0].add(EPGRefreshService(str(sref), None))
123
124 def Plugins(**kwargs):
125         return [
126                 PluginDescriptor(
127                         name = "EPGRefresh",
128                         where = [
129                                 PluginDescriptor.WHERE_AUTOSTART,
130                                 PluginDescriptor.WHERE_SESSIONSTART
131                         ],
132                         fnc = autostart,
133                         wakeupfnc = getNextWakeup
134                 ),
135                 PluginDescriptor(
136                         name = "EPGRefresh",
137                         description = _("Automated EPGRefresher"),
138                         where = PluginDescriptor.WHERE_PLUGINMENU,
139                         fnc = main
140                 ),
141                 PluginDescriptor(
142                         name = _("Add to EPGRefresh"),
143                         where = PluginDescriptor.WHERE_EVENTINFO,
144                         fnc = eventinfo
145                 ),
146         ]