Pip is now working with CI
[vuplus_dvbapp] / lib / python / Screens / Ci.py
1 from Screen import *
2 from Components.MenuList import MenuList
3 from Components.ActionMap import ActionMap
4 from Components.ActionMap import NumberActionMap
5 from Components.Header import Header
6 from Components.Button import Button
7 from Components.Label import Label
8
9 from Components.HTMLComponent import *
10 from Components.GUIComponent import *
11 from Components.config import *
12 from Components.ConfigList import ConfigList
13
14 from enigma import eTimer, eDVBCI_UI, eListboxPythonStringContent, eListboxPythonConfigContent
15
16 TYPE_MENU = 0
17 TYPE_CONFIG = 1
18
19 class CiMmi(Screen):
20         def __init__(self, session, slotid, action):
21                 Screen.__init__(self, session)
22
23                 print "ciMMI with action" + str(action)
24
25                 self.tag = None
26                 self.slotid = slotid
27
28                 self.timer = eTimer()
29                 self.timer.timeout.get().append(self.keyCancel)
30
31                 #else the skins fails
32                 self["title"] = Label("")
33                 self["subtitle"] = Label("")
34                 self["bottom"] = Label("")
35                 self["entries"] = ConfigList([ ])
36                 self.listtype = TYPE_CONFIG
37
38                 self["actions"] = NumberActionMap(["SetupActions"],
39                         {
40                                 "ok": self.okbuttonClick,
41                                 "cancel": self.keyCancel,
42                                 #for PIN
43                                 "left": self.keyLeft,
44                                 "right": self.keyRight,
45                                 "1": self.keyNumberGlobal,
46                                 "2": self.keyNumberGlobal,
47                                 "3": self.keyNumberGlobal,
48                                 "4": self.keyNumberGlobal,
49                                 "5": self.keyNumberGlobal,
50                                 "6": self.keyNumberGlobal,
51                                 "7": self.keyNumberGlobal,
52                                 "8": self.keyNumberGlobal,
53                                 "9": self.keyNumberGlobal,
54                                 "0": self.keyNumberGlobal
55                         }, -1)
56
57                 self.action = action
58
59                 if action == 2:         #start MMI
60                         eDVBCI_UI.getInstance().startMMI(self.slotid)
61                         self.showWait()
62                 elif action == 3:               #mmi already there (called from infobar)
63                         self.showScreen()
64
65         def addEntry(self, list, entry):
66                 if entry[0] == "TEXT":          #handle every item (text / pin only?)
67                         list.append( (entry[1], entry[2]) )
68                 if entry[0] == "PIN":
69                         self.pinlength = entry[1]
70                         if entry[3] == 1:
71                                 # masked pins:
72                                 x = configElement_nonSave("", configSequence, [1234], configsequencearg.get("PINCODE", (self.pinlength, "*")))
73                         else:                           
74                                 # unmasked pins:
75                                 x = configElement_nonSave("", configSequence, [1234], configsequencearg.get("PINCODE", (self.pinlength, "")))
76                         self["subtitle"].setText(entry[2])
77                         self.pin = getConfigListEntry("",x)
78                         list.append( self.pin )
79                         self["bottom"].setText(_("please press OK when ready"))
80
81         def okbuttonClick(self):
82                 self.timer.stop()
83                 if not self.tag:
84                         return
85                 if self.tag == "WAIT":
86                         print "do nothing - wait"
87                 elif self.tag == "MENU":
88                         print "answer MENU"
89                         cur = self["entries"].getCurrent()
90                         if cur:
91                                 eDVBCI_UI.getInstance().answerMenu(self.slotid, cur[1])
92                         else:
93                                 eDVBCI_UI.getInstance().answerMenu(self.slotid, 0)
94                         self.showWait() 
95                 elif self.tag == "LIST":
96                         print "answer LIST"
97                         eDVBCI_UI.getInstance().answerMenu(self.slotid, 0)
98                         self.showWait() 
99                 elif self.tag == "ENQ":
100                         answer = str(self.pin[1].parent.value[0])
101                         length = len(answer)
102                         while length < self.pinlength:
103                                 answer = '0'+answer
104                                 length+=1
105                         eDVBCI_UI.getInstance().answerEnq(self.slotid, answer)
106                         self.showWait()
107
108         def closeMmi(self):
109                 self.timer.stop()
110                 self.close(self.slotid)
111
112         def keyCancel(self):
113                 self.timer.stop()
114                 if not self.tag:
115                         return
116                 if self.tag == "WAIT":
117                         eDVBCI_UI.getInstance().stopMMI(self.slotid)
118                         self.closeMmi()
119                 elif self.tag in [ "MENU", "LIST" ]:
120                         print "cancel list"
121                         eDVBCI_UI.getInstance().answerMenu(self.slotid, 0)
122                         self.showWait()
123                 elif self.tag == "ENQ":
124                         print "cancel enq"
125                         eDVBCI_UI.getInstance().cancelEnq(self.slotid)
126                         self.showWait()
127                 else:
128                         print "give cancel action to ci"        
129
130         def keyConfigEntry(self, key):
131                 self.timer.stop()
132                 try:
133                         self["entries"].handleKey(key)
134                 except:
135                         pass
136
137         def keyNumberGlobal(self, number):
138                 self.timer.stop()
139                 self.keyConfigEntry(config.key[str(number)])
140
141         def keyLeft(self):
142                 self.timer.stop()
143                 self.keyConfigEntry(config.key["prevElement"])
144
145         def keyRight(self):
146                 self.timer.stop()
147                 self.keyConfigEntry(config.key["nextElement"])
148
149         def updateList(self, list):
150                 List = self["entries"]
151                 try:
152                         List.instance.moveSelectionTo(0)
153                 except:
154                         List.l.setList(list)
155                         return
156
157                 if self.tag and self.tag == "ENQ":
158                         type = TYPE_CONFIG
159                 else:
160                         type = TYPE_MENU
161
162                 if type != self.listtype:
163                         if type == TYPE_CONFIG:
164                                 List.l = eListboxPythonConfigContent()
165                         else:
166                                 List.l = eListboxPythonStringContent()
167                         List.instance.setContent(List.l)
168                         self.listtype = type
169
170                 List.l.setList(list)
171
172         def showWait(self):
173                 self.tag = "WAIT"
174                 self["title"].setText("")
175                 self["subtitle"].setText("")
176                 self["bottom"].setText("")
177                 list = [ ]
178                 list.append( ("wait for ci...", 0) )
179                 self.updateList(list)
180
181         def showScreen(self):
182                 screen = eDVBCI_UI.getInstance().getMMIScreen(self.slotid)
183         
184                 list = [ ]
185
186                 self.timer.stop()
187                 if len(screen) > 0 and screen[0][0] == "CLOSE":
188                         timeout = screen[0][1]
189                         if timeout > 0:
190                                 self.timer.start(timeout*1000, True)
191                         else:
192                                 self.keyCancel()
193                 else:
194                         self.tag = screen[0][0]
195                         for entry in screen:
196                                 if entry[0] == "PIN":
197                                         self.addEntry(list, entry)
198                                 else:
199                                         if entry[0] == "TITLE":
200                                                 self["title"].setText(entry[1])
201                                         elif entry[0] == "SUBTITLE":
202                                                 self["subtitle"].setText(entry[1])
203                                         elif entry[0] == "BOTTOM":
204                                                 self["bottom"].setText(entry[1])
205                                         elif entry[0] == "TEXT":
206                                                 self.addEntry(list, entry)
207                         self.updateList(list)
208
209         def ciStateChanged(self):
210                 if self.action == 0:                    #reset
211                         self.closeMmi()
212                 if self.action == 1:                    #init
213                         self.closeMmi()
214
215                 #module still there ?                   
216                 if eDVBCI_UI.getInstance().getState(self.slotid) != 2:
217                         self.closeMmi()
218
219                 #mmi session still active ?                     
220                 if eDVBCI_UI.getInstance().getMMIState(self.slotid) != 1:
221                         self.closeMmi()
222
223                 if self.action > 1 and eDVBCI_UI.getInstance().availableMMI(self.slotid) == 1:
224                         self.showScreen()
225
226                 #FIXME: check for mmi-session closed    
227
228 class CiMessageHandler:
229         def __init__(self):
230                 self.session = None
231                 self.ci = { }
232                 self.dlgs = { }
233                 eDVBCI_UI.getInstance().ciStateChanged.get().append(self.ciStateChanged)
234
235         def setSession(self, session):
236                 self.session = session
237
238         def ciStateChanged(self, slot):
239                 if slot in self.ci:
240                         self.ci[slot](slot)
241                 else:
242                         if slot in self.dlgs:
243                                 self.dlgs[slot].ciStateChanged()
244                         elif eDVBCI_UI.getInstance().availableMMI(slot) == 1:
245                                 if self.session:
246                                         self.dlgs[slot] = self.session.openWithCallback(self.dlgClosed, CiMmi, slot, 3)
247                                 else:
248                                         print "no session"
249
250         def dlgClosed(self, slot):
251                 del self.dlgs[slot]
252
253         def registerCIMessageHandler(self, slot, func):
254                 self.unregisterCIMessageHandler(slot)
255                 self.ci[slot] = func
256
257         def unregisterCIMessageHandler(self, slot):
258                 if slot in self.ci:
259                         del self.ci[slot]
260
261 CiHandler = CiMessageHandler()
262
263 class CiSelection(Screen):
264         def __init__(self, session):
265                 Screen.__init__(self, session)
266
267                 self["actions"] = ActionMap(["OkCancelActions"],
268                         {
269                                 "ok": self.okbuttonClick,
270                                 "cancel": self.cancel
271                         })
272
273                 self.dlg = None
274                 self.state = { }
275                 self.list = [ ]
276
277                 for slot in range(4):
278                         state = eDVBCI_UI.getInstance().getState(slot)
279                         self.appendEntries(slot, state) # FIXME more than one CI
280                         CiHandler.registerCIMessageHandler(slot, self.ciStateChanged)
281
282                 menuList = MenuList(list)
283                 menuList.list = self.list
284                 menuList.l.setList(self.list)
285                 self["entries"] = menuList
286
287         def appendEntries(self, slot, state):
288                 self.state[slot] = state
289                 self.list.append( (_("Reset"), 0, slot) )
290                 self.list.append( (_("Init"), 1, slot) )
291
292                 if self.state[slot] == 0:                       #no module
293                         self.list.append( (_("no module found"), 2, slot) )
294                 elif self.state[slot] == 1:             #module in init
295                         self.list.append( (_("init module"), 2, slot) )
296                 elif self.state[slot] == 2:             #module ready
297                         #get appname
298                         appname = eDVBCI_UI.getInstance().getAppName(slot)
299                         self.list.append( (appname, 2, slot) )
300
301         def updateState(self, slot):
302                 state = eDVBCI_UI.getInstance().getState(slot)
303                 self.state[slot] = state
304
305                 slotidx=0
306                 while self.list[slotidx][2] != slot:
307                         slotidx += 1
308
309                 slotidx += 1 # do not change Reset
310                 slotidx += 1 # do not change Init
311
312                 if state == 0:                  #no module
313                         self.list[slotidx] = (_("no module found"), 2, slot)
314                 elif state == 1:                #module in init
315                         self.list[slotidx] = (_("init module"), 2, slot)
316                 elif state == 2:                #module ready
317                         #get appname
318                         appname = eDVBCI_UI.getInstance().getAppName(slot)
319                         self.list[slotidx] = (appname, 2, slot)
320
321                 lst = self["entries"]
322                 lst.list = self.list
323                 lst.l.setList(self.list)
324
325         def ciStateChanged(self, slot):
326                 if self.dlg:
327                         self.dlg.ciStateChanged()
328                 else:
329                         state = eDVBCI_UI.getInstance().getState(slot)
330                         if self.state[slot] != state:
331                                 #print "something happens"
332                                 self.state[slot] = state
333                                 self.updateState(slot)
334
335         def dlgClosed(self, slot):
336                 self.dlg = None
337
338         def okbuttonClick(self):
339                 cur = self["entries"].getCurrent()
340                 if cur:
341                         action = cur[1]
342                         slot = cur[2]
343                         if action == 0:         #reset
344                                 eDVBCI_UI.getInstance().setReset(slot)
345                         elif action == 1:               #init
346                                 eDVBCI_UI.getInstance().setInit(slot)
347                         elif self.state[slot] == 2:
348                                 self.dlg = self.session.openWithCallback(self.dlgClosed, CiMmi, slot, action)
349
350         def cancel(self):
351                 CiHandler.unregisterCIMessageHandler(0)
352                 self.close()