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/gtk/importer/import_progress_dialog_gtk.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/utf_string_conversions.h" | |
12 #include "chrome/browser/importer/importer_host.h" | |
13 #include "chrome/browser/importer/importer_observer.h" | |
14 #include "chrome/browser/ui/gtk/gtk_util.h" | |
15 #include "grit/chromium_strings.h" | |
16 #include "grit/generated_resources.h" | |
17 #include "ui/base/gtk/gtk_hig_constants.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 | |
20 namespace { | |
21 | |
22 void SetItemImportStatus(GtkWidget* label, int res_id, bool is_done) { | |
23 std::string status = l10n_util::GetStringUTF8(res_id); | |
24 // Windows version of this has fancy throbbers to indicate progress. Here | |
25 // we rely on text until we can have something equivalent on Linux. | |
26 if (is_done) | |
27 status = "\xE2\x9C\x94 " + status; // U+2714 HEAVY CHECK MARK | |
28 else | |
29 status.append(" ..."); | |
30 gtk_label_set_text(GTK_LABEL(label), status.c_str()); | |
31 } | |
32 | |
33 } // namespace | |
34 | |
35 // static | |
36 void ImportProgressDialogGtk::StartImport( | |
37 uint16 items, | |
38 ImporterHost* importer_host, | |
39 ImporterObserver* importer_observer, | |
40 const importer::SourceProfile& source_profile, | |
41 Profile* profile, | |
42 bool first_run) { | |
43 ImportProgressDialogGtk* dialog = new ImportProgressDialogGtk(items, | |
44 importer_host, importer_observer, source_profile.importer_name, | |
45 source_profile.importer_type == importer::TYPE_BOOKMARKS_FILE); | |
46 | |
47 // In headless mode it means that we don't show the progress window, but it | |
48 // still need it to exist. No user interaction will be required. | |
49 if (!importer_host->is_headless()) | |
50 dialog->ShowDialog(); | |
51 | |
52 importer_host->StartImportSettings( | |
53 source_profile, profile, items, new ProfileWriter(profile), first_run); | |
54 } | |
55 | |
56 ImportProgressDialogGtk::ImportProgressDialogGtk( | |
57 uint16 items, | |
58 ImporterHost* importer_host, | |
59 ImporterObserver* importer_observer, | |
60 const string16& importer_name, | |
61 bool bookmarks_import) | |
62 : items_(items), | |
63 importer_host_(importer_host), | |
64 importer_observer_(importer_observer), | |
65 importing_(true) { | |
66 importer_host_->SetObserver(this); | |
67 | |
68 // Build the dialog. | |
69 dialog_ = gtk_dialog_new_with_buttons( | |
70 l10n_util::GetStringUTF8(IDS_IMPORT_PROGRESS_TITLE).c_str(), | |
71 NULL, | |
72 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), | |
73 GTK_STOCK_CANCEL, | |
74 GTK_RESPONSE_REJECT, | |
75 NULL); | |
76 importer_host_->set_parent_window(GTK_WINDOW(dialog_)); | |
77 | |
78 GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog_)); | |
79 gtk_box_set_spacing(GTK_BOX(content_area), ui::kContentAreaSpacing); | |
80 | |
81 GtkWidget* control_group = gtk_vbox_new(FALSE, ui::kControlSpacing); | |
82 | |
83 GtkWidget* import_info = gtk_label_new( | |
84 l10n_util::GetStringFUTF8(IDS_IMPORT_PROGRESS_INFO, | |
85 importer_name).c_str()); | |
86 gtk_util::SetLabelWidth(import_info, 400); | |
87 gtk_box_pack_start(GTK_BOX(control_group), import_info, FALSE, FALSE, 0); | |
88 | |
89 GtkWidget* item_box = gtk_vbox_new(FALSE, ui::kControlSpacing); | |
90 | |
91 if (items_ & importer::HISTORY) { | |
92 history_ = gtk_label_new( | |
93 l10n_util::GetStringUTF8(IDS_IMPORT_PROGRESS_STATUS_HISTORY).c_str()); | |
94 gtk_misc_set_alignment(GTK_MISC(history_), 0, 0.5); | |
95 gtk_box_pack_start(GTK_BOX(item_box), history_, FALSE, FALSE, 0); | |
96 } | |
97 | |
98 if (items_ & importer::FAVORITES) { | |
99 bookmarks_ = gtk_label_new( | |
100 l10n_util::GetStringUTF8(IDS_IMPORT_PROGRESS_STATUS_BOOKMARKS).c_str()); | |
101 gtk_misc_set_alignment(GTK_MISC(bookmarks_), 0, 0.5); | |
102 gtk_box_pack_start(GTK_BOX(item_box), bookmarks_, FALSE, FALSE, 0); | |
103 } | |
104 | |
105 if (items_ & importer::SEARCH_ENGINES) { | |
106 search_engines_ = gtk_label_new( | |
107 l10n_util::GetStringUTF8(IDS_IMPORT_PROGRESS_STATUS_SEARCH).c_str()); | |
108 gtk_misc_set_alignment(GTK_MISC(search_engines_), 0, 0.5); | |
109 gtk_box_pack_start(GTK_BOX(item_box), search_engines_, FALSE, FALSE, 0); | |
110 } | |
111 | |
112 if (items_ & importer::PASSWORDS) { | |
113 passwords_ = gtk_label_new( | |
114 l10n_util::GetStringUTF8(IDS_IMPORT_PROGRESS_STATUS_PASSWORDS).c_str()); | |
115 gtk_misc_set_alignment(GTK_MISC(passwords_), 0, 0.5); | |
116 gtk_box_pack_start(GTK_BOX(item_box), passwords_, FALSE, FALSE, 0); | |
117 } | |
118 | |
119 gtk_box_pack_start(GTK_BOX(control_group), gtk_util::IndentWidget(item_box), | |
120 FALSE, FALSE, 0); | |
121 gtk_box_pack_start(GTK_BOX(content_area), control_group, FALSE, FALSE, 0); | |
122 | |
123 g_signal_connect(dialog_, "response", | |
124 G_CALLBACK(OnResponseThunk), this); | |
125 gtk_window_set_resizable(GTK_WINDOW(dialog_), FALSE); | |
126 } | |
127 | |
128 ImportProgressDialogGtk::~ImportProgressDialogGtk() {} | |
129 | |
130 void ImportProgressDialogGtk::OnResponse(GtkWidget* dialog, int response_id) { | |
131 if (!importing_) { | |
132 CloseDialog(); | |
133 return; | |
134 } | |
135 | |
136 // Only response to the dialog is to close it so we hide immediately. | |
137 gtk_widget_hide_all(dialog_); | |
138 if (response_id == GTK_RESPONSE_REJECT) | |
139 importer_host_->Cancel(); | |
140 } | |
141 | |
142 void ImportProgressDialogGtk::ShowDialog() { | |
143 gtk_widget_show_all(dialog_); | |
144 } | |
145 | |
146 void ImportProgressDialogGtk::CloseDialog() { | |
147 gtk_widget_destroy(dialog_); | |
148 dialog_ = NULL; | |
149 delete this; | |
150 } | |
151 | |
152 void ImportProgressDialogGtk::ImportStarted() { | |
153 importing_ = true; | |
154 } | |
155 | |
156 void ImportProgressDialogGtk::ImportItemStarted(importer::ImportItem item) { | |
157 DCHECK(items_ & item); | |
158 switch (item) { | |
159 case importer::FAVORITES: | |
160 SetItemImportStatus(bookmarks_, | |
161 IDS_IMPORT_PROGRESS_STATUS_BOOKMARKS, false); | |
162 break; | |
163 case importer::SEARCH_ENGINES: | |
164 SetItemImportStatus(search_engines_, | |
165 IDS_IMPORT_PROGRESS_STATUS_SEARCH, false); | |
166 break; | |
167 case importer::PASSWORDS: | |
168 SetItemImportStatus(passwords_, | |
169 IDS_IMPORT_PROGRESS_STATUS_PASSWORDS, false); | |
170 break; | |
171 case importer::HISTORY: | |
172 SetItemImportStatus(history_, | |
173 IDS_IMPORT_PROGRESS_STATUS_HISTORY, false); | |
174 break; | |
175 default: | |
176 break; | |
177 } | |
178 } | |
179 | |
180 void ImportProgressDialogGtk::ImportItemEnded(importer::ImportItem item) { | |
181 DCHECK(items_ & item); | |
182 switch (item) { | |
183 case importer::FAVORITES: | |
184 SetItemImportStatus(bookmarks_, | |
185 IDS_IMPORT_PROGRESS_STATUS_BOOKMARKS, true); | |
186 break; | |
187 case importer::SEARCH_ENGINES: | |
188 SetItemImportStatus(search_engines_, | |
189 IDS_IMPORT_PROGRESS_STATUS_SEARCH, true); | |
190 break; | |
191 case importer::PASSWORDS: | |
192 SetItemImportStatus(passwords_, | |
193 IDS_IMPORT_PROGRESS_STATUS_PASSWORDS, true); | |
194 break; | |
195 case importer::HISTORY: | |
196 SetItemImportStatus(history_, | |
197 IDS_IMPORT_PROGRESS_STATUS_HISTORY, true); | |
198 break; | |
199 default: | |
200 break; | |
201 } | |
202 } | |
203 | |
204 void ImportProgressDialogGtk::ImportEnded() { | |
205 importing_ = false; | |
206 importer_host_->SetObserver(NULL); | |
207 if (importer_observer_) | |
208 importer_observer_->ImportCompleted(); | |
209 CloseDialog(); | |
210 } | |
211 | |
212 namespace importer { | |
213 | |
214 void ShowImportProgressDialog(uint16 items, | |
215 ImporterHost* importer_host, | |
216 ImporterObserver* importer_observer, | |
217 const SourceProfile& source_profile, | |
218 Profile* profile, | |
219 bool first_run) { | |
220 DCHECK_NE(0, items); | |
221 ImportProgressDialogGtk::StartImport(items, importer_host, importer_observer, | |
222 source_profile, profile, first_run); | |
223 } | |
224 | |
225 } // namespace importer | |
OLD | NEW |