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.
5 #include "chrome/browser/ui/autofill/autofill_dialog_models.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/time.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/autofill/core/browser/autofill_country.h"
16 #include "ui/base/l10n/l10n_util.h"
20 SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
22 // SuggestionsMenuModel ----------------------------------------------------
24 SuggestionsMenuModel::SuggestionsMenuModel(
25 SuggestionsMenuModelDelegate* delegate)
26 : ui::SimpleMenuModel(this),
30 SuggestionsMenuModel::~SuggestionsMenuModel() {}
32 void SuggestionsMenuModel::AddKeyedItem(
33 const std::string& key, const base::string16& display_label) {
34 Item item = { key, true };
35 items_.push_back(item);
36 AddCheckItem(items_.size() - 1, display_label);
39 void SuggestionsMenuModel::AddKeyedItemWithIcon(
40 const std::string& key,
41 const base::string16& display_label,
42 const gfx::Image& icon) {
43 AddKeyedItem(key, display_label);
44 SetIcon(items_.size() - 1, icon);
47 void SuggestionsMenuModel::AddKeyedItemWithMinorText(
48 const std::string& key,
49 const base::string16& display_label,
50 const base::string16& display_minor_text) {
51 AddKeyedItem(key, display_label);
52 SetMinorText(items_.size() - 1, display_minor_text);
55 void SuggestionsMenuModel::AddKeyedItemWithMinorTextAndIcon(
56 const std::string& key,
57 const base::string16& display_label,
58 const base::string16& display_minor_text,
59 const gfx::Image& icon) {
60 AddKeyedItemWithIcon(key, display_label, icon);
61 SetMinorText(items_.size() - 1, display_minor_text);
64 void SuggestionsMenuModel::Reset() {
70 std::string SuggestionsMenuModel::GetItemKeyAt(int index) const {
71 return items_[index].key;
74 std::string SuggestionsMenuModel::GetItemKeyForCheckedItem() const {
78 return items_[checked_item_].key;
81 void SuggestionsMenuModel::SetCheckedItem(const std::string& item_key) {
82 for (size_t i = 0; i < items_.size(); ++i) {
83 if (items_[i].key == item_key) {
91 void SuggestionsMenuModel::SetCheckedIndex(size_t index) {
92 DCHECK_LT(index, items_.size());
93 checked_item_ = index;
96 void SuggestionsMenuModel::SetEnabled(const std::string& item_key,
98 items_[GetItemIndex(item_key)].enabled = enabled;
101 bool SuggestionsMenuModel::IsCommandIdChecked(
102 int command_id) const {
103 return checked_item_ == command_id;
106 bool SuggestionsMenuModel::IsCommandIdEnabled(
107 int command_id) const {
108 // Please note: command_id is same as the 0-based index in |items_|.
109 DCHECK_GE(command_id, 0);
110 DCHECK_LT(static_cast<size_t>(command_id), items_.size());
111 return items_[command_id].enabled;
114 bool SuggestionsMenuModel::GetAcceleratorForCommandId(
116 ui::Accelerator* accelerator) {
120 void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
121 delegate_->SuggestionItemSelected(this, command_id);
124 size_t SuggestionsMenuModel::GetItemIndex(const std::string& item_key) {
125 for (size_t i = 0; i < items_.size(); ++i) {
126 if (items_[i].key == item_key)
134 // MonthComboboxModel ----------------------------------------------------------
136 MonthComboboxModel::MonthComboboxModel() {}
138 MonthComboboxModel::~MonthComboboxModel() {}
140 int MonthComboboxModel::GetItemCount() const {
141 // 12 months plus the empty entry.
146 base::string16 MonthComboboxModel::FormatMonth(int index) {
147 return base::ASCIIToUTF16(base::StringPrintf("%.2d", index));
150 base::string16 MonthComboboxModel::GetItemAt(int index) {
152 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) :
156 // YearComboboxModel -----------------------------------------------------------
158 YearComboboxModel::YearComboboxModel() : this_year_(0) {
159 base::Time time = base::Time::Now();
160 base::Time::Exploded exploded;
161 time.LocalExplode(&exploded);
162 this_year_ = exploded.year;
165 YearComboboxModel::~YearComboboxModel() {}
167 int YearComboboxModel::GetItemCount() const {
168 // 10 years plus the empty entry.
172 base::string16 YearComboboxModel::GetItemAt(int index) {
174 return l10n_util::GetStringUTF16(
175 IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR);
178 return base::IntToString16(this_year_ + index - 1);
181 } // namespace autofill