add eventinfo hook
[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 # Autostart
43 def autostart(reason, **kwargs):
44         if config.plugins.epgrefresh.enabled.value and reason == 0 \
45                 and kwargs.has_key("session"):
46
47                 epgrefresh.start(kwargs["session"])
48
49         elif reason == 1:
50                 epgrefresh.stop()
51
52 def getNextWakeup():
53         # Return invalid time if not automatically refreshing
54         if not config.plugins.epgrefresh.enabled.value or \
55                 not config.plugins.epgrefresh.wakeup.value:
56
57                 return -1
58
59         now = localtime()
60         begin = int(mktime(
61                 (now.tm_year, now.tm_mon, now.tm_mday,
62                 config.plugins.epgrefresh.begin.value[0],
63                 config.plugins.epgrefresh.begin.value[1],
64                 0, now.tm_wday, now.tm_yday, now.tm_isdst)
65         ))
66         # todays timespan has not yet begun
67         if begin > time():
68                 return begin
69         # otherwise add 1 day
70         return begin+86400
71
72 # Mainfunction
73 def main(session, **kwargs):
74         epgrefresh.stop()
75         session.openWithCallback(
76                 doneConfiguring,
77                 EPGRefreshConfiguration
78         )
79
80 def doneConfiguring(session, **kwargs):
81         if config.plugins.epgrefresh.enabled.value:
82                 epgrefresh.start(session)
83
84 # Eventinfo
85 def eventinfo(session, servicelist, **kwargs):
86         ref = session.nav.getCurrentlyPlayingServiceReference()
87         if not ref:
88                 return
89         sref = ref.toString()
90         # strip all after last :
91         pos = sref.rfind(':')
92         if pos != -1:
93                 sref = sref[:pos+1]
94
95         epgrefresh.services[0].add(EPGRefreshService(sref, None))
96
97 def Plugins(**kwargs):
98         return [
99                 PluginDescriptor(
100                         name = "EPGRefresh",
101                         description = _("Automated EPGRefresher"),
102                         where = [
103                                 PluginDescriptor.WHERE_AUTOSTART,
104                                 PluginDescriptor.WHERE_SESSIONSTART
105                         ],
106                         fnc = autostart,
107                         wakeupfnc = getNextWakeup
108                 ),
109                 PluginDescriptor(
110                         name = "EPGRefresh",
111                         description = _("Automated EPGRefresher"),
112                         where = PluginDescriptor.WHERE_PLUGINMENU,
113                         fnc = main
114                 ),
115                 PluginDescriptor(
116                         name = _("Add to EPGRefresh"),
117                         where = PluginDescriptor.WHERE_EVENTINFO,
118                         fnc = eventinfo
119                 ),
120         ]