xref: /haiku/src/kits/tracker/AutoMounterSettings.cpp (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 #include "AutoMounterSettings.h"
36 
37 #include <Alert.h>
38 #include <Box.h>
39 #include <Button.h>
40 #include <CheckBox.h>
41 #include <Debug.h>
42 #include <GridLayoutBuilder.h>
43 #include <GroupLayoutBuilder.h>
44 #include <SpaceLayoutItem.h>
45 #include <SeparatorView.h>
46 #include <Message.h>
47 #include <RadioButton.h>
48 #include <Window.h>
49 
50 #include <MountServer.h>
51 
52 
53 const uint32 kAutomountSettingsChanged = 'achg';
54 const uint32 kBootMountSettingsChanged = 'bchg';
55 const uint32 kEjectWhenUnmountingChanged = 'ejct';
56 
57 
58 class AutomountSettingsPanel : public BBox {
59 public:
60 								AutomountSettingsPanel(BMessage* settings,
61 									const BMessenger& target);
62 	virtual						~AutomountSettingsPanel();
63 
64 protected:
65 	virtual	void				MessageReceived(BMessage* message);
66 	virtual	void				AttachedToWindow();
67 
68 private:
69 			void				_SendSettings(bool rescan);
70 
71 			BRadioButton*		fInitialDontMountCheck;
72 			BRadioButton*		fInitialMountAllBFSCheck;
73 			BRadioButton*		fInitialMountAllCheck;
74 			BRadioButton*		fInitialMountRestoreCheck;
75 
76 			BRadioButton*		fScanningDisabledCheck;
77 			BRadioButton*		fAutoMountAllBFSCheck;
78 			BRadioButton*		fAutoMountAllCheck;
79 
80 			BCheckBox*			fEjectWhenUnmountingCheckBox;
81 
82 			BButton*			fDone;
83 			BButton*			fMountAllNow;
84 
85 			BMessenger			fTarget;
86 
87 			typedef BBox _inherited;
88 };
89 
90 
91 AutomountSettingsDialog* AutomountSettingsDialog::sOneCopyOnly = NULL;
92 
93 
94 AutomountSettingsPanel::AutomountSettingsPanel(BMessage* settings,
95 		const BMessenger& target)
96 	:
97 	BBox("", B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP, B_NO_BORDER),
98 	fTarget(target)
99 {
100 	const float spacing = 8;
101 
102 	// "Automatic Disk Mounting" group
103 
104 	BBox* autoMountBox = new BBox("autoMountBox", B_WILL_DRAW | B_FRAME_EVENTS
105 		| B_PULSE_NEEDED | B_NAVIGABLE_JUMP);
106 	autoMountBox->SetLabel("Automatic disk mounting");
107 	BGroupLayout* autoMountLayout = new BGroupLayout(B_VERTICAL, spacing);
108 	autoMountBox->SetLayout(autoMountLayout);
109 	autoMountLayout->SetInsets(spacing,
110 		autoMountBox->InnerFrame().top + spacing, spacing, spacing);
111 
112 	fScanningDisabledCheck = new BRadioButton("scanningOff", "Don't automount",
113 		new BMessage(kAutomountSettingsChanged));
114 
115 	fAutoMountAllBFSCheck = new BRadioButton("autoBFS", "All BeOS disks",
116 		new BMessage(kAutomountSettingsChanged));
117 
118 	fAutoMountAllCheck = new BRadioButton("autoAll", "All disks",
119 		new BMessage(kAutomountSettingsChanged));
120 
121 	// "Disk Mounting During Boot" group
122 
123 	BBox* bootMountBox = new BBox("", B_WILL_DRAW | B_FRAME_EVENTS
124 		| B_PULSE_NEEDED | B_NAVIGABLE_JUMP);
125 	bootMountBox->SetLabel("Disk mounting during boot");
126 	BGroupLayout* bootMountLayout = new BGroupLayout(B_VERTICAL, spacing);
127 	bootMountBox->SetLayout(bootMountLayout);
128 	bootMountLayout->SetInsets(spacing,
129 		bootMountBox->InnerFrame().top + spacing, spacing, spacing);
130 
131 	fInitialDontMountCheck = new BRadioButton("initialNone",
132 		"Only the boot disk", new BMessage(kBootMountSettingsChanged));
133 
134 	fInitialMountRestoreCheck = new BRadioButton("initialRestore",
135 		"Previously mounted disks", new BMessage(kBootMountSettingsChanged));
136 
137 	fInitialMountAllBFSCheck = new BRadioButton("initialBFS",
138 		"All BeOS disks", new BMessage(kBootMountSettingsChanged));
139 
140 	fInitialMountAllCheck = new BRadioButton("initialAll",
141 		"All disks", new BMessage(kBootMountSettingsChanged));
142 
143 	fEjectWhenUnmountingCheckBox = new BCheckBox("ejectWhenUnmounting",
144 		"Eject when unmounting", new BMessage(kEjectWhenUnmountingChanged));
145 
146 	// Buttons
147 
148 	fDone = new BButton("Done", new BMessage(B_QUIT_REQUESTED));
149 
150 	fMountAllNow = new BButton("mountAll", "Mount all disks now",
151 		new BMessage(kMountAllNow));
152 
153 	fDone->MakeDefault(true);
154 
155 	// Layout the controls
156 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
157 		.Add(BGroupLayoutBuilder(B_VERTICAL, spacing)
158 			.Add(BGroupLayoutBuilder(autoMountLayout)
159 				.Add(fScanningDisabledCheck)
160 				.Add(fAutoMountAllBFSCheck)
161 				.Add(fAutoMountAllCheck)
162 			)
163 			.Add(BGroupLayoutBuilder(bootMountLayout)
164 				.Add(fInitialDontMountCheck)
165 				.Add(fInitialMountRestoreCheck)
166 				.Add(fInitialMountAllBFSCheck)
167 				.Add(fInitialMountAllCheck)
168 			)
169 			.Add(BGroupLayoutBuilder(B_HORIZONTAL)
170 				.Add(BSpaceLayoutItem::CreateHorizontalStrut(spacing - 1))
171 				.Add(fEjectWhenUnmountingCheckBox)
172 			)
173 			.SetInsets(spacing, spacing, spacing, spacing)
174 		)
175 		.Add(new BSeparatorView(B_HORIZONTAL/*, B_FANCY_BORDER*/))
176 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, spacing)
177 			.AddGlue()
178 			.Add(fMountAllNow)
179 			.Add(fDone)
180 			.SetInsets(spacing, spacing, spacing, spacing)
181 		)
182 	);
183 
184 
185 	// Apply the settings
186 
187 	bool result;
188 	if (settings->FindBool("autoMountAll", &result) == B_OK && result)
189 		fAutoMountAllCheck->SetValue(B_CONTROL_ON);
190 	else if (settings->FindBool("autoMountAllBFS", &result) == B_OK && result)
191 		fAutoMountAllBFSCheck->SetValue(B_CONTROL_ON);
192 	else
193 		fScanningDisabledCheck->SetValue(B_CONTROL_ON);
194 
195 	if (settings->FindBool("suspended", &result) == B_OK && result)
196 		fScanningDisabledCheck->SetValue(B_CONTROL_ON);
197 
198 	if (settings->FindBool("initialMountAll", &result) == B_OK && result)
199 		fInitialMountAllCheck->SetValue(B_CONTROL_ON);
200 	else if (settings->FindBool("initialMountRestore", &result) == B_OK
201 		&& result) {
202 		fInitialMountRestoreCheck->SetValue(B_CONTROL_ON);
203 	} else if (settings->FindBool("initialMountAllBFS", &result) == B_OK
204 		&& result) {
205 		fInitialMountAllBFSCheck->SetValue(B_CONTROL_ON);
206 	} else
207 		fInitialDontMountCheck->SetValue(B_CONTROL_ON);
208 
209 	if (settings->FindBool("ejectWhenUnmounting", &result) == B_OK && result)
210 		fEjectWhenUnmountingCheckBox->SetValue(B_CONTROL_ON);
211 }
212 
213 
214 AutomountSettingsPanel::~AutomountSettingsPanel()
215 {
216 }
217 
218 
219 void
220 AutomountSettingsPanel::AttachedToWindow()
221 {
222 	fInitialMountAllCheck->SetTarget(this);
223 	fInitialMountAllBFSCheck->SetTarget(this);
224 	fInitialMountRestoreCheck->SetTarget(this);
225 	fInitialDontMountCheck->SetTarget(this);
226 
227 	fAutoMountAllCheck->SetTarget(this);
228 	fAutoMountAllBFSCheck->SetTarget(this);
229 	fScanningDisabledCheck->SetTarget(this);
230 	fEjectWhenUnmountingCheckBox->SetTarget(this);
231 
232 	fDone->SetTarget(this);
233 	fMountAllNow->SetTarget(fTarget);
234 }
235 
236 
237 void
238 AutomountSettingsPanel::MessageReceived(BMessage* message)
239 {
240 	switch (message->what) {
241 		case B_QUIT_REQUESTED:
242 			Window()->Quit();
243 			break;
244 
245 		case kAutomountSettingsChanged:
246 			_SendSettings(true);
247 			break;
248 
249 		case kBootMountSettingsChanged:
250 		case kEjectWhenUnmountingChanged:
251 			_SendSettings(false);
252 			break;
253 
254 		default:
255 			_inherited::MessageReceived(message);
256 			break;
257 	}
258 }
259 
260 
261 void
262 AutomountSettingsPanel::_SendSettings(bool rescan)
263 {
264 	BMessage message(kSetAutomounterParams);
265 
266 	message.AddBool("autoMountAll", (bool)fAutoMountAllCheck->Value());
267 	message.AddBool("autoMountAllBFS", (bool)fAutoMountAllBFSCheck->Value());
268 	if (fAutoMountAllBFSCheck->Value())
269 		message.AddBool("autoMountAllHFS", false);
270 
271 	message.AddBool("suspended", (bool)fScanningDisabledCheck->Value());
272 	message.AddBool("rescanNow", rescan);
273 
274 	message.AddBool("initialMountAll", (bool)fInitialMountAllCheck->Value());
275 	message.AddBool("initialMountAllBFS",
276 		(bool)fInitialMountAllBFSCheck->Value());
277 	message.AddBool("initialMountRestore",
278 		(bool)fInitialMountRestoreCheck->Value());
279 	if (fInitialDontMountCheck->Value())
280 		message.AddBool("initialMountAllHFS", false);
281 
282 	message.AddBool("ejectWhenUnmounting",
283 		(bool)fEjectWhenUnmountingCheckBox->Value());
284 
285 	fTarget.SendMessage(&message);
286 }
287 
288 
289 //	#pragma mark -
290 
291 
292 AutomountSettingsDialog::AutomountSettingsDialog(BMessage* settings,
293 		const BMessenger& target)
294 	:
295 	BWindow(BRect(100, 100, 320, 370), "Disk mount settings",
296 		B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE
297 			| B_AUTO_UPDATE_SIZE_LIMITS)
298 {
299 	SetLayout(new BGroupLayout(B_HORIZONTAL));
300 	BView* view = new AutomountSettingsPanel(settings, target);
301 	AddChild(view);
302 
303 	ASSERT(!sOneCopyOnly);
304 	sOneCopyOnly = this;
305 }
306 
307 
308 AutomountSettingsDialog::~AutomountSettingsDialog()
309 {
310 	ASSERT(sOneCopyOnly);
311 	sOneCopyOnly = NULL;
312 }
313 
314 
315 void
316 AutomountSettingsDialog::RunAutomountSettings(const BMessenger& target)
317 {
318 	// either activate an existing mount settings dialog or create a new one
319 	if (sOneCopyOnly) {
320 		sOneCopyOnly->Activate();
321 		return;
322 	}
323 
324 	BMessage message(kGetAutomounterParams);
325 	BMessage reply;
326 	status_t ret = target.SendMessage(&message, &reply, 2500000);
327 	if (ret != B_OK) {
328 		(new BAlert("Mount server error", "The mount server could not be "
329 			"contacted.", "OK", NULL, NULL, B_WIDTH_AS_USUAL,
330 			B_STOP_ALERT))->Go();
331 		return;
332 	}
333 
334 	(new AutomountSettingsDialog(&reply, target))->Show();
335 }
336 
337