plugins are now able to add device specific text to /etc/networking/interfaces
[enigma2.git] / lib / python / Screens / NetworkSetup.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.ConfigList import ConfigList, ConfigListScreen
4 from Components.config import config, getConfigListEntry
5 from Components.Network import iNetwork
6 from Components.Label import Label
7 from Components.MenuList import MenuList
8 from Components.config import config, ConfigYesNo, ConfigIP, NoSave, ConfigSubsection, ConfigNothing
9 from Components.PluginComponent import plugins
10 from Plugins.Plugin import PluginDescriptor
11
12
13 class NetworkAdapterSelection(Screen):
14         def __init__(self, session):
15                 Screen.__init__(self, session)
16                 
17                 self["adapterlist"] = MenuList(iNetwork.getAdapterList())
18                 
19                 self["actions"] = ActionMap(["OkCancelActions"],
20                 {
21                         "ok": self.okbuttonClick ,
22                         "cancel": self.close
23                 })
24
25         def okbuttonClick(self):
26                 selection = self["adapterlist"].getCurrent()
27                 print "selection:", selection
28                 if selection is not None:
29                         self.session.open(AdapterSetup, selection)
30                         
31 class NameserverSetup(Screen, ConfigListScreen):
32         def __init__(self, session):
33                 Screen.__init__(self, session)
34                 self.backupNameserverList = iNetwork.getNameserverList()[:]
35                 print "backup-list:", self.backupNameserverList
36                 
37                 self["red"] = Label(_("Delete"))
38                 self["green"] = Label(_("Add"))
39                 
40                 self.createConfig()
41                 
42                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
43                 {
44                         "ok": self.ok,
45                         "cancel": self.cancel,
46                         "green": self.add,
47                         "red": self.remove
48                 }, -2)
49
50                 self.list = []
51                 ConfigListScreen.__init__(self, self.list)
52                 self.createSetup()
53                 
54         def createConfig(self):
55                 self.nameservers = iNetwork.getNameserverList()
56                 self.nameserverEntries = []
57                 
58                 for nameserver in self.nameservers:
59                         self.nameserverEntries.append(NoSave(ConfigIP(default=nameserver)))
60                         
61         def createSetup(self):
62                 self.list = []
63
64                 #self.nameserverConfigEntries = []
65                 for i in range(len(self.nameserverEntries)):
66                         self.list.append(getConfigListEntry(_("Nameserver %d") % (i + 1), self.nameserverEntries[i]))
67                 
68                 self["config"].list = self.list
69                 self["config"].l.setList(self.list)
70
71         def ok(self):
72                 iNetwork.clearNameservers()
73                 for nameserver in self.nameserverEntries:
74                         iNetwork.addNameserver(nameserver.value)
75                 iNetwork.writeNameserverConfig()
76                 self.close()
77         
78         def cancel(self):
79                 iNetwork.clearNameservers()
80                 print "backup-list:", self.backupNameserverList
81                 for nameserver in self.backupNameserverList:
82                         iNetwork.addNameserver(nameserver)
83                 self.close()
84         
85         def add(self):
86                 iNetwork.addNameserver([0,0,0,0])
87                 self.createConfig()
88                 self.createSetup()
89         
90         def remove(self):
91                 print "currentIndex:", self["config"].getCurrentIndex()
92                 
93                 index = self["config"].getCurrentIndex()
94                 if index < len(self.nameservers):
95                         iNetwork.removeNameserver(self.nameservers[index])
96                         self.createConfig()
97                         self.createSetup()
98
99                 
100 class AdapterSetup(Screen, ConfigListScreen):
101         def __init__(self, session, iface):
102                 Screen.__init__(self, session)
103                 
104                 self.iface = iface
105
106                 print iNetwork.getAdapterAttribute(self.iface, "dhcp")
107                 self.dhcpConfigEntry = NoSave(ConfigYesNo(default=iNetwork.getAdapterAttribute(self.iface, "dhcp")))
108                 self.hasGatewayConfigEntry = NoSave(ConfigYesNo(default=True))
109                 self.ipConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "ip")))
110                 self.netmaskConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "netmask")))
111                 self.gatewayConfigEntry = NoSave(ConfigIP(default=iNetwork.getAdapterAttribute(self.iface, "gateway")))
112         
113                 self["iface"] = Label(iNetwork.getAdapterName(self.iface))
114                         
115                 self["actions"] = ActionMap(["SetupActions"],
116                 {
117                         "ok": self.ok,
118                         "cancel": self.cancel,
119                 }, -2)
120
121                 self.list = []
122                 ConfigListScreen.__init__(self, self.list)
123                 self.createSetup()
124
125                 self["introduction"] = Label(_("Press OK to activate the settings."))
126
127         def createSetup(self):
128                 self.list = []
129
130                 self.dhcpEntry = getConfigListEntry(_("Use DHCP"), self.dhcpConfigEntry)
131                 self.list.append(self.dhcpEntry)
132                 if not self.dhcpConfigEntry.value:
133                         self.list.append(getConfigListEntry(_('IP Address'), self.ipConfigEntry))
134                         self.list.append(getConfigListEntry(_('Netmask'), self.netmaskConfigEntry))
135                         self.list.append(getConfigListEntry(_('Use a gateway'), self.hasGatewayConfigEntry))
136                         if self.hasGatewayConfigEntry.value:
137                                 self.list.append(getConfigListEntry(_('Gateway'), self.gatewayConfigEntry))
138                 
139                 self.extended = None
140                 for p in plugins.getPlugins(PluginDescriptor.WHERE_NETWORKSETUP):
141                         callFnc = p.__call__["ifaceSupported"](self.iface)
142                         if callFnc is not None:
143                                 self.extended = callFnc
144                                 print p.__call__
145                                 if p.__call__.has_key("configStrings"):
146                                         self.configStrings = p.__call__["configStrings"]
147                                 else:
148                                         self.configStrings = None
149                                 self.extendedSetup = getConfigListEntry(_('Extended Setup...'), NoSave(ConfigNothing()))
150                                 self.list.append(self.extendedSetup)
151
152                 self["config"].list = self.list
153                 self["config"].l.setList(self.list)
154
155         def newConfig(self):
156                 print self["config"].getCurrent()
157                 if self["config"].getCurrent() == self.dhcpEntry:
158                         self.createSetup()
159
160         def keyLeft(self):
161                 ConfigListScreen.keyLeft(self)
162                 self.createSetup()
163
164         def keyRight(self):
165                 ConfigListScreen.keyRight(self)
166                 self.createSetup()
167
168         def ok(self):
169                 selection = self["config"].getCurrent()
170                 if selection == self.extendedSetup:
171                         self.extended(self.session, self.iface)
172                 else:
173                         iNetwork.setAdapterAttribute(self.iface, "dhcp", self.dhcpConfigEntry.value)
174                         iNetwork.setAdapterAttribute(self.iface, "ip", self.ipConfigEntry.value)
175                         iNetwork.setAdapterAttribute(self.iface, "netmask", self.netmaskConfigEntry.value)
176                         if self.hasGatewayConfigEntry.value:
177                                 iNetwork.setAdapterAttribute(self.iface, "gateway", self.gatewayConfigEntry.value)
178                         else:
179                                 iNetwork.removeAdapterAttribute(self.iface, "gateway")
180                         
181                         print "self.extended:", self.extended
182                         print "self.configStrings:", self.configStrings
183                         if self.extended is not None and self.configStrings is not None:
184                                 iNetwork.setAdapterAttribute(self.iface, "configStrings", self.configStrings(self.iface))
185
186                         iNetwork.deactivateNetworkConfig()
187                         iNetwork.writeNetworkConfig()    
188                         iNetwork.activateNetworkConfig()
189                         self.close()
190
191         def cancel(self):
192                 iNetwork.getInterfaces()
193                 self.close()