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