| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/importer/import_progress_dialog_view.h" | |
| 6 | |
| 7 #include "chrome/browser/importer/importer_host.h" | |
| 8 #include "chrome/browser/importer/importer_observer.h" | |
| 9 #include "chrome/browser/importer/importer_progress_dialog.h" | |
| 10 #include "grit/chromium_strings.h" | |
| 11 #include "grit/generated_resources.h" | |
| 12 #include "grit/locale_settings.h" | |
| 13 #include "ui/base/l10n/l10n_util.h" | |
| 14 #include "ui/views/controls/label.h" | |
| 15 #include "ui/views/controls/throbber.h" | |
| 16 #include "ui/views/layout/grid_layout.h" | |
| 17 #include "ui/views/layout/layout_constants.h" | |
| 18 #include "ui/views/widget/widget.h" | |
| 19 | |
| 20 ImportProgressDialogView::ImportProgressDialogView( | |
| 21 uint16 items, | |
| 22 ImporterHost* importer_host, | |
| 23 ImporterObserver* importer_observer, | |
| 24 const string16& importer_name, | |
| 25 bool bookmarks_import) | |
| 26 : state_bookmarks_(new views::CheckmarkThrobber), | |
| 27 state_searches_(new views::CheckmarkThrobber), | |
| 28 state_passwords_(new views::CheckmarkThrobber), | |
| 29 state_history_(new views::CheckmarkThrobber), | |
| 30 state_cookies_(new views::CheckmarkThrobber), | |
| 31 label_bookmarks_(new views::Label( | |
| 32 l10n_util::GetStringUTF16(IDS_IMPORT_PROGRESS_STATUS_BOOKMARKS))), | |
| 33 label_searches_(new views::Label( | |
| 34 l10n_util::GetStringUTF16(IDS_IMPORT_PROGRESS_STATUS_SEARCH))), | |
| 35 label_passwords_(new views::Label( | |
| 36 l10n_util::GetStringUTF16(IDS_IMPORT_PROGRESS_STATUS_PASSWORDS))), | |
| 37 label_history_(new views::Label( | |
| 38 l10n_util::GetStringUTF16(IDS_IMPORT_PROGRESS_STATUS_HISTORY))), | |
| 39 label_cookies_(new views::Label( | |
| 40 l10n_util::GetStringUTF16(IDS_IMPORT_PROGRESS_STATUS_COOKIES))), | |
| 41 items_(items), | |
| 42 importer_host_(importer_host), | |
| 43 importer_observer_(importer_observer), | |
| 44 importing_(true), | |
| 45 bookmarks_import_(bookmarks_import) { | |
| 46 const string16 info_text = bookmarks_import ? | |
| 47 l10n_util::GetStringUTF16(IDS_IMPORT_BOOKMARKS) : | |
| 48 l10n_util::GetStringFUTF16(IDS_IMPORT_PROGRESS_INFO, importer_name); | |
| 49 label_info_ = new views::Label(info_text); | |
| 50 label_info_->SetMultiLine(true); | |
| 51 label_info_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 52 label_bookmarks_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 53 label_searches_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 54 label_passwords_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 55 label_history_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 56 label_cookies_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 57 | |
| 58 importer_host_->SetObserver(this); | |
| 59 | |
| 60 // These are scoped pointers, so we don't need the parent to delete them. | |
| 61 state_bookmarks_->set_owned_by_client(); | |
| 62 state_searches_->set_owned_by_client(); | |
| 63 state_passwords_->set_owned_by_client(); | |
| 64 state_history_->set_owned_by_client(); | |
| 65 state_cookies_->set_owned_by_client(); | |
| 66 label_bookmarks_->set_owned_by_client(); | |
| 67 label_searches_->set_owned_by_client(); | |
| 68 label_passwords_->set_owned_by_client(); | |
| 69 label_history_->set_owned_by_client(); | |
| 70 label_cookies_->set_owned_by_client(); | |
| 71 } | |
| 72 | |
| 73 ImportProgressDialogView::~ImportProgressDialogView() { | |
| 74 RemoveChildView(state_bookmarks_.get()); | |
| 75 RemoveChildView(state_searches_.get()); | |
| 76 RemoveChildView(state_passwords_.get()); | |
| 77 RemoveChildView(state_history_.get()); | |
| 78 RemoveChildView(state_cookies_.get()); | |
| 79 RemoveChildView(label_bookmarks_.get()); | |
| 80 RemoveChildView(label_searches_.get()); | |
| 81 RemoveChildView(label_passwords_.get()); | |
| 82 RemoveChildView(label_history_.get()); | |
| 83 RemoveChildView(label_cookies_.get()); | |
| 84 | |
| 85 if (importing_) { | |
| 86 // We're being deleted while importing, clean up state so that the importer | |
| 87 // doesn't have a reference to us and cancel the import. We can get here | |
| 88 // if our parent window is closed, which closes our window and deletes us. | |
| 89 importing_ = false; | |
| 90 importer_host_->SetObserver(NULL); | |
| 91 importer_host_->Cancel(); | |
| 92 if (importer_observer_) | |
| 93 importer_observer_->ImportCompleted(); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 gfx::Size ImportProgressDialogView::GetPreferredSize() { | |
| 98 return gfx::Size(views::Widget::GetLocalizedContentsSize( | |
| 99 IDS_IMPORTPROGRESS_DIALOG_WIDTH_CHARS, | |
| 100 IDS_IMPORTPROGRESS_DIALOG_HEIGHT_LINES)); | |
| 101 } | |
| 102 | |
| 103 void ImportProgressDialogView::ViewHierarchyChanged(bool is_add, | |
| 104 views::View* parent, | |
| 105 views::View* child) { | |
| 106 if (is_add && child == this) | |
| 107 InitControlLayout(); | |
| 108 } | |
| 109 | |
| 110 int ImportProgressDialogView::GetDialogButtons() const { | |
| 111 return ui::DIALOG_BUTTON_CANCEL; | |
| 112 } | |
| 113 | |
| 114 string16 ImportProgressDialogView::GetDialogButtonLabel( | |
| 115 ui::DialogButton button) const { | |
| 116 DCHECK_EQ(button, ui::DIALOG_BUTTON_CANCEL); | |
| 117 return l10n_util::GetStringUTF16(IDS_IMPORT_PROGRESS_STATUS_CANCEL); | |
| 118 } | |
| 119 | |
| 120 string16 ImportProgressDialogView::GetWindowTitle() const { | |
| 121 return l10n_util::GetStringUTF16(IDS_IMPORT_PROGRESS_TITLE); | |
| 122 } | |
| 123 | |
| 124 bool ImportProgressDialogView::Cancel() { | |
| 125 // When the user cancels the import, we need to tell the importer_host to stop | |
| 126 // importing and return false so that the window lives long enough to receive | |
| 127 // ImportEnded, which will close the window. Closing the window results in | |
| 128 // another call to this function and at that point we must return true to | |
| 129 // allow the window to close. | |
| 130 if (!importing_) | |
| 131 return true; // We have received ImportEnded, so we can close. | |
| 132 | |
| 133 // Cancel the import and wait for further instructions. | |
| 134 importer_host_->Cancel(); | |
| 135 return false; | |
| 136 } | |
| 137 | |
| 138 void ImportProgressDialogView::InitControlLayout() { | |
| 139 using views::GridLayout; | |
| 140 using views::ColumnSet; | |
| 141 | |
| 142 GridLayout* layout = GridLayout::CreatePanel(this); | |
| 143 SetLayoutManager(layout); | |
| 144 | |
| 145 gfx::Size ps = state_history_->GetPreferredSize(); | |
| 146 | |
| 147 const int single_column_view_set_id = 0; | |
| 148 ColumnSet* column_set = layout->AddColumnSet(single_column_view_set_id); | |
| 149 if (bookmarks_import_) { | |
| 150 column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, | |
| 151 GridLayout::FIXED, ps.width(), 0); | |
| 152 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
| 153 } | |
| 154 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 155 GridLayout::USE_PREF, 0, 0); | |
| 156 const int double_column_view_set_id = 1; | |
| 157 column_set = layout->AddColumnSet(double_column_view_set_id); | |
| 158 column_set->AddPaddingColumn( | |
| 159 0, views::kUnrelatedControlLargeHorizontalSpacing); | |
| 160 column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, | |
| 161 GridLayout::FIXED, ps.width(), 0); | |
| 162 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
| 163 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 1, | |
| 164 GridLayout::USE_PREF, 0, 0); | |
| 165 column_set->AddPaddingColumn( | |
| 166 0, views::kUnrelatedControlLargeHorizontalSpacing); | |
| 167 | |
| 168 layout->StartRow(0, single_column_view_set_id); | |
| 169 if (bookmarks_import_) | |
| 170 layout->AddView(state_bookmarks_.get()); | |
| 171 layout->AddView(label_info_); | |
| 172 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); | |
| 173 | |
| 174 if (items_ & importer::HISTORY) { | |
| 175 layout->StartRow(0, double_column_view_set_id); | |
| 176 layout->AddView(state_history_.get()); | |
| 177 layout->AddView(label_history_.get()); | |
| 178 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 179 } | |
| 180 if (items_ & importer::FAVORITES && !bookmarks_import_) { | |
| 181 layout->StartRow(0, double_column_view_set_id); | |
| 182 layout->AddView(state_bookmarks_.get()); | |
| 183 layout->AddView(label_bookmarks_.get()); | |
| 184 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 185 } | |
| 186 if (items_ & importer::SEARCH_ENGINES) { | |
| 187 layout->StartRow(0, double_column_view_set_id); | |
| 188 layout->AddView(state_searches_.get()); | |
| 189 layout->AddView(label_searches_.get()); | |
| 190 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 191 } | |
| 192 if (items_ & importer::PASSWORDS) { | |
| 193 layout->StartRow(0, double_column_view_set_id); | |
| 194 layout->AddView(state_passwords_.get()); | |
| 195 layout->AddView(label_passwords_.get()); | |
| 196 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 197 } | |
| 198 if (items_ & importer::COOKIES) { | |
| 199 layout->StartRow(0, double_column_view_set_id); | |
| 200 layout->AddView(state_cookies_.get()); | |
| 201 layout->AddView(label_cookies_.get()); | |
| 202 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 void ImportProgressDialogView::ImportStarted() { | |
| 207 importing_ = true; | |
| 208 } | |
| 209 | |
| 210 void ImportProgressDialogView::ImportItemStarted(importer::ImportItem item) { | |
| 211 DCHECK(items_ & item); | |
| 212 switch (item) { | |
| 213 case importer::FAVORITES: | |
| 214 state_bookmarks_->Start(); | |
| 215 break; | |
| 216 case importer::SEARCH_ENGINES: | |
| 217 state_searches_->Start(); | |
| 218 break; | |
| 219 case importer::PASSWORDS: | |
| 220 state_passwords_->Start(); | |
| 221 break; | |
| 222 case importer::HISTORY: | |
| 223 state_history_->Start(); | |
| 224 break; | |
| 225 case importer::COOKIES: | |
| 226 state_cookies_->Start(); | |
| 227 break; | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 void ImportProgressDialogView::ImportItemEnded(importer::ImportItem item) { | |
| 232 DCHECK(items_ & item); | |
| 233 switch (item) { | |
| 234 case importer::FAVORITES: | |
| 235 state_bookmarks_->Stop(); | |
| 236 state_bookmarks_->SetChecked(true); | |
| 237 break; | |
| 238 case importer::SEARCH_ENGINES: | |
| 239 state_searches_->Stop(); | |
| 240 state_searches_->SetChecked(true); | |
| 241 break; | |
| 242 case importer::PASSWORDS: | |
| 243 state_passwords_->Stop(); | |
| 244 state_passwords_->SetChecked(true); | |
| 245 break; | |
| 246 case importer::HISTORY: | |
| 247 state_history_->Stop(); | |
| 248 state_history_->SetChecked(true); | |
| 249 break; | |
| 250 case importer::COOKIES: | |
| 251 state_cookies_->Stop(); | |
| 252 state_cookies_->SetChecked(true); | |
| 253 break; | |
| 254 } | |
| 255 } | |
| 256 | |
| 257 void ImportProgressDialogView::ImportEnded() { | |
| 258 // This can happen because: | |
| 259 // - the import completed successfully. | |
| 260 // - the import was canceled by the user. | |
| 261 // - the user chose to skip the import because they didn't want to shut down | |
| 262 // Firefox. | |
| 263 // In every case, we need to close the UI now. | |
| 264 importing_ = false; | |
| 265 importer_host_->SetObserver(NULL); | |
| 266 GetWidget()->Close(); | |
| 267 if (importer_observer_) | |
| 268 importer_observer_->ImportCompleted(); | |
| 269 } | |
| 270 | |
| 271 namespace importer { | |
| 272 | |
| 273 void ShowImportProgressDialog(uint16 items, | |
| 274 ImporterHost* importer_host, | |
| 275 ImporterObserver* importer_observer, | |
| 276 const SourceProfile& source_profile, | |
| 277 Profile* target_profile, | |
| 278 bool first_run) { | |
| 279 DCHECK_NE(items, 0u); | |
| 280 ImportProgressDialogView* progress_view = new ImportProgressDialogView( | |
| 281 items, | |
| 282 importer_host, | |
| 283 importer_observer, | |
| 284 source_profile.importer_name, | |
| 285 source_profile.importer_type == importer::TYPE_BOOKMARKS_FILE); | |
| 286 | |
| 287 views::Widget* window = views::Widget::CreateWindow(progress_view); | |
| 288 | |
| 289 if (!importer_host->is_headless() && !first_run) | |
| 290 window->Show(); | |
| 291 | |
| 292 importer_host->StartImportSettings(source_profile, target_profile, items, | |
| 293 new ProfileWriter(target_profile), first_run); | |
| 294 } | |
| 295 | |
| 296 } // namespace importer | |
| OLD | NEW |