xref: /haiku/src/kits/tracker/AutoMounterSettings.cpp (revision 6c4a44e36ba846c54467103f884d65dfa13e7fcb)
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 
36 #include "AutoMounterSettings.h"
37 
38 #include <Alert.h>
39 #include <Box.h>
40 #include <Button.h>
41 #include <Catalog.h>
42 #include <CheckBox.h>
43 #include <ControlLook.h>
44 #include <Debug.h>
45 #include <LayoutBuilder.h>
46 #include <Locale.h>
47 #include <Message.h>
48 #include <RadioButton.h>
49 #include <SeparatorView.h>
50 #include <SpaceLayoutItem.h>
51 #include <Window.h>
52 
53 #include <MountServer.h>
54 
55 
56 const uint32 kAutomountSettingsChanged = 'achg';
57 const uint32 kBootMountSettingsChanged = 'bchg';
58 const uint32 kEjectWhenUnmountingChanged = 'ejct';
59 
60 #undef B_TRANSLATION_CONTEXT
61 #define B_TRANSLATION_CONTEXT "AutoMounterSettings"
62 
63 class AutomountSettingsPanel : public BBox {
64 public:
65 								AutomountSettingsPanel(BMessage* settings,
66 									const BMessenger& target);
67 	virtual						~AutomountSettingsPanel();
68 
69 protected:
70 	virtual	void				MessageReceived(BMessage* message);
71 	virtual	void				AttachedToWindow();
72 
73 private:
74 			void				_SendSettings(bool rescan);
75 
76 			BRadioButton*		fInitialDontMountCheck;
77 			BRadioButton*		fInitialMountAllBFSCheck;
78 			BRadioButton*		fInitialMountAllCheck;
79 			BRadioButton*		fInitialMountRestoreCheck;
80 
81 			BRadioButton*		fScanningDisabledCheck;
82 			BRadioButton*		fAutoMountAllBFSCheck;
83 			BRadioButton*		fAutoMountAllCheck;
84 
85 			BCheckBox*			fEjectWhenUnmountingCheckBox;
86 
87 			BButton*			fDone;
88 			BButton*			fMountAllNow;
89 
90 			BMessenger			fTarget;
91 
92 			typedef BBox _inherited;
93 };
94 
95 
96 AutomountSettingsDialog* AutomountSettingsDialog::sOneCopyOnly = NULL;
97 
98 
99 AutomountSettingsPanel::AutomountSettingsPanel(BMessage* settings,
100 		const BMessenger& target)
101 	:
102 	BBox("", B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP, B_NO_BORDER),
103 	fTarget(target)
104 {
105 	const float spacing = be_control_look->DefaultItemSpacing();
106 
107 	// "Automatic Disk Mounting" group
108 
109 	BBox* autoMountBox = new BBox("autoMountBox", B_WILL_DRAW | B_FRAME_EVENTS
110 		| B_PULSE_NEEDED | B_NAVIGABLE_JUMP);
111 	autoMountBox->SetLabel(B_TRANSLATE("Automatic disk mounting"));
112 	BGroupLayout* autoMountLayout = new BGroupLayout(B_VERTICAL, 0);
113 	autoMountBox->SetLayout(autoMountLayout);
114 	autoMountLayout->SetInsets(spacing,
115 		autoMountBox->InnerFrame().top + spacing / 2, spacing, spacing);
116 
117 	fScanningDisabledCheck = new BRadioButton("scanningOff",
118 		B_TRANSLATE("Don't automount"),
119 		new BMessage(kAutomountSettingsChanged));
120 
121 	fAutoMountAllBFSCheck = new BRadioButton("autoBFS",
122 		B_TRANSLATE("All BeOS disks"),
123 			new BMessage(kAutomountSettingsChanged));
124 
125 	fAutoMountAllCheck = new BRadioButton("autoAll",
126 		B_TRANSLATE("All disks"), new BMessage(kAutomountSettingsChanged));
127 
128 	// "Disk Mounting During Boot" group
129 
130 	BBox* bootMountBox = new BBox("", B_WILL_DRAW | B_FRAME_EVENTS
131 		| B_PULSE_NEEDED | B_NAVIGABLE_JUMP);
132 	bootMountBox->SetLabel(B_TRANSLATE("Disk mounting during boot"));
133 	BGroupLayout* bootMountLayout = new BGroupLayout(B_VERTICAL, 0);
134 	bootMountBox->SetLayout(bootMountLayout);
135 	bootMountLayout->SetInsets(spacing,
136 		bootMountBox->InnerFrame().top + spacing / 2, spacing, spacing);
137 
138 	fInitialDontMountCheck = new BRadioButton("initialNone",
139 		B_TRANSLATE("Only the boot disk"),
140 		new BMessage(kBootMountSettingsChanged));
141 
142 	fInitialMountRestoreCheck = new BRadioButton("initialRestore",
143 		B_TRANSLATE("Previously mounted disks"),
144 		new BMessage(kBootMountSettingsChanged));
145 
146 	fInitialMountAllBFSCheck = new BRadioButton("initialBFS",
147 		B_TRANSLATE("All BeOS disks"),
148 		new BMessage(kBootMountSettingsChanged));
149 
150 	fInitialMountAllCheck = new BRadioButton("initialAll",
151 		B_TRANSLATE("All disks"), new BMessage(kBootMountSettingsChanged));
152 
153 	fEjectWhenUnmountingCheckBox = new BCheckBox("ejectWhenUnmounting",
154 		B_TRANSLATE("Eject when unmounting"),
155 		new BMessage(kEjectWhenUnmountingChanged));
156 
157 	// Buttons
158 
159 	fDone = new BButton(B_TRANSLATE("Done"), new BMessage(B_QUIT_REQUESTED));
160 
161 	fMountAllNow = new BButton("mountAll", B_TRANSLATE("Mount all disks now"),
162 		new BMessage(kMountAllNow));
163 
164 	fDone->MakeDefault(true);
165 
166 	// Layout the controls
167 	BGroupView* contentView = new BGroupView(B_VERTICAL, 0);
168 	AddChild(contentView);
169 	BLayoutBuilder::Group<>(contentView)
170 		.AddGroup(B_VERTICAL, spacing)
171 			.SetInsets(spacing, spacing, spacing, spacing)
172 			.AddGroup(autoMountLayout)
173 				.Add(fScanningDisabledCheck)
174 				.Add(fAutoMountAllBFSCheck)
175 				.Add(fAutoMountAllCheck)
176 				.End()
177 			.AddGroup(bootMountLayout)
178 				.Add(fInitialDontMountCheck)
179 				.Add(fInitialMountRestoreCheck)
180 				.Add(fInitialMountAllBFSCheck)
181 				.Add(fInitialMountAllCheck)
182 				.End()
183 			.AddGroup(B_HORIZONTAL)
184 				.AddStrut(spacing - 1)
185 				.Add(fEjectWhenUnmountingCheckBox)
186 				.End()
187 			.End()
188 		.Add(new BSeparatorView(B_HORIZONTAL/*, B_FANCY_BORDER*/))
189 		.AddGroup(B_HORIZONTAL, spacing)
190 			.SetInsets(0, spacing, spacing, spacing)
191 			.AddGlue()
192 			.Add(fMountAllNow)
193 			.Add(fDone);
194 
195 	// Apply the settings
196 
197 	bool result;
198 	if (settings->FindBool("autoMountAll", &result) == B_OK && result)
199 		fAutoMountAllCheck->SetValue(B_CONTROL_ON);
200 	else if (settings->FindBool("autoMountAllBFS", &result) == B_OK && result)
201 		fAutoMountAllBFSCheck->SetValue(B_CONTROL_ON);
202 	else
203 		fScanningDisabledCheck->SetValue(B_CONTROL_ON);
204 
205 	if (settings->FindBool("suspended", &result) == B_OK && result)
206 		fScanningDisabledCheck->SetValue(B_CONTROL_ON);
207 
208 	if (settings->FindBool("initialMountAll", &result) == B_OK && result)
209 		fInitialMountAllCheck->SetValue(B_CONTROL_ON);
210 	else if (settings->FindBool("initialMountRestore", &result) == B_OK
211 		&& result) {
212 		fInitialMountRestoreCheck->SetValue(B_CONTROL_ON);
213 	} else if (settings->FindBool("initialMountAllBFS", &result) == B_OK
214 		&& result) {
215 		fInitialMountAllBFSCheck->SetValue(B_CONTROL_ON);
216 	} else
217 		fInitialDontMountCheck->SetValue(B_CONTROL_ON);
218 
219 	if (settings->FindBool("ejectWhenUnmounting", &result) == B_OK && result)
220 		fEjectWhenUnmountingCheckBox->SetValue(B_CONTROL_ON);
221 }
222 
223 
224 AutomountSettingsPanel::~AutomountSettingsPanel()
225 {
226 }
227 
228 
229 void
230 AutomountSettingsPanel::AttachedToWindow()
231 {
232 	fInitialMountAllCheck->SetTarget(this);
233 	fInitialMountAllBFSCheck->SetTarget(this);
234 	fInitialMountRestoreCheck->SetTarget(this);
235 	fInitialDontMountCheck->SetTarget(this);
236 
237 	fAutoMountAllCheck->SetTarget(this);
238 	fAutoMountAllBFSCheck->SetTarget(this);
239 	fScanningDisabledCheck->SetTarget(this);
240 	fEjectWhenUnmountingCheckBox->SetTarget(this);
241 
242 	fDone->SetTarget(this);
243 	fMountAllNow->SetTarget(fTarget);
244 }
245 
246 
247 void
248 AutomountSettingsPanel::MessageReceived(BMessage* message)
249 {
250 	switch (message->what) {
251 		case B_QUIT_REQUESTED:
252 			Window()->Quit();
253 			break;
254 
255 		case kAutomountSettingsChanged:
256 			_SendSettings(true);
257 			break;
258 
259 		case kBootMountSettingsChanged:
260 		case kEjectWhenUnmountingChanged:
261 			_SendSettings(false);
262 			break;
263 
264 		default:
265 			_inherited::MessageReceived(message);
266 			break;
267 	}
268 }
269 
270 
271 void
272 AutomountSettingsPanel::_SendSettings(bool rescan)
273 {
274 	BMessage message(kSetAutomounterParams);
275 
276 	message.AddBool("autoMountAll", (bool)fAutoMountAllCheck->Value());
277 	message.AddBool("autoMountAllBFS", (bool)fAutoMountAllBFSCheck->Value());
278 	if (fAutoMountAllBFSCheck->Value())
279 		message.AddBool("autoMountAllHFS", false);
280 
281 	message.AddBool("suspended", (bool)fScanningDisabledCheck->Value());
282 	message.AddBool("rescanNow", rescan);
283 
284 	message.AddBool("initialMountAll", (bool)fInitialMountAllCheck->Value());
285 	message.AddBool("initialMountAllBFS",
286 		(bool)fInitialMountAllBFSCheck->Value());
287 	message.AddBool("initialMountRestore",
288 		(bool)fInitialMountRestoreCheck->Value());
289 	if (fInitialDontMountCheck->Value())
290 		message.AddBool("initialMountAllHFS", false);
291 
292 	message.AddBool("ejectWhenUnmounting",
293 		(bool)fEjectWhenUnmountingCheckBox->Value());
294 
295 	fTarget.SendMessage(&message);
296 }
297 
298 
299 //	#pragma mark -
300 
301 
302 AutomountSettingsDialog::AutomountSettingsDialog(BMessage* settings,
303 		const BMessenger& target)
304 	:
305 	BWindow(BRect(100, 100, 320, 370), B_TRANSLATE("Disk mount settings"),
306 		B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE
307 		| B_AUTO_UPDATE_SIZE_LIMITS)
308 {
309 	SetLayout(new BGroupLayout(B_HORIZONTAL));
310 	BView* view = new AutomountSettingsPanel(settings, target);
311 	AddChild(view);
312 
313 	ASSERT(!sOneCopyOnly);
314 	sOneCopyOnly = this;
315 }
316 
317 
318 AutomountSettingsDialog::~AutomountSettingsDialog()
319 {
320 	ASSERT(sOneCopyOnly);
321 	sOneCopyOnly = NULL;
322 }
323 
324 
325 void
326 AutomountSettingsDialog::RunAutomountSettings(const BMessenger& target)
327 {
328 	// either activate an existing mount settings dialog or create a new one
329 	if (sOneCopyOnly) {
330 		sOneCopyOnly->Activate();
331 		return;
332 	}
333 
334 	BMessage message(kGetAutomounterParams);
335 	BMessage reply;
336 	status_t ret = target.SendMessage(&message, &reply, 2500000);
337 	if (ret != B_OK) {
338 		BAlert* alert = new BAlert(B_TRANSLATE("Mount server error"),
339 			B_TRANSLATE("The mount server could not be contacted."),
340 			B_TRANSLATE("OK"),
341 			NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
342 		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
343 		alert->Go();
344 		return;
345 	}
346 
347 	(new AutomountSettingsDialog(&reply, target))->Show();
348 }
349