fix bug described here -> http://www.dream-multimedia-tv.de/board/thread.php?threadid...
[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, fixed_size = False)
88                         self.timerentry_description = ConfigText(default = self.timer.description, 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 = ConfigSelection([servicename])
113                         
114                         self.timerentry_startdate.addNotifier(self.checkDate)
115                         self.timerentry_enddate.addNotifier(self.checkDate)
116
117         def checkDate(self, configElement):
118                 if configElement is self.timerentry_startdate:
119                         if self.timerentry_enddate.value < self.timerentry_startdate.value:
120                                 self.timerentry_enddate.value = self.timerentry_startdate.value
121                                 self["config"].invalidate(self.entryEndDate)
122                 if configElement is self.timerentry_enddate:
123                         if (self.timerentry_enddate.value < self.timerentry_startdate.value):
124                                 self.timerentry_startdate.value = self.timerentry_enddate.value
125                                 self["config"].invalidate(self.entryStartDate)
126
127         def createSetup(self, widget):
128                 self.list = []
129                 self.list.append(getConfigListEntry(_("Name"), self.timerentry_name))
130                 self.list.append(getConfigListEntry(_("Description"), self.timerentry_description))
131                 self.timerJustplayEntry = getConfigListEntry(_("Timer Type"), self.timerentry_justplay)
132                 self.list.append(self.timerJustplayEntry)
133                 self.timerTypeEntry = getConfigListEntry(_("Repeat Type"), self.timerentry_type)
134                 self.list.append(self.timerTypeEntry)
135
136                 if self.timerentry_type.value == "once":
137                         self.frequencyEntry = None
138                 else: # repeated
139                         self.frequencyEntry = getConfigListEntry(_("Frequency"), self.timerentry_repeated)
140                         self.list.append(self.frequencyEntry)
141                         self.repeatedbegindateEntry = getConfigListEntry(_("Starting on"), self.timerentry_repeatedbegindate)
142                         self.list.append(self.repeatedbegindateEntry)
143                         if self.timerentry_repeated.value == "daily":
144                                 pass
145                         if self.timerentry_repeated.value == "weekdays":
146                                 pass
147                         if self.timerentry_repeated.value == "weekly":
148                                 self.list.append(getConfigListEntry(_("Weekday"), self.timerentry_weekday))
149
150                         if self.timerentry_repeated.value == "user":
151                                 self.list.append(getConfigListEntry(_("Monday"), self.timerentry_day[0]))
152                                 self.list.append(getConfigListEntry(_("Tuesday"), self.timerentry_day[1]))
153                                 self.list.append(getConfigListEntry(_("Wednesday"), self.timerentry_day[2]))
154                                 self.list.append(getConfigListEntry(_("Thursday"), self.timerentry_day[3]))
155                                 self.list.append(getConfigListEntry(_("Friday"), self.timerentry_day[4]))
156                                 self.list.append(getConfigListEntry(_("Saturday"), self.timerentry_day[5]))
157                                 self.list.append(getConfigListEntry(_("Sunday"), self.timerentry_day[6]))
158
159                         #self.list.append(getConfigListEntry("StartDate", self.timerentry_startdate))
160 #               self.list.append(getConfigListEntry("Weekday", self.timerentry_weekday))
161
162                 self.entryStartDate = getConfigListEntry(_("Start"), self.timerentry_startdate)
163                 if self.timerentry_type.value == "once":
164                         self.list.append(self.entryStartDate)
165                         self.list.append(getConfigListEntry(" ", self.timerentry_starttime))
166                 else:
167                         self.list.append(getConfigListEntry(_("StartTime"), self.timerentry_starttime))
168
169                 self.entryEndDate = getConfigListEntry(_("End"), self.timerentry_enddate)
170                 if self.timerentry_type.value == "once":
171                         if self.timerentry_justplay.value != "zap":
172                                 self.list.append(self.entryEndDate)
173                                 self.list.append(getConfigListEntry(" ", self.timerentry_endtime))
174                 else:
175                         if self.timerentry_justplay.value != "zap":
176                                 self.list.append(getConfigListEntry(_("EndTime"), self.timerentry_endtime))
177
178                 if self.timerentry_justplay.value != "zap":
179                         self.list.append(getConfigListEntry(_("After event"), self.timerentry_afterevent))
180
181                 self.channelEntry = getConfigListEntry(_("Channel"), self.timerentry_service)
182                 self.list.append(self.channelEntry)
183
184                 self[widget].list = self.list
185                 self[widget].l.setList(self.list)
186
187         def newConfig(self):
188                 print "newConfig", self["config"].getCurrent()
189                 if self["config"].getCurrent() == self.timerTypeEntry:
190                         self.createSetup("config")
191                 if self["config"].getCurrent() == self.timerJustplayEntry:
192                         self.createSetup("config")
193                 if self["config"].getCurrent() == self.frequencyEntry:
194                         self.createSetup("config")
195
196         def keyLeft(self):
197                 if self["config"].getCurrent() is self.channelEntry:
198                         self.keySelect()
199                 else:
200                         ConfigListScreen.keyLeft(self)
201                         self.newConfig()
202
203         def keyRight(self):
204                 if self["config"].getCurrent() is self.channelEntry:
205                         self.keySelect()
206                 else:
207                         ConfigListScreen.keyRight(self)
208                         self.newConfig()
209                 
210         def keySelect(self):
211                 if self["config"].getCurrent() == self.channelEntry:
212                         self.session.openWithCallback(self.finishedChannelSelection, ChannelSelection.SimpleChannelSelection, _("Select channel to record from"))
213                 else:
214                         self.keyGo()
215
216         def finishedChannelSelection(self, *args):
217                 if len(args):
218                         self.timer.service_ref = ServiceReference(args[0])
219                         self.timerentry_service.setCurrentText(self.timer.service_ref.getServiceName())
220                         self["config"].invalidate(self.channelEntry)
221
222         def getTimestamp(self, date, mytime):
223                 d = time.localtime(date)
224                 dt = datetime.datetime(d.tm_year, d.tm_mon, d.tm_mday, mytime[0], mytime[1])
225                 return int(time.mktime(dt.timetuple()))
226
227         def buildRepeatedBegin(self, rep_time, start_time):
228                 d = time.localtime(rep_time)
229                 dt = datetime.datetime(d.tm_year, d.tm_mon, d.tm_mday, start_time[0], start_time[1])
230                 return int(time.mktime(dt.timetuple()))
231
232         def getBeginEnd(self):
233                 enddate = self.timerentry_enddate.value
234                 endtime = self.timerentry_endtime.value
235                 
236                 startdate = self.timerentry_startdate.value
237                 starttime = self.timerentry_starttime.value
238                 
239                 begin = self.getTimestamp(startdate, starttime)
240                 end = self.getTimestamp(enddate, endtime)
241                 
242                 # because of the dateChecks, startdate can't be < enddate.
243                 # however, the endtime can be less than the starttime.
244                 # in this case, add 1 day.
245                 if end < begin:
246                         end += 86400
247                 return begin, end
248
249         def keyGo(self):
250                 self.timer.name = self.timerentry_name.value
251                 self.timer.description = self.timerentry_description.value
252                 self.timer.justplay = self.timerentry_justplay.value == "zap"
253                 self.timer.resetRepeated()
254                 self.timer.afterEvent = {"nothing": AFTEREVENT.NONE, "deepstandby": AFTEREVENT.DEEPSTANDBY, "standby": AFTEREVENT.STANDBY}[self.timerentry_afterevent.value]
255                 
256                 if self.timerentry_type.value == "once":
257                         self.timer.begin, self.timer.end = self.getBeginEnd()
258                 if self.timerentry_type.value == "repeated":
259                         if self.timerentry_repeated.value == "daily":
260                                 for x in range(0,7):
261                                         self.timer.setRepeated(x)
262
263                         if self.timerentry_repeated.value == "weekly":
264                                 self.timer.setRepeated(self.timerentry_weekday.index)
265                                 
266                         if self.timerentry_repeated.value == "weekdays":
267                                 for x in range(0,5):
268                                         self.timer.setRepeated(x)
269                                 
270                         if self.timerentry_repeated.value == "user":
271                                 for x in range(0,7):
272                                         if self.timerentry_day[x].value:
273                                                 self.timer.setRepeated(x)
274
275                         self.timer.repeatedbegindate = self.buildRepeatedBegin(self.timerentry_repeatedbegindate.value, self.timerentry_starttime.value)
276                         self.timer.begin = self.getTimestamp(time.time(), self.timerentry_starttime.value)
277                         self.timer.end = self.getTimestamp(time.time(), self.timerentry_endtime.value)
278                         
279                         # when a timer end is set before the start, add 1 day
280                         if self.timer.end < self.timer.begin:
281                                 self.timer.end += 86400
282
283                 if self.timer.eit is not None:
284                         event = eEPGCache.getInstance().lookupEventId(self.timer.service_ref.ref, self.timer.eit)
285                         if event is not None:
286                                 n = event.getNumOfLinkageServices()
287                                 if n > 0:
288                                         tlist = []
289                                         ref = self.session.nav.getCurrentlyPlayingServiceReference()
290                                         parent = self.timer.service_ref.ref
291                                         selection = 0
292                                         for x in range(n):
293                                                 i = event.getLinkageService(parent, x)
294                                                 if i.toString() == ref.toString():
295                                                         selection = x
296                                                 tlist.append((i.getName(), i))
297                                         self.session.openWithCallback(self.subserviceSelected, ChoiceBox, title=_("Please select a subservice to record..."), list = tlist, selection = selection)
298                                         return
299
300                 self.saveTimer()
301                 self.close((True, self.timer))
302
303         def subserviceSelected(self, service):
304                 if not service is None:
305                         self.timer.service_ref = ServiceReference(service[1])
306                 self.saveTimer()
307                 self.close((True, self.timer))
308
309         def saveTimer(self):
310                 self.session.nav.RecordTimer.saveTimer()
311
312         def keyCancel(self):
313                 self.close((False,))
314                 
315 class TimerLog(Screen):
316         def __init__(self, session, timer):
317                 Screen.__init__(self, session)
318                 self.timer = timer;
319                 self.log_entries = self.timer.log_entries[:]
320                 
321                 self.fillLogList()
322                 
323                 self["loglist"] = MenuList(self.list)
324                 self["logentry"] = Label()
325                 
326                 self["key_red"] = Button(_("Delete entry"))
327                 self["key_green"] = Button()
328                 self["key_yellow"] = Button("")
329                 self["key_blue"] = Button(_("Clear log"))
330                 
331                 self.onShown.append(self.updateText)
332
333                 self["actions"] = NumberActionMap(["OkCancelActions", "DirectionActions", "ColorActions"],
334                 {
335                         "ok": self.keyClose,
336                         "cancel": self.keyClose,
337                         "up": self.up,
338                         "down": self.down,
339                         "left": self.left,
340                         "right": self.right,
341                         "red": self.deleteEntry,
342                         "blue": self.clearLog
343                 }, -1)
344
345         def deleteEntry(self):
346                 self.log_entries.remove(self["loglist"].getCurrent()[1])
347                 self.fillLogList()
348                 self["loglist"].l.setList(self.list)
349                 self.updateText()
350
351         def fillLogList(self):
352                 self.list = [ ]
353                 for x in self.log_entries:
354                         self.list.append((str(time.strftime("%Y-%m-%d %H-%M", time.localtime(x[0])) + " - " + x[2]), x))
355         
356         def clearLog(self):             
357                 self.log_entries = []
358                 self.fillLogList()
359                 self["loglist"].l.setList(self.list)
360                 self.updateText()
361                 
362         def keyClose(self):
363                 if self.timer.log_entries != self.log_entries:
364                         self.timer.log_entries = self.log_entries
365                         self.close((True, self.timer))
366                 else:
367                         self.close((False,))
368                 
369         def up(self):
370                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveUp)
371                 self.updateText()
372                 
373         def down(self):
374                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveDown)
375                 self.updateText()
376
377         def left(self):
378                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageUp)
379                 self.updateText()
380                 
381         def right(self):
382                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageDown)
383                 self.updateText()
384
385         def updateText(self):
386                 if len(self.list) > 0:
387                         self["logentry"].setText(str(self["loglist"].getCurrent()[1][2]))
388                 else:
389                         self["logentry"].setText("")