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