fix some problems where the config was not read before saving
[enigma2-plugins.git] / 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
28 autotimer = None
29 autopoller = None
30
31 # Autostart
32 def autostart(reason, **kwargs):
33         global autotimer
34         global autopoller
35
36         # Startup
37         if config.plugins.autotimer.autopoll.value and reason == 0:
38                 # Initialize AutoTimer
39                 from AutoTimer import AutoTimer
40                 autotimer = AutoTimer()
41
42                 # Start Poller
43                 from AutoPoller import AutoPoller
44                 autopoller = AutoPoller()
45                 autopoller.start()
46         # Shutdown
47         elif reason == 1:
48                 # Stop Poller
49                 if autopoller is not None:
50                         autopoller.stop()
51                         autopoller = None
52
53                 if autotimer is not None:
54                         # We re-read the config so we won't save wrong information
55                         try:
56                                 autotimer.readXml()
57                         except:
58                                 pass
59
60                         # Save xml
61                         autotimer.writeXml()
62
63                         # Remove AutoTimer
64                         autotimer = None
65
66 # Mainfunction
67 def main(session, **kwargs):
68         global autotimer
69         global autopoller
70
71         if autotimer is None:
72                 from AutoTimer import AutoTimer
73                 autotimer = AutoTimer()
74
75         from xml.parsers.expat import ExpatError
76
77         try:
78                 autotimer.readXml()
79         except ExpatError, ee:
80                 session.open(
81                         MessageBox,
82                         "Your config file is not well-formed.\nError parsing in line: %s" % (ee.lineno),
83                         type = MessageBox.TYPE_ERROR,
84                         timeout = 10
85                 )
86                 return
87
88         # Do not run in background while editing, this might screw things up
89         if autopoller is not None:
90                 autopoller.stop()
91
92         from AutoTimerOverview import AutoTimerOverview
93         session.openWithCallback(
94                 editCallback,
95                 AutoTimerOverview,
96                 autotimer
97         )
98
99 def editCallback(session):
100         global autotimer
101         global autopoller
102
103         # XXX: canceling of GUI (Overview) won't affect config values which might have been changed - is this intended?
104
105         # Don't parse EPG if editing was canceled
106         if session is not None:
107                 # Poll EPGCache
108                 ret = autotimer.parseEPG()
109                 session.open(
110                         MessageBox,
111                         "Found a total of %d matching Events.\n%d Timer were added and %d modified.." % (ret[0], ret[1], ret[2]),
112                         type = MessageBox.TYPE_INFO,
113                         timeout = 10
114                 )
115
116         # Start autopoller again if wanted
117         if config.plugins.autotimer.autopoll.value:
118                 if autopoller is None:
119                         from AutoPoller import AutoPoller
120                         autopoller = AutoPoller()
121                 autopoller.start(initial = False)
122         # Remove instance if not running in background
123         else:
124                 autopoller = None
125
126                 # Save xml (as long as we did not cancel)
127                 session and autotimer.writeXml()
128                 autotimer = None
129
130 # Movielist
131 def movielist(session, service, **kwargs):
132         from AutoTimerEditor import addAutotimerFromService
133         addAutotimerFromService(session, service)
134
135 def Plugins(**kwargs):
136         from Plugins.Plugin import PluginDescriptor
137
138         return [
139                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart),
140                 PluginDescriptor(name="AutoTimer", description = _("Edit Timers and scan for new Events"), where = PluginDescriptor.WHERE_PLUGINMENU, icon = "plugin.png", fnc = main),
141                 PluginDescriptor(name="AutoTimer", description = _("Edit Timers and scan for new Events"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main),
142                 PluginDescriptor(name="AutoTimer", description= _("Add AutoTimer..."), where = PluginDescriptor.WHERE_MOVIELIST, fnc = movielist)
143         ]
144