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