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