xref: /haiku/src/preferences/screensaver/PasswordWindow.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2003-2006, Haiku.
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  */
9 
10 
11 #include "PasswordWindow.h"
12 
13 #include <Alert.h>
14 #include <Box.h>
15 #include <Button.h>
16 #include <RadioButton.h>
17 #include <Screen.h>
18 #include <TextControl.h>
19 
20 #include <stdio.h>
21 #include <unistd.h>
22 
23 
24 const uint32 kMsgDone = 'done';
25 const uint32 kMsgPasswordTypeChanged = 'pwtp';
26 
27 
28 PasswordWindow::PasswordWindow(ScreenSaverSettings& settings)
29 	: BWindow(BRect(100, 100, 380, 249), "Password Window", B_MODAL_WINDOW_LOOK,
30 		B_MODAL_APP_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_NOT_RESIZABLE),
31 	fSettings(settings)
32 {
33 	_Setup();
34 
35 	BRect screenFrame = BScreen(B_MAIN_SCREEN_ID).Frame();
36 	BPoint point;
37 	point.x = (screenFrame.Width() - Bounds().Width()) / 2;
38 	point.y = (screenFrame.Height() - Bounds().Height()) / 2;
39 
40 	if (screenFrame.Contains(point))
41 		MoveTo(point);
42 }
43 
44 
45 void
46 PasswordWindow::_Setup()
47 {
48 	BView* topView = new BView(Bounds(), "mainView", B_FOLLOW_ALL, B_WILL_DRAW);
49 	topView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
50 	AddChild(topView);
51 
52 	fUseNetwork = new BRadioButton(BRect(14,10,159,20), "useNetwork",
53 		"Use Network password", new BMessage(kMsgPasswordTypeChanged), B_FOLLOW_NONE);
54 	topView->AddChild(fUseNetwork);
55 	fUseCustom = new BRadioButton(BRect(30,50,130,60), "fUseCustom",
56 		"Use custom password", new BMessage(kMsgPasswordTypeChanged), B_FOLLOW_NONE);
57 	fUseNetwork->ResizeToPreferred();
58 	fUseCustom->ResizeToPreferred();
59 
60 	BBox *customBox = new BBox(BRect(9,30,269,105), "custBeBox", B_FOLLOW_NONE);
61 	customBox->SetLabel(fUseCustom);
62 	fPasswordControl = new BTextControl(BRect(10,20,251,35), "pwdCntrl", "Password:", NULL, B_FOLLOW_NONE);
63 	fConfirmControl = new BTextControl(BRect(10,45,251,60), "fConfirmCntrl", "Confirm password:", NULL, B_FOLLOW_NONE);
64 	fPasswordControl->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
65 	float divider = be_plain_font->StringWidth("Confirm password:") + 5.0;
66 	fPasswordControl->SetDivider(divider);
67 	fPasswordControl->TextView()->HideTyping(true);
68 	fConfirmControl->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
69 	fConfirmControl->SetDivider(divider);
70 	fConfirmControl->TextView()->HideTyping(true);
71 	customBox->AddChild(fPasswordControl);
72 	customBox->AddChild(fConfirmControl);
73 	topView->AddChild(customBox);
74 
75 	BButton* button = new BButton(BRect(194,118,269,129), "done", "Done", new BMessage(kMsgDone), B_FOLLOW_NONE);
76 	topView->AddChild(button);
77 	button->MakeDefault(true);
78 
79 	button = new BButton(BRect(109,118,184,129), "cancel", "Cancel", new BMessage(B_CANCEL), B_FOLLOW_NONE);
80 	topView->AddChild(button);
81 
82 	Update();
83 }
84 
85 
86 void
87 PasswordWindow::Update()
88 {
89 	if (fSettings.IsNetworkPassword())
90 		fUseNetwork->SetValue(B_CONTROL_ON);
91 	else
92 		fUseCustom->SetValue(B_CONTROL_ON);
93 	bool useNetPassword=(fUseCustom->Value()>0);
94 	fConfirmControl->SetEnabled(useNetPassword);
95 	fPasswordControl->SetEnabled(useNetPassword);
96 }
97 
98 
99 void
100 PasswordWindow::MessageReceived(BMessage *message)
101 {
102 	switch (message->what) {
103 		case kMsgDone:
104 			fSettings.SetLockMethod(fUseCustom->Value() ? "custom" : "network");
105 			if (fUseCustom->Value()) {
106 				if (strcmp(fPasswordControl->Text(),fConfirmControl->Text())) {
107 					BAlert *alert=new BAlert("noMatch","Passwords don't match. Try again.","OK");
108 					alert->Go();
109 					break;
110 				}
111 				fSettings.SetPassword(crypt(fPasswordControl->Text(), fPasswordControl->Text()));
112 			} else {
113 				fSettings.SetPassword("");
114 			}
115 			fPasswordControl->SetText("");
116 			fConfirmControl->SetText("");
117 			Hide();
118 			break;
119 
120 	case B_CANCEL:
121 		fPasswordControl->SetText("");
122 		fConfirmControl->SetText("");
123 		Hide();
124 		break;
125 
126 	case kMsgPasswordTypeChanged:
127 		fSettings.SetLockMethod(fUseCustom->Value() > 0 ? "custom" : "network");
128 		Update();
129 		break;
130 
131 	default:
132 		BWindow::MessageReceived(message);
133 		break;
134  	}
135 }
136