Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / kits / tracker / FilePermissionsView.cpp
blob5aeb73c3bf00e177ada713e5314a815131d7d175
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 "FilePermissionsView.h"
38 #include <stdio.h>
39 #include <stdlib.h>
41 #include <Beep.h>
42 #include <Catalog.h>
43 #include <Locale.h>
46 #undef B_TRANSLATION_CONTEXT
47 #define B_TRANSLATION_CONTEXT "FilePermissionsView"
50 const uint32 kPermissionsChanged = 'prch';
51 const uint32 kNewOwnerEntered = 'nwow';
52 const uint32 kNewGroupEntered = 'nwgr';
55 // #pragma mark - FilePermissionsView
58 FilePermissionsView::FilePermissionsView(BRect rect, Model* model)
60 BView(rect, "FilePermissionsView", B_FOLLOW_LEFT_RIGHT, B_WILL_DRAW),
61 fModel(model)
63 // Constants for the column labels: "User", "Group" and "Other".
64 const float kColumnLabelMiddle = 77, kColumnLabelTop = 6,
65 kColumnLabelSpacing = 37, kColumnLabelBottom = 20,
66 kColumnLabelWidth = 35, kAttribFontHeight = 10;
68 BStringView* strView;
70 strView = new BStringView(
71 BRect(kColumnLabelMiddle - kColumnLabelWidth / 2,
72 kColumnLabelTop,
73 kColumnLabelMiddle + kColumnLabelWidth / 2,
74 kColumnLabelBottom),
75 "", B_TRANSLATE("Owner"));
76 AddChild(strView);
77 strView->SetAlignment(B_ALIGN_CENTER);
78 strView->SetFontSize(kAttribFontHeight);
80 strView = new BStringView(
81 BRect(kColumnLabelMiddle - kColumnLabelWidth / 2
82 + kColumnLabelSpacing,
83 kColumnLabelTop,
84 kColumnLabelMiddle + kColumnLabelWidth / 2 + kColumnLabelSpacing,
85 kColumnLabelBottom),
86 "", B_TRANSLATE("Group"));
87 AddChild(strView);
88 strView->SetAlignment(B_ALIGN_CENTER);
89 strView->SetFontSize(kAttribFontHeight);
91 strView = new BStringView(
92 BRect(kColumnLabelMiddle - kColumnLabelWidth / 2
93 + 2 * kColumnLabelSpacing,
94 kColumnLabelTop,
95 kColumnLabelMiddle + kColumnLabelWidth / 2
96 + 2 * kColumnLabelSpacing,
97 kColumnLabelBottom),
98 "", B_TRANSLATE("Other"));
99 AddChild(strView);
100 strView->SetAlignment(B_ALIGN_CENTER);
101 strView->SetFontSize(kAttribFontHeight);
103 // Constants for the row labels: "Read", "Write" and "Execute".
104 const float kRowLabelLeft = 10, kRowLabelTop = kColumnLabelTop + 15,
105 kRowLabelVerticalSpacing = 18, kRowLabelRight = kColumnLabelMiddle
106 - kColumnLabelWidth / 2 - 5, kRowLabelHeight = 14;
108 strView = new BStringView(BRect(kRowLabelLeft, kRowLabelTop,
109 kRowLabelRight, kRowLabelTop + kRowLabelHeight),
110 "", B_TRANSLATE("Read"));
111 AddChild(strView);
112 strView->SetAlignment(B_ALIGN_RIGHT);
113 strView->SetFontSize(kAttribFontHeight);
115 strView = new BStringView(BRect(kRowLabelLeft, kRowLabelTop
116 + kRowLabelVerticalSpacing, kRowLabelRight, kRowLabelTop
117 + kRowLabelVerticalSpacing + kRowLabelHeight),
118 "", B_TRANSLATE("Write"));
119 AddChild(strView);
120 strView->SetAlignment(B_ALIGN_RIGHT);
121 strView->SetFontSize(kAttribFontHeight);
123 strView = new BStringView(BRect(kRowLabelLeft, kRowLabelTop
124 + 2 * kRowLabelVerticalSpacing, kRowLabelRight, kRowLabelTop
125 + 2 * kRowLabelVerticalSpacing + kRowLabelHeight),
126 "", B_TRANSLATE("Execute"));
127 AddChild(strView);
128 strView->SetAlignment(B_ALIGN_RIGHT);
129 strView->SetFontSize(kAttribFontHeight);
131 // Constants for the 3x3 check box array.
132 const float kLeftMargin = kRowLabelRight + 15,
133 kTopMargin = kRowLabelTop - 2,
134 kHorizontalSpacing = kColumnLabelSpacing,
135 kVerticalSpacing = kRowLabelVerticalSpacing,
136 kCheckBoxWidth = 18, kCheckBoxHeight = 18;
138 BCheckBox** checkBoxArray[3][3] = {
140 &fReadUserCheckBox,
141 &fReadGroupCheckBox,
142 &fReadOtherCheckBox
145 &fWriteUserCheckBox,
146 &fWriteGroupCheckBox,
147 &fWriteOtherCheckBox
150 &fExecuteUserCheckBox,
151 &fExecuteGroupCheckBox,
152 &fExecuteOtherCheckBox
156 for (int32 x = 0; x < 3; x++) {
157 for (int32 y = 0; y < 3; y++) {
158 *checkBoxArray[y][x] =
159 new BCheckBox(BRect(kLeftMargin + kHorizontalSpacing * x,
160 kTopMargin + kVerticalSpacing * y,
161 kLeftMargin + kHorizontalSpacing * x + kCheckBoxWidth,
162 kTopMargin + kVerticalSpacing * y + kCheckBoxHeight),
163 "", "", new BMessage(kPermissionsChanged));
164 AddChild(*checkBoxArray[y][x]);
168 const float kTextControlLeft = 170, kTextControlRight = 270,
169 kTextControlTop = kColumnLabelTop, kTextControlHeight = 14,
170 kTextControlSpacing = 16;
172 strView = new BStringView(BRect(kTextControlLeft, kTextControlTop,
173 kTextControlRight, kTextControlTop + kTextControlHeight), "",
174 B_TRANSLATE("Owner"));
175 strView->SetAlignment(B_ALIGN_CENTER);
176 strView->SetFontSize(kAttribFontHeight);
177 AddChild(strView);
179 fOwnerTextControl = new BTextControl(
180 BRect(kTextControlLeft,
181 kTextControlTop - 2 + kTextControlSpacing,
182 kTextControlRight,
183 kTextControlTop + kTextControlHeight - 2 + kTextControlSpacing),
184 "", "", "", new BMessage(kNewOwnerEntered));
185 fOwnerTextControl->SetDivider(0);
186 AddChild(fOwnerTextControl);
188 strView = new BStringView(BRect(kTextControlLeft,
189 kTextControlTop + 5 + 2 * kTextControlSpacing,
190 kTextControlRight,
191 kTextControlTop + 2 + 2 * kTextControlSpacing
192 + kTextControlHeight),
193 "", B_TRANSLATE("Group"));
194 strView->SetAlignment(B_ALIGN_CENTER);
195 strView->SetFontSize(kAttribFontHeight);
196 AddChild(strView);
198 fGroupTextControl = new BTextControl(BRect(kTextControlLeft,
199 kTextControlTop + 3 * kTextControlSpacing,
200 kTextControlRight,
201 kTextControlTop + 3 * kTextControlSpacing + kTextControlHeight),
202 "", "", "", new BMessage(kNewGroupEntered));
203 fGroupTextControl->SetDivider(0);
204 AddChild(fGroupTextControl);
206 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
208 ModelChanged(model);
212 void
213 FilePermissionsView::ModelChanged(Model* model)
215 fModel = model;
217 bool hideCheckBoxes = false;
218 uid_t nodeOwner = 0;
219 gid_t nodeGroup = 0;
220 mode_t perms = 0;
222 if (fModel != NULL) {
223 BNode node(fModel->EntryRef());
225 if (node.InitCheck() == B_OK) {
226 if (fReadUserCheckBox->IsHidden()) {
227 fReadUserCheckBox->Show();
228 fReadGroupCheckBox->Show();
229 fReadOtherCheckBox->Show();
230 fWriteUserCheckBox->Show();
231 fWriteGroupCheckBox->Show();
232 fWriteOtherCheckBox->Show();
233 fExecuteUserCheckBox->Show();
234 fExecuteGroupCheckBox->Show();
235 fExecuteOtherCheckBox->Show();
238 if (node.GetPermissions(&perms) == B_OK) {
239 fReadUserCheckBox->SetValue((int32)(perms & S_IRUSR));
240 fReadGroupCheckBox->SetValue((int32)(perms & S_IRGRP));
241 fReadOtherCheckBox->SetValue((int32)(perms & S_IROTH));
242 fWriteUserCheckBox->SetValue((int32)(perms & S_IWUSR));
243 fWriteGroupCheckBox->SetValue((int32)(perms & S_IWGRP));
244 fWriteOtherCheckBox->SetValue((int32)(perms & S_IWOTH));
245 fExecuteUserCheckBox->SetValue((int32)(perms & S_IXUSR));
246 fExecuteGroupCheckBox->SetValue((int32)(perms & S_IXGRP));
247 fExecuteOtherCheckBox->SetValue((int32)(perms & S_IXOTH));
248 } else
249 hideCheckBoxes = true;
251 if (node.GetOwner(&nodeOwner) == B_OK) {
252 BString user;
253 if (nodeOwner == 0)
254 if (getenv("USER") != NULL)
255 user << getenv("USER");
256 else
257 user << "root";
258 else
259 user << nodeOwner;
260 fOwnerTextControl->SetText(user.String());
261 } else
262 fOwnerTextControl->SetText(B_TRANSLATE("Unknown"));
264 if (node.GetGroup(&nodeGroup) == B_OK) {
265 BString group;
266 if (nodeGroup == 0)
267 if (getenv("GROUP") != NULL)
268 group << getenv("GROUP");
269 else
270 group << "0";
271 else
272 group << nodeGroup;
273 fGroupTextControl->SetText(group.String());
274 } else
275 fGroupTextControl->SetText(B_TRANSLATE("Unknown"));
277 // Unless we're root, only allow the owner to transfer the
278 // ownership, i.e. disable text controls if uid:s doesn't match:
279 thread_id thisThread = find_thread(NULL);
280 thread_info threadInfo;
281 get_thread_info(thisThread, &threadInfo);
282 team_info teamInfo;
283 get_team_info(threadInfo.team, &teamInfo);
284 if (teamInfo.uid != 0 && nodeOwner != teamInfo.uid) {
285 fOwnerTextControl->SetEnabled(false);
286 fGroupTextControl->SetEnabled(false);
287 } else {
288 fOwnerTextControl->SetEnabled(true);
289 fGroupTextControl->SetEnabled(true);
291 } else
292 hideCheckBoxes = true;
293 } else
294 hideCheckBoxes = true;
296 if (hideCheckBoxes) {
297 fReadUserCheckBox->Hide();
298 fReadGroupCheckBox->Hide();
299 fReadOtherCheckBox->Hide();
300 fWriteUserCheckBox->Hide();
301 fWriteGroupCheckBox->Hide();
302 fWriteOtherCheckBox->Hide();
303 fExecuteUserCheckBox->Hide();
304 fExecuteGroupCheckBox->Hide();
305 fExecuteOtherCheckBox->Hide();
310 void
311 FilePermissionsView::MessageReceived(BMessage* message)
313 switch(message->what) {
314 case kPermissionsChanged:
315 if (fModel != NULL) {
316 mode_t newPermissions = 0;
317 newPermissions
318 = (mode_t)((fReadUserCheckBox->Value() ? S_IRUSR : 0)
319 | (fReadGroupCheckBox->Value() ? S_IRGRP : 0)
320 | (fReadOtherCheckBox->Value() ? S_IROTH : 0)
322 | (fWriteUserCheckBox->Value() ? S_IWUSR : 0)
323 | (fWriteGroupCheckBox->Value() ? S_IWGRP : 0)
324 | (fWriteOtherCheckBox->Value() ? S_IWOTH : 0)
326 | (fExecuteUserCheckBox->Value() ? S_IXUSR : 0)
327 | (fExecuteGroupCheckBox->Value() ? S_IXGRP :0)
328 | (fExecuteOtherCheckBox->Value() ? S_IXOTH : 0));
330 BNode node(fModel->EntryRef());
332 if (node.InitCheck() == B_OK)
333 node.SetPermissions(newPermissions);
334 else {
335 ModelChanged(fModel);
336 beep();
339 break;
341 case kNewOwnerEntered:
342 if (fModel != NULL) {
343 uid_t owner;
344 if (sscanf(fOwnerTextControl->Text(), "%d", &owner) == 1) {
345 BNode node(fModel->EntryRef());
346 if (node.InitCheck() == B_OK)
347 node.SetOwner(owner);
348 else {
349 ModelChanged(fModel);
350 beep();
352 } else {
353 ModelChanged(fModel);
354 beep();
357 break;
359 case kNewGroupEntered:
360 if (fModel != NULL) {
361 gid_t group;
362 if (sscanf(fGroupTextControl->Text(), "%d", &group) == 1) {
363 BNode node(fModel->EntryRef());
364 if (node.InitCheck() == B_OK)
365 node.SetGroup(group);
366 else {
367 ModelChanged(fModel);
368 beep();
370 } else {
371 ModelChanged(fModel);
372 beep();
375 break;
377 default:
378 _inherited::MessageReceived(message);
379 break;
384 void
385 FilePermissionsView::AttachedToWindow()
387 fReadUserCheckBox->SetTarget(this);
388 fReadGroupCheckBox->SetTarget(this);
389 fReadOtherCheckBox->SetTarget(this);
390 fWriteUserCheckBox->SetTarget(this);
391 fWriteGroupCheckBox->SetTarget(this);
392 fWriteOtherCheckBox->SetTarget(this);
393 fExecuteUserCheckBox->SetTarget(this);
394 fExecuteGroupCheckBox->SetTarget(this);
395 fExecuteOtherCheckBox->SetTarget(this);
397 fOwnerTextControl->SetTarget(this);
398 fGroupTextControl->SetTarget(this);