Merge branch 'master' of https://github.com/OpenPLi/enigma2
[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(_("Enable wakeup timer"),
87                         config.usage.wakeup_enabled,
88                         _("Note: when enabled, and you do want standby mode after wake up, set option 'Startup to Standby' as 'No, except Wakeup timer'.")))
89                 if config.usage.wakeup_enabled.value:
90                         for i in range(7):
91                                 self.list.append(getConfigListEntry([_("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday"), _("Sunday")][i],
92                                         config.usage.wakeup_day[i]))
93                                 if config.usage.wakeup_day[i].value:
94                                         self.list.append(getConfigListEntry(_("Wakeup time"),
95                                                 config.usage.wakeup_time[i]))
96                 self["config"].list = self.list
97                 self["config"].l.setList(self.list)
98
99         def ok(self):
100                 if self.getCurrentEntry() == _("Wakeup timer"):
101                         self.session.open(WakeupTimerEdit)
102                         return
103                 if self["config"].isChanged():
104                         for x in self["config"].list:
105                                 x[1].save()
106                 if self.getCurrentEntry().startswith(_("Sleeptimer")):
107                         sleepTimer = config.usage.sleep_timer.value
108                         if sleepTimer == "event_standby":
109                                 sleepTimer = self.currentEventTime()
110                         else:
111                                 sleepTimer = int(sleepTimer)
112                         if sleepTimer or not self.getCurrentEntry().endswith(_("(not activated)")):
113                                 InfoBar.instance.setSleepTimer(sleepTimer)
114                         self.close(True)
115                 self.close()
116
117         def cancel(self, answer = None):
118                 if answer is None:
119                         if self["config"].isChanged():
120                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
121                         else:
122                                 self.close()
123                 elif answer:
124                         for x in self["config"].list:
125                                 x[1].cancel()
126                         self.close()
127
128         def keyLeft(self):
129                 ConfigListScreen.keyLeft(self)
130                 self.createSetup()
131
132         def keyRight(self):
133                 ConfigListScreen.keyRight(self)
134                 self.createSetup()
135
136         def currentEventTime(self):
137                 remaining = 0
138                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
139                 if ref:
140                         path = ref.getPath()
141                         if path: # Movie
142                                 service = self.session.nav.getCurrentService()
143                                 seek = service and service.seek()
144                                 if seek:
145                                         length = seek.getLength()
146                                         position = seek.getPlayPosition()
147                                         if length and position:
148                                                 remaining = length[1] - position[1]
149                                                 if remaining > 0:
150                                                         remaining = remaining / 90000
151                         else: # DVB
152                                 epg = eEPGCache.getInstance()
153                                 event = epg.lookupEventTime(ref, -1, 0)
154                                 if event:
155                                         now = int(time())
156                                         start = event.getBeginTime()
157                                         duration = event.getDuration()
158                                         end = start + duration
159                                         remaining = end - now
160                 return remaining + config.recording.margin_after.value * 60
161
162 def isNextWakeupTime():
163         if config.usage.wakeup_enabled.value:
164                 wakeup_day, wakeup_time = WakeupDayTimeOfWeek()
165                 if wakeup_day == -1:
166                                 return -1
167                 elif wakeup_day == 0:
168                         return wakeup_time
169                 return wakeup_time + (86400 * wakeup_day)
170         return -1
171
172 def WakeupDayTimeOfWeek():
173         now = localtime()
174         current_day = int(now.tm_wday)
175         if current_day >= 0:
176                 if config.usage.wakeup_day[current_day].value:
177                         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)))
178                         if wakeup_time > time():
179                                 return 0, wakeup_time
180                 for i in range(1,8):
181                         if config.usage.wakeup_day[(current_day+i)%7].value:
182                                 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)))
183         return -1, None