xref: /haiku/src/apps/webpositive/AuthenticationPanel.cpp (revision c237c4ce593ee823d9867fd997e51e4c447f5623)
1 /*
2  * Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de>
3  *
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 #include "AuthenticationPanel.h"
7 
8 #include <Button.h>
9 #include <Catalog.h>
10 #include <CheckBox.h>
11 #include <ControlLook.h>
12 #include <GridLayoutBuilder.h>
13 #include <GroupLayoutBuilder.h>
14 #include <Locale.h>
15 #include <Message.h>
16 #include <Screen.h>
17 #include <SeparatorView.h>
18 #include <SpaceLayoutItem.h>
19 #include <StringView.h>
20 #include <TextControl.h>
21 #include <stdio.h>
22 
23 static const uint32 kMsgPanelOK = 'pnok';
24 static const uint32 kMsgJitter = 'jitr';
25 static const uint32 kHidePassword = 'hdpw';
26 
27 
28 #undef B_TRANSLATION_CONTEXT
29 #define B_TRANSLATION_CONTEXT "Authentication Panel"
30 
31 AuthenticationPanel::AuthenticationPanel(BRect parentFrame)
32 	:
33 	BWindow(BRect(-1000, -1000, -900, -900),
34 		B_TRANSLATE("Authentication required"), B_TITLED_WINDOW_LOOK,
35 		B_MODAL_APP_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS | B_NOT_RESIZABLE
36 			| B_NOT_ZOOMABLE | B_CLOSE_ON_ESCAPE | B_AUTO_UPDATE_SIZE_LIMITS),
37 	m_parentWindowFrame(parentFrame),
38 	m_usernameTextControl(new BTextControl("user", B_TRANSLATE("Username:"),
39 		"", NULL)),
40 	m_passwordTextControl(new BTextControl("pass", B_TRANSLATE("Password:"),
41 		"", NULL)),
42 	m_hidePasswordCheckBox(new BCheckBox("hide", B_TRANSLATE("Hide password "
43 		"text"), new BMessage(kHidePassword))),
44 	m_rememberCredentialsCheckBox(new BCheckBox("remember",
45 		B_TRANSLATE("Remember username and password for this site"), NULL)),
46 	m_okButton(new BButton("ok", B_TRANSLATE("OK"),
47 		new BMessage(kMsgPanelOK))),
48 	m_cancelButton(new BButton("cancel", B_TRANSLATE("Cancel"),
49 		new BMessage(B_QUIT_REQUESTED))),
50 	m_cancelled(false),
51 	m_exitSemaphore(create_sem(0, "Authentication Panel"))
52 {
53 }
54 
55 
56 AuthenticationPanel::~AuthenticationPanel()
57 {
58 	delete_sem(m_exitSemaphore);
59 }
60 
61 
62 bool
63 AuthenticationPanel::QuitRequested()
64 {
65 	m_cancelled = true;
66 	release_sem(m_exitSemaphore);
67 	return false;
68 }
69 
70 
71 void
72 AuthenticationPanel::MessageReceived(BMessage* message)
73 {
74 	switch (message->what) {
75 	case kMsgPanelOK:
76 		release_sem(m_exitSemaphore);
77 		break;
78 	case kHidePassword: {
79 		// TODO: Toggling this is broken in BTextView. Workaround is to
80 		// set the text and selection again.
81 		BString text = m_passwordTextControl->Text();
82 		int32 selectionStart;
83 		int32 selectionEnd;
84 		m_passwordTextControl->TextView()->GetSelection(&selectionStart,
85 			&selectionEnd);
86 		m_passwordTextControl->TextView()->HideTyping(
87 			m_hidePasswordCheckBox->Value() == B_CONTROL_ON);
88 		m_passwordTextControl->SetText(text.String());
89 		m_passwordTextControl->TextView()->Select(selectionStart,
90 			selectionEnd);
91 		break;
92 	}
93 	case kMsgJitter: {
94 		UpdateIfNeeded();
95 		BPoint leftTop = Frame().LeftTop();
96 		const float jitterOffsets[] = { -10, 0, 10, 0 };
97 		const int32 jitterOffsetCount = sizeof(jitterOffsets) / sizeof(float);
98 		for (int32 i = 0; i < 20; i++) {
99 			float offset = jitterOffsets[i % jitterOffsetCount];
100 			MoveTo(leftTop.x + offset, leftTop.y);
101 			snooze(15000);
102 		}
103 		MoveTo(leftTop);
104 		break;
105 	}
106 	default:
107 		BWindow::MessageReceived(message);
108 	}
109 }
110 
111 
112 bool AuthenticationPanel::getAuthentication(const BString& text,
113 	const BString& previousUser, const BString& previousPass,
114 	bool previousRememberCredentials, bool badPassword,
115 	BString& user, BString&  pass, bool* rememberCredentials)
116 {
117 	// Configure panel and layout controls.
118 	rgb_color infoColor = ui_color(B_PANEL_TEXT_COLOR);
119 	BRect textBounds(0, 0, 250, 200);
120 	BTextView* textView = new BTextView(textBounds, "text", textBounds,
121 		be_plain_font, &infoColor, B_FOLLOW_NONE, B_WILL_DRAW
122 			| B_SUPPORTS_LAYOUT);
123 	textView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
124 	textView->SetText(text.String());
125 	textView->MakeEditable(false);
126 	textView->MakeSelectable(false);
127 
128 	m_usernameTextControl->SetText(previousUser.String());
129 	m_passwordTextControl->TextView()->HideTyping(true);
130 	// Ignore the previous password, if it didn't work.
131 	if (!badPassword)
132 		m_passwordTextControl->SetText(previousPass.String());
133 	m_hidePasswordCheckBox->SetValue(B_CONTROL_ON);
134 	m_rememberCredentialsCheckBox->SetValue(previousRememberCredentials);
135 
136 	// create layout
137 	SetLayout(new BGroupLayout(B_VERTICAL, 0.0));
138 	float spacing = be_control_look->DefaultItemSpacing();
139 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0.0)
140 		.Add(BGridLayoutBuilder(0, spacing)
141 			.Add(textView, 0, 0, 2)
142 			.Add(m_usernameTextControl->CreateLabelLayoutItem(), 0, 1)
143 			.Add(m_usernameTextControl->CreateTextViewLayoutItem(), 1, 1)
144 			.Add(m_passwordTextControl->CreateLabelLayoutItem(), 0, 2)
145 			.Add(m_passwordTextControl->CreateTextViewLayoutItem(), 1, 2)
146 			.Add(BSpaceLayoutItem::CreateGlue(), 0, 3)
147 			.Add(m_hidePasswordCheckBox, 1, 3)
148 			.Add(m_rememberCredentialsCheckBox, 0, 4, 2)
149 			.SetInsets(spacing, spacing, spacing, spacing)
150 		)
151 		.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
152 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, spacing)
153 			.AddGlue()
154 			.Add(m_cancelButton)
155 			.Add(m_okButton)
156 			.SetInsets(spacing, spacing, spacing, spacing)
157 		)
158 	);
159 
160 	float textHeight = textView->LineHeight(0) * textView->CountLines();
161 	textView->SetExplicitMinSize(BSize(B_SIZE_UNSET, textHeight));
162 
163 	SetDefaultButton(m_okButton);
164 	if (badPassword && previousUser.Length())
165 		m_passwordTextControl->MakeFocus(true);
166 	else
167 		m_usernameTextControl->MakeFocus(true);
168 
169 	if (m_parentWindowFrame.IsValid())
170 		CenterIn(m_parentWindowFrame);
171 	else
172 		CenterOnScreen();
173 
174 	// Start AuthenticationPanel window thread
175 	Show();
176 
177 	// Let the window jitter, if the previous password was invalid
178 	if (badPassword)
179 		PostMessage(kMsgJitter);
180 
181 	// Block calling thread
182 	// Get the originating window, if it exists, to let it redraw itself.
183 	BWindow* window = dynamic_cast<BWindow*>
184 		(BLooper::LooperForThread(find_thread(NULL)));
185 	if (window) {
186 		status_t err;
187 		for (;;) {
188 			do {
189 				err = acquire_sem_etc(m_exitSemaphore, 1, B_RELATIVE_TIMEOUT,
190 					10000);
191 				// We've (probably) had our time slice taken away from us
192 			} while (err == B_INTERRUPTED);
193 
194 			if (err != B_TIMED_OUT) {
195 				// Semaphore was finally released or nuked.
196 				break;
197 			}
198 			window->UpdateIfNeeded();
199 		}
200 	} else {
201 		// No window to update, so just hang out until we're done.
202 		while (acquire_sem(m_exitSemaphore) == B_INTERRUPTED) {
203 		}
204 	}
205 
206 	// AuthenticationPanel wants to quit.
207 	Lock();
208 
209 	user = m_usernameTextControl->Text();
210 	pass = m_passwordTextControl->Text();
211 	if (rememberCredentials)
212 		*rememberCredentials = m_rememberCredentialsCheckBox->Value()
213 			== B_CONTROL_ON;
214 
215 	bool canceled = m_cancelled;
216 	Quit();
217 	// AuthenticationPanel object is TOAST here.
218 	return !canceled;
219 }
220