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