summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/importing_progress_view.cc
blob: db4ad1f579e55ed77a4b3a70c841c732a38c4cd7 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// 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/importing_progress_view.h"

#include "chrome/app/locales/locale_settings.h"
#include "chrome/browser/standard_layout.h"
#include "chrome/common/l10n_util.h"
#include "chrome/views/grid_layout.h"
#include "chrome/views/label.h"
#include "chrome/views/throbber.h"
#include "chrome/views/window.h"
#include "generated_resources.h"

////////////////////////////////////////////////////////////////////////////////
// ImportingProgressView, public:

ImportingProgressView::ImportingProgressView(const std::wstring& source_name,
                                             int16 items,
                                             ImporterHost* coordinator,
                                             ImportObserver* observer,
                                             HWND parent_window)
    : state_bookmarks_(new ChromeViews::CheckmarkThrobber),
      state_searches_(new ChromeViews::CheckmarkThrobber),
      state_passwords_(new ChromeViews::CheckmarkThrobber),
      state_history_(new ChromeViews::CheckmarkThrobber),
      state_cookies_(new ChromeViews::CheckmarkThrobber),
      label_info_(new ChromeViews::Label(l10n_util::GetStringF(
          IDS_IMPORT_PROGRESS_INFO, source_name))),
      label_bookmarks_(new ChromeViews::Label(
          l10n_util::GetString(IDS_IMPORT_PROGRESS_STATUS_BOOKMARKS))),
      label_searches_(new ChromeViews::Label(
          l10n_util::GetString(IDS_IMPORT_PROGRESS_STATUS_SEARCH))),
      label_passwords_(new ChromeViews::Label(
          l10n_util::GetString(IDS_IMPORT_PROGRESS_STATUS_PASSWORDS))),
      label_history_(new ChromeViews::Label(
          l10n_util::GetString(IDS_IMPORT_PROGRESS_STATUS_HISTORY))),
      label_cookies_(new ChromeViews::Label(
          l10n_util::GetString(IDS_IMPORT_PROGRESS_STATUS_COOKIES))),
      parent_window_(parent_window),
      coordinator_(coordinator),
      import_observer_(observer),
      items_(items),
      importing_(true) {
  coordinator_->SetObserver(this);
  label_info_->SetMultiLine(true);
  label_info_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  label_bookmarks_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  label_searches_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  label_passwords_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  label_history_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
  label_cookies_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);

  // These are scoped pointers, so we don't need the parent to delete them.
  state_bookmarks_->SetParentOwned(false);
  state_searches_->SetParentOwned(false);
  state_passwords_->SetParentOwned(false);
  state_history_->SetParentOwned(false);
  state_cookies_->SetParentOwned(false);
  label_bookmarks_->SetParentOwned(false);
  label_searches_->SetParentOwned(false);
  label_passwords_->SetParentOwned(false);
  label_history_->SetParentOwned(false);
  label_cookies_->SetParentOwned(false);
}

ImportingProgressView::~ImportingProgressView() {
  RemoveChildView(state_bookmarks_.get());
  RemoveChildView(state_searches_.get());
  RemoveChildView(state_passwords_.get());
  RemoveChildView(state_history_.get());
  RemoveChildView(state_cookies_.get());
  RemoveChildView(label_bookmarks_.get());
  RemoveChildView(label_searches_.get());
  RemoveChildView(label_passwords_.get());
  RemoveChildView(label_history_.get());
  RemoveChildView(label_cookies_.get());
}

////////////////////////////////////////////////////////////////////////////////
// ImportingProgressView, ImporterObserver implementation:

void ImportingProgressView::ImportItemStarted(ImportItem item) {
  DCHECK(items_ & item);
  switch (item) {
    case FAVORITES:
      state_bookmarks_->Start();
      break;
    case SEARCH_ENGINES:
      state_searches_->Start();
      break;
    case PASSWORDS:
      state_passwords_->Start();
      break;
    case HISTORY:
      state_history_->Start();
      break;
    case COOKIES:
      state_cookies_->Start();
      break;
  }
}

void ImportingProgressView::ImportItemEnded(ImportItem item) {
  DCHECK(items_ & item);
  switch (item) {
    case FAVORITES:
      state_bookmarks_->Stop();
      state_bookmarks_->SetChecked(true);
      break;
    case SEARCH_ENGINES:
      state_searches_->Stop();
      state_searches_->SetChecked(true);
      break;
    case PASSWORDS:
      state_passwords_->Stop();
      state_passwords_->SetChecked(true);
      break;
    case HISTORY:
      state_history_->Stop();
      state_history_->SetChecked(true);
      break;
    case COOKIES:
      state_cookies_->Stop();
      state_cookies_->SetChecked(true);
      break;
  }
}

void ImportingProgressView::ImportStarted() {
  importing_ = true;
}

void ImportingProgressView::ImportEnded() {
  // This can happen because:
  // - the import completed successfully.
  // - the import was canceled by the user.
  // - the user chose to skip the import because they didn't want to shut down
  //   Firefox.
  // In every case, we need to close the UI now.
  importing_ = false;
  coordinator_->SetObserver(NULL);
  window()->Close();
  if (import_observer_)
    import_observer_->ImportComplete();
}

////////////////////////////////////////////////////////////////////////////////
// ImportingProgressView, ChromeViews::View overrides:

void ImportingProgressView::GetPreferredSize(CSize* out) {
  DCHECK(out);
  *out = ChromeViews::Window::GetLocalizedContentsSize(
      IDS_IMPORTPROGRESS_DIALOG_WIDTH_CHARS,
      IDS_IMPORTPROGRESS_DIALOG_HEIGHT_LINES).ToSIZE();
}

void ImportingProgressView::ViewHierarchyChanged(bool is_add,
                                                 ChromeViews::View* parent,
                                                 ChromeViews::View* child) {
  if (is_add && child == this)
    InitControlLayout();
}

////////////////////////////////////////////////////////////////////////////////
// ImportingProgressView, ChromeViews::DialogDelegate implementation:

int ImportingProgressView::GetDialogButtons() const {
  return DIALOGBUTTON_CANCEL;
}

std::wstring ImportingProgressView::GetDialogButtonLabel(
    DialogButton button) const {
  DCHECK(button == DIALOGBUTTON_CANCEL);
  return l10n_util::GetString(IDS_IMPORT_PROGRESS_STATUS_CANCEL);
}

bool ImportingProgressView::IsModal() const {
  return parent_window_ != NULL;
}

std::wstring ImportingProgressView::GetWindowTitle() const {
  return l10n_util::GetString(IDS_IMPORT_PROGRESS_TITLE);
}

bool ImportingProgressView::Cancel() {
  if (!importing_)
    return true;

  coordinator_->Cancel();
  // Return false because the window needs to live long enough to receive
  // ImportEnded, which will close the window.
  return false;
}

ChromeViews::View* ImportingProgressView::GetContentsView() {
  return this;
}

////////////////////////////////////////////////////////////////////////////////
// ImportingProgressView, private:

void ImportingProgressView::InitControlLayout() {
  using ChromeViews::GridLayout;
  using ChromeViews::ColumnSet;

  GridLayout* layout = CreatePanelGridLayout(this);
  SetLayoutManager(layout);

  CSize ps;
  state_history_->GetPreferredSize(&ps);

  const int single_column_view_set_id = 0;
  ColumnSet* column_set = layout->AddColumnSet(single_column_view_set_id);
  column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
                        GridLayout::USE_PREF, 0, 0);
  const int double_column_view_set_id = 1;
  column_set = layout->AddColumnSet(double_column_view_set_id);
  column_set->AddPaddingColumn(0, kUnrelatedControlLargeHorizontalSpacing);
  column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
                        GridLayout::FIXED, ps.cx, 0);
  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 1,
                        GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, kUnrelatedControlLargeHorizontalSpacing);

  layout->StartRow(0, single_column_view_set_id);
  layout->AddView(label_info_);
  layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing);

  if (items_ & FAVORITES) {
    layout->StartRow(0, double_column_view_set_id);
    layout->AddView(state_bookmarks_.get());
    layout->AddView(label_bookmarks_.get());
    layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
  }
  if (items_ & SEARCH_ENGINES) {
    layout->StartRow(0, double_column_view_set_id);
    layout->AddView(state_searches_.get());
    layout->AddView(label_searches_.get());
    layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
  }
  if (items_ & PASSWORDS) {
    layout->StartRow(0, double_column_view_set_id);
    layout->AddView(state_passwords_.get());
    layout->AddView(label_passwords_.get());
    layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
  }
  if (items_ & HISTORY) {
    layout->StartRow(0, double_column_view_set_id);
    layout->AddView(state_history_.get());
    layout->AddView(label_history_.get());
    layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
  }
  if (items_ & COOKIES) {
    layout->StartRow(0, double_column_view_set_id);
    layout->AddView(state_cookies_.get());
    layout->AddView(label_cookies_.get());
    layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
  }
}

////////////////////////////////////////////////////////////////////////////////
// StartImportingWithUI

void StartImportingWithUI(HWND parent_window,
                          int16 items,
                          ImporterHost* coordinator,
                          const ProfileInfo& source_profile,
                          Profile* target_profile,
                          ImportObserver* observer,
                          bool first_run) {
  DCHECK(items != 0);
  ImportingProgressView* v = new ImportingProgressView(
      source_profile.description, items, coordinator, observer, parent_window);
  ChromeViews::Window::CreateChromeWindow(parent_window, gfx::Rect(),
                                          v)->Show();
  coordinator->StartImportSettings(source_profile, items,
                                   new ProfileWriter(target_profile),
                                   first_run);
}