PushService: Initial release
[enigma2-plugins.git] / pushservice / src / Plugins / RecordNotification.py
1 # Config
2 from Components.config import ConfigYesNo, NoSave
3
4 # Plugin internal
5 from Plugins.Extensions.PushService.__init__ import _
6 from Plugins.Extensions.PushService.PluginBase import PluginBase
7
8 # Plugin specific
9 import NavigationInstance
10 from time import localtime, strftime
11 from enigma import eTimer
12
13
14 SUBJECT = _("Record Notification")
15
16
17 class RecordNotification(PluginBase):
18         
19         ForceSingleInstance = True
20         
21         def __init__(self):
22                 # Is called on instance creation
23                 PluginBase.__init__(self)
24                 
25                 self.forceBindRecordTimer = eTimer()
26                 self.forceBindRecordTimer.callback.append(self.begin)
27  
28                 # Default configuration
29                 self.setOption( 'send_on_start', NoSave(ConfigYesNo( default = False )), _("Send notification on record start") )
30                 self.setOption( 'send_on_end',   NoSave(ConfigYesNo( default = True )),  _("Send notification on record end") )
31
32         def begin(self):
33                 # Is called after starting PushSerive
34                 if self.getValue('send_on_start') or self.getValue('send_on_end'):
35                         if NavigationInstance.instance:
36                                 if self.onRecordEvent not in NavigationInstance.instance.RecordTimer.on_state_change:
37                                         # Append callback function
38                                         NavigationInstance.instance.RecordTimer.on_state_change.append(self.onRecordEvent)
39                         else:
40                                 # Try again later
41                                 self.forceBindRecordTimer.startLongTimer(1)
42                 else:
43                         # Remove callback function
44                         self.end()
45
46         def end(self):
47                 # Is called after stopping PushSerive
48                 if NavigationInstance.instance:
49                         # Remove callback function
50                         if self.onRecordEvent in NavigationInstance.instance.RecordTimer.on_state_change:
51                                 NavigationInstance.instance.RecordTimer.on_state_change.remove(self.onRecordEvent)
52
53         def onRecordEvent(self, timer):
54                 text = ""
55                 if timer.state == timer.StatePrepared:
56                         pass
57                 
58                 elif timer.state == timer.StateRunning:
59                         if self.getValue('send_on_start'):
60                                 text += _("Record started:\n") \
61                                                         + str(timer.name) + "  " \
62                                                         + strftime(_("%Y.%m.%d %H:%M"), localtime(timer.begin)) + " - " \
63                                                         + strftime(_("%H:%M"), localtime(timer.end)) + "  " \
64                                                         + str(timer.service_ref and timer.service_ref.getServiceName() or "")
65                                 del timer
66                         
67                 # Finished repeating timer will report the state StateEnded+1 or StateWaiting
68                 else:
69                         if self.getValue('send_on_end'):
70                                 text += _("Record finished:\n  ") \
71                                                         + str(timer.name) + "\t" \
72                                                         + strftime(_("%Y.%m.%d %H:%M"), localtime(timer.begin)) + " - " \
73                                                         + strftime(_("%H:%M"), localtime(timer.end)) + "\t" \
74                                                         + str(timer.service_ref and timer.service_ref.getServiceName() or "")
75                                 del timer
76                 
77                 if text:
78                         #TODO Problem test tun won't get the message
79                         # Push mail
80                         from Plugins.Extensions.PushService.plugin import gPushService
81                         if gPushService:
82                                 gPushService.push(SUBJECT, text, [], self.success, self.error)
83