summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/first_run_customize_view.cc
blob: 7a378efda85364ec8a0605ceeac16a0e52700adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/views/first_run_customize_view.h"

#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "chrome/browser/importer/importer.h"
#include "chrome/browser/first_run.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "grit/theme_resources.h"
#include "views/controls/button/checkbox.h"
#include "views/controls/combo_box.h"
#include "views/controls/image_view.h"
#include "views/controls/label.h"
#include "views/controls/throbber.h"
#include "views/standard_layout.h"
#include "views/window/window.h"

FirstRunCustomizeView::FirstRunCustomizeView(Profile* profile,
                                             ImporterHost* importer_host,
                                             CustomizeViewObserver* observer,
                                             bool default_browser_checked)
    : FirstRunViewBase(profile),
      main_label_(NULL),
      import_cbox_(NULL),
      import_from_combo_(NULL),
      shortcuts_label_(NULL),
      desktop_shortcut_cbox_(NULL),
      quick_shortcut_cbox_(NULL),
      customize_observer_(observer) {
  importer_host_ = importer_host;
  DCHECK(importer_host_);
  SetupControls();

  // The checkbox for Default Browser should be the same for FirstRun and
  // the customize view, so that the user selection isn't lost when you uncheck
  // and then open the Customize dialog. Therefore, we propagate the selection
  // status of the default browser here.
  default_browser_->SetChecked(default_browser_checked);
}

FirstRunCustomizeView::~FirstRunCustomizeView() {
}

views::Checkbox* FirstRunCustomizeView::MakeCheckBox(int label_id) {
  views::Checkbox* cbox = new views::Checkbox(l10n_util::GetString(label_id));
  cbox->set_listener(this);
  AddChildView(cbox);
  return cbox;
}

void FirstRunCustomizeView::SetupControls() {
  using views::Label;
  using views::Checkbox;

  main_label_ = new Label(l10n_util::GetString(IDS_FR_CUSTOMIZE_DLG_TEXT));
  main_label_->SetMultiLine(true);
  main_label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
  AddChildView(main_label_);

  import_cbox_ = MakeCheckBox(IDS_FR_CUSTOMIZE_IMPORT);

  import_from_combo_ = new views::ComboBox(this);
  AddChildView(import_from_combo_);

  shortcuts_label_ =
      new Label(l10n_util::GetString(IDS_FR_CUSTOMIZE_SHORTCUTS));
  shortcuts_label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
  AddChildView(shortcuts_label_);

  // The two check boxes for the different shortcut creation.
  desktop_shortcut_cbox_ = MakeCheckBox(IDS_FR_CUSTOM_SHORTCUT_DESKTOP);
  desktop_shortcut_cbox_->SetChecked(true);

  quick_shortcut_cbox_ = MakeCheckBox(IDS_FR_CUSTOM_SHORTCUT_QUICKL);
  quick_shortcut_cbox_->SetChecked(true);
}

gfx::Size FirstRunCustomizeView::GetPreferredSize() {
  return gfx::Size(views::Window::GetLocalizedContentsSize(
      IDS_FIRSTRUNCUSTOMIZE_DIALOG_WIDTH_CHARS,
      IDS_FIRSTRUNCUSTOMIZE_DIALOG_HEIGHT_LINES));
}

void FirstRunCustomizeView::Layout() {
  FirstRunViewBase::Layout();

  const int kVertSpacing = 8;
  const int kComboExtraPad = 8;

  gfx::Size canvas = GetPreferredSize();

  // Welcome label goes in to to the left. It does not go across the
  // entire window because the background gets busy on the right.
  gfx::Size pref_size = main_label_->GetPreferredSize();
  main_label_->SetBounds(kPanelHorizMargin, kPanelVertMargin,
                         canvas.width() - pref_size.width(),
                         pref_size.height());
  AdjustDialogWidth(main_label_);

  int next_v_space = background_image()->y() +
                     background_image()->height() + kPanelVertMargin;

  pref_size = import_cbox_->GetPreferredSize();
  import_cbox_->SetBounds(kPanelHorizMargin, next_v_space,
                               pref_size.width(), pref_size.height());

  import_cbox_->SetChecked(true);

  int x_offset = import_cbox_->x() +
                 import_cbox_->width();

  pref_size = import_from_combo_->GetPreferredSize();
  import_from_combo_->SetBounds(x_offset,
                                next_v_space +
                                    (import_cbox_->height() -
                                     pref_size.height()) / 2,
                                pref_size.width() + kComboExtraPad,
                                pref_size.height());

  AdjustDialogWidth(import_from_combo_);

  next_v_space = import_cbox_->y() + import_cbox_->height() +
                 kUnrelatedControlVerticalSpacing;

  pref_size = shortcuts_label_->GetPreferredSize();
  shortcuts_label_->SetBounds(kPanelHorizMargin, next_v_space,
                              pref_size.width(), pref_size.height());

  AdjustDialogWidth(shortcuts_label_);

  next_v_space += shortcuts_label_->height() +
                  kRelatedControlVerticalSpacing;

  pref_size = desktop_shortcut_cbox_->GetPreferredSize();
  desktop_shortcut_cbox_->SetBounds(kPanelHorizMargin, next_v_space,
                                    pref_size.width(), pref_size.height());

  AdjustDialogWidth(desktop_shortcut_cbox_);

  next_v_space += desktop_shortcut_cbox_->height() +
                  kRelatedControlVerticalSpacing;

  pref_size = quick_shortcut_cbox_->GetPreferredSize();
  quick_shortcut_cbox_->SetBounds(kPanelHorizMargin, next_v_space,
                                  pref_size.width(), pref_size.height());

  AdjustDialogWidth(quick_shortcut_cbox_);
}

void FirstRunCustomizeView::ButtonPressed(views::Button* sender) {
  if (import_cbox_ == sender) {
    // Disable the import combobox if the user unchecks the checkbox.
    import_from_combo_->SetEnabled(import_cbox_->checked());
  }
}

int FirstRunCustomizeView::GetItemCount(views::ComboBox* source) {
  return importer_host_->GetAvailableProfileCount();
}

std::wstring FirstRunCustomizeView::GetItemAt(views::ComboBox* source,
                                              int index) {
  return importer_host_->GetSourceProfileNameAt(index);
}

std::wstring FirstRunCustomizeView::GetWindowTitle() const {
  return l10n_util::GetString(IDS_FR_CUSTOMIZE_DLG_TITLE);
}

views::View* FirstRunCustomizeView::GetContentsView() {
  return this;
}

bool FirstRunCustomizeView::Accept() {
  if (!IsDialogButtonEnabled(MessageBoxFlags::DIALOGBUTTON_OK))
    return false;

  DisableButtons();
  import_cbox_->SetEnabled(false);
  import_from_combo_->SetEnabled(false);
  desktop_shortcut_cbox_->SetEnabled(false);
  quick_shortcut_cbox_->SetEnabled(false);

  if (desktop_shortcut_cbox_->checked()) {
    UserMetrics::RecordAction(L"FirstRunCustom_Do_DesktopShortcut", profile_);
    CreateDesktopShortcut();
  }
  if (quick_shortcut_cbox_->checked()) {
    UserMetrics::RecordAction(L"FirstRunCustom_Do_QuickLShortcut", profile_);
    CreateQuickLaunchShortcut();
  }
  if (!import_cbox_->checked()) {
    UserMetrics::RecordAction(L"FirstRunCustom_No_Import", profile_);
  } else {
    int browser_selected = import_from_combo_->GetSelectedItem();
    FirstRun::ImportSettings(profile_,
        importer_host_->GetSourceProfileInfoAt(browser_selected).browser_type,
        GetDefaultImportItems(), window()->GetNativeWindow());
  }
  if (default_browser_->checked())
    SetDefaultBrowser();

  if (customize_observer_)
    customize_observer_->CustomizeAccepted();

  // Exit the message loop we were started with so that startup can continue.
  MessageLoop::current()->Quit();

  return true;
}

bool FirstRunCustomizeView::Cancel() {
  if (customize_observer_)
    customize_observer_->CustomizeCanceled();

  // Don't quit the message loop in this case - we're still showing the main
  // First run dialog box underneath ourselves.

  return true;
}