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