1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All 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
56const uint32 kAutomountSettingsChanged = 'achg';
57const uint32 kBootMountSettingsChanged = 'bchg';
58const uint32 kEjectWhenUnmountingChanged = 'ejct';
59
60#undef B_TRANSLATION_CONTEXT
61#define B_TRANSLATION_CONTEXT "AutoMounterSettings"
62
63class AutomountSettingsPanel : public BBox {
64public:
65								AutomountSettingsPanel(BMessage* settings,
66									const BMessenger& target);
67	virtual						~AutomountSettingsPanel();
68
69protected:
70	virtual	void				MessageReceived(BMessage* message);
71	virtual	void				AttachedToWindow();
72
73private:
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
96AutomountSettingsDialog* AutomountSettingsDialog::sOneCopyOnly = NULL;
97
98
99AutomountSettingsPanel::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			.Add(fEjectWhenUnmountingCheckBox)
184			.End()
185		.Add(new BSeparatorView(B_HORIZONTAL/*, B_FANCY_BORDER*/))
186		.AddGroup(B_HORIZONTAL, spacing)
187			.SetInsets(0, spacing, spacing, spacing)
188			.AddGlue()
189			.Add(fMountAllNow)
190			.Add(fDone);
191
192	// Apply the settings
193
194	bool result;
195	if (settings->FindBool("autoMountAll", &result) == B_OK && result)
196		fAutoMountAllCheck->SetValue(B_CONTROL_ON);
197	else if (settings->FindBool("autoMountAllBFS", &result) == B_OK && result)
198		fAutoMountAllBFSCheck->SetValue(B_CONTROL_ON);
199	else
200		fScanningDisabledCheck->SetValue(B_CONTROL_ON);
201
202	if (settings->FindBool("suspended", &result) == B_OK && result)
203		fScanningDisabledCheck->SetValue(B_CONTROL_ON);
204
205	if (settings->FindBool("initialMountAll", &result) == B_OK && result)
206		fInitialMountAllCheck->SetValue(B_CONTROL_ON);
207	else if (settings->FindBool("initialMountRestore", &result) == B_OK
208		&& result) {
209		fInitialMountRestoreCheck->SetValue(B_CONTROL_ON);
210	} else if (settings->FindBool("initialMountAllBFS", &result) == B_OK
211		&& result) {
212		fInitialMountAllBFSCheck->SetValue(B_CONTROL_ON);
213	} else
214		fInitialDontMountCheck->SetValue(B_CONTROL_ON);
215
216	if (settings->FindBool("ejectWhenUnmounting", &result) == B_OK && result)
217		fEjectWhenUnmountingCheckBox->SetValue(B_CONTROL_ON);
218}
219
220
221AutomountSettingsPanel::~AutomountSettingsPanel()
222{
223}
224
225
226void
227AutomountSettingsPanel::AttachedToWindow()
228{
229	fInitialMountAllCheck->SetTarget(this);
230	fInitialMountAllBFSCheck->SetTarget(this);
231	fInitialMountRestoreCheck->SetTarget(this);
232	fInitialDontMountCheck->SetTarget(this);
233
234	fAutoMountAllCheck->SetTarget(this);
235	fAutoMountAllBFSCheck->SetTarget(this);
236	fScanningDisabledCheck->SetTarget(this);
237	fEjectWhenUnmountingCheckBox->SetTarget(this);
238
239	fDone->SetTarget(this);
240	fMountAllNow->SetTarget(fTarget);
241}
242
243
244void
245AutomountSettingsPanel::MessageReceived(BMessage* message)
246{
247	switch (message->what) {
248		case B_QUIT_REQUESTED:
249			Window()->Quit();
250			break;
251
252		case kAutomountSettingsChanged:
253			_SendSettings(true);
254			break;
255
256		case kBootMountSettingsChanged:
257		case kEjectWhenUnmountingChanged:
258			_SendSettings(false);
259			break;
260
261		default:
262			_inherited::MessageReceived(message);
263			break;
264	}
265}
266
267
268void
269AutomountSettingsPanel::_SendSettings(bool rescan)
270{
271	BMessage message(kSetAutomounterParams);
272
273	message.AddBool("autoMountAll", (bool)fAutoMountAllCheck->Value());
274	message.AddBool("autoMountAllBFS", (bool)fAutoMountAllBFSCheck->Value());
275	if (fAutoMountAllBFSCheck->Value())
276		message.AddBool("autoMountAllHFS", false);
277
278	message.AddBool("suspended", (bool)fScanningDisabledCheck->Value());
279	message.AddBool("rescanNow", rescan);
280
281	message.AddBool("initialMountAll", (bool)fInitialMountAllCheck->Value());
282	message.AddBool("initialMountAllBFS",
283		(bool)fInitialMountAllBFSCheck->Value());
284	message.AddBool("initialMountRestore",
285		(bool)fInitialMountRestoreCheck->Value());
286	if (fInitialDontMountCheck->Value())
287		message.AddBool("initialMountAllHFS", false);
288
289	message.AddBool("ejectWhenUnmounting",
290		(bool)fEjectWhenUnmountingCheckBox->Value());
291
292	fTarget.SendMessage(&message);
293}
294
295
296//	#pragma mark -
297
298
299AutomountSettingsDialog::AutomountSettingsDialog(BMessage* settings,
300		const BMessenger& target)
301	:
302	BWindow(BRect(100, 100, 320, 370), B_TRANSLATE("Disk mount settings"),
303		B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE
304		| B_AUTO_UPDATE_SIZE_LIMITS)
305{
306	SetLayout(new BGroupLayout(B_HORIZONTAL));
307	BView* view = new AutomountSettingsPanel(settings, target);
308	AddChild(view);
309
310	ASSERT(!sOneCopyOnly);
311	sOneCopyOnly = this;
312}
313
314
315AutomountSettingsDialog::~AutomountSettingsDialog()
316{
317	ASSERT(sOneCopyOnly);
318	sOneCopyOnly = NULL;
319}
320
321
322void
323AutomountSettingsDialog::RunAutomountSettings(const BMessenger& target)
324{
325	// either activate an existing mount settings dialog or create a new one
326	if (sOneCopyOnly) {
327		sOneCopyOnly->Activate();
328		return;
329	}
330
331	BMessage message(kGetAutomounterParams);
332	BMessage reply;
333	status_t ret = target.SendMessage(&message, &reply, 2500000);
334	if (ret != B_OK) {
335		BAlert* alert = new BAlert(B_TRANSLATE("Mount server error"),
336			B_TRANSLATE("The mount server could not be contacted."),
337			B_TRANSLATE("OK"),
338			NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
339		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
340		alert->Go();
341		return;
342	}
343
344	(new AutomountSettingsDialog(&reply, target))->Show();
345}
346