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