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