add extended description of timers
[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
9 import time
10
11 class Timer( Source):
12     LIST = 0
13     ADDBYID = 1
14     ADD = 2
15     DEL = 3
16     
17     def __init__(self, session,func = LIST):
18         self.func = func
19         Source.__init__(self)        
20         self.session = session
21         self.recordtimer = session.nav.RecordTimer
22         self.epgcache = eEPGCache.getInstance()
23
24     def handleCommand(self,cmd):
25         if self.func is self.ADDBYID:
26             self.result = self.addTimerByEventID(cmd)
27         elif self.func is self.ADD:
28             self.result = self.addTimer(cmd)
29         elif self.func is self.DEL:
30             self.result = self.delTimer(cmd)
31         else:
32             self.result = False,"unknown command"
33
34     def delTimer(self,param):
35         # is there an easier and better way? :\ 
36         print "delTimer",param
37         
38         if param['serviceref'] is None:
39             return False,"ServiceReference missing"
40         else: 
41             serviceref = ServiceReference(param['serviceref'])
42         
43         if param['begin'] is None:
44            return False,"begin missing"
45         else:
46             begin = float(param['begin'])
47         
48         if param['end'] is None:
49             return False,"end missing"
50         else:
51                 end = float(param['end'])
52              
53         toDelete = None
54         for x in self.session.nav.RecordTimer.timer_list+self.session.nav.RecordTimer.processed_timers:
55                 if str(x.service_ref) == str(serviceref) and float(x.begin) == begin and float(x.end) == end:
56                         toDelete = x
57             
58         if toDelete is not None:
59                 self.session.nav.RecordTimer.removeEntry(toDelete)
60                 return True,"Timer removed"
61         else:
62                 return False,"Timer not found"
63                 print "Timer not found"
64         
65         #self.session.nav.RecordTimer.saveTimer()
66     
67     def addTimer(self,param):
68         # is there an easier and better way? :\ 
69         print "addTimerByEventID",param
70         if param['serviceref'] is None:
71             return False,"ServiceReference missing"
72         else: 
73             serviceref = ServiceReference(param['serviceref'])
74         
75         if param['begin'] is None:
76            return False,"begin missing"
77         elif time.time() <= float(param['begin']):
78             begin = float(param['begin'])
79         else:
80             return False,"incorrect time begin"
81             
82         
83         if param['end'] is None:
84             return False,"end missing"
85         elif begin < float(param['end']):
86             end = float(param['end'])
87         else:
88              return False,"incorrect time end"
89                 
90         if param['name'] is None:
91             return False,"name is missing"
92         else:
93             name = param['name']
94             
95         if param['description'] is not None:
96             description = param['description']
97         else: 
98             description = ""
99             
100         if param['disabled'] =="0":
101             disabled = False
102         elif param['disabled'] =="1":
103             disabled = True
104         else:
105             return False,"disabled incorrect"
106         
107         if param['justplay'] == "0":
108             justplay = False
109         elif param['justplay'] == "1":
110             justplay = True
111         else:
112             return False,"justplay incorrect"
113             
114         if param['afterevent'] == "0":
115             afterevent = 0
116         elif param['afterevent'] == "1":
117             afterevent = 1
118         elif param['afterevent'] == "2":
119             afterevent = 2
120         else:
121             return False,"afterevent incorrect"
122             
123         newtimer = RecordTimerEntry(serviceref, begin, end, name, description, 0, disabled, justplay, afterevent)
124         self.session.nav.RecordTimer.record(newtimer)
125         #self.session.nav.RecordTimer.saveTimer()
126         return True,"Timer added"        
127
128     def addTimerByEventID(self,param):
129         print "addTimerByEventID",param['serviceref'],param['eventid']
130         if param['serviceref'] is None:
131             return False,"ServiceReference not set"
132         if param['eventid'] is None:
133             return False,"Eventid not set"
134         epgcache = eEPGCache.getInstance()
135         event = epgcache.lookupEventId(eServiceReference(param['serviceref']),int(param['eventid']))
136         if event is None:
137             return False,"Eventid not found"
138         (begin, end, name, description, eit) =parseEvent(event)
139         newtimer = RecordTimerEntry(ServiceReference(param['serviceref']), begin, end, name, description, eit, False, False, AFTEREVENT.NONE)
140         self.session.nav.RecordTimer.record(newtimer)
141         return True,"Timer added"    
142             
143     def getText(self):    
144         (result,text) = self.result
145         xml = "<?xml version=\"1.0\"?>\n"
146         xml  += "<e2timeraddresult>\n"
147         if result:
148             xml += "<e2state>True</e2state>\n"
149         else:
150             xml += "<e2state>False</e2state>\n"            
151         xml += "<e2statetext>%s</e2statetext>\n"%text
152         xml += "</e2timeraddresult>\n"
153         return xml
154     
155     text = property(getText)    
156     
157     ## part for listfiller requests
158     def command(self):
159         timerlist = []
160         for item in self.recordtimer.timer_list+self.recordtimer.processed_timers:
161             timer = []
162             timer.append(item.service_ref)
163             timer.append(item.service_ref.getServiceName())
164             timer.append(item.eit)
165             timer.append(item.name)
166             timer.append(item.description)
167             timer.append(item.disabled)
168             timer.append(item.begin)
169             timer.append(item.end)
170             timer.append(item.end-item.begin)
171             timer.append(item.start_prepare)
172             timer.append(item.justplay)
173             timer.append(item.afterEvent)
174             timer.append(item.log_entries)
175             try: 
176                 timer.append(item.Filename)
177             except AttributeError:
178                 timer.append("")
179             
180             timer.append(item.backoff)       
181             try:
182                 timer.append(item.next_activation)
183             except AttributeError:
184                 timer.append("")
185             timer.append(item.first_try_prepare)  
186             timer.append(item.state)  
187             timer.append(item.repeated)
188             timer.append(item.dontSave)
189             timer.append(item.cancelled)
190             
191             event = self.epgcache.lookupEvent(['E',("%s"%item.service_ref,2,item.eit)])
192             if event[0][0] is not None:
193                 timer.append(event[0][0])
194             else:
195                 timer.append("N/A")
196                 
197             timerlist.append(timer) 
198             
199         return timerlist
200     
201     list = property(command)
202     lut = {"ServiceReference": 0
203            ,"ServiceName": 1
204            ,"EIT":2
205            ,"Name":3
206            ,"Description":4
207            ,"Disabled":5
208            ,"TimeBegin":6
209            ,"TimeEnd":7
210            ,"Duration":8
211            ,"startPrepare":9
212            ,"justPlay":10
213            ,"afterEvent":11
214            ,"LogEntries":12
215            ,"Filename":13
216            ,"Backoff":14
217            ,"nextActivation":15
218            ,"firstTryPrepare":16
219            ,"State":17
220            ,"Repeated":18
221            ,"dontSave":19
222            ,"Cancled":20
223            ,"DescriptionExtended":21
224            }
225