add missing file
[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                         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                         self["green_text"].setText(_("Sleep timer action:") + " " + _("Deep Standby"))
81                 elif config.SleepTimer.action.value == "standby":
82                         self["green_text"].setText(_("Sleep timer action:") + " " + _("Standby"))
83                 
84                 if config.SleepTimer.ask.value:
85                         self["yellow_text"].setText(_("Ask before shutdown:") + " " + _("yes"))
86                 else:
87                         self["yellow_text"].setText(_("Ask before shutdown:") + " " + _("no"))
88                 self["blue_text"].setText(_("Use time of currently running service"))
89
90         def cancel(self):
91                 config.SleepTimer.ask.cancel()
92                 config.SleepTimer.action.cancel()
93                 self.close()
94
95         def select(self):
96                 if self.status:
97                         time = int(self["input"].getText())
98                         config.SleepTimer.defaulttime.setValue(time)
99                         config.SleepTimer.defaulttime.save()
100                         config.SleepTimer.action.save()
101                         self.session.nav.SleepTimer.setSleepTime(time)
102                         self.session.openWithCallback(self.close, MessageBox, _("The sleep timer has been activated."), MessageBox.TYPE_INFO)
103                 else:
104                         self.session.nav.SleepTimer.clear()
105                         self.session.openWithCallback(self.close, MessageBox, _("The sleep timer has been disabled."), MessageBox.TYPE_INFO)
106
107         def keyNumberGlobal(self, number):
108                 self["input"].number(number)
109
110         def selectLeft(self):
111                 self["input"].left()
112
113         def selectRight(self):
114                 self["input"].right()
115
116         def selectHome(self):
117                 self["input"].home()
118
119         def selectEnd(self):
120                 self["input"].end()
121
122         def deleteForward(self):
123                 self["input"].delete()
124
125         def deleteBackward(self):
126                 self["input"].deleteBackward()
127
128         def disableTimer(self):
129                 self.status = not self.status
130                 self.updateColors()
131
132         def toggleAction(self):
133                 if config.SleepTimer.action.value == "shutdown":
134                         config.SleepTimer.action.value = "standby"
135                 elif config.SleepTimer.action.value == "standby":
136                         config.SleepTimer.action.value = "shutdown"
137                 self.updateColors()
138
139         def toggleAsk(self):
140                 config.SleepTimer.ask.value = not config.SleepTimer.ask.value
141                 self.updateColors()
142                 
143         def useServiceTime(self):
144                 remaining = None
145                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
146                 if ref:
147                         path = ref.getPath()
148                         if path: # Movie
149                                 service = self.session.nav.getCurrentService()
150                                 seek = service and service.seek()
151                                 if seek:
152                                         length = seek.getLength()
153                                         position = seek.getPlayPosition()
154                                         if length and position:
155                                                 remaining = length[1] - position[1]
156                                                 if remaining > 0:
157                                                         remaining = remaining / 90000
158                         else: # DVB
159                                 epg = eEPGCache.getInstance()
160                                 event = epg.lookupEventTime(ref, -1, 0)
161                                 if event:
162                                         now = int(time())
163                                         start = event.getBeginTime()
164                                         duration = event.getDuration()
165                                         end = start + duration
166                                         remaining = end - now
167                 if remaining:
168                         config.SleepTimer.defaulttime.value = (remaining / 60) + 2
169                         self["input"].setText(str((remaining / 60) + 2))