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