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