new wap-page, that you can edit-timers
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / WAPfunctions.py
1 Version = '$Header$';
2
3 from Components.Sources.Source import Source
4 from Components.Sources.Source import Source
5 from Components.Sources.ServiceList import ServiceList
6 from enigma import eServiceReference
7
8
9 import time, re
10
11 class WAPfunctions( Source):
12     LISTTIME = 0
13     REPEATED = 1
14     SERVICELIST = 2
15     OPTIONLIST = 3
16     FILLVALUE = 4
17     DELETEOLD = 5
18     
19     lut = {"Name":0
20           ,"Value":1
21           ,"Selected":2
22           }
23     
24     def __init__(self, session,func = LISTTIME):
25         self.func = func
26         Source.__init__(self)        
27         self.session = session
28         self.result = ["unknown command (%s)"  % self.func]
29
30     def handleCommand(self,cmd):
31         print "WAPfunctions: handleCommand", cmd
32         if self.func is self.LISTTIME:
33             self.result = self.fillListTime(cmd)
34         elif self.func is self.REPEATED:
35             self.result = self.fillRepeated(cmd)
36         elif self.func is self.SERVICELIST:
37             self.result = self.serviceList(cmd)
38         elif self.func is self.OPTIONLIST:
39             self.result = self.fillOptionList(cmd)
40         elif self.func is self.FILLVALUE:
41             self.result = self.fillValue(cmd)
42         elif self.func is self.DELETEOLD:
43             self.result = self.deleteOldSaved(cmd)
44         else:
45             self.result = ["unknown command cmd(%s) self.func(%s)" % (cmd, self.func)]
46
47     def fillListTime(self,param):
48         print "fillListTime",param
49         
50         input = 0
51         start = 1
52         end = 1
53         
54         timeNow = time.time()
55         timePlusTwo = timeNow + 7200
56
57         if param.has_key('begin'):
58             begin = param['begin'] or 0
59             begin = int(begin)
60             del param['begin']
61             if begin > 0:
62                 timeNow = begin
63         if param.has_key('end'):
64             end = param['end'] or 0
65             end = int(end)
66             del param['end']
67             if end > 0:
68                 timePlusTwo = end
69         
70         t = {}
71         t["sday"]=time.strftime("%d", time.localtime(timeNow))
72         t["smonth"]=time.strftime("%m", time.localtime(timeNow))
73         t["syear"]=time.strftime("%Y", time.localtime(timeNow))
74         t["smin"]=time.strftime("%M", time.localtime(timeNow))
75         t["shour"]=time.strftime("%H", time.localtime(timeNow))
76         
77         t["eday"]=time.strftime("%d", time.localtime(timePlusTwo))
78         t["emonth"]=time.strftime("%m", time.localtime(timePlusTwo))
79         t["eyear"]=time.strftime("%Y", time.localtime(timePlusTwo))
80         t["emin"]=time.strftime("%M", time.localtime(timePlusTwo))
81         t["ehour"]=time.strftime("%H", time.localtime(timePlusTwo))
82         
83         key = ""
84         for i in param:
85             p = str(i)
86             if p != "sRef":
87                 key = p
88
89         cutKey = re.sub("^[es]", "", key, 1)
90         
91         if cutKey == "min":
92             start = 0
93             end = 59
94         elif cutKey == "hour":
95             start = 1
96             end = 24
97         elif cutKey == "day":
98             start = 1
99             end = 31
100         elif cutKey == "month":
101             start = 1
102             end = 12
103         else:
104             start = int(t[key])
105             end = int(t[key])+2
106         
107         if param[key] == "now" or param[key] == "end" or param[key] == "begin":
108             input = int(t[key])
109         else:
110             input = param[key] or 0
111             input = int(input)
112         #print cutKey,param[key],input
113         
114         self.result = self.fillOptionListAny(input,start,end)
115         return self.result
116     
117     def fillOptionListAny(self,input,start,end):
118         returnList = []
119         for i in range(start,end+1,1):
120             returnList1 = []
121             j = str(i)
122             if len(j) == 1:
123                 j = "0%s" % j
124             returnList1.append(j)
125             returnList1.append(i)
126             if i==input:
127                 returnList1.append("selected")
128             else:
129                 returnList1.append("")
130             returnList.append(returnList1)
131         return returnList
132         
133     def fillRepeated(self,param):
134         print "fillRepeated",param
135         repeated = param or 0
136         repeated = int(repeated)
137         
138         self.lut = {"Name":0
139           ,"Value":1
140           ,"Description":2
141           ,"Selected":3
142           }
143         
144         mo = ["mo",   1, "Mo "]#"Monday"]
145         tu = ["tu",   2, "Tu "]#"Tuesday"]
146         we = ["we",   4, "We "]#"Wednesday"]
147         th = ["th",   8, "Th "]#"Thursday"]
148         fr = ["fr",  16, "Fr "]#"Friday"]
149         sa = ["sa",  32, "Sa "]#"Saturday"]
150         su = ["su",  64, "Su "]#"Sunday"]
151         mf = ["mf",  31, "Mo-Fr"]
152         ms = ["ms", 127, "Mo-Su"]
153         
154         if repeated == 127:
155             repeated = repeated - 127
156             ms.append("checked")
157         else:
158             ms.append("")
159         
160         if repeated >= 64:
161             repeated = repeated - 64
162             su.append("checked")
163         else:
164             su.append("")
165         
166         if repeated >= 32:
167             repeated = repeated - 32
168             sa.append("checked")
169         else:
170             sa.append("")
171         
172         if repeated == 31:
173             repeated = repeated - 31
174             mf.append("checked")
175         else:
176             mf.append("")
177
178         if repeated >= 16:
179             repeated = repeated - 16
180             fr.append("checked")
181         else:
182             fr.append("")
183
184         if repeated >= 8:
185             repeated = repeated - 8
186             th.append("checked")
187         else:
188             th.append("")
189         
190         if repeated >= 4:
191             repeated = repeated - 4
192             we.append("checked")
193         else:
194             we.append("")
195         
196         if repeated >= 2:
197             repeated = repeated - 2
198             tu.append("checked")
199         else:
200             tu.append("")
201         
202         if repeated == 1:
203             repeated = repeated - 1
204             mo.append("checked")
205         else:
206             mo.append("")
207             
208         returnList = []
209         returnList.append(mo)
210         returnList.append(tu)
211         returnList.append(we)
212         returnList.append(th)
213         returnList.append(fr)
214         returnList.append(sa)
215         returnList.append(su)
216         returnList.append(mf)
217         returnList.append(ms)
218
219         return returnList
220     
221     def serviceList(self,param):
222         print "serviceList: ",param
223         sRef = str(param["sRef"])
224         bouquet = str(param["bouquet"])
225         returnList = []
226         sRefFound = 0
227         
228         if bouquet == '':
229             bouquet = '1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "userbouquet.favourites.tv" ORDER BY bouquet'
230         ref = eServiceReference(bouquet)
231         self.servicelist = ServiceList(ref, command_func = self.getServiceList, validate_commands=False)
232         self.servicelist.setRoot(ref)
233         for (ref2, name) in self.servicelist.getServicesAsList():
234             print "ref2: (",ref2, ") name: (",name,")"
235             returnListPart = []
236             returnListPart.append(name)
237             returnListPart.append(ref2)
238             if ref2 == str(sRef):
239                 returnListPart.append("selected")
240                 sRefFound = 1
241             else:
242                 returnListPart.append("")
243             returnList.append(returnListPart)
244
245         if sRefFound == 0 and sRef != '':
246             returnListPart = ["Inserted", sRef, "selected"]
247             returnList.append(returnListPart)
248         #print returnList
249         return returnList
250
251     def getServiceList(self, ref):
252         self.servicelist.root = ref
253     
254     def fillOptionList(self,param):
255          
256         print "fillOptionList",param
257         returnList = []
258         if param.has_key("justplay"):
259             number = param["justplay"] or 0
260             number = int(number)
261             if number == 1:
262                 returnList.append(["Record",0,""])
263                 returnList.append(["Zap",1,"selected"])
264             else:
265                 returnList.append(["Record",0,"selected"])
266                 returnList.append(["Zap",1,""])
267         elif param.has_key("afterevent"):
268             number = param["afterevent"] or 0
269             number = int(number)
270             if number == 2:
271                 returnList.append(["Nothing",0,""])
272                 returnList.append(["Standby",1,""])
273                 returnList.append(["Deepstandby/Shutdown",2,"selected"])
274             elif number == 1:
275                 returnList.append(["Nothing",0,""])
276                 returnList.append(["Standby",1,"selected"])
277                 returnList.append(["Deepstandby/Shutdown",2,""])
278             else:
279                 returnList.append(["Nothing",0,"selected"])
280                 returnList.append(["Standby",1,""])
281                 returnList.append(["Deepstandby/Shutdown",2,""])
282         
283         return returnList
284     
285     def deleteOldSaved(self,param):
286         print "deleteOldSaved",param
287         returnList = []
288         returnList.append(["deleteOldOnSave",param["deleteOldOnSave"],""])
289         returnList.append(["command",param["command"],""])
290         if int(param["deleteOldOnSave"]) == 1:
291             returnList.append(["channelOld",param["sRef"],""])
292             returnList.append(["beginOld",param["begin"],""])
293             returnList.append(["endOld",param["end"],""])
294         return returnList
295             
296     
297     def fillValue(self,param):
298         print "fillValue: ",param
299         return [["",param,""]]
300
301     def getText(self):
302         (result,text) = self.result
303         return text
304     
305     def filterXML(self, item):
306         item = item.replace("&", "&amp;").replace("<", "&lt;").replace('"', '&quot;').replace(">", "&gt;")
307         return item
308
309     def getList(self):
310         return self.result
311
312     ## part for listfiller requests
313     def command(self):
314         timerlist = []
315
316         return timerlist
317     
318     text = property(getText)
319     list = property(getList)