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