SleepTimerEdit: Make the abort handling correctly
[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 Tools.Notifications import AddPopup
10 from enigma import eEPGCache
11 from time import time
12
13 class SleepTimerEdit(ConfigListScreen, Screen):
14         def __init__(self, session):
15                 Screen.__init__(self, session)
16                 self.skinName = ["SleepTimerSetup", "Setup" ]
17                 self.setup_title = _("SleepTimer Configuration")
18                 self.onChangedEntry = [ ]
19
20                 self["key_red"] = StaticText(_("Cancel"))
21                 self["key_green"] = StaticText(_("Save"))
22                 self["description"] = Label("")
23
24                 self.list = []
25                 self.list.append(getConfigListEntry(_("Sleeptimer"),
26                         config.usage.sleep_timer,
27                         _("Configure the duration in minutes and action (shut down or standby) for the sleeptimer.")))
28                 self.list.append(getConfigListEntry(_("Action when receiver is not controlled"),
29                         config.usage.inactivity_timer,
30                         _("Configure the duration in hours and action (shut down or standby) when the receiver is not controlled.")))
31
32                 ConfigListScreen.__init__(self, self.list, session = session)
33                 
34                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
35                 {
36                     "green": self.ok,
37                     "red": self.cancel,
38                     "cancel": self.cancel,
39                     "ok": self.ok,
40                 }, -2)
41
42         def layoutFinished(self):
43                 self.setTitle(self.setup_title)
44
45         def ok(self):
46                 config.usage.sleep_timer.save()
47                 config.usage.inactivity_timer.save()
48                 if self.getCurrentEntry() == _("Sleeptimer"):
49                         sleepTimer = config.usage.sleep_timer.value
50                         if sleepTimer == "event_shutdown":
51                                 sleepTimer = -self.currentEventTime()
52                         elif sleepTimer == "event_standby":
53                                 sleepTimer = self.currentEventTime()
54                         else:
55                                 sleepTimer = int(sleepTimer)
56                         if sleepTimer:
57                                 if sleepTimer < 0:
58                                         message = _("And will shutdown your receiver over ")
59                                 else:
60                                         message = _("And will put your receiver in standby over ")
61                                 m = abs(sleepTimer / 60)
62                                 message = _("The sleep timer has been activated.") + "\n" + message + ngettext("%d minute", "%d minutes", m) % m
63                                 InfoBar.instance.setSleepTimer(sleepTimer)
64                         else:
65                                 message = _("The sleep timer has been disabled.")
66                                 InfoBar.instance.setSleepTimer(0)
67                         AddPopup(message, type = MessageBox.TYPE_INFO, timeout = 5)
68                         self.close(True)
69                 self.close()
70
71         def cancel(self, answer = None):
72                 if answer is None:
73                         if self["config"].isChanged():
74                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
75                         else:
76                                 answer = True
77                 elif answer:
78                         if self["config"].isChanged():
79                                 for x in self["config"].list:
80                                         x[1].cancel()
81                         self.close()
82
83         def getCurrentEntry(self):
84                 return self["config"].getCurrent()[0]
85
86         def getCurrentValue(self):
87                 return str(self["config"].getCurrent()[1].getText())
88
89         def getCurrentDescription(self):
90                 return self["config"].getCurrent() and len(self["config"].getCurrent()) > 2 and self["config"].getCurrent()[2] or ""
91
92         def createSummary(self):
93                 from Screens.Setup import SetupSummary
94                 return SetupSummary
95
96         def currentEventTime(self):
97                 remaining = 0
98                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
99                 if ref:
100                         path = ref.getPath()
101                         if path: # Movie
102                                 service = self.session.nav.getCurrentService()
103                                 seek = service and service.seek()
104                                 if seek:
105                                         length = seek.getLength()
106                                         position = seek.getPlayPosition()
107                                         if length and position:
108                                                 remaining = length[1] - position[1]
109                                                 if remaining > 0:
110                                                         remaining = remaining / 90000
111                         else: # DVB
112                                 epg = eEPGCache.getInstance()
113                                 event = epg.lookupEventTime(ref, -1, 0)
114                                 if event:
115                                         now = int(time())
116                                         start = event.getBeginTime()
117                                         duration = event.getDuration()
118                                         end = start + duration
119                                         remaining = end - now
120                 return remaining + config.recording.margin_after.value * 60