BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / add-ons / network_settings / dialup / IPCPAddon.cpp
blob26d8d6ac00e69974dfc49ec388526ee723653a1e
1 /*
2 * Copyright 2003-2004 Waldemar Kornewald. All rights reserved.
3 * Copyright 2017 Haiku, Inc. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
7 //-----------------------------------------------------------------------
8 // IPCPAddon saves the loaded settings.
9 // IPCPView saves the current settings.
10 //-----------------------------------------------------------------------
12 #include "IPCPAddon.h"
14 #include "MessageDriverSettingsUtils.h"
16 #include <StringView.h>
17 #include <LayoutBuilder.h>
18 #include <SpaceLayoutItem.h>
20 #include <PPPDefs.h>
21 #include <IPCP.h>
22 // from IPCP addon
25 // GUI constants
26 static const uint32 kDefaultButtonWidth = 80;
28 // message constants
29 static const uint32 kMsgUpdateControls = 'UCTL';
31 // labels
32 static const char *kLabelIPCP = "TCP/IP";
33 static const char *kLabelIPAddress = "IP Address: ";
34 static const char *kLabelPrimaryDNS = "Primary DNS: ";
35 static const char *kLabelSecondaryDNS = "Secondary DNS: ";
36 static const char *kLabelOptional = "(Optional)";
37 static const char *kLabelExtendedOptions = "Extended Options:";
38 static const char *kLabelEnabled = "Enable TCP/IP Protocol";
40 // add-on descriptions
41 static const char *kKernelModuleName = "ipcp";
44 IPCPAddon::IPCPAddon(BMessage *addons)
45 : DialUpAddon(addons),
46 fSettings(NULL),
47 fProfile(NULL),
48 fIPCPView(NULL)
53 IPCPAddon::~IPCPAddon()
58 bool
59 IPCPAddon::LoadSettings(BMessage *settings, BMessage *profile, bool isNew)
61 fIsNew = isNew;
62 fIsEnabled = false;
63 fIPAddress = fPrimaryDNS = fSecondaryDNS = "";
64 fSettings = settings;
65 fProfile = profile;
67 if(fIPCPView)
68 fIPCPView->Reload();
69 // reset all views (empty settings)
71 if(!settings || !profile || isNew)
72 return true;
74 BMessage protocol;
76 // settings
77 int32 protocolIndex = FindIPCPProtocol(*fSettings, &protocol);
78 if(protocolIndex < 0)
79 return true;
81 protocol.AddBool(MDSU_VALID, true);
82 fSettings->ReplaceMessage(MDSU_PARAMETERS, protocolIndex, &protocol);
84 // profile
85 protocolIndex = FindIPCPProtocol(*fProfile, &protocol);
86 if(protocolIndex < 0)
87 return true;
89 fIsEnabled = true;
91 // the "Local" side parameter
92 BMessage local;
93 int32 localSideIndex = 0;
94 if(!FindMessageParameter(IPCP_LOCAL_SIDE_KEY, protocol, &local, &localSideIndex))
95 local.MakeEmpty();
96 // just fall through and pretend we have an empty "Local" side parameter
98 // now load the supported parameters (the client-relevant subset)
99 BString name;
100 BMessage parameter;
101 int32 index = 0;
102 if(!FindMessageParameter(IPCP_IP_ADDRESS_KEY, local, &parameter, &index)
103 || parameter.FindString(MDSU_VALUES, &fIPAddress) != B_OK)
104 fIPAddress = "";
105 else {
106 if(fIPAddress == "auto")
107 fIPAddress = "";
109 parameter.AddBool(MDSU_VALID, true);
110 local.ReplaceMessage(MDSU_PARAMETERS, index, &parameter);
113 index = 0;
114 if(!FindMessageParameter(IPCP_PRIMARY_DNS_KEY, local, &parameter, &index)
115 || parameter.FindString(MDSU_VALUES, &fPrimaryDNS) != B_OK)
116 fPrimaryDNS = "";
117 else {
118 if(fPrimaryDNS == "auto")
119 fPrimaryDNS = "";
121 parameter.AddBool(MDSU_VALID, true);
122 local.ReplaceMessage(MDSU_PARAMETERS, index, &parameter);
125 index = 0;
126 if(!FindMessageParameter(IPCP_SECONDARY_DNS_KEY, local, &parameter, &index)
127 || parameter.FindString(MDSU_VALUES, &fSecondaryDNS) != B_OK)
128 fSecondaryDNS = "";
129 else {
130 if(fSecondaryDNS == "auto")
131 fSecondaryDNS = "";
133 parameter.AddBool(MDSU_VALID, true);
134 local.ReplaceMessage(MDSU_PARAMETERS, index, &parameter);
137 local.AddBool(MDSU_VALID, true);
138 protocol.ReplaceMessage(MDSU_PARAMETERS, localSideIndex, &local);
139 protocol.AddBool(MDSU_VALID, true);
140 fProfile->ReplaceMessage(MDSU_PARAMETERS, protocolIndex, &protocol);
142 if(fIPCPView)
143 fIPCPView->Reload();
145 return true;
149 void
150 IPCPAddon::IsModified(bool *settings, bool *profile) const
152 if(!fSettings) {
153 *settings = *profile = false;
154 return;
157 *settings = fIsEnabled != fIPCPView->IsEnabled();
158 *profile = (*settings || fIPAddress != fIPCPView->IPAddress()
159 || fPrimaryDNS != fIPCPView->PrimaryDNS()
160 || fSecondaryDNS != fIPCPView->SecondaryDNS());
164 bool
165 IPCPAddon::SaveSettings(BMessage *settings, BMessage *profile, bool saveTemporary)
167 if(!fSettings || !settings)
168 return false;
170 if(!fIPCPView->IsEnabled())
171 return true;
173 BMessage protocol, local;
174 protocol.AddString(MDSU_NAME, PPP_PROTOCOL_KEY);
175 protocol.AddString(MDSU_VALUES, kKernelModuleName);
176 settings->AddMessage(MDSU_PARAMETERS, &protocol);
177 // the settings contain a simple "protocol ipcp" string
179 // now create the profile with all subparameters
180 local.AddString(MDSU_NAME, IPCP_LOCAL_SIDE_KEY);
181 bool needsLocal = false;
183 if(fIPCPView->IPAddress() && strlen(fIPCPView->IPAddress()) > 0) {
184 // save IP address, too
185 needsLocal = true;
186 BMessage ip;
187 ip.AddString(MDSU_NAME, IPCP_IP_ADDRESS_KEY);
188 ip.AddString(MDSU_VALUES, fIPCPView->IPAddress());
189 local.AddMessage(MDSU_PARAMETERS, &ip);
192 if(fIPCPView->PrimaryDNS() && strlen(fIPCPView->PrimaryDNS()) > 0) {
193 // save service name, too
194 needsLocal = true;
195 BMessage dns;
196 dns.AddString(MDSU_NAME, IPCP_PRIMARY_DNS_KEY);
197 dns.AddString(MDSU_VALUES, fIPCPView->PrimaryDNS());
198 local.AddMessage(MDSU_PARAMETERS, &dns);
201 if(fIPCPView->SecondaryDNS() && strlen(fIPCPView->SecondaryDNS()) > 0) {
202 // save service name, too
203 needsLocal = true;
204 BMessage dns;
205 dns.AddString(MDSU_NAME, IPCP_SECONDARY_DNS_KEY);
206 dns.AddString(MDSU_VALUES, fIPCPView->SecondaryDNS());
207 local.AddMessage(MDSU_PARAMETERS, &dns);
210 if(needsLocal)
211 protocol.AddMessage(MDSU_PARAMETERS, &local);
213 profile->AddMessage(MDSU_PARAMETERS, &protocol);
215 return true;
219 bool
220 IPCPAddon::GetPreferredSize(float *width, float *height) const
222 BRect rect;
223 if(Addons()->FindRect(DUN_TAB_VIEW_RECT, &rect) != B_OK)
224 rect.Set(0, 0, 200, 300);
225 // set default values
227 if(width)
228 *width = rect.Width();
229 if(height)
230 *height = rect.Height();
232 return true;
236 BView*
237 IPCPAddon::CreateView()
239 if (!fIPCPView) {
240 fIPCPView = new IPCPView(this);
241 fIPCPView->Reload();
244 return fIPCPView;
248 int32
249 IPCPAddon::FindIPCPProtocol(const BMessage& message, BMessage *protocol) const
251 if(!protocol)
252 return -1;
254 BString name;
255 for(int32 index = 0; FindMessageParameter(PPP_PROTOCOL_KEY, message, protocol,
256 &index); index++)
257 if(protocol->FindString(MDSU_VALUES, &name) == B_OK
258 && name == kKernelModuleName)
259 return index;
261 return -1;
265 IPCPView::IPCPView(IPCPAddon *addon)
266 : BView(kLabelIPCP, 0),
267 fAddon(addon)
269 fIPAddress = new BTextControl("ip", kLabelIPAddress, NULL, NULL);
270 fPrimaryDNS = new BTextControl("primaryDNS", kLabelPrimaryDNS, NULL, NULL);
271 fSecondaryDNS = new BTextControl("secondaryDNS", kLabelSecondaryDNS, NULL,
272 NULL);
273 fIsEnabled = new BCheckBox("isEnabled", kLabelEnabled,
274 new BMessage(kMsgUpdateControls));
276 BLayoutBuilder::Grid<>(this, B_USE_HALF_ITEM_SPACING)
277 .SetInsets(B_USE_HALF_ITEM_INSETS)
278 .Add(fIsEnabled, 0, 0)
279 .Add(new BStringView("expert", kLabelExtendedOptions), 0, 1)
280 .Add(fIPAddress, 0, 2)
281 .Add(new BStringView("optional_1", kLabelOptional), 1, 2)
282 .Add(fPrimaryDNS, 0, 3)
283 .Add(new BStringView("optional_2", kLabelOptional), 1, 3)
284 .Add(fSecondaryDNS, 0, 4)
285 .Add(new BStringView("optional_3", kLabelOptional), 1, 4)
286 .Add(BSpaceLayoutItem::CreateGlue(), 0, 5)
287 .End();
291 void
292 IPCPView::Reload()
294 fIsEnabled->SetValue(Addon()->IsEnabled() || Addon()->IsNew());
295 // enable TCP/IP by default
296 fIPAddress->SetText(Addon()->IPAddress());
297 fPrimaryDNS->SetText(Addon()->PrimaryDNS());
298 fSecondaryDNS->SetText(Addon()->SecondaryDNS());
300 UpdateControls();
304 void
305 IPCPView::AttachedToWindow()
307 SetViewColor(Parent()->ViewColor());
308 fIsEnabled->SetTarget(this);
312 void
313 IPCPView::MessageReceived(BMessage *message)
315 switch(message->what) {
316 case kMsgUpdateControls:
317 UpdateControls();
318 break;
320 default:
321 BView::MessageReceived(message);
326 void
327 IPCPView::UpdateControls()
329 fIPAddress->SetEnabled(IsEnabled());
330 fPrimaryDNS->SetEnabled(IsEnabled());
331 fSecondaryDNS->SetEnabled(IsEnabled());