summaryrefslogtreecommitdiffstats
path: root/iw/network.py
blob: 6bcf293bad3ffe6130dcc6154f8ce9e2a7d436c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from gtk import *
from iw import *
from isys import *
from gui import _

class NetworkWindow (InstallWindow):		

    def __init__ (self, ics):
	InstallWindow.__init__ (self, ics)

        ics.setTitle ("Network Configuration")
        ics.setNextEnabled (1)
        ics.readHTML ("netconf")
        self.todo = ics.getToDo ()
        self.calcNMHandler = None

    def getNext (self):
	if not self.__dict__.has_key("gw"):
	    return None
        self.todo.network.gateway = self.gw.get_text ()
        self.todo.network.primaryNS = self.ns.get_text ()
        self.todo.network.secondaryNS = self.ns2.get_text ()
        self.todo.network.ternaryNS = self.ns3.get_text ()
        if (self.hostname.get_text () != ""):
            self.todo.network.hostname = self.hostname.get_text ()
        return None

    def focusInIP (self, widget, event, (ip, nm)):
        if nm.get_text() == "":
            self.calcNetmask (None, (ip, nm))
            ip.calcNMHandler = ip.connect ("changed", self.calcNetmask, (ip, nm))

    def focusOutIP (self, widget, event, ip):
        self.todo.network.guessHostnames ()
        if (self.hostname.get_text () == ""
            and self.todo.network.hostname != "localhost.localdomain"):
            self.hostname.set_text (self.todo.network.hostname)

        if ip.calcNMHandler != None:
            ip.disconnect (ip.calcNMHandler)
            ip.calcNMHandler = None

    def focusOutNM (self, widget, event, (dev, ip, nm, nw, bc)):
        try:
            network, broadcast = inet_calcNetBroad (ip.get_text (), nm.get_text ())
            if nw.get_text () == "":
                nw.set_text (network)
                dev.set (("network", network))
            if bc.get_text () == "":
                bc.set_text (broadcast)
                dev.set (("broadcast", broadcast))
        except:
            pass

    def focusOutBC (self, widget, event, dev):
        if self.gw.get_text () == "":
            try:
                gw = inet_calcGateway (widget.get_text ())
                self.gw.set_text (gw)
            except:
                pass

    def focusOutNW (self, widget, event, dev):
        if self.ns.get_text () == "":
            try:
                ns = inet_calcNS (widget.get_text ())
                self.ns.set_text (ns)
            except:
                pass
            
    # not currently used
    def setupTODO (self):
        if self.devs:
            if self.DHCPcb.get_active ():
                self.dev.set (("bootproto", "dhcp"))
                self.dev.unset ("ipaddr", "netmask", "network", "broadcast")
            else:
                try:
                    network, broadcast = inet_calcNetBroad (self.ip.get_text (), self.nm.get_text ())
                    self.dev.set (("bootproto", "static"))
                    self.dev.set (("ipaddr", self.ip.get_text ()), ("netmask", self.nm.get_text ()),
                                  ("network", network), ("broadcast", broadcast), ("onboot", "yes"))
                    self.todo.network.gateway = self.gw.get_text ()
                    self.todo.network.primaryNS = self.dns1.get_text ()
                    self.todo.network.guessHostnames ()
                except:
                    pass
            
                self.dev.set (("onboot", "yes"))


    def calcNWBC (self, widget, (dev, ip, nm, nw, bc)):
        for addr in (ip, nm):
            dots = 0
            for ch in addr.get_text ():
                if ch == '.':
                    dots = dots + 1
            if dots != 3: return

        dev.set (("ipaddr", ip.get_text ()))
        dev.set (("netmask", nm.get_text ()))

        
    def calcNetmask (self, widget, (ip, nm)):
        ip = ip.get_text ()
        dots = 0
        for x in ip:
            if x == '.':
                dots = dots + 1
        if dots != 3: return

        new_nm = inet_calcNetmask (ip)
        if (new_nm != nm.get_text ()):
            nm.set_text (new_nm)

    def DHCPtoggled (self, widget, table):
        table.set_sensitive (not widget.get_active ())
        self.ipTable.set_sensitive (not widget.get_active ())

    def getScreen (self):
        box = GtkVBox ()
        box.set_border_width (5)
        
        notebook = GtkNotebook ()
        devs = self.todo.network.available ()
        if devs:
            devs.keys ().sort ()
            for i in devs.keys ():
                devbox = GtkVBox ()
                align = GtkAlignment ()
                DHCPcb = GtkCheckButton (_("Configure using DHCP"))
                DHCPcb.set_active (devs[i].get ("bootproto") == "dhcp")
        
                align.add (DHCPcb)
                devbox.pack_start (align, FALSE)
                
                align = GtkAlignment ()
                bootcb = GtkCheckButton (_("Activate on boot"))
                devs[i].set (("onboot", "yes"))  # TEMPRORY FIX UNTIL TODO SETS THIS
                bootcb.set_active (devs[i].get ("onboot") == "yes")
                align.add (bootcb)

                devbox.pack_start (align, FALSE)

                devbox.pack_start (GtkHSeparator (), FALSE, padding=3)

                options = [_("IP Address"), _("Netmask"), _("Network"), _("Broadcast")]
                ipTable = GtkTable (len (options), 2)

                DHCPcb.connect ("toggled", self.DHCPtoggled, ipTable)

		forward = lambda widget, box=box: box.focus (DIR_TAB_FORWARD)

                for t in range (len (options)):
                    label = GtkLabel ("%s:" % (options[t],))
                    label.set_alignment (0.0, 0.5)
                    ipTable.attach (label, 0, 1, t, t+1, FILL, 0, 10)
                    entry = GtkEntry (15)
                    # entry.set_usize (gdk_char_width (entry.get_style ().font, '0')*15, -1)
                    entry.set_usize (7 * 15, -1)
                    entry.connect ("activate", forward)
                    options[t] = entry
                    ipTable.attach (entry, 1, 2, t, t+1, 0, FILL|EXPAND)


                for t in range (len (options)):
                    if t == 0 or t == 1:
                        options[t].connect ("changed", self.calcNWBC, (devs[i],) + tuple (options))
#                      else:
#                          options[t].set_sensitive (FALSE)
#                          options[t].set_editable (FALSE)
#                          options[t]['can_focus'] = FALSE

                # add event handlers for the main IP widget to calcuate the netmask
                options[0].connect ("focus_in_event", self.focusInIP, (options[0], options[1]))
                options[0].connect ("focus_out_event", self.focusOutIP, options[0])
                options[1].connect ("focus_out_event", self.focusOutNM, (devs[i],) + tuple (options))
                options[2].connect ("focus_out_event", self.focusOutNW, devs[i])
                options[3].connect ("focus_out_event", self.focusOutBC, devs[i])

                devbox.pack_start (ipTable, FALSE, FALSE, 5)
                notebook.append_page (devbox, GtkLabel (i))

            box.pack_start (notebook, FALSE)
            box.pack_start (GtkHSeparator (), FALSE, padding=10)

            options = [_("Hostname"),
                       _("Gateway"), _("Primary DNS"), _("Secondary DNS"), _("Ternary DNS")]
            ipTable = GtkTable (len (options), 2)
            for i in range (len (options)):
                label = GtkLabel ("%s:" % (options[i],))
                label.set_alignment (0.0, 0.0)
                ipTable.attach (label, 0, 1, i, i+1, FILL, 0, 10)
                if i == 0:
                    options[i] = GtkEntry ()
                    options[i].set_usize (7 * 30, -1)
                else:
                    options[i] = GtkEntry (15)
                    options[i].set_usize (7 * 15, -1)
                options[i].connect ("activate", forward)
                align = GtkAlignment (0, 0.5)
                align.add (options[i])
                ipTable.attach (align, 1, 2, i, i+1, FILL, 0)
            ipTable.set_row_spacing (0, 5)

            self.ipTable = ipTable
            
            self.hostname = options[0]
            self.gw = options[1]
            self.ns = options[2]
            self.ns2 = options[3]
            self.ns3 = options[4]
            box.pack_start (ipTable, FALSE, FALSE, 5)
        return box