From 5eb61e485689dfd540b2ca4fbfe36809c4bcc6c3 Mon Sep 17 00:00:00 2001 From: stippi Date: Sun, 14 Feb 2010 20:31:05 +0000 Subject: [PATCH] Added AuthenticationPanel implementation. git-svn-id: http://svn.haiku-os.org/webpositive/webkit/trunk@70 94f232f2-1747-11df-bad5-a5bfde151594 --- src/apps/webpositive/AuthenticationPanel.cpp | 190 +++++++++++++++++++ src/apps/webpositive/AuthenticationPanel.h | 65 +++++++ src/apps/webpositive/LauncherApp.cpp | 2 + src/apps/webpositive/LauncherWindow.cpp | 24 ++- 4 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 src/apps/webpositive/AuthenticationPanel.cpp create mode 100644 src/apps/webpositive/AuthenticationPanel.h diff --git a/src/apps/webpositive/AuthenticationPanel.cpp b/src/apps/webpositive/AuthenticationPanel.cpp new file mode 100644 index 0000000000..b6ddb3bd3f --- /dev/null +++ b/src/apps/webpositive/AuthenticationPanel.cpp @@ -0,0 +1,190 @@ +/* + * Copyright (C) 2010 Stephan Aßmus + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "AuthenticationPanel.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const uint32 kMsgPanelOK = 'pnok'; +static const uint32 kMsgJitter = 'jitr'; +static const uint32 kHidePassword = 'hdpw'; + +AuthenticationPanel::AuthenticationPanel(BRect parentFrame) + : BWindow(BRect(-1000, -1000, -900, -900), "Authentication Required", + B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL, + B_ASYNCHRONOUS_CONTROLS | B_NOT_RESIZABLE | B_NOT_ZOOMABLE + | B_CLOSE_ON_ESCAPE | B_AUTO_UPDATE_SIZE_LIMITS) + , m_parentWindowFrame(parentFrame) + , m_usernameTextControl(new BTextControl("user", "Username:", "", NULL)) + , m_passwordTextControl(new BTextControl("pass", "Password:", "", NULL)) + , m_hidePasswordCheckBox(new BCheckBox("hide", "Hide password text", new BMessage(kHidePassword))) + , m_remberCredentialsCheckBox(new BCheckBox("remember", "Rember this username and password", NULL)) + , m_okButton(new BButton("ok", "OK", new BMessage(kMsgPanelOK))) + , m_cancelButton(new BButton("cancel", "Cancel", new BMessage(B_QUIT_REQUESTED))) + , m_cancelled(false) + , m_exitSemaphore(create_sem(0, "Authentication Panel")) +{ +} + +AuthenticationPanel::~AuthenticationPanel() +{ + delete_sem(m_exitSemaphore); +} + +bool +AuthenticationPanel::QuitRequested() +{ + m_cancelled = true; + release_sem(m_exitSemaphore); + return false; +} + +void +AuthenticationPanel::MessageReceived(BMessage* message) +{ + switch (message->what) { + case kMsgPanelOK: + release_sem(m_exitSemaphore); + break; + case kHidePassword: { + // TODO: Toggling this is broken in BTextView. Workaround is to + // set the text and selection again. + BString text = m_passwordTextControl->Text(); + int32 selectionStart; + int32 selectionEnd; + m_passwordTextControl->TextView()->GetSelection(&selectionStart, &selectionEnd); + m_passwordTextControl->TextView()->HideTyping( + m_hidePasswordCheckBox->Value() == B_CONTROL_ON); + m_passwordTextControl->SetText(text.String()); + m_passwordTextControl->TextView()->Select(selectionStart, selectionEnd); + break; + } + case kMsgJitter: { + UpdateIfNeeded(); + BPoint leftTop = Frame().LeftTop(); + const float jitterOffsets[] = { -10, 0, 10, 0 }; + const int32 jitterOffsetCount = sizeof(jitterOffsets) / sizeof(float); + for (int32 i = 0; i < 20; i++) { + float offset = jitterOffsets[i % jitterOffsetCount]; + MoveTo(leftTop.x + offset, leftTop.y); + snooze(15000); + } + MoveTo(leftTop); + break; + } + default: + BWindow::MessageReceived(message); + } +} + +bool AuthenticationPanel::getAuthentication(const BString& realm, + const BString& method, const BString& previousUser, const BString& previousPass, + bool previousRememberCredentials, bool badPassword, + BString& user, BString& pass, bool* rememberCredentials) +{ + // Configure panel and layout controls. + BString infoText("Enter login information for: "); + infoText << realm << "\n\n"; + infoText << "Authentication method: "; + infoText << method; + + m_usernameTextControl->SetText(previousUser.String()); + m_passwordTextControl->TextView()->HideTyping(true); + // Ignore the previous password, if it didn't work. + if (!badPassword) + m_passwordTextControl->SetText(previousPass.String()); + m_hidePasswordCheckBox->SetValue(B_CONTROL_ON); + m_remberCredentialsCheckBox->SetValue(previousRememberCredentials); + + // create layout + SetLayout(new BGroupLayout(B_VERTICAL)); + float spacing = be_control_look->DefaultItemSpacing(); + AddChild(BGroupLayoutBuilder(B_VERTICAL) + // TODO: Add text view here explaining realm and method... + .Add(BGridLayoutBuilder(0, spacing) + .Add(m_usernameTextControl->CreateLabelLayoutItem(), 0, 0) + .Add(m_usernameTextControl->CreateTextViewLayoutItem(), 1, 0) + .Add(m_passwordTextControl->CreateLabelLayoutItem(), 0, 1) + .Add(m_passwordTextControl->CreateTextViewLayoutItem(), 1, 1) + .Add(BSpaceLayoutItem::CreateGlue(), 0, 2) + .Add(m_hidePasswordCheckBox, 1, 2) + .Add(m_remberCredentialsCheckBox, 0, 3, 2) + .SetInsets(spacing, spacing, spacing, spacing) + ) + .Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER)) + .Add(BGroupLayoutBuilder(B_HORIZONTAL, spacing) + .AddGlue() + .Add(m_cancelButton) + .Add(m_okButton) + .SetInsets(spacing, spacing, spacing, spacing) + ) + ); + + SetDefaultButton(m_okButton); + m_usernameTextControl->MakeFocus(true); + + if (m_parentWindowFrame.IsValid()) + CenterIn(m_parentWindowFrame); + else + CenterOnScreen(); + + // Start AuthenticationPanel window thread + Show(); + + // Let the window jitter, if the previous password was invalid + if (badPassword) + PostMessage(kMsgJitter); + + // Block calling thread + // TODO: When called from other window threads, this should periodically + // call UpdateIfNeeded()... + acquire_sem(m_exitSemaphore); + + // AuthenticationPanel wants to quit. + Lock(); + + user = m_usernameTextControl->Text(); + pass = m_passwordTextControl->Text(); + if (rememberCredentials) + *rememberCredentials = m_remberCredentialsCheckBox->Value() == B_CONTROL_ON; + + bool canceled = m_cancelled; + Quit(); + // AuthenticationPanel object is TOAST here. + return !canceled; +} diff --git a/src/apps/webpositive/AuthenticationPanel.h b/src/apps/webpositive/AuthenticationPanel.h new file mode 100644 index 0000000000..0f8b6ab847 --- /dev/null +++ b/src/apps/webpositive/AuthenticationPanel.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2010 Stephan Aßmus + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef AuthenticationPanel_h +#define AuthenticationPanel_h + +#include +#include + +class BCheckBox; +class BTextControl; + +class AuthenticationPanel : public BWindow { +public: + AuthenticationPanel(BRect parentFrame = BRect()); + virtual ~AuthenticationPanel(); + + virtual bool QuitRequested(); + + virtual void MessageReceived(BMessage *message); + + bool getAuthentication(const BString& realm, const BString& method, + const BString& previousUser, const BString& previousPass, + bool previousRememberCredentials, bool badPassword, BString& user, + BString& pass, bool* rememberCredentials); + +private: + BRect m_parentWindowFrame; + BTextControl* m_usernameTextControl; + BTextControl* m_passwordTextControl; + BCheckBox* m_hidePasswordCheckBox; + BCheckBox* m_remberCredentialsCheckBox; + BButton* m_okButton; + BButton* m_cancelButton; + + bool m_cancelled; + + sem_id m_exitSemaphore; +}; + +#endif // AuthenticationPanel_h diff --git a/src/apps/webpositive/LauncherApp.cpp b/src/apps/webpositive/LauncherApp.cpp index 44baaf0210..67792d13d6 100644 --- a/src/apps/webpositive/LauncherApp.cpp +++ b/src/apps/webpositive/LauncherApp.cpp @@ -58,6 +58,8 @@ LauncherApp::LauncherApp() , m_initialized(false) , m_downloadWindow(0) { + // Since we will essentially run the GUI... + set_thread_priority(Thread(), B_DISPLAY_PRIORITY); } LauncherApp::~LauncherApp() diff --git a/src/apps/webpositive/LauncherWindow.cpp b/src/apps/webpositive/LauncherWindow.cpp index bc9c3970c4..08ebb14570 100644 --- a/src/apps/webpositive/LauncherWindow.cpp +++ b/src/apps/webpositive/LauncherWindow.cpp @@ -31,10 +31,10 @@ #include "config.h" #include "LauncherWindow.h" +#include "AuthenticationPanel.h" #include "WebProcess.h" #include "WebView.h" #include "WebViewConstants.h" - #include #include #include @@ -64,7 +64,9 @@ enum { TEXT_SHOW_FIND_GROUP = 'sfnd', TEXT_HIDE_FIND_GROUP = 'hfnd', TEXT_FIND_NEXT = 'fndn', - TEXT_FIND_PREVIOUS = 'fndp' + TEXT_FIND_PREVIOUS = 'fndp', + + TEST_AUTHENTICATION_PANEL = 'tapn' }; using namespace WebCore; @@ -199,6 +201,7 @@ LauncherWindow::LauncherWindow(BRect frame, const BMessenger& downloadListener, AddShortcut('G', B_COMMAND_KEY | B_SHIFT_KEY, new BMessage(TEXT_FIND_PREVIOUS)); AddShortcut('F', B_COMMAND_KEY, new BMessage(TEXT_SHOW_FIND_GROUP)); AddShortcut('F', B_COMMAND_KEY | B_SHIFT_KEY, new BMessage(TEXT_HIDE_FIND_GROUP)); + AddShortcut('T', B_COMMAND_KEY, new BMessage(TEST_AUTHENTICATION_PANEL)); navigationCapabilitiesChanged(false, false, false); @@ -259,6 +262,23 @@ void LauncherWindow::MessageReceived(BMessage* message) be_app->PostMessage(message); break; + case TEST_AUTHENTICATION_PANEL: { + BString realm("Realm"); + BString method("Method"); + BString previousUser; + BString previousPass; + bool rememberCredentials = false; + bool badPassword = true; + BString user; + BString pass; + AuthenticationPanel* panel = new AuthenticationPanel(Frame()); + bool success = panel->getAuthentication(realm, method, previousUser, + previousPass, rememberCredentials, badPassword, user, pass, + &rememberCredentials); + printf("success: %d\n", success); + break; + } + default: WebViewWindow::MessageReceived(message); break;