take care of running recording before realy shutdown e2
[enigma2.git] / SleepTimer.py
1 import timer
2 import time
3 import math
4
5 from Tools import Notifications
6
7 from Components.config import config, ConfigYesNo, ConfigSelection, ConfigSubsection
8
9 from Screens.MessageBox import MessageBox
10 from Screens.Standby import Standby, TryQuitMainloop, inStandby, inTryQuitMainloop
11
12 class SleepTimerEntry(timer.TimerEntry):
13         def __init__(self, begin):
14                 timer.TimerEntry.__init__(self, int(begin), int(begin))
15                 
16                 self.prepare_time = 0
17                 
18         def getNextActivation(self):
19                 return self.begin
20                 
21         def activate(self):
22                 if self.state == self.StateRunning:
23                         if config.SleepTimer.action.value == "shutdown":
24                                 global inTryQuitMainloop
25                                 if config.SleepTimer.ask.value and not inTryQuitMainloop:
26                                         Notifications.AddNotificationWithCallback(self.shutdown, MessageBox, _("A sleep timer want's to shut down\nyour Dreambox. Shutdown now?"), timeout = 20)
27                                 else:
28                                         self.shutdown(True)
29                         elif config.SleepTimer.action.value == "standby":
30                                 global inStandby
31                                 if config.SleepTimer.ask.value and not inStandby:
32                                         Notifications.AddNotificationWithCallback(self.standby, MessageBox, _("A sleep timer want's to set your\nDreambox to standby. Do that now?"), timeout = 20)
33                                 else:
34                                         self.standby(True)
35
36                 return True
37                 
38         def shouldSkip(self):
39                 return False
40         
41         def shutdown(self, answer):
42                 global inTryQuitMainloop
43                 if answer is not None:
44                         if answer and not inTryQuitMainloop:
45                                 Notifications.AddNotification(TryQuitMainloop, 1)
46
47         def standby(self, answer):
48                 if answer is not None:
49                         global inStandby
50                         if answer and not inStandby:
51                                 Notifications.AddNotification(Standby)
52                 
53 class SleepTimer(timer.Timer):
54         def __init__(self):
55                 config.SleepTimer = ConfigSubsection()
56                 config.SleepTimer.ask = ConfigYesNo(default = True)
57                 config.SleepTimer.action = ConfigSelection(default = "shutdown", choices = [("shutdown", _("shutdown")), ("standby", _("standby"))])
58                 
59                 timer.Timer.__init__(self)
60                 self.defaultTime = 30
61                 
62         def setSleepTime(self, sleeptime):
63                 self.clear()
64                 self.addTimerEntry(SleepTimerEntry(time.time() + 60 * sleeptime))
65
66         def clear(self):
67                 self.timer_list = []
68                 
69         def getCurrentSleepTime(self):
70                 if (self.getNextRecordingTime() == -1):
71                         return self.defaultTime
72                 return int(math.ceil((self.getNextRecordingTime() - time.time()) / 60))
73
74         def isActive(self):
75                 return len(self.timer_list) > 0
76         
77