2 * Copyright 2003-2013 Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Jérôme Duval, jerome.duval@free.fr
7 * Julun, host.haiku@gmx.de
9 * John Scipione, jscipione@gmail.com
13 #include "PasswordWindow.h"
19 #include <ControlLook.h>
20 #include <LayoutBuilder.h>
21 #include <LayoutItem.h>
22 #include <RadioButton.h>
25 #include <TextControl.h>
29 #include "ScreenSaverSettings.h"
32 #undef B_TRANSLATION_CONTEXT
33 #define B_TRANSLATION_CONTEXT "ScreenSaver"
36 static const uint32 kPasswordTextWidth
= 12;
38 static const uint32 kMsgDone
= 'done';
39 static const uint32 kMsgPasswordTypeChanged
= 'pwtp';
42 PasswordWindow::PasswordWindow(ScreenSaverSettings
& settings
)
44 BWindow(BRect(100, 100, 300, 200), B_TRANSLATE("Password Window"),
45 B_MODAL_WINDOW_LOOK
, B_MODAL_APP_WINDOW_FEEL
, B_NOT_RESIZABLE
46 | B_ASYNCHRONOUS_CONTROLS
| B_AUTO_UPDATE_SIZE_LIMITS
),
55 PasswordWindow::_Setup()
57 float spacing
= be_control_look
->DefaultItemSpacing();
59 BView
* topView
= new BView("topView", B_WILL_DRAW
);
60 topView
->SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
61 topView
->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED
, B_SIZE_UNLIMITED
));
63 BBox
* networkBox
= new BBox("networkBox");
64 networkBox
->SetBorder(B_NO_BORDER
);
66 fUseNetwork
= new BRadioButton("useNetwork",
67 B_TRANSLATE("Use network password"),
68 new BMessage(kMsgPasswordTypeChanged
));
69 networkBox
->SetLabel(fUseNetwork
);
71 BBox
* customBox
= new BBox("customBox");
73 fUseCustom
= new BRadioButton("useCustom",
74 B_TRANSLATE("Use custom password"),
75 new BMessage(kMsgPasswordTypeChanged
));
76 customBox
->SetLabel(fUseCustom
);
78 fPasswordControl
= new BTextControl("passwordTextView",
79 B_TRANSLATE("Password:"), B_EMPTY_STRING
, NULL
);
80 fPasswordControl
->TextView()->HideTyping(true);
81 fPasswordControl
->SetAlignment(B_ALIGN_RIGHT
, B_ALIGN_LEFT
);
83 BLayoutItem
* passwordTextView
84 = fPasswordControl
->CreateTextViewLayoutItem();
85 passwordTextView
->SetExplicitMinSize(BSize(spacing
* kPasswordTextWidth
,
88 fConfirmControl
= new BTextControl("confirmTextView",
89 B_TRANSLATE("Confirm password:"), B_EMPTY_STRING
, NULL
);
90 fConfirmControl
->SetExplicitMinSize(BSize(spacing
* kPasswordTextWidth
,
92 fConfirmControl
->TextView()->HideTyping(true);
93 fConfirmControl
->SetAlignment(B_ALIGN_RIGHT
, B_ALIGN_LEFT
);
95 BLayoutItem
* confirmTextView
= fConfirmControl
->CreateTextViewLayoutItem();
96 confirmTextView
->SetExplicitMinSize(BSize(spacing
* kPasswordTextWidth
,
99 customBox
->AddChild(BLayoutBuilder::Group
<>(B_VERTICAL
)
100 .SetInsets(B_USE_SMALL_SPACING
)
101 .AddGrid(B_USE_DEFAULT_SPACING
, B_USE_SMALL_SPACING
)
102 .Add(fPasswordControl
->CreateLabelLayoutItem(), 0, 0)
103 .Add(passwordTextView
, 1, 0)
104 .Add(fConfirmControl
->CreateLabelLayoutItem(), 0, 1)
105 .Add(confirmTextView
, 1, 1)
109 BButton
* doneButton
= new BButton("done", B_TRANSLATE("Done"),
110 new BMessage(kMsgDone
));
112 BButton
* cancelButton
= new BButton("cancel", B_TRANSLATE("Cancel"),
113 new BMessage(B_CANCEL
));
115 BLayoutBuilder::Group
<>(topView
, B_VERTICAL
, 0)
116 .SetInsets(B_USE_DEFAULT_SPACING
)
119 .AddStrut(B_USE_DEFAULT_SPACING
)
120 .AddGroup(B_HORIZONTAL
)
127 doneButton
->MakeDefault(true);
129 SetLayout(new BGroupLayout(B_VERTICAL
));
130 GetLayout()->AddView(topView
);
135 PasswordWindow::Update()
137 if (fSettings
.IsNetworkPassword())
138 fUseNetwork
->SetValue(B_CONTROL_ON
);
140 fUseCustom
->SetValue(B_CONTROL_ON
);
142 bool useNetPassword
= (fUseCustom
->Value() > 0);
143 fConfirmControl
->SetEnabled(useNetPassword
);
144 fPasswordControl
->SetEnabled(useNetPassword
);
149 PasswordWindow::MessageReceived(BMessage
* message
)
151 switch (message
->what
) {
153 fSettings
.SetLockMethod(fUseCustom
->Value() ? "custom" : "network");
154 if (fUseCustom
->Value()) {
155 if (strcmp(fPasswordControl
->Text(), fConfirmControl
->Text())
157 BAlert
* alert
= new BAlert("noMatch",
158 B_TRANSLATE("Passwords don't match. Please try again."),
160 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
164 fSettings
.SetPassword(crypt(fPasswordControl
->Text(), NULL
));
166 fSettings
.SetPassword("");
168 fPasswordControl
->SetText("");
169 fConfirmControl
->SetText("");
175 fPasswordControl
->SetText("");
176 fConfirmControl
->SetText("");
180 case kMsgPasswordTypeChanged
:
181 fSettings
.SetLockMethod(fUseCustom
->Value() > 0 ? "custom" : "network");
186 BWindow::MessageReceived(message
);