summaryrefslogtreecommitdiffstats
path: root/iw/ipwidget.py
blob: f6405599130af1aa75f5c3c05da3a3bf38a54ceb (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
#
# class to create an IP address entry widget and to sanity check entered values
#
# Jonathan Blandford <jrb@redhat.com>
# Michael Fulbright <msf@redhat.com>
#
# Copyright 2002 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import re
import string
import gettext
import gtk
import gobject
import gui
import network
from rhpl.translate import _, N_

ip_fields = ['entry1', 'entry2', 'entry3', 'entry4']

# Includes an error message, and the widget with problems
class IPError(Exception):
    pass

class IPMissing(Exception):
    pass

class IPEditor:
    def __init__ (self):
	self.entrys = {}
	for name in ip_fields:
	    self.entrys[name] = gtk.Entry(3)
	    self.entrys[name].set_size_request(50,-1)
	    self.entrys[name].set_max_length(3)

	for i in range(0, len(ip_fields)):
	    name = ip_fields[i]
	    if name != 'entry4':
		nname = self.entrys[ip_fields[i+1]]
	    else:
		nname = None

	    self.entrys[name].connect('insert_text', self.entry_insert_text_cb, nname)

	hbox = gtk.HBox()
	for name in ip_fields:
	    hbox.pack_start(self.entrys[name], False, False)
	    if name != 'entry4':
		hbox.pack_start(gtk.Label('.'), False, False)

	self.widget = hbox

    def getWidget(self):
	return self.widget

    def getFocusableWidget(self):
        return self.entrys['entry1']
	
    def clear_entries (self):
	for name in ip_fields:
	    self.entrys[name].set_text('')
        
    def hydrate (self, ip_string):
        self.clear_entries()

        octets = network.sanityCheckIPString(ip_string)
        if octets is None:
            return

	i = 0
	for name in ip_fields:
	    self.entrys[name].set_text(octets[i])
	    i = i + 1

    def dehydrate (self):
        widget = None
	# test if empty
	empty = 1
	for e in ['entry1', 'entry2', 'entry3', 'entry4']:
	    if len(string.strip(self.entrys[e].get_text())) > 0:
		empty = 0
		break

	if empty:
	    raise IPMissing, (_("IP Address is missing"), widget)
	    
        try:
            widget = self.entrys['entry1']
            if int(widget.get_text()) > 255 or int(widget.get_text()) <= 0:
                raise IPError, (_("IP Addresses must contain numbers between 1 and 255"), widget)                    
            
            for ent in ['entry2', 'entry3', 'entry4']:
                widget = self.entrys[ent]
                if int(widget.get_text()) > 255:
                    raise IPError, (_("IP Addresses must contain numbers between 0 and 255"), widget)                    
        except ValueError, msg:
            raise IPError, (_("IP Addresses must contain numbers between 0 and 255"), widget)

        return self.entrys['entry1'].get_text() + "." + self.entrys['entry2'].get_text() + "." +self.entrys['entry3'].get_text() + "." +self.entrys['entry4'].get_text()

    def entry_insert_text_cb(self, entry, text, length, pos, next):
        if text == '.':
            entry.emit_stop_by_name ("insert_text")
            if next:
                next.grab_focus()
            return
        reg = re.compile ("[^0-9]+")
        if reg.match (text):
            entry.emit_stop_by_name ("insert_text")



if __name__ == "__main__":
    def output(xxx, data):
	try:
	    print data.dehydrate()
	except:
	    print "oops errors"
	gtk.mainquit()

    win = gtk.Window()
    win.connect('destroy', gtk.mainquit)
    vbox = gtk.VBox()
    ip = IPEditor()
    vbox = gtk.VBox()
    vbox.pack_start(ip.getWidget())
    button = gtk.Button("Quit")
    button.connect("pressed", output, ip)
    vbox.pack_start(button, False, False)
    win.add(vbox)
    win.show_all()
    gtk.main()