xref: /haiku/src/preferences/screensaver/PasswordWindow.cpp (revision b30304acc8c37e678a1bf66976d15bdab103f931)
1 /*
2  * Copyright 2003-2007, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Phipps
7  *		Jérôme Duval, jerome.duval@free.fr
8  *		Julun <host.haiku@gmx.de>
9  */
10 
11 #include "PasswordWindow.h"
12 
13 
14 #include <Alert.h>
15 #include <Box.h>
16 #include <Button.h>
17 #include <RadioButton.h>
18 #include <Screen.h>
19 #include <TextControl.h>
20 
21 
22 const uint32 kMsgDone = 'done';
23 const uint32 kMsgPasswordTypeChanged = 'pwtp';
24 
25 
26 PasswordWindow::PasswordWindow(ScreenSaverSettings& settings)
27 	: BWindow(BRect(100, 100, 380, 249), "Password Window", B_MODAL_WINDOW_LOOK,
28 		B_MODAL_APP_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS | B_NOT_RESIZABLE),
29 	fSettings(settings)
30 {
31 	_Setup();
32 	Update();
33 
34 	BRect screenFrame = BScreen(B_MAIN_SCREEN_ID).Frame();
35 	BPoint point;
36 	point.x = (screenFrame.Width() - Bounds().Width()) / 2;
37 	point.y = (screenFrame.Height() - Bounds().Height()) / 2;
38 
39 	if (screenFrame.Contains(point))
40 		MoveTo(point);
41 }
42 
43 
44 void
45 PasswordWindow::_Setup()
46 {
47 	BRect bounds = Bounds();
48 	BView* topView = new BView(bounds, "topView", B_FOLLOW_ALL, B_WILL_DRAW);
49 	topView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
50 	AddChild(topView);
51 
52 	bounds.InsetBy(10.0, 10.0);
53 	fUseNetwork = new BRadioButton(bounds, "useNetwork", "Use Network password",
54 		new BMessage(kMsgPasswordTypeChanged), B_FOLLOW_NONE);
55 	topView->AddChild(fUseNetwork);
56 	fUseNetwork->ResizeToPreferred();
57 	fUseNetwork->MoveBy(10.0, 0.0);
58 
59 	bounds.OffsetBy(0.0, fUseNetwork->Bounds().Height());
60 	BBox *customBox = new BBox(bounds, "customBox", B_FOLLOW_NONE);
61 	topView->AddChild(customBox);
62 
63 	fUseCustom = new BRadioButton(BRect(), "useCustom", "Use custom password",
64 		new BMessage(kMsgPasswordTypeChanged), B_FOLLOW_NONE);
65 	customBox->SetLabel(fUseCustom);
66 	fUseCustom->ResizeToPreferred();
67 
68 	fPasswordControl = new BTextControl(bounds, "passwordControl", "Password:",
69 		NULL, B_FOLLOW_NONE);
70 	customBox->AddChild(fPasswordControl);
71 	fPasswordControl->ResizeToPreferred();
72 	fPasswordControl->TextView()->HideTyping(true);
73 	fPasswordControl->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
74 
75 	bounds.OffsetBy(0.0, fPasswordControl->Bounds().Height() + 5.0);
76 	fConfirmControl = new BTextControl(bounds, "confirmControl",
77 		"Confirm password:", "VeryLongPasswordPossible", B_FOLLOW_NONE);
78 	customBox->AddChild(fConfirmControl);
79 	fConfirmControl->TextView()->HideTyping(true);
80 	fConfirmControl->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
81 
82 	float width, height;
83 	fConfirmControl->GetPreferredSize(&width, &height);
84 	fPasswordControl->ResizeTo(width, height);
85 
86 	float divider = be_plain_font->StringWidth("Confirm password:") + 5.0;
87 	fConfirmControl->SetDivider(divider);
88 	fPasswordControl->SetDivider(divider);
89 
90 	customBox->ResizeTo(fConfirmControl->Frame().right + 10.0,
91 		fConfirmControl->Frame().bottom + 10.0);
92 
93 	BButton* button = new BButton(BRect(), "done", "Done", new BMessage(kMsgDone));
94 	topView->AddChild(button);
95 	button->ResizeToPreferred();
96 
97 	BRect frame = customBox->Frame();
98 	button->MoveTo(frame.right - button->Bounds().Width(), frame.bottom + 10.0);
99 
100 	frame = button->Frame();
101 	button->MakeDefault(true);
102 
103 	button = new BButton(frame, "cancel", "Cancel", new BMessage(B_CANCEL));
104 	topView->AddChild(button);
105 	button->ResizeToPreferred();
106 	button->MoveBy(-(button->Bounds().Width() + 10.0), 0.0);
107 
108 	ResizeTo(customBox->Frame().right + 10.0, frame.bottom + 10.0);
109 }
110 
111 
112 void
113 PasswordWindow::Update()
114 {
115 	if (fSettings.IsNetworkPassword())
116 		fUseNetwork->SetValue(B_CONTROL_ON);
117 	else
118 		fUseCustom->SetValue(B_CONTROL_ON);
119 
120 	bool useNetPassword = (fUseCustom->Value() > 0);
121 	fConfirmControl->SetEnabled(useNetPassword);
122 	fPasswordControl->SetEnabled(useNetPassword);
123 }
124 
125 
126 void
127 PasswordWindow::MessageReceived(BMessage *message)
128 {
129 	switch (message->what) {
130 		case kMsgDone:
131 			fSettings.SetLockMethod(fUseCustom->Value() ? "custom" : "network");
132 			if (fUseCustom->Value()) {
133 				if (strcmp(fPasswordControl->Text(), fConfirmControl->Text())) {
134 					BAlert *alert = new BAlert("noMatch",
135 						"Passwords don't match. Try again.","OK");
136 					alert->Go();
137 					break;
138 				}
139 				fSettings.SetPassword(crypt(fPasswordControl->Text(),
140 					fPasswordControl->Text()));
141 			} else {
142 				fSettings.SetPassword("");
143 			}
144 			fPasswordControl->SetText("");
145 			fConfirmControl->SetText("");
146 			fSettings.Save();
147 			Hide();
148 			break;
149 
150 	case B_CANCEL:
151 		fPasswordControl->SetText("");
152 		fConfirmControl->SetText("");
153 		Hide();
154 		break;
155 
156 	case kMsgPasswordTypeChanged:
157 		fSettings.SetLockMethod(fUseCustom->Value() > 0 ? "custom" : "network");
158 		Update();
159 		break;
160 
161 	default:
162 		BWindow::MessageReceived(message);
163 		break;
164  	}
165 }
166