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/options/content_exception_editor.h" | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "app/resource_bundle.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/content_exceptions_table_model.h" | |
12 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
13 #include "chrome/browser/gtk/gtk_util.h" | |
14 #include "grit/app_resources.h" | |
15 #include "grit/generated_resources.h" | |
16 #include "grit/theme_resources.h" | |
17 | |
18 ContentExceptionEditor::ContentExceptionEditor( | |
19 GtkWindow* parent, | |
20 ContentExceptionEditor::Delegate* delegate, | |
21 ContentExceptionsTableModel* model, | |
22 bool allow_off_the_record, | |
23 int index, | |
24 const ContentSettingsPattern& pattern, | |
25 ContentSetting setting, | |
26 bool is_off_the_record) | |
27 : delegate_(delegate), | |
28 model_(model), | |
29 cb_model_(model->content_type()), | |
30 index_(index), | |
31 pattern_(pattern), | |
32 setting_(setting) { | |
33 dialog_ = gtk_dialog_new_with_buttons( | |
34 l10n_util::GetStringUTF8(is_new() ? | |
35 IDS_EXCEPTION_EDITOR_NEW_TITLE : | |
36 IDS_EXCEPTION_EDITOR_TITLE).c_str(), | |
37 parent, | |
38 // Non-modal. | |
39 static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), | |
40 GTK_STOCK_CANCEL, | |
41 GTK_RESPONSE_CANCEL, | |
42 GTK_STOCK_OK, | |
43 GTK_RESPONSE_OK, | |
44 NULL); | |
45 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_OK); | |
46 | |
47 entry_ = gtk_entry_new(); | |
48 gtk_entry_set_text(GTK_ENTRY(entry_), pattern_.AsString().c_str()); | |
49 g_signal_connect(entry_, "changed", G_CALLBACK(OnEntryChangedThunk), this); | |
50 gtk_entry_set_activates_default(GTK_ENTRY(entry_), TRUE); | |
51 | |
52 pattern_image_ = gtk_image_new_from_pixbuf(NULL); | |
53 | |
54 action_combo_ = gtk_combo_box_new_text(); | |
55 for (int i = 0; i < cb_model_.GetItemCount(); ++i) { | |
56 gtk_combo_box_append_text(GTK_COMBO_BOX(action_combo_), | |
57 UTF16ToUTF8(cb_model_.GetItemAt(i)).c_str()); | |
58 } | |
59 gtk_combo_box_set_active(GTK_COMBO_BOX(action_combo_), | |
60 cb_model_.IndexForSetting(setting_)); | |
61 | |
62 otr_checkbox_ = gtk_check_button_new_with_label( | |
63 l10n_util::GetStringUTF8(IDS_EXCEPTION_EDITOR_OTR_TITLE).c_str()); | |
64 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(otr_checkbox_), | |
65 is_off_the_record); | |
66 | |
67 GtkWidget* table = gtk_util::CreateLabeledControlsGroup( | |
68 NULL, | |
69 l10n_util::GetStringUTF8(IDS_EXCEPTION_EDITOR_PATTERN_TITLE).c_str(), | |
70 gtk_util::CreateEntryImageHBox(entry_, pattern_image_), | |
71 l10n_util::GetStringUTF8(IDS_EXCEPTION_EDITOR_ACTION_TITLE).c_str(), | |
72 action_combo_, | |
73 NULL); | |
74 if (allow_off_the_record) { | |
75 gtk_table_attach(GTK_TABLE(table), otr_checkbox_, | |
76 0, 2, 2, 3, GTK_FILL, GTK_FILL, 0, 0); | |
77 } | |
78 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), table, | |
79 FALSE, FALSE, 0); | |
80 | |
81 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), | |
82 gtk_util::kContentAreaSpacing); | |
83 | |
84 // Prime the state of the buttons. | |
85 OnEntryChanged(entry_); | |
86 | |
87 gtk_util::ShowDialog(dialog_); | |
88 | |
89 g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this); | |
90 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroyThunk), this); | |
91 } | |
92 | |
93 bool ContentExceptionEditor::IsPatternValid( | |
94 const ContentSettingsPattern& pattern, | |
95 bool is_off_the_record) const { | |
96 bool is_valid_pattern = pattern.IsValid() && | |
97 (model_->IndexOfExceptionByPattern(pattern, is_off_the_record) == -1); | |
98 | |
99 return is_new() ? is_valid_pattern : (!pattern.AsString().empty() && | |
100 ((pattern_ == pattern) || is_valid_pattern)); | |
101 } | |
102 | |
103 void ContentExceptionEditor::UpdateImage(GtkWidget* image, bool is_valid) { | |
104 return gtk_image_set_from_pixbuf(GTK_IMAGE(image), | |
105 ResourceBundle::GetSharedInstance().GetPixbufNamed( | |
106 is_valid ? IDR_INPUT_GOOD : IDR_INPUT_ALERT)); | |
107 } | |
108 | |
109 void ContentExceptionEditor::OnEntryChanged(GtkWidget* entry) { | |
110 ContentSettingsPattern new_pattern(gtk_entry_get_text(GTK_ENTRY(entry))); | |
111 bool is_off_the_record = | |
112 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(otr_checkbox_)); | |
113 bool is_valid = IsPatternValid(new_pattern, is_off_the_record); | |
114 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), | |
115 GTK_RESPONSE_OK, is_valid); | |
116 UpdateImage(pattern_image_, is_valid); | |
117 } | |
118 | |
119 void ContentExceptionEditor::OnResponse(GtkWidget* sender, int response_id) { | |
120 if (response_id == GTK_RESPONSE_OK) { | |
121 // Notify our delegate to update everything. | |
122 ContentSettingsPattern new_pattern(gtk_entry_get_text(GTK_ENTRY(entry_))); | |
123 ContentSetting setting = cb_model_.SettingForIndex( | |
124 gtk_combo_box_get_active(GTK_COMBO_BOX(action_combo_))); | |
125 bool is_off_the_record = | |
126 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(otr_checkbox_)); | |
127 delegate_->AcceptExceptionEdit( | |
128 new_pattern, setting, is_off_the_record, index_, is_new()); | |
129 } | |
130 | |
131 gtk_widget_destroy(dialog_); | |
132 } | |
133 | |
134 void ContentExceptionEditor::OnWindowDestroy(GtkWidget* widget) { | |
135 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
136 } | |
OLD | NEW |