fix AfterEvent "Auto" and make it the default Event
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / Timer.py
1 Version = '$Header$';
2
3 from enigma import eServiceReference, eEPGCache
4 from Components.Sources.Source import Source
5 from Components.config import config
6 from ServiceReference import ServiceReference
7 from RecordTimer import RecordTimerEntry, RecordTimer, AFTEREVENT, parseEvent
8 from Components.config import config
9 from xml.sax.saxutils import unescape
10 from time import time, strftime, localtime, mktime
11 from string import split
12
13 class Timer( Source):
14     LIST = 0
15     ADDBYID = 1
16     ADD = 2
17     DEL = 3
18     TVBROWSER = 4
19     CHANGE = 5
20     WRITE = 6
21     RECNOW = 7
22     
23     def __init__(self, session, func = LIST):
24         self.func = func
25         Source.__init__(self)        
26         self.session = session
27         self.recordtimer = session.nav.RecordTimer
28         self.epgcache = eEPGCache.getInstance()
29         self.result = False,"unknown command"
30
31     def handleCommand(self,cmd):
32         if self.func is self.ADDBYID:
33             self.result = self.addTimerByEventID(cmd)
34             self.writeTimerList()
35         elif self.func is self.ADD:
36             self.result = self.addTimer(cmd)
37             self.writeTimerList()
38         elif self.func is self.TVBROWSER:
39             self.result = self.tvBrowser(cmd)
40             self.writeTimerList()
41         elif self.func is self.DEL:
42             self.result = self.delTimer(cmd)
43             self.writeTimerList()
44         elif self.func is self.CHANGE:
45             self.result = self.changeTimer(cmd)
46             self.writeTimerList()
47         elif self.func is self.WRITE:
48             self.result = self.writeTimerList(force=True)
49         elif self.func is self.RECNOW:
50             print "RECNOW"
51             self.result = self.recordNow(cmd)
52         else:
53             self.result = False,"unknown command cmd(%s) self.func(%s)" % (cmd, self.func)
54
55     def delTimer(self,param):
56         # is there an easier and better way? :\ 
57         print "delTimer",param
58         
59         if param['sRef'] is None:
60             return False,"ServiceReference missing"
61         else: 
62             serviceref = ServiceReference(param['sRef'])
63         
64         if param['begin'] is None:
65            return False,"begin missing"
66         else:
67             begin = float(param['begin'])
68         
69         if param['end'] is None:
70             return False,"end missing"
71         else:
72                 end = float(param['end'])
73              
74         toDelete = None
75         try:
76             print "timer_list ", self.recordtimer.timer_list
77             print "processed_timers", self.recordtimer.processed_timers
78             for x in self.recordtimer.timer_list + self.recordtimer.processed_timers:
79                 #print "x.begin(%s), x.end(%s), x.service_ref(%s)" % (x.begin, x.end, x.service_ref)
80                 if str(x.service_ref) == str(serviceref) and float(x.begin) == begin and float(x.end) == end:
81                                   toDelete = x
82         except:
83             print "Fehler\n"
84             
85         if toDelete is not None:
86                 self.recordtimer.removeEntry(toDelete)
87                 return True,"Timer removed"
88         else:
89                 return False,"Timer not found"
90                 print "Timer not found"
91         
92         #self.session.nav.RecordTimer.saveTimer()
93     
94     def tvBrowser(self,param):
95         print "tvbrowser",param
96         
97         """ Therefor the URL's for the tvBrowser-Capture-Driver are:
98         
99             http://dreambox/web/tvbrowser? +
100             
101         To add something:
102             &command=add&&syear={start_year}&smonth={start_month}&sday={start_day}&shour={start_hour}&smin={start_minute}&eyear={end_year}&emonth={end_month}&eday={end_day}&ehour={end_hour}&emin={end_minute}&sRef={urlencode(channel_name_external, "utf8")}&name={urlencode(title, "utf8")}&description={urlencode(title, "utf8")}&afterevent=0&eit=&disabled=0&justplay=0&repeated=0
103         
104         to zap for some time:
105             &command=add&&syear={start_year}&smonth={start_month}&sday={start_day}&shour={start_hour}&smin={start_minute}&eyear={end_year}&emonth={end_month}&eday={end_day}&ehour={end_hour}&emin={end_minute}&sRef={urlencode(channel_name_external, "utf8")}&name={urlencode(title, "utf8")}&description={urlencode(title, afterevent=0&eit=&disabled=0&justplay=1&repeated=0
106         
107         to delete something:
108             &command=del&&syear={start_year}&smonth={start_month}&sday={start_day}&shour={start_hour}&smin={start_minute}&eyear={end_year}&emonth={end_month}&eday={end_day}&ehour={end_hour}&emin={end_minute}&sRef={urlencode(channel_name_external, "utf8")}&name={urlencode(title, "utf8")}&description={urlencode(title, "utf8")}&afterevent=0&eit=&disabled=0&justplay=0&repeated=0
109         """
110         
111         listDate = ['syear','smonth','sday','shour','smin','eyear','emonth','eday','ehour','emin']
112         for element in listDate:
113             if param[element] is None:
114                 return False,"%s missing"%element
115             else:
116                 param[element] = int(param[element])
117         param['begin'] = int( strftime("%s",  localtime(mktime( (param['syear'], param['smonth'], param['sday'], param['shour'], param['smin'], 0, 0, 0, -1) ) ) ) )
118         param['end']   = int( strftime("%s",  localtime(mktime( (param['eyear'], param['emonth'], param['eday'], param['ehour'], param['emin'], 0, 0, 0, -1) ) ) ) )
119         
120         for element in listDate:
121             del param[element]
122         
123         if param['sRef'] is None:
124             return False,"sRef missing"
125         else:
126             takeApart = split(param['sRef'], '|')
127             if len(takeApart) > 1:
128                 param['sRef'] = takeApart[1]
129         
130         repeated = 0
131         if param.has_key('repeated'):
132             repeated = int(param['repeated'])
133         if repeated == 0:
134             list = ["mo","tu","we","th","fr","sa","su","ms","mf"]
135             for element in list:
136                 if param.has_key(element):
137                     number = param[element] or 0
138                     del param[element]
139                     repeated = repeated + int(number)
140             if repeated > 127:
141                 repeated = 127
142         param['repeated'] = repeated
143
144         if param['command'] == "add":
145             del param['command']
146             return self.addTimer(param)
147         elif param['command'] == "del":
148             del param['command']
149             return self.delTimer(param)
150         elif param['command'] == "change":
151             del param['command']
152             return self.changeTimer(param)
153         else:
154             return False,"command missing"
155     
156     def recordNow(self,param):
157         print "recordNow ",param
158         
159         limitEvent = True
160         if param == "undefinitely":
161             limitEvent = False
162         
163         serviceref = self.session.nav.getCurrentlyPlayingServiceReference()
164         print serviceref
165         #assert isinstance(serviceref, ServiceReference)
166         serviceref = ServiceReference(serviceref.toString())
167         event = None
168         try:
169             service = self.session.nav.getCurrentService()
170             event = self.epgcache.lookupEventTime(serviceref, -1, 0)
171             if event is None:
172                 info = service.info()
173                 ev = info.getEvent(0)
174                 event = ev
175         except:
176             pass
177
178         begin = time()
179         end = begin + 3600 * 10
180         name = "instant record"
181         description = ""
182         eventid = 0
183
184         if event is not None:
185             curEvent = parseEvent(event)
186             name = curEvent[2]
187             description = curEvent[3]
188             eventid = curEvent[4]
189             if limitEvent:
190                 end = curEvent[1]
191         else:
192             if limitEvent:
193                 return False, "No event found, started recording undefinitely"
194
195         newtimer = RecordTimerEntry(serviceref, begin, end, name, description, eventid, False, False, 0)
196         newtimer.dontSave = True
197         self.recordtimer.record(newtimer)
198
199         return True,"recording was startet"
200         
201     def addTimer(self,param):
202         # is there an easier and better way? :\ 
203         print "addTimer",param
204         if param['sRef'] is None:
205             return False,"ServiceReference missing"
206         else: 
207             serviceref = ServiceReference(param['sRef'])
208         
209         if param['begin'] is None:
210            return False,"begin missing"
211         else:
212             begin = float(param['begin'])
213         
214         if param['end'] is None:
215             return False,"end missing"
216         elif float(param['end']) > time():
217             end = float(param['end'])
218         else:
219              return False,"end is in the past"
220                 
221         if param['name'] is None:
222             return False,"name is missing"
223         else:
224             print "name1 ",param['name']
225             name = unescape(param['name'])#.encode("UTF-16LE")#.decode('utf-8')
226             print "name2 ",name
227             #).decode('utf_8')
228             
229         if param['description'] is not None:
230             print "description1 ",param['description']
231             description = unescape(param['description'])#.encode("UTF-16LE")#.decode('utf-8')
232             description = description.replace("\n"," ") # if a \n is in the timedescription, the resulting .meta breacks
233             print "description2 ",description
234         else: 
235             description = ""
236             
237         if param['disabled'] =="0":
238             disabled = False
239         elif param['disabled'] =="1":
240             disabled = True
241         else:
242             return False,"disabled incorrect"
243         
244         if param['justplay'] == "0":
245             justplay = False
246         elif param['justplay'] == "1":
247             justplay = True
248         else:
249             return False,"justplay incorrect"
250             
251         if param['afterevent'] == "0":
252             afterevent = 0
253         elif param['afterevent'] == "1":
254             afterevent = 1
255         elif param['afterevent'] == "2":
256             afterevent = 2
257         elif param['afterevent'] == "3":
258             afterevent = 3
259         else:
260             return False,"afterevent incorrect"
261         
262         repeated = 0
263         if param.has_key('repeated'):
264             repeated = int(param['repeated'])
265         
266         newtimer = RecordTimerEntry(serviceref, begin, end, name, description, 0, disabled, justplay, afterevent)
267         newtimer.repeated = repeated
268         self.recordtimer.record(newtimer)
269         #self.session.nav.RecordTimer.saveTimer()
270         return True,"Timer added"        
271
272     def addTimerByEventID(self,param):
273         print "addTimerByEventID",param
274         if param['sRef'] is None:
275             return False,"ServiceReference not set"
276         if param['eventid'] is None:
277             return False,"Eventid not set"
278         
279         justplay = False
280         if param['justplay'] is not None:
281             if param['justplay'] == "1":
282                 justplay = True
283
284         epgcache = eEPGCache.getInstance()
285         event = epgcache.lookupEventId(eServiceReference(param['sRef']),int(param['eventid']))
286         if event is None:
287             return False,"Eventid not found"
288         (begin, end, name, description, eit) = parseEvent(event)
289         
290         print "addTimerByEventID newtimer ", param['sRef'], begin, end, name, description, eit, False, justplay
291         newtimer = RecordTimerEntry(ServiceReference(param['sRef']), begin , end, name, description, eit, False, justplay, AFTEREVENT.NONE)                        
292                 
293         self.recordtimer.record(newtimer)
294         return True,"Timer added"    
295             
296     def changeTimer(self, param):
297         
298         print "changeTimer ", param
299         if param['deleteOldOnSave'] is not None:
300             if int(param['deleteOldOnSave']) == 1:
301                 
302                 if param['sRef'] is None:
303                     return [False, "ServiceReference missing"]
304                 else: 
305                     serviceref = ServiceReference(param['sRef'])
306     
307                 if param['repeated'] is not None:
308                     repeated = int(param['repeated'])
309                 else: 
310                     repeated = 0
311                 
312                 if param['begin'] is None:
313                     return False,"begin missing"
314                 elif time() <= float(param['begin']):
315                     begin = float(param['begin'])
316                 elif time() > float(param['begin']) and repeated == 1:
317                     begin = time()
318                 else:
319                     return False,"incorrect time begin"
320             
321                 if param['end'] is None:
322                     return False,"end missing"
323                 elif begin < float(param['end']):
324                     end = float(param['end'])
325                 else:
326                     return False,"incorrect time end"
327                     
328                 if param['name'] is None:
329                     return False,"name is missing"
330                 else:
331                     name = param['name']
332                 
333                 if param['description'] is not None:
334                     description = param['description']
335                     description = description.replace("\n"," ") # if a \n is in the timedescription, the resulting .meta breacks
336                 else: 
337                     description = ""
338     
339                 if param['repeated'] is not None:
340                     repeated = int(param['repeated'])
341                 else: 
342                     repeated = 0
343     
344                 if param['disabled'] =="0":
345                     disabled = False
346                 elif param['disabled'] =="1":
347                     disabled = True
348                 else:
349                     return False,"disabled incorrect"
350             
351                 if param['justplay'] == "0":
352                     justplay = False
353                 elif param['justplay'] == "1":
354                     justplay = True
355                 else:
356                     return False,"justplay incorrect"
357                 
358                 if param['afterevent'] == "0":
359                     afterevent = 0
360                 elif param['afterevent'] == "1":
361                     afterevent = 1
362                 elif param['afterevent'] == "2":
363                     afterevent = 2
364                 else:
365                     return False,"afterevent incorrect"
366             
367                 if param['channelOld'] is None:
368                     return False,"channelOld missing"
369                 else: 
370                     channelOld = ServiceReference(param['channelOld'])
371                 
372                 if param['beginOld'] is None:
373                     return False,"beginOld missing"
374                 else:
375                     beginOld = float(param['beginOld'])
376                 
377                 if param['endOld'] is None:
378                     return False,"endOld missing"
379                 else:
380                     endOld = float(param['endOld'])
381                     
382                 toChange = None
383                 try:
384                     #print "beginOld(%s), endOld(%s), channelOld(%s)" % (beginOld, endOld, channelOld)
385                     for x in self.recordtimer.timer_list + self.recordtimer.processed_timers:
386                         #print "x.begin(%s), x.end(%s), x.service_ref(%s)" % (float(x.begin), float(x.end), x.service_ref)
387                         if str(x.service_ref) == str(channelOld) and float(x.begin) == beginOld and float(x.end) == endOld:
388                             #print "one found"
389                             toChange = x
390                             toChange.service_ref = ServiceReference(param['sRef'])
391                             toChange.begin = int(begin)
392                             toChange.end = int(end)
393                             toChange.name = name
394                             toChange.description = description
395                             toChange.disabled = disabled
396                             toChange.justplay = justplay
397                             toChange.afterEvent = afterevent
398                             toChange.repeated = repeated
399                             self.session.nav.RecordTimer.timeChanged(toChange)
400                             print "Timer changed"
401                             return True,"Timer changed"
402                             break
403                 except:
404                     return False, "error searching for old Timer"            
405                 if toChange is None:
406                     return False, "Timer not found"
407             else:
408                 return self.addTimer(param)
409         else:
410                 return False, "Parameter deleteOldOnSave not Set"
411         
412     def writeTimerList(self,force=False):
413         # is there an easier and better way? :\
414         if config.plugins.Webinterface.autowritetimer.value or force: 
415             print "Timer.py writing timer to flash"
416             self.session.nav.RecordTimer.saveTimer()
417             return True, "TimerList was saved "
418         else:
419             return False, "TimerList was not saved "    
420
421     def getText(self):
422         print self.result
423         (result,text) = self.result
424         xml  = "<e2simplexmlresult>\n"
425         if result:
426             xml += "<e2state>True</e2state>\n"
427         else:
428             xml += "<e2state>False</e2state>\n"            
429         xml += "<e2statetext>%s</e2statetext>\n" % text
430         xml += "</e2simplexmlresult>\n"
431         return xml
432     
433     text = property(getText)
434     
435     ## part for listfiller requests
436     def command(self):
437         timerlist = []
438
439         for item in self.recordtimer.timer_list + self.recordtimer.processed_timers:
440             timer = []
441             timer.append(item.service_ref)
442             timer.append(item.service_ref.getServiceName())
443             timer.append(item.eit)
444             timer.append(item.name)
445             timer.append(item.description)
446             timer.append(item.dirname)
447
448             if item.disabled is True:
449                 timer.append("1")
450             else:
451                 timer.append("0")
452
453             timer.append(item.begin)
454             timer.append(item.end)
455             timer.append(item.end - item.begin)
456             timer.append(item.start_prepare)
457             
458             if item.justplay is True:
459                 timer.append(1)
460             else:
461                 timer.append(0)
462
463             timer.append(item.afterEvent)
464             
465             """
466 No passing Logevents, because of error:
467 XML-Verarbeitungsfehler: nicht wohlgeformt
468 Adresse: http://dreambox/web/timerlist
469 Zeile Nr. 374, Spalte 259:        <e2logentries>[(1171275272, 15, 'record time changed, start prepare is now: Mon Feb 12 12:29:40 2007'), (1171279780, 5, 'activating state 1'), (1171279780, 0, "Filename calculated as: '/hdd/movie/20070212 1230 - DISNEY CHANNEL - Quack Pack - Onkel Donald & Die Boys'"), (1171279780, 3, 'prepare ok, writing meta information to /hdd/movie/20070212 1230 - DISNEY CHANNEL - Quack Pack - Onkel Donald & Die Boys'), (1171279780, 6, 'prepare ok, waiting for begin'), (1171279800, 5, 'activating state 2'), (1171279800, 11, 'start recording'), (1171281900, 5, 'activating state 3'), (1171281900, 12, 'stop recording')]</e2logentries>
470 ------------------------------------------------------------------------------------------------------------
471 No clue, what it could be.
472             """
473             timer.append(item.log_entries)
474             
475             try:
476                 timer.append(item.Filename)
477             except:
478                 timer.append("")
479             
480             timer.append(item.backoff)       
481             
482             try:
483                 timer.append(item.next_activation)
484             except:
485                 timer.append("")
486                 
487             timer.append(item.first_try_prepare)  
488             timer.append(item.state)
489             timer.append(item.repeated)
490             
491             if item.dontSave is True:
492                 timer.append(1)
493             else:
494                 timer.append(0)
495 #            timer.append(item.dontSave)
496
497             timer.append(item.cancelled)
498             
499             if item.eit is not None:
500                 event = self.epgcache.lookupEvent(['EX',("%s" % item.service_ref ,2,item.eit)])
501                 if event[0][0] is not None:
502                     timer.append(event[0][0])
503                 else:
504                     timer.append("N/A")
505             else:
506                 timer.append("N/A")
507             
508             #toggleDisabled
509             if item.disabled is True:
510                 timer.append("0")
511                 timer.append("on")
512             else:
513                 timer.append("1")
514                 timer.append("off")
515
516             timerlist.append(timer)
517             
518         return timerlist
519     
520     list = property(command)
521     lut = {"ServiceReference":0
522            ,"ServiceName": 1
523            ,"EIT":2
524            ,"Name":3
525            ,"Description":4
526            ,"Directory":5
527            ,"Disabled":6
528            ,"TimeBegin":7
529            ,"TimeEnd":8
530            ,"Duration":9
531            ,"startPrepare":10
532            ,"justPlay":11
533            ,"afterEvent":12
534            ,"LogEntries":13
535            ,"Filename":14
536            ,"Backoff":15
537            ,"nextActivation":16
538            ,"firstTryPrepare":17
539            ,"State":18
540            ,"Repeated":19
541            ,"dontSave":20
542            ,"Cancled":21
543            ,"DescriptionExtended":22
544            ,"toggleDisabled":23
545            ,"toggleDisabledIMG":24
546            }