PEP8: Fix whitespace
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / SleepTimerEdit.py
index 24566fe..e02b335 100644 (file)
@@ -7,14 +7,13 @@ from Components.Label import Label
 from Components.Sources.StaticText import StaticText
 from Components.config import config, getConfigListEntry
 from enigma import eEPGCache
-from time import time
+from time import time, localtime, mktime
 
 class SleepTimerEdit(ConfigListScreen, Screen):
        def __init__(self, session):
                Screen.__init__(self, session)
                self.skinName = ["SleepTimerSetup", "Setup"]
                self.setup_title = _("SleepTimer Configuration")
-               self.onChangedEntry = [ ]
 
                self["key_red"] = StaticText(_("Cancel"))
                self["key_green"] = StaticText(_("Save"))
@@ -23,7 +22,7 @@ class SleepTimerEdit(ConfigListScreen, Screen):
                self.list = []
                ConfigListScreen.__init__(self, self.list, session = session)
                self.createSetup()
-               
+
                self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
                {
                    "green": self.ok,
@@ -39,12 +38,16 @@ class SleepTimerEdit(ConfigListScreen, Screen):
 
        def createSetup(self):
                self.list = []
-               self.list.append(getConfigListEntry(_("Sleeptimer"),
+               if InfoBar.instance and InfoBar.instance.sleepTimer.isActive():
+                       statusSleeptimerText = _("(activated +%d min)") % InfoBar.instance.sleepTimerState()
+               else:
+                       statusSleeptimerText = _("(not activated)")
+               self.list.append(getConfigListEntry(_("Sleeptimer") + " " + statusSleeptimerText,
                        config.usage.sleep_timer,
-                       _("Configure the duration in minutes and action, which could be shut down or standby, for the sleeptimer. Select this entry and click OK or green to start/stop the sleeptimer")))
+                       _("Configure the duration in minutes for the sleeptimer. Select this entry and click OK or green to start/stop the sleeptimer")))
                self.list.append(getConfigListEntry(_("Inactivity Sleeptimer"),
                        config.usage.inactivity_timer,
-                       _("Configure the duration in hours and action, which could be shut down or standby, when the receiver is not controlled.")))
+                       _("Configure the duration in hours the receiver should go to standby when the receiver is not controlled.")))
                if int(config.usage.inactivity_timer.value):
                        self.list.append(getConfigListEntry(_("Specify timeframe to ignore inactivity sleeptimer"),
                                config.usage.inactivity_timer_blocktime,
@@ -56,29 +59,51 @@ class SleepTimerEdit(ConfigListScreen, Screen):
                                self.list.append(getConfigListEntry(_("End time to ignore inactivity sleeptimer"),
                                        config.usage.inactivity_timer_blocktime_end,
                                        _("Specify the end time until the inactivity sleeptimer should be ignored")))
+                               self.list.append(getConfigListEntry(_("Specify extra timeframe to ignore inactivity sleeptimer"),
+                                       config.usage.inactivity_timer_blocktime_extra,
+                                       _("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")))
+                               if config.usage.inactivity_timer_blocktime_extra.value:
+                                       self.list.append(getConfigListEntry(_("Extra start time to ignore inactivity sleeptimer"),
+                                               config.usage.inactivity_timer_blocktime_extra_begin,
+                                               _("Specify the extra start time when the inactivity sleeptimer should be ignored")))
+                                       self.list.append(getConfigListEntry(_("Extra end time to ignore inactivity sleeptimer"),
+                                               config.usage.inactivity_timer_blocktime_extra_end,
+                                               _("Specify the extra end time until the inactivity sleeptimer should be ignored")))
                self.list.append(getConfigListEntry(_("Shutdown when in Standby"),
                        config.usage.standby_to_shutdown_timer,
                        _("Configure the duration when the receiver should go to shut down in case the receiver is in standby mode.")))
-
+               if int(config.usage.standby_to_shutdown_timer.value):
+                       self.list.append(getConfigListEntry(_("Specify timeframe to ignore the shutdown in standby"),
+                               config.usage.standby_to_shutdown_timer_blocktime,
+                               _("When enabled you can specify a timeframe to ignore the shutdown timer when the receiver is in standby mode")))
+                       if config.usage.standby_to_shutdown_timer_blocktime.value:
+                               self.list.append(getConfigListEntry(_("Start time to ignore shutdown in standby"),
+                                       config.usage.standby_to_shutdown_timer_blocktime_begin,
+                                       _("Specify the start time to ignore the shutdown timer when the receiver is in standby mode")))
+                               self.list.append(getConfigListEntry(_("End time to ignore shutdown in standby"),
+                                       config.usage.standby_to_shutdown_timer_blocktime_end,
+                                       _("Specify the end time to ignore the shutdown timer when the receiver is in standby mode")))
+               self.list.append(getConfigListEntry(_("Wakeup timer"),
+                       config.usage.wakeup_menu,
+                       _("Press OK and configure the days and times wakeup receiver from deep standby mode.")))
                self["config"].list = self.list
                self["config"].l.setList(self.list)
 
        def ok(self):
-               config.usage.sleep_timer.save()
-               config.usage.inactivity_timer.save()
-               config.usage.inactivity_timer_blocktime.save()
-               config.usage.inactivity_timer_blocktime_begin.save()
-               config.usage.inactivity_timer_blocktime_end.save()
-               config.usage.standby_to_shutdown_timer.save()
-               if self.getCurrentEntry() == _("Sleeptimer"):
+               if self.getCurrentEntry() == _("Wakeup timer"):
+                       self.session.open(WakeupTimerEdit)
+                       return
+               if self["config"].isChanged():
+                       for x in self["config"].list:
+                               x[1].save()
+               if self.getCurrentEntry().startswith(_("Sleeptimer")):
                        sleepTimer = config.usage.sleep_timer.value
-                       if sleepTimer == "event_shutdown":
-                               sleepTimer = -self.currentEventTime()
-                       elif sleepTimer == "event_standby":
+                       if sleepTimer == "event_standby":
                                sleepTimer = self.currentEventTime()
                        else:
                                sleepTimer = int(sleepTimer)
-                       InfoBar.instance.setSleepTimer(sleepTimer)
+                       if sleepTimer or not self.getCurrentEntry().endswith(_("(not activated)")):
+                               InfoBar.instance.setSleepTimer(sleepTimer)
                        self.close(True)
                self.close()
 
@@ -101,19 +126,6 @@ class SleepTimerEdit(ConfigListScreen, Screen):
                ConfigListScreen.keyRight(self)
                self.createSetup()
 
-       def getCurrentEntry(self):
-               return self["config"].getCurrent()[0]
-
-       def getCurrentValue(self):
-               return str(self["config"].getCurrent()[1].getText())
-
-       def getCurrentDescription(self):
-               return self["config"].getCurrent() and len(self["config"].getCurrent()) > 2 and self["config"].getCurrent()[2] or ""
-
-       def createSummary(self):
-               from Screens.Setup import SetupSummary
-               return SetupSummary
-
        def currentEventTime(self):
                remaining = 0
                ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
@@ -139,3 +151,101 @@ class SleepTimerEdit(ConfigListScreen, Screen):
                                        end = start + duration
                                        remaining = end - now
                return remaining + config.recording.margin_after.value * 60
+
+weekdays = [
+       _("Monday"),
+       _("Tuesday"),
+       _("Wednesday"),
+       _("Thursday"),
+       _("Friday"),
+       _("Saturday"),
+       _("Sunday"),
+       ]
+
+class WakeupTimerEdit(ConfigListScreen, Screen):
+       def __init__(self, session):
+               Screen.__init__(self, session)
+               self.skinName = ["WakeupTimerSetup", "Setup"]
+               self.setup_title = _("WakeupTimer Configuration")
+
+               self["key_red"] = StaticText(_("Cancel"))
+               self["key_green"] = StaticText(_("Save"))
+               self["description"] = Label("")
+
+               self.list = []
+               ConfigListScreen.__init__(self, self.list, session = session)
+               self.createSetup()
+
+               self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
+               {
+                   "green": self.ok,
+                   "red": self.cancel,
+                   "cancel": self.cancel,
+                   "ok": self.ok,
+               }, -2)
+
+               self.onLayoutFinish.append(self.layoutFinished)
+
+       def layoutFinished(self):
+               self.setTitle(self.setup_title)
+
+       def createSetup(self):
+               self.list = []
+               self.list.append(getConfigListEntry(_("Enable wakeup timer"),
+                       config.usage.wakeup_enabled,
+                       _("Note: when enabled, and you do want standby mode after wake up, set option 'Startup to Standby' as 'No, except Wakeup timer'.")))
+               if config.usage.wakeup_enabled.value:
+                       for i in range(7):
+                               self.list.append(getConfigListEntry(weekdays[i], config.usage.wakeup_day[i]))
+                               if config.usage.wakeup_day[i].value:
+                                       self.list.append(getConfigListEntry(_("Wakeup time"), config.usage.wakeup_time[i]))
+               self["config"].list = self.list
+               self["config"].l.setList(self.list)
+
+       def ok(self):
+               if self["config"].isChanged():
+                       for x in self["config"].list:
+                               x[1].save()
+               self.close()
+
+       def cancel(self, answer = None):
+               if answer is None:
+                       if self["config"].isChanged():
+                               self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
+                       else:
+                               self.close()
+               elif answer:
+                       for x in self["config"].list:
+                               x[1].cancel()
+                       self.close()
+
+       def keyLeft(self):
+               ConfigListScreen.keyLeft(self)
+               self.createSetup()
+
+       def keyRight(self):
+               ConfigListScreen.keyRight(self)
+               self.createSetup()
+
+def isNextWakeupTime():
+       if config.usage.wakeup_enabled.value:
+               wakeup_day, wakeup_time = WakeupDayTimeOfWeek()
+               if wakeup_day == -1:
+                               return -1
+               elif wakeup_day == 0:
+                       return wakeup_time
+               return wakeup_time + (86400 * wakeup_day)
+       return -1
+
+def WakeupDayTimeOfWeek():
+       now = localtime()
+       current_day = int(now.tm_wday)
+       if current_day >= 0:
+               if config.usage.wakeup_day[current_day].value:
+                       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)))
+                       if wakeup_time > time():
+                               return 0, wakeup_time
+               for i in range(1,8):
+                       if config.usage.wakeup_day[(current_day+i)%7].value:
+                               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)))
+       return -1, None