vfs: check userland buffers before reading them.
[haiku.git] / src / kits / tracker / AutoMounterSettings.cpp
blobae8cf28eb73ac9ebc2ece06740807e81bb15965e
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
36 #include <Alert.h>
37 #include <Box.h>
38 #include <Button.h>
39 #include <Catalog.h>
40 #include <CheckBox.h>
41 #include <ControlLook.h>
42 #include <Debug.h>
43 #include <LayoutBuilder.h>
44 #include <Locale.h>
45 #include <Message.h>
46 #include <RadioButton.h>
47 #include <SeparatorView.h>
48 #include <SpaceLayoutItem.h>
49 #include <Window.h>
51 #include <MountServer.h>
53 #include "SettingsViews.h"
56 #undef B_TRANSLATION_CONTEXT
57 #define B_TRANSLATION_CONTEXT "AutoMounterSettings"
60 const uint32 kAutomountSettingsChanged = 'achg';
61 const uint32 kBootMountSettingsChanged = 'bchg';
62 const uint32 kEjectWhenUnmountingChanged = 'ejct';
65 // #pragma mark - AutomountSettingsPanel
68 AutomountSettingsPanel::AutomountSettingsPanel()
70 SettingsView(""),
71 fTarget(kMountServerSignature)
73 const float spacing = be_control_look->DefaultItemSpacing();
75 // "Automatic Disk Mounting" group
77 BBox* autoMountBox = new BBox("autoMountBox", B_WILL_DRAW | B_FRAME_EVENTS
78 | B_PULSE_NEEDED | B_NAVIGABLE_JUMP);
79 autoMountBox->SetLabel(B_TRANSLATE("Automatic disk mounting"));
80 BGroupLayout* autoMountLayout = new BGroupLayout(B_VERTICAL, 0);
81 autoMountBox->SetLayout(autoMountLayout);
82 autoMountLayout->SetInsets(spacing,
83 autoMountBox->InnerFrame().top + spacing / 2, spacing, spacing);
85 fScanningDisabledCheck = new BRadioButton("scanningOff",
86 B_TRANSLATE("Don't automount"),
87 new BMessage(kAutomountSettingsChanged));
89 fAutoMountAllBFSCheck = new BRadioButton("autoBFS",
90 B_TRANSLATE("All Haiku disks"),
91 new BMessage(kAutomountSettingsChanged));
93 fAutoMountAllCheck = new BRadioButton("autoAll",
94 B_TRANSLATE("All disks"), new BMessage(kAutomountSettingsChanged));
96 // "Disk Mounting During Boot" group
98 BBox* bootMountBox = new BBox("", B_WILL_DRAW | B_FRAME_EVENTS
99 | B_PULSE_NEEDED | B_NAVIGABLE_JUMP);
100 bootMountBox->SetLabel(B_TRANSLATE("Disk mounting during boot"));
101 BGroupLayout* bootMountLayout = new BGroupLayout(B_VERTICAL, 0);
102 bootMountBox->SetLayout(bootMountLayout);
103 bootMountLayout->SetInsets(spacing,
104 bootMountBox->InnerFrame().top + spacing / 2, spacing, spacing);
106 fInitialDontMountCheck = new BRadioButton("initialNone",
107 B_TRANSLATE("Only the boot disk"),
108 new BMessage(kBootMountSettingsChanged));
110 fInitialMountRestoreCheck = new BRadioButton("initialRestore",
111 B_TRANSLATE("Previously mounted disks"),
112 new BMessage(kBootMountSettingsChanged));
114 fInitialMountAllBFSCheck = new BRadioButton("initialBFS",
115 B_TRANSLATE("All Haiku disks"),
116 new BMessage(kBootMountSettingsChanged));
118 fInitialMountAllCheck = new BRadioButton("initialAll",
119 B_TRANSLATE("All disks"), new BMessage(kBootMountSettingsChanged));
121 fEjectWhenUnmountingCheckBox = new BCheckBox("ejectWhenUnmounting",
122 B_TRANSLATE("Eject when unmounting"),
123 new BMessage(kEjectWhenUnmountingChanged));
125 // Buttons
127 fMountAllNow = new BButton("mountAll", B_TRANSLATE("Mount all disks now"),
128 new BMessage(kMountAllNow));
130 // Layout the controls
132 BGroupView* contentView = new BGroupView(B_VERTICAL, 0);
133 AddChild(contentView);
134 BLayoutBuilder::Group<>(contentView)
135 .AddGroup(B_VERTICAL, spacing)
136 .SetInsets(spacing, spacing, spacing, spacing)
137 .AddGroup(autoMountLayout)
138 .Add(fScanningDisabledCheck)
139 .Add(fAutoMountAllBFSCheck)
140 .Add(fAutoMountAllCheck)
141 .End()
142 .AddGroup(bootMountLayout)
143 .Add(fInitialDontMountCheck)
144 .Add(fInitialMountRestoreCheck)
145 .Add(fInitialMountAllBFSCheck)
146 .Add(fInitialMountAllCheck)
147 .End()
148 .Add(fEjectWhenUnmountingCheckBox)
149 .End()
150 .Add(new BSeparatorView(B_HORIZONTAL/*, B_FANCY_BORDER*/))
151 .AddGroup(B_HORIZONTAL, spacing)
152 .SetInsets(spacing, spacing, spacing, spacing)
153 .Add(fMountAllNow)
154 .AddGlue();
156 ShowCurrentSettings();
160 AutomountSettingsPanel::~AutomountSettingsPanel()
165 bool
166 AutomountSettingsPanel::IsDefaultable() const
168 return false;
172 void
173 AutomountSettingsPanel::Revert()
175 _ParseSettings(fInitialSettings);
176 _SendSettings(false);
180 void
181 AutomountSettingsPanel::ShowCurrentSettings()
183 // Apply the settings
184 BMessage settings;
185 _GetSettings(&settings);
186 _ParseSettings(settings);
190 void
191 AutomountSettingsPanel::RecordRevertSettings()
193 _GetSettings(&fInitialSettings);
197 bool
198 AutomountSettingsPanel::IsRevertable() const
200 BMessage currentSettings;
201 _GetSettings(&currentSettings);
203 return !currentSettings.HasSameData(fInitialSettings);
207 void
208 AutomountSettingsPanel::AttachedToWindow()
210 fInitialMountAllCheck->SetTarget(this);
211 fInitialMountAllBFSCheck->SetTarget(this);
212 fInitialMountRestoreCheck->SetTarget(this);
213 fInitialDontMountCheck->SetTarget(this);
215 fAutoMountAllCheck->SetTarget(this);
216 fAutoMountAllBFSCheck->SetTarget(this);
217 fScanningDisabledCheck->SetTarget(this);
218 fEjectWhenUnmountingCheckBox->SetTarget(this);
220 fMountAllNow->SetTarget(fTarget);
224 void
225 AutomountSettingsPanel::MessageReceived(BMessage* message)
227 switch (message->what) {
228 case B_QUIT_REQUESTED:
229 Window()->Quit();
230 break;
232 case kAutomountSettingsChanged:
233 _SendSettings(true);
234 break;
236 case kBootMountSettingsChanged:
237 case kEjectWhenUnmountingChanged:
238 _SendSettings(false);
239 break;
241 default:
242 _inherited::MessageReceived(message);
243 break;
248 void
249 AutomountSettingsPanel::_SendSettings(bool rescan)
251 BMessage message(kSetAutomounterParams);
253 message.AddBool("autoMountAll", (bool)fAutoMountAllCheck->Value());
254 message.AddBool("autoMountAllBFS", (bool)fAutoMountAllBFSCheck->Value());
255 if (fAutoMountAllBFSCheck->Value())
256 message.AddBool("autoMountAllHFS", false);
258 message.AddBool("suspended", (bool)fScanningDisabledCheck->Value());
259 message.AddBool("rescanNow", rescan);
261 message.AddBool("initialMountAll", (bool)fInitialMountAllCheck->Value());
262 message.AddBool("initialMountAllBFS",
263 (bool)fInitialMountAllBFSCheck->Value());
264 message.AddBool("initialMountRestore",
265 (bool)fInitialMountRestoreCheck->Value());
266 if (fInitialDontMountCheck->Value())
267 message.AddBool("initialMountAllHFS", false);
269 message.AddBool("ejectWhenUnmounting",
270 (bool)fEjectWhenUnmountingCheckBox->Value());
272 fTarget.SendMessage(&message);
274 // Tell the settings window the contents have changed:
275 Window()->PostMessage(kSettingsContentsModified);
279 void
280 AutomountSettingsPanel::_GetSettings(BMessage* reply) const
282 BMessage message(kGetAutomounterParams);
283 if (fTarget.SendMessage(&message, reply, 2500000) != B_OK) {
284 BAlert* alert = new BAlert(B_TRANSLATE("Mount server error"),
285 B_TRANSLATE("The mount server could not be contacted."),
286 B_TRANSLATE("OK"),
287 NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
288 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
289 alert->Go();
294 void
295 AutomountSettingsPanel::_ParseSettings(const BMessage& settings)
297 bool result;
298 if (settings.FindBool("autoMountAll", &result) == B_OK && result)
299 fAutoMountAllCheck->SetValue(B_CONTROL_ON);
300 else if (settings.FindBool("autoMountAllBFS", &result) == B_OK && result)
301 fAutoMountAllBFSCheck->SetValue(B_CONTROL_ON);
302 else
303 fScanningDisabledCheck->SetValue(B_CONTROL_ON);
305 if (settings.FindBool("suspended", &result) == B_OK && result)
306 fScanningDisabledCheck->SetValue(B_CONTROL_ON);
308 if (settings.FindBool("initialMountAll", &result) == B_OK && result)
309 fInitialMountAllCheck->SetValue(B_CONTROL_ON);
310 else if (settings.FindBool("initialMountRestore", &result) == B_OK
311 && result) {
312 fInitialMountRestoreCheck->SetValue(B_CONTROL_ON);
313 } else if (settings.FindBool("initialMountAllBFS", &result) == B_OK
314 && result) {
315 fInitialMountAllBFSCheck->SetValue(B_CONTROL_ON);
316 } else
317 fInitialDontMountCheck->SetValue(B_CONTROL_ON);
319 if (settings.FindBool("ejectWhenUnmounting", &result) == B_OK && result)
320 fEjectWhenUnmountingCheckBox->SetValue(B_CONTROL_ON);