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