Merge branch 'master' of git://git.opendreambox.org/git/acidburn/enigma2-master
[enigma2.git] / lib / python / Plugins / SystemPlugins / CommonInterfaceAssignment / plugin.py
1 from Screens.Screen import Screen
2 from Screens.ChannelSelection import *
3 from Components.ActionMap import HelpableActionMap, ActionMap, NumberActionMap
4 from Components.Sources.List import List
5 from Components.Sources.StaticText import StaticText
6 from Components.config import ConfigNothing
7 from Components.ConfigList import ConfigList
8 from Components.Label import Label
9 from Components.SelectionList import SelectionList
10 from Components.MenuList import MenuList
11 from ServiceReference import ServiceReference
12 from Plugins.Plugin import PluginDescriptor
13 from xml.etree.cElementTree import parse as ci_parse
14 from Tools.XMLTools import elementsWithTag, mergeText, stringToXML
15 from enigma import eDVBCI_UI, eDVBCIInterfaces
16
17 from os import system, path as os_path
18
19 class CIselectMainMenu(Screen):
20         skin = """
21                 <screen position="180,150" size="350,270"  title="CI assignment" >
22                         <widget name="CiList" position="10,10" size="330,200" scrollbarMode="showOnDemand" />
23                         <ePixmap position="10,210" size="290,2" pixmap="skin_default/div-h.png" transparent="1" alphatest="on" />
24                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,220" size="140,40" alphatest="on" />
25                         <ePixmap pixmap="skin_default/buttons/green.png" position="160,220" size="140,40" alphatest="on" />
26                         <widget source="key_red" render="Label" position="10,220" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
27                         <widget source="key_green" render="Label" position="160,220" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
28                 </screen>"""
29
30         def __init__(self, session, args = 0):
31                 self.skin = CIselectMainMenu.skin
32                 Screen.__init__(self, session)
33
34                 self["key_red"] = StaticText(_("Cancel"))
35                 self["key_green"] = StaticText(_("Config"))
36
37                 self["actions"] = ActionMap(["ColorActions","SetupActions"],
38                         {
39                                 "green": self.greenPressed,
40                                 "red": self.redPressed,
41                                 "yellow": self.yellowPressed,
42                                 "ok": self.greenPressed,
43                                 "cancel": self.cancel
44                         }, -1)
45
46                 NUM_CI=eDVBCIInterfaces.getInstance().getNumOfSlots()
47
48                 print "[CI_Wizzard] FOUND %d CI Slots " % NUM_CI
49
50                 self.dlg = None
51                 self.state = { }
52                 self.list = [ ]
53                 if NUM_CI > 0:
54                         for slot in range(NUM_CI):
55                                 state = eDVBCI_UI.getInstance().getState(slot)
56                                 if state == 0:
57                                         appname = _("Slot %d") %(slot+1) + " - " + _("no module found")
58                                 elif state == 1:        
59                                         appname = _("Slot %d") %(slot+1) + " - " + _("init modules")
60                                 elif state == 2:
61                                         appname = _("Slot %d") %(slot+1) + " - " + eDVBCI_UI.getInstance().getAppName(slot)
62                                 self.list.append( (appname, ConfigNothing(), 0, slot) )
63                 else:
64                         self.list.append( (_("no CI slots found") , ConfigNothing(), 1, -1) )
65
66                 menuList = ConfigList(self.list)
67                 menuList.list = self.list
68                 menuList.l.setList(self.list)
69                 self["CiList"] = menuList
70                 self.onShown.append(self.setWindowTitle)
71
72         def setWindowTitle(self):
73                 self.setTitle(_("CI assignment"))
74
75         def greenPressed(self):
76                 cur = self["CiList"].getCurrent()
77                 if cur and len(cur) > 2:
78                         action = cur[2]
79                         slot = cur[3]
80                         if action == 1:
81                                 print "[CI_Wizzard] there is no CI Slot in your receiver"
82                         else:
83                                 print "[CI_Wizzard] selected CI Slot : %d" % slot
84                                 if config.usage.setup_level.index > 1: # advanced
85                                         self.session.open(CIconfigMenu, slot)
86                                 else:
87                                         self.session.open(easyCIconfigMenu, slot)
88
89         def yellowPressed(self):
90                 NUM_CI=eDVBCIInterfaces.getInstance().getNumOfSlots()
91                 print "[CI_Check] FOUND %d CI Slots " % NUM_CI
92                 if NUM_CI > 0:
93                         for ci in range(NUM_CI):
94                                 print eDVBCIInterfaces.getInstance().getDescrambleRules(ci)
95 #               else:
96 #                       print "no ci found"
97
98         def redPressed(self):
99                 print "[CI_Config] RED BUTTON not implemented yet - only use self.cancel()"
100                 self.cancel()
101
102         def cancel(self):
103                 self.close()
104
105 class CIconfigMenu(Screen):
106         skin = """
107                 <screen position="60,80" size="595,436" title="CI assignment" >
108                         <widget name="CAidList.desc" position="10,10" size="575,22" font="Regular;20" />
109                         <widget name="CAidList" position="10,40" size="575,45" font="Regular;20" />
110                         <widget name="ServiceList.desc" position="10,90" size="575,22" font="Regular;20" />
111                         <widget name="ServiceList" position="10,120" size="575,250" scrollbarMode="showOnDemand" />
112                         <ePixmap position="10,380" size="575,2" pixmap="skin_default/div-h.png" transparent="1" alphatest="on" />
113                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,390" size="140,40" alphatest="on" />
114                         <ePixmap pixmap="skin_default/buttons/green.png" position="155,390" size="140,40" alphatest="on" />
115                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="300,390" size="140,40" alphatest="on" />
116                         <ePixmap pixmap="skin_default/buttons/blue.png" position="445,390" size="140,40" alphatest="on" />
117                         <widget source="key_red" render="Label" position="10,390" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
118                         <widget source="key_green" render="Label" position="155,390" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
119                         <widget source="key_yellow" render="Label" position="300,390" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
120                         <widget source="key_blue" render="Label" position="445,390" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
121                 </screen>"""
122
123         def __init__(self, session, ci_slot="9"):
124                 self.skin = CIconfigMenu.skin
125                 Screen.__init__(self, session)
126                 self.ci_slot=ci_slot
127                 self.filename="/etc/enigma2/ci"+str(self.ci_slot)+".xml"
128
129                 self["key_red"] = StaticText(_("delete"))
130                 self["key_green"] = StaticText(_("add Service"))
131                 self["key_yellow"] = StaticText(_("add Provider"))
132                 self["key_blue"] = StaticText(_("select CAId"))
133                 self["CAidList.desc"] = Label(_("assigned CAIds"))
134                 self["ServiceList.desc"] = Label(_("assigned Services/Provider"))
135
136                 self["actions"] = ActionMap(["ColorActions","SetupActions"],
137                         {
138                                 "green": self.greenPressed,
139                                 "red": self.redPressed,
140                                 "yellow": self.yellowPressed,
141                                 "blue": self.bluePressed,
142                                 "ok": self.okPressed,
143                                 "cancel": self.cancel
144                         }, -1)
145
146                 print "[CI_Wizzard_Config] Configuring CI Slots : %d  " % self.ci_slot
147
148                 i=0
149                 self.caidlist=[]
150                 print eDVBCIInterfaces.getInstance().readCICaIds(self.ci_slot)
151                 for caid in eDVBCIInterfaces.getInstance().readCICaIds(self.ci_slot):
152                         i+=1
153                         self.caidlist.append((str(hex(int(caid))),str(caid),i))
154
155                 print "[CI_Wizzard_Config_CI%d] read following CAIds from CI: %s" %(self.ci_slot, self.caidlist)
156                 self.selectedcaid =[]
157                 self.servicelist = []
158                 self.caids=_("no CAId selected")
159                 self["CAidList"] = Label(self.caids)
160
161                 serviceList = ConfigList(self.servicelist)
162                 serviceList.list = self.servicelist
163                 serviceList.l.setList(self.servicelist)
164                 self["ServiceList"] = serviceList
165
166                 self.loadXML()
167                 # if config mode !=advanced autoselect any caid
168                 if config.usage.setup_level.index <= 1: # advanced
169                         self.selectedcaid=self.caidlist
170                 self.onShown.append(self.setWindowTitle)
171
172         def setWindowTitle(self):
173                 self.setTitle(_("CI assignment"))
174
175         def redPressed(self):
176                 self.delete()
177
178         def greenPressed(self):
179                 self.session.openWithCallback( self.finishedChannelSelection, myChannelSelection, None)
180
181         def yellowPressed(self):
182                 self.session.openWithCallback( self.finishedProviderSelection, myProviderSelection, None)
183
184         def bluePressed(self):
185                 self.session.openWithCallback(self.finishedCAidSelection, CAidSelect, self.caidlist, self.selectedcaid)
186
187         def okPressed(self):
188                 print "[CI_Config_CI%d] OK BUTTON not implemented yet" %self.ci_slot
189
190         def cancel(self):
191                 self.saveXML()
192                 activate_all(self)
193                 self.close()
194
195         def delete(self):
196                 cur = self["ServiceList"].getCurrent()
197                 if cur and len(cur) > 2:
198                         self.servicelist.remove(cur)
199                 self["ServiceList"].l.setList(self.servicelist)
200
201         def finishedChannelSelection(self, *args):
202                 if len(args):
203                         ref=args[0]
204                         service_ref = ServiceReference(ref)
205                         service_name = service_ref.getServiceName()
206                         if find_in_list(self.servicelist, service_name, 0)==False:
207                                 split_ref=service_ref.ref.toString().split(":")
208                                 if split_ref[0] == "1":#== dvb service und nicht muell von None
209                                         self.servicelist.append( (service_name , ConfigNothing(), 0, service_ref.ref.toString()) )
210                                         self["ServiceList"].l.setList(self.servicelist)
211
212         def finishedProviderSelection(self, *args):
213                 if len(args)>1: # bei nix selected kommt nur 1 arg zurueck (==None)
214                         name=args[0]
215                         dvbnamespace=args[1]
216                         if find_in_list(self.servicelist, name, 0)==False:
217                                 self.servicelist.append( (name , ConfigNothing(), 1, dvbnamespace) )
218                                 self["ServiceList"].l.setList(self.servicelist)
219
220         def finishedCAidSelection(self, *args):
221                 if len(args):
222                         self.selectedcaid=args[0]
223                         self.caids=""
224                         for item in self.selectedcaid:
225                                 if len(self.caids):
226                                         self.caids+= ", " + item[0]
227                                 else:
228                                         self.caids=item[0]
229                 else:
230                         self.selectedcaid=[]
231                         self.caids=_("no CAId selected")
232                 self["CAidList"].setText(self.caids)
233
234         def saveXML(self):
235                 try:
236                         fp = file(self.filename, 'w')
237                         fp.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n")
238                         fp.write("<ci>\n")
239                         fp.write("\t<slot>\n")
240                         fp.write("\t\t<id>%s</id>\n" % self.ci_slot)
241                         for item in self.selectedcaid:
242                                 if len(self.selectedcaid):
243                                         fp.write("\t\t<caid id=\"%s\" />\n" % item[0])
244                         for item in self.servicelist:
245                                 if len(self.servicelist):
246                                         if item[2]==1:
247                                                 fp.write("\t\t<provider name=\"%s\" dvbnamespace=\"%s\" />\n" % (item[0], item[3]))
248                                         else:
249                                                 fp.write("\t\t<service name=\"%s\" ref=\"%s\" />\n"  % (item[0], item[3]))
250                         fp.write("\t</slot>\n")
251                         fp.write("</ci>\n")
252                         fp.close()
253                 except:
254                         print "[CI_Config_CI%d] xml not written" %self.ci_slot
255                         os.unlink(self.filename)
256
257         def loadXML(self):
258                 if not os_path.exists(self.filename):
259                         return
260
261                 def getValue(definitions, default):
262                         ret = ""
263                         Len = len(definitions)
264                         return Len > 0 and definitions[Len-1].text or default
265
266                 try:
267                         tree = ci_parse(self.filename).getroot()
268                         self.read_services=[]
269                         self.read_providers=[]
270                         self.usingcaid=[]
271                         self.ci_config=[]
272 #                       for ci in tree.findall("ci"):
273                         for slot in tree.findall("slot"):
274                                 read_slot = getValue(slot.findall("id"), False).encode("UTF-8")
275                                 print "ci " + read_slot
276
277                                 i=0
278                                 for caid in slot.findall("caid"):
279                                         read_caid = caid.get("id").encode("UTF-8")
280                                         self.selectedcaid.append((str(read_caid),str(read_caid),i))
281                                         self.usingcaid.append(long(read_caid,16))
282                                         i+=1
283
284                                 for service in  slot.findall("service"):
285                                         read_service_name = service.get("name").encode("UTF-8")
286                                         read_service_ref = service.get("ref").encode("UTF-8")
287                                         self.read_services.append (read_service_ref)
288
289                                 for provider in  slot.findall("provider"):
290                                         read_provider_name = provider.get("name").encode("UTF-8")
291                                         read_provider_dvbname = provider.get("dvbnamespace").encode("UTF-8")
292                                         self.read_providers.append((read_provider_name,read_provider_dvbname))
293
294                                 self.ci_config.append((int(read_slot), (self.read_services, self.read_providers, self.usingcaid)))
295                 except:
296                         print "[CI_Config_CI%d] error parsing xml..." %self.ci_slot
297
298                 for item in self.read_services:
299                         if len(item):
300                                 self.finishedChannelSelection(item)
301
302                 for item in self.read_providers:
303                         if len(item):
304                                 self.finishedProviderSelection(item[0],item[1])
305
306                 print self.ci_config
307                 self.finishedCAidSelection(self.selectedcaid)
308                 self["ServiceList"].l.setList(self.servicelist)
309
310 class easyCIconfigMenu(CIconfigMenu):
311         skin = """
312                 <screen position="80,80" size="470,420" title="CI assignment" >
313                         <widget name="ServiceList.desc" position="10,10" size="420,22" font="Regular;20" />
314                         <widget name="ServiceList" position="10,40" size="450,340" scrollbarMode="showOnDemand" />
315                         <ePixmap position="10,360" size="450,2" pixmap="skin_default/div-h.png" transparent="1" alphatest="on" />
316                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,370" size="140,40" alphatest="on" />
317                         <ePixmap pixmap="skin_default/buttons/green.png" position="160,370" size="140,40" alphatest="on" />
318                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="310,370" size="140,40" alphatest="on" />
319                         <widget source="key_red" render="Label" position="10,370" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
320                         <widget source="key_green" render="Label" position="160,370" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
321                         <widget source="key_yellow" render="Label" position="310,370" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
322                 </screen>"""
323
324         def __init__(self, session, ci_slot="9"):
325                 ci=ci_slot
326                 CIconfigMenu.__init__(self, session, ci_slot)
327                 self.skin = easyCIconfigMenu.skin
328
329                 self["actions"] = ActionMap(["ColorActions","SetupActions"],
330                 {
331                         "green": self.greenPressed,
332                         "red": self.redPressed,
333                         "yellow": self.yellowPressed,
334                         "blue": self.bluePressed,
335                         "ok": self.okPressed,
336                         "cancel": self.cancel
337                 }, -1)
338
339         def bluePressed(self):
340                 print "do nothing"
341
342 class CAidSelect(Screen):
343         skin = """
344                 <screen position="210,140" size="310,290"  title="select CAId's" >
345                         <widget name="list" position="10,10" size="290,210" scrollbarMode="showOnDemand" />
346                         <ePixmap position="10,230" size="290,2" pixmap="skin_default/div-h.png" transparent="1" alphatest="on" />
347                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,240" size="140,40" alphatest="on" />
348                         <ePixmap pixmap="skin_default/buttons/green.png" position="160,240" size="140,40" alphatest="on" />
349                         <widget source="key_red" render="Label" position="10,240" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
350                         <widget source="key_green" render="Label" position="160,240" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
351                 </screen>"""
352
353         def __init__(self, session, list, selected_caids):
354                 self.skin = CAidSelect.skin
355                 Screen.__init__(self, session)
356
357                 self.list = SelectionList()
358                 self["list"] = self.list
359
360                 for listindex in range(len(list)):
361                         if find_in_list(selected_caids,list[listindex][0],0):
362                                 self.list.addSelection(list[listindex][0], list[listindex][1], listindex, True)
363                         else:
364                                 self.list.addSelection(list[listindex][0], list[listindex][1], listindex, False)
365
366                 self["key_red"] = StaticText(_("Cancel"))
367                 self["key_green"] = StaticText(_("Save"))
368
369                 self["actions"] = ActionMap(["ColorActions","SetupActions"],
370                 {
371                         "ok": self.list.toggleSelection, 
372                         "cancel": self.cancel, 
373                         "green": self.greenPressed,
374                         "red": self.cancel
375                 }, -1)
376                 self.onShown.append(self.setWindowTitle)
377
378         def setWindowTitle(self):
379                 self.setTitle(_("select CAId's"))
380
381         def greenPressed(self):
382                 list = self.list.getSelectionsList()
383                 print list
384                 self.close(list)
385
386         def cancel(self):
387                 self.close()
388
389 class myProviderSelection(ChannelSelectionBase):
390         skin = """
391                 <screen position="80,80" size="560,430" title="Select provider to add...">
392                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
393                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
394                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
395                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
396                         <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
397                         <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
398                         <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
399                         <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
400                         <widget name="list" position="00,45" size="560,364" scrollbarMode="showOnDemand" />
401                 </screen>"""
402
403         def __init__(self, session, title):
404                 ChannelSelectionBase.__init__(self, session)
405                 self.onShown.append(self.__onExecCallback)
406
407                 self["actions"] = ActionMap(["OkCancelActions", "ChannelSelectBaseActions"],
408                         {
409                                 "showFavourites": self.doNothing,
410                                 "showAllServices": self.doNothing,
411                                 "showProviders": self.doNothing,
412                                 "showSatellites": self.doNothing,
413                                 "cancel": self.cancel,
414                                 "ok": self.channelSelected,
415                         })
416                 self["key_red"] = StaticText(_(""))
417                 self["key_green"] = StaticText(_(""))
418                 self["key_yellow"] = StaticText(_(""))
419                 self["key_blue"] = StaticText(_(""))
420
421         def doNothing(self):
422                 print "nothing to do..."
423
424         def __onExecCallback(self):
425                 self.showSatellites()
426                 self.setTitle(_("Select provider to add..."))
427
428         def channelSelected(self): # just return selected service
429                 ref = self.getCurrentSelection()
430                 splited_ref=ref.toString().split(":")
431                 if ref.flags == 7 and splited_ref[6] != "0":
432                         self.dvbnamespace=splited_ref[6]
433                         self.enterPath(ref)
434                 else:
435                         self.close(ref.getName(), self.dvbnamespace)
436
437         def showSatellites(self):
438                 if not self.pathChangeDisabled:
439                         refstr = '%s FROM SATELLITES ORDER BY satellitePosition'%(self.service_types)
440                         if not self.preEnterPath(refstr):
441                                 ref = eServiceReference(refstr)
442                                 justSet=False
443                                 prev = None
444
445                                 if self.isBasePathEqual(ref):
446                                         if self.isPrevPathEqual(ref):
447                                                 justSet=True
448                                         prev = self.pathUp(justSet)
449                                 else:
450                                         currentRoot = self.getRoot()
451                                         if currentRoot is None or currentRoot != ref:
452                                                 justSet=True
453                                                 self.clearPath()
454                                                 self.enterPath(ref, True)
455                                 if justSet:
456                                         serviceHandler = eServiceCenter.getInstance()
457                                         servicelist = serviceHandler.list(ref)
458                                         if not servicelist is None:
459                                                 while True:
460                                                         service = servicelist.getNext()
461                                                         if not service.valid(): #check if end of list
462                                                                 break
463                                                         unsigned_orbpos = service.getUnsignedData(4) >> 16
464                                                         orbpos = service.getData(4) >> 16
465                                                         if orbpos < 0:
466                                                                 orbpos += 3600
467                                                         if service.getPath().find("FROM PROVIDER") != -1:
468                                                                 service_type = _("Providers")
469                                                                 try:
470                                                                         # why we need this cast?
471                                                                         service_name = str(nimmanager.getSatDescription(orbpos))
472                                                                 except:
473                                                                         if unsigned_orbpos == 0xFFFF: #Cable
474                                                                                 service_name = _("Cable")
475                                                                         elif unsigned_orbpos == 0xEEEE: #Terrestrial
476                                                                                 service_name = _("Terrestrial")
477                                                                         else:
478                                                                                 if orbpos > 1800: # west
479                                                                                         orbpos = 3600 - orbpos
480                                                                                         h = _("W")
481                                                                                 else:
482                                                                                         h = _("E")
483                                                                                 service_name = ("%d.%d" + h) % (orbpos / 10, orbpos % 10)
484                                                                 service.setName("%s - %s" % (service_name, service_type))
485                                                                 self.servicelist.addService(service)
486                                                 self.servicelist.finishFill()
487                                                 if prev is not None:
488                                                         self.setCurrentSelection(prev)
489
490         def cancel(self):
491                 self.close(None)
492
493 class myChannelSelection(ChannelSelectionBase):
494         skin = """
495                 <screen position="80,80" size="560,430" title="Select service to add...">
496                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
497                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
498                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
499                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
500                         <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
501                         <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
502                         <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
503                         <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
504                         <widget name="list" position="00,45" size="560,364" scrollbarMode="showOnDemand" />
505                 </screen>"""
506
507         def __init__(self, session, title):
508                 ChannelSelectionBase.__init__(self, session)
509                 self.onShown.append(self.__onExecCallback)
510                 service = self.session.nav.getCurrentService()
511                 if service:
512                         info = service.info()
513                         if info:
514                                 refstr = info.getInfoString(iServiceInformation.sServiceref)
515                                 self.servicelist.setPlayableIgnoreService(eServiceReference(refstr))
516
517                 self["actions"] = ActionMap(["OkCancelActions", "TvRadioActions", "ChannelSelectBaseActions"],
518                         {
519                                 "showProviders": self.doNothing,
520                                 "showSatellites": self.doNothing,
521                                 "cancel": self.cancel,
522                                 "ok": self.channelSelected,
523                                 "keyRadio": self.setModeRadio,
524                                 "keyTV": self.setModeTv
525                         })
526
527                 self["key_green"] = StaticText(_(""))
528                 self["key_yellow"] = StaticText(_(""))
529
530         def __onExecCallback(self):
531                 self.setModeTv()
532                 self.setTitle(_("Select service to add..."))
533
534         def doNothing(self):
535                 print "nothing to do..."
536
537         def channelSelected(self): # just return selected service
538                 ref = self.getCurrentSelection()
539                 if (ref.flags & 7) == 7:
540                         self.enterPath(ref)
541                 elif not (ref.flags & eServiceReference.isMarker):
542                         ref = self.getCurrentSelection()
543                         self.close(ref)
544
545         def setModeTv(self):
546                 self.setTvMode()
547                 self.showFavourites()
548
549         def setModeRadio(self):
550                 self.setRadioMode()
551                 self.showFavourites()
552
553         def cancel(self):
554                 self.close(None)
555
556 def activate_all(session):
557         NUM_CI=eDVBCIInterfaces.getInstance().getNumOfSlots()
558         print "[CI_Activate] FOUND %d CI Slots " % NUM_CI
559         if NUM_CI > 0:
560                 ci_config=[]
561                 def getValue(definitions, default):
562                         # Initialize Output
563                         ret = ""
564                         # How many definitions are present
565                         Len = len(definitions)
566                         return Len > 0 and definitions[Len-1].text or default   
567
568                 for ci in range(NUM_CI):
569                         filename="/etc/enigma2/ci"+str(ci)+".xml"
570
571                         if not os_path.exists(filename):
572                                 print "[CI_Activate_Config_CI%d] no config file found" %ci
573
574                         try:
575                                 tree = ci_parse(filename).getroot()
576                                 read_services=[]
577                                 read_providers=[]
578                                 usingcaid=[]
579                                 for slot in tree.findall("slot"):
580                                         read_slot = getValue(slot.findall("id"), False).encode("UTF-8")
581
582                                         for caid in slot.findall("caid"):
583                                                 read_caid = caid.get("id").encode("UTF-8")
584                                                 usingcaid.append(long(read_caid,16))
585
586                                         for service in slot.findall("service"):
587                                                 read_service_ref = service.get("ref").encode("UTF-8")
588                                                 read_services.append (read_service_ref)
589
590                                         for provider in slot.findall("provider"):
591                                                 read_provider_name = provider.get("name").encode("UTF-8")
592                                                 read_provider_dvbname = provider.get("dvbnamespace").encode("UTF-8")
593                                                 read_providers.append((read_provider_name,long(read_provider_dvbname,16)))
594
595                                         ci_config.append((int(read_slot), (read_services, read_providers, usingcaid)))
596                         except:
597                                 print "[CI_Activate_Config_CI%d] error parsing xml..." %ci
598
599                 for item in ci_config:
600                         print "[CI_Activate] activate CI%d with following settings:" %item[0]
601                         print item[0]
602                         print item[1]
603                         try:
604                                 eDVBCIInterfaces.getInstance().setDescrambleRules(item[0],item[1])
605                         except:
606                                 print "[CI_Activate_Config_CI%d] error setting DescrambleRules..." %item[0]
607
608 def find_in_list(list, search, listpos=0):
609         for item in list:
610                 if item[listpos]==search:
611                         return True
612         return False
613
614 global_session = None
615
616 def sessionstart(reason, session):
617         global global_session
618         global_session = session
619
620 def autostart(reason, **kwargs):
621         global global_session
622         if reason == 0:
623                 print "[CI_Assignment] activating ci configs:"
624                 activate_all(global_session)
625         elif reason == 1:
626                 global_session = None
627
628 def main(session, **kwargs):
629         session.open(CIselectMainMenu)
630
631 def menu(menuid, **kwargs):
632         if menuid == "setup" and eDVBCIInterfaces.getInstance().getNumOfSlots():
633                 return [(_("Common Interface Assignment"), main, "ci_assign", 11)]
634         return [ ]
635
636 def Plugins(**kwargs):
637         if config.usage.setup_level.index > 1:
638                 return [PluginDescriptor( where = PluginDescriptor.WHERE_SESSIONSTART, fnc = sessionstart ),
639                                 PluginDescriptor( where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart ),
640                                 PluginDescriptor( name = "CommonInterfaceAssignment", description = _("a gui to assign services/providers/caids to common interface modules"), where = PluginDescriptor.WHERE_MENU, fnc = menu )]
641         else:
642                 return [PluginDescriptor( where = PluginDescriptor.WHERE_SESSIONSTART, fnc = sessionstart ),
643                                 PluginDescriptor( where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart ),
644                                 PluginDescriptor( name = "CommonInterfaceAssignment", description = _("a gui to assign services/providers to common interface modules"), where = PluginDescriptor.WHERE_MENU, fnc = menu )]