PEP8: Fix whitespace
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / SleepTimerEdit.py
index ac50066..e02b335 100644 (file)
+from Screens.InfoBar import InfoBar
 from Screens.Screen import Screen
 from Screens.MessageBox import MessageBox
-from Components.ActionMap import NumberActionMap
-from Components.Input import Input
+from Components.ActionMap import ActionMap
+from Components.ConfigList import ConfigListScreen
 from Components.Label import Label
-from Components.Pixmap import Pixmap
-from Components.config import config
-from SleepTimer import SleepTimerEntry
-import time
+from Components.Sources.StaticText import StaticText
+from Components.config import config, getConfigListEntry
+from enigma import eEPGCache
+from time import time, localtime, mktime
 
-class SleepTimerEdit(Screen):
+class SleepTimerEdit(ConfigListScreen, Screen):
        def __init__(self, session):
                Screen.__init__(self, session)
-               
-               self["red"] = Pixmap()
-               self["green"] = Pixmap()
-               self["yellow"] = Pixmap()
-               self["blue"] = Pixmap()
-               self["red_text"] = Label()
-               self["green_text"] = Label()
-               self["yellow_text"] = Label()
-               self["blue_text"] = Label()
-               self.updateColors()
-               
-               self["pretext"] = Label(_("Shutdown Dreambox after"))
-               self["input"] = Input(text = str(self.session.nav.SleepTimer.getCurrentSleepTime()), maxSize = False, type = Input.NUMBER)
-               self["aftertext"] = Label(_("minutes"))
-               
-               self["actions"] = NumberActionMap(["SleepTimerEditorActions"], 
+               self.skinName = ["SleepTimerSetup", "Setup"]
+               self.setup_title = _("SleepTimer 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"],
                {
-                       "exit": self.close,
-                       "select": self.select,
-                       "1": self.keyNumberGlobal,
-                       "2": self.keyNumberGlobal,
-                       "3": self.keyNumberGlobal,
-                       "4": self.keyNumberGlobal,
-                       "5": self.keyNumberGlobal,
-                       "6": self.keyNumberGlobal,
-                       "7": self.keyNumberGlobal,
-                       "8": self.keyNumberGlobal,
-                       "9": self.keyNumberGlobal,
-                       "0": self.keyNumberGlobal,
-                       "selectLeft": self.selectLeft,
-                       "selectRight": self.selectRight,
-                       "disableTimer": self.disableTimer,
-                       "toggleAction": self.toggleAction,
-                       "toggleAsk": self.toggleAsk
-               }, -1)
-               
-       def updateColors(self):
-               if self.session.nav.SleepTimer.isActive():
-                       self["red_text"].setText(_("Timer status:") + " " + _("Enabled"))
-               else:
-                       self["red_text"].setText(_("Timer status:") + " " + _("Disabled"))
-               if config.SleepTimer.action.value == "shutdown":
-                       self["green_text"].setText(_("Sleep timer action:") + " " + _("Deep Standby"))
-               elif config.SleepTimer.action.value == "standby":
-                       self["green_text"].setText(_("Sleep timer action:") + " " + _("Standby"))
-
-               if config.SleepTimer.ask.value:
-                       self["yellow_text"].setText(_("Ask before shutdown:") + " " + _("yes"))
-               else:
-                       self["yellow_text"].setText(_("Ask before shutdown:") + " " + _("no"))
-               self["blue_text"].setText(_("Settings"))
-               
-               
-       def select(self):
-               self.session.nav.SleepTimer.setSleepTime(int(self["input"].getText()))
-               self.session.openWithCallback(self.close, MessageBox, _("The sleep timer has been acitvated."), MessageBox.TYPE_INFO)
-       
-       def keyNumberGlobal(self, number):
-               self["input"].number(number)
-               
-       def selectLeft(self):
-               self["input"].left()
-
-       def selectRight(self):
-               self["input"].right()
-       
-       def disableTimer(self):
-               if self.session.nav.SleepTimer.isActive():
-                       self.session.nav.SleepTimer.clear()
+                   "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 = []
+               if InfoBar.instance and InfoBar.instance.sleepTimer.isActive():
+                       statusSleeptimerText = _("(activated +%d min)") % InfoBar.instance.sleepTimerState()
                else:
-                       self.session.nav.SleepTimer.setSleepTime(int(self["input"].getText()))
-               self.updateColors()
-               
-       def toggleAction(self):
-               if config.SleepTimer.action.value == "shutdown":
-                       config.SleepTimer.action.value = "standby"
-               elif config.SleepTimer.action.value == "standby":
-                       config.SleepTimer.action.value = "shutdown"
-               self.updateColors()
-               
-       def toggleAsk(self):
-               config.SleepTimer.ask.value = not config.SleepTimer.ask.value
-               self.updateColors()
\ No newline at end of file
+                       statusSleeptimerText = _("(not activated)")
+               self.list.append(getConfigListEntry(_("Sleeptimer") + " " + statusSleeptimerText,
+                       config.usage.sleep_timer,
+                       _("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 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,
+                               _("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")))
+                       if config.usage.inactivity_timer_blocktime.value:
+                               self.list.append(getConfigListEntry(_("Start time to ignore inactivity sleeptimer"),
+                                       config.usage.inactivity_timer_blocktime_begin,
+                                       _("Specify the start time when the inactivity sleeptimer should be ignored")))
+                               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):
+               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_standby":
+                               sleepTimer = self.currentEventTime()
+                       else:
+                               sleepTimer = int(sleepTimer)
+                       if sleepTimer or not self.getCurrentEntry().endswith(_("(not activated)")):
+                               InfoBar.instance.setSleepTimer(sleepTimer)
+                       self.close(True)
+               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 currentEventTime(self):
+               remaining = 0
+               ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
+               if ref:
+                       path = ref.getPath()
+                       if path: # Movie
+                               service = self.session.nav.getCurrentService()
+                               seek = service and service.seek()
+                               if seek:
+                                       length = seek.getLength()
+                                       position = seek.getPlayPosition()
+                                       if length and position:
+                                               remaining = length[1] - position[1]
+                                               if remaining > 0:
+                                                       remaining = remaining / 90000
+                       else: # DVB
+                               epg = eEPGCache.getInstance()
+                               event = epg.lookupEventTime(ref, -1, 0)
+                               if event:
+                                       now = int(time())
+                                       start = event.getBeginTime()
+                                       duration = event.getDuration()
+                                       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