summaryrefslogtreecommitdiff
path: root/SleepTimer.py
blob: 221e8f0b29422826edeb3368fcf24bdd752d7a6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import timer
import time
import math

from Tools import Notifications

from Components.config import config, ConfigYesNo, ConfigSelection, ConfigSubsection

from Screens.MessageBox import MessageBox
import Screens.Standby

config.SleepTimer = ConfigSubsection()
config.SleepTimer.ask = ConfigYesNo(default = True)
config.SleepTimer.action = ConfigSelection(default = "shutdown", choices = [("shutdown", _("shutdown")), ("standby", _("standby"))])

class SleepTimerEntry(timer.TimerEntry):
	def __init__(self, begin):
		timer.TimerEntry.__init__(self, int(begin), int(begin))
		
		self.prepare_time = 0
		
	def getNextActivation(self):
		return self.begin
		
	def activate(self):
		if self.state == self.StateRunning:
			if config.SleepTimer.action.value == "shutdown":
				if config.SleepTimer.ask.value and not Screens.Standby.inTryQuitMainloop:
					Notifications.AddNotificationWithCallback(self.shutdown, MessageBox, _("A sleep timer wants to shut down\nyour Dreambox. Shutdown now?"), timeout = 20)
				else:
					self.shutdown(True)
			elif config.SleepTimer.action.value == "standby":
				if config.SleepTimer.ask.value and not Screens.Standby.inStandby:
					Notifications.AddNotificationWithCallback(self.standby, MessageBox, _("A sleep timer wants to set your\nDreambox to standby. Do that now?"), timeout = 20)
				else:
					self.standby(True)

		return True
		
	def shouldSkip(self):
		return False
	
	def shutdown(self, answer):
		if answer is not None:
			if answer and not Screens.Standby.inTryQuitMainloop:
				Notifications.AddNotification(Screens.Standby.TryQuitMainloop, 1)

	def standby(self, answer):
		if answer is not None:
			if answer and not Screens.Standby.inStandby:
				Notifications.AddNotification(Screens.Standby.Standby)

class SleepTimer(timer.Timer):
	def __init__(self):
		timer.Timer.__init__(self)
		self.defaultTime = 30

	def setSleepTime(self, sleeptime):
		self.clear()
		self.addTimerEntry(SleepTimerEntry(time.time() + 60 * sleeptime))

	def clear(self):
		self.timer_list = []

	def getCurrentSleepTime(self):
		llen = len(self.timer_list)
		idx = 0
		while idx < llen:
			timer = self.timer_list[idx]
			return int(math.ceil((timer.begin - time.time()) / 60))
		return self.defaultTime

	def isActive(self):
		return len(self.timer_list) > 0