- save configuration when leaving overview/importer,
[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         try:
76                 autotimer.readXml()
77         except SyntaxError, se:
78                 session.open(
79                         MessageBox,
80                         _("Your config file is not well-formed:\n%s") % (str(se)),
81                         type = MessageBox.TYPE_ERROR,
82                         timeout = 10
83                 )
84                 return
85
86         # Do not run in background while editing, this might screw things up
87         if autopoller is not None:
88                 autopoller.stop()
89
90         from AutoTimerOverview import AutoTimerOverview
91         session.openWithCallback(
92                 editCallback,
93                 AutoTimerOverview,
94                 autotimer
95         )
96
97 def editCallback(session):
98         global autotimer
99         global autopoller
100
101         # XXX: canceling of GUI (Overview) won't affect config values which might have been changed - is this intended?
102
103         # Don't parse EPG if editing was canceled
104         if session is not None:
105                 # Poll EPGCache
106                 ret = autotimer.parseEPG()
107                 session.open(
108                         MessageBox,
109                         _("Found a total of %d matching Events.\n%d Timer were added and %d modified.") % (ret[0], ret[1], ret[2]),
110                         type = MessageBox.TYPE_INFO,
111                         timeout = 10
112                 )
113
114                 # Save xml
115                 autotimer.writeXml()
116
117         # Start autopoller again if wanted
118         if config.plugins.autotimer.autopoll.value:
119                 if autopoller is None:
120                         from AutoPoller import AutoPoller
121                         autopoller = AutoPoller()
122                 autopoller.start(initial = False)
123         # Remove instance if not running in background
124         else:
125                 autopoller = None
126                 autotimer = None
127
128 # Movielist
129 def movielist(session, service, **kwargs):
130         from AutoTimerEditor import addAutotimerFromService
131         addAutotimerFromService(session, service)
132
133 # Event Info
134 def eventinfo(session, servicelist, **kwargs):
135         from AutoTimerEditor import AutoTimerEPGSelection
136         ref = session.nav.getCurrentlyPlayingServiceReference()
137         session.open(AutoTimerEPGSelection, ref)
138
139 def Plugins(**kwargs):
140         from Plugins.Plugin import PluginDescriptor
141
142         return [
143                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart),
144                 PluginDescriptor(name="AutoTimer", description = _("Edit Timers and scan for new Events"), where = PluginDescriptor.WHERE_PLUGINMENU, icon = "plugin.png", fnc = main),
145                 PluginDescriptor(name="AutoTimer", description = _("Edit Timers and scan for new Events"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main),
146                 PluginDescriptor(name="AutoTimer", description= _("Add AutoTimer..."), where = PluginDescriptor.WHERE_MOVIELIST, fnc = movielist),
147                 PluginDescriptor(name="AutoTimer", description= _("Add AutoTimer..."), where = PluginDescriptor.WHERE_EVENTINFO, fnc = eventinfo),
148         ]
149