OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/options/exception_editor_view.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/content_exceptions_table_model.h" | |
9 #include "googleurl/src/url_canon.h" | |
10 #include "googleurl/src/url_parse.h" | |
11 #include "grit/app_resources.h" | |
12 #include "grit/generated_resources.h" | |
13 #include "grit/theme_resources.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 #include "ui/base/resource/resource_bundle.h" | |
16 #include "views/controls/image_view.h" | |
17 #include "views/controls/label.h" | |
18 #include "views/controls/textfield/textfield.h" | |
19 #include "views/layout/grid_layout.h" | |
20 #include "views/layout/layout_constants.h" | |
21 #include "views/window/window.h" | |
22 | |
23 ExceptionEditorView::ExceptionEditorView( | |
24 Delegate* delegate, | |
25 ContentExceptionsTableModel* model, | |
26 bool allow_off_the_record, | |
27 int index, | |
28 const ContentSettingsPattern& pattern, | |
29 ContentSetting setting, | |
30 bool is_off_the_record) | |
31 : delegate_(delegate), | |
32 model_(model), | |
33 cb_model_(model->content_type()), | |
34 allow_off_the_record_(allow_off_the_record), | |
35 index_(index), | |
36 pattern_(pattern), | |
37 setting_(setting), | |
38 is_off_the_record_(is_off_the_record) { | |
39 // Geolocation exceptions are handled by SimpleContentExceptionsView. | |
40 DCHECK_NE(setting_, CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
41 // Notification exceptions are handled by SimpleContentExceptionsView. | |
42 DCHECK_NE(setting_, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | |
43 Init(); | |
44 } | |
45 | |
46 void ExceptionEditorView::Show(gfx::NativeWindow parent) { | |
47 views::Window* window = | |
48 views::Window::CreateChromeWindow(parent, gfx::Rect(), this); | |
49 window->Show(); | |
50 GetDialogClientView()->UpdateDialogButtons(); | |
51 pattern_tf_->SelectAll(); | |
52 pattern_tf_->RequestFocus(); | |
53 } | |
54 | |
55 bool ExceptionEditorView::IsModal() const { | |
56 return true; | |
57 } | |
58 | |
59 std::wstring ExceptionEditorView::GetWindowTitle() const { | |
60 if (is_new()) | |
61 return UTF16ToWide( | |
62 l10n_util::GetStringUTF16(IDS_EXCEPTION_EDITOR_NEW_TITLE)); | |
63 | |
64 return UTF16ToWide(l10n_util::GetStringUTF16(IDS_EXCEPTION_EDITOR_TITLE)); | |
65 } | |
66 | |
67 bool ExceptionEditorView::IsDialogButtonEnabled( | |
68 MessageBoxFlags::DialogButton button) const { | |
69 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { | |
70 return IsPatternValid(ContentSettingsPattern( | |
71 UTF16ToUTF8(pattern_tf_->text())), | |
72 incognito_cb_->checked()); | |
73 } | |
74 return true; | |
75 } | |
76 | |
77 bool ExceptionEditorView::Cancel() { | |
78 return true; | |
79 } | |
80 | |
81 bool ExceptionEditorView::Accept() { | |
82 ContentSettingsPattern new_pattern(UTF16ToUTF8(pattern_tf_->text())); | |
83 ContentSetting setting = | |
84 cb_model_.SettingForIndex(action_cb_->selected_item()); | |
85 bool is_off_the_record = incognito_cb_->checked(); | |
86 delegate_->AcceptExceptionEdit( | |
87 new_pattern, setting, is_off_the_record, index_, is_new()); | |
88 return true; | |
89 } | |
90 | |
91 views::View* ExceptionEditorView::GetContentsView() { | |
92 return this; | |
93 } | |
94 | |
95 void ExceptionEditorView::ContentsChanged(views::Textfield* sender, | |
96 const std::wstring& new_contents) { | |
97 GetDialogClientView()->UpdateDialogButtons(); | |
98 UpdateImageView(pattern_iv_, IsPatternValid(ContentSettingsPattern( | |
99 UTF16ToUTF8(pattern_tf_->text())), incognito_cb_->checked())); | |
100 } | |
101 | |
102 bool ExceptionEditorView::HandleKeyEvent(views::Textfield* sender, | |
103 const views::KeyEvent& key_event) { | |
104 return false; | |
105 } | |
106 | |
107 void ExceptionEditorView::Init() { | |
108 using views::GridLayout; | |
109 | |
110 pattern_tf_ = new views::Textfield(); | |
111 pattern_tf_->SetText(UTF8ToUTF16(pattern_.AsString())); | |
112 pattern_tf_->SetController(this); | |
113 | |
114 pattern_iv_ = new views::ImageView; | |
115 | |
116 UpdateImageView(pattern_iv_, IsPatternValid(ContentSettingsPattern( | |
117 UTF16ToUTF8(pattern_tf_->text())), is_off_the_record_)); | |
118 | |
119 action_cb_ = new views::Combobox(&cb_model_); | |
120 if (!is_new()) | |
121 action_cb_->SetSelectedItem(cb_model_.IndexForSetting(setting_)); | |
122 | |
123 incognito_cb_ = new views::Checkbox( | |
124 UTF16ToWide(l10n_util::GetStringUTF16(IDS_EXCEPTION_EDITOR_OTR_TITLE))); | |
125 incognito_cb_->SetChecked(is_off_the_record_); | |
126 | |
127 GridLayout* layout = GridLayout::CreatePanel(this); | |
128 SetLayoutManager(layout); | |
129 | |
130 // For the Textfields. | |
131 views::ColumnSet* column_set = layout->AddColumnSet(1); | |
132 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, | |
133 GridLayout::USE_PREF, 0, 0); | |
134 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
135 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, | |
136 GridLayout::USE_PREF, 0, 0); | |
137 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
138 column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, | |
139 GridLayout::USE_PREF, 0, 0); | |
140 | |
141 // Add the contents. | |
142 layout->StartRow(0, 1); | |
143 layout->AddView(CreateLabel(IDS_EXCEPTION_EDITOR_PATTERN_TITLE)); | |
144 layout->AddView(pattern_tf_); | |
145 layout->AddView(pattern_iv_); | |
146 | |
147 layout->StartRowWithPadding(0, 1, 0, views::kRelatedControlVerticalSpacing); | |
148 layout->AddView(CreateLabel(IDS_EXCEPTION_EDITOR_ACTION_TITLE)); | |
149 layout->AddView(action_cb_); | |
150 | |
151 if (allow_off_the_record_) { | |
152 layout->StartRowWithPadding(0, 1, 0, views::kRelatedControlVerticalSpacing); | |
153 layout->AddView(incognito_cb_, 3, 1, GridLayout::FILL, GridLayout::FILL); | |
154 } | |
155 } | |
156 | |
157 views::Label* ExceptionEditorView::CreateLabel(int message_id) { | |
158 views::Label* label = | |
159 new views::Label(UTF16ToWide(l10n_util::GetStringUTF16(message_id))); | |
160 label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
161 return label; | |
162 } | |
163 | |
164 bool ExceptionEditorView::IsPatternValid( | |
165 const ContentSettingsPattern& pattern, | |
166 bool is_off_the_record) const { | |
167 bool is_valid_pattern = pattern.IsValid() && | |
168 (model_->IndexOfExceptionByPattern(pattern, is_off_the_record) == -1); | |
169 | |
170 return is_new() ? is_valid_pattern : (!pattern.AsString().empty() && | |
171 ((pattern_ == pattern) || is_valid_pattern)); | |
172 } | |
173 | |
174 void ExceptionEditorView::UpdateImageView(views::ImageView* image_view, | |
175 bool is_valid) { | |
176 return image_view->SetImage( | |
177 ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
178 is_valid ? IDR_INPUT_GOOD : IDR_INPUT_ALERT)); | |
179 } | |
OLD | NEW |