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