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