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