fix stupid code
[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                 if slot in self.dlgs:
239                         del self.dlgs[slot]
240
241         def registerCIMessageHandler(self, slot, func):
242                 self.unregisterCIMessageHandler(slot)
243                 self.ci[slot] = func
244
245         def unregisterCIMessageHandler(self, slot):
246                 if slot in self.ci:
247                         del self.ci[slot]
248
249 CiHandler = CiMessageHandler()
250
251 class CiSelection(Screen):
252         def __init__(self, session):
253                 Screen.__init__(self, session)
254
255                 self["actions"] = ActionMap(["OkCancelActions", "CiSelectionActions"],
256                         {
257                                 "left": self.keyLeft,
258                                 "right": self.keyLeft,
259                                 "ok": self.okbuttonClick,
260                                 "cancel": self.cancel
261                         },-1)
262
263                 self.dlg = None
264                 self.state = { }
265                 self.list = [ ]
266
267                 for slot in range(MAX_NUM_CI):
268                         state = eDVBCI_UI.getInstance().getState(slot)
269                         if state != -1:
270                                 self.appendEntries(slot, state)
271                                 CiHandler.registerCIMessageHandler(slot, self.ciStateChanged)
272
273                 menuList = ConfigList(self.list)
274                 menuList.list = self.list
275                 menuList.l.setList(self.list)
276                 self["entries"] = menuList
277                 self["entries"].onSelectionChanged.append(self.selectionChanged)
278                 self["text"] = Label(_("Slot %d")%(1))
279
280         def selectionChanged(self):
281                 cur_idx = self["entries"].getCurrentIndex()
282                 self["text"].setText(_("Slot %d")%((cur_idx / 4)+1))
283
284         def keyConfigEntry(self, key):
285                 try:
286                         self["entries"].handleKey(key)
287                         self["entries"].getCurrent()[1].save()
288                 except:
289                         pass
290
291         def keyLeft(self):
292                 self.keyConfigEntry(KEY_LEFT)
293
294         def keyRight(self):
295                 self.keyConfigEntry(KEY_RIGHT)
296
297         def appendEntries(self, slot, state):
298                 self.state[slot] = state
299                 self.list.append( (_("Reset"), ConfigNothing(), 0, slot) )
300                 self.list.append( (_("Init"), ConfigNothing(), 1, slot) )
301
302                 if self.state[slot] == 0:                       #no module
303                         self.list.append( (_("no module found"), ConfigNothing(), 2, slot) )
304                 elif self.state[slot] == 1:             #module in init
305                         self.list.append( (_("init module"), ConfigNothing(), 2, slot) )
306                 elif self.state[slot] == 2:             #module ready
307                         #get appname
308                         appname = eDVBCI_UI.getInstance().getAppName(slot)
309                         self.list.append( (appname, ConfigNothing(), 2, slot) )
310
311                 self.list.append(getConfigListEntry(_("Multiple service support"), config.ci[slot].canDescrambleMultipleServices))
312
313         def updateState(self, slot):
314                 state = eDVBCI_UI.getInstance().getState(slot)
315                 self.state[slot] = state
316
317                 slotidx=0
318                 while len(self.list[slotidx]) < 3 or self.list[slotidx][3] != slot:
319                         slotidx += 1
320
321                 slotidx += 1 # do not change Reset
322                 slotidx += 1 # do not change Init
323
324                 if state == 0:                  #no module
325                         self.list[slotidx] = (_("no module found"), ConfigNothing(), 2, slot)
326                 elif state == 1:                #module in init
327                         self.list[slotidx] = (_("init module"), ConfigNothing(), 2, slot)
328                 elif state == 2:                #module ready
329                         #get appname
330                         appname = eDVBCI_UI.getInstance().getAppName(slot)
331                         self.list[slotidx] = (appname, ConfigNothing(), 2, slot)
332
333                 lst = self["entries"]
334                 lst.list = self.list
335                 lst.l.setList(self.list)
336
337         def ciStateChanged(self, slot):
338                 if self.dlg:
339                         self.dlg.ciStateChanged()
340                 else:
341                         state = eDVBCI_UI.getInstance().getState(slot)
342                         if self.state[slot] != state:
343                                 #print "something happens"
344                                 self.state[slot] = state
345                                 self.updateState(slot)
346
347         def dlgClosed(self, slot):
348                 self.dlg = None
349
350         def okbuttonClick(self):
351                 cur = self["entries"].getCurrent()
352                 if cur and len(cur) > 2:
353                         action = cur[2]
354                         slot = cur[3]
355                         if action == 0:         #reset
356                                 eDVBCI_UI.getInstance().setReset(slot)
357                         elif action == 1:               #init
358                                 eDVBCI_UI.getInstance().setInit(slot)
359                         elif self.state[slot] == 2:
360                                 self.dlg = self.session.openWithCallback(self.dlgClosed, CiMmi, slot, action)
361
362         def cancel(self):
363                 CiHandler.unregisterCIMessageHandler(0)
364                 self.close()