Merge branch 'bug_532_fixed_servicelist_contextmenu_crash'
[enigma2.git] / lib / python / Plugins / SystemPlugins / Satfinder / plugin.py
1 from enigma import eDVBResourceManager,\
2         eDVBFrontendParametersSatellite, eDVBFrontendParameters
3
4 from Screens.Screen import Screen
5 from Screens.ScanSetup import ScanSetup
6 from Screens.MessageBox import MessageBox
7 from Plugins.Plugin import PluginDescriptor
8
9 from Components.Label import Label
10 from Components.Sources.FrontendStatus import FrontendStatus
11 from Components.ActionMap import ActionMap
12 from Components.NimManager import nimmanager, getConfigSatlist
13 from Components.MenuList import MenuList
14 from Components.config import ConfigSelection, getConfigListEntry
15 from Components.TuneTest import Tuner
16
17 class Satfinder(ScanSetup):
18         def openFrontend(self):
19                 res_mgr = eDVBResourceManager.getInstance()
20                 if res_mgr:
21                         self.raw_channel = res_mgr.allocateRawChannel(self.feid)
22                         if self.raw_channel:
23                                 self.frontend = self.raw_channel.getFrontend()
24                                 if self.frontend:
25                                         return True
26                                 else:
27                                         print "getFrontend failed"
28                         else:
29                                 print "getRawChannel failed"
30                 else:
31                         print "getResourceManager instance failed"
32                 return False
33
34         def __init__(self, session, feid):
35                 self.initcomplete = False
36                 self.feid = feid
37                 self.oldref = None
38
39                 if not self.openFrontend():
40                         self.oldref = session.nav.getCurrentlyPlayingServiceReference()
41                         session.nav.stopService() # try to disable foreground service
42                         if not self.openFrontend():
43                                 if session.pipshown: # try to disable pip
44                                         session.pipshown = False
45                                         del session.pip
46                                         if not self.openFrontend():
47                                                 self.frontend = None # in normal case this should not happen
48
49                 ScanSetup.__init__(self, session)
50                 self.tuner = Tuner(self.frontend)
51                 self["introduction"].setText("")
52                 self["Frontend"] = FrontendStatus(frontend_source = lambda : self.frontend, update_interval = 100)
53                 self.initcomplete = True
54                 self.onClose.append(self.__onClose)
55
56         def __onClose(self):
57                 self.session.nav.playService(self.oldref)
58
59         def createSetup(self):
60                 self.typeOfTuningEntry = None
61                 self.satEntry = None
62                 
63                 self.list = []
64
65                 self.typeOfTuningEntry = getConfigListEntry(_('Tune'), self.tuning_type)
66                 self.list.append(self.typeOfTuningEntry)
67                 self.satEntry = getConfigListEntry(_('Satellite'), self.tuning_sat)
68                 self.list.append(self.satEntry)
69
70                 nim = nimmanager.nim_slots[self.feid]
71
72                 self.systemEntry = None
73                 if self.tuning_type.value == "manual_transponder":
74                         if nim.isCompatible("DVB-S2"):
75                                 self.systemEntry = getConfigListEntry(_('System'), self.scan_sat.system)
76                                 self.list.append(self.systemEntry)
77                         else:
78                                 # downgrade to dvb-s, in case a -s2 config was active
79                                 self.scan_sat.system.value = eDVBFrontendParametersSatellite.System_DVB_S
80                         self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
81                         self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
82                         self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
83                         self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
84                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
85                                 self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
86                         elif self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
87                                 self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec_s2))
88                                 self.modulationEntry = getConfigListEntry(_('Modulation'), self.scan_sat.modulation)
89                                 self.list.append(self.modulationEntry)
90                                 self.list.append(getConfigListEntry(_('Rolloff'), self.scan_sat.rolloff))
91                                 self.list.append(getConfigListEntry(_('Pilot'), self.scan_sat.pilot))
92                 elif self.tuning_transponder and self.tuning_type.value == "predefined_transponder":
93                         self.list.append(getConfigListEntry(_("Transponder"), self.tuning_transponder))
94                 self["config"].list = self.list
95                 self["config"].l.setList(self.list)
96
97         def newConfig(self):
98                 cur = self["config"].getCurrent()
99                 if cur in (self.typeOfTuningEntry, self.systemEntry):
100                         self.createSetup()
101                 elif cur == self.satEntry:
102                         self.updateSats()
103                         self.createSetup()
104
105         def sat_changed(self, config_element):
106                 self.newConfig()
107                 self.retune(config_element)
108
109         def retune(self, configElement):
110                 returnvalue = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
111                 satpos = int(self.tuning_sat.value)
112                 if self.tuning_type.value == "manual_transponder":
113                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
114                                 fec = self.scan_sat.fec_s2.value
115                         else:
116                                 fec = self.scan_sat.fec.value
117                         returnvalue = (
118                                 self.scan_sat.frequency.value,
119                                 self.scan_sat.symbolrate.value,
120                                 self.scan_sat.polarization.value,
121                                 fec,
122                                 self.scan_sat.inversion.value,
123                                 satpos,
124                                 self.scan_sat.system.value,
125                                 self.scan_sat.modulation.value,
126                                 self.scan_sat.rolloff.value,
127                                 self.scan_sat.pilot.value)
128                         self.tune(returnvalue)
129                 elif self.tuning_type.value == "predefined_transponder":
130                         tps = nimmanager.getTransponders(satpos)
131                         l = len(tps)
132                         if l > self.tuning_transponder.index:
133                                 transponder = tps[self.tuning_transponder.index]
134                                 returnvalue = (transponder[1] / 1000, transponder[2] / 1000,
135                                         transponder[3], transponder[4], 2, satpos, transponder[5], transponder[6], transponder[8], transponder[9])
136                                 self.tune(returnvalue)
137
138         def createConfig(self, foo):
139                 self.tuning_transponder = None
140                 self.tuning_type = ConfigSelection(choices = [("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined transponder"))])
141                 self.tuning_sat = getConfigSatlist(192, nimmanager.getSatListForNim(self.feid))
142                 ScanSetup.createConfig(self, None)
143                 
144                 self.updateSats()
145
146                 for x in (self.tuning_type, self.tuning_sat, self.scan_sat.frequency,
147                         self.scan_sat.inversion, self.scan_sat.symbolrate,
148                         self.scan_sat.polarization, self.scan_sat.fec, self.scan_sat.pilot,
149                         self.scan_sat.fec_s2, self.scan_sat.fec, self.scan_sat.modulation,
150                         self.scan_sat.rolloff, self.scan_sat.system):
151                         x.addNotifier(self.retune, initial_call = False)
152
153         def updateSats(self):
154                 orb_pos = self.tuning_sat.orbital_position
155                 if orb_pos is not None:
156                         transponderlist = nimmanager.getTransponders(orb_pos)
157                         list = []
158                         default = None
159                         index = 0
160                         for x in transponderlist:
161                                 if x[3] == 0:
162                                         pol = "H"
163                                 elif x[3] == 1:
164                                         pol = "V"
165                                 elif x[3] == 2:
166                                         pol = "CL"
167                                 elif x[3] == 3:
168                                         pol = "CR"
169                                 else:
170                                         pol = "??"
171                                 if x[4] == 0:
172                                         fec = "FEC Auto"
173                                 elif x[4] == 1:
174                                         fec = "FEC 1/2"
175                                 elif x[4] == 2:
176                                         fec = "FEC 2/3"
177                                 elif x[4] == 3:
178                                         fec = "FEC 3/4"
179                                 elif x[4] == 4:
180                                         fec = "FEC 5/6"
181                                 elif x[4] == 5:
182                                         fec = "FEC 7/8"
183                                 elif x[4] == 6:
184                                         fec = "FEC 8/9"
185                                 elif x[4] == 7:
186                                         fec = "FEC 3/5"
187                                 elif x[4] == 8:
188                                         fec = "FEC 4/5"
189                                 elif x[4] == 9:
190                                         fec = "FEC 9/10"
191                                 elif x[4] == 15:
192                                         fec = "FEC None"
193                                 else:
194                                         fec = "FEC Unknown"
195                                 e = str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec
196                                 if default is None:
197                                         default = str(index)
198                                 list.append((str(index), e))
199                                 index += 1
200                         self.tuning_transponder = ConfigSelection(choices = list, default = default)
201                         self.tuning_transponder.addNotifier(self.retune, initial_call = False)
202
203         def keyGo(self):
204                 self.retune(self.tuning_type)
205
206         def restartPrevService(self, yesno):
207                 if yesno:
208                         if self.frontend:
209                                 self.frontend = None
210                                 del self.raw_channel
211                 else:
212                         self.oldref = None
213                 self.close(None)
214
215         def keyCancel(self):
216                 if self.oldref:
217                         self.session.openWithCallback(self.restartPrevService, MessageBox, _("Zap back to service before satfinder?"), MessageBox.TYPE_YESNO)
218                 else:
219                         self.restartPrevService(False)
220
221         def tune(self, transponder):
222                 if self.initcomplete:
223                         if transponder is not None:
224                                 self.tuner.tune(transponder)
225
226 class SatNimSelection(Screen):
227         skin = """
228                 <screen position="140,165" size="400,130" title="select Slot">
229                         <widget name="nimlist" position="20,10" size="360,100" />
230                 </screen>"""
231                 
232         def __init__(self, session):
233                 Screen.__init__(self, session)
234
235                 nimlist = nimmanager.getNimListOfType("DVB-S")
236                 nimMenuList = []
237                 for x in nimlist:
238                         nimMenuList.append((nimmanager.nim_slots[x].friendly_full_description, x))
239
240                 self["nimlist"] = MenuList(nimMenuList)
241
242                 self["actions"] = ActionMap(["OkCancelActions"],
243                 {
244                         "ok": self.okbuttonClick ,
245                         "cancel": self.close
246                 }, -1)
247
248         def okbuttonClick(self):
249                 selection = self["nimlist"].getCurrent()[1]
250                 self.session.open(Satfinder, selection)
251
252 def SatfinderMain(session, **kwargs):
253         nims = nimmanager.getNimListOfType("DVB-S")
254
255         nimList = []
256         for x in nims:
257                 if not nimmanager.getNimConfig(x).configMode.value in ("loopthrough", "satposdepends", "nothing"):
258                         nimList.append(x)
259
260         if len(nimList) == 0:
261                 session.open(MessageBox, _("No satellite frontend found!!"), MessageBox.TYPE_ERROR)
262         else:
263                 if session.nav.RecordTimer.isRecording():
264                         session.open(MessageBox, _("A recording is currently running. Please stop the recording before trying to start the satfinder."), MessageBox.TYPE_ERROR)
265                 else:
266                         if len(nimList) == 1:
267                                 session.open(Satfinder, nimList[0])
268                         else:
269                                 session.open(SatNimSelection)
270
271 def SatfinderStart(menuid, **kwargs):
272         if menuid == "scan":
273                 return [(_("Satfinder"), SatfinderMain, "satfinder", None)]
274         else:
275                 return []
276
277 def Plugins(**kwargs):
278         if (nimmanager.hasNimType("DVB-S")):
279                 return PluginDescriptor(name=_("Satfinder"), description="Helps setting up your dish", where = PluginDescriptor.WHERE_MENU, fnc=SatfinderStart)
280         else:
281                 return []