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