| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/gtk/import_dialog_gtk.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "app/l10n_util.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "chrome/browser/gtk/accessible_widget_helper_gtk.h" | |
| 12 #include "chrome/browser/gtk/gtk_util.h" | |
| 13 #include "chrome/browser/importer/importer_data_types.h" | |
| 14 #include "grit/generated_resources.h" | |
| 15 #include "grit/locale_settings.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Returns true if the checkbox is checked. | |
| 20 gboolean IsChecked(GtkWidget* widget) { | |
| 21 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 // static | |
| 27 void ImportDialogGtk::Show(GtkWindow* parent, Profile* profile, | |
| 28 int initial_state) { | |
| 29 new ImportDialogGtk(parent, profile, initial_state); | |
| 30 } | |
| 31 | |
| 32 //////////////////////////////////////////////////////////////////////////////// | |
| 33 // ImportObserver implementation: | |
| 34 void ImportDialogGtk::ImportCanceled() { | |
| 35 ImportComplete(); | |
| 36 } | |
| 37 | |
| 38 void ImportDialogGtk::ImportComplete() { | |
| 39 gtk_widget_destroy(dialog_); | |
| 40 delete this; | |
| 41 } | |
| 42 | |
| 43 ImportDialogGtk::ImportDialogGtk(GtkWindow* parent, Profile* profile, | |
| 44 int initial_state) | |
| 45 : parent_(parent), | |
| 46 profile_(profile), | |
| 47 importer_host_(new ImporterHost(this)), | |
| 48 initial_state_(initial_state) { | |
| 49 // Build the dialog. | |
| 50 std::string dialog_name = l10n_util::GetStringUTF8( | |
| 51 IDS_IMPORT_SETTINGS_TITLE); | |
| 52 dialog_ = gtk_dialog_new_with_buttons( | |
| 53 dialog_name.c_str(), | |
| 54 parent, | |
| 55 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), | |
| 56 GTK_STOCK_CANCEL, | |
| 57 GTK_RESPONSE_REJECT, | |
| 58 NULL); | |
| 59 | |
| 60 accessible_widget_helper_.reset(new AccessibleWidgetHelper( | |
| 61 dialog_, profile)); | |
| 62 accessible_widget_helper_->SendOpenWindowNotification(dialog_name); | |
| 63 | |
| 64 importer_host_->set_parent_window(GTK_WINDOW(dialog_)); | |
| 65 | |
| 66 // Add import button separately as we might need to disable it, if | |
| 67 // no supported browsers found. | |
| 68 import_button_ = gtk_util::AddButtonToDialog(dialog_, | |
| 69 l10n_util::GetStringUTF8(IDS_IMPORT_COMMIT).c_str(), | |
| 70 GTK_STOCK_APPLY, GTK_RESPONSE_ACCEPT); | |
| 71 GTK_WIDGET_SET_FLAGS(import_button_, GTK_CAN_DEFAULT); | |
| 72 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); | |
| 73 | |
| 74 GtkWidget* content_area = GTK_DIALOG(dialog_)->vbox; | |
| 75 gtk_box_set_spacing(GTK_BOX(content_area), gtk_util::kContentAreaSpacing); | |
| 76 | |
| 77 GtkWidget* combo_hbox = gtk_hbox_new(FALSE, gtk_util::kLabelSpacing); | |
| 78 GtkWidget* from = gtk_label_new( | |
| 79 l10n_util::GetStringUTF8(IDS_IMPORT_FROM_LABEL).c_str()); | |
| 80 gtk_box_pack_start(GTK_BOX(combo_hbox), from, FALSE, FALSE, 0); | |
| 81 | |
| 82 combo_ = gtk_combo_box_new_text(); | |
| 83 gtk_box_pack_start(GTK_BOX(combo_hbox), combo_, TRUE, TRUE, 0); | |
| 84 | |
| 85 gtk_box_pack_start(GTK_BOX(content_area), combo_hbox, FALSE, FALSE, 0); | |
| 86 | |
| 87 GtkWidget* vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); | |
| 88 | |
| 89 GtkWidget* description = gtk_label_new( | |
| 90 l10n_util::GetStringUTF8(IDS_IMPORT_ITEMS_LABEL).c_str()); | |
| 91 gtk_misc_set_alignment(GTK_MISC(description), 0, 0); | |
| 92 gtk_box_pack_start(GTK_BOX(vbox), description, FALSE, FALSE, 0); | |
| 93 | |
| 94 bookmarks_ = gtk_check_button_new_with_label( | |
| 95 l10n_util::GetStringUTF8(IDS_IMPORT_FAVORITES_CHKBOX).c_str()); | |
| 96 gtk_box_pack_start(GTK_BOX(vbox), bookmarks_, FALSE, FALSE, 0); | |
| 97 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(bookmarks_), | |
| 98 (initial_state_ & importer::FAVORITES) != 0); | |
| 99 g_signal_connect(bookmarks_, "toggled", | |
| 100 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
| 101 | |
| 102 search_engines_ = gtk_check_button_new_with_label( | |
| 103 l10n_util::GetStringUTF8(IDS_IMPORT_SEARCH_ENGINES_CHKBOX).c_str()); | |
| 104 gtk_box_pack_start(GTK_BOX(vbox), search_engines_, FALSE, FALSE, 0); | |
| 105 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(search_engines_), | |
| 106 (initial_state_ & importer::SEARCH_ENGINES) != 0); | |
| 107 g_signal_connect(search_engines_, "toggled", | |
| 108 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
| 109 | |
| 110 passwords_ = gtk_check_button_new_with_label( | |
| 111 l10n_util::GetStringUTF8(IDS_IMPORT_PASSWORDS_CHKBOX).c_str()); | |
| 112 gtk_box_pack_start(GTK_BOX(vbox), passwords_, FALSE, FALSE, 0); | |
| 113 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(passwords_), | |
| 114 (initial_state_ & importer::PASSWORDS) != 0); | |
| 115 g_signal_connect(passwords_, "toggled", | |
| 116 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
| 117 | |
| 118 history_ = gtk_check_button_new_with_label( | |
| 119 l10n_util::GetStringUTF8(IDS_IMPORT_HISTORY_CHKBOX).c_str()); | |
| 120 gtk_box_pack_start(GTK_BOX(vbox), history_, FALSE, FALSE, 0); | |
| 121 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(history_), | |
| 122 (initial_state_ & importer::HISTORY) !=0); | |
| 123 g_signal_connect(history_, "toggled", | |
| 124 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
| 125 | |
| 126 gtk_box_pack_start(GTK_BOX(content_area), vbox, FALSE, FALSE, 0); | |
| 127 | |
| 128 // Let the user know profiles are being loaded. | |
| 129 gtk_combo_box_append_text(GTK_COMBO_BOX(combo_), | |
| 130 l10n_util::GetStringUTF8(IDS_IMPORT_LOADING_PROFILES).c_str()); | |
| 131 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_), 0); | |
| 132 | |
| 133 // Disable controls until source profiles are loaded. | |
| 134 SetDialogControlsSensitive(false); | |
| 135 | |
| 136 g_signal_connect(dialog_, "response", | |
| 137 G_CALLBACK(OnDialogResponseThunk), this); | |
| 138 | |
| 139 UpdateDialogButtons(); | |
| 140 | |
| 141 gtk_util::ShowDialogWithLocalizedSize(dialog_, | |
| 142 IDS_IMPORT_DIALOG_WIDTH_CHARS, | |
| 143 -1, // height | |
| 144 false); // resizable | |
| 145 } | |
| 146 | |
| 147 ImportDialogGtk::~ImportDialogGtk() { | |
| 148 } | |
| 149 | |
| 150 void ImportDialogGtk::SourceProfilesLoaded() { | |
| 151 // Detect any supported browsers that we can import from and fill | |
| 152 // up the combo box. If none found, disable all controls except cancel. | |
| 153 int profiles_count = importer_host_->GetAvailableProfileCount(); | |
| 154 SetDialogControlsSensitive(profiles_count != 0); | |
| 155 gtk_combo_box_remove_text(GTK_COMBO_BOX(combo_), 0); | |
| 156 if (profiles_count > 0) { | |
| 157 for (int i = 0; i < profiles_count; i++) { | |
| 158 std::wstring profile = importer_host_->GetSourceProfileNameAt(i); | |
| 159 gtk_combo_box_append_text(GTK_COMBO_BOX(combo_), | |
| 160 WideToUTF8(profile).c_str()); | |
| 161 } | |
| 162 gtk_widget_grab_focus(import_button_); | |
| 163 } else { | |
| 164 gtk_combo_box_append_text(GTK_COMBO_BOX(combo_), | |
| 165 l10n_util::GetStringUTF8(IDS_IMPORT_NO_PROFILE_FOUND).c_str()); | |
| 166 } | |
| 167 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_), 0); | |
| 168 } | |
| 169 | |
| 170 void ImportDialogGtk::OnDialogResponse(GtkWidget* widget, int response) { | |
| 171 gtk_widget_hide_all(dialog_); | |
| 172 if (response == GTK_RESPONSE_ACCEPT) { | |
| 173 uint16 items = GetCheckedItems(); | |
| 174 if (items == 0) { | |
| 175 ImportComplete(); | |
| 176 } else { | |
| 177 const ProfileInfo& source_profile = | |
| 178 importer_host_->GetSourceProfileInfoAt( | |
| 179 gtk_combo_box_get_active(GTK_COMBO_BOX(combo_))); | |
| 180 StartImportingWithUI(parent_, items, importer_host_.get(), | |
| 181 source_profile, profile_, this, false); | |
| 182 } | |
| 183 } else { | |
| 184 ImportCanceled(); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 void ImportDialogGtk::OnDialogWidgetClicked(GtkWidget* widget) { | |
| 189 UpdateDialogButtons(); | |
| 190 } | |
| 191 | |
| 192 void ImportDialogGtk::UpdateDialogButtons() { | |
| 193 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT, | |
| 194 GetCheckedItems() != 0); | |
| 195 } | |
| 196 | |
| 197 void ImportDialogGtk::SetDialogControlsSensitive(bool sensitive) { | |
| 198 gtk_widget_set_sensitive(bookmarks_, sensitive); | |
| 199 gtk_widget_set_sensitive(search_engines_, sensitive); | |
| 200 gtk_widget_set_sensitive(passwords_, sensitive); | |
| 201 gtk_widget_set_sensitive(history_, sensitive); | |
| 202 gtk_widget_set_sensitive(import_button_, sensitive); | |
| 203 } | |
| 204 | |
| 205 uint16 ImportDialogGtk::GetCheckedItems() { | |
| 206 uint16 items = importer::NONE; | |
| 207 if (IsChecked(bookmarks_)) | |
| 208 items |= importer::FAVORITES; | |
| 209 if (IsChecked(search_engines_)) | |
| 210 items |= importer::SEARCH_ENGINES; | |
| 211 if (IsChecked(passwords_)) | |
| 212 items |= importer::PASSWORDS; | |
| 213 if (IsChecked(history_)) | |
| 214 items |= importer::HISTORY; | |
| 215 return items; | |
| 216 } | |
| OLD | NEW |