1 /* 2 * Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de> 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "AuthenticationPanel.h" 29 30 #include <Button.h> 31 #include <Catalog.h> 32 #include <CheckBox.h> 33 #include <ControlLook.h> 34 #include <GridLayoutBuilder.h> 35 #include <GroupLayoutBuilder.h> 36 #include <Locale.h> 37 #include <Message.h> 38 #include <Screen.h> 39 #include <SeparatorView.h> 40 #include <SpaceLayoutItem.h> 41 #include <StringView.h> 42 #include <TextControl.h> 43 #include <stdio.h> 44 45 static const uint32 kMsgPanelOK = 'pnok'; 46 static const uint32 kMsgJitter = 'jitr'; 47 static const uint32 kHidePassword = 'hdpw'; 48 49 50 #undef B_TRANSLATION_CONTEXT 51 #define B_TRANSLATION_CONTEXT "Authentication Panel" 52 53 AuthenticationPanel::AuthenticationPanel(BRect parentFrame) 54 : 55 BWindow(BRect(-1000, -1000, -900, -900), 56 B_TRANSLATE("Authentication required"), B_TITLED_WINDOW_LOOK, 57 B_MODAL_APP_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS | B_NOT_RESIZABLE 58 | B_NOT_ZOOMABLE | B_CLOSE_ON_ESCAPE | B_AUTO_UPDATE_SIZE_LIMITS), 59 m_parentWindowFrame(parentFrame), 60 m_usernameTextControl(new BTextControl("user", B_TRANSLATE("Username:"), 61 "", NULL)), 62 m_passwordTextControl(new BTextControl("pass", B_TRANSLATE("Password:"), 63 "", NULL)), 64 m_hidePasswordCheckBox(new BCheckBox("hide", B_TRANSLATE("Hide password " 65 "text"), new BMessage(kHidePassword))), 66 m_rememberCredentialsCheckBox(new BCheckBox("remember", 67 B_TRANSLATE("Remember username and password for this site"), NULL)), 68 m_okButton(new BButton("ok", B_TRANSLATE("OK"), 69 new BMessage(kMsgPanelOK))), 70 m_cancelButton(new BButton("cancel", B_TRANSLATE("Cancel"), 71 new BMessage(B_QUIT_REQUESTED))), 72 m_cancelled(false), 73 m_exitSemaphore(create_sem(0, "Authentication Panel")) 74 { 75 } 76 77 78 AuthenticationPanel::~AuthenticationPanel() 79 { 80 delete_sem(m_exitSemaphore); 81 } 82 83 84 bool 85 AuthenticationPanel::QuitRequested() 86 { 87 m_cancelled = true; 88 release_sem(m_exitSemaphore); 89 return false; 90 } 91 92 93 void 94 AuthenticationPanel::MessageReceived(BMessage* message) 95 { 96 switch (message->what) { 97 case kMsgPanelOK: 98 release_sem(m_exitSemaphore); 99 break; 100 case kHidePassword: { 101 // TODO: Toggling this is broken in BTextView. Workaround is to 102 // set the text and selection again. 103 BString text = m_passwordTextControl->Text(); 104 int32 selectionStart; 105 int32 selectionEnd; 106 m_passwordTextControl->TextView()->GetSelection(&selectionStart, 107 &selectionEnd); 108 m_passwordTextControl->TextView()->HideTyping( 109 m_hidePasswordCheckBox->Value() == B_CONTROL_ON); 110 m_passwordTextControl->SetText(text.String()); 111 m_passwordTextControl->TextView()->Select(selectionStart, 112 selectionEnd); 113 break; 114 } 115 case kMsgJitter: { 116 UpdateIfNeeded(); 117 BPoint leftTop = Frame().LeftTop(); 118 const float jitterOffsets[] = { -10, 0, 10, 0 }; 119 const int32 jitterOffsetCount = sizeof(jitterOffsets) / sizeof(float); 120 for (int32 i = 0; i < 20; i++) { 121 float offset = jitterOffsets[i % jitterOffsetCount]; 122 MoveTo(leftTop.x + offset, leftTop.y); 123 snooze(15000); 124 } 125 MoveTo(leftTop); 126 break; 127 } 128 default: 129 BWindow::MessageReceived(message); 130 } 131 } 132 133 134 bool AuthenticationPanel::getAuthentication(const BString& text, 135 const BString& previousUser, const BString& previousPass, 136 bool previousRememberCredentials, bool badPassword, 137 BString& user, BString& pass, bool* rememberCredentials) 138 { 139 // Configure panel and layout controls. 140 rgb_color infoColor = ui_color(B_PANEL_TEXT_COLOR); 141 BRect textBounds(0, 0, 250, 200); 142 BTextView* textView = new BTextView(textBounds, "text", textBounds, 143 be_plain_font, &infoColor, B_FOLLOW_NONE, B_WILL_DRAW 144 | B_SUPPORTS_LAYOUT); 145 textView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 146 textView->SetText(text.String()); 147 textView->MakeEditable(false); 148 textView->MakeSelectable(false); 149 150 m_usernameTextControl->SetText(previousUser.String()); 151 m_passwordTextControl->TextView()->HideTyping(true); 152 // Ignore the previous password, if it didn't work. 153 if (!badPassword) 154 m_passwordTextControl->SetText(previousPass.String()); 155 m_hidePasswordCheckBox->SetValue(B_CONTROL_ON); 156 m_rememberCredentialsCheckBox->SetValue(previousRememberCredentials); 157 158 // create layout 159 SetLayout(new BGroupLayout(B_VERTICAL, 0.0)); 160 float spacing = be_control_look->DefaultItemSpacing(); 161 AddChild(BGroupLayoutBuilder(B_VERTICAL, 0.0) 162 .Add(BGridLayoutBuilder(0, spacing) 163 .Add(textView, 0, 0, 2) 164 .Add(m_usernameTextControl->CreateLabelLayoutItem(), 0, 1) 165 .Add(m_usernameTextControl->CreateTextViewLayoutItem(), 1, 1) 166 .Add(m_passwordTextControl->CreateLabelLayoutItem(), 0, 2) 167 .Add(m_passwordTextControl->CreateTextViewLayoutItem(), 1, 2) 168 .Add(BSpaceLayoutItem::CreateGlue(), 0, 3) 169 .Add(m_hidePasswordCheckBox, 1, 3) 170 .Add(m_rememberCredentialsCheckBox, 0, 4, 2) 171 .SetInsets(spacing, spacing, spacing, spacing) 172 ) 173 .Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER)) 174 .Add(BGroupLayoutBuilder(B_HORIZONTAL, spacing) 175 .AddGlue() 176 .Add(m_cancelButton) 177 .Add(m_okButton) 178 .SetInsets(spacing, spacing, spacing, spacing) 179 ) 180 ); 181 182 float textHeight = textView->LineHeight(0) * textView->CountLines(); 183 textView->SetExplicitMinSize(BSize(B_SIZE_UNSET, textHeight)); 184 185 SetDefaultButton(m_okButton); 186 if (badPassword && previousUser.Length()) 187 m_passwordTextControl->MakeFocus(true); 188 else 189 m_usernameTextControl->MakeFocus(true); 190 191 if (m_parentWindowFrame.IsValid()) 192 CenterIn(m_parentWindowFrame); 193 else 194 CenterOnScreen(); 195 196 // Start AuthenticationPanel window thread 197 Show(); 198 199 // Let the window jitter, if the previous password was invalid 200 if (badPassword) 201 PostMessage(kMsgJitter); 202 203 // Block calling thread 204 // Get the originating window, if it exists, to let it redraw itself. 205 BWindow* window = dynamic_cast<BWindow*> 206 (BLooper::LooperForThread(find_thread(NULL))); 207 if (window) { 208 status_t err; 209 for (;;) { 210 do { 211 err = acquire_sem_etc(m_exitSemaphore, 1, B_RELATIVE_TIMEOUT, 212 10000); 213 // We've (probably) had our time slice taken away from us 214 } while (err == B_INTERRUPTED); 215 216 if (err != B_TIMED_OUT) { 217 // Semaphore was finally released or nuked. 218 break; 219 } 220 window->UpdateIfNeeded(); 221 } 222 } else { 223 // No window to update, so just hang out until we're done. 224 while (acquire_sem(m_exitSemaphore) == B_INTERRUPTED) { 225 } 226 } 227 228 // AuthenticationPanel wants to quit. 229 Lock(); 230 231 user = m_usernameTextControl->Text(); 232 pass = m_passwordTextControl->Text(); 233 if (rememberCredentials) 234 *rememberCredentials = m_rememberCredentialsCheckBox->Value() 235 == B_CONTROL_ON; 236 237 bool canceled = m_cancelled; 238 Quit(); 239 // AuthenticationPanel object is TOAST here. 240 return !canceled; 241 } 242