[RecordTimer] fix repeat timer issue.
[vuplus_dvbapp] / 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 STB 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                         if self["input"].getText()=='':
103                                 self.session.nav.SleepTimer.clear()
104                                 self.session.openWithCallback(self.close, MessageBox, _("The sleep timer is invalid."), MessageBox.TYPE_INFO)
105                         else:                           
106                                 time = int(self["input"].getText())
107                                 config.SleepTimer.defaulttime.setValue(time)
108                                 config.SleepTimer.defaulttime.save()
109                                 config.SleepTimer.action.save()
110                                 config.SleepTimer.ask.save()
111                                 self.session.nav.SleepTimer.setSleepTime(time)
112                                 self.session.openWithCallback(self.close, MessageBox, _("The sleep timer has been activated."), MessageBox.TYPE_INFO)
113                 else:
114                         self.session.nav.SleepTimer.clear()
115                         self.session.openWithCallback(self.close, MessageBox, _("The sleep timer has been disabled."), MessageBox.TYPE_INFO)
116
117         def keyNumberGlobal(self, number):
118                 if self["input"].getText() == '' :
119                         inputtime=0
120                 else:
121                         inputtime= int(self["input"].getText())*10 +number
122                 if inputtime < 10000 :
123                         self["input"].number(number)
124
125         def selectLeft(self):
126                 self["input"].left()
127
128         def selectRight(self):
129                 self["input"].right()
130
131         def selectHome(self):
132                 self["input"].home()
133
134         def selectEnd(self):
135                 self["input"].end()
136
137         def deleteForward(self):
138                 self["input"].delete()
139
140         def deleteBackward(self):
141                 self["input"].deleteBackward()
142
143         def disableTimer(self):
144                 self.status = not self.status
145                 self.updateColors()
146
147         def toggleAction(self):
148                 if config.SleepTimer.action.value == "shutdown":
149                         config.SleepTimer.action.value = "standby"
150                 elif config.SleepTimer.action.value == "standby":
151                         config.SleepTimer.action.value = "shutdown"
152                 self.updateColors()
153
154         def toggleAsk(self):
155                 config.SleepTimer.ask.value = not config.SleepTimer.ask.value
156                 self.updateColors()
157                 
158         def useServiceTime(self):
159                 remaining = None
160                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
161                 if ref:
162                         path = ref.getPath()
163                         if path: # Movie
164                                 service = self.session.nav.getCurrentService()
165                                 seek = service and service.seek()
166                                 if seek:
167                                         length = seek.getLength()
168                                         position = seek.getPlayPosition()
169                                         if length and position:
170                                                 remaining = length[1] - position[1]
171                                                 if remaining > 0:
172                                                         remaining = remaining / 90000
173                         else: # DVB
174                                 epg = eEPGCache.getInstance()
175                                 event = epg.lookupEventTime(ref, -1, 0)
176                                 if event:
177                                         now = int(time())
178                                         start = event.getBeginTime()
179                                         duration = event.getDuration()
180                                         end = start + duration
181                                         remaining = end - now
182                 if remaining:
183                         config.SleepTimer.defaulttime.value = (remaining / 60) + 2
184                         self["input"].setText(str((remaining / 60) + 2))