fix crash which occurred when canceling overview more than once per session
[vuplus_dvbapp-plugin] / autotimer / src / plugin.py
1 # GUI (Screens)
2 from Screens.MessageBox import MessageBox
3
4 # Config
5 from Components.config import config, ConfigSubsection, ConfigEnableDisable, \
6         ConfigNumber, ConfigSelection
7
8 # Initialize Configuration
9 config.plugins.autotimer = ConfigSubsection()
10 config.plugins.autotimer.autopoll = ConfigEnableDisable(default = False)
11 config.plugins.autotimer.interval = ConfigNumber(default = 3)
12 config.plugins.autotimer.refresh = ConfigSelection(choices = [
13                 ("none", _("None")),
14                 ("auto", _("Only AutoTimers created during this Session")),
15                 ("all", _("All non-repeating Timers"))
16         ], default = "none"
17 )
18 config.plugins.autotimer.try_guessing = ConfigEnableDisable(default = True)
19
20 autotimer = None
21 autopoller = None
22
23 # Autostart
24 def autostart(reason, **kwargs):
25         global autotimer
26         global autopoller
27
28         # Startup
29         if config.plugins.autotimer.autopoll.value and reason == 0:
30                 # Initialize AutoTimer
31                 from AutoTimer import AutoTimer
32                 autotimer = AutoTimer()
33
34                 # Start Poller
35                 from AutoPoller import AutoPoller
36                 autopoller = AutoPoller()
37                 autopoller.start()
38         # Shutdown
39         elif reason == 1:
40                 # Stop Poller
41                 if autopoller is not None:
42                         # We might shutdown when configuring, timer won't be runnign then
43                         try:
44                                 autopoller.stop()
45                         except ValueError, ve:
46                                 pass
47
48                         autopoller = None
49
50                 if autotimer is not None:
51                         # Save xml
52                         autotimer.writeXml()
53
54                         # Remove AutoTimer
55                         autotimer = None
56
57 # Mainfunction
58 def main(session, **kwargs):
59         global autotimer
60         global autopoller
61
62         if autotimer is None:
63                 from AutoTimer import AutoTimer
64                 autotimer = AutoTimer()
65
66         from xml.parsers.expat import ExpatError
67
68         try:
69                 autotimer.readXml()
70         except ExpatError, ee:
71                 session.open(
72                         MessageBox,
73                         "Your config file is not well-formed.\nError parsing in line: %s" % (ee.lineno),
74                         type = MessageBox.TYPE_ERROR,
75                         timeout = 10
76                 )
77                 return
78
79         # Do not run in background while editing, this might screw things up
80         if autopoller is not None:
81                 autopoller.stop()
82
83         from AutoTimerOverview import AutoTimerOverview
84         session.openWithCallback(
85                 editCallback,
86                 AutoTimerOverview,
87                 autotimer
88         )
89
90 def editCallback(session):
91         global autotimer
92         global autopoller
93
94         # XXX: canceling of GUI (Overview) won't affect config values which might have been changed - is this intended?
95
96         # Don't parse EPG if editing was canceled
97         if session is not None:
98                 # Poll EPGCache
99                 ret = autotimer.parseEPG()
100                 session.open(
101                         MessageBox,
102                         "Found a total of %d matching Events.\n%d Timer were added and %d modified.." % (ret[0], ret[1], ret[2]),
103                         type = MessageBox.TYPE_INFO,
104                         timeout = 10
105                 )
106
107         # Start autopoller again if wanted
108         if config.plugins.autotimer.autopoll.value:
109                 if autopoller is None:
110                         from AutoPoller import AutoPoller
111                         autopoller = AutoPoller()
112                 autopoller.start(initial = False)
113         # Remove instance if not running in background
114         else:
115                 autopoller = None
116
117                 # Save xml (as long as we did not cancel)
118                 session and autotimer.writeXml()
119                 autotimer = None
120
121 def Plugins(**kwargs):
122         from Plugins.Plugin import PluginDescriptor
123
124         return [
125                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart),
126                 PluginDescriptor(name="AutoTimer", description = "Edit Timers and scan for new Events", where = PluginDescriptor.WHERE_PLUGINMENU, icon = "plugin.png", fnc = main),
127                 PluginDescriptor(name="AutoTimer", description = "Edit Timers and scan for new Events", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main)
128         ]