xref: /haiku/src/add-ons/network_settings/dialup/ConnectionOptionsAddon.cpp (revision 0fb349bc5d470bc4c826cb401f7a5e296dcca110)
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  */
6 
7 //-----------------------------------------------------------------------
8 // ConnectionOptionsAddon saves the loaded settings.
9 // ConnectionOptionsView saves the current settings.
10 //-----------------------------------------------------------------------
11 
12 #include "ConnectionOptionsAddon.h"
13 
14 #include "MessageDriverSettingsUtils.h"
15 
16 #include <LayoutBuilder.h>
17 
18 #include <PPPDefs.h>
19 #include <settings_tools.h>
20 
21 
22 // message constants
23 static const uint32 kMsgUpdateControls = 'UCTL';
24 
25 // labels
26 static const char *kLabelConnectionOptions = "Options";
27 static const char *kLabelDialOnDemand = "Connect Automatically When Needed";
28 static const char *kLabelAskBeforeDialing = "Ask Before Dialing";
29 static const char *kLabelAutoRedial = "Redial Automatically";
30 
31 
ConnectionOptionsAddon(BMessage * addons)32 ConnectionOptionsAddon::ConnectionOptionsAddon(BMessage *addons)
33 	: DialUpAddon(addons),
34 	fSettings(NULL),
35 	fProfile(NULL),
36 	fConnectionOptionsView(NULL)
37 {
38 }
39 
40 
~ConnectionOptionsAddon()41 ConnectionOptionsAddon::~ConnectionOptionsAddon()
42 {
43 }
44 
45 
46 bool
LoadSettings(BMessage * settings,BMessage * profile,bool isNew)47 ConnectionOptionsAddon::LoadSettings(BMessage *settings, BMessage *profile, bool isNew)
48 {
49 	fIsNew = isNew;
50 	fDoesDialOnDemand = fAskBeforeDialing = fDoesAutoRedial = false;
51 	fSettings = settings;
52 	fProfile = profile;
53 
54 	if(fConnectionOptionsView)
55 		fConnectionOptionsView->Reload();
56 			// reset all views (empty settings)
57 
58 	if(!settings || !profile || isNew)
59 		return true;
60 
61 	BMessage parameter;
62 	int32 index = 0;
63 	const char *value;
64 	/* FIXME!
65 	if(FindMessageParameter(PPP_DIAL_ON_DEMAND_KEY, *fSettings, &parameter, &index)
66 			&& parameter.FindString(MDSU_VALUES, &value) == B_OK) {
67 		if(get_boolean_value(value, false))
68 			fDoesDialOnDemand = true;
69 
70 		parameter.AddBool(MDSU_VALID, true);
71 		fSettings->ReplaceMessage(MDSU_PARAMETERS, index, &parameter);
72 	}
73 
74 	index = 0;
75 	if(FindMessageParameter(PPP_ASK_BEFORE_DIALING_KEY, *fSettings, &parameter, &index)
76 			&& parameter.FindString(MDSU_VALUES, &value) == B_OK) {
77 		if(get_boolean_value(value, false))
78 			fAskBeforeDialing = true;
79 
80 		parameter.AddBool(MDSU_VALID, true);
81 		fSettings->ReplaceMessage(MDSU_PARAMETERS, index, &parameter);
82 	}
83 
84 	index = 0;
85 	if(FindMessageParameter(PPP_AUTO_REDIAL_KEY, *fSettings, &parameter, &index)
86 			&& parameter.FindString(MDSU_VALUES, &value) == B_OK) {
87 		if(get_boolean_value(value, false))
88 			fDoesAutoRedial = true;
89 
90 		parameter.AddBool(MDSU_VALID, true);
91 		fSettings->ReplaceMessage(MDSU_PARAMETERS, index, &parameter);
92 	}
93 	*/
94 
95 	if(fConnectionOptionsView)
96 		fConnectionOptionsView->Reload();
97 			// reload new settings
98 
99 	return true;
100 }
101 
102 
103 void
IsModified(bool * settings,bool * profile) const104 ConnectionOptionsAddon::IsModified(bool *settings, bool *profile) const
105 {
106 	*settings = *profile = false;
107 
108 	if(!fSettings || !fConnectionOptionsView)
109 		return;
110 
111 	*settings = DoesDialOnDemand() != fConnectionOptionsView->DoesDialOnDemand()
112 		|| AskBeforeDialing() != fConnectionOptionsView->AskBeforeDialing()
113 		|| DoesAutoRedial() != fConnectionOptionsView->DoesAutoRedial();
114 }
115 
116 
117 bool
SaveSettings(BMessage * settings,BMessage * profile,bool saveTemporary)118 ConnectionOptionsAddon::SaveSettings(BMessage *settings, BMessage *profile, bool saveTemporary)
119 {
120 	if(!fSettings || !settings)
121 		return false;
122 
123 	BMessage parameter;
124 	if(fConnectionOptionsView->DoesDialOnDemand()) {
125 		parameter.MakeEmpty();
126 		// parameter.AddString(MDSU_NAME, PPP_DIAL_ON_DEMAND_KEY); // FIXME
127 		parameter.AddString(MDSU_VALUES, "enabled");
128 		settings->AddMessage(MDSU_PARAMETERS, &parameter);
129 
130 		if(fConnectionOptionsView->AskBeforeDialing()) {
131 			parameter.MakeEmpty();
132 			// parameter.AddString(MDSU_NAME, PPP_ASK_BEFORE_DIALING_KEY); // FIXME
133 			parameter.AddString(MDSU_VALUES, "enabled");
134 			settings->AddMessage(MDSU_PARAMETERS, &parameter);
135 		}
136 	}
137 
138 	if(fConnectionOptionsView->DoesAutoRedial()) {
139 		parameter.MakeEmpty();
140 		// parameter.AddString(MDSU_NAME, PPP_AUTO_REDIAL_KEY); // FIXME
141 		parameter.AddString(MDSU_VALUES, "enabled");
142 		settings->AddMessage(MDSU_PARAMETERS, &parameter);
143 	}
144 
145 	return true;
146 }
147 
148 
149 bool
GetPreferredSize(float * width,float * height) const150 ConnectionOptionsAddon::GetPreferredSize(float *width, float *height) const
151 {
152 	BRect rect;
153 	if(Addons()->FindRect(DUN_TAB_VIEW_RECT, &rect) != B_OK)
154 		rect.Set(0, 0, 200, 300);
155 			// set default values
156 
157 	if(width)
158 		*width = rect.Width();
159 	if(height)
160 		*height = rect.Height();
161 
162 	return true;
163 }
164 
165 
166 BView*
CreateView()167 ConnectionOptionsAddon::CreateView()
168 {
169 	if(!fConnectionOptionsView) {
170 		fConnectionOptionsView = new ConnectionOptionsView(this);
171 		fConnectionOptionsView->Reload();
172 	}
173 
174 	return fConnectionOptionsView;
175 }
176 
177 
ConnectionOptionsView(ConnectionOptionsAddon * addon)178 ConnectionOptionsView::ConnectionOptionsView(ConnectionOptionsAddon *addon)
179 	: BView(kLabelConnectionOptions, 0),
180 	fAddon(addon)
181 {
182 	fDialOnDemand = new BCheckBox("DialOnDemand", kLabelDialOnDemand,
183 		new BMessage(kMsgUpdateControls));
184 	fAskBeforeDialing = new BCheckBox("AskBeforeDialing", kLabelAskBeforeDialing,
185 		NULL);
186 	fAutoRedial = new BCheckBox("AutoRedial", kLabelAutoRedial, NULL);
187 
188 	BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_HALF_ITEM_SPACING)
189 		.SetInsets(B_USE_HALF_ITEM_INSETS)
190 		.Add(fDialOnDemand)
191 		.Add(fAskBeforeDialing)
192 		.Add(fAutoRedial)
193 		.AddGlue()
194 	.End();
195 }
196 
197 
198 void
Reload()199 ConnectionOptionsView::Reload()
200 {
201 	fDialOnDemand->SetValue(Addon()->DoesDialOnDemand() || Addon()->IsNew());
202 		// this is enabled by default
203 	fAskBeforeDialing->SetValue(Addon()->AskBeforeDialing());
204 	fAutoRedial->SetValue(Addon()->DoesAutoRedial());
205 
206 	if(!Addon()->Settings())
207 		return;
208 
209 	UpdateControls();
210 }
211 
212 
213 void
AttachedToWindow()214 ConnectionOptionsView::AttachedToWindow()
215 {
216 	SetViewColor(Parent()->ViewColor());
217 	fDialOnDemand->SetTarget(this);
218 }
219 
220 
221 void
MessageReceived(BMessage * message)222 ConnectionOptionsView::MessageReceived(BMessage *message)
223 {
224 	switch(message->what) {
225 		case kMsgUpdateControls:
226 			UpdateControls();
227 		break;
228 
229 		default:
230 			BView::MessageReceived(message);
231 	}
232 }
233 
234 
235 void
UpdateControls()236 ConnectionOptionsView::UpdateControls()
237 {
238 	fAskBeforeDialing->SetEnabled(fDialOnDemand->Value());
239 	fAskBeforeDialing->SetValue(fDialOnDemand->Value());
240 }
241