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