6e1991b71ad1df4e8084589046dedea08beab181
[enigma2.git] / lib / python / Screens / SleepTimerEdit.py
1 from Screens.Screen import Screen
2 from Screens.MessageBox import MessageBox
3 from Components.ActionMap import NumberActionMap
4 from Components.Input import Input
5 from Components.Label import Label
6 from Components.Pixmap import Pixmap
7 from Components.config import config, ConfigInteger
8 from enigma import eEPGCache
9 from SleepTimer import SleepTimer
10 from time import time
11
12 config.SleepTimer.defaulttime = ConfigInteger(default = 30)
13
14 class SleepTimerEdit(Screen):
15         def __init__(self, session):
16                 Screen.__init__(self, session)
17                 
18                 self["red"] = Pixmap()
19                 self["green"] = Pixmap()
20                 self["yellow"] = Pixmap()
21                 self["blue"] = Pixmap()
22                 self["red_text"] = Label()
23                 self["green_text"] = Label()
24                 self["yellow_text"] = Label()
25                 self["blue_text"] = Label()
26                 self["current_status"] = Label()
27                 self.is_active = self.session.nav.SleepTimer.isActive()
28                 if self.is_active:
29                         self["current_status"].setText(_("Timer status:") + " " + _("Enabled"))
30                 else:
31                         self["current_status"].setText(_("Timer status:") + " " + _("Disabled"))
32                 
33                 if self.is_active:
34                         self.time = self.session.nav.SleepTimer.getCurrentSleepTime()
35                 else:
36                         remaining = None
37                         ref = self.session.nav.getCurrentlyPlayingServiceReference()
38                         if ref:
39                                 path = ref.getPath()
40                                 if path: # Movie
41                                         service = self.session.nav.getCurrentService()
42                                         seek = service and service.seek()
43                                         if seek:
44                                                 length = seek.getLength()
45                                                 position = seek.getPlayPosition()
46                                                 if length and position:
47                                                         remaining = length[1] - position[1]
48                                                         if remaining > 0:
49                                                                 remaining = remaining / 90000
50                                 else: # DVB
51                                         epg = eEPGCache.getInstance()
52                                         event = epg.lookupEventTime(ref, -1, 0)
53                                         if event:
54                                                 now = int(time())
55                                                 start = event.getBeginTime()
56                                                 duration = event.getDuration()
57                                                 end = start + duration
58                                                 remaining = end - now
59                         if remaining:
60                                 config.SleepTimer.defaulttime.value = (remaining / 60) + 2
61                         self.time = config.SleepTimer.defaulttime.value
62                 self["input"] = Input(text = str(self.time), maxSize = False, type = Input.NUMBER)
63                 
64                 self.status = True
65                 self.updateColors()
66                 
67                 self["pretext"] = Label(_("Shutdown Dreambox after"))
68                 self["aftertext"] = Label(_("minutes"))
69                 
70                 self["actions"] = NumberActionMap(["SleepTimerEditorActions", "TextEntryActions", "KeyboardInputActions"], 
71                 {
72                         "exit": self.cancel,
73                         "select": self.select,
74                         "1": self.keyNumberGlobal,
75                         "2": self.keyNumberGlobal,
76                         "3": self.keyNumberGlobal,
77                         "4": self.keyNumberGlobal,
78                         "5": self.keyNumberGlobal,
79                         "6": self.keyNumberGlobal,
80                         "7": self.keyNumberGlobal,
81                         "8": self.keyNumberGlobal,
82                         "9": self.keyNumberGlobal,
83                         "0": self.keyNumberGlobal,
84                         "selectLeft": self.selectLeft,
85                         "selectRight": self.selectRight,
86                         "left": self.selectLeft,
87                         "right": self.selectRight,
88                         "home": self.selectHome,
89                         "end": self.selectEnd,
90                         "deleteForward": self.deleteForward,
91                         "deleteBackward": self.deleteBackward,
92                         "disableTimer": self.disableTimer,
93                         "toggleAction": self.toggleAction,
94                         "toggleAsk": self.toggleAsk
95                 }, -1)
96
97         def updateColors(self):
98                 if self.status:
99                         self["red_text"].setText(_("Action:") + " " + _("Enable timer"))
100                 else:
101                         self["red_text"].setText(_("Action:") + " " + _("Disable timer"))
102                 
103                 if config.SleepTimer.action.value == "shutdown":
104                         self["green_text"].setText(_("Sleep timer action:") + " " + _("Deep Standby"))
105                 elif config.SleepTimer.action.value == "standby":
106                         self["green_text"].setText(_("Sleep timer action:") + " " + _("Standby"))
107                 
108                 if config.SleepTimer.ask.value:
109                         self["yellow_text"].setText(_("Ask before shutdown:") + " " + _("yes"))
110                 else:
111                         self["yellow_text"].setText(_("Ask before shutdown:") + " " + _("no"))
112                 self["blue_text"].setText(_("Settings"))
113
114         def cancel(self):
115                 config.SleepTimer.ask.cancel()
116                 config.SleepTimer.action.cancel()
117                 self.close()
118
119         def select(self):
120                 if self.status:
121                         time = int(self["input"].getText())
122                         config.SleepTimer.defaulttime.setValue(time)
123                         self.session.nav.SleepTimer.setSleepTime(time)
124                         self.session.openWithCallback(self.close, MessageBox, _("The sleep timer has been activated."), MessageBox.TYPE_INFO)
125                 else:
126                         self.session.nav.SleepTimer.clear()
127                         self.session.openWithCallback(self.close, MessageBox, _("The sleep timer has been disabled."), MessageBox.TYPE_INFO)
128
129         def keyNumberGlobal(self, number):
130                 self["input"].number(number)
131
132         def selectLeft(self):
133                 self["input"].left()
134
135         def selectRight(self):
136                 self["input"].right()
137
138         def selectHome(self):
139                 self["input"].home()
140
141         def selectEnd(self):
142                 self["input"].end()
143
144         def deleteForward(self):
145                 self["input"].delete()
146
147         def deleteBackward(self):
148                 self["input"].deleteBackward()
149
150         def disableTimer(self):
151                 self.status = not self.status
152                 self.updateColors()
153
154         def toggleAction(self):
155                 if config.SleepTimer.action.value == "shutdown":
156                         config.SleepTimer.action.value = "standby"
157                 elif config.SleepTimer.action.value == "standby":
158                         config.SleepTimer.action.value = "shutdown"
159                 self.updateColors()
160
161         def toggleAsk(self):
162                 config.SleepTimer.ask.value = not config.SleepTimer.ask.value
163                 self.updateColors()