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