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/chromeos/login/password_changed_view.h"
6
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/chromeos/login/rounded_rect_painter.h"
9#include "chrome/browser/chromeos/login/textfield_with_margin.h"
10#include "chrome/browser/chromeos/login/wizard_accessibility_helper.h"
11#include "grit/generated_resources.h"
12#include "grit/locale_settings.h"
13#include "ui/base/keycodes/keyboard_codes.h"
14#include "ui/base/l10n/l10n_util.h"
15#include "ui/base/resource/resource_bundle.h"
16#include "views/controls/button/radio_button.h"
17#include "views/controls/label.h"
18#include "views/controls/textfield/textfield.h"
19#include "views/layout/grid_layout.h"
20#include "views/layout/layout_constants.h"
21#include "views/window/window.h"
22
23using views::Button;
24using views::GridLayout;
25using views::Label;
26using views::RadioButton;
27using views::Textfield;
28
29namespace chromeos {
30
31namespace {
32const int kPasswordFieldWidthChars = 20;
33}  // namespace
34
35PasswordChangedView::PasswordChangedView(Delegate* delegate,
36                                         bool full_sync_disabled)
37    : title_label_(NULL),
38      description_label_(NULL),
39      full_sync_radio_(NULL),
40      delta_sync_radio_(NULL),
41      old_password_field_(NULL),
42      delegate_(delegate),
43      full_sync_disabled_(full_sync_disabled) {
44}
45
46bool PasswordChangedView::Accept() {
47  return ExitDialog();
48}
49
50int PasswordChangedView::GetDialogButtons() const {
51 return MessageBoxFlags::DIALOGBUTTON_OK;
52}
53
54views::View* PasswordChangedView::GetInitiallyFocusedView() {
55  if (!full_sync_disabled_) {
56    return views::DialogDelegate::GetInitiallyFocusedView();
57  } else {
58    DCHECK(old_password_field_);
59    return old_password_field_;
60  }
61}
62
63std::wstring PasswordChangedView::GetWindowTitle() const {
64  return UTF16ToWide(
65      l10n_util::GetStringUTF16(IDS_LOGIN_PASSWORD_CHANGED_DIALOG_BOX_TITLE));
66}
67
68gfx::Size PasswordChangedView::GetPreferredSize() {
69  // TODO(nkostylev): Once UI is finalized, create locale settings.
70  return gfx::Size(views::Window::GetLocalizedContentsSize(
71      IDS_PASSWORD_CHANGED_DIALOG_WIDTH_CHARS,
72      IDS_PASSWORD_CHANGED_DIALOG_HEIGHT_LINES));
73}
74
75void PasswordChangedView::ViewHierarchyChanged(bool is_add,
76                                               views::View* parent,
77                                               views::View* child) {
78  if (is_add && child == this) {
79    Init();
80  }
81}
82
83void PasswordChangedView::Init() {
84  // Set up fonts.
85  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
86  gfx::Font title_font = rb.GetFont(ResourceBundle::MediumBoldFont);
87
88  // Create controls
89  title_label_ = new Label();
90  title_label_->SetFont(title_font);
91  title_label_->SetText(
92      UTF16ToWide(l10n_util::GetStringUTF16(IDS_LOGIN_PASSWORD_CHANGED_TITLE)));
93  title_label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
94
95  description_label_ = new Label();
96  description_label_->SetText(
97      UTF16ToWide(l10n_util::GetStringUTF16(IDS_LOGIN_PASSWORD_CHANGED_DESC)));
98  description_label_->SetMultiLine(true);
99  description_label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
100
101  full_sync_radio_ = new RadioButton(
102      UTF16ToWide(l10n_util::GetStringUTF16(IDS_LOGIN_PASSWORD_CHANGED_RESET)),
103      0);
104  full_sync_radio_->set_listener(this);
105  full_sync_radio_->SetMultiLine(true);
106
107  delta_sync_radio_ = new RadioButton(
108      UTF16ToWide(
109          l10n_util::GetStringUTF16(IDS_LOGIN_PASSWORD_CHANGED_MIGRATE)),
110      0);
111  delta_sync_radio_->set_listener(this);
112  delta_sync_radio_->SetMultiLine(true);
113
114  old_password_field_ = new TextfieldWithMargin(Textfield::STYLE_PASSWORD);
115  old_password_field_->set_text_to_display_when_empty(
116      l10n_util::GetStringUTF16(IDS_LOGIN_PREVIOUS_PASSWORD));
117  old_password_field_->set_default_width_in_chars(kPasswordFieldWidthChars);
118  old_password_field_->SetController(this);
119
120  // Define controls layout.
121  GridLayout* layout = GridLayout::CreatePanel(this);
122  SetLayoutManager(layout);
123
124  views::ColumnSet* column_set = layout->AddColumnSet(0);
125  column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
126                        GridLayout::USE_PREF, 0, 0);
127  column_set = layout->AddColumnSet(1);
128  column_set->AddPaddingColumn(
129      0, views::kUnrelatedControlLargeHorizontalSpacing);
130  column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
131                        GridLayout::USE_PREF, 0, 0);
132
133  layout->StartRow(0, 0);
134  layout->AddView(title_label_);
135  layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
136
137  layout->StartRow(0, 0);
138  layout->AddView(description_label_);
139  layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
140
141  layout->StartRow(0, 0);
142  layout->AddView(full_sync_radio_);
143  layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
144
145  layout->StartRow(0, 0);
146  layout->AddView(delta_sync_radio_);
147  layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
148
149  layout->StartRow(0, 1);
150  layout->AddView(
151      old_password_field_, 1, 1, GridLayout::LEADING, GridLayout::CENTER);
152  layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
153
154  layout->StartRow(0, 0);
155  layout->AddView(old_password_field_);
156
157  // Disable options if needed.
158  if (!full_sync_disabled_) {
159    full_sync_radio_->SetChecked(true);
160    old_password_field_->SetEnabled(false);
161  } else {
162    full_sync_radio_->SetEnabled(false);
163    delta_sync_radio_->SetChecked(true);
164    old_password_field_->SetEnabled(true);
165  }
166
167}
168
169bool PasswordChangedView::ExitDialog() {
170  if (delta_sync_radio_->checked() && old_password_field_->text().empty())
171    return false;
172
173  // TODO(nkostylev): Need to sanitize memory used to store password.
174  if (full_sync_radio_->checked())
175    delegate_->ResyncEncryptedData();
176  else
177    delegate_->RecoverEncryptedData(UTF16ToUTF8(old_password_field_->text()));
178
179  return true;
180}
181
182void PasswordChangedView::ButtonPressed(Button* sender,
183                                        const views::Event& event) {
184  if (sender == full_sync_radio_) {
185    old_password_field_->SetEnabled(false);
186    old_password_field_->SetText(string16());
187  } else if (sender == delta_sync_radio_) {
188    old_password_field_->SetEnabled(true);
189    old_password_field_->RequestFocus();
190  }
191}
192
193}  // namespace chromeos
194