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/clear_browsing_data_dialog_gtk.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "app/l10n_util.h" | |
10 #include "chrome/browser/browsing_data_remover.h" | |
11 #include "chrome/browser/gtk/accessible_widget_helper_gtk.h" | |
12 #include "chrome/browser/gtk/browser_window_gtk.h" | |
13 #include "chrome/browser/gtk/gtk_chrome_link_button.h" | |
14 #include "chrome/browser/gtk/gtk_util.h" | |
15 #include "chrome/browser/prefs/pref_service.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/ui/browser.h" | |
18 #include "chrome/common/pref_names.h" | |
19 #include "grit/generated_resources.h" | |
20 #include "grit/locale_settings.h" | |
21 | |
22 namespace { | |
23 | |
24 // Returns true if the checkbox is checked. | |
25 gboolean IsChecked(GtkWidget* widget) { | |
26 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 // static | |
32 void ClearBrowsingDataDialogGtk::Show(GtkWindow* parent, Profile* profile) { | |
33 new ClearBrowsingDataDialogGtk(parent, profile); | |
34 } | |
35 | |
36 ClearBrowsingDataDialogGtk::ClearBrowsingDataDialogGtk(GtkWindow* parent, | |
37 Profile* profile) : | |
38 profile_(profile), remover_(NULL) { | |
39 // Build the dialog. | |
40 std::string dialog_name = l10n_util::GetStringUTF8( | |
41 IDS_CLEAR_BROWSING_DATA_TITLE); | |
42 dialog_ = gtk_dialog_new_with_buttons( | |
43 dialog_name.c_str(), | |
44 parent, | |
45 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), | |
46 NULL); | |
47 | |
48 GtkWidget* cancel_button = gtk_dialog_add_button(GTK_DIALOG(dialog_), | |
49 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT); | |
50 gtk_widget_grab_focus(cancel_button); | |
51 | |
52 accessible_widget_helper_.reset(new AccessibleWidgetHelper(dialog_, profile)); | |
53 accessible_widget_helper_->SendOpenWindowNotification(dialog_name); | |
54 | |
55 gtk_util::AddButtonToDialog(dialog_, | |
56 l10n_util::GetStringUTF8(IDS_CLEAR_BROWSING_DATA_COMMIT).c_str(), | |
57 GTK_STOCK_APPLY, GTK_RESPONSE_ACCEPT); | |
58 | |
59 GtkWidget* content_area = GTK_DIALOG(dialog_)->vbox; | |
60 gtk_box_set_spacing(GTK_BOX(content_area), gtk_util::kContentAreaSpacing); | |
61 | |
62 GtkWidget* vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); | |
63 gtk_container_add(GTK_CONTAINER(content_area), vbox); | |
64 | |
65 // Label on top of the checkboxes. | |
66 GtkWidget* description = gtk_label_new( | |
67 l10n_util::GetStringUTF8(IDS_CLEAR_BROWSING_DATA_LABEL).c_str()); | |
68 gtk_misc_set_alignment(GTK_MISC(description), 0, 0); | |
69 gtk_box_pack_start(GTK_BOX(vbox), description, FALSE, FALSE, 0); | |
70 | |
71 // History checkbox. | |
72 del_history_checkbox_ = gtk_check_button_new_with_label( | |
73 l10n_util::GetStringUTF8(IDS_DEL_BROWSING_HISTORY_CHKBOX).c_str()); | |
74 gtk_box_pack_start(GTK_BOX(vbox), del_history_checkbox_, FALSE, FALSE, 0); | |
75 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(del_history_checkbox_), | |
76 profile_->GetPrefs()->GetBoolean(prefs::kDeleteBrowsingHistory)); | |
77 g_signal_connect(del_history_checkbox_, "toggled", | |
78 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
79 | |
80 // Downloads checkbox. | |
81 del_downloads_checkbox_ = gtk_check_button_new_with_label( | |
82 l10n_util::GetStringUTF8(IDS_DEL_DOWNLOAD_HISTORY_CHKBOX).c_str()); | |
83 gtk_box_pack_start(GTK_BOX(vbox), del_downloads_checkbox_, FALSE, FALSE, 0); | |
84 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(del_downloads_checkbox_), | |
85 profile_->GetPrefs()->GetBoolean(prefs::kDeleteDownloadHistory)); | |
86 g_signal_connect(del_downloads_checkbox_, "toggled", | |
87 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
88 | |
89 // Cache checkbox. | |
90 del_cache_checkbox_ = gtk_check_button_new_with_label( | |
91 l10n_util::GetStringUTF8(IDS_DEL_CACHE_CHKBOX).c_str()); | |
92 gtk_box_pack_start(GTK_BOX(vbox), del_cache_checkbox_, FALSE, FALSE, 0); | |
93 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(del_cache_checkbox_), | |
94 profile_->GetPrefs()->GetBoolean(prefs::kDeleteCache)); | |
95 g_signal_connect(del_cache_checkbox_, "toggled", | |
96 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
97 | |
98 // Cookies checkbox. | |
99 del_cookies_checkbox_ = gtk_check_button_new_with_label( | |
100 l10n_util::GetStringUTF8(IDS_DEL_COOKIES_CHKBOX).c_str()); | |
101 gtk_box_pack_start(GTK_BOX(vbox), del_cookies_checkbox_, FALSE, FALSE, 0); | |
102 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(del_cookies_checkbox_), | |
103 profile_->GetPrefs()->GetBoolean(prefs::kDeleteCookies)); | |
104 g_signal_connect(del_cookies_checkbox_, "toggled", | |
105 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
106 | |
107 // Passwords checkbox. | |
108 del_passwords_checkbox_ = gtk_check_button_new_with_label( | |
109 l10n_util::GetStringUTF8(IDS_DEL_PASSWORDS_CHKBOX).c_str()); | |
110 gtk_box_pack_start(GTK_BOX(vbox), del_passwords_checkbox_, FALSE, FALSE, 0); | |
111 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(del_passwords_checkbox_), | |
112 profile_->GetPrefs()->GetBoolean(prefs::kDeletePasswords)); | |
113 g_signal_connect(del_passwords_checkbox_, "toggled", | |
114 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
115 | |
116 // Form data checkbox. | |
117 del_form_data_checkbox_ = gtk_check_button_new_with_label( | |
118 l10n_util::GetStringUTF8(IDS_DEL_FORM_DATA_CHKBOX).c_str()); | |
119 gtk_box_pack_start(GTK_BOX(vbox), del_form_data_checkbox_, FALSE, FALSE, 0); | |
120 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(del_form_data_checkbox_), | |
121 profile_->GetPrefs()->GetBoolean(prefs::kDeleteFormData)); | |
122 g_signal_connect(del_form_data_checkbox_, "toggled", | |
123 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
124 | |
125 // Create a horizontal layout for the combo box and label. | |
126 GtkWidget* combo_hbox = gtk_hbox_new(FALSE, gtk_util::kLabelSpacing); | |
127 GtkWidget* time_period_label_ = gtk_label_new( | |
128 l10n_util::GetStringUTF8(IDS_CLEAR_BROWSING_DATA_TIME_LABEL).c_str()); | |
129 gtk_box_pack_start(GTK_BOX(combo_hbox), time_period_label_, FALSE, FALSE, 0); | |
130 | |
131 // Time period combo box items. | |
132 time_period_combobox_ = gtk_combo_box_new_text(); | |
133 gtk_combo_box_append_text(GTK_COMBO_BOX(time_period_combobox_), | |
134 l10n_util::GetStringUTF8(IDS_CLEAR_DATA_HOUR).c_str()); | |
135 gtk_combo_box_append_text(GTK_COMBO_BOX(time_period_combobox_), | |
136 l10n_util::GetStringUTF8(IDS_CLEAR_DATA_DAY).c_str()); | |
137 gtk_combo_box_append_text(GTK_COMBO_BOX(time_period_combobox_), | |
138 l10n_util::GetStringUTF8(IDS_CLEAR_DATA_WEEK).c_str()); | |
139 gtk_combo_box_append_text(GTK_COMBO_BOX(time_period_combobox_), | |
140 l10n_util::GetStringUTF8(IDS_CLEAR_DATA_4WEEKS).c_str()); | |
141 gtk_combo_box_append_text(GTK_COMBO_BOX(time_period_combobox_), | |
142 l10n_util::GetStringUTF8(IDS_CLEAR_DATA_EVERYTHING).c_str()); | |
143 gtk_combo_box_set_active(GTK_COMBO_BOX(time_period_combobox_), | |
144 profile_->GetPrefs()->GetInteger(prefs::kDeleteTimePeriod)); | |
145 gtk_box_pack_start(GTK_BOX(combo_hbox), | |
146 time_period_combobox_, FALSE, FALSE, 0); | |
147 g_signal_connect(time_period_combobox_, "changed", | |
148 G_CALLBACK(OnDialogWidgetClickedThunk), this); | |
149 | |
150 // Add the combo/label time period box to the vertical layout. | |
151 gtk_box_pack_start(GTK_BOX(vbox), combo_hbox, FALSE, FALSE, 0); | |
152 | |
153 // Add widgets for the area below the accept buttons. | |
154 GtkWidget* flash_link = gtk_chrome_link_button_new( | |
155 l10n_util::GetStringUTF8(IDS_FLASH_STORAGE_SETTINGS).c_str()); | |
156 g_signal_connect(G_OBJECT(flash_link), "clicked", | |
157 G_CALLBACK(OnFlashLinkClickedThunk), this); | |
158 GtkWidget* flash_link_hbox = gtk_hbox_new(FALSE, 0); | |
159 gtk_box_pack_start(GTK_BOX(flash_link_hbox), flash_link, FALSE, FALSE, 0); | |
160 gtk_box_pack_end(GTK_BOX(content_area), flash_link_hbox, FALSE, FALSE, 0); | |
161 | |
162 GtkWidget* separator = gtk_hseparator_new(); | |
163 gtk_box_pack_end(GTK_BOX(content_area), separator, FALSE, FALSE, 0); | |
164 | |
165 // Make sure we can move things around. | |
166 DCHECK_EQ(GTK_DIALOG(dialog_)->action_area->parent, content_area); | |
167 | |
168 // Now rearrange those because they're *above* the accept buttons...there's | |
169 // no way to place them in the correct position with gtk_box_pack_end() so | |
170 // manually move things into the correct order. | |
171 gtk_box_reorder_child(GTK_BOX(content_area), flash_link_hbox, -1); | |
172 gtk_box_reorder_child(GTK_BOX(content_area), separator, -1); | |
173 gtk_box_reorder_child(GTK_BOX(content_area), GTK_DIALOG(dialog_)->action_area, | |
174 -1); | |
175 | |
176 g_signal_connect(dialog_, "response", | |
177 G_CALLBACK(OnDialogResponseThunk), this); | |
178 | |
179 UpdateDialogButtons(); | |
180 | |
181 gtk_util::ShowModalDialogWithMinLocalizedWidth(dialog_, | |
182 IDS_CLEARDATA_DIALOG_WIDTH_CHARS); | |
183 } | |
184 | |
185 ClearBrowsingDataDialogGtk::~ClearBrowsingDataDialogGtk() { | |
186 } | |
187 | |
188 void ClearBrowsingDataDialogGtk::OnDialogResponse(GtkWidget* widget, | |
189 int response) { | |
190 if (response == GTK_RESPONSE_ACCEPT) { | |
191 PrefService* prefs = profile_->GetPrefs(); | |
192 prefs->SetBoolean(prefs::kDeleteBrowsingHistory, | |
193 IsChecked(del_history_checkbox_)); | |
194 prefs->SetBoolean(prefs::kDeleteDownloadHistory, | |
195 IsChecked(del_downloads_checkbox_)); | |
196 prefs->SetBoolean(prefs::kDeleteCache, | |
197 IsChecked(del_cache_checkbox_)); | |
198 prefs->SetBoolean(prefs::kDeleteCookies, | |
199 IsChecked(del_cookies_checkbox_)); | |
200 prefs->SetBoolean(prefs::kDeletePasswords, | |
201 IsChecked(del_passwords_checkbox_)); | |
202 prefs->SetBoolean(prefs::kDeleteFormData, | |
203 IsChecked(del_form_data_checkbox_)); | |
204 prefs->SetInteger(prefs::kDeleteTimePeriod, | |
205 gtk_combo_box_get_active(GTK_COMBO_BOX(time_period_combobox_))); | |
206 | |
207 int period_selected = gtk_combo_box_get_active( | |
208 GTK_COMBO_BOX(time_period_combobox_)); | |
209 | |
210 // BrowsingDataRemover deletes itself when done. | |
211 remover_ = new BrowsingDataRemover(profile_, | |
212 static_cast<BrowsingDataRemover::TimePeriod>(period_selected), | |
213 base::Time()); | |
214 remover_->Remove(GetCheckedItems()); | |
215 } | |
216 | |
217 delete this; | |
218 gtk_widget_destroy(GTK_WIDGET(widget)); | |
219 } | |
220 | |
221 void ClearBrowsingDataDialogGtk::OnDialogWidgetClicked(GtkWidget* widget) { | |
222 UpdateDialogButtons(); | |
223 } | |
224 | |
225 void ClearBrowsingDataDialogGtk::OnFlashLinkClicked(GtkWidget* button) { | |
226 // We open a new browser window so the Options dialog doesn't get lost | |
227 // behind other windows. | |
228 Browser* browser = Browser::Create(profile_); | |
229 browser->OpenURL(GURL(l10n_util::GetStringUTF8(IDS_FLASH_STORAGE_URL)), | |
230 GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); | |
231 browser->window()->Show(); | |
232 } | |
233 | |
234 void ClearBrowsingDataDialogGtk::UpdateDialogButtons() { | |
235 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT, | |
236 GetCheckedItems() != 0); | |
237 } | |
238 | |
239 int ClearBrowsingDataDialogGtk::GetCheckedItems() { | |
240 int items = 0; | |
241 if (IsChecked(del_history_checkbox_)) | |
242 items |= BrowsingDataRemover::REMOVE_HISTORY; | |
243 if (IsChecked(del_downloads_checkbox_)) | |
244 items |= BrowsingDataRemover::REMOVE_DOWNLOADS; | |
245 if (IsChecked(del_cookies_checkbox_)) | |
246 items |= BrowsingDataRemover::REMOVE_COOKIES; | |
247 if (IsChecked(del_passwords_checkbox_)) | |
248 items |= BrowsingDataRemover::REMOVE_PASSWORDS; | |
249 if (IsChecked(del_form_data_checkbox_)) | |
250 items |= BrowsingDataRemover::REMOVE_FORM_DATA; | |
251 if (IsChecked(del_cache_checkbox_)) | |
252 items |= BrowsingDataRemover::REMOVE_CACHE; | |
253 return items; | |
254 } | |
OLD | NEW |