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