workaround: flush on AUDIO_STOP
[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 import Screens.Standby
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                                 if config.SleepTimer.ask.value and not Screens.Standby.inTryQuitMainloop:
25                                         Notifications.AddNotificationWithCallback(self.shutdown, MessageBox, _("A sleep timer wants to shut down\nyour Dreambox. Shutdown now?"), timeout = 20)
26                                 else:
27                                         self.shutdown(True)
28                         elif config.SleepTimer.action.value == "standby":
29                                 if config.SleepTimer.ask.value and not Screens.Standby.inStandby:
30                                         Notifications.AddNotificationWithCallback(self.standby, MessageBox, _("A sleep timer wants to set your\nDreambox to standby. Do that now?"), timeout = 20)
31                                 else:
32                                         self.standby(True)
33
34                 return True
35                 
36         def shouldSkip(self):
37                 return False
38         
39         def shutdown(self, answer):
40                 if answer is not None:
41                         if answer and not Screens.Standby.inTryQuitMainloop:
42                                 Notifications.AddNotification(Screens.Standby.TryQuitMainloop, 1)
43
44         def standby(self, answer):
45                 if answer is not None:
46                         if answer and not Screens.Standby.inStandby:
47                                 Notifications.AddNotification(Screens.Standby.Standby)
48
49 class SleepTimer(timer.Timer):
50         def __init__(self):
51                 config.SleepTimer = ConfigSubsection()
52                 config.SleepTimer.ask = ConfigYesNo(default = True)
53                 config.SleepTimer.action = ConfigSelection(default = "shutdown", choices = [("shutdown", _("shutdown")), ("standby", _("standby"))])
54                 timer.Timer.__init__(self)
55                 self.defaultTime = 30
56
57         def setSleepTime(self, sleeptime):
58                 self.clear()
59                 self.addTimerEntry(SleepTimerEntry(time.time() + 60 * sleeptime))
60
61         def clear(self):
62                 self.timer_list = []
63
64         def getCurrentSleepTime(self):
65                 llen = len(self.timer_list)
66                 idx = 0
67                 while idx < llen:
68                         timer = self.timer_list[idx]
69                         return int(math.ceil((timer.begin - time.time()) / 60))
70                 return self.defaultTime
71
72         def isActive(self):
73                 return len(self.timer_list) > 0