fix for the tvbrowser zap-option
[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     
20     def __init__(self, session,func = LIST):
21         self.func = func
22         Source.__init__(self)        
23         self.session = session
24         self.recordtimer = session.nav.RecordTimer
25         self.epgcache = eEPGCache.getInstance()
26
27     def handleCommand(self,cmd):
28         if self.func is self.ADDBYID:
29             self.result = self.addTimerByEventID(cmd)
30         elif self.func is self.ADD:
31             self.result = self.addTimer(cmd)
32         elif self.func is self.TVBROWSER:
33             self.result = self.tvBrowser(cmd)
34         elif self.func is self.DEL:
35             self.result = self.delTimer(cmd)
36         else:
37             self.result = False,"unknown command"
38
39     def delTimer(self,param):
40         # is there an easier and better way? :\ 
41         print "delTimer",param
42         
43         if param['serviceref'] is None:
44             return False,"ServiceReference missing"
45         else: 
46             serviceref = ServiceReference(param['serviceref'])
47         
48         if param['begin'] is None:
49            return False,"begin missing"
50         else:
51             begin = float(param['begin'])
52         
53         if param['end'] is None:
54             return False,"end missing"
55         else:
56                 end = float(param['end'])
57              
58         toDelete = None
59         try:
60             print "timer_list ", self.recordtimer.timer_list
61             print "processed_timers", self.recordtimer.processed_timers
62             for x in self.recordtimer.timer_list + self.recordtimer.processed_timers:
63                 print "x.begin(%s), x.end(%s), x.service_ref(%s)" % (x.begin, x.end, x.service_ref)
64                 if str(x.service_ref) == str(serviceref) and float(x.begin) == begin and float(x.end) == end:
65                                   toDelete = x
66         except:
67             print "Fehler\n"
68             
69         if toDelete is not None:
70                 self.recordtimer.removeEntry(toDelete)
71                 return True,"Timer removed"
72         else:
73                 return False,"Timer not found"
74                 print "Timer not found"
75         
76         #self.session.nav.RecordTimer.saveTimer()
77     
78     def tvBrowser(self,param):
79         print "tvbrowser",param
80         
81         """ Therefor the URL's for the tvBrowser-Capture-Driver are:
82         
83             http://dreambox/web/tvbrowser? +
84             
85         To add something:
86             &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
87         
88         to zap for some time:
89             &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
90         
91         to delete something:
92             &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
93         """
94         
95         syear = 0
96         if param['syear'] is None:
97            return False,"syear missing"
98         else:
99             syear = int(param['syear'])
100         
101         smonth = 0
102         if param['smonth'] is None:
103            return False,"smonth missing"
104         else:
105             smonth = int(param['smonth'])
106         
107         sday = 0
108         if param['sday'] is None:
109            return False,"sday missing"
110         else:
111             sday = int(param['sday'])
112         
113         shour = 0
114         if param['shour'] is None:
115            return False,"shour missing"
116         else:
117             shour = int(param['shour'])
118         
119         smin = 0
120         if param['smin'] is None:
121            return False,"smin missing"
122         else:
123             smin = int(param['smin'])
124             
125         eyear = 0
126         if param['eyear'] is None:
127            return False,"eyear missing"
128         else:
129             eyear = int(param['eyear'])
130         
131         emonth = 0
132         if param['emonth'] is None:
133            return False,"emonth missing"
134         else:
135             emonth = int(param['emonth'])
136         
137         eday = 0
138         if param['eday'] is None:
139            return False,"eday missing"
140         else:
141             eday = int(param['eday'])
142         
143         ehour = 0
144         if param['ehour'] is None:
145            return False,"ehour missing"
146         else:
147             ehour = int(param['ehour'])
148         
149         emin = 0
150         if param['emin'] is None:
151            return False,"emin missing"
152         else:
153             emin = int(param['emin'])
154         
155         # for compatibility reasons
156         if param['serviceref'] is None:
157             return False,"ServiceReference missing"
158         else:
159             takeApart = string.split(param['serviceref'], '|')
160             if len(takeApart) > 1:
161                 param['serviceref'] = takeApart[1]
162
163         param['begin'] = int( time.strftime("%s",  time.localtime(time.mktime( (syear, smonth, sday, shour, smin, 0, 0, 0, 0) ) ) ) )
164         param['end']   = int( time.strftime("%s",  time.localtime(time.mktime( (eyear, emonth, eday, ehour, emin, 0, 0, 0, 0) ) ) ) )
165         
166         if param['command'] == "add":
167             return self.addTimer(param)
168         elif param['command'] == "del":
169             return self.delTimer(param)
170         else:
171             return False,"command missing"
172         
173     def addTimer(self,param):
174         # is there an easier and better way? :\ 
175         print "addTimer",param
176         if param['serviceref'] is None:
177             return False,"ServiceReference missing"
178         else: 
179             serviceref = ServiceReference(param['serviceref'])
180         
181         if param['begin'] is None:
182            return False,"begin missing"
183         elif time.time() <= float(param['begin']):
184             begin = float(param['begin'])
185         else:
186             return False,"incorrect time begin"
187             
188         
189         if param['end'] is None:
190             return False,"end missing"
191         elif begin < float(param['end']):
192             end = float(param['end'])
193         else:
194              return False,"incorrect time end"
195                 
196         if param['name'] is None:
197             return False,"name is missing"
198         else:
199             name = param['name']
200             
201         if param['description'] is not None:
202             description = param['description']
203         else: 
204             description = ""
205             
206         if param['disabled'] =="0":
207             disabled = False
208         elif param['disabled'] =="1":
209             disabled = True
210         else:
211             return False,"disabled incorrect"
212         
213         if param['justplay'] == "0":
214             justplay = False
215         elif param['justplay'] == "1":
216             justplay = True
217         else:
218             return False,"justplay incorrect"
219             
220         if param['afterevent'] == "0":
221             afterevent = 0
222         elif param['afterevent'] == "1":
223             afterevent = 1
224         elif param['afterevent'] == "2":
225             afterevent = 2
226         else:
227             return False,"afterevent incorrect"
228             
229         newtimer = RecordTimerEntry(serviceref, begin, end, name, description, 0, disabled, justplay, afterevent)
230         self.recordtimer.record(newtimer)
231         #self.session.nav.RecordTimer.saveTimer()
232         return True,"Timer added"        
233
234     def addTimerByEventID(self,param):
235         print "addTimerByEventID",param['serviceref'],param['eventid']
236         if param['serviceref'] is None:
237             return False,"ServiceReference not set"
238         if param['eventid'] is None:
239             return False,"Eventid not set"
240         epgcache = eEPGCache.getInstance()
241         event = epgcache.lookupEventId(eServiceReference(param['serviceref']),int(param['eventid']))
242         if event is None:
243             return False,"Eventid not found"
244         (begin, end, name, description, eit) =parseEvent(event)
245         justplay = False
246         if param['justplay'] is None and param['justplay'] == "True":
247             justplay = True
248         
249         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)
250         self.recordtimer.record(newtimer)
251         return True,"Timer added"    
252             
253     def getText(self):    
254         (result,text) = self.result
255         xml = "<?xml version=\"1.0\"?>\n"
256         xml  += "<e2timeraddresult>\n"
257         if result:
258             xml += "<e2state>True</e2state>\n"
259         else:
260             xml += "<e2state>False</e2state>\n"            
261         xml += "<e2statetext>%s</e2statetext>\n"%text
262         xml += "</e2timeraddresult>\n"
263         return xml
264     
265     text = property(getText)    
266     
267     ## part for listfiller requests
268     def command(self):
269         timerlist = []
270 #        print "len(self.recordtimer.timer_list) ", len(self.recordtimer.timer_list)
271  #       print "timer_list ", self.recordtimer.timer_list
272   #      print "processed_timers", self.recordtimer.processed_timers
273 #        try:
274         for item in self.recordtimer.timer_list + self.recordtimer.processed_timers:
275             timer = []
276             timer.append(item.service_ref)
277             timer.append(item.service_ref.getServiceName())
278             timer.append(item.eit)
279             timer.append(item.name)
280             timer.append(item.description)
281             if item.disabled is True:
282                 timer.append(1)
283             else:
284                 timer.append(0)
285             timer.append(item.begin)
286             timer.append(item.end)
287             timer.append(item.end - item.begin)
288             timer.append(item.start_prepare)
289             if item.justplay is True:
290                 timer.append(1)
291             else:
292                 timer.append(0)
293
294             timer.append(item.afterEvent)
295             timer.append(item.log_entries)
296             
297             try:
298                 timer.append(item.Filename)
299             except:
300                 timer.append("")
301             
302             timer.append(item.backoff)       
303             
304             try:
305                 timer.append(item.next_activation)
306             except:
307                 timer.append("")
308                 
309             timer.append(item.first_try_prepare)  
310             timer.append(item.state)
311             timer.append(item.repeated)
312             timer.append(item.dontSave)
313             timer.append(item.cancelled)
314             
315             if item.eit is not None:
316                 event = self.epgcache.lookupEvent(['E',("%s" % item.service_ref ,2,item.eit)])
317                 if event[0][0] is not None:
318                     timer.append(event[0][0])
319                 else:
320                     timer.append("N/A")
321             else:
322                 timer.append("N/A")
323                 
324             timerlist.append(timer)
325 #        except:
326 #            print sys.exc_info()[0]
327 #            print sys.exc_info()[1]
328 #            print traceback.extract_tb(sys.exc_info()[2])
329             
330         return timerlist
331     
332     list = property(command)
333     lut = {"ServiceReference":0
334            ,"ServiceName": 1
335            ,"EIT":2
336            ,"Name":3
337            ,"Description":4
338            ,"Disabled":5
339            ,"TimeBegin":6
340            ,"TimeEnd":7
341            ,"Duration":8
342            ,"startPrepare":9
343            ,"justPlay":10
344            ,"afterEvent":11
345            ,"LogEntries":12
346            ,"Filename":13
347            ,"Backoff":14
348            ,"nextActivation":15
349            ,"firstTryPrepare":16
350            ,"State":17
351            ,"Repeated":18
352            ,"dontSave":19
353            ,"Cancled":20
354            ,"DescriptionExtended":21
355            }
356