servicemp3.cpp: more simple/flexible streaming detection
[enigma2.git] / lib / python / Plugins / SystemPlugins / NetworkWizard / NetworkWizard.py
1 from Screens.Wizard import wizardManager, WizardSummary
2 from Screens.WizardLanguage import WizardLanguage
3 from Screens.Rc import Rc
4 from Screens.MessageBox import MessageBox
5 from Components.Pixmap import Pixmap, MovingPixmap, MultiPixmap
6 from Components.Sources.Boolean import Boolean
7 from Components.Network import iNetwork
8 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
9 from enigma import eTimer
10 from os import system
11
12 class NetworkWizard(WizardLanguage, Rc):
13         skin = """
14                 <screen position="0,0" size="720,576" title="Welcome..." flags="wfNoBorder" >
15                         <widget name="text" position="153,40" size="340,300" font="Regular;22" />
16                         <widget source="list" render="Listbox" position="53,340" size="440,180" scrollbarMode="showOnDemand" >
17                                 <convert type="StringList" />
18                         </widget>
19                         <widget name="config" position="53,340" zPosition="1" size="440,180" transparent="1" scrollbarMode="showOnDemand" />
20                         <ePixmap pixmap="skin_default/buttons/button_red.png" position="40,225" zPosition="0" size="15,16" transparent="1" alphatest="on" />
21                         <widget name="languagetext" position="55,225" size="95,30" font="Regular;18" />
22                         <widget name="wizard" pixmap="skin_default/wizard.png" position="40,50" zPosition="10" size="110,174" alphatest="on" />
23                         <widget name="rc" pixmaps="skin_default/rc.png,skin_default/rcold.png" position="500,50" zPosition="10" size="154,500" alphatest="on" />
24                         <widget name="arrowdown" pixmap="skin_default/arrowdown.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
25                         <widget name="arrowdown2" pixmap="skin_default/arrowdown.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
26                         <widget name="arrowup" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
27                         <widget name="arrowup2" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" alphatest="on" />
28                         <widget source="VKeyIcon" render="Pixmap" pixmap="skin_default/buttons/key_text.png" position="40,260" zPosition="0" size="35,25" transparent="1" alphatest="on" >
29                                 <convert type="ConditionalShowHide" />
30                         </widget>
31                         <widget name="HelpWindow" pixmap="skin_default/buttons/key_text.png" position="125,170" zPosition="1" size="1,1" transparent="1" alphatest="on" />      
32                 </screen>"""
33         def __init__(self, session, interface = None):
34                 self.xmlfile = resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkWizard/networkwizard.xml")
35                 WizardLanguage.__init__(self, session, showSteps = False, showStepSlider = False)
36                 Rc.__init__(self)
37                 self.session = session
38                 self["wizard"] = Pixmap()
39                 self["HelpWindow"] = Pixmap()
40                 self["HelpWindow"].hide()
41                 self["VKeyIcon"] = Boolean(False)
42
43                 self.InstalledInterfaceCount = None
44                 self.Adapterlist = None
45                 self.InterfaceState = None
46                 self.isInterfaceUp = None
47                 self.WlanPluginInstalled = False
48                 self.ap = None
49                 self.w = None
50                 if interface is not None:
51                         self.selectedInterface = interface
52                 else:
53                         self.selectedInterface = None
54                 self.NextStep = None
55                 self.resetRef = None
56                 self.checkRef = None
57                 self.AdapterRef = None
58                 self.APList = None
59                 self.newAPlist = None
60                 self.oldlist = None
61                 
62                 self.originalInterfaceState = {}
63                 self.originalInterfaceStateChanged = False
64                 self.Text = None
65                 self.rescanTimer = eTimer()
66                 self.rescanTimer.callback.append(self.rescanTimerFired)
67                 self.getInstalledInterfaceCount()
68                 self.isWlanPluginInstalled()
69                 
70         def exitWizardQuestion(self, ret = False):
71                 if (ret):
72                         self.markDone()
73                         self.close()
74                 
75         def markDone(self):
76                 self.stopScan()
77                 del self.rescanTimer
78                 self.checkOldInterfaceState()
79                 pass
80
81         def back(self):
82                 self.stopScan()
83                 self.ap = None
84                 WizardLanguage.back(self)
85                 
86         def stopScan(self):
87                 self.rescanTimer.stop()
88                 if self.w is not None:
89                         from Plugins.SystemPlugins.WirelessLan.Wlan import iWlan
90                         iWlan.stopGetNetworkList()
91                         self.w = None
92
93         def getInstalledInterfaceCount(self):
94                 self.originalInterfaceState = {}
95                 self.Adapterlist = iNetwork.getAdapterList()
96                 self.InstalledInterfaceCount = len(self.Adapterlist)
97                 if self.Adapterlist is not None:
98                         if self.InstalledInterfaceCount == 1 and self.selectedInterface is None:
99                                         self.selectedInterface = self.Adapterlist[0]
100                 for interface in iNetwork.getAdapterList():
101                         self.originalInterfaceState[interface] = {}
102                         self.originalInterfaceState[interface]["up"] = iNetwork.getAdapterAttribute(interface, 'up')
103
104         def selectInterface(self):
105                 self.InterfaceState = None
106                 if self.selectedInterface is None:
107                         if self.InstalledInterfaceCount <= 1:
108                                 if not iNetwork.isWirelessInterface(self.selectedInterface):
109                                         self.NextStep = 'nwconfig'
110                                 else:
111                                         self.NextStep = 'asknetworktype'
112                                 self.checkInterface(self.selectedInterface)
113                         else:
114                                 self.NextStep = 'selectinterface'
115                                 self.currStep = self.getStepWithID(self.NextStep)
116                                 self.afterAsyncCode()
117                 else:
118                         if not iNetwork.isWirelessInterface(self.selectedInterface):
119                                 self.NextStep = 'nwconfig'
120                         else:
121                                 self.NextStep = 'asknetworktype'
122                         self.checkInterface(self.selectedInterface)
123
124         def checkOldInterfaceState(self):
125                 # disable up interface if it was originally down and config is unchanged.
126                 if self.originalInterfaceStateChanged is False:
127                         for interface in self.originalInterfaceState.keys():
128                                 if interface == self.selectedInterface:
129                                         if self.originalInterfaceState[interface]["up"] is False:
130                                                 if iNetwork.checkforInterface(interface) is True:
131                                                         system("ifconfig " + interface + " down")
132
133         def listInterfaces(self):
134                 self.checkOldInterfaceState()
135                 list = [(iNetwork.getFriendlyAdapterName(x),x) for x in iNetwork.getAdapterList()]
136                 list.append((_("Exit network wizard"), "end"))
137                 return list
138
139         def InterfaceSelectionMade(self, index):
140                 self.selectedInterface = index
141                 self.InterfaceSelect(index)
142
143         def InterfaceSelect(self, index):
144                 if index == 'end':
145                         self.NextStep = 'end'
146                 elif index == 'eth0':
147                         self.NextStep = 'nwconfig'
148                 else:
149                         self.NextStep = 'asknetworktype'
150
151         def InterfaceSelectionMoved(self):
152                 self.InterfaceSelect(self.selection)
153                 
154         def checkInterface(self,iface):
155                 self.stopScan()
156                 if self.Adapterlist is None:
157                         self.Adapterlist = iNetwork.getAdapterList()
158                 if self.NextStep is not 'end':
159                         if len(self.Adapterlist) == 0:
160                                 #Reset Network to defaults if network broken
161                                 iNetwork.resetNetworkConfig('lan', self.resetNetworkConfigCB)
162                                 self.resetRef = self.session.openWithCallback(self.resetNetworkConfigFinished, MessageBox, _("Please wait while we prepare your network interfaces..."), type = MessageBox.TYPE_INFO, enable_input = False)
163                         if iface in iNetwork.getInstalledAdapters():
164                                 if iface in iNetwork.configuredNetworkAdapters and len(iNetwork.configuredNetworkAdapters) == 1:
165                                         if iNetwork.getAdapterAttribute(iface, 'up') is True:
166                                                 self.isInterfaceUp = True
167                                         else:
168                                                 self.isInterfaceUp = False
169                                         self.currStep = self.getStepWithID(self.NextStep)
170                                         self.afterAsyncCode()
171                                 else:
172                                         self.isInterfaceUp = iNetwork.checkforInterface(iface)
173                                         self.currStep = self.getStepWithID(self.NextStep)
174                                         self.afterAsyncCode()
175                 else:
176                         self.resetNetworkConfigFinished(False)
177                         
178         def resetNetworkConfigFinished(self,data):
179                 if data is True:
180                         self.currStep = self.getStepWithID(self.NextStep)
181                         self.afterAsyncCode()
182                 else:
183                         self.currStep = self.getStepWithID(self.NextStep)
184                         self.afterAsyncCode()
185
186         def resetNetworkConfigCB(self,callback,iface):
187                 if callback is not None:
188                         if callback is True:
189                                 iNetwork.getInterfaces(self.getInterfacesFinished)
190                                 
191         def getInterfacesFinished(self, data):
192                 if data is True:
193                         if iNetwork.getAdapterAttribute(self.selectedInterface, 'up') is True:
194                                 self.isInterfaceUp = True
195                         else:
196                                 self.isInterfaceUp = False
197                         self.resetRef.close(True)
198                 else:
199                         print "we should never come here!"
200
201         def AdapterSetupEnd(self, iface):
202                 self.originalInterfaceStateChanged = True
203                 if iNetwork.getAdapterAttribute(iface, "dhcp") is True:
204                         iNetwork.checkNetworkState(self.AdapterSetupEndFinished)
205                         self.AdapterRef = self.session.openWithCallback(self.AdapterSetupEndCB, MessageBox, _("Please wait while we test your network..."), type = MessageBox.TYPE_INFO, enable_input = False)
206                 else:
207                         self.currStep = self.getStepWithID("confdns")
208                         self.afterAsyncCode()
209
210         def AdapterSetupEndCB(self,data):
211                 if data is True:
212                         if iNetwork.isWirelessInterface(self.selectedInterface):
213                                 if self.WlanPluginInstalled == True:
214                                         from Plugins.SystemPlugins.WirelessLan.Wlan import iStatus
215                                         iStatus.getDataForInterface(self.selectedInterface,self.checkWlanStateCB)
216                                 else:
217                                         self.currStep = self.getStepWithID("checklanstatusend")
218                                         self.afterAsyncCode()                                   
219                         else:
220                                 self.currStep = self.getStepWithID("checklanstatusend")
221                                 self.afterAsyncCode()
222
223         def AdapterSetupEndFinished(self,data):
224                 if data <= 2:
225                         self.InterfaceState = True
226                 else:
227                         self.InterfaceState = False
228                 self.AdapterRef.close(True)
229                         
230         def checkWlanStateCB(self,data,status):
231                 if data is not None:
232                         if data is True:
233                                 if status is not None:
234                                         text1 = _("Your Dreambox is now ready to use.\n\nYour internet connection is working now.\n\n")
235                                         text2 = _('Accesspoint:') + "\t" + str(status[self.selectedInterface]["accesspoint"]) + "\n"
236                                         text3 = _('SSID:') + "\t" + str(status[self.selectedInterface]["essid"]) + "\n"
237                                         text4 = _('Link Quality:') + "\t" + str(status[self.selectedInterface]["quality"])+ "\n"
238                                         text5 = _('Signal Strength:') + "\t" + str(status[self.selectedInterface]["signal"]) + "\n"
239                                         text6 = _('Bitrate:') + "\t" + str(status[self.selectedInterface]["bitrate"]) + "\n"
240                                         text7 = _('Encryption:') + " " + str(status[self.selectedInterface]["encryption"]) + "\n"
241                                         text8 = _("Please press OK to continue.")
242                                         infotext = text1 + text2 + text3 + text4 + text5 + text7 +"\n" + text8
243                                         self.currStep = self.getStepWithID("checkWlanstatusend")
244                                         self.Text = infotext
245                                         if str(status[self.selectedInterface]["accesspoint"]) == "Not-Associated":
246                                                 self.InterfaceState = False
247                                         self.afterAsyncCode()
248
249         def checkNetwork(self):
250                 iNetwork.checkNetworkState(self.checkNetworkStateCB)
251                 self.checkRef = self.session.openWithCallback(self.checkNetworkCB, MessageBox, _("Please wait while we test your network..."), type = MessageBox.TYPE_INFO, enable_input = False)
252
253         def checkNetworkCB(self,data):
254                 if data is True:
255                         if iNetwork.isWirelessInterface(self.selectedInterface):
256                                 if self.WlanPluginInstalled == True:
257                                         from Plugins.SystemPlugins.WirelessLan.Wlan import iStatus
258                                         iStatus.getDataForInterface(self.selectedInterface,self.checkWlanStateCB)
259                                 else:
260                                         self.currStep = self.getStepWithID("checklanstatusend")
261                                         self.afterAsyncCode()                                   
262                         else:
263                                 self.currStep = self.getStepWithID("checklanstatusend")
264                                 self.afterAsyncCode()
265
266         def checkNetworkStateCB(self,data):
267                 if data <= 2:
268                         self.InterfaceState = True
269                 else:
270                         self.InterfaceState = False
271                 self.checkRef.close(True)
272         
273         def rescanTimerFired(self):
274                 self.rescanTimer.stop()
275                 self.updateAPList()
276
277         def updateAPList(self):
278                 self.oldlist = self.APList
279                 self.newAPlist = []
280                 newList = []
281                 newListIndex = None
282                 currentListEntry = None
283                 newList = self.listAccessPoints()
284
285                 for oldentry in self.oldlist:
286                         if oldentry not in newList:
287                                 newList.append(oldentry)
288                                 
289                 for newentry in newList:
290                         self.newAPlist.append(newentry)
291
292                 if len(self.newAPlist):
293                         if (self.wizard[self.currStep].has_key("dynamiclist")):
294                                 currentListEntry = self["list"].getCurrent()
295                                 if currentListEntry is not None:
296                                         idx = 0
297                                         for entry in self.newAPlist:
298                                                 if entry == currentListEntry:
299                                                         newListIndex = idx
300                                                 idx +=1
301                                 self.wizard[self.currStep]["evaluatedlist"] = self.newAPlist
302                                 self['list'].setList(self.newAPlist)
303                                 if newListIndex is not None:
304                                         self["list"].setIndex(newListIndex)
305                                 self["list"].updateList(self.newAPlist)
306
307         def listAccessPoints(self):
308                 self.APList = []
309                 if self.WlanPluginInstalled is False:
310                         self.APList.append( ( _("No networks found"), None ) )
311                 else:
312                         from Plugins.SystemPlugins.WirelessLan.Wlan import iWlan
313                         iWlan.setInterface(self.selectedInterface)
314                         self.w = iWlan.getInterface()
315                         aps = iWlan.getNetworkList()
316                         if aps is not None:
317                                 print "[NetworkWizard.py] got Accespoints!"
318                                 tmplist = []
319                                 complist = []
320                                 for ap in aps:
321                                         a = aps[ap]
322                                         if a['active']:
323                                                 tmplist.append( (a['bssid'], a['essid']) )
324                                                 complist.append( (a['bssid'], a['essid']) )
325                                 
326                                 for entry in tmplist:
327                                         if entry[1] == "":
328                                                 for compentry in complist:
329                                                         if compentry[0] == entry[0]:
330                                                                 complist.remove(compentry)
331                                 for entry in complist:
332                                         self.APList.append( (entry[1], entry[1]) )
333                         if not len(aps):
334                                 self.APList.append( ( _("No networks found"), None ) )
335                         
336                 self.rescanTimer.start(4000)
337                 return self.APList
338
339
340         def AccessPointsSelectionMoved(self):
341                 self.ap = self.selection
342                 self.NextStep = 'wlanconfig'
343
344         def checkWlanSelection(self):
345                 self.stopScan()
346                 self.currStep = self.getStepWithID(self.NextStep)
347
348         def isWlanPluginInstalled(self):
349                 try:
350                         from Plugins.SystemPlugins.WirelessLan.Wlan import iWlan
351                 except ImportError:
352                         self.WlanPluginInstalled = False
353                 else:
354                         self.WlanPluginInstalled = True
355
356         def listChoices(self):
357                 self.stopScan()
358                 list = []
359                 if self.WlanPluginInstalled == True:
360                         list.append((_("Configure your wireless LAN again"), "scanwlan"))
361                 list.append((_("Configure your internal LAN"), "nwconfig"))
362                 list.append((_("Exit network wizard"), "end"))          
363                 return list
364
365         def ChoicesSelectionMade(self, index):
366                 self.ChoicesSelect(index)
367
368         def ChoicesSelect(self, index):
369                 if index == 'end':
370                         self.NextStep = 'end'
371                 elif index == 'nwconfig':
372                         self.selectedInterface = "eth0"
373                         self.NextStep = 'nwconfig'
374                 else:
375                         self.NextStep = 'asknetworktype'
376
377         def ChoicesSelectionMoved(self):
378                 pass