smaller optimizations
[vuplus_dvbapp-plugin] / autotimer / src / plugin.py
1 # for localized messages
2 from . import _
3
4 # GUI (Screens)
5 from Screens.MessageBox import MessageBox
6
7 # Config
8 from Components.config import config, ConfigSubsection, ConfigEnableDisable, \
9         ConfigNumber, ConfigSelection
10
11 # Initialize Configuration
12 config.plugins.autotimer = ConfigSubsection()
13 config.plugins.autotimer.autopoll = ConfigEnableDisable(default = False)
14 config.plugins.autotimer.interval = ConfigNumber(default = 3)
15 config.plugins.autotimer.refresh = ConfigSelection(choices = [
16                 ("none", _("None")),
17                 ("auto", _("Only AutoTimers created during this Session")),
18                 ("all", _("All non-repeating Timers"))
19         ], default = "none"
20 )
21 config.plugins.autotimer.try_guessing = ConfigEnableDisable(default = True)
22 config.plugins.autotimer.editor = ConfigSelection(choices = [
23                 ("plain", _("Classic")),
24                 ("wizard", _("Wizard"))
25         ], default = "wizard"
26 )
27 config.plugins.autotimer.disabled_on_conflict = ConfigEnableDisable(default = False)
28
29 autotimer = None
30 autopoller = None
31
32 # Autostart
33 def autostart(reason, **kwargs):
34         global autotimer
35         global autopoller
36
37         # Startup
38         if config.plugins.autotimer.autopoll.value and reason == 0:
39                 # Initialize AutoTimer
40                 from AutoTimer import AutoTimer
41                 autotimer = AutoTimer()
42
43                 # Start Poller
44                 from AutoPoller import AutoPoller
45                 autopoller = AutoPoller()
46                 autopoller.start()
47         # Shutdown
48         elif reason == 1:
49                 # Stop Poller
50                 if autopoller is not None:
51                         autopoller.stop()
52                         autopoller = None
53
54                 if autotimer is not None:
55                         # We re-read the config so we won't save wrong information
56                         try:
57                                 autotimer.readXml()
58                         except:
59                                 pass
60
61                         # Save xml
62                         autotimer.writeXml()
63
64                         # Remove AutoTimer
65                         autotimer = None
66
67 # Mainfunction
68 def main(session, **kwargs):
69         global autotimer
70         global autopoller
71
72         if autotimer is None:
73                 from AutoTimer import AutoTimer
74                 autotimer = AutoTimer()
75
76         try:
77                 autotimer.readXml()
78         except SyntaxError, se:
79                 session.open(
80                         MessageBox,
81                         _("Your config file is not well-formed:\n%s") % (str(se)),
82                         type = MessageBox.TYPE_ERROR,
83                         timeout = 10
84                 )
85                 return
86
87         # Do not run in background while editing, this might screw things up
88         if autopoller is not None:
89                 autopoller.stop()
90
91         from AutoTimerOverview import AutoTimerOverview
92         session.openWithCallback(
93                 editCallback,
94                 AutoTimerOverview,
95                 autotimer
96         )
97
98 def editCallback(session):
99         global autotimer
100         global autopoller
101
102         # XXX: canceling of GUI (Overview) won't affect config values which might have been changed - is this intended?
103
104         # Don't parse EPG if editing was canceled
105         if session is not None:
106                 # Poll EPGCache
107                 ret = autotimer.parseEPG()
108                 session.open(
109                         MessageBox,
110                         _("Found a total of %d matching Events.\n%d Timer were added and %d modified.") % (ret[0], ret[1], ret[2]),
111                         type = MessageBox.TYPE_INFO,
112                         timeout = 10
113                 )
114
115                 # Save xml
116                 autotimer.writeXml()
117
118         # Start autopoller again if wanted
119         if config.plugins.autotimer.autopoll.value:
120                 if autopoller is None:
121                         from AutoPoller import AutoPoller
122                         autopoller = AutoPoller()
123                 autopoller.start(initial = False)
124         # Remove instance if not running in background
125         else:
126                 autopoller = None
127                 autotimer = None
128
129 # Movielist
130 def movielist(session, service, **kwargs):
131         from AutoTimerEditor import addAutotimerFromService
132         addAutotimerFromService(session, service)
133
134 # Event Info
135 def eventinfo(session, servicelist, **kwargs):
136         from AutoTimerEditor import AutoTimerEPGSelection
137         ref = session.nav.getCurrentlyPlayingServiceReference()
138         session.open(AutoTimerEPGSelection, ref)
139
140 def Plugins(**kwargs):
141         from Plugins.Plugin import PluginDescriptor
142
143         return [
144                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart),
145                 PluginDescriptor(name="AutoTimer", description = _("Edit Timers and scan for new Events"), where = PluginDescriptor.WHERE_PLUGINMENU, icon = "plugin.png", fnc = main),
146                 PluginDescriptor(name="AutoTimer", description = _("Edit Timers and scan for new Events"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main),
147                 PluginDescriptor(name="AutoTimer", description= _("Add AutoTimer..."), where = PluginDescriptor.WHERE_MOVIELIST, fnc = movielist),
148                 PluginDescriptor(name="AutoTimer", description= _("Add AutoTimer..."), where = PluginDescriptor.WHERE_EVENTINFO, fnc = eventinfo),
149         ]
150