xref: /haiku/src/preferences/network/IPAddressControl.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 /*
2  * Copyright 2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, <axeld@pinc-software.de>
7  */
8 
9 
10 #include "IPAddressControl.h"
11 
12 #include <NetworkAddress.h>
13 
14 
15 static const uint32 kMsgModified = 'txmd';
16 
17 
18 IPAddressControl::IPAddressControl(int family, const char* label,
19 	const char* name)
20 	:
21 	BTextControl(name, label, "", NULL),
22 	fFamily(family),
23 	fAllowEmpty(true)
24 {
25 	SetModificationMessage(new BMessage(kMsgModified));
26 }
27 
28 
29 IPAddressControl::~IPAddressControl()
30 {
31 }
32 
33 
34 bool
35 IPAddressControl::AllowEmpty() const
36 {
37 	return fAllowEmpty;
38 }
39 
40 
41 void
42 IPAddressControl::SetAllowEmpty(bool empty)
43 {
44 	fAllowEmpty = empty;
45 }
46 
47 
48 void
49 IPAddressControl::AttachedToWindow()
50 {
51 	BTextControl::AttachedToWindow();
52 	SetTarget(this);
53 	_UpdateMark();
54 }
55 
56 
57 void
58 IPAddressControl::MessageReceived(BMessage* message)
59 {
60 	switch (message->what) {
61 		case kMsgModified:
62 			_UpdateMark();
63 			break;
64 
65 		default:
66 			BTextControl::MessageReceived(message);
67 			break;
68 	}
69 }
70 
71 
72 void
73 IPAddressControl::_UpdateMark()
74 {
75 	if (TextLength() == 0) {
76 		MarkAsInvalid(!fAllowEmpty);
77 		return;
78 	}
79 
80 	BNetworkAddress address;
81 	bool success = address.SetTo(fFamily, Text(), (char*)NULL,
82 		B_NO_ADDRESS_RESOLUTION) == B_OK;
83 
84 	MarkAsInvalid(!success);
85 }
86