show current visible cislot number
[enigma2.git] / 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                 self["entries"].onSelectionChanged.append(self.selectionChanged)
277                 self["text"] = Label(_("Slot %d")%(1))
278
279         def selectionChanged(self):
280                 cur_idx = self["entries"].getCurrentIndex()
281                 self["text"].setText(_("Slot %d")%((cur_idx / 4)+1))
282
283         def keyConfigEntry(self, key):
284                 try:
285                         self["entries"].handleKey(key)
286                         self["entries"].getCurrent()[1].save()
287                 except:
288                         pass
289
290         def keyLeft(self):
291                 self.keyConfigEntry(KEY_LEFT)
292
293         def keyRight(self):
294                 self.keyConfigEntry(KEY_RIGHT)
295
296         def appendEntries(self, slot, state):
297                 self.state[slot] = state
298                 self.list.append( (_("Reset"), ConfigNothing(), 0, slot) )
299                 self.list.append( (_("Init"), ConfigNothing(), 1, slot) )
300
301                 if self.state[slot] == 0:                       #no module
302                         self.list.append( (_("no module found"), ConfigNothing(), 2, slot) )
303                 elif self.state[slot] == 1:             #module in init
304                         self.list.append( (_("init module"), ConfigNothing(), 2, slot) )
305                 elif self.state[slot] == 2:             #module ready
306                         #get appname
307                         appname = eDVBCI_UI.getInstance().getAppName(slot)
308                         self.list.append( (appname, ConfigNothing(), 2, slot) )
309
310                 self.list.append(getConfigListEntry(_("Multiple service support"), config.ci[slot].canDescrambleMultipleServices))
311
312         def updateState(self, slot):
313                 state = eDVBCI_UI.getInstance().getState(slot)
314                 self.state[slot] = state
315
316                 slotidx=0
317                 while len(self.list[slotidx]) < 3 or self.list[slotidx][3] != slot:
318                         slotidx += 1
319
320                 slotidx += 1 # do not change Reset
321                 slotidx += 1 # do not change Init
322
323                 if state == 0:                  #no module
324                         self.list[slotidx] = (_("no module found"), ConfigNothing(), 2, slot)
325                 elif state == 1:                #module in init
326                         self.list[slotidx] = (_("init module"), ConfigNothing(), 2, slot)
327                 elif state == 2:                #module ready
328                         #get appname
329                         appname = eDVBCI_UI.getInstance().getAppName(slot)
330                         self.list[slotidx] = (appname, ConfigNothing(), 2, slot)
331
332                 lst = self["entries"]
333                 lst.list = self.list
334                 lst.l.setList(self.list)
335
336         def ciStateChanged(self, slot):
337                 if self.dlg:
338                         self.dlg.ciStateChanged()
339                 else:
340                         state = eDVBCI_UI.getInstance().getState(slot)
341                         if self.state[slot] != state:
342                                 #print "something happens"
343                                 self.state[slot] = state
344                                 self.updateState(slot)
345
346         def dlgClosed(self, slot):
347                 self.dlg = None
348
349         def okbuttonClick(self):
350                 cur = self["entries"].getCurrent()
351                 if cur and len(cur) > 2:
352                         action = cur[2]
353                         slot = cur[3]
354                         if action == 0:         #reset
355                                 eDVBCI_UI.getInstance().setReset(slot)
356                         elif action == 1:               #init
357                                 eDVBCI_UI.getInstance().setInit(slot)
358                         elif self.state[slot] == 2:
359                                 self.dlg = self.session.openWithCallback(self.dlgClosed, CiMmi, slot, action)
360
361         def cancel(self):
362                 CiHandler.unregisterCIMessageHandler(0)
363                 self.close()