networksetup fixes / cleanups
[enigma2.git] / lib / python / Screens / NetworkSetup.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap,NumberActionMap
3 from Screens.MessageBox import MessageBox
4 from Screens.Standby import *
5 from Components.ConfigList import ConfigListScreen
6 from Components.config import config, getConfigListEntry
7 from Components.Network import iNetwork
8 from Tools.Directories import resolveFilename, SCOPE_SKIN_IMAGE
9 from Components.Label import Label,MultiColorLabel
10 from Components.Pixmap import Pixmap,MultiPixmap
11 from Tools.LoadPixmap import LoadPixmap
12 from Components.MenuList import MenuList
13 from Components.config import config, ConfigYesNo, ConfigIP, NoSave, ConfigNothing, ConfigSubsection, ConfigText, ConfigSelection, getConfigListEntry
14 from Components.PluginComponent import plugins
15 from Plugins.Plugin import PluginDescriptor
16 from enigma import eTimer, eConsoleAppContainer,gRGB
17 import time, os, re
18 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
19
20 class NetworkAdapterSelection(Screen):
21         def __init__(self, session):
22                 Screen.__init__(self, session)
23                 iNetwork.getInterfaces()
24                 self.wlan_errortext = _("No working wireless networkadapter found.\nPlease verify that you have attached a compatible WLAN USB Stick and your Network is configured correctly.")
25                 self.lan_errortext = _("No working local networkadapter found.\nPlease verify that you have attached a network cable and your Network is configured correctly.")
26                 self.adapters = [(iNetwork.getFriendlyAdapterName(x),x) for x in iNetwork.getAdapterList()]
27                 if len(self.adapters) == 0:
28                         self.onFirstExecBegin.append(self.NetworkFallback)
29                         
30                 self["adapterlist"] = MenuList(self.adapters)
31                 self["actions"] = ActionMap(["OkCancelActions"],
32                 {
33                         "ok": self.okbuttonClick,
34                         "cancel": self.close
35                 })
36
37                 if len(self.adapters) == 1:
38                         self.onFirstExecBegin.append(self.okbuttonClick)
39
40         def okbuttonClick(self):
41                 selection = self["adapterlist"].getCurrent()
42                 if selection is not None:
43                         self.session.openWithCallback(self.AdapterSetupClosed, AdapterSetupConfiguration, selection[1])
44
45         def AdapterSetupClosed(self, *ret):
46                 self.close()
47
48         def NetworkFallback(self):
49                 if iNetwork.configuredInterfaces.has_key('wlan0') is True:
50                         self.session.openWithCallback(self.ErrorMessageClosed, MessageBox, self.wlan_errortext, type = MessageBox.TYPE_INFO,timeout = 10)
51                 else:
52                         self.session.openWithCallback(self.ErrorMessageClosed, MessageBox, self.lan_errortext, type = MessageBox.TYPE_INFO,timeout = 10)
53
54         def ErrorMessageClosed(self, *ret):
55                 if iNetwork.configuredInterfaces.has_key('wlan0') is True:
56                         self.session.openWithCallback(self.AdapterSetupClosed, AdapterSetupConfiguration, 'wlan0')
57                 else:
58                         self.session.openWithCallback(self.AdapterSetupClosed, AdapterSetupConfiguration, 'eth0')
59
60 class NameserverSetup(Screen, ConfigListScreen):
61         def __init__(self, session):
62                 Screen.__init__(self, session)
63                 iNetwork.getInterfaces()
64                 self.backupNameserverList = iNetwork.getNameserverList()[:]
65                 print "backup-list:", self.backupNameserverList
66                 
67                 self["ButtonGreentext"] = Label(_("Add"))
68                 self["ButtonYellowtext"] = Label(_("Delete"))
69                 self["ButtonRedtext"] = Label(_("Close"))
70                 self.createConfig()
71                 
72                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
73                 {
74                         "ok": self.ok,
75                         "cancel": self.cancel,
76                         "red": self.cancel,
77                         "green": self.add,
78                         "yellow": self.remove
79                 }, -2)
80                 
81                 self.list = []
82                 ConfigListScreen.__init__(self, self.list)
83                 self.createSetup()
84
85         def createConfig(self):
86                 self.nameservers = iNetwork.getNameserverList()
87                 self.nameserverEntries = []
88                 
89                 for nameserver in self.nameservers:
90                         self.nameserverEntries.append(NoSave(ConfigIP(default=nameserver)))
91
92         def createSetup(self):
93                 self.list = []
94                 
95                 for i in range(len(self.nameserverEntries)):
96                         self.list.append(getConfigListEntry(_("Nameserver %d") % (i + 1), self.nameserverEntries[i]))
97                 
98                 self["config"].list = self.list
99                 self["config"].l.setList(self.list)
100
101         def ok(self):
102                 iNetwork.clearNameservers()
103                 for nameserver in self.nameserverEntries:
104                         iNetwork.addNameserver(nameserver.value)
105                 iNetwork.writeNameserverConfig()
106                 self.close()
107
108         def cancel(self):
109                 iNetwork.clearNameservers()
110                 print "backup-list:", self.backupNameserverList
111                 for nameserver in self.backupNameserverList:
112                         iNetwork.addNameserver(nameserver)
113                 self.close()
114
115         def add(self):
116                 iNetwork.addNameserver([0,0,0,0])
117                 self.createConfig()
118                 self.createSetup()
119
120         def remove(self):
121                 print "currentIndex:", self["config"].getCurrentIndex()
122                 
123                 index = self["config"].getCurrentIndex()
124                 if index < len(self.nameservers):
125                         iNetwork.removeNameserver(self.nameservers[index])
126                         self.createConfig()
127                         self.createSetup()
128
129 class AdapterSetup(Screen, ConfigListScreen):
130         def __init__(self, session, iface,essid=None, aplist=None):
131                 Screen.__init__(self, session)
132                 self.session = session
133                 iNetwork.getInterfaces()
134                 self.essid = essid
135                 self.aplist = aplist
136                 ## FIXME , workaround against current wizzard not able to send arguments
137                 if iface == 0:
138                         self.iface = "eth0"
139                 elif iface == 1:
140                         self.iface = "wlan0"
141                 else:
142                         self.iface = iface
143                 
144                 if self.iface == 'wlan0':
145                         from Plugins.SystemPlugins.WirelessLan.Wlan import wpaSupplicant,Wlan
146                         self.ws = wpaSupplicant()
147                         list = []
148                         list.append(_("WEP"))
149                         list.append(_("WPA"))
150                         list.append(_("WPA2"))
151                         if self.aplist is not None:
152                                 self.nwlist = self.aplist
153                                 self.nwlist.sort(key = lambda x: x[0])
154                         else:
155                                 if iNetwork.getAdapterAttribute('wlan0', 'up') is True:
156                                         self.nwlist = []
157                                         try:
158                                                 self.w = Wlan('wlan0')
159                                                 aps = self.w.getNetworkList()
160                                                 if aps is not None:
161                                                         print "[Wlan.py] got Accespoints!"
162                                                         for ap in aps:
163                                                                 a = aps[ap]
164                                                                 if a['active']:
165                                                                         if a['essid'] == "":
166                                                                                 a['essid'] = a['bssid']
167                                                                         self.nwlist.append( a['essid'])
168                                                 self.nwlist.sort(key = lambda x: x[0])
169                                         except:
170                                                 self.nwlist.append("No Networks found")
171                                 else:
172                                         self.nwlist.append("No Networks found")
173                                 
174                         wsconfig = self.ws.loadConfig()
175                         config.plugins.wlan.essid = NoSave(ConfigSelection(self.nwlist, default = self.essid or wsconfig['ssid'] ))
176                         config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = wsconfig['encryption'] ))
177                         config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = wsconfig['encryption_type'] ))
178                         config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = wsconfig['key'], fixed_size = False,visible_width = 30))
179                         #self.ws.loadConfig()
180                 
181                 self.dhcpConfigEntry = NoSave(ConfigYesNo(default=iNetwork.getAdapterAttribute(self.iface, "dhcp") or False))
182                 self.hasGatewayConfigEntry = NoSave(ConfigYesNo(default=True))
183                 self.ipConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "ip")) or [0,0,0,0])
184                 self.netmaskConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "netmask") or [255,0,0,0]))
185                 self.gatewayConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "gateway") or [0,0,0,0]))
186                 nameserver = (iNetwork.getNameserverList() + [[0,0,0,0]] * 2)[0:2]
187                 self.primaryDNS = NoSave(ConfigIP(default=nameserver[0]))
188                 self.secondaryDNS = NoSave(ConfigIP(default=nameserver[1]))
189                 
190                 self["actions"] = ActionMap(["SetupActions","ShortcutActions"],
191                 {
192                         "ok": self.ok,
193                         "cancel": self.cancel,
194                         "red": self.cancel,
195                         "blue": self.KeyBlue,
196                 }, -2)
197                 
198                 self.list = []
199                 ConfigListScreen.__init__(self, self.list)
200                 self.createSetup()
201                 self.onLayoutFinish.append(self.layoutFinished)
202                 
203                 self["DNS1text"] = Label(_("Primary DNS"))
204                 self["DNS2text"] = Label(_("Secondary DNS"))
205                 self["DNS1"] = Label()
206                 self["DNS2"] = Label()
207                 
208                 self["introduction"] = Label(_("Current settings:"))
209                 
210                 self["IPtext"] = Label(_("IP Address"))
211                 self["Netmasktext"] = Label(_("Netmask"))
212                 self["Gatewaytext"] = Label(_("Gateway"))
213                 
214                 self["IP"] = Label()
215                 self["Mask"] = Label()
216                 self["Gateway"] = Label()
217                 
218                 self["BottomBG"] = Pixmap()
219                 self["Adaptertext"] = Label(_("Network:"))
220                 self["Adapter"] = Label()
221                 self["introduction2"] = Label(_("Press OK to activate the settings."))
222                 self["ButtonRed"] = Pixmap()
223                 self["ButtonRedtext"] = Label(_("Close"))
224                 self["ButtonBlue"] = Pixmap()
225                 self["ButtonBluetext"] = Label(_("Edit DNS"))
226
227         def layoutFinished(self):
228                 self["DNS1"].setText(self.primaryDNS.getText())
229                 self["DNS2"].setText(self.secondaryDNS.getText())
230                 if self.ipConfigEntry.getText() is not None:
231                         self["IP"].setText(self.ipConfigEntry.getText())
232                 else:
233                         self["IP"].setText([0,0,0,0])
234                 self["Mask"].setText(self.netmaskConfigEntry.getText())
235                 self["Gateway"].setText(self.gatewayConfigEntry.getText())
236                 self["Adapter"].setText(iNetwork.getFriendlyAdapterName(self.iface))
237
238         def createSetup(self):
239                 self.list = []
240                 
241                 self.dhcpEntry = getConfigListEntry(_("Use DHCP"), self.dhcpConfigEntry)
242                 self.list.append(self.dhcpEntry)
243                 if not self.dhcpConfigEntry.value:
244                         self.list.append(getConfigListEntry(_('IP Address'), self.ipConfigEntry))
245                         self.list.append(getConfigListEntry(_('Netmask'), self.netmaskConfigEntry))
246                         self.list.append(getConfigListEntry(_('Use a gateway'), self.hasGatewayConfigEntry))
247                         if self.hasGatewayConfigEntry.value:
248                                 self.list.append(getConfigListEntry(_('Gateway'), self.gatewayConfigEntry))
249                 
250                 self.extended = None
251                 self.extendedSetup = None
252                 for p in plugins.getPlugins(PluginDescriptor.WHERE_NETWORKSETUP):
253                         callFnc = p.__call__["ifaceSupported"](self.iface)
254                         if callFnc is not None:
255                                 self.extended = callFnc
256                                 print p.__call__
257                                 if p.__call__.has_key("configStrings"):
258                                         self.configStrings = p.__call__["configStrings"]
259                                 else:
260                                         self.configStrings = None
261                                 
262                                 self.list.append(getConfigListEntry(_("Network SSID"), config.plugins.wlan.essid))
263                                 self.encryptionEnabled = getConfigListEntry(_("Encryption"), config.plugins.wlan.encryption.enabled)
264                                 self.list.append(self.encryptionEnabled)
265                                 
266                                 if config.plugins.wlan.encryption.enabled.value:
267                                         self.list.append(getConfigListEntry(_("Encryption Type"), config.plugins.wlan.encryption.type))
268                                         self.list.append(getConfigListEntry(_("Encryption Key"), config.plugins.wlan.encryption.psk))
269                 
270                 self["config"].list = self.list
271                 self["config"].l.setList(self.list)
272
273         def KeyBlue(self):
274                 self.session.open(NameserverSetup)
275
276         def newConfig(self):
277                 print self["config"].getCurrent()
278                 if self["config"].getCurrent() == self.dhcpEntry:
279                         self.createSetup()
280
281         def keyLeft(self):
282                 ConfigListScreen.keyLeft(self)
283                 self.createSetup()
284
285         def keyRight(self):
286                 ConfigListScreen.keyRight(self)
287                 self.createSetup()
288
289         def ok(self):
290                 selection = self["config"].getCurrent()
291                 if selection == self.extendedSetup:
292                         self.extended(self.session, self.iface)
293                 else:
294                         iNetwork.setAdapterAttribute(self.iface, "dhcp", self.dhcpConfigEntry.value)
295                         iNetwork.setAdapterAttribute(self.iface, "ip", self.ipConfigEntry.value)
296                         iNetwork.setAdapterAttribute(self.iface, "netmask", self.netmaskConfigEntry.value)
297                         if self.hasGatewayConfigEntry.value:
298                                 iNetwork.setAdapterAttribute(self.iface, "gateway", self.gatewayConfigEntry.value)
299                         else:
300                                 iNetwork.removeAdapterAttribute(self.iface, "gateway")
301                         
302                         if self.extended is not None and self.configStrings is not None:
303                                 iNetwork.setAdapterAttribute(self.iface, "configStrings", self.configStrings(self.iface))
304                                 self.ws.writeConfig()
305                         
306                         iNetwork.deactivateNetworkConfig()
307                         iNetwork.writeNetworkConfig()
308                         iNetwork.activateNetworkConfig()
309                         self.close()
310
311         def cancel(self):
312                 iNetwork.getInterfaces()
313                 self.close()
314
315         def run(self):
316                 self.ok()
317
318
319 class AdapterSetupConfiguration(Screen):
320         def __init__(self, session,iface):
321                 Screen.__init__(self, session)
322                 self.iface = iface
323                 self.session = session
324                 self.mainmenu = self.genMainMenu()
325                 self["menulist"] = MenuList(self.mainmenu)
326                 self["description"] = Label()
327                 self["IFtext"] = Label()
328                 self["IF"] = Label()
329                 self["BottomBG"] = Label()
330                 self["Statustext"] = Label()
331                 self["statuspic"] = MultiPixmap()
332                 self["statuspic"].hide()
333                 self["BottomBG"] = Pixmap()
334                 self["ButtonRed"] = Pixmap()
335                 self["ButtonRedtext"] = Label(_("Close"))
336                 
337                 self.oktext = _("Press OK on your remote control to continue.")
338                 self.reboottext = _("Your Dreambox will restart after pressing OK on your remote control.")
339                 self.errortext = _("No working wireless interface found.\n Please verify that you have attached a compatible WLAN USB Stick or enable you local network interface.")    
340                 
341                 self["actions"] = NumberActionMap(["WizardActions","ShortcutActions"],
342                 {
343                         "ok": self.ok,
344                         "back": self.close,
345                         "up": self.up,
346                         "down": self.down,
347                         "red": self.close,
348                         "left": self.left,
349                         "right": self.right,
350                 }, -2)
351                 
352                 iNetwork.getInterfaces()
353                 self.onLayoutFinish.append(self.layoutFinished)
354                 self.updateStatusbar()
355
356         def ok(self):
357                 if self["menulist"].getCurrent()[1] == 'edit':
358                         if self.iface == 'wlan0':
359                                 from Plugins.SystemPlugins.WirelessLan.iwlibs import Wireless
360                                 ifobj = Wireless(self.iface) # a Wireless NIC Object
361                                 self.wlanresponse = ifobj.getStatistics()
362                                 if self.wlanresponse[0] != 19: # Wlan Interface found.
363                                         self.session.open(AdapterSetup,self.iface)
364                                 else:
365                                         # Display Wlan not available Message
366                                         self.showErrorMessage()
367                         else:
368                                 self.session.open(AdapterSetup,self.iface)
369                 if self["menulist"].getCurrent()[1] == 'test':
370                         self.session.open(NetworkAdapterTest,self.iface)
371                 if self["menulist"].getCurrent()[1] == 'dns':
372                         self.session.open(NameserverSetup)
373                 if self["menulist"].getCurrent()[1] == 'scanwlan':
374                         from Plugins.SystemPlugins.WirelessLan.iwlibs import Wireless
375                         ifobj = Wireless(self.iface) # a Wireless NIC Object
376                         self.wlanresponse = ifobj.getStatistics()
377                         if self.wlanresponse[0] != 19:
378                                 from Plugins.SystemPlugins.WirelessLan.plugin import WlanScan
379                                 self.session.openWithCallback(self.WlanScanClosed, WlanScan, self.iface)
380                         else:
381                                 # Display Wlan not available Message
382                                 self.showErrorMessage()
383                 if self["menulist"].getCurrent()[1] == 'wlanstatus':
384                         from Plugins.SystemPlugins.WirelessLan.iwlibs import Wireless
385                         ifobj = Wireless(self.iface) # a Wireless NIC Object
386                         self.wlanresponse = ifobj.getStatistics()
387                         if self.wlanresponse[0] != 19:
388                                 from Plugins.SystemPlugins.WirelessLan.plugin import WlanStatus
389                                 self.session.open(WlanStatus,self.iface)
390                         else:
391                                 # Display Wlan not available Message
392                                 self.showErrorMessage()
393                 if self["menulist"].getCurrent()[1] == 'lanrestart':
394                         self.session.openWithCallback(self.restartLan, MessageBox, (_("Are you sure you want to restart your network interfaces?\n\n") + self.oktext ) )
395                 if self["menulist"].getCurrent()[1] == 'enablewlan':
396                         self.session.openWithCallback(self.enableWlan, MessageBox, _("Are you sure you want to enable WLAN support?\nConnect your Wlan USB Stick to your Dreambox and press OK.\n\n") )
397                 if self["menulist"].getCurrent()[1] == 'enablelan':
398                         self.session.openWithCallback(self.enableLan, MessageBox, (_("Are you sure you want to enable your local network?\n\n") + self.oktext ) )
399                 if self["menulist"].getCurrent()[1] == 'openwizard':
400                         from Plugins.SystemPlugins.NetworkWizard.NetworkWizard import NetworkWizard
401                         self.session.openWithCallback(self.AdapterSetupClosed, NetworkWizard)
402         
403         def up(self):
404                 self["menulist"].up()
405                 self.loadDescription()
406
407         def down(self):
408                 self["menulist"].down()
409                 self.loadDescription()
410
411         def left(self):
412                 self["menulist"].pageUp()
413                 self.loadDescription()
414
415         def right(self):
416                 self["menulist"].pageDown()
417                 self.loadDescription()
418
419         def layoutFinished(self):
420                 idx = 0
421                 self["menulist"].moveToIndex(idx)
422                 self.loadDescription()
423
424         def loadDescription(self):
425                 if self["menulist"].getCurrent()[1] == 'edit':
426                         self["description"].setText(_("Edit the network configuration of your Dreambox.\n" ) + self.oktext )
427                 if self["menulist"].getCurrent()[1] == 'test':
428                         self["description"].setText(_("Test the network configuration of your Dreambox.\n" ) + self.oktext )
429                 if self["menulist"].getCurrent()[1] == 'enablelan':
430                         self["description"].setText(_("Enable the local network of your Dreambox.\n\n" ) + self.oktext )
431                 if self["menulist"].getCurrent()[1] == 'dns':
432                         self["description"].setText(_("Edit the Nameserver configuration of your Dreambox.\n" ) + self.oktext )
433                 if self["menulist"].getCurrent()[1] == 'scanwlan':
434                         self["description"].setText(_("Scan your network for wireless Access Points and connect to them using your WLAN USB Stick\n" ) + self.oktext )
435                 if self["menulist"].getCurrent()[1] == 'wlanstatus':
436                         self["description"].setText(_("Shows the state of your wireless LAN connection.\n" ) + self.oktext )
437                 if self["menulist"].getCurrent()[1] == 'lanrestart':
438                         self["description"].setText(_("Restart your network connection and interfaces.\n" ) + self.oktext )
439                 if self["menulist"].getCurrent()[1] == 'enablewlan':
440                         self["description"].setText(_("Pressing OK enables the built in wireless LAN support of your Dreambox.\nWlan USB Sticks with Zydas ZD1211B and RAlink RT73 Chipset are supported.\nConnect your Wlan USB Stick to your Dreambox before pressing OK.\n\n" ) + self.reboottext )
441                 if self["menulist"].getCurrent()[1] == 'openwizard':
442                         self["description"].setText(_("Use the Networkwizard to configure your Network\n" ) + self.oktext )
443
444         def updateStatusbar(self):
445                 self["IFtext"].setText(_("Network:"))
446                 self["IF"].setText(iNetwork.getFriendlyAdapterName(self.iface))
447                 self["Statustext"].setText(_("Link:"))
448                 
449                 if self.iface == 'wlan0':
450                         try:
451                                 from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
452                                 w = Wlan(self.iface)
453                                 stats = w.getStatus()
454                                 if stats['BSSID'] == "00:00:00:00:00:00":
455                                         self["statuspic"].setPixmapNum(1)
456                                 else:
457                                         self["statuspic"].setPixmapNum(0)
458                                 self["statuspic"].show()
459                         except:
460                                         self["statuspic"].setPixmapNum(1)
461                                         self["statuspic"].show()
462                 else:
463                         self.getLinkState(self.iface)
464
465         def doNothing(self):
466                 pass
467
468         def genMainMenu(self):
469                 menu = []
470                 menu.append((_("Adapter settings"), "edit"))
471                 menu.append((_("Nameserver settings"), "dns"))
472                 menu.append((_("Network test"), "test"))
473                 menu.append((_("Restart network"), "lanrestart"))
474                 
475                 self.extended = None
476                 self.extendedSetup = None
477                 for p in plugins.getPlugins(PluginDescriptor.WHERE_NETWORKSETUP):
478                         callFnc = p.__call__["ifaceSupported"](self.iface)
479                         if callFnc is not None:
480                                 menu.append((_("Scan Wireless Networks"), "scanwlan"))
481                                 menu.append((_("Show WLAN Status"), "wlanstatus"))
482                                 menu.append((_("Enable LAN"), "enablelan"))
483                         if callFnc is None and iNetwork.ifaces.has_key('wlan0') is False:
484                                 menu.append((_("Enable WLAN"), "enablewlan"))
485                         if callFnc is None and iNetwork.ifaces.has_key('wlan0') is True:
486                                 menu.append((_("Enable LAN"), "enablelan"))
487                                 
488                 if os.path.exists(resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkWizard/networkwizard.xml")):
489                         menu.append((_("NetworkWizard"), "openwizard"));
490                 return menu
491
492         def AdapterSetupClosed(self, *ret):
493                 self.mainmenu = self.genMainMenu()
494                 self["menulist"].l.setList(self.mainmenu)
495                 self.updateStatusbar()
496
497         def WlanScanClosed(self,*ret):
498                 if ret[0] is not None:
499                         self.session.openWithCallback(self.AdapterSetupClosed, AdapterSetup, 'wlan0',ret[0],ret[1])
500                 else:
501                         self.session.openWithCallback(self.AdapterSetupClosed, AdapterSetup, 'wlan0',None,ret[0])
502
503         def enableWlan(self, ret = False):
504                 if (ret == True):
505                         iNetwork.resetNetworkConfig('wlan')
506                         iNetwork.getInterfaces()
507                         if iNetwork.getAdapterAttribute('wlan0', 'up') is True:
508                                 self.iface = 'wlan0'
509                                 try:
510                                         from Plugins.SystemPlugins.WirelessLan.plugin import WlanScan
511                                 except ImportError:
512                                         self.session.open(MessageBox, _("The wireless LAN plugin is not installed!\nPlease install it."), type = MessageBox.TYPE_INFO,timeout = 10 )
513                                 else:   
514                                         self.session.openWithCallback(self.WlanScanClosed, WlanScan, self.iface)
515                         else:
516                                 self.session.openWithCallback(self.restartDreambox, MessageBox, _("Your wireless LAN Adapter could not be started.\nDo you want to reboot your Dreambox to apply the new configuration?\n"))
517
518         def enableLan(self, ret = False):
519                 if (ret == True):
520                         iNetwork.resetNetworkConfig('lan')
521                         iNetwork.getInterfaces()
522                         if iNetwork.getAdapterAttribute('eth0', 'up') is True:
523                                 self.iface = 'eth0'
524                                 self.session.openWithCallback(self.AdapterSetupClosed, AdapterSetup, 'eth0')
525                         else:
526                                 self.session.openWithCallback(self.restartDreambox, MessageBox, _("Your wired LAN Adapter could not be started.\nDo you want to reboot your Dreambox to apply the new configuration?\n"))
527
528         def restartLan(self, ret = False):
529                 if (ret == True):
530                         iNetwork.restartNetwork()
531
532         def restartDreambox(self, ret = False):
533                 if (ret == True):
534                         TryQuitMainloop(self.session,2)
535
536         def getLinkState(self,iface):
537                 iNetwork.getLinkState(iface,self.dataAvail)
538
539         def dataAvail(self,data):
540                 self.output = data.strip()
541                 result = self.output.split('\n')
542                 pattern = re.compile("Link detected: yes")
543                 for item in result:
544                         if re.search(pattern, item):
545                                 self["statuspic"].setPixmapNum(0)
546                         else:
547                                 self["statuspic"].setPixmapNum(1)
548                 self["statuspic"].show()
549
550         def showErrorMessage(self):
551                 self.session.open(MessageBox, self.errortext, type = MessageBox.TYPE_INFO,timeout = 10 )
552
553
554 class NetworkAdapterTest(Screen):       
555         def __init__(self, session,iface):
556                 Screen.__init__(self, session)
557                 self.iface = iface
558                 iNetwork.getInterfaces()
559                 self.setLabels()
560                 
561                 self["updown_actions"] = NumberActionMap(["WizardActions","ShortcutActions"],
562                 {
563                         "ok": self.KeyOK,
564                         "blue": self.KeyOK,
565                         "up": lambda: self.updownhandler('up'),
566                         "down": lambda: self.updownhandler('down'),
567                 
568                 }, -2)
569                 
570                 self["shortcuts"] = ActionMap(["ShortcutActions","WizardActions"],
571                 {
572                         "red": self.close,
573                         "back": self.close,
574                 }, -2)
575                 self["infoshortcuts"] = ActionMap(["ShortcutActions","WizardActions"],
576                 {
577                         "red": self.closeInfo,
578                         "back": self.closeInfo,
579                 }, -2)
580                 self["shortcutsgreen"] = ActionMap(["ShortcutActions"],
581                 {
582                         "green": self.KeyGreen,
583                 }, -2)
584                 self["shortcutsgreen_restart"] = ActionMap(["ShortcutActions"],
585                 {
586                         "green": self.KeyGreenRestart,
587                 }, -2)
588                 self["shortcutsyellow"] = ActionMap(["ShortcutActions"],
589                 {
590                         "yellow": self.KeyYellow,
591                 }, -2)
592                 
593                 self["shortcutsgreen_restart"].setEnabled(False)
594                 self["updown_actions"].setEnabled(False)
595                 self["infoshortcuts"].setEnabled(False)
596                 self.onClose.append(self.delTimer)      
597                 self.onLayoutFinish.append(self.layoutFinished)
598                 self.steptimer = False
599                 self.nextstep = 0
600                 self.activebutton = 0
601                 self.nextStepTimer = eTimer()
602                 self.nextStepTimer.callback.append(self.nextStepTimerFire)
603
604         def closeInfo(self):
605                 self["shortcuts"].setEnabled(True)              
606                 self["infoshortcuts"].setEnabled(False)
607                 self["InfoText"].hide()
608                 self["InfoTextBorder"].hide()
609                 self["ButtonRedtext"].setText(_("Close"))
610
611         def delTimer(self):
612                 del self.steptimer
613                 del self.nextStepTimer
614
615         def nextStepTimerFire(self):
616                 self.nextStepTimer.stop()
617                 self.steptimer = False
618                 self.runTest()
619
620         def updownhandler(self,direction):
621                 if direction == 'up':
622                         if self.activebutton >=2:
623                                 self.activebutton -= 1
624                         else:
625                                 self.activebutton = 6
626                         self.setActiveButton(self.activebutton)
627                 if direction == 'down':
628                         if self.activebutton <=5:
629                                 self.activebutton += 1
630                         else:
631                                 self.activebutton = 1
632                         self.setActiveButton(self.activebutton)
633
634         def setActiveButton(self,button):
635                 if button == 1:
636                         self["EditSettingsButton"].setPixmapNum(0)
637                         self["EditSettings_Text"].setForegroundColorNum(0)
638                         self["NetworkInfo"].setPixmapNum(0)
639                         self["NetworkInfo_Text"].setForegroundColorNum(1)
640                         self["AdapterInfo"].setPixmapNum(1)               # active
641                         self["AdapterInfo_Text"].setForegroundColorNum(2) # active
642                 if button == 2:
643                         self["AdapterInfo_Text"].setForegroundColorNum(1)
644                         self["AdapterInfo"].setPixmapNum(0)
645                         self["DhcpInfo"].setPixmapNum(0)
646                         self["DhcpInfo_Text"].setForegroundColorNum(1)
647                         self["NetworkInfo"].setPixmapNum(1)               # active
648                         self["NetworkInfo_Text"].setForegroundColorNum(2) # active
649                 if button == 3:
650                         self["NetworkInfo"].setPixmapNum(0)
651                         self["NetworkInfo_Text"].setForegroundColorNum(1)
652                         self["IPInfo"].setPixmapNum(0)
653                         self["IPInfo_Text"].setForegroundColorNum(1)
654                         self["DhcpInfo"].setPixmapNum(1)                  # active
655                         self["DhcpInfo_Text"].setForegroundColorNum(2)    # active
656                 if button == 4:
657                         self["DhcpInfo"].setPixmapNum(0)
658                         self["DhcpInfo_Text"].setForegroundColorNum(1)
659                         self["DNSInfo"].setPixmapNum(0)
660                         self["DNSInfo_Text"].setForegroundColorNum(1)
661                         self["IPInfo"].setPixmapNum(1)                  # active
662                         self["IPInfo_Text"].setForegroundColorNum(2)    # active                
663                 if button == 5:
664                         self["IPInfo"].setPixmapNum(0)
665                         self["IPInfo_Text"].setForegroundColorNum(1)
666                         self["EditSettingsButton"].setPixmapNum(0)
667                         self["EditSettings_Text"].setForegroundColorNum(0)
668                         self["DNSInfo"].setPixmapNum(1)                 # active
669                         self["DNSInfo_Text"].setForegroundColorNum(2)   # active
670                 if button == 6:
671                         self["DNSInfo"].setPixmapNum(0)
672                         self["DNSInfo_Text"].setForegroundColorNum(1)
673                         self["EditSettingsButton"].setPixmapNum(1)         # active
674                         self["EditSettings_Text"].setForegroundColorNum(2) # active
675                         self["AdapterInfo"].setPixmapNum(0)
676                         self["AdapterInfo_Text"].setForegroundColorNum(1)
677                         
678         def runTest(self):
679                 next = self.nextstep
680                 if next == 0:
681                         self.doStep1()
682                 elif next == 1:
683                         self.doStep2()
684                 elif next == 2:
685                         self.doStep3()
686                 elif next == 3:
687                         self.doStep4()
688                 elif next == 4:
689                         self.doStep5()
690                 elif next == 5:
691                         self.doStep6()
692                 self.nextstep += 1
693
694         def doStep1(self):
695                 self.steptimer = True
696                 self.nextStepTimer.start(3000)
697
698         def doStep2(self):
699                 self["Adapter"].setText(iNetwork.getFriendlyAdapterName(self.iface))
700                 self["Adapter"].setForegroundColorNum(2)
701                 self["Adaptertext"].setForegroundColorNum(1)
702                 self["AdapterInfo_Text"].setForegroundColorNum(1)
703                 self["AdapterInfo_OK"].show()
704                 self.steptimer = True
705                 self.nextStepTimer.start(3000)
706
707         def doStep3(self):
708                 self["Networktext"].setForegroundColorNum(1)
709                 self.getLinkState(self.iface)
710                 self["NetworkInfo_Text"].setForegroundColorNum(1)
711                 self.steptimer = True
712                 self.nextStepTimer.start(3000)
713
714         def doStep4(self):
715                 self["Dhcptext"].setForegroundColorNum(1)
716                 if iNetwork.getAdapterAttribute(self.iface, 'dhcp') is True:
717                         self["Dhcp"].setForegroundColorNum(2)
718                         self["Dhcp"].setText(_("enabled"))
719                         self["DhcpInfo_Check"].setPixmapNum(0)
720                 else:
721                         self["Dhcp"].setForegroundColorNum(1)
722                         self["Dhcp"].setText(_("disabled"))
723                         self["DhcpInfo_Check"].setPixmapNum(1)
724                 self["DhcpInfo_Check"].show()
725                 self["DhcpInfo_Text"].setForegroundColorNum(1)
726                 self.steptimer = True
727                 self.nextStepTimer.start(3000)
728
729         def doStep5(self):
730                 self["IPtext"].setForegroundColorNum(1)
731                 ret = iNetwork.checkNetworkState()
732                 if ret == True:
733                         self["IP"].setForegroundColorNum(2)
734                         self["IP"].setText(_("confirmed"))
735                         self["IPInfo_Check"].setPixmapNum(0)
736                 else:
737                         self["IP"].setForegroundColorNum(1)
738                         self["IP"].setText(_("unconfirmed"))
739                         self["IPInfo_Check"].setPixmapNum(1)
740                 self["IPInfo_Check"].show()
741                 self["IPInfo_Text"].setForegroundColorNum(1)
742                 self.steptimer = True
743                 self.nextStepTimer.start(3000)
744
745         def doStep6(self):
746                 self.steptimer = False
747                 self.nextStepTimer.stop()
748                 self["DNStext"].setForegroundColorNum(1)
749                 ret = iNetwork.checkDNSLookup()
750                 if ret == True:
751                         self["DNS"].setForegroundColorNum(2)
752                         self["DNS"].setText(_("confirmed"))
753                         self["DNSInfo_Check"].setPixmapNum(0)
754                 else:
755                         self["DNS"].setForegroundColorNum(1)
756                         self["DNS"].setText(_("unconfirmed"))
757                         self["DNSInfo_Check"].setPixmapNum(1)
758                 self["DNSInfo_Check"].show()
759                 self["DNSInfo_Text"].setForegroundColorNum(1)
760                 
761                 self["EditSettings_Text"].show()
762                 self["EditSettingsButton"].setPixmapNum(1)
763                 self["EditSettings_Text"].setForegroundColorNum(2) # active
764                 self["EditSettingsButton"].show()
765                 self["ButtonYellow_Check"].setPixmapNum(1)
766                 self["ButtonGreentext"].setText(_("Restart test"))
767                 self["ButtonGreen_Check"].setPixmapNum(0)
768                 self["shortcutsgreen"].setEnabled(False)
769                 self["shortcutsgreen_restart"].setEnabled(True)
770                 self["shortcutsyellow"].setEnabled(False)
771                 self["updown_actions"].setEnabled(True)
772                 self.activebutton = 6
773
774         def KeyGreen(self):
775                 self["shortcutsgreen"].setEnabled(False)
776                 self["shortcutsyellow"].setEnabled(True)
777                 self["updown_actions"].setEnabled(False)
778                 self["ButtonYellow_Check"].setPixmapNum(0)
779                 self["ButtonGreen_Check"].setPixmapNum(1)
780                 self.steptimer = True
781                 self.nextStepTimer.start(1000)
782
783         def KeyGreenRestart(self):
784                 self.nextstep = 0
785                 self.layoutFinished()
786                 self["Adapter"].setText((""))
787                 self["Network"].setText((""))
788                 self["Dhcp"].setText((""))
789                 self["IP"].setText((""))
790                 self["DNS"].setText((""))
791                 self["AdapterInfo_Text"].setForegroundColorNum(0)
792                 self["NetworkInfo_Text"].setForegroundColorNum(0)
793                 self["DhcpInfo_Text"].setForegroundColorNum(0)
794                 self["IPInfo_Text"].setForegroundColorNum(0)
795                 self["DNSInfo_Text"].setForegroundColorNum(0)
796                 self["shortcutsgreen_restart"].setEnabled(False)
797                 self["shortcutsgreen"].setEnabled(False)
798                 self["shortcutsyellow"].setEnabled(True)
799                 self["updown_actions"].setEnabled(False)
800                 self["ButtonYellow_Check"].setPixmapNum(0)
801                 self["ButtonGreen_Check"].setPixmapNum(1)
802                 self.steptimer = True
803                 self.nextStepTimer.start(1000)
804
805         def KeyOK(self):
806                 self["infoshortcuts"].setEnabled(True)
807                 self["shortcuts"].setEnabled(False)
808                 if self.activebutton == 1: # Adapter Check
809                         self["InfoText"].setText(_("This test detects your configured LAN-Adapter."))
810                         self["InfoTextBorder"].show()
811                         self["InfoText"].show()
812                         self["ButtonRedtext"].setText(_("Back"))
813                 if self.activebutton == 2: #LAN Check
814                         self["InfoText"].setText(_("This test checks whether a network cable is connected to your LAN-Adapter.\nIf you get a \"disconnected\" message:\n- verify that a network cable is attached\n- verify that the cable is not broken"))
815                         self["InfoTextBorder"].show()
816                         self["InfoText"].show()
817                         self["ButtonRedtext"].setText(_("Back"))
818                 if self.activebutton == 3: #DHCP Check
819                         self["InfoText"].setText(_("This test checks whether your LAN Adapter is set up for automatic IP Address configuration with DHCP.\nIf you get a \"disabled\" message:\n - then your LAN Adapter is configured for manual IP Setup\n- verify thay you have entered correct IP informations in the AdapterSetup dialog.\nIf you get an \"enabeld\" message:\n-verify that you have a configured and working DHCP Server in your network."))
820                         self["InfoTextBorder"].show()
821                         self["InfoText"].show()
822                         self["ButtonRedtext"].setText(_("Back"))
823                 if self.activebutton == 4: # IP Check
824                         self["InfoText"].setText(_("This test checks whether a valid IP Address is found for your LAN Adapter.\nIf you get a \"unconfirmed\" message:\n- no valid IP Address was found\n- please check your DHCP, cabling and adapter setup"))
825                         self["InfoTextBorder"].show()
826                         self["InfoText"].show()
827                         self["ButtonRedtext"].setText(_("Back"))
828                 if self.activebutton == 5: # DNS Check
829                         self["InfoText"].setText(_("This test checks for configured Nameservers.\nIf you get a \"unconfirmed\" message:\n- please check your DHCP, cabling and Adapter setup\n- if you configured your Nameservers manually please verify your entries in the \"Nameserver\" Configuration"))
830                         self["InfoTextBorder"].show()
831                         self["InfoText"].show()
832                         self["ButtonRedtext"].setText(_("Back"))
833                 if self.activebutton == 6: # Edit Settings
834                         self.session.open(AdapterSetup,self.iface)
835
836         def KeyYellow(self):
837                 self.nextstep = 0
838                 self["shortcutsgreen_restart"].setEnabled(True)
839                 self["shortcutsgreen"].setEnabled(False)
840                 self["shortcutsyellow"].setEnabled(False)
841                 self["ButtonGreentext"].setText(_("Restart test"))
842                 self["ButtonYellow_Check"].setPixmapNum(1)
843                 self["ButtonGreen_Check"].setPixmapNum(0)
844                 self.steptimer = False
845                 self.nextStepTimer.stop()
846
847         def layoutFinished(self):
848                 self["shortcutsyellow"].setEnabled(False)
849                 self["AdapterInfo_OK"].hide()
850                 self["NetworkInfo_Check"].hide()
851                 self["DhcpInfo_Check"].hide()
852                 self["IPInfo_Check"].hide()
853                 self["DNSInfo_Check"].hide()
854                 self["EditSettings_Text"].hide()
855                 self["EditSettingsButton"].hide()
856                 self["InfoText"].hide()
857                 self["InfoTextBorder"].hide()
858
859         def setLabels(self):
860                 self["Adaptertext"] = MultiColorLabel(_("LAN Adapter"))
861                 self["Adapter"] = MultiColorLabel()
862                 self["AdapterInfo"] = MultiPixmap()
863                 self["AdapterInfo_Text"] = MultiColorLabel(_("Show Info"))
864                 self["AdapterInfo_OK"] = Pixmap()
865                 
866                 if self.iface == 'wlan0':
867                         self["Networktext"] = MultiColorLabel(_("Wireless Network"))
868                 else:
869                         self["Networktext"] = MultiColorLabel(_("Local Network"))
870                 
871                 self["Network"] = MultiColorLabel()
872                 self["NetworkInfo"] = MultiPixmap()
873                 self["NetworkInfo_Text"] = MultiColorLabel(_("Show Info"))
874                 self["NetworkInfo_Check"] = MultiPixmap()
875                 
876                 self["Dhcptext"] = MultiColorLabel(_("DHCP"))
877                 self["Dhcp"] = MultiColorLabel()
878                 self["DhcpInfo"] = MultiPixmap()
879                 self["DhcpInfo_Text"] = MultiColorLabel(_("Show Info"))
880                 self["DhcpInfo_Check"] = MultiPixmap()
881                 
882                 self["IPtext"] = MultiColorLabel(_("IP Address"))
883                 self["IP"] = MultiColorLabel()
884                 self["IPInfo"] = MultiPixmap()
885                 self["IPInfo_Text"] = MultiColorLabel(_("Show Info"))
886                 self["IPInfo_Check"] = MultiPixmap()
887                 
888                 self["DNStext"] = MultiColorLabel(_("Nameserver"))
889                 self["DNS"] = MultiColorLabel()
890                 self["DNSInfo"] = MultiPixmap()
891                 self["DNSInfo_Text"] = MultiColorLabel(_("Show Info"))
892                 self["DNSInfo_Check"] = MultiPixmap()
893                 
894                 self["EditSettings_Text"] = MultiColorLabel(_("Edit settings"))
895                 self["EditSettingsButton"] = MultiPixmap()
896                 
897                 self["ButtonRedtext"] = Label(_("Close"))
898                 self["ButtonRed"] = Pixmap()
899
900                 self["ButtonGreentext"] = Label(_("Start test"))
901                 self["ButtonGreen_Check"] = MultiPixmap()
902                 
903                 self["ButtonYellowtext"] = Label(_("Stop test"))
904                 self["ButtonYellow_Check"] = MultiPixmap()
905                 
906                 self["InfoTextBorder"] = Pixmap()
907                 self["InfoText"] = Label()
908
909         def getLinkState(self,iface):
910                 if iface == 'wlan0':
911                         try:
912                                 from Plugins.SystemPlugins.WirelessLan.Wlan import Wlan
913                                 w = Wlan(iface)
914                                 stats = w.getStatus()
915                                 if stats['BSSID'] == "00:00:00:00:00:00":
916                                         self["Network"].setForegroundColorNum(1)
917                                         self["Network"].setText(_("disconnected"))
918                                         self["NetworkInfo_Check"].setPixmapNum(1)
919                                         self["NetworkInfo_Check"].show()
920                                 else:
921                                         self["Network"].setForegroundColorNum(2)
922                                         self["Network"].setText(_("connected"))
923                                         self["NetworkInfo_Check"].setPixmapNum(0)
924                                         self["NetworkInfo_Check"].show()
925                         except:
926                                         self["Network"].setForegroundColorNum(1)
927                                         self["Network"].setText(_("disconnected"))
928                                         self["NetworkInfo_Check"].setPixmapNum(1)
929                                         self["NetworkInfo_Check"].show()
930                 else:
931                         iNetwork.getLinkState(iface,self.dataAvail)
932
933         def dataAvail(self,data):
934                 self.output = data.strip()
935                 result = self.output.split('\n')
936                 pattern = re.compile("Link detected: yes")
937                 for item in result:
938                         if re.search(pattern, item):
939                                 self["Network"].setForegroundColorNum(2)
940                                 self["Network"].setText(_("connected"))
941                                 self["NetworkInfo_Check"].setPixmapNum(0)
942                         else:
943                                 self["Network"].setForegroundColorNum(1)
944                                 self["Network"].setText(_("disconnected"))
945                                 self["NetworkInfo_Check"].setPixmapNum(1)
946                 self["NetworkInfo_Check"].show()
947
948