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