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