enigma2 20130114 (master) -> 20130216 (master)
[enigma2.git] / usr / lib / enigma2 / 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 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                         self.time = config.SleepTimer.defaulttime.value
37                 self["input"] = Input(text = str(self.time), maxSize = False, type = Input.NUMBER)
38                 
39                 self.status = True
40                 self.updateColors()
41                 
42                 self["pretext"] = Label(_("Shutdown Dreambox after"))
43                 self["aftertext"] = Label(_("minutes"))
44                 
45                 self["actions"] = NumberActionMap(["SleepTimerEditorActions", "TextEntryActions", "KeyboardInputActions"], 
46                 {
47                         "exit": self.cancel,
48                         "select": self.select,
49                         "1": self.keyNumberGlobal,
50                         "2": self.keyNumberGlobal,
51                         "3": self.keyNumberGlobal,
52                         "4": self.keyNumberGlobal,
53                         "5": self.keyNumberGlobal,
54                         "6": self.keyNumberGlobal,
55                         "7": self.keyNumberGlobal,
56                         "8": self.keyNumberGlobal,
57                         "9": self.keyNumberGlobal,
58                         "0": self.keyNumberGlobal,
59                         "selectLeft": self.selectLeft,
60                         "selectRight": self.selectRight,
61                         "left": self.selectLeft,
62                         "right": self.selectRight,
63                         "home": self.selectHome,
64                         "end": self.selectEnd,
65                         "deleteForward": self.deleteForward,
66                         "deleteBackward": self.deleteBackward,
67                         "disableTimer": self.disableTimer,
68                         "toggleAction": self.toggleAction,
69                         "toggleAsk": self.toggleAsk,
70                         "useServiceTime": self.useServiceTime
71                 }, -1)
72
73         def updateColors(self):
74                 if self.status:
75                         self["red_text"].setText(_("Action:") + " " + _("Enable timer"))
76                 else:
77                         self["red_text"].setText(_("Action:") + " " + _("Disable timer"))
78                 
79                 if config.SleepTimer.action.value == "shutdown":
80                         if SystemInfo["DeepstandbySupport"]:
81                                 shutdownString = _("Deep Standby")
82                         else:
83                                 shutdownString = _("Shutdown")
84                         self["green_text"].setText(_("Sleep timer action:") + " " + shutdownString)
85                 elif config.SleepTimer.action.value == "standby":
86                         self["green_text"].setText(_("Sleep timer action:") + " " + _("Idle Mode"))
87                 
88                 if config.SleepTimer.ask.value:
89                         self["yellow_text"].setText(_("Ask before shutdown:") + " " + _("yes"))
90                 else:
91                         self["yellow_text"].setText(_("Ask before shutdown:") + " " + _("no"))
92                 self["blue_text"].setText(_("Use time of currently running service"))
93
94         def cancel(self):
95                 config.SleepTimer.ask.cancel()
96                 config.SleepTimer.action.cancel()
97                 self.close()
98
99         def select(self):
100                 if self.status:
101                         time = int(self["input"].getText())
102                         config.SleepTimer.defaulttime.setValue(time)
103                         config.SleepTimer.defaulttime.save()
104                         config.SleepTimer.action.save()
105                         config.SleepTimer.ask.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 and path[0] == '/': # 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))