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