| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/views/edit_keyword_controller.h" | 5 #include "chrome/browser/views/edit_keyword_controller.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/metrics/user_metrics.h" | 10 #include "chrome/browser/metrics/user_metrics.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 74 |
| 75 std::wstring EditKeywordController::GetWindowTitle() const { | 75 std::wstring EditKeywordController::GetWindowTitle() const { |
| 76 return l10n_util::GetString(template_url_ ? | 76 return l10n_util::GetString(template_url_ ? |
| 77 IDS_SEARCH_ENGINES_EDITOR_EDIT_WINDOW_TITLE : | 77 IDS_SEARCH_ENGINES_EDITOR_EDIT_WINDOW_TITLE : |
| 78 IDS_SEARCH_ENGINES_EDITOR_NEW_WINDOW_TITLE); | 78 IDS_SEARCH_ENGINES_EDITOR_NEW_WINDOW_TITLE); |
| 79 } | 79 } |
| 80 | 80 |
| 81 bool EditKeywordController::IsDialogButtonEnabled( | 81 bool EditKeywordController::IsDialogButtonEnabled( |
| 82 MessageBoxFlags::DialogButton button) const { | 82 MessageBoxFlags::DialogButton button) const { |
| 83 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { | 83 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { |
| 84 return (IsKeywordValid() && !title_tf_->GetText().empty() && IsURLValid()); | 84 return (IsKeywordValid() && !title_tf_->text().empty() && IsURLValid()); |
| 85 } | 85 } |
| 86 return true; | 86 return true; |
| 87 } | 87 } |
| 88 | 88 |
| 89 void EditKeywordController::DeleteDelegate() { | 89 void EditKeywordController::DeleteDelegate() { |
| 90 // User canceled the save, delete us. | 90 // User canceled the save, delete us. |
| 91 delete this; | 91 delete this; |
| 92 } | 92 } |
| 93 | 93 |
| 94 bool EditKeywordController::Cancel() { | 94 bool EditKeywordController::Cancel() { |
| 95 CleanUpCancelledAdd(); | 95 CleanUpCancelledAdd(); |
| 96 return true; | 96 return true; |
| 97 } | 97 } |
| 98 | 98 |
| 99 bool EditKeywordController::Accept() { | 99 bool EditKeywordController::Accept() { |
| 100 std::wstring url_string = GetURL(); | 100 std::wstring url_string = GetURL(); |
| 101 DCHECK(!url_string.empty()); | 101 DCHECK(!url_string.empty()); |
| 102 const std::wstring& keyword = keyword_tf_->GetText(); | 102 const std::wstring& keyword = keyword_tf_->text(); |
| 103 | 103 |
| 104 const TemplateURL* existing = | 104 const TemplateURL* existing = |
| 105 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword); | 105 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword); |
| 106 if (existing && | 106 if (existing && |
| 107 (!keyword_editor_view_ || existing != template_url_)) { | 107 (!keyword_editor_view_ || existing != template_url_)) { |
| 108 // An entry may have been added with the same keyword string while the | 108 // An entry may have been added with the same keyword string while the |
| 109 // user edited the dialog, either automatically or by the user (if we're | 109 // user edited the dialog, either automatically or by the user (if we're |
| 110 // confirming a JS addition, they could have the Options dialog open at the | 110 // confirming a JS addition, they could have the Options dialog open at the |
| 111 // same time). If so, just ignore this add. | 111 // same time). If so, just ignore this add. |
| 112 // TODO(pamg): Really, we should modify the entry so this later one | 112 // TODO(pamg): Really, we should modify the entry so this later one |
| 113 // overwrites it. But we don't expect this case to be common. | 113 // overwrites it. But we don't expect this case to be common. |
| 114 CleanUpCancelledAdd(); | 114 CleanUpCancelledAdd(); |
| 115 return true; | 115 return true; |
| 116 } | 116 } |
| 117 | 117 |
| 118 if (!keyword_editor_view_) { | 118 if (!keyword_editor_view_) { |
| 119 // Confiming an entry we got from JS. We have a template_url_, but it | 119 // Confiming an entry we got from JS. We have a template_url_, but it |
| 120 // hasn't yet been added to the model. | 120 // hasn't yet been added to the model. |
| 121 DCHECK(template_url_); | 121 DCHECK(template_url_); |
| 122 // const_cast is ugly, but this is the same thing the TemplateURLModel | 122 // const_cast is ugly, but this is the same thing the TemplateURLModel |
| 123 // does in a similar situation (updating an existing TemplateURL with | 123 // does in a similar situation (updating an existing TemplateURL with |
| 124 // data from a new one). | 124 // data from a new one). |
| 125 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url_); | 125 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url_); |
| 126 modifiable_url->set_short_name(title_tf_->GetText()); | 126 modifiable_url->set_short_name(title_tf_->text()); |
| 127 modifiable_url->set_keyword(keyword); | 127 modifiable_url->set_keyword(keyword); |
| 128 modifiable_url->SetURL(url_string, 0, 0); | 128 modifiable_url->SetURL(url_string, 0, 0); |
| 129 // TemplateURLModel takes ownership of template_url_. | 129 // TemplateURLModel takes ownership of template_url_. |
| 130 profile_->GetTemplateURLModel()->Add(modifiable_url); | 130 profile_->GetTemplateURLModel()->Add(modifiable_url); |
| 131 UserMetrics::RecordAction(L"KeywordEditor_AddKeywordJS", profile_); | 131 UserMetrics::RecordAction(L"KeywordEditor_AddKeywordJS", profile_); |
| 132 } else if (!template_url_) { | 132 } else if (!template_url_) { |
| 133 // Adding a new entry via the KeywordEditorView. | 133 // Adding a new entry via the KeywordEditorView. |
| 134 DCHECK(keyword_editor_view_); | 134 DCHECK(keyword_editor_view_); |
| 135 if (keyword_editor_view_) | 135 if (keyword_editor_view_) |
| 136 keyword_editor_view_->AddTemplateURL(title_tf_->GetText(), | 136 keyword_editor_view_->AddTemplateURL(title_tf_->text(), |
| 137 keyword_tf_->GetText(), | 137 keyword_tf_->text(), |
| 138 url_string); | 138 url_string); |
| 139 } else { | 139 } else { |
| 140 // Modifying an entry via the KeywordEditorView. | 140 // Modifying an entry via the KeywordEditorView. |
| 141 DCHECK(keyword_editor_view_); | 141 DCHECK(keyword_editor_view_); |
| 142 if (keyword_editor_view_) { | 142 if (keyword_editor_view_) { |
| 143 keyword_editor_view_->ModifyTemplateURL(template_url_, | 143 keyword_editor_view_->ModifyTemplateURL(template_url_, |
| 144 title_tf_->GetText(), | 144 title_tf_->text(), |
| 145 keyword_tf_->GetText(), | 145 keyword_tf_->text(), |
| 146 url_string); | 146 url_string); |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 return true; | 149 return true; |
| 150 } | 150 } |
| 151 | 151 |
| 152 views::View* EditKeywordController::GetContentsView() { | 152 views::View* EditKeywordController::GetContentsView() { |
| 153 return view_; | 153 return view_; |
| 154 } | 154 } |
| 155 | 155 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 297 |
| 298 // If the url has a search term, replace it with a random string and make | 298 // If the url has a search term, replace it with a random string and make |
| 299 // sure the resulting URL is valid. We don't check the validity of the url | 299 // sure the resulting URL is valid. We don't check the validity of the url |
| 300 // with the search term as that is not necessarily valid. | 300 // with the search term as that is not necessarily valid. |
| 301 return template_ref.ReplaceSearchTerms(TemplateURL(), L"a", | 301 return template_ref.ReplaceSearchTerms(TemplateURL(), L"a", |
| 302 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()).is_valid(); | 302 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()).is_valid(); |
| 303 } | 303 } |
| 304 | 304 |
| 305 std::wstring EditKeywordController::GetURL() const { | 305 std::wstring EditKeywordController::GetURL() const { |
| 306 std::wstring url; | 306 std::wstring url; |
| 307 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(url_tf_->GetText()), | 307 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(url_tf_->text()), |
| 308 TRIM_ALL, &url); | 308 TRIM_ALL, &url); |
| 309 if (url.empty()) | 309 if (url.empty()) |
| 310 return url; | 310 return url; |
| 311 | 311 |
| 312 // Parse the string as a URL to determine the scheme. If a scheme hasn't been | 312 // Parse the string as a URL to determine the scheme. If a scheme hasn't been |
| 313 // specified, we add it. | 313 // specified, we add it. |
| 314 url_parse::Parsed parts; | 314 url_parse::Parsed parts; |
| 315 std::wstring scheme(URLFixerUpper::SegmentURL(url, &parts)); | 315 std::wstring scheme(URLFixerUpper::SegmentURL(url, &parts)); |
| 316 if (!parts.scheme.is_valid()) { | 316 if (!parts.scheme.is_valid()) { |
| 317 std::wstring fixed_scheme(scheme); | 317 std::wstring fixed_scheme(scheme); |
| 318 fixed_scheme.append(L"://"); | 318 fixed_scheme.append(L"://"); |
| 319 url.insert(0, fixed_scheme); | 319 url.insert(0, fixed_scheme); |
| 320 } | 320 } |
| 321 | 321 |
| 322 return url; | 322 return url; |
| 323 } | 323 } |
| 324 | 324 |
| 325 bool EditKeywordController::IsKeywordValid() const { | 325 bool EditKeywordController::IsKeywordValid() const { |
| 326 std::wstring keyword = keyword_tf_->GetText(); | 326 std::wstring keyword = keyword_tf_->text(); |
| 327 if (keyword.empty()) | 327 if (keyword.empty()) |
| 328 return true; // Always allow no keyword. | 328 return true; // Always allow no keyword. |
| 329 const TemplateURL* turl_with_keyword = | 329 const TemplateURL* turl_with_keyword = |
| 330 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword); | 330 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword); |
| 331 return (turl_with_keyword == NULL || turl_with_keyword == template_url_); | 331 return (turl_with_keyword == NULL || turl_with_keyword == template_url_); |
| 332 } | 332 } |
| 333 | 333 |
| 334 void EditKeywordController::UpdateImageViews() { | 334 void EditKeywordController::UpdateImageViews() { |
| 335 UpdateImageView(keyword_iv_, IsKeywordValid(), | 335 UpdateImageView(keyword_iv_, IsKeywordValid(), |
| 336 IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT); | 336 IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT); |
| 337 UpdateImageView(url_iv_, IsURLValid(), IDS_SEARCH_ENGINES_INVALID_URL_TT); | 337 UpdateImageView(url_iv_, IsURLValid(), IDS_SEARCH_ENGINES_INVALID_URL_TT); |
| 338 UpdateImageView(title_iv_, !title_tf_->GetText().empty(), | 338 UpdateImageView(title_iv_, !title_tf_->text().empty(), |
| 339 IDS_SEARCH_ENGINES_INVALID_TITLE_TT); | 339 IDS_SEARCH_ENGINES_INVALID_TITLE_TT); |
| 340 } | 340 } |
| 341 | 341 |
| 342 void EditKeywordController::UpdateImageView(ImageView* image_view, | 342 void EditKeywordController::UpdateImageView(ImageView* image_view, |
| 343 bool is_valid, | 343 bool is_valid, |
| 344 int invalid_message_id) { | 344 int invalid_message_id) { |
| 345 if (is_valid) { | 345 if (is_valid) { |
| 346 image_view->SetTooltipText(std::wstring()); | 346 image_view->SetTooltipText(std::wstring()); |
| 347 image_view->SetImage( | 347 image_view->SetImage( |
| 348 ResourceBundle::GetSharedInstance().GetBitmapNamed( | 348 ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 349 IDR_INPUT_GOOD)); | 349 IDR_INPUT_GOOD)); |
| 350 } else { | 350 } else { |
| 351 image_view->SetTooltipText(l10n_util::GetString(invalid_message_id)); | 351 image_view->SetTooltipText(l10n_util::GetString(invalid_message_id)); |
| 352 image_view->SetImage( | 352 image_view->SetImage( |
| 353 ResourceBundle::GetSharedInstance().GetBitmapNamed( | 353 ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 354 IDR_INPUT_ALERT)); | 354 IDR_INPUT_ALERT)); |
| 355 } | 355 } |
| 356 } | 356 } |
| 357 | 357 |
| 358 void EditKeywordController::CleanUpCancelledAdd() { | 358 void EditKeywordController::CleanUpCancelledAdd() { |
| 359 if (!keyword_editor_view_ && template_url_) { | 359 if (!keyword_editor_view_ && template_url_) { |
| 360 // When we have no KeywordEditorView, we know that the template_url_ | 360 // When we have no KeywordEditorView, we know that the template_url_ |
| 361 // hasn't yet been added to the model, so we need to clean it up. | 361 // hasn't yet been added to the model, so we need to clean it up. |
| 362 delete template_url_; | 362 delete template_url_; |
| 363 template_url_ = NULL; | 363 template_url_ = NULL; |
| 364 } | 364 } |
| 365 } | 365 } |
| OLD | NEW |