PEP8: Fix whitespace
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / SleepTimerEdit.py
1 from Screens.InfoBar import InfoBar
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import ActionMap
5 from Components.ConfigList import ConfigListScreen
6 from Components.Label import Label
7 from Components.Sources.StaticText import StaticText
8 from Components.config import config, getConfigListEntry
9 from enigma import eEPGCache
10 from time import time, localtime, mktime
11
12 class SleepTimerEdit(ConfigListScreen, Screen):
13         def __init__(self, session):
14                 Screen.__init__(self, session)
15                 self.skinName = ["SleepTimerSetup", "Setup"]
16                 self.setup_title = _("SleepTimer Configuration")
17
18                 self["key_red"] = StaticText(_("Cancel"))
19                 self["key_green"] = StaticText(_("Save"))
20                 self["description"] = Label("")
21
22                 self.list = []
23                 ConfigListScreen.__init__(self, self.list, session = session)
24                 self.createSetup()
25
26                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
27                 {
28                     "green": self.ok,
29                     "red": self.cancel,
30                     "cancel": self.cancel,
31                     "ok": self.ok,
32                 }, -2)
33
34                 self.onLayoutFinish.append(self.layoutFinished)
35
36         def layoutFinished(self):
37                 self.setTitle(self.setup_title)
38
39         def createSetup(self):
40                 self.list = []
41                 if InfoBar.instance and InfoBar.instance.sleepTimer.isActive():
42                         statusSleeptimerText = _("(activated +%d min)") % InfoBar.instance.sleepTimerState()
43                 else:
44                         statusSleeptimerText = _("(not activated)")
45                 self.list.append(getConfigListEntry(_("Sleeptimer") + " " + statusSleeptimerText,
46                         config.usage.sleep_timer,
47                         _("Configure the duration in minutes for the sleeptimer. Select this entry and click OK or green to start/stop the sleeptimer")))
48                 self.list.append(getConfigListEntry(_("Inactivity Sleeptimer"),
49                         config.usage.inactivity_timer,
50                         _("Configure the duration in hours the receiver should go to standby when the receiver is not controlled.")))
51                 if int(config.usage.inactivity_timer.value):
52                         self.list.append(getConfigListEntry(_("Specify timeframe to ignore inactivity sleeptimer"),
53                                 config.usage.inactivity_timer_blocktime,
54                                 _("When enabled you can specify a timeframe were the inactivity sleeptimer is ignored. Not the detection is disabled during this timeframe but the inactivity timeout is disabled")))
55                         if config.usage.inactivity_timer_blocktime.value:
56                                 self.list.append(getConfigListEntry(_("Start time to ignore inactivity sleeptimer"),
57                                         config.usage.inactivity_timer_blocktime_begin,
58                                         _("Specify the start time when the inactivity sleeptimer should be ignored")))
59                                 self.list.append(getConfigListEntry(_("End time to ignore inactivity sleeptimer"),
60                                         config.usage.inactivity_timer_blocktime_end,
61                                         _("Specify the end time until the inactivity sleeptimer should be ignored")))
62                                 self.list.append(getConfigListEntry(_("Specify extra timeframe to ignore inactivity sleeptimer"),
63                                         config.usage.inactivity_timer_blocktime_extra,
64                                         _("When enabled you can specify an extra timeframe were the inactivity sleeptimer is ignored. Not the detection is disabled during this timeframe but the inactivity timeout is disabled")))
65                                 if config.usage.inactivity_timer_blocktime_extra.value:
66                                         self.list.append(getConfigListEntry(_("Extra start time to ignore inactivity sleeptimer"),
67                                                 config.usage.inactivity_timer_blocktime_extra_begin,
68                                                 _("Specify the extra start time when the inactivity sleeptimer should be ignored")))
69                                         self.list.append(getConfigListEntry(_("Extra end time to ignore inactivity sleeptimer"),
70                                                 config.usage.inactivity_timer_blocktime_extra_end,
71                                                 _("Specify the extra end time until the inactivity sleeptimer should be ignored")))
72                 self.list.append(getConfigListEntry(_("Shutdown when in Standby"),
73                         config.usage.standby_to_shutdown_timer,
74                         _("Configure the duration when the receiver should go to shut down in case the receiver is in standby mode.")))
75                 if int(config.usage.standby_to_shutdown_timer.value):
76                         self.list.append(getConfigListEntry(_("Specify timeframe to ignore the shutdown in standby"),
77                                 config.usage.standby_to_shutdown_timer_blocktime,
78                                 _("When enabled you can specify a timeframe to ignore the shutdown timer when the receiver is in standby mode")))
79                         if config.usage.standby_to_shutdown_timer_blocktime.value:
80                                 self.list.append(getConfigListEntry(_("Start time to ignore shutdown in standby"),
81                                         config.usage.standby_to_shutdown_timer_blocktime_begin,
82                                         _("Specify the start time to ignore the shutdown timer when the receiver is in standby mode")))
83                                 self.list.append(getConfigListEntry(_("End time to ignore shutdown in standby"),
84                                         config.usage.standby_to_shutdown_timer_blocktime_end,
85                                         _("Specify the end time to ignore the shutdown timer when the receiver is in standby mode")))
86                 self.list.append(getConfigListEntry(_("Wakeup timer"),
87                         config.usage.wakeup_menu,
88                         _("Press OK and configure the days and times wakeup receiver from deep standby mode.")))
89                 self["config"].list = self.list
90                 self["config"].l.setList(self.list)
91
92         def ok(self):
93                 if self.getCurrentEntry() == _("Wakeup timer"):
94                         self.session.open(WakeupTimerEdit)
95                         return
96                 if self["config"].isChanged():
97                         for x in self["config"].list:
98                                 x[1].save()
99                 if self.getCurrentEntry().startswith(_("Sleeptimer")):
100                         sleepTimer = config.usage.sleep_timer.value
101                         if sleepTimer == "event_standby":
102                                 sleepTimer = self.currentEventTime()
103                         else:
104                                 sleepTimer = int(sleepTimer)
105                         if sleepTimer or not self.getCurrentEntry().endswith(_("(not activated)")):
106                                 InfoBar.instance.setSleepTimer(sleepTimer)
107                         self.close(True)
108                 self.close()
109
110         def cancel(self, answer = None):
111                 if answer is None:
112                         if self["config"].isChanged():
113                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
114                         else:
115                                 self.close()
116                 elif answer:
117                         for x in self["config"].list:
118                                 x[1].cancel()
119                         self.close()
120
121         def keyLeft(self):
122                 ConfigListScreen.keyLeft(self)
123                 self.createSetup()
124
125         def keyRight(self):
126                 ConfigListScreen.keyRight(self)
127                 self.createSetup()
128
129         def currentEventTime(self):
130                 remaining = 0
131                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
132                 if ref:
133                         path = ref.getPath()
134                         if path: # Movie
135                                 service = self.session.nav.getCurrentService()
136                                 seek = service and service.seek()
137                                 if seek:
138                                         length = seek.getLength()
139                                         position = seek.getPlayPosition()
140                                         if length and position:
141                                                 remaining = length[1] - position[1]
142                                                 if remaining > 0:
143                                                         remaining = remaining / 90000
144                         else: # DVB
145                                 epg = eEPGCache.getInstance()
146                                 event = epg.lookupEventTime(ref, -1, 0)
147                                 if event:
148                                         now = int(time())
149                                         start = event.getBeginTime()
150                                         duration = event.getDuration()
151                                         end = start + duration
152                                         remaining = end - now
153                 return remaining + config.recording.margin_after.value * 60
154
155 weekdays = [
156         _("Monday"),
157         _("Tuesday"),
158         _("Wednesday"),
159         _("Thursday"),
160         _("Friday"),
161         _("Saturday"),
162         _("Sunday"),
163         ]
164
165 class WakeupTimerEdit(ConfigListScreen, Screen):
166         def __init__(self, session):
167                 Screen.__init__(self, session)
168                 self.skinName = ["WakeupTimerSetup", "Setup"]
169                 self.setup_title = _("WakeupTimer Configuration")
170
171                 self["key_red"] = StaticText(_("Cancel"))
172                 self["key_green"] = StaticText(_("Save"))
173                 self["description"] = Label("")
174
175                 self.list = []
176                 ConfigListScreen.__init__(self, self.list, session = session)
177                 self.createSetup()
178
179                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
180                 {
181                     "green": self.ok,
182                     "red": self.cancel,
183                     "cancel": self.cancel,
184                     "ok": self.ok,
185                 }, -2)
186
187                 self.onLayoutFinish.append(self.layoutFinished)
188
189         def layoutFinished(self):
190                 self.setTitle(self.setup_title)
191
192         def createSetup(self):
193                 self.list = []
194                 self.list.append(getConfigListEntry(_("Enable wakeup timer"),
195                         config.usage.wakeup_enabled,
196                         _("Note: when enabled, and you do want standby mode after wake up, set option 'Startup to Standby' as 'No, except Wakeup timer'.")))
197                 if config.usage.wakeup_enabled.value:
198                         for i in range(7):
199                                 self.list.append(getConfigListEntry(weekdays[i], config.usage.wakeup_day[i]))
200                                 if config.usage.wakeup_day[i].value:
201                                         self.list.append(getConfigListEntry(_("Wakeup time"), config.usage.wakeup_time[i]))
202                 self["config"].list = self.list
203                 self["config"].l.setList(self.list)
204
205         def ok(self):
206                 if self["config"].isChanged():
207                         for x in self["config"].list:
208                                 x[1].save()
209                 self.close()
210
211         def cancel(self, answer = None):
212                 if answer is None:
213                         if self["config"].isChanged():
214                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
215                         else:
216                                 self.close()
217                 elif answer:
218                         for x in self["config"].list:
219                                 x[1].cancel()
220                         self.close()
221
222         def keyLeft(self):
223                 ConfigListScreen.keyLeft(self)
224                 self.createSetup()
225
226         def keyRight(self):
227                 ConfigListScreen.keyRight(self)
228                 self.createSetup()
229
230 def isNextWakeupTime():
231         if config.usage.wakeup_enabled.value:
232                 wakeup_day, wakeup_time = WakeupDayTimeOfWeek()
233                 if wakeup_day == -1:
234                                 return -1
235                 elif wakeup_day == 0:
236                         return wakeup_time
237                 return wakeup_time + (86400 * wakeup_day)
238         return -1
239
240 def WakeupDayTimeOfWeek():
241         now = localtime()
242         current_day = int(now.tm_wday)
243         if current_day >= 0:
244                 if config.usage.wakeup_day[current_day].value:
245                         wakeup_time = int(mktime((now.tm_year, now.tm_mon, now.tm_mday, config.usage.wakeup_time[current_day].value[0], config.usage.wakeup_time[current_day].value[1], 0, now.tm_wday, now.tm_yday, now.tm_isdst)))
246                         if wakeup_time > time():
247                                 return 0, wakeup_time
248                 for i in range(1,8):
249                         if config.usage.wakeup_day[(current_day+i)%7].value:
250                                 return i, int(mktime((now.tm_year, now.tm_mon, now.tm_mday, config.usage.wakeup_time[(current_day+i)%7].value[0], config.usage.wakeup_time[(current_day+i)%7].value[1], 0, now.tm_wday, now.tm_yday, now.tm_isdst)))
251         return -1, None