movieplayer_select.patch by Moritz Venn
[enigma2.git] / lib / python / Screens / NetworkSetup.py
index 3d64f522fb822aed3256b01e7115d07b9d6d1b59..cf03f1179e2e38d7d05ed5aaf8baf9d98fc7a1cb 100644 (file)
 from Screen import Screen
-from Components.ActionMap import NumberActionMap
-from Components.ConfigList import ConfigList
-from Components.config import config
-from Components.config import getConfigListEntry
+from Components.ActionMap import ActionMap
+from Components.ConfigList import ConfigListScreen
+from Components.config import config, getConfigListEntry
 from Components.Network import iNetwork
 from Components.Label import Label
+from Components.MenuList import MenuList
+from Components.config import config, ConfigYesNo, ConfigIP, NoSave, ConfigNothing
+from Components.PluginComponent import plugins
+from Plugins.Plugin import PluginDescriptor
 
-class NetworkSetup(Screen):
+class NetworkAdapterSelection(Screen):
        def __init__(self, session):
                Screen.__init__(self, session)
-        
-               self["actions"] = NumberActionMap(["SetupActions"],
+
+               self.adapters = [(iNetwork.getFriendlyAdapterName(x),x) for x in iNetwork.getAdapterList()]
+
+               self["adapterlist"] = MenuList(self.adapters)
+               self["actions"] = ActionMap(["OkCancelActions"],
+               {
+                       "ok": self.okbuttonClick,
+                       "cancel": self.close
+               })
+
+               if len(self.adapters) == 1:
+                       self.onFirstExecBegin.append(self.okbuttonClick)
+
+       def okbuttonClick(self):
+               selection = self["adapterlist"].getCurrent()
+               if selection is not None:
+                       self.session.openWithCallback(self.AdapterSetupClosed, AdapterSetup, selection[1])
+
+       def AdapterSetupClosed(self, *ret):
+               if len(self.adapters) == 1: # just one network adapter.. close selection
+                       self.close()
+
+class NameserverSetup(Screen, ConfigListScreen):
+       def __init__(self, session):
+               Screen.__init__(self, session)
+               self.backupNameserverList = iNetwork.getNameserverList()[:]
+               print "backup-list:", self.backupNameserverList
+
+               self["red"] = Label(_("Delete"))
+               self["green"] = Label(_("Add"))
+
+               self.createConfig()
+
+               self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
                {
-                       "ok": self.keySave,
-                       "cancel": self.keyCancel,
-                       "left": self.keyLeft,
-                       "right": self.keyRight,
-                       "1": self.keyNumberGlobal,
-                       "2": self.keyNumberGlobal,
-                       "3": self.keyNumberGlobal,
-                       "4": self.keyNumberGlobal,
-                       "5": self.keyNumberGlobal,
-                       "6": self.keyNumberGlobal,
-                       "7": self.keyNumberGlobal,
-                       "8": self.keyNumberGlobal,
-                       "9": self.keyNumberGlobal,
-                       "0": self.keyNumberGlobal
-               }, -1)
+                       "ok": self.ok,
+                       "cancel": self.cancel,
+                       "green": self.add,
+                       "red": self.remove
+               }, -2)
 
                self.list = []
-               self["config"] = ConfigList(self.list)
+               ConfigListScreen.__init__(self, self.list)
                self.createSetup()
-        
-               self["introduction"] = Label("Press OK to activate the settings.")
-        
+
+       def createConfig(self):
+               self.nameservers = iNetwork.getNameserverList()
+               self.nameserverEntries = []
+
+               for nameserver in self.nameservers:
+                       self.nameserverEntries.append(NoSave(ConfigIP(default=nameserver)))
+
        def createSetup(self):
                self.list = []
-        
-               self.list.append(getConfigListEntry(_("Use DHCP"), config.network.dhcp))
-               self.list.append(getConfigListEntry(_('IP Address'), config.network.ip))
-               if (config.network.dhcp.value == 0):
-                       self.list.append(getConfigListEntry(_('Netmask'), config.network.netmask))
-                       self.list.append(getConfigListEntry(_('Gateway'), config.network.gateway))
-                       self.list.append(getConfigListEntry(_('Nameserver'), config.network.dns))
-        
+
+               #self.nameserverConfigEntries = []
+               for i in range(len(self.nameserverEntries)):
+                       self.list.append(getConfigListEntry(_("Nameserver %d") % (i + 1), self.nameserverEntries[i]))
+
                self["config"].list = self.list
                self["config"].l.setList(self.list)
-        
+
+       def ok(self):
+               iNetwork.clearNameservers()
+               for nameserver in self.nameserverEntries:
+                       iNetwork.addNameserver(nameserver.value)
+               iNetwork.writeNameserverConfig()
+               self.close()
+
+       def cancel(self):
+               iNetwork.clearNameservers()
+               print "backup-list:", self.backupNameserverList
+               for nameserver in self.backupNameserverList:
+                       iNetwork.addNameserver(nameserver)
+               self.close()
+
+       def add(self):
+               iNetwork.addNameserver([0,0,0,0])
+               self.createConfig()
+               self.createSetup()
+
+       def remove(self):
+               print "currentIndex:", self["config"].getCurrentIndex()
+
+               index = self["config"].getCurrentIndex()
+               if index < len(self.nameservers):
+                       iNetwork.removeNameserver(self.nameservers[index])
+                       self.createConfig()
+                       self.createSetup()
+
+class AdapterSetup(Screen, ConfigListScreen):
+       def __init__(self, session, iface):
+               Screen.__init__(self, session)
+
+               self.iface = iface
+
+               print iNetwork.getAdapterAttribute(self.iface, "dhcp")
+               self.dhcpConfigEntry = NoSave(ConfigYesNo(default=iNetwork.getAdapterAttribute(self.iface, "dhcp") or False))
+               self.hasGatewayConfigEntry = NoSave(ConfigYesNo(default=True))
+               self.ipConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "ip")) or [0,0,0,0])
+               self.netmaskConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "netmask") or [255,0,0,0]))
+               self.gatewayConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "gateway") or [0,0,0,0]))
+       
+               self["iface"] = Label(iNetwork.getFriendlyAdapterName(self.iface))
+
+               self["actions"] = ActionMap(["SetupActions"],
+               {
+                       "ok": self.ok,
+                       "cancel": self.cancel,
+               }, -2)
+
+               self.list = []
+               ConfigListScreen.__init__(self, self.list)
+               self.createSetup()
+
+               self["introduction"] = Label(_("Press OK to activate the settings."))
+               self["introduction2"] = Label(_("Current settings:"))
+               
+               self["IPtext"] = Label(_("IP Address"))
+               self["Netmasktext"] = Label(_("Netmask"))
+               self["Gatewaytext"] = Label(_("Gateway"))
+               
+               self["IP"] = Label(self.ipConfigEntry.getText())
+               self["Mask"] = Label(self.netmaskConfigEntry.getText())
+               self["Gateway"] = Label(self.gatewayConfigEntry.getText())
+
+       def createSetup(self):
+               self.list = []
+
+               self.dhcpEntry = getConfigListEntry(_("Use DHCP"), self.dhcpConfigEntry)
+               self.list.append(self.dhcpEntry)
+               if not self.dhcpConfigEntry.value:
+                       self.list.append(getConfigListEntry(_('IP Address'), self.ipConfigEntry))
+                       self.list.append(getConfigListEntry(_('Netmask'), self.netmaskConfigEntry))
+                       self.list.append(getConfigListEntry(_('Use a gateway'), self.hasGatewayConfigEntry))
+                       if self.hasGatewayConfigEntry.value:
+                               self.list.append(getConfigListEntry(_('Gateway'), self.gatewayConfigEntry))
+
+               self.extended = None
+               self.extendedSetup = None
+               for p in plugins.getPlugins(PluginDescriptor.WHERE_NETWORKSETUP):
+                       callFnc = p.__call__["ifaceSupported"](self.iface)
+                       if callFnc is not None:
+                               self.extended = callFnc
+                               print p.__call__
+                               if p.__call__.has_key("configStrings"):
+                                       self.configStrings = p.__call__["configStrings"]
+                               else:
+                                       self.configStrings = None
+
+                               if p.__call__.has_key("menuEntryName"):
+                                       menuEntryName = p.__call__["menuEntryName"](self.iface)
+                               else:
+                                       menuEntryName = _('Extended Setup...')
+                               self.extendedSetup = getConfigListEntry(menuEntryName, NoSave(ConfigNothing()))
+                               self.list.append(self.extendedSetup)
+
+               self["config"].list = self.list
+               self["config"].l.setList(self.list)
+
        def newConfig(self):
                print self["config"].getCurrent()
-               if self["config"].getCurrent()[0] == _("Use DHCP"):
+               if self["config"].getCurrent() == self.dhcpEntry:
                        self.createSetup()
 
        def keyLeft(self):
-               self["config"].handleKey(config.key["prevElement"])
-               self.newConfig()
+               ConfigListScreen.keyLeft(self)
+               self.createSetup()
 
        def keyRight(self):
-               self["config"].handleKey(config.key["nextElement"])
-               self.newConfig()
-    
-       def keyNumberGlobal(self, number):
-               print "You pressed number " + str(number)
-               if (self["config"].getCurrent()[1].parent.enabled == True):
-                       self["config"].handleKey(config.key[str(number)])
-        
-       def keySave(self):
-               #for x in self["config"].list:
-                       #x[1].save()
-        
-               iNetwork.writeNetworkConfig()    
-               iNetwork.activateNetworkConfig()
-               self.close()
+               ConfigListScreen.keyRight(self)
+               self.createSetup()
+
+       def ok(self):
+               selection = self["config"].getCurrent()
+               if selection == self.extendedSetup:
+                       self.extended(self.session, self.iface)
+               else:
+                       iNetwork.setAdapterAttribute(self.iface, "dhcp", self.dhcpConfigEntry.value)
+                       iNetwork.setAdapterAttribute(self.iface, "ip", self.ipConfigEntry.value)
+                       iNetwork.setAdapterAttribute(self.iface, "netmask", self.netmaskConfigEntry.value)
+                       if self.hasGatewayConfigEntry.value:
+                               iNetwork.setAdapterAttribute(self.iface, "gateway", self.gatewayConfigEntry.value)
+                       else:
+                               iNetwork.removeAdapterAttribute(self.iface, "gateway")
 
-       def keyCancel(self):
-               for x in self["config"].list:
-                       x[1].cancel()
-               iNetwork.loadNetworkConfig()
-               self.close()
\ No newline at end of file
+                       if self.extended is not None and self.configStrings is not None:
+                               iNetwork.setAdapterAttribute(self.iface, "configStrings", self.configStrings(self.iface))
+
+                       iNetwork.deactivateNetworkConfig()
+                       iNetwork.writeNetworkConfig()
+                       iNetwork.activateNetworkConfig()
+                       self.close()
+
+       def cancel(self):
+               iNetwork.getInterfaces()
+               self.close()