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