autofill_dialog_models.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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/autofill/autofill_dialog_models.h"
6
7#include "base/bind.h"
8#include "base/prefs/pref_service.h"
9#include "base/strings/string_number_conversions.h"
10#include "base/strings/stringprintf.h"
11#include "base/strings/utf_string_conversions.h"
12#include "base/time.h"
13#include "chrome/common/pref_names.h"
14#include "components/autofill/browser/autofill_country.h"
15#include "grit/generated_resources.h"
16#include "grit/theme_resources.h"
17#include "ui/base/l10n/l10n_util.h"
18#include "ui/base/resource/resource_bundle.h"
19
20namespace autofill {
21
22SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
23
24// SuggestionsMenuModel ----------------------------------------------------
25
26SuggestionsMenuModel::SuggestionsMenuModel(
27    SuggestionsMenuModelDelegate* delegate)
28    : ui::SimpleMenuModel(this),
29      delegate_(delegate),
30      checked_item_(0) {}
31
32SuggestionsMenuModel::~SuggestionsMenuModel() {}
33
34void SuggestionsMenuModel::AddKeyedItem(
35    const std::string& key, const string16& display_label) {
36  Item item = { key, true };
37  items_.push_back(item);
38  AddCheckItem(items_.size() - 1, display_label);
39}
40
41void SuggestionsMenuModel::AddKeyedItemWithIcon(
42    const std::string& key,
43    const string16& display_label,
44    const gfx::Image& icon) {
45  AddKeyedItem(key, display_label);
46  SetIcon(items_.size() - 1, icon);
47}
48
49void SuggestionsMenuModel::AddKeyedItemWithSublabel(
50    const std::string& key,
51    const string16& display_label,
52    const string16& display_sublabel) {
53  AddKeyedItem(key, display_label);
54  SetSublabel(items_.size() - 1, display_sublabel);
55}
56
57void SuggestionsMenuModel::AddKeyedItemWithSublabelAndIcon(
58    const std::string& key,
59    const string16& display_label,
60    const string16& display_sublabel,
61    const gfx::Image& icon) {
62  AddKeyedItemWithIcon(key, display_label, icon);
63  SetSublabel(items_.size() - 1, display_sublabel);
64}
65
66void SuggestionsMenuModel::Reset() {
67  checked_item_ = 0;
68  items_.clear();
69  Clear();
70}
71
72std::string SuggestionsMenuModel::GetItemKeyAt(int index) const {
73  return items_[index].key;
74}
75
76std::string SuggestionsMenuModel::GetItemKeyForCheckedItem() const {
77  if (items_.empty())
78    return std::string();
79
80  return items_[checked_item_].key;
81}
82
83void SuggestionsMenuModel::SetCheckedItem(const std::string& item_key) {
84  SetCheckedItemNthWithKey(item_key, 1);
85}
86
87void SuggestionsMenuModel::SetCheckedIndex(size_t index) {
88  DCHECK_LT(index, items_.size());
89  checked_item_ = index;
90}
91
92void SuggestionsMenuModel::SetCheckedItemNthWithKey(const std::string& item_key,
93                                                    size_t n) {
94  for (size_t i = 0; i < items_.size(); ++i) {
95    if (items_[i].key == item_key) {
96      checked_item_ = i;
97      if (n-- <= 1)
98        return;
99    }
100  }
101}
102
103void SuggestionsMenuModel::SetEnabled(const std::string& item_key,
104                                      bool enabled) {
105  items_[GetItemIndex(item_key)].enabled = enabled;
106}
107
108bool SuggestionsMenuModel::IsCommandIdChecked(
109    int command_id) const {
110  return checked_item_ == command_id;
111}
112
113bool SuggestionsMenuModel::IsCommandIdEnabled(
114    int command_id) const {
115  // Please note: command_id is same as the 0-based index in |items_|.
116  DCHECK_GE(command_id, 0);
117  DCHECK_LT(static_cast<size_t>(command_id), items_.size());
118  return items_[command_id].enabled;
119}
120
121bool SuggestionsMenuModel::GetAcceleratorForCommandId(
122    int command_id,
123    ui::Accelerator* accelerator) {
124  return false;
125}
126
127void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
128  delegate_->SuggestionItemSelected(this, command_id);
129}
130
131size_t SuggestionsMenuModel::GetItemIndex(const std::string& item_key) {
132  for (size_t i = 0; i < items_.size(); ++i) {
133    if (items_[i].key == item_key)
134      return i;
135  }
136
137  NOTREACHED();
138  return 0;
139}
140
141// MonthComboboxModel ----------------------------------------------------------
142
143MonthComboboxModel::MonthComboboxModel() {}
144
145MonthComboboxModel::~MonthComboboxModel() {}
146
147int MonthComboboxModel::GetItemCount() const {
148  // 12 months plus the empty entry.
149  return 13;
150}
151
152// static
153string16 MonthComboboxModel::FormatMonth(int index) {
154  return ASCIIToUTF16(base::StringPrintf("%.2d", index));
155}
156
157string16 MonthComboboxModel::GetItemAt(int index) {
158  return index == 0 ? string16() : FormatMonth(index);
159}
160
161// YearComboboxModel -----------------------------------------------------------
162
163YearComboboxModel::YearComboboxModel() : this_year_(0) {
164  base::Time time = base::Time::Now();
165  base::Time::Exploded exploded;
166  time.LocalExplode(&exploded);
167  this_year_ = exploded.year;
168}
169
170YearComboboxModel::~YearComboboxModel() {}
171
172int YearComboboxModel::GetItemCount() const {
173  // 10 years plus the empty entry.
174  return 11;
175}
176
177string16 YearComboboxModel::GetItemAt(int index) {
178  if (index == 0)
179    return string16();
180
181  return base::IntToString16(this_year_ + index - 1);
182}
183
184}  // autofill
185