fix bluescreen
[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 *
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                 if len(args):
236                         self.timer.service_ref = ServiceReference(args[0])
237                         config.timerentry.service.vals = (str(self.timer.service_ref.getServiceName()),)
238                         self["config"].invalidate(config.timerentry.service)
239
240         def keyNumberGlobal(self, number):
241                 print "You pressed number " + str(number)
242                 if (self["config"].getCurrent()[1].parent.enabled == True):
243                         self["config"].handleKey(config.key[str(number)])
244
245         def getTimestamp(self, date, mytime):
246                 d = time.localtime(date)
247                 dt = datetime.datetime(d.tm_year, d.tm_mon, d.tm_mday, mytime[0], mytime[1])
248                 return int(mktime(dt.timetuple()))
249
250         def keyGo(self):
251                 self.timer.name = config.timerentry.name.value
252                 self.timer.description = config.timerentry.description.value
253                 self.timer.justplay = (currentConfigSelectionElement(config.timerentry.justplay) == "zap")
254                 self.timer.resetRepeated()
255                 
256                 if (config.timerentry.type.value == 0): # once
257                         self.timer.begin = self.getTimestamp(config.timerentry.startdate.value, config.timerentry.starttime.value)
258                         self.timer.end = self.getTimestamp(config.timerentry.enddate.value, config.timerentry.endtime.value)
259                 if (config.timerentry.type.value == 1): # repeated
260                         if (config.timerentry.repeated.value == 0): # daily
261                                 for x in range(0,7):
262                                         self.timer.setRepeated(x)
263
264                         if (config.timerentry.repeated.value == 1): # weekly
265                                 self.timer.setRepeated(config.timerentry.weekday.value)
266                                 
267                         if (config.timerentry.repeated.value == 2): # Mon-Fri
268                                 for x in range(0,5):
269                                         self.timer.setRepeated(x)
270                                 
271                         if (config.timerentry.repeated.value == 3): # user defined
272                                 for x in range(0,7):
273                                         if (config.timerentry.day[x].value == 0): self.timer.setRepeated(x)
274
275                         self.timer.begin = self.getTimestamp(time.time(), config.timerentry.starttime.value)
276                         self.timer.end = self.getTimestamp(time.time(), config.timerentry.endtime.value)
277
278                 if self.timer.eit is not None:
279                         event = eEPGCache.getInstance().lookupEventId(self.timer.service_ref.ref, self.timer.eit)
280                         if event is not None:
281                                 if event.getNumOfLinkageServices() > 0:
282                                         self.session.openWithCallback(self.subserviceSelected, SubserviceSelection, event, self.timer.service_ref.ref)
283                                         return
284                 self.close((True, self.timer))
285
286         def subserviceSelected(self, service):
287                 if not service is None:
288                         self.timer.service_ref = ServiceReference(service)
289                 self.close((True, self.timer))
290
291         def keyCancel(self):
292                 self.close((False,))
293                 
294 class TimerLog(Screen):
295         def __init__(self, session, timer):
296                 Screen.__init__(self, session)
297                 self.timer = timer;
298                 self.log_entries = self.timer.log_entries[:]
299                 
300                 self.fillLogList()
301                 
302                 self["loglist"] = MenuList(self.list)
303                 self["logentry"] = Label()
304                 
305                 self["key_red"] = Button(_("Delete entry"))
306                 self["key_green"] = Button()
307                 self["key_yellow"] = Button("")
308                 self["key_blue"] = Button(_("Clear log"))
309                 
310                 self.onShown.append(self.updateText)
311
312                 self["actions"] = NumberActionMap(["OkCancelActions", "DirectionActions", "ColorActions"],
313                 {
314                         "ok": self.keyClose,
315                         "cancel": self.keyClose,
316                         "up": self.up,
317                         "down": self.down,
318                         "left": self.left,
319                         "right": self.right,
320                         "red": self.deleteEntry,
321                         "blue": self.clearLog
322                 }, -1)
323
324         def deleteEntry(self):
325                 self.log_entries.remove(self["loglist"].getCurrent()[1])
326                 self.fillLogList()
327                 self["loglist"].l.setList(self.list)
328                 self.updateText()
329
330         def fillLogList(self):
331                 self.list = [ ]
332                 for x in self.log_entries:
333                         self.list.append((str(strftime("%Y-%m-%d %H-%M", localtime(x[0])) + " - " + x[2]), x))
334         
335         def clearLog(self):             
336                 self.log_entries = []
337                 self.fillLogList()
338                 self["loglist"].l.setList(self.list)
339                 self.updateText()
340                 
341         def keyClose(self):
342                 if self.timer.log_entries != self.log_entries:
343                         self.timer.log_entries = self.log_entries
344                         self.close((True, self.timer))
345                 else:
346                         self.close((False,))
347                 
348         def up(self):
349                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveUp)
350                 self.updateText()
351                 
352         def down(self):
353                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveDown)
354                 self.updateText()
355
356         def left(self):
357                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageUp)
358                 self.updateText()
359                 
360         def right(self):
361                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageDown)
362                 self.updateText()
363
364         def updateText(self):
365                 if len(self.list) > 0:
366                         self["logentry"].setText(str(self["loglist"].getCurrent()[1][2]))
367                 else:
368                         self["logentry"].setText("")