add possibility to enable / disable ac3 downmix in audio track selection
[enigma2.git] / lib / python / Screens / TimerEntry.py
1 from Screen import Screen
2 from LocationBox import LocationBox
3 import ChannelSelection
4 from ServiceReference import ServiceReference
5 from Components.config import config, ConfigSelection, ConfigText, ConfigSubList, ConfigDateTime, ConfigClock, ConfigYesNo, getConfigListEntry
6 from Components.ActionMap import NumberActionMap
7 from Components.ConfigList import ConfigListScreen
8 from Components.MenuList import MenuList
9 from Components.Button import Button
10 from Components.Label import Label
11 from Components.Pixmap import Pixmap
12 from Screens.ChoiceBox import ChoiceBox
13 from RecordTimer import AFTEREVENT
14 from enigma import eEPGCache
15 import time
16 import datetime
17
18 class TimerEntry(Screen, ConfigListScreen):
19         def __init__(self, session, timer):
20                 Screen.__init__(self, session)
21                 self.timer = timer
22                 
23                 self.entryStartDate = None
24                 self.entryEndDate = None
25                 self.entryService = None
26                 
27                 self["oktext"] = Label(_("OK"))
28                 self["canceltext"] = Label(_("Cancel"))
29                 self["locationtext"] = Label(_("Location"))
30                 self["ok"] = Pixmap()
31                 self["cancel"] = Pixmap()
32                 self["location"] = Pixmap()
33
34                 self.createConfig()
35
36                 self["actions"] = NumberActionMap(["SetupActions", "ColorActions"],
37                 {
38                         "ok": self.keySelect,
39                         "save": self.keyGo,
40                         "cancel": self.keyCancel,
41                         "yellow": self.selectPath,
42                 }, -2)
43
44                 self.list = []
45                 ConfigListScreen.__init__(self, self.list, session = session)
46                 self.createSetup("config")
47
48                 self.onLayoutFinish.append(self.handleLocation)
49
50         def handleLocation(self):
51                 if config.usage.setup_level.index < 2: # -expert
52                         self["locationtext"].hide()
53                         self["location"].hide()
54
55         def createConfig(self):
56                         justplay = self.timer.justplay
57                                 
58                         afterevent = { AFTEREVENT.NONE: "nothing", AFTEREVENT.DEEPSTANDBY: "deepstandby", AFTEREVENT.STANDBY: "standby"}[self.timer.afterEvent]
59                         
60                         weekday_table = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
61
62                         # calculate default values
63                         day = []
64                         weekday = 0
65                         for x in range(0,7):
66                                 day.append(0)
67                         if self.timer.repeated: # repeated
68                                 type = "repeated"
69                                 if (self.timer.repeated == 31): # Mon-Fri
70                                         repeated = "weekdays"
71                                 elif (self.timer.repeated == 127): # daily
72                                         repeated = "daily"
73                                 else:
74                                         flags = self.timer.repeated
75                                         repeated = "user"
76                                         count = 0
77                                         for x in range(0, 7):
78                                                 if flags == 1: # weekly
79                                                         print "Set to weekday " + str(x)
80                                                         weekday = x
81                                                 if flags & 1 == 1: # set user defined flags
82                                                         day[x] = 1
83                                                         count += 1
84                                                 else:
85                                                         day[x] = 0
86                                                 flags = flags >> 1
87                                         if count == 1:
88                                                 repeated = "weekly"
89                         else: # once
90                                 type = "once"
91                                 repeated = None
92                                 weekday = (int(time.strftime("%w", time.localtime(self.timer.begin))) - 1) % 7
93                                 day[weekday] = 1
94                         
95                         self.timerentry_justplay = ConfigSelection(choices = [("zap", _("zap")), ("record", _("record"))], default = {0: "record", 1: "zap"}[justplay])
96                         self.timerentry_afterevent = ConfigSelection(choices = [("nothing", _("do nothing")), ("standby", _("go to standby")), ("deepstandby", _("go to deep standby"))], default = afterevent)
97                         self.timerentry_type = ConfigSelection(choices = [("once",_("once")), ("repeated", _("repeated"))], default = type)
98                         self.timerentry_name = ConfigText(default = self.timer.name, visible_width = 50, fixed_size = False)
99                         self.timerentry_description = ConfigText(default = self.timer.description, visible_width = 50, fixed_size = False)
100
101                         self.timerentry_repeated = ConfigSelection(default = repeated, choices = [("daily", _("daily")), ("weekly", _("weekly")), ("weekdays", _("Mon-Fri")), ("user", _("user defined"))])
102
103                         self.timerentry_startdate = ConfigDateTime(default = self.timer.begin, formatstring = _("%d.%B %Y"), increment = 86400)
104                         self.timerentry_starttime = ConfigClock(default = self.timer.begin)
105
106                         self.timerentry_enddate = ConfigDateTime(default = self.timer.end, formatstring =  _("%d.%B %Y"), increment = 86400)
107                         self.timerentry_endtime = ConfigClock(default = self.timer.end)
108
109                         self.timerentry_dirname = ConfigSelection(choices = [self.timer.dirname or "/hdd/movie/"])
110
111                         self.timerentry_repeatedbegindate = ConfigDateTime(default = self.timer.repeatedbegindate, formatstring = _("%d.%B %Y"), increment = 86400)
112
113                         self.timerentry_weekday = ConfigSelection(default = weekday_table[weekday], choices = [("mon",_("Monday")), ("tue", _("Tuesday")), ("wed",_("Wednesday")), ("thu", _("Thursday")), ("fri", _("Friday")), ("sat", _("Saturday")), ("sun", _("Sunday"))])
114
115                         self.timerentry_day = ConfigSubList()
116                         for x in range(0,7):
117                                 self.timerentry_day.append(ConfigYesNo(default = day[x]))
118
119                         # FIXME some service-chooser needed here
120                         servicename = "N/A"
121                         try: # no current service available?
122                                 servicename = str(self.timer.service_ref.getServiceName())
123                         except:
124                                 pass
125                         self.timerentry_service_ref = self.timer.service_ref
126                         self.timerentry_service = ConfigSelection([servicename])
127                         
128                         self.timerentry_startdate.addNotifier(self.checkDate)
129                         self.timerentry_enddate.addNotifier(self.checkDate)
130
131         def checkDate(self, configElement):
132                 if configElement is self.timerentry_startdate:
133                         if self.timerentry_enddate.value < self.timerentry_startdate.value:
134                                 self.timerentry_enddate.value = self.timerentry_startdate.value
135                                 self["config"].invalidate(self.entryEndDate)
136                 if configElement is self.timerentry_enddate:
137                         if (self.timerentry_enddate.value < self.timerentry_startdate.value):
138                                 self.timerentry_startdate.value = self.timerentry_enddate.value
139                                 self["config"].invalidate(self.entryStartDate)
140
141         def createSetup(self, widget):
142                 self.list = []
143                 self.list.append(getConfigListEntry(_("Name"), self.timerentry_name))
144                 self.list.append(getConfigListEntry(_("Description"), self.timerentry_description))
145                 self.timerJustplayEntry = getConfigListEntry(_("Timer Type"), self.timerentry_justplay)
146                 self.list.append(self.timerJustplayEntry)
147                 self.timerTypeEntry = getConfigListEntry(_("Repeat Type"), self.timerentry_type)
148                 self.list.append(self.timerTypeEntry)
149
150                 if self.timerentry_type.value == "once":
151                         self.frequencyEntry = None
152                 else: # repeated
153                         self.frequencyEntry = getConfigListEntry(_("Repeats"), self.timerentry_repeated)
154                         self.list.append(self.frequencyEntry)
155                         self.repeatedbegindateEntry = getConfigListEntry(_("Starting on"), self.timerentry_repeatedbegindate)
156                         self.list.append(self.repeatedbegindateEntry)
157                         if self.timerentry_repeated.value == "daily":
158                                 pass
159                         if self.timerentry_repeated.value == "weekdays":
160                                 pass
161                         if self.timerentry_repeated.value == "weekly":
162                                 self.list.append(getConfigListEntry(_("Weekday"), self.timerentry_weekday))
163
164                         if self.timerentry_repeated.value == "user":
165                                 self.list.append(getConfigListEntry(_("Monday"), self.timerentry_day[0]))
166                                 self.list.append(getConfigListEntry(_("Tuesday"), self.timerentry_day[1]))
167                                 self.list.append(getConfigListEntry(_("Wednesday"), self.timerentry_day[2]))
168                                 self.list.append(getConfigListEntry(_("Thursday"), self.timerentry_day[3]))
169                                 self.list.append(getConfigListEntry(_("Friday"), self.timerentry_day[4]))
170                                 self.list.append(getConfigListEntry(_("Saturday"), self.timerentry_day[5]))
171                                 self.list.append(getConfigListEntry(_("Sunday"), self.timerentry_day[6]))
172
173                         #self.list.append(getConfigListEntry("StartDate", self.timerentry_startdate))
174 #               self.list.append(getConfigListEntry("Weekday", self.timerentry_weekday))
175
176                 self.entryStartDate = getConfigListEntry(_("Start"), self.timerentry_startdate)
177                 if self.timerentry_type.value == "once":
178                         self.list.append(self.entryStartDate)
179                         self.list.append(getConfigListEntry(" ", self.timerentry_starttime))
180                 else:
181                         self.list.append(getConfigListEntry(_("StartTime"), self.timerentry_starttime))
182
183                 self.entryEndDate = getConfigListEntry(_("End"), self.timerentry_enddate)
184                 if self.timerentry_type.value == "once":
185                         if self.timerentry_justplay.value != "zap":
186                                 self.list.append(self.entryEndDate)
187                                 self.list.append(getConfigListEntry(" ", self.timerentry_endtime))
188                 else:
189                         if self.timerentry_justplay.value != "zap":
190                                 self.list.append(getConfigListEntry(_("EndTime"), self.timerentry_endtime))
191
192                 if self.timerentry_justplay.value != "zap":
193                         if config.usage.setup_level.index >= 2: # expert+
194                                 self.list.append(getConfigListEntry(_("Location"), self.timerentry_dirname))
195                         self.list.append(getConfigListEntry(_("After event"), self.timerentry_afterevent))
196
197                 self.channelEntry = getConfigListEntry(_("Channel"), self.timerentry_service)
198                 self.list.append(self.channelEntry)
199
200                 self[widget].list = self.list
201                 self[widget].l.setList(self.list)
202
203         def newConfig(self):
204                 print "newConfig", self["config"].getCurrent()
205                 if self["config"].getCurrent() == self.timerTypeEntry:
206                         self.createSetup("config")
207                 if self["config"].getCurrent() == self.timerJustplayEntry:
208                         self.createSetup("config")
209                 if self["config"].getCurrent() == self.frequencyEntry:
210                         self.createSetup("config")
211
212         def keyLeft(self):
213                 if self["config"].getCurrent() is self.channelEntry:
214                         self.keySelect()
215                 else:
216                         ConfigListScreen.keyLeft(self)
217                         self.newConfig()
218
219         def keyRight(self):
220                 if self["config"].getCurrent() is self.channelEntry:
221                         self.keySelect()
222                 else:
223                         ConfigListScreen.keyRight(self)
224                         self.newConfig()
225                 
226         def keySelect(self):
227                 if self["config"].getCurrent() == self.channelEntry:
228                         self.session.openWithCallback(self.finishedChannelSelection, ChannelSelection.SimpleChannelSelection, _("Select channel to record from"))
229                 else:
230                         self.keyGo()
231
232         def finishedChannelSelection(self, *args):
233                 if len(args):
234                         self.timerentry_service_ref = ServiceReference(args[0])
235                         self.timerentry_service.setCurrentText(self.timerentry_service_ref.getServiceName())
236                         self["config"].invalidate(self.channelEntry)
237
238         def getTimestamp(self, date, mytime):
239                 d = time.localtime(date)
240                 dt = datetime.datetime(d.tm_year, d.tm_mon, d.tm_mday, mytime[0], mytime[1])
241                 return int(time.mktime(dt.timetuple()))
242
243         def buildRepeatedBegin(self, rep_time, start_time):
244                 d = time.localtime(rep_time)
245                 dt = datetime.datetime(d.tm_year, d.tm_mon, d.tm_mday, start_time[0], start_time[1])
246                 return int(time.mktime(dt.timetuple()))
247
248         def getBeginEnd(self):
249                 enddate = self.timerentry_enddate.value
250                 endtime = self.timerentry_endtime.value
251                 
252                 startdate = self.timerentry_startdate.value
253                 starttime = self.timerentry_starttime.value
254                 
255                 begin = self.getTimestamp(startdate, starttime)
256                 end = self.getTimestamp(enddate, endtime)
257                 
258                 # because of the dateChecks, startdate can't be < enddate.
259                 # however, the endtime can be less than the starttime.
260                 # in this case, add 1 day.
261                 if end < begin:
262                         end += 86400
263                 return begin, end
264
265         def keyGo(self):
266                 self.timer.name = self.timerentry_name.value
267                 self.timer.description = self.timerentry_description.value
268                 self.timer.justplay = self.timerentry_justplay.value == "zap"
269                 self.timer.resetRepeated()
270                 self.timer.afterEvent = {"nothing": AFTEREVENT.NONE, "deepstandby": AFTEREVENT.DEEPSTANDBY, "standby": AFTEREVENT.STANDBY}[self.timerentry_afterevent.value]
271                 self.timer.service_ref = self.timerentry_service_ref
272
273                 # TODO: fix that thing with none (this might as well just be ignored)
274                 if self.timerentry_dirname.value == "/hdd/movie/":
275                         self.timer.dirname = None
276                 else:
277                         self.timer.dirname = self.timerentry_dirname.value
278
279                 if self.timerentry_type.value == "once":
280                         self.timer.begin, self.timer.end = self.getBeginEnd()
281                 if self.timerentry_type.value == "repeated":
282                         if self.timerentry_repeated.value == "daily":
283                                 for x in range(0,7):
284                                         self.timer.setRepeated(x)
285
286                         if self.timerentry_repeated.value == "weekly":
287                                 self.timer.setRepeated(self.timerentry_weekday.index)
288                                 
289                         if self.timerentry_repeated.value == "weekdays":
290                                 for x in range(0,5):
291                                         self.timer.setRepeated(x)
292                                 
293                         if self.timerentry_repeated.value == "user":
294                                 for x in range(0,7):
295                                         if self.timerentry_day[x].value:
296                                                 self.timer.setRepeated(x)
297
298                         self.timer.repeatedbegindate = self.buildRepeatedBegin(self.timerentry_repeatedbegindate.value, self.timerentry_starttime.value)
299                         self.timer.begin = self.getTimestamp(time.time(), self.timerentry_starttime.value)
300                         self.timer.end = self.getTimestamp(time.time(), self.timerentry_endtime.value)
301                         
302                         # when a timer end is set before the start, add 1 day
303                         if self.timer.end < self.timer.begin:
304                                 self.timer.end += 86400
305
306                 if self.timer.eit is not None:
307                         event = eEPGCache.getInstance().lookupEventId(self.timer.service_ref.ref, self.timer.eit)
308                         if event is not None:
309                                 n = event.getNumOfLinkageServices()
310                                 if n > 0:
311                                         tlist = []
312                                         ref = self.session.nav.getCurrentlyPlayingServiceReference()
313                                         parent = self.timer.service_ref.ref
314                                         selection = 0
315                                         for x in range(n):
316                                                 i = event.getLinkageService(parent, x)
317                                                 if i.toString() == ref.toString():
318                                                         selection = x
319                                                 tlist.append((i.getName(), i))
320                                         self.session.openWithCallback(self.subserviceSelected, ChoiceBox, title=_("Please select a subservice to record..."), list = tlist, selection = selection)
321                                         return
322
323                 self.saveTimer()
324                 self.close((True, self.timer))
325
326         def subserviceSelected(self, service):
327                 if not service is None:
328                         self.timer.service_ref = ServiceReference(service[1])
329                 self.saveTimer()
330                 self.close((True, self.timer))
331
332         def saveTimer(self):
333                 self.session.nav.RecordTimer.saveTimer()
334
335         def keyCancel(self):
336                 self.close((False,))
337
338         def selectPath(self):
339                 if config.usage.setup_level.index < 2: #-expert
340                         return
341                 self.session.openWithCallback(
342                         self.pathSelected,
343                         LocationBox,
344                         text = _("Choose target folder"),
345                         filename = "",
346                         currDir = None, # TODO: fix FileList to correctly determine mountpoint
347                         minFree = 100
348                 )
349
350         def pathSelected(self, res):
351                 if res is not None:
352                         self.timerentry_dirname.choices.append(res)
353                         self.timerentry_dirname.description[res] = res
354                         self.timerentry_dirname.value = res
355
356 class TimerLog(Screen):
357         def __init__(self, session, timer):
358                 Screen.__init__(self, session)
359                 self.timer = timer;
360                 self.log_entries = self.timer.log_entries[:]
361                 
362                 self.fillLogList()
363                 
364                 self["loglist"] = MenuList(self.list)
365                 self["logentry"] = Label()
366                 
367                 self["key_red"] = Button(_("Delete entry"))
368                 self["key_green"] = Button()
369                 self["key_yellow"] = Button("")
370                 self["key_blue"] = Button(_("Clear log"))
371                 
372                 self.onShown.append(self.updateText)
373
374                 self["actions"] = NumberActionMap(["OkCancelActions", "DirectionActions", "ColorActions"],
375                 {
376                         "ok": self.keyClose,
377                         "cancel": self.keyClose,
378                         "up": self.up,
379                         "down": self.down,
380                         "left": self.left,
381                         "right": self.right,
382                         "red": self.deleteEntry,
383                         "blue": self.clearLog
384                 }, -1)
385
386         def deleteEntry(self):
387                 cur = self["loglist"].getCurrent()
388                 if cur is None:
389                         return 
390                 self.log_entries.remove(cur[1])
391                 self.fillLogList()
392                 self["loglist"].l.setList(self.list)
393                 self.updateText()
394
395         def fillLogList(self):
396                 self.list = [ ]
397                 for x in self.log_entries:
398                         self.list.append((str(time.strftime("%Y-%m-%d %H-%M", time.localtime(x[0])) + " - " + x[2]), x))
399         
400         def clearLog(self):             
401                 self.log_entries = []
402                 self.fillLogList()
403                 self["loglist"].l.setList(self.list)
404                 self.updateText()
405                 
406         def keyClose(self):
407                 if self.timer.log_entries != self.log_entries:
408                         self.timer.log_entries = self.log_entries
409                         self.close((True, self.timer))
410                 else:
411                         self.close((False,))
412                 
413         def up(self):
414                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveUp)
415                 self.updateText()
416                 
417         def down(self):
418                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveDown)
419                 self.updateText()
420
421         def left(self):
422                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageUp)
423                 self.updateText()
424                 
425         def right(self):
426                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageDown)
427                 self.updateText()
428
429         def updateText(self):
430                 if len(self.list) > 0:
431                         self["logentry"].setText(str(self["loglist"].getCurrent()[1][2]))
432                 else:
433                         self["logentry"].setText("")