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