PushService: Fixed load error
[enigma2-plugins.git] / pushservice / src / Controller / RecordNotification.py
1 #######################################################################
2 #
3 #    Push Service for Enigma-2
4 #    Coded by betonme (c) 2012 <glaserfrank(at)gmail.com>
5 #    Support: http://www.i-have-a-dreambox.com/wbb2/thread.php?threadid=167779
6 #
7 #    This program is free software; you can redistribute it and/or
8 #    modify it under the terms of the GNU General Public License
9 #    as published by the Free Software Foundation; either version 2
10 #    of the License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #######################################################################
18
19 # Config
20 from Components.config import ConfigYesNo, NoSave
21
22 # Plugin internal
23 #from Plugins.Extensions.PushService.__init__ import _
24 from Plugins.Extensions.PushService.ControllerBase import ControllerBase
25
26 # Plugin specific
27 import NavigationInstance
28 from time import localtime, strftime
29 from enigma import eTimer
30
31
32 # Constants
33 SUBJECT = _("Record Notification")
34
35
36 class RecordNotification(ControllerBase):
37         
38         ForceSingleInstance = True
39         
40         def __init__(self):
41                 # Is called on instance creation
42                 ControllerBase.__init__(self)
43                 
44                 self.forceBindRecordTimer = eTimer()
45                 self.forceBindRecordTimer.callback.append(self.begin)
46  
47                 # Default configuration
48                 self.setOption( 'send_on_start', NoSave(ConfigYesNo( default = False )), _("Send notification on record start") )
49                 self.setOption( 'send_on_end',   NoSave(ConfigYesNo( default = True )),  _("Send notification on record end") )
50                 #TODO option to send free space
51
52         def begin(self):
53                 # Is called after starting PushService
54                 
55                 if self.getValue('send_on_start') or self.getValue('send_on_end'):
56                         if NavigationInstance.instance:
57                                 if self.onRecordEvent not in NavigationInstance.instance.RecordTimer.on_state_change:
58                                         print "append"
59                                         # Append callback function
60                                         NavigationInstance.instance.RecordTimer.on_state_change.append(self.onRecordEvent)
61                         else:
62                                 # Try again later
63                                 self.forceBindRecordTimer.startLongTimer(1)
64                 else:
65                         # Remove callback function
66                         self.end()
67
68         def end(self):
69                 # Is called after stopping PushSerive
70                 if NavigationInstance.instance:
71                         # Remove callback function
72                         if self.onRecordEvent in NavigationInstance.instance.RecordTimer.on_state_change:
73                                 NavigationInstance.instance.RecordTimer.on_state_change.remove(self.onRecordEvent)
74
75         def run(self, callback, errback):
76                 # At the end a plugin has to call one of the functions: callback or errback
77                 # Callback should return with at least one of the parameter: Header, Body, Attachment
78                 # If empty or none is returned, nothing will be sent
79                 callback()
80
81         def onRecordEvent(self, timer):
82                 text = ""
83                 if timer.state == timer.StatePrepared:
84                         pass
85                 
86                 elif timer.state == timer.StateRunning:
87                         if self.getValue('send_on_start'):
88                                 text += _("Record started:\n") \
89                                                         + str(timer.name) + "  " \
90                                                         + strftime(_("%Y.%m.%d %H:%M"), localtime(timer.begin)) + " - " \
91                                                         + strftime(_("%H:%M"), localtime(timer.end)) + "  " \
92                                                         + str(timer.service_ref and timer.service_ref.getServiceName() or "")
93                                 del timer
94                         
95                 # Finished repeating timer will report the state StateEnded+1 or StateWaiting
96                 else:
97                         if self.getValue('send_on_end'):
98                                 text += _("Record finished:\n  ") \
99                                                         + str(timer.name) + "\t" \
100                                                         + strftime(_("%Y.%m.%d %H:%M"), localtime(timer.begin)) + " - " \
101                                                         + strftime(_("%H:%M"), localtime(timer.end)) + "\t" \
102                                                         + str(timer.service_ref and timer.service_ref.getServiceName() or "")
103                                 del timer
104                 
105                 if text:
106                         #TODO Problem test tun won't get the message
107                         # Push mail
108                         from Plugins.Extensions.PushService.plugin import gPushService
109                         if gPushService:
110                                 gPushService.push(self, SUBJECT, text)
111