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.
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>
26 static const uint32 kDefaultButtonWidth
= 80;
29 static const uint32 kMsgUpdateControls
= 'UCTL';
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
),
53 IPCPAddon::~IPCPAddon()
59 IPCPAddon::LoadSettings(BMessage
*settings
, BMessage
*profile
, bool isNew
)
63 fIPAddress
= fPrimaryDNS
= fSecondaryDNS
= "";
69 // reset all views (empty settings)
71 if(!settings
|| !profile
|| isNew
)
77 int32 protocolIndex
= FindIPCPProtocol(*fSettings
, &protocol
);
81 protocol
.AddBool(MDSU_VALID
, true);
82 fSettings
->ReplaceMessage(MDSU_PARAMETERS
, protocolIndex
, &protocol
);
85 protocolIndex
= FindIPCPProtocol(*fProfile
, &protocol
);
91 // the "Local" side parameter
93 int32 localSideIndex
= 0;
94 if(!FindMessageParameter(IPCP_LOCAL_SIDE_KEY
, protocol
, &local
, &localSideIndex
))
96 // just fall through and pretend we have an empty "Local" side parameter
98 // now load the supported parameters (the client-relevant subset)
102 if(!FindMessageParameter(IPCP_IP_ADDRESS_KEY
, local
, ¶meter
, &index
)
103 || parameter
.FindString(MDSU_VALUES
, &fIPAddress
) != B_OK
)
106 if(fIPAddress
== "auto")
109 parameter
.AddBool(MDSU_VALID
, true);
110 local
.ReplaceMessage(MDSU_PARAMETERS
, index
, ¶meter
);
114 if(!FindMessageParameter(IPCP_PRIMARY_DNS_KEY
, local
, ¶meter
, &index
)
115 || parameter
.FindString(MDSU_VALUES
, &fPrimaryDNS
) != B_OK
)
118 if(fPrimaryDNS
== "auto")
121 parameter
.AddBool(MDSU_VALID
, true);
122 local
.ReplaceMessage(MDSU_PARAMETERS
, index
, ¶meter
);
126 if(!FindMessageParameter(IPCP_SECONDARY_DNS_KEY
, local
, ¶meter
, &index
)
127 || parameter
.FindString(MDSU_VALUES
, &fSecondaryDNS
) != B_OK
)
130 if(fSecondaryDNS
== "auto")
133 parameter
.AddBool(MDSU_VALID
, true);
134 local
.ReplaceMessage(MDSU_PARAMETERS
, index
, ¶meter
);
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
);
150 IPCPAddon::IsModified(bool *settings
, bool *profile
) const
153 *settings
= *profile
= false;
157 *settings
= fIsEnabled
!= fIPCPView
->IsEnabled();
158 *profile
= (*settings
|| fIPAddress
!= fIPCPView
->IPAddress()
159 || fPrimaryDNS
!= fIPCPView
->PrimaryDNS()
160 || fSecondaryDNS
!= fIPCPView
->SecondaryDNS());
165 IPCPAddon::SaveSettings(BMessage
*settings
, BMessage
*profile
, bool saveTemporary
)
167 if(!fSettings
|| !settings
)
170 if(!fIPCPView
->IsEnabled())
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
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
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
205 dns
.AddString(MDSU_NAME
, IPCP_SECONDARY_DNS_KEY
);
206 dns
.AddString(MDSU_VALUES
, fIPCPView
->SecondaryDNS());
207 local
.AddMessage(MDSU_PARAMETERS
, &dns
);
211 protocol
.AddMessage(MDSU_PARAMETERS
, &local
);
213 profile
->AddMessage(MDSU_PARAMETERS
, &protocol
);
220 IPCPAddon::GetPreferredSize(float *width
, float *height
) const
223 if(Addons()->FindRect(DUN_TAB_VIEW_RECT
, &rect
) != B_OK
)
224 rect
.Set(0, 0, 200, 300);
225 // set default values
228 *width
= rect
.Width();
230 *height
= rect
.Height();
237 IPCPAddon::CreateView()
240 fIPCPView
= new IPCPView(this);
249 IPCPAddon::FindIPCPProtocol(const BMessage
& message
, BMessage
*protocol
) const
255 for(int32 index
= 0; FindMessageParameter(PPP_PROTOCOL_KEY
, message
, protocol
,
257 if(protocol
->FindString(MDSU_VALUES
, &name
) == B_OK
258 && name
== kKernelModuleName
)
265 IPCPView::IPCPView(IPCPAddon
*addon
)
266 : BView(kLabelIPCP
, 0),
269 fIPAddress
= new BTextControl("ip", kLabelIPAddress
, NULL
, NULL
);
270 fPrimaryDNS
= new BTextControl("primaryDNS", kLabelPrimaryDNS
, NULL
, NULL
);
271 fSecondaryDNS
= new BTextControl("secondaryDNS", kLabelSecondaryDNS
, 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)
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());
305 IPCPView::AttachedToWindow()
307 SetViewColor(Parent()->ViewColor());
308 fIsEnabled
->SetTarget(this);
313 IPCPView::MessageReceived(BMessage
*message
)
315 switch(message
->what
) {
316 case kMsgUpdateControls
:
321 BView::MessageReceived(message
);
327 IPCPView::UpdateControls()
329 fIPAddress
->SetEnabled(IsEnabled());
330 fPrimaryDNS
->SetEnabled(IsEnabled());
331 fSecondaryDNS
->SetEnabled(IsEnabled());