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