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