xref: /haiku/src/preferences/screensaver/PasswordWindow.cpp (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 /*
2  * Copyright 2003-2005, 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 #include "PasswordWindow.h"
11 #include <stdio.h>
12 #include <Alert.h>
13 #include <Box.h>
14 #include <RadioButton.h>
15 #include <Screen.h>
16 
17 
18 PasswordWindow::PasswordWindow(ScreenSaverPrefs &prefs)
19 	: BWindow(BRect(100,100,380,249),"",B_MODAL_WINDOW_LOOK,B_MODAL_APP_WINDOW_FEEL,B_NOT_RESIZABLE),
20 	fPrefs(prefs)
21 {
22 	Setup();
23 	BRect screenFrame = BScreen(B_MAIN_SCREEN_ID).Frame();
24 	BPoint point;
25 	point.x = (screenFrame.Width() - Bounds().Width()) / 2;
26 	point.y = (screenFrame.Height() - Bounds().Height()) / 2;
27 
28 	if (screenFrame.Contains(point))
29 		MoveTo(point);
30 }
31 
32 
33 void
34 PasswordWindow::Setup()
35 {
36 	BBox *owner=new BBox(Bounds(),"ownerView",B_FOLLOW_NONE,B_WILL_DRAW, B_PLAIN_BORDER);
37 	AddChild(owner);
38 	fUseNetwork=new BRadioButton(BRect(14,10,159,20),"useNetwork","Use Network password",new BMessage(kButton_changed),B_FOLLOW_NONE);
39 	owner->AddChild(fUseNetwork);
40 	fUseCustom=new BRadioButton(BRect(30,50,130,60),"fUseCustom","Use custom password",new BMessage(kButton_changed),B_FOLLOW_NONE);
41 
42 	BBox *customBox=new BBox(BRect(9,30,269,105),"custBeBox",B_FOLLOW_NONE);
43 	customBox->SetLabel(fUseCustom);
44 	fPassword=new BTextControl(BRect(10,20,251,35),"pwdCntrl","Password:",NULL,B_FOLLOW_NONE);
45 	fConfirm=new BTextControl(BRect(10,45,251,60),"fConfirmCntrl","Confirm password:",NULL,B_FOLLOW_NONE);
46 	fPassword->SetAlignment(B_ALIGN_RIGHT,B_ALIGN_LEFT);
47 	float divider = be_plain_font->StringWidth("Confirm password:") + 5.0;
48 	fPassword->SetDivider(divider);
49 	fPassword->TextView()->HideTyping(true);
50 	fConfirm->SetAlignment(B_ALIGN_RIGHT,B_ALIGN_LEFT);
51 	fConfirm->SetDivider(divider);
52 	fConfirm->TextView()->HideTyping(true);
53 	customBox->AddChild(fPassword);
54 	customBox->AddChild(fConfirm);
55 	owner->AddChild(customBox);
56 
57 	fDone = new BButton(BRect(194,118,269,129),"done","Done",new BMessage (kDone_clicked),B_FOLLOW_NONE);
58 	fCancel = new BButton(BRect(109,118,184,129),"cancel","Cancel",new BMessage (kCancel_clicked),B_FOLLOW_NONE);
59 	owner->AddChild(fDone);
60 	owner->AddChild(fCancel);
61 	fDone->MakeDefault(true);
62 	Update();
63 }
64 
65 
66 void
67 PasswordWindow::Update()
68 {
69 	if (fPrefs.IsNetworkPassword())
70 		fUseNetwork->SetValue(B_CONTROL_ON);
71 	else
72 		fUseCustom->SetValue(B_CONTROL_ON);
73 	bool useNetPassword=(fUseCustom->Value()>0);
74 	fConfirm->SetEnabled(useNetPassword);
75 	fPassword->SetEnabled(useNetPassword);
76 }
77 
78 
79 void
80 PasswordWindow::MessageReceived(BMessage *message)
81 {
82 	switch(message->what) {
83 	case kDone_clicked:
84 		fPrefs.SetLockMethod(fUseCustom->Value() ? "custom" : "network");
85 		if (fUseCustom->Value()) {
86 			if (strcmp(fPassword->Text(),fConfirm->Text())) {
87 				BAlert *alert=new BAlert("noMatch","Passwords don't match. Try again.","OK");
88 				alert->Go();
89 				break;
90 			}
91 			fPrefs.SetPassword(crypt(fPassword->Text(), fPassword->Text()));
92 		} else {
93 			fPrefs.SetPassword("");
94 		}
95 		fPassword->SetText("");
96 		fConfirm->SetText("");
97 		Hide();
98 		break;
99 	case kCancel_clicked:
100 		fPassword->SetText("");
101 		fConfirm->SetText("");
102 		Hide();
103 		break;
104 	case kButton_changed:
105 		fPrefs.SetLockMethod(fUseCustom->Value()>0 ? "custom" : "network");
106 		Update();
107 		break;
108 	case kShow:
109 		Show();
110 		break;
111 	default:
112 		BWindow::MessageReceived(message);
113 		break;
114  	}
115 }
116