PushService: Controller Updated
[enigma2-plugins.git] / pushservice / src / Controller / DeactivatedTimers.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
30
31 # Constants
32 SUBJECT = _("Found deactivated timer(s)")
33 BODY    = _("Deactivated timer list:\n%s")
34 TAG     = _("DeactivatedTimerPushed")
35
36
37 class DeactivatedTimers(ControllerBase):
38         
39         ForceSingleInstance = True
40         
41         def __init__(self):
42                 # Is called on instance creation
43                 ControllerBase.__init__(self)
44                 self.timers = []
45                 
46                 # Default configuration
47                 self.setOption( 'remove_timer', NoSave(ConfigYesNo( default = False )), _("Remove deactivated timer(s)") )
48
49         def run(self, callback, errback):
50                 # At the end a plugin has to call one of the functions: callback or errback
51                 # Callback should return with at least one of the parameter: Header, Body, Attachment
52                 # If empty or none is returned, nothing will be sent
53                 self.timers = []
54                 text = ""
55                 for timer in NavigationInstance.instance.RecordTimer.timer_list + NavigationInstance.instance.RecordTimer.processed_timers:
56                         if timer.disabled and TAG not in timer.tags:
57                                 text += str(timer.name) + "    " \
58                                                         + strftime(_("%Y.%m.%d %H:%M"), localtime(timer.begin)) + " - " \
59                                                         + strftime(_("%H:%M"), localtime(timer.end)) + "    " \
60                                                         + str(timer.service_ref and timer.service_ref.getServiceName() or "") \
61                                                         + " " + str(timer.tags) \
62                                                         + "\n"
63                                 self.timers.append( timer )
64                 if self.timers and text:
65                         callback( SUBJECT, BODY % text )
66                 else:
67                         callback()
68
69         # Callback functions
70         def callback(self):
71                 # Called after all services succeded
72                 if self.getValue('remove_timer'):
73                         # Remove deactivated timers
74                         for timer in self.timers[:]:
75                                 if timer in NavigationInstance.instance.RecordTimer.processed_timers:
76                                         NavigationInstance.instance.RecordTimer.processed_timers.remove(timer)
77                                 elif timer in NavigationInstance.instance.RecordTimer.timer_list:
78                                         NavigationInstance.instance.RecordTimer.timer_list.remove(timer)
79                                 self.timers.remove(timer)
80                 else:
81                         # Set tag to avoid resending it
82                         for timer in self.timers[]:
83                                 if TAG not in timer.tags:
84                                         timer.tags.append(TAG)
85                         NavigationInstance.instance.RecordTimer.saveTimer()
86                         self.timers = []
87
88         def errback(self):
89                 # Called after all services has returned, but at least one has failed
90                 self.timers = []