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/search_engines/keyword_editor_controller.h" 6 7 #include "base/utf_string_conversions.h" 8 #include "chrome/browser/metrics/user_metrics.h" 9 #include "chrome/browser/prefs/pref_service.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/search_engines/template_url.h" 12 #include "chrome/browser/search_engines/template_url_model.h" 13 #include "chrome/browser/ui/search_engines/template_url_table_model.h" 14 #include "chrome/common/pref_names.h" 15 16 KeywordEditorController::KeywordEditorController(Profile* profile) 17 : profile_(profile) { 18 table_model_.reset(new TemplateURLTableModel(profile->GetTemplateURLModel())); 19 } 20 21 KeywordEditorController::~KeywordEditorController() { 22 } 23 24 // static 25 // TODO(rsesek): Other platforms besides Mac should remember window 26 // placement. http://crbug.com/22269 27 void KeywordEditorController::RegisterPrefs(PrefService* prefs) { 28 prefs->RegisterDictionaryPref(prefs::kKeywordEditorWindowPlacement); 29 } 30 31 int KeywordEditorController::AddTemplateURL(const string16& title, 32 const string16& keyword, 33 const std::string& url) { 34 DCHECK(!url.empty()); 35 36 UserMetrics::RecordAction(UserMetricsAction("KeywordEditor_AddKeyword"), 37 profile_); 38 39 TemplateURL* template_url = new TemplateURL(); 40 template_url->set_short_name(title); 41 template_url->set_keyword(keyword); 42 template_url->SetURL(url, 0, 0); 43 44 // There's a bug (1090726) in TableView with groups enabled such that newly 45 // added items in groups ALWAYS appear at the end, regardless of the index 46 // passed in. Worse yet, the selected rows get messed up when this happens 47 // causing other problems. As a work around we always add the item to the end 48 // of the list. 49 const int new_index = table_model_->RowCount(); 50 table_model_->Add(new_index, template_url); 51 52 return new_index; 53 } 54 55 void KeywordEditorController::ModifyTemplateURL(const TemplateURL* template_url, 56 const string16& title, 57 const string16& keyword, 58 const std::string& url) { 59 const int index = table_model_->IndexOfTemplateURL(template_url); 60 if (index == -1) { 61 // Will happen if url was deleted out from under us while the user was 62 // editing it. 63 return; 64 } 65 66 // Don't do anything if the entry didn't change. 67 if (template_url->short_name() == title && 68 template_url->keyword() == keyword && 69 ((url.empty() && !template_url->url()) || 70 (!url.empty() && template_url->url() && 71 template_url->url()->url() == url))) { 72 return; 73 } 74 75 table_model_->ModifyTemplateURL(index, title, keyword, url); 76 77 UserMetrics::RecordAction(UserMetricsAction("KeywordEditor_ModifiedKeyword"), 78 profile_); 79 } 80 81 bool KeywordEditorController::CanEdit(const TemplateURL* url) const { 82 return !url_model()->is_default_search_managed() || 83 url != url_model()->GetDefaultSearchProvider(); 84 } 85 86 bool KeywordEditorController::CanMakeDefault(const TemplateURL* url) const { 87 return url_model()->CanMakeDefault(url); 88 } 89 90 bool KeywordEditorController::CanRemove(const TemplateURL* url) const { 91 return url != url_model()->GetDefaultSearchProvider(); 92 } 93 94 void KeywordEditorController::RemoveTemplateURL(int index) { 95 table_model_->Remove(index); 96 UserMetrics::RecordAction(UserMetricsAction("KeywordEditor_RemoveKeyword"), 97 profile_); 98 } 99 100 int KeywordEditorController::MakeDefaultTemplateURL(int index) { 101 return table_model_->MakeDefaultTemplateURL(index); 102 } 103 104 bool KeywordEditorController::loaded() const { 105 return url_model()->loaded(); 106 } 107 108 const TemplateURL* KeywordEditorController::GetTemplateURL(int index) const { 109 return &table_model_->GetTemplateURL(index); 110 } 111 112 TemplateURLModel* KeywordEditorController::url_model() const { 113 return table_model_->template_url_model(); 114 } 115