smaller fixes, bump required e2 version, add sample for custom destination path
[enigma2-plugins.git] / autotimer / src / plugin.py
1 # GUI (Screens)
2 from Screens.MessageBox import MessageBox
3
4 # Plugin definition
5 from Plugins.Plugin import PluginDescriptor
6
7 # ExpatError
8 from xml.parsers.expat import ExpatError
9
10 # Config
11 from Components.config import config, ConfigSubsection, ConfigEnableDisable, \
12         ConfigInteger, ConfigSelection
13
14 # Initialize Configuration
15 config.plugins.autotimer = ConfigSubsection()
16 config.plugins.autotimer.autopoll = ConfigEnableDisable(default = False)
17 config.plugins.autotimer.interval = ConfigInteger(default = 3, limits=(1, 24))
18 config.plugins.autotimer.refresh = ConfigSelection(choices = [("none", _("None")), ("auto", _("Only AutoTimers created during this Session")), ("all", _("All non-repeating Timers"))], default = "none")
19 config.plugins.autotimer.try_guessing = ConfigEnableDisable(default = True)
20
21 autotimer = None
22 autopoller = None
23
24 # Autostart
25 def autostart(reason, **kwargs):
26         global autotimer
27         global autopoller
28
29         # Startup
30         if config.plugins.autotimer.autopoll.value and reason == 0:
31                 # Initialize AutoTimer
32                 from AutoTimer import AutoTimer
33                 autotimer = AutoTimer()
34
35                 # Start Poller
36                 from AutoPoller import AutoPoller
37                 autopoller = AutoPoller()
38                 autopoller.start()
39         # Shutdown
40         elif reason == 1:
41                 # Stop Poller
42                 if autopoller is not None:
43                         # We might shutdown when configuring, timer won't be runnign then
44                         try:
45                                 autopoller.stop()
46                         except ValueError, ve:
47                                 pass
48
49                         autopoller = None
50
51                 if autotimer is not None:
52                         # Save xml
53                         autotimer.writeXml()
54
55                         # Remove AutoTimer
56                         autotimer = None
57
58 # Mainfunction
59 def main(session, **kwargs):
60         global autotimer
61         global autopoller
62
63         if autotimer is None:
64                 from AutoTimer import AutoTimer
65                 autotimer = AutoTimer()
66
67         try:
68                 autotimer.readXml()
69         except ExpatError, ee:
70                 session.open(
71                         MessageBox,
72                         "Your config file is not well-formed.\nError parsing in line: %s" % (ee.lineno),
73                         type = MessageBox.TYPE_ERROR,
74                         timeout = 10
75                 )
76                 return
77         except Exception, e:
78                 # Don't crash during development
79                 import traceback, sys
80                 traceback.print_exc(file=sys.stdout)
81                 session.open(
82                         MessageBox,
83                         "An unexpected error occured: %s" % (e),
84                         type = MessageBox.TYPE_ERROR,
85                         timeout = 15
86                 )
87                 return
88
89         # Do not run in background while editing, this might screw things up
90         if autopoller is not None:
91                 autopoller.stop()
92
93         from AutoTimerOverview import AutoTimerOverview
94         session.openWithCallback(
95                 editCallback,
96                 AutoTimerOverview,
97                 autotimer
98         )
99
100 def editCallback(session):
101         global autotimer
102         global autopoller
103
104         # Start autopoller again if wanted
105         if config.plugins.autotimer.autopoll.value:
106                 if autopoller is None:
107                         from AutoPoller import AutoPoller
108                         autopoller = AutoPoller()
109                 autopoller.start(initial = False)
110
111         # Don't do anything when editing was canceled
112         if session is None:
113                 return
114
115         # We might re-parse Xml so catch parsing error
116         try:
117                 ret = autotimer.parseEPG()
118                 session.open(
119                         MessageBox,
120                         "Found a total of %d matching Events.\n%d Timer were added and %d modified.." % (ret[0], ret[1], ret[2]),
121                         type = MessageBox.TYPE_INFO,
122                         timeout = 10
123                 )
124         except Exception, e:
125                 # Don't crash during development
126                 import traceback, sys
127                 traceback.print_exc(file=sys.stdout)
128                 session.open(
129                         MessageBox,
130                         "An unexpected error occured: %s" % (e),
131                         type = MessageBox.TYPE_ERROR,
132                         timeout = 15
133                 )
134
135         # Remove instance if not running in background
136         if not config.plugins.autotimer.autopoll.value:
137                 autopoller = None
138
139                 # Save xml
140                 autotimer.writeXml()
141                 autotimer = None
142
143 def Plugins(**kwargs):
144         return [
145                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart),
146                 PluginDescriptor(name="AutoTimer", description = "Edit Timers and scan for new Events", where = PluginDescriptor.WHERE_PLUGINMENU, icon = "plugin.png", fnc = main),
147                 PluginDescriptor(name="AutoTimer", description = "Edit Timers and scan for new Events", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main)
148         ]