Merge branch 'master' into dvd_scale_test
[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                         self.lcd_summary.updateProgress(self.scan.getProgress())
22                         if self.scan.isDone():
23                                 errcode = self.scan.getError()
24                                 
25                                 if errcode == 0:
26                                         self.state = self.Done
27                                 else:
28                                         self.state = self.Error
29                                         self.errorcode = errcode
30                                 self.network.setText("")
31                                 self.transponder.setText("")
32                         else:
33                                 self.text.setText(_("scan in progress - %d%% done!") % self.scan.getProgress() + ' ' + _("%d services found!") % (self.foundServices + self.scan.getNumServices()))
34                                 transponder = self.scan.getCurrentTransponder()
35                                 network = ""
36                                 tp_text = ""
37                                 if transponder:
38                                         tp_type = transponder.getSystem()
39                                         if not tp_type[0]:
40                                                 tp_type = tp_type[1]
41                                                 if tp_type == iDVBFrontend.feSatellite:
42                                                         network = _("Satellite")
43                                                         tp = transponder.getDVBS()
44                                                         if not tp[0]:
45                                                                 tp = tp[1]
46                                                                 orb_pos = tp.orbital_position
47                                                                 try:
48                                                                         sat_name = str(nimmgr.getSatDescription(orb_pos))
49                                                                 except KeyError:
50                                                                         sat_name = ""
51                                                                 if orb_pos > 1800: # west
52                                                                         orb_pos = 3600 - orb_pos
53                                                                         h = _("W")
54                                                                 else:
55                                                                         h = _("E")
56                                                                 if sat_name.find("%d.%d" % (orb_pos/10, orb_pos%10)) != -1:
57                                                                         network = sat_name
58                                                                 else:
59                                                                         network = ("%s %d.%d %s") % (sat_name, orb_pos / 10, orb_pos % 10, h)
60                                                                 tp_text = ("%s %s %d%c / %d / %s") %( { 0 : "DVB-S", 1 : "DVB-S2" }[tp.system],
61                                                                         { 0 : "Auto", 1 : "QPSK", 2 : "M8PSK", 3 : "QAM16" }[tp.modulation],
62                                                                         tp.frequency/1000,
63                                                                         { 0 : 'H', 1 : 'V', 2 : 'L', 3 : 'R' }[tp.polarisation],
64                                                                         tp.symbol_rate/1000,
65                                                                         { 0 : "AUTO", 1 : "1/2", 2 : "2/3", 3 : "3/4", 4 : "5/6",
66                                                                          5 : "7/8", 6 : "8/9", 7 : "3/5", 8 : "4/5", 9 : "9/10",
67                                                                          15 : "NONE" }[tp.fec] )
68                                                 elif tp_type == iDVBFrontend.feCable:
69                                                         network = _("Cable")
70                                                         tp = transponder.getDVBC()
71                                                         if not tp[0]:
72                                                                 tp = tp[1]
73                                                                 tp_text = ("DVB-C %s %d / %d / %s") %( { 0 : "AUTO", 1 : "QAM16", 2 : "QAM32", 3 : "QAM64", 4 : "QAM128", 5 : "QAM256" }[tp.modulation],
74                                                                         tp.frequency,
75                                                                         tp.symbol_rate/1000,
76                                                                         { 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] )
77                                                 elif tp_type == iDVBFrontend.feTerrestrial:
78                                                         network = _("Terrestrial")
79                                                         tp = transponder.getDVBT()
80                                                         if not tp[0]:
81                                                                 tp = tp[1]
82                                                                 tp_text = ("DVB-T %s %d %s") %( { 0 : "QPSK", 1 : "QAM16", 2 : "QAM64", 3 : "AUTO"}[tp.modulation],
83                                                                         tp.frequency,
84                                                                         { 0 : "Bw 8MHz", 1 : "Bw 7MHz", 2 : "Bw 6MHz", 3 : "Bw Auto" }[tp.bandwidth])
85                                                 else:
86                                                         print "unknown transponder type in scanStatusChanged"
87                                 self.network.setText(network)
88                                 self.transponder.setText(tp_text)
89                 
90                 if self.state == self.Done:
91                         if self.scan.getNumServices() == 0:
92                                 self.text.setText(_("scan done!") + ' ' + _("%d services found!") % 0 )
93                         else:
94                                 self.text.setText(_("scan done!") + ' ' + _("%d services found!") % (self.foundServices + self.scan.getNumServices()))
95                 
96                 if self.state == self.Error:
97                         self.text.setText(_("ERROR - failed to scan (%s)!") % (self.Errors[self.errorcode]) )
98                         
99                 if self.state == self.Done or self.state == self.Error:
100                         if self.run != len(self.scanList) - 1:
101                                 self.foundServices += self.scan.getNumServices()
102                                 self.execEnd()
103                                 self.run += 1
104                                 self.execBegin()
105         
106         def __init__(self, progressbar, text, servicelist, passNumber, scanList, network, transponder, frontendInfo, lcd_summary):
107                 self.foundServices = 0
108                 self.progressbar = progressbar
109                 self.text = text
110                 self.servicelist = servicelist
111                 self.passNumber = passNumber
112                 self.scanList = scanList
113                 self.frontendInfo = frontendInfo
114                 self.transponder = transponder
115                 self.network = network
116                 self.run = 0
117                 self.lcd_summary = lcd_summary
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                 self.lcd_summary.updateService(self.scan.getLastServiceName())
164
165         def destroy(self):
166                 pass