addtimerbyevent is now checking the settings for recordingmargins and is adding them
[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&action=0&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         to delete something:
88             &command=del&action=0&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
89         """
90         
91         syear = 0
92         if param['syear'] is None:
93            return False,"syear missing"
94         else:
95             syear = int(param['syear'])
96         
97         smonth = 0
98         if param['smonth'] is None:
99            return False,"smonth missing"
100         else:
101             smonth = int(param['smonth'])
102         
103         sday = 0
104         if param['sday'] is None:
105            return False,"sday missing"
106         else:
107             sday = int(param['sday'])
108         
109         shour = 0
110         if param['shour'] is None:
111            return False,"shour missing"
112         else:
113             shour = int(param['shour'])
114         
115         smin = 0
116         if param['smin'] is None:
117            return False,"smin missing"
118         else:
119             smin = int(param['smin'])
120             
121         eyear = 0
122         if param['eyear'] is None:
123            return False,"eyear missing"
124         else:
125             eyear = int(param['eyear'])
126         
127         emonth = 0
128         if param['emonth'] is None:
129            return False,"emonth missing"
130         else:
131             emonth = int(param['emonth'])
132         
133         eday = 0
134         if param['eday'] is None:
135            return False,"eday missing"
136         else:
137             eday = int(param['eday'])
138         
139         ehour = 0
140         if param['ehour'] is None:
141            return False,"ehour missing"
142         else:
143             ehour = int(param['ehour'])
144         
145         emin = 0
146         if param['emin'] is None:
147            return False,"emin missing"
148         else:
149             emin = int(param['emin'])
150         
151         # for compatibility reasons
152         if param['serviceref'] is None:
153             return False,"ServiceReference missing"
154         else:
155             takeApart = string.split(param['serviceref'], '|')
156             if len(takeApart) > 1:
157                 param['serviceref'] = takeApart[1]
158
159         param['begin'] = int( time.strftime("%s",  time.localtime(time.mktime( (syear, smonth, sday, shour, smin, 0, 0, 0, 0) ) ) ) )
160         param['end']   = int( time.strftime("%s",  time.localtime(time.mktime( (eyear, emonth, eday, ehour, emin, 0, 0, 0, 0) ) ) ) )
161         
162         if param['command'] == "add":
163             return self.addTimer(param)
164         elif param['command'] == "del":
165             return self.delTimer(param)
166         else:
167             return False,"command missing"
168         
169     def addTimer(self,param):
170         # is there an easier and better way? :\ 
171         print "addTimer",param
172         if param['serviceref'] is None:
173             return False,"ServiceReference missing"
174         else: 
175             serviceref = ServiceReference(param['serviceref'])
176         
177         if param['begin'] is None:
178            return False,"begin missing"
179         elif time.time() <= float(param['begin']):
180             begin = float(param['begin'])
181         else:
182             return False,"incorrect time begin"
183             
184         
185         if param['end'] is None:
186             return False,"end missing"
187         elif begin < float(param['end']):
188             end = float(param['end'])
189         else:
190              return False,"incorrect time end"
191                 
192         if param['name'] is None:
193             return False,"name is missing"
194         else:
195             name = param['name']
196             
197         if param['description'] is not None:
198             description = param['description']
199         else: 
200             description = ""
201             
202         if param['disabled'] =="0":
203             disabled = False
204         elif param['disabled'] =="1":
205             disabled = True
206         else:
207             return False,"disabled incorrect"
208         
209         if param['justplay'] == "0":
210             justplay = False
211         elif param['justplay'] == "1":
212             justplay = True
213         else:
214             return False,"justplay incorrect"
215             
216         if param['afterevent'] == "0":
217             afterevent = 0
218         elif param['afterevent'] == "1":
219             afterevent = 1
220         elif param['afterevent'] == "2":
221             afterevent = 2
222         else:
223             return False,"afterevent incorrect"
224             
225         newtimer = RecordTimerEntry(serviceref, begin, end, name, description, 0, disabled, justplay, afterevent)
226         self.recordtimer.record(newtimer)
227         #self.session.nav.RecordTimer.saveTimer()
228         return True,"Timer added"        
229
230     def addTimerByEventID(self,param):
231         print "addTimerByEventID",param['serviceref'],param['eventid']
232         if param['serviceref'] is None:
233             return False,"ServiceReference not set"
234         if param['eventid'] is None:
235             return False,"Eventid not set"
236         epgcache = eEPGCache.getInstance()
237         event = epgcache.lookupEventId(eServiceReference(param['serviceref']),int(param['eventid']))
238         if event is None:
239             return False,"Eventid not found"
240         (begin, end, name, description, eit) =parseEvent(event)
241         justplay = False
242         if param['justplay'] is None and param['justplay'] == "True":
243             justplay = True
244         
245         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)
246         self.recordtimer.record(newtimer)
247         return True,"Timer added"    
248             
249     def getText(self):    
250         (result,text) = self.result
251         xml = "<?xml version=\"1.0\"?>\n"
252         xml  += "<e2timeraddresult>\n"
253         if result:
254             xml += "<e2state>True</e2state>\n"
255         else:
256             xml += "<e2state>False</e2state>\n"            
257         xml += "<e2statetext>%s</e2statetext>\n"%text
258         xml += "</e2timeraddresult>\n"
259         return xml
260     
261     text = property(getText)    
262     
263     ## part for listfiller requests
264     def command(self):
265         timerlist = []
266 #        print "len(self.recordtimer.timer_list) ", len(self.recordtimer.timer_list)
267  #       print "timer_list ", self.recordtimer.timer_list
268   #      print "processed_timers", self.recordtimer.processed_timers
269 #        try:
270         for item in self.recordtimer.timer_list + self.recordtimer.processed_timers:
271             timer = []
272             timer.append(item.service_ref)
273             timer.append(item.service_ref.getServiceName())
274             timer.append(item.eit)
275             timer.append(item.name)
276             timer.append(item.description)
277             timer.append(item.disabled)
278             timer.append(item.begin)
279             timer.append(item.end)
280             timer.append(item.end - item.begin)
281             timer.append(item.start_prepare)
282             timer.append(item.justplay)
283             timer.append(item.afterEvent)
284             timer.append(item.log_entries)
285             
286             try:
287                 timer.append(item.Filename)
288             except:
289                 timer.append("")
290             
291             timer.append(item.backoff)       
292             
293             try:
294                 timer.append(item.next_activation)
295             except:
296                 timer.append("")
297                 
298             timer.append(item.first_try_prepare)  
299             timer.append(item.state)
300             timer.append(item.repeated)
301             timer.append(item.dontSave)
302             timer.append(item.cancelled)
303             
304             if item.eit is not None:
305                 event = self.epgcache.lookupEvent(['E',("%s" % item.service_ref ,2,item.eit)])
306                 if event[0][0] is not None:
307                     timer.append(event[0][0])
308                 else:
309                     timer.append("N/A")
310             else:
311                 timer.append("N/A")
312                 
313             timerlist.append(timer)
314 #        except:
315 #            print sys.exc_info()[0]
316 #            print sys.exc_info()[1]
317 #            print traceback.extract_tb(sys.exc_info()[2])
318             
319         return timerlist
320     
321     list = property(command)
322     lut = {"ServiceReference":0
323            ,"ServiceName": 1
324            ,"EIT":2
325            ,"Name":3
326            ,"Description":4
327            ,"Disabled":5
328            ,"TimeBegin":6
329            ,"TimeEnd":7
330            ,"Duration":8
331            ,"startPrepare":9
332            ,"justPlay":10
333            ,"afterEvent":11
334            ,"LogEntries":12
335            ,"Filename":13
336            ,"Backoff":14
337            ,"nextActivation":15
338            ,"firstTryPrepare":16
339            ,"State":17
340            ,"Repeated":18
341            ,"dontSave":19
342            ,"Cancled":20
343            ,"DescriptionExtended":21
344            }
345