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