fix and simplify PluginList
[enigma2.git] / lib / python / Components / ServiceScan.py
1 from enigma import eComponentScan, iDVBFrontend
2 from Components.NimManager import nimmanager as nimmgr
3
4 class ServiceScan:
5         
6         Idle = 1
7         Running = 2
8         Done = 3
9         Error = 4
10         
11         Errors = { 
12                 0: "error starting scanning",
13                 1: "error while scanning",
14                 2: "no resource manager",
15                 3: "no channel list"
16                 }
17         
18         def scanStatusChanged(self):
19                 if self.state == self.Running:
20                         self.progressbar.setValue(self.scan.getProgress())
21                         if self.scan.isDone():
22                                 errcode = self.scan.getError()
23                                 
24                                 if errcode == 0:
25                                         self.state = self.Done
26                                 else:
27                                         self.state = self.Error
28                                         self.errorcode = errcode
29                                 self.network.setText("")
30                                 self.transponder.setText("")
31                         else:
32                                 self.text.setText(_("scan in progress - %d %% done! %d services found!") % (self.scan.getProgress(), self.foundServices + self.scan.getNumServices()))
33                                 transponder = self.scan.getCurrentTransponder()
34                                 network = ""
35                                 tp_text = ""
36                                 if transponder:
37                                         tp_type = transponder.getSystem()
38                                         if not tp_type[0]:
39                                                 tp_type = tp_type[1]
40                                                 if tp_type == iDVBFrontend.feSatellite:
41                                                         network = _("Satellite")
42                                                         tp = transponder.getDVBS()
43                                                         if not tp[0]:
44                                                                 tp = tp[1]
45                                                                 orb_pos = tp.orbital_position
46                                                                 try:
47                                                                         sat_name = str(nimmgr.getSatDescription(orb_pos))
48                                                                 except KeyError:
49                                                                         sat_name = ""
50                                                                 if orb_pos > 1800: # west
51                                                                         orb_pos = 3600 - orb_pos
52                                                                         h = _("W")
53                                                                 else:
54                                                                         h = _("E")
55                                                                 if sat_name.find("%d.%d" % (orb_pos/10, orb_pos%10)) != -1:
56                                                                         network = sat_name
57                                                                 else:
58                                                                         network = ("%s %d.%d %s") % (sat_name, orb_pos / 10, orb_pos % 10, h)
59                                                                 tp_text = ("%s %s %d%c / %d / %s") %( { 0 : "DVB-S", 1 : "DVB-S2" }[tp.system],
60                                                                         { 0 : "Auto", 1 : "QPSK", 2 : "M8PSK", 3 : "QAM16" }[tp.modulation],
61                                                                         tp.frequency/1000,
62                                                                         { 0 : 'H', 1 : 'V', 2 : 'L', 3 : 'R' }[tp.polarisation],
63                                                                         tp.symbol_rate/1000,
64                                                                         { 0 : "AUTO", 1 : "1/2", 2 : "2/3", 3 : "3/4", 4 : "5/6",
65                                                                          5 : "7/8", 6 : "8/9", 7 : "3/5", 8 : "4/5", 9 : "9/10",
66                                                                          15 : "NONE" }[tp.fec] )
67                                                 elif tp_type == iDVBFrontend.feCable:
68                                                         network = _("Cable")
69                                                         tp = transponder.getDVBC()
70                                                         if not tp[0]:
71                                                                 tp = tp[1]
72                                                                 tp_text = ("DVB-C %s %d / %d / %s") %( { 0 : "AUTO", 1 : "QAM16", 2 : "QAM32", 3 : "QAM64", 4 : "QAM128", 5 : "QAM256" }[tp.modulation],
73                                                                         tp.frequency,
74                                                                         tp.symbol_rate/1000,
75                                                                         { 0 : "AUTO", 1 : "1/2", 2 : "2/3", 3 : "3/4", 4 : "5/6", 5 : "7/8", 6 : "8/9", 15 : "NONE" }[tp.fec_inner] )
76                                                 elif tp_type == iDVBFrontend.feTerrestrial:
77                                                         network = _("Terrestrial")
78                                                         tp = transponder.getDVBT()
79                                                         if not tp[0]:
80                                                                 tp = tp[1]
81                                                                 tp_text = ("DVB-T %s %d %s") %( { 0 : "QPSK", 1 : "QAM16", 2 : "QAM64", 3 : "AUTO"}[tp.modulation],
82                                                                         tp.frequency,
83                                                                         { 0 : "Bw 8MHz", 1 : "Bw 7MHz", 2 : "Bw 6MHz", 3 : "Bw Auto" }[tp.bandwidth])
84                                                 else:
85                                                         print "unknown transponder type in scanStatusChanged"
86                                 self.network.setText(network)
87                                 self.transponder.setText(tp_text)
88                 
89                 if self.state == self.Done:
90                         if self.scan.getNumServices() == 1:
91                                 self.text.setText(_("scan done! One service found!"))
92                         elif self.scan.getNumServices() == 0:
93                                 self.text.setText(_("scan done! No service found!"))
94                         else:
95                                 self.text.setText(_("scan done! %d services found!") % (self.foundServices + self.scan.getNumServices()))
96                 
97                 if self.state == self.Error:
98                         self.text.setText(_("ERROR - failed to scan (%s)!") % (self.Errors[self.errorcode]) )
99                         
100                 if self.state == self.Done or self.state == self.Error:
101                         if self.run != len(self.scanList) - 1:
102                                 self.foundServices += self.scan.getNumServices()
103                                 self.execEnd()
104                                 self.run += 1
105                                 self.execBegin()
106         
107         def __init__(self, progressbar, text, servicelist, passNumber, scanList, network, transponder, frontendInfo):
108                 self.foundServices = 0
109                 self.progressbar = progressbar
110                 self.text = text
111                 self.servicelist = servicelist
112                 self.passNumber = passNumber
113                 self.scanList = scanList
114                 self.frontendInfo = frontendInfo
115                 self.transponder = transponder
116                 self.network = network
117                 self.run = 0
118
119         def doRun(self):
120                 self.scan = eComponentScan()
121                 self.frontendInfo.frontend_source = lambda : self.scan.getFrontend()
122                 self.feid = self.scanList[self.run]["feid"]
123                 self.flags = self.scanList[self.run]["flags"]
124                 self.state = self.Idle
125                 self.scanStatusChanged()
126                 
127                 for x in self.scanList[self.run]["transponders"]:
128                         self.scan.addInitial(x)
129
130         def updatePass(self):
131                 size = len(self.scanList)
132                 if size > 1:
133                         self.passNumber.setText(_("pass") + " " + str(self.run + 1) + "/" + str(size) + " (" + _("Tuner") + " " + str(self.scanList[self.run]["feid"]) + ")")
134                 
135         def execBegin(self):
136                 self.doRun()
137                 self.updatePass()
138                 self.scan.statusChanged.get().append(self.scanStatusChanged)
139                 self.scan.newService.get().append(self.newService)
140                 self.servicelist.clear()
141                 self.state = self.Running
142                 err = self.scan.start(self.feid, self.flags)
143                 self.frontendInfo.updateFrontendData()
144                 if err:
145                         self.state = self.Error
146                         self.errorcode = 0
147                 self.scanStatusChanged()
148         
149         def execEnd(self):
150                 self.scan.statusChanged.get().remove(self.scanStatusChanged)
151                 self.scan.newService.get().remove(self.newService)
152                 if not self.isDone():
153                         print "*** warning *** scan was not finished!"
154                 
155                 del self.scan
156
157         def isDone(self):
158                 return self.state == self.Done or self.state == self.Error
159
160         def newService(self):
161                 newServiceName = self.scan.getLastServiceName()
162                 self.servicelist.addItem(newServiceName)
163
164         def destroy(self):
165                 pass