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