xref: /haiku/src/kits/tracker/AutoMounterSettings.cpp (revision b30304acc8c37e678a1bf66976d15bdab103f931)
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 "AutoMounter.h"
37 #include "AutoMounterSettings.h"
38 
39 #include <Box.h>
40 #include <Button.h>
41 #include <Debug.h>
42 #include <Message.h>
43 #include <RadioButton.h>
44 #include <Window.h>
45 
46 
47 const uint32 kAutomountSettingsChanged = 'achg';
48 const uint32 kBootMountSettingsChanged = 'bchg';
49 const uint32 kAutoAll = 'aall';
50 const uint32 kAutoBFS = 'abfs';
51 const uint32 kAutoHFS = 'ahfs';
52 const uint32 kInitAll = 'iall';
53 const uint32 kInitBFS = 'ibfs';
54 const uint32 kInitHFS = 'ihfs';
55 
56 class AutomountSettingsPanel : public BBox {
57 	public:
58 		AutomountSettingsPanel(BRect, BMessage *, AutoMounter *);
59 		virtual ~AutomountSettingsPanel();
60 
61 	protected:
62 		virtual void MessageReceived(BMessage *);
63 		virtual void AttachedToWindow();
64 		virtual void GetPreferredSize(float* _width, float* _height);
65 
66 	private:
67 		void SendSettings(bool rescan);
68 
69 		BRadioButton *fInitialDontMountCheck;
70 		BRadioButton *fInitialMountAllBFSCheck;
71 		BRadioButton *fInitialMountAllCheck;
72 		BRadioButton *fInitialMountRestoreCheck;
73 
74 		BRadioButton *fScanningDisabledCheck;
75 		BRadioButton *fAutoMountAllBFSCheck;
76 		BRadioButton *fAutoMountAllCheck;
77 
78 		BButton *fDone;
79 		BButton *fMountAllNow;
80 
81 		AutoMounter *fTarget;
82 
83 		typedef BBox _inherited;
84 };
85 
86 AutomountSettingsDialog *AutomountSettingsDialog::sOneCopyOnly = NULL;
87 
88 
89 AutomountSettingsPanel::AutomountSettingsPanel(BRect frame,
90 		BMessage *settings, AutoMounter *target)
91 	: BBox(frame, "", B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS
92 			| B_NAVIGABLE_JUMP, B_NO_BORDER),
93 		fTarget(target)
94 {
95 	// "Automatic Disk Mounting" group
96 
97 	BRect boxRect(Bounds());
98 	boxRect.InsetBy(8.0f, 8.0f);
99 	boxRect.bottom = boxRect.top + 85;
100 	BBox *box = new BBox(boxRect, "autoMountBox", B_FOLLOW_LEFT_RIGHT,
101 		B_WILL_DRAW | B_FRAME_EVENTS | B_PULSE_NEEDED | B_NAVIGABLE_JUMP);
102 	box->SetLabel("Automatic Disk Mounting");
103 	AddChild(box);
104 
105 	font_height fontHeight;
106 	box->GetFontHeight(&fontHeight);
107 
108 	BRect checkBoxRect(box->Bounds());
109 	checkBoxRect.InsetBy(8.0f, 8.0f);
110 	checkBoxRect.top += fontHeight.ascent + fontHeight.descent;
111 
112 	fScanningDisabledCheck = new BRadioButton(checkBoxRect, "scanningOff",
113 		"Don't Automount", new BMessage(kAutomountSettingsChanged));
114 	fScanningDisabledCheck->ResizeToPreferred();
115 	box->AddChild(fScanningDisabledCheck);
116 
117 	checkBoxRect.OffsetBy(0, fScanningDisabledCheck->Bounds().Height());
118 	fAutoMountAllBFSCheck = new BRadioButton(checkBoxRect, "autoBFS",
119 		"All BeOS Disks", new BMessage(kAutomountSettingsChanged));
120 	fAutoMountAllBFSCheck->ResizeToPreferred();
121 	box->AddChild(fAutoMountAllBFSCheck);
122 
123 	checkBoxRect.OffsetBy(0, fScanningDisabledCheck->Bounds().Height());
124 	fAutoMountAllCheck = new BRadioButton(checkBoxRect, "autoAll",
125 		"All Disks", new BMessage(kAutomountSettingsChanged));
126 	fAutoMountAllCheck->ResizeToPreferred();
127 	box->AddChild(fAutoMountAllCheck);
128 
129 	box->ResizeTo(box->Bounds().Width(), fAutoMountAllCheck->Frame().bottom + 8.0f);
130 
131 	// "Disk Mounting During Boot" group
132 
133 	boxRect.top = box->Frame().bottom + 8.0f;
134 	boxRect.bottom = boxRect.top + 105;
135 	box = new BBox(boxRect, "", B_FOLLOW_LEFT_RIGHT, B_WILL_DRAW | B_FRAME_EVENTS
136 			| B_PULSE_NEEDED | B_NAVIGABLE_JUMP);
137 	box->SetLabel("Disk Mounting During Boot");
138 	AddChild(box);
139 
140 	checkBoxRect = box->Bounds();
141 	checkBoxRect.InsetBy(8.0f, 8.0f);
142 	checkBoxRect.top += fontHeight.ascent + fontHeight.descent;
143 
144 	checkBoxRect.bottom = checkBoxRect.top + 20;
145 	fInitialDontMountCheck = new BRadioButton(checkBoxRect, "initialNone",
146 		"Only The Boot Disk", new BMessage(kBootMountSettingsChanged));
147 	fInitialDontMountCheck->ResizeToPreferred();
148 	box->AddChild(fInitialDontMountCheck);
149 
150 	checkBoxRect.OffsetBy(0, fInitialDontMountCheck->Bounds().Height());
151 	fInitialMountRestoreCheck = new BRadioButton(checkBoxRect, "initialRestore",
152 		"Previously Mounted Disks", new BMessage(kBootMountSettingsChanged));
153 	fInitialMountRestoreCheck->ResizeToPreferred();
154 	box->AddChild(fInitialMountRestoreCheck);
155 
156 	checkBoxRect.OffsetBy(0, fInitialDontMountCheck->Bounds().Height());
157 	fInitialMountAllBFSCheck = new BRadioButton(checkBoxRect, "initialBFS",
158 		"All BeOS Disks", new BMessage(kBootMountSettingsChanged));
159 	fInitialMountAllBFSCheck->ResizeToPreferred();
160 	box->AddChild(fInitialMountAllBFSCheck);
161 
162 	checkBoxRect.OffsetBy(0, fInitialDontMountCheck->Bounds().Height());
163 	fInitialMountAllCheck = new BRadioButton(checkBoxRect, "initialAll",
164 		"All Disks", new BMessage(kBootMountSettingsChanged));
165 	fInitialMountAllCheck->ResizeToPreferred();
166 	box->AddChild(fInitialMountAllCheck);
167 
168 	box->ResizeTo(box->Bounds().Width(), fInitialMountAllCheck->Frame().bottom + 8.0f);
169 
170 	// Buttons
171 
172 	fDone = new BButton(Bounds(), "done", "Done", new BMessage(B_QUIT_REQUESTED),
173 		B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
174 	fDone->ResizeToPreferred();
175 	fDone->MoveTo(Bounds().Width() - fDone->Bounds().Width() - 8.0f,
176 		Bounds().bottom - fDone->Bounds().Height() - 8.0f);
177 	AddChild(fDone);
178 
179 	fMountAllNow = new BButton(fDone->Frame(), "mountAll", "Mount all disks now",
180 		new BMessage(kMountAllNow), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
181 	fMountAllNow->ResizeToPreferred();
182 	fMountAllNow->MoveBy(-fMountAllNow->Bounds().Width() - 10.0f, 0.0f);
183 	AddChild(fMountAllNow);
184 
185 	fDone->MakeDefault(true);
186 
187 	bool result;
188 	if (settings->FindBool("autoMountAll", &result) == B_OK && result)
189 		fAutoMountAllCheck->SetValue(1);
190 	else if (settings->FindBool("autoMountAllBFS", &result) == B_OK && result)
191 		fAutoMountAllBFSCheck->SetValue(1);
192 	else
193 		fScanningDisabledCheck->SetValue(1);
194 
195 	if (settings->FindBool("suspended", &result) == B_OK && result)
196 		fScanningDisabledCheck->SetValue(1);
197 
198 	if (settings->FindBool("initialMountAll", &result) == B_OK && result)
199 		fInitialMountAllCheck->SetValue(1);
200 	else if (settings->FindBool("initialMountRestore", &result) == B_OK && result)
201 		fInitialMountRestoreCheck->SetValue(1);
202 	else if (settings->FindBool("initialMountAllBFS", &result) == B_OK && result)
203 		fInitialMountAllBFSCheck->SetValue(1);
204 	else
205 		fInitialDontMountCheck->SetValue(1);
206 }
207 
208 
209 AutomountSettingsPanel::~AutomountSettingsPanel()
210 {
211 }
212 
213 
214 void
215 AutomountSettingsPanel::SendSettings(bool rescan)
216 {
217 	BMessage message(kSetAutomounterParams);
218 
219 	message.AddBool("autoMountAll", (bool)fAutoMountAllCheck->Value());
220 	message.AddBool("autoMountAllBFS", (bool)fAutoMountAllBFSCheck->Value());
221 	if (fAutoMountAllBFSCheck->Value())
222 		message.AddBool("autoMountAllHFS", false);
223 
224 	message.AddBool("suspended", (bool)fScanningDisabledCheck->Value());
225 	message.AddBool("rescanNow", rescan);
226 
227 	message.AddBool("initialMountAll", (bool)fInitialMountAllCheck->Value());
228 	message.AddBool("initialMountAllBFS", (bool)fInitialMountAllBFSCheck->Value());
229 	message.AddBool("initialMountRestore", (bool)fInitialMountRestoreCheck->Value());
230 	if (fInitialDontMountCheck->Value())
231 		message.AddBool("initialMountAllHFS", false);
232 
233 	fTarget->PostMessage(&message, NULL);
234 }
235 
236 
237 void
238 AutomountSettingsPanel::GetPreferredSize(float* _width, float* _height)
239 {
240 	BBox* box = dynamic_cast<BBox*>(fInitialMountRestoreCheck->Parent());
241 
242 	if (_width) {
243 		float a = fInitialMountRestoreCheck->Bounds().Width() + 32.0f;
244 		float b = box != NULL ? box->StringWidth(box->Label()) + 24.0f : 0.0f;
245 		float c = fMountAllNow->Bounds().Width() + fDone->Bounds().Width() + 26.0f;
246 
247 		a = max_c(a, b);
248 		*_width = max_c(a, c);
249 	}
250 
251 	if (_height) {
252 		*_height = (box != NULL ? box->Frame().bottom : 0.0f) + 16.0f
253 			+ fMountAllNow->Bounds().Height();
254 	}
255 }
256 
257 
258 void
259 AutomountSettingsPanel::AttachedToWindow()
260 {
261 	fInitialMountAllCheck->SetTarget(this);
262 	fInitialMountAllBFSCheck->SetTarget(this);
263 	fInitialMountRestoreCheck->SetTarget(this);
264 	fInitialDontMountCheck->SetTarget(this);
265 
266 	fAutoMountAllCheck->SetTarget(this);
267 	fAutoMountAllBFSCheck->SetTarget(this);
268 	fScanningDisabledCheck->SetTarget(this);
269 
270 	fDone->SetTarget(this);
271 	fMountAllNow->SetTarget(fTarget);
272 }
273 
274 
275 void
276 AutomountSettingsPanel::MessageReceived(BMessage *message)
277 {
278 	switch (message->what) {
279 		case B_QUIT_REQUESTED:
280 			Window()->Quit();
281 			break;
282 
283 		case kAutomountSettingsChanged:
284 			SendSettings(true);
285 			break;
286 
287 		case kBootMountSettingsChanged:
288 			SendSettings(false);
289 			break;
290 
291 		default:
292 			_inherited::MessageReceived(message);
293 			break;
294 	}
295 }
296 
297 
298 //	#pragma mark -
299 
300 
301 AutomountSettingsDialog::AutomountSettingsDialog(BMessage *settings,
302 		AutoMounter *target)
303 	: BWindow(BRect(100, 100, 320, 370), "Disk Mount Settings",
304 		B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
305 {
306 	BView* view = new AutomountSettingsPanel(Bounds(), settings, target);
307 	AddChild(view);
308 
309 	float width, height;
310 	view->GetPreferredSize(&width, &height);
311 	ResizeTo(width, height);
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(AutoMounter *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;
335 	target->GetSettings(&message);
336 	(new AutomountSettingsDialog(&message, target))->Show();
337 }
338 
339