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