OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/crypto_module_password_dialog_view.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "googleurl/src/gurl.h" |
| 11 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/views/controls/label.h" |
| 14 #include "ui/views/controls/button/text_button.h" |
| 15 #include "ui/views/controls/textfield/textfield.h" |
| 16 #include "ui/views/layout/grid_layout.h" |
| 17 #include "ui/views/layout/layout_constants.h" |
| 18 |
| 19 int kInputPasswordMinWidth = 8; |
| 20 |
| 21 namespace browser { |
| 22 // CryptoModulePasswordDialogView |
| 23 //////////////////////////////////////////////////////////////////////////////// |
| 24 CryptoModulePasswordDialogView::CryptoModulePasswordDialogView( |
| 25 const std::string& slot_name, |
| 26 browser::CryptoModulePasswordReason reason, |
| 27 const std::string& server, |
| 28 const base::Callback<void(const char*)>& callback) |
| 29 : callback_(callback) { |
| 30 Init(server, slot_name, reason); |
| 31 } |
| 32 |
| 33 void CryptoModulePasswordDialogView::Init( |
| 34 const std::string& server, |
| 35 const std::string& slot_name, |
| 36 browser::CryptoModulePasswordReason reason) { |
| 37 // Select an appropriate text for the reason. |
| 38 std::string text; |
| 39 const string16& server16 = UTF8ToUTF16(server); |
| 40 const string16& slot16 = UTF8ToUTF16(slot_name); |
| 41 switch (reason) { |
| 42 case browser::kCryptoModulePasswordKeygen: |
| 43 text = l10n_util::GetStringFUTF8( |
| 44 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_KEYGEN, slot16, server16); |
| 45 break; |
| 46 case browser::kCryptoModulePasswordCertEnrollment: |
| 47 text = l10n_util::GetStringFUTF8( |
| 48 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_ENROLLMENT, slot16, server16); |
| 49 break; |
| 50 case browser::kCryptoModulePasswordClientAuth: |
| 51 text = l10n_util::GetStringFUTF8( |
| 52 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CLIENT_AUTH, slot16, server16); |
| 53 break; |
| 54 case browser::kCryptoModulePasswordListCerts: |
| 55 text = l10n_util::GetStringFUTF8( |
| 56 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_LIST_CERTS, slot16); |
| 57 break; |
| 58 case browser::kCryptoModulePasswordCertImport: |
| 59 text = l10n_util::GetStringFUTF8( |
| 60 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_IMPORT, slot16); |
| 61 break; |
| 62 case browser::kCryptoModulePasswordCertExport: |
| 63 text = l10n_util::GetStringFUTF8( |
| 64 IDS_CRYPTO_MODULE_AUTH_DIALOG_TEXT_CERT_EXPORT, slot16); |
| 65 break; |
| 66 default: |
| 67 NOTREACHED(); |
| 68 } |
| 69 reason_label_ = new views::Label(UTF8ToUTF16(text)); |
| 70 reason_label_->SetMultiLine(true); |
| 71 |
| 72 password_label_ = new views::Label(l10n_util::GetStringUTF16( |
| 73 IDS_CRYPTO_MODULE_AUTH_DIALOG_PASSWORD_FIELD)); |
| 74 |
| 75 password_entry_ = new views::Textfield(views::Textfield::STYLE_PASSWORD); |
| 76 password_entry_->SetController(this); |
| 77 |
| 78 views::GridLayout* layout = views::GridLayout::CreatePanel(this); |
| 79 SetLayoutManager(layout); |
| 80 |
| 81 views::ColumnSet* reason_column_set = layout->AddColumnSet(0); |
| 82 reason_column_set->AddColumn( |
| 83 views::GridLayout::LEADING, views::GridLayout::LEADING, 1, |
| 84 views::GridLayout::USE_PREF, 0, 0); |
| 85 |
| 86 views::ColumnSet* column_set = layout->AddColumnSet(1); |
| 87 column_set->AddColumn(views::GridLayout::LEADING, |
| 88 views::GridLayout::LEADING, 0, |
| 89 views::GridLayout::USE_PREF, 0, 0); |
| 90 column_set->AddPaddingColumn( |
| 91 0, views::kUnrelatedControlLargeHorizontalSpacing); |
| 92 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 93 views::GridLayout::USE_PREF, 0, 0); |
| 94 |
| 95 layout->StartRow(0, 0); |
| 96 layout->AddView(reason_label_); |
| 97 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| 98 |
| 99 layout->StartRow(0, 1); |
| 100 layout->AddView(password_label_); |
| 101 layout->AddView(password_entry_); |
| 102 } |
| 103 |
| 104 views::View* CryptoModulePasswordDialogView::GetInitiallyFocusedView() { |
| 105 return password_entry_; |
| 106 } |
| 107 bool CryptoModulePasswordDialogView::IsModal() const { |
| 108 return true; |
| 109 } |
| 110 views::View* CryptoModulePasswordDialogView::GetContentsView() { |
| 111 return this; |
| 112 } |
| 113 |
| 114 string16 CryptoModulePasswordDialogView::GetDialogButtonLabel( |
| 115 ui::DialogButton button) const { |
| 116 if (button == ui::DIALOG_BUTTON_OK) |
| 117 return UTF8ToUTF16(l10n_util::GetStringUTF8( |
| 118 IDS_CRYPTO_MODULE_AUTH_DIALOG_OK_BUTTON_LABEL)); |
| 119 else if (button == ui::DIALOG_BUTTON_CANCEL) |
| 120 return UTF8ToUTF16(l10n_util::GetStringUTF8(IDS_CANCEL)); |
| 121 const string16 empty; |
| 122 return empty; |
| 123 } |
| 124 |
| 125 bool CryptoModulePasswordDialogView::Accept() { |
| 126 callback_.Run(UTF16ToUTF8(password_entry_->text()).c_str()); |
| 127 const string16 empty; |
| 128 password_entry_->SetText(empty); |
| 129 return true; |
| 130 } |
| 131 |
| 132 bool CryptoModulePasswordDialogView::Cancel() { |
| 133 callback_.Run(static_cast<const char*>(NULL)); |
| 134 const string16 empty; |
| 135 password_entry_->SetText(empty); |
| 136 return true; |
| 137 } |
| 138 |
| 139 bool CryptoModulePasswordDialogView::HandleKeyEvent( |
| 140 views::Textfield* sender, |
| 141 const views::KeyEvent& keystroke) { |
| 142 return false; |
| 143 } |
| 144 |
| 145 string16 CryptoModulePasswordDialogView::GetWindowTitle() const { |
| 146 return UTF8ToUTF16(l10n_util::GetStringUTF8( |
| 147 IDS_CRYPTO_MODULE_AUTH_DIALOG_TITLE)); |
| 148 } |
| 149 } // namespace browser |
OLD | NEW |