1 /*
2 * Copyright 2016-2017, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
5 #include "NetworkConnectionConfigView.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include <LayoutBuilder.h>
11 #include <MenuField.h>
12 #include <TextControl.h>
13
14 #include "Settings.h"
15 #include "SettingsDescription.h"
16 #include "TargetHostInterfaceInfo.h"
17
18
19 enum {
20 MSG_NET_CONFIG_INPUT_CHANGED = 'ncic'
21 };
22
23
24 static const char* kHostSetting = "hostname";
25 static const char* kPortSetting = "port";
26
27
NetworkConnectionConfigView()28 NetworkConnectionConfigView::NetworkConnectionConfigView()
29 :
30 ConnectionConfigView("NetworkConnectionConfig"),
31 fProtocolField(NULL),
32 fHostInput(NULL),
33 fPortInput(NULL),
34 fSettings(NULL),
35 fHostSetting(NULL),
36 fPortSetting(NULL)
37 {
38 }
39
40
~NetworkConnectionConfigView()41 NetworkConnectionConfigView::~NetworkConnectionConfigView()
42 {
43 if (fSettings != NULL)
44 fSettings->ReleaseReference();
45 }
46
47
48 void
AttachedToWindow()49 NetworkConnectionConfigView::AttachedToWindow()
50 {
51 ConnectionConfigView::AttachedToWindow();
52
53 fHostInput->SetTarget(this);
54 fPortInput->SetTarget(this);
55 }
56
57
58 void
MessageReceived(BMessage * message)59 NetworkConnectionConfigView::MessageReceived(BMessage* message)
60 {
61 switch (message->what) {
62 case MSG_NET_CONFIG_INPUT_CHANGED:
63 {
64 fSettings->SetValue(fHostSetting, fHostInput->Text());
65 uint16 port = (uint16)strtoul(fPortInput->Text(), NULL, 10);
66 fSettings->SetValue(fPortSetting, port);
67 NotifyConfigurationChanged(fSettings);
68 break;
69 }
70 default:
71 {
72 ConnectionConfigView::MessageReceived(message);
73 break;
74 }
75 }
76 }
77
78
79 status_t
InitSpecific()80 NetworkConnectionConfigView::InitSpecific()
81 {
82 SettingsDescription* description
83 = InterfaceInfo()->GetSettingsDescription();
84
85 for (int32 i = 0; i < description->CountSettings(); i++) {
86 Setting* setting = description->SettingAt(i);
87 if (strcmp(setting->ID(), kPortSetting) == 0)
88 fPortSetting = setting;
89 else if (strcmp(setting->ID(), kHostSetting) == 0)
90 fHostSetting = setting;
91 }
92
93 if (fPortSetting == NULL || fHostSetting == NULL)
94 return B_BAD_VALUE;
95
96 fSettings = new Settings(description);
97 fPortInput = new BTextControl("port_input", "Port:", "",
98 new BMessage(MSG_NET_CONFIG_INPUT_CHANGED));
99
100 BLayoutItem* textLayoutItem;
101 BLayoutBuilder::Group<>(this, B_HORIZONTAL)
102 .Add((fHostInput = new BTextControl("host_input", "Host:", "",
103 new BMessage(MSG_NET_CONFIG_INPUT_CHANGED))))
104 .Add(fPortInput->CreateLabelLayoutItem())
105 .Add((textLayoutItem = fPortInput->CreateTextViewLayoutItem()))
106 .End();
107
108 // since port numbers are limited to 5 digits, there's no need for
109 // the port input to expand farther than that. Instead, let the
110 // host field take the extra space.
111 textLayoutItem->SetExplicitMaxSize(BSize(
112 be_plain_font->StringWidth("999999"), B_SIZE_UNSET));
113
114
115 BString buffer;
116 buffer.SetToFormat("%" B_PRIu16, fPortSetting->DefaultValue().ToUInt16());
117 fPortInput->SetText(buffer);
118
119 fHostInput->SetModificationMessage(
120 new BMessage(MSG_NET_CONFIG_INPUT_CHANGED));
121 fPortInput->SetModificationMessage(
122 new BMessage(MSG_NET_CONFIG_INPUT_CHANGED));
123
124 return B_OK;
125 }
126
127
128