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