summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search_engines/keyword_editor_controller.cc
blob: b7d2c5e1dc492d0b26cd15964ecb187eda652a23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/search_engines/keyword_editor_controller.h"

#include "base/utf_string_conversions.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/search_engines/template_url_table_model.h"
#include "chrome/common/pref_names.h"

KeywordEditorController::KeywordEditorController(Profile* profile)
    : profile_(profile) {
  table_model_.reset(new TemplateURLTableModel(profile->GetTemplateURLModel()));
}

KeywordEditorController::~KeywordEditorController() {
}

// static
// TODO(rsesek): Other platforms besides Mac should remember window
// placement. http://crbug.com/22269
void KeywordEditorController::RegisterPrefs(PrefService* prefs) {
  prefs->RegisterDictionaryPref(prefs::kKeywordEditorWindowPlacement);
}

int KeywordEditorController::AddTemplateURL(const string16& title,
                                            const string16& keyword,
                                            const std::string& url) {
  DCHECK(!url.empty());

  UserMetrics::RecordAction(UserMetricsAction("KeywordEditor_AddKeyword"),
                            profile_);

  TemplateURL* template_url = new TemplateURL();
  template_url->set_short_name(UTF16ToWideHack(title));
  template_url->set_keyword(UTF16ToWideHack(keyword));
  template_url->SetURL(url, 0, 0);

  // There's a bug (1090726) in TableView with groups enabled such that newly
  // added items in groups ALWAYS appear at the end, regardless of the index
  // passed in. Worse yet, the selected rows get messed up when this happens
  // causing other problems. As a work around we always add the item to the end
  // of the list.
  const int new_index = table_model_->RowCount();
  table_model_->Add(new_index, template_url);

  return new_index;
}

void KeywordEditorController::ModifyTemplateURL(const TemplateURL* template_url,
                                                const string16& title,
                                                const string16& keyword,
                                                const std::string& url) {
  const int index = table_model_->IndexOfTemplateURL(template_url);
  if (index == -1) {
    // Will happen if url was deleted out from under us while the user was
    // editing it.
    return;
  }

  // Don't do anything if the entry didn't change.
  if (template_url->short_name() == UTF16ToWideHack(title) &&
      template_url->keyword() == UTF16ToWideHack(keyword) &&
      ((url.empty() && !template_url->url()) ||
       (!url.empty() && template_url->url() &&
        template_url->url()->url() == url))) {
    return;
  }

  table_model_->ModifyTemplateURL(index, title, keyword, url);

  UserMetrics::RecordAction(UserMetricsAction("KeywordEditor_ModifiedKeyword"),
                            profile_);
}

bool KeywordEditorController::CanEdit(const TemplateURL* url) const {
  return !url_model()->is_default_search_managed() ||
      url != url_model()->GetDefaultSearchProvider();
}

bool KeywordEditorController::CanMakeDefault(const TemplateURL* url) const {
  return url_model()->CanMakeDefault(url);
}

bool KeywordEditorController::CanRemove(const TemplateURL* url) const {
  return url != url_model()->GetDefaultSearchProvider();
}

void KeywordEditorController::RemoveTemplateURL(int index) {
  table_model_->Remove(index);
  UserMetrics::RecordAction(UserMetricsAction("KeywordEditor_RemoveKeyword"),
                            profile_);
}

int KeywordEditorController::MakeDefaultTemplateURL(int index) {
  return table_model_->MakeDefaultTemplateURL(index);
}

bool KeywordEditorController::loaded() const {
  return url_model()->loaded();
}

const TemplateURL* KeywordEditorController::GetTemplateURL(int index) const {
  return &table_model_->GetTemplateURL(index);
}

TemplateURLModel* KeywordEditorController::url_model() const {
  return table_model_->template_url_model();
}