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