| 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/views/edit_search_engine_dialog.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/i18n/case_conversion.h" | |
| 10 #include "base/i18n/rtl.h" | |
| 11 #include "base/metrics/histogram_macros.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | |
| 15 #include "chrome/browser/ui/search_engines/edit_search_engine_controller.h" | |
| 16 #include "chrome/grit/generated_resources.h" | |
| 17 #include "components/constrained_window/constrained_window_views.h" | |
| 18 #include "components/search_engines/template_url.h" | |
| 19 #include "content/public/browser/user_metrics.h" | |
| 20 #include "grit/theme_resources.h" | |
| 21 #include "ui/base/l10n/l10n_util.h" | |
| 22 #include "ui/base/resource/resource_bundle.h" | |
| 23 #include "ui/events/event.h" | |
| 24 #include "ui/views/controls/image_view.h" | |
| 25 #include "ui/views/controls/label.h" | |
| 26 #include "ui/views/controls/textfield/textfield.h" | |
| 27 #include "ui/views/layout/grid_layout.h" | |
| 28 #include "ui/views/layout/layout_constants.h" | |
| 29 #include "ui/views/widget/widget.h" | |
| 30 #include "ui/views/window/dialog_client_view.h" | |
| 31 #include "url/gurl.h" | |
| 32 | |
| 33 using views::GridLayout; | |
| 34 using views::Textfield; | |
| 35 | |
| 36 EditSearchEngineDialog::EditSearchEngineDialog( | |
| 37 TemplateURL* template_url, | |
| 38 EditSearchEngineControllerDelegate* delegate, | |
| 39 Profile* profile) | |
| 40 : controller_(new EditSearchEngineController(template_url, | |
| 41 delegate, | |
| 42 profile)) { | |
| 43 Init(); | |
| 44 } | |
| 45 | |
| 46 EditSearchEngineDialog::~EditSearchEngineDialog() { | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 void EditSearchEngineDialog::Show(gfx::NativeWindow parent, | |
| 51 TemplateURL* template_url, | |
| 52 EditSearchEngineControllerDelegate* delegate, | |
| 53 Profile* profile) { | |
| 54 UMA_HISTOGRAM_ENUMERATION( | |
| 55 "Search.AddSearchProvider2", | |
| 56 EditSearchEngineController::CONFIRMATION_DIALOG_SHOWN, | |
| 57 EditSearchEngineController::NUM_EDIT_SEARCH_ENGINE_ACTIONS); | |
| 58 EditSearchEngineDialog* contents = | |
| 59 new EditSearchEngineDialog(template_url, delegate, profile); | |
| 60 // Window interprets an empty rectangle as needing to query the content for | |
| 61 // the size as well as centering relative to the parent. | |
| 62 constrained_window::CreateBrowserModalDialogViews(contents, parent); | |
| 63 contents->GetWidget()->Show(); | |
| 64 contents->GetDialogClientView()->UpdateDialogButtons(); | |
| 65 contents->title_tf_->SelectAll(true); | |
| 66 contents->title_tf_->RequestFocus(); | |
| 67 } | |
| 68 | |
| 69 ui::ModalType EditSearchEngineDialog::GetModalType() const { | |
| 70 return ui::MODAL_TYPE_WINDOW; | |
| 71 } | |
| 72 | |
| 73 base::string16 EditSearchEngineDialog::GetWindowTitle() const { | |
| 74 return l10n_util::GetStringUTF16(controller_->template_url() ? | |
| 75 IDS_SEARCH_ENGINES_EDITOR_EDIT_WINDOW_TITLE : | |
| 76 IDS_SEARCH_ENGINES_EDITOR_NEW_WINDOW_TITLE); | |
| 77 } | |
| 78 | |
| 79 bool EditSearchEngineDialog::IsDialogButtonEnabled( | |
| 80 ui::DialogButton button) const { | |
| 81 if (button == ui::DIALOG_BUTTON_OK) { | |
| 82 return (controller_->IsKeywordValid(keyword_tf_->text()) && | |
| 83 controller_->IsTitleValid(title_tf_->text()) && | |
| 84 controller_->IsURLValid(base::UTF16ToUTF8(url_tf_->text()))); | |
| 85 } | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 bool EditSearchEngineDialog::Cancel() { | |
| 90 UMA_HISTOGRAM_ENUMERATION( | |
| 91 "Search.AddSearchProvider2", | |
| 92 EditSearchEngineController::CONFIRMATION_DIALOG_CANCELLED, | |
| 93 EditSearchEngineController::NUM_EDIT_SEARCH_ENGINE_ACTIONS); | |
| 94 controller_->CleanUpCancelledAdd(); | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 bool EditSearchEngineDialog::Accept() { | |
| 99 UMA_HISTOGRAM_ENUMERATION( | |
| 100 "Search.AddSearchProvider2", | |
| 101 EditSearchEngineController::CONFIRMATION_DIALOG_CONFIRMED, | |
| 102 EditSearchEngineController::NUM_EDIT_SEARCH_ENGINE_ACTIONS); | |
| 103 controller_->AcceptAddOrEdit(title_tf_->text(), keyword_tf_->text(), | |
| 104 base::UTF16ToUTF8(url_tf_->text())); | |
| 105 return true; | |
| 106 } | |
| 107 | |
| 108 void EditSearchEngineDialog::ContentsChanged( | |
| 109 Textfield* sender, | |
| 110 const base::string16& new_contents) { | |
| 111 // Force the keyword text to be lowercase, keep the caret or selection. | |
| 112 if (sender == keyword_tf_ && !sender->HasCompositionText()) { | |
| 113 // TODO(msw): Prevent textfield scrolling with selection model changes here. | |
| 114 const gfx::SelectionModel selection_model = sender->GetSelectionModel(); | |
| 115 sender->SetText(base::i18n::ToLower(new_contents)); | |
| 116 sender->SelectSelectionModel(selection_model); | |
| 117 } | |
| 118 | |
| 119 GetDialogClientView()->UpdateDialogButtons(); | |
| 120 UpdateImageViews(); | |
| 121 } | |
| 122 | |
| 123 bool EditSearchEngineDialog::HandleKeyEvent( | |
| 124 Textfield* sender, | |
| 125 const ui::KeyEvent& key_event) { | |
| 126 return false; | |
| 127 } | |
| 128 | |
| 129 void EditSearchEngineDialog::Init() { | |
| 130 // Create the views we'll need. | |
| 131 if (controller_->template_url()) { | |
| 132 title_tf_ = CreateTextfield(controller_->template_url()->short_name()); | |
| 133 keyword_tf_ = CreateTextfield(controller_->template_url()->keyword()); | |
| 134 url_tf_ = CreateTextfield( | |
| 135 controller_->template_url()->url_ref().DisplayURL( | |
| 136 UIThreadSearchTermsData(controller_->profile()))); | |
| 137 // We don't allow users to edit prepopulate URLs. This is done as | |
| 138 // occasionally we need to update the URL of prepopulated TemplateURLs. | |
| 139 url_tf_->SetReadOnly(controller_->template_url()->prepopulate_id() != 0); | |
| 140 } else { | |
| 141 title_tf_ = CreateTextfield(base::string16()); | |
| 142 keyword_tf_ = CreateTextfield(base::string16()); | |
| 143 url_tf_ = CreateTextfield(base::string16()); | |
| 144 } | |
| 145 title_iv_ = new views::ImageView(); | |
| 146 keyword_iv_ = new views::ImageView(); | |
| 147 url_iv_ = new views::ImageView(); | |
| 148 | |
| 149 UpdateImageViews(); | |
| 150 | |
| 151 const int related_x = views::kRelatedControlHorizontalSpacing; | |
| 152 const int related_y = views::kRelatedControlVerticalSpacing; | |
| 153 const int unrelated_y = views::kUnrelatedControlVerticalSpacing; | |
| 154 | |
| 155 // View and GridLayout take care of deleting GridLayout for us. | |
| 156 GridLayout* layout = GridLayout::CreatePanel(this); | |
| 157 SetLayoutManager(layout); | |
| 158 | |
| 159 // Define the structure of the layout. | |
| 160 | |
| 161 // For the buttons. | |
| 162 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
| 163 column_set->AddPaddingColumn(1, 0); | |
| 164 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | |
| 165 GridLayout::USE_PREF, 0, 0); | |
| 166 column_set->AddPaddingColumn(0, related_x); | |
| 167 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | |
| 168 GridLayout::USE_PREF, 0, 0); | |
| 169 column_set->LinkColumnSizes(1, 3, -1); | |
| 170 | |
| 171 // For the Textfields. | |
| 172 column_set = layout->AddColumnSet(1); | |
| 173 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, | |
| 174 GridLayout::USE_PREF, 0, 0); | |
| 175 column_set->AddPaddingColumn(0, related_x); | |
| 176 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, | |
| 177 GridLayout::USE_PREF, 0, 0); | |
| 178 column_set->AddPaddingColumn(0, related_x); | |
| 179 column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, | |
| 180 GridLayout::USE_PREF, 0, 0); | |
| 181 | |
| 182 // For the description. | |
| 183 column_set = layout->AddColumnSet(2); | |
| 184 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | |
| 185 GridLayout::USE_PREF, 0, 0); | |
| 186 | |
| 187 // Add the contents. | |
| 188 layout->StartRow(0, 1); | |
| 189 layout->AddView(CreateLabel(IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_LABEL)); | |
| 190 layout->AddView(title_tf_); | |
| 191 layout->AddView(title_iv_); | |
| 192 | |
| 193 layout->StartRowWithPadding(0, 1, 0, related_y); | |
| 194 layout->AddView(CreateLabel(IDS_SEARCH_ENGINES_EDITOR_KEYWORD_LABEL)); | |
| 195 layout->AddView(keyword_tf_); | |
| 196 layout->AddView(keyword_iv_); | |
| 197 | |
| 198 layout->StartRowWithPadding(0, 1, 0, related_y); | |
| 199 layout->AddView(CreateLabel(IDS_SEARCH_ENGINES_EDITOR_URL_LABEL)); | |
| 200 layout->AddView(url_tf_); | |
| 201 layout->AddView(url_iv_); | |
| 202 | |
| 203 // On RTL UIs (such as Arabic and Hebrew) the description text is not | |
| 204 // displayed correctly since it contains the substring "%s". This substring | |
| 205 // is not interpreted by the Unicode BiDi algorithm as an LTR string and | |
| 206 // therefore the end result is that the following right to left text is | |
| 207 // displayed: ".three two s% one" (where 'one', 'two', etc. are words in | |
| 208 // Hebrew). | |
| 209 // | |
| 210 // In order to fix this problem we transform the substring "%s" so that it | |
| 211 // is displayed correctly when rendered in an RTL context. | |
| 212 layout->StartRowWithPadding(0, 2, 0, unrelated_y); | |
| 213 base::string16 description = l10n_util::GetStringUTF16( | |
| 214 IDS_SEARCH_ENGINES_EDITOR_URL_DESCRIPTION_LABEL); | |
| 215 if (base::i18n::IsRTL()) { | |
| 216 const base::string16 reversed_percent(base::ASCIIToUTF16("s%")); | |
| 217 base::string16::size_type percent_index = | |
| 218 description.find(base::ASCIIToUTF16("%s"), | |
| 219 static_cast<base::string16::size_type>(0)); | |
| 220 if (percent_index != base::string16::npos) | |
| 221 description.replace(percent_index, | |
| 222 reversed_percent.length(), | |
| 223 reversed_percent); | |
| 224 } | |
| 225 | |
| 226 views::Label* description_label = new views::Label(description); | |
| 227 description_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 228 layout->AddView(description_label); | |
| 229 | |
| 230 layout->AddPaddingRow(0, related_y); | |
| 231 } | |
| 232 | |
| 233 views::Label* EditSearchEngineDialog::CreateLabel(int message_id) { | |
| 234 views::Label* label = | |
| 235 new views::Label(l10n_util::GetStringUTF16(message_id)); | |
| 236 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 237 return label; | |
| 238 } | |
| 239 | |
| 240 Textfield* EditSearchEngineDialog::CreateTextfield(const base::string16& text) { | |
| 241 Textfield* text_field = new Textfield(); | |
| 242 text_field->SetText(text); | |
| 243 text_field->set_controller(this); | |
| 244 return text_field; | |
| 245 } | |
| 246 | |
| 247 void EditSearchEngineDialog::UpdateImageViews() { | |
| 248 UpdateImageView(keyword_iv_, controller_->IsKeywordValid(keyword_tf_->text()), | |
| 249 IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT); | |
| 250 UpdateImageView(url_iv_, | |
| 251 controller_->IsURLValid(base::UTF16ToUTF8(url_tf_->text())), | |
| 252 IDS_SEARCH_ENGINES_INVALID_URL_TT); | |
| 253 UpdateImageView(title_iv_, controller_->IsTitleValid(title_tf_->text()), | |
| 254 IDS_SEARCH_ENGINES_INVALID_TITLE_TT); | |
| 255 } | |
| 256 | |
| 257 void EditSearchEngineDialog::UpdateImageView(views::ImageView* image_view, | |
| 258 bool is_valid, | |
| 259 int invalid_message_id) { | |
| 260 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 261 if (is_valid) { | |
| 262 image_view->SetTooltipText(base::string16()); | |
| 263 image_view->SetImage(rb.GetImageSkiaNamed(IDR_INPUT_GOOD)); | |
| 264 } else { | |
| 265 image_view->SetTooltipText(l10n_util::GetStringUTF16(invalid_message_id)); | |
| 266 image_view->SetImage(rb.GetImageSkiaNamed(IDR_INPUT_ALERT)); | |
| 267 } | |
| 268 } | |
| OLD | NEW |