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 "components/autofill/core/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"
22 SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
24 // SuggestionsMenuModel ----------------------------------------------------
26 SuggestionsMenuModel::SuggestionsMenuModel(
27 SuggestionsMenuModelDelegate* delegate)
28 : ui::SimpleMenuModel(this),
32 SuggestionsMenuModel::~SuggestionsMenuModel() {}
34 void SuggestionsMenuModel::AddKeyedItem(
35 const std::string& key, const base::string16& display_label) {
36 Item item = { key, true };
37 items_.push_back(item);
38 AddCheckItem(items_.size() - 1, display_label);
41 void SuggestionsMenuModel::AddKeyedItemWithIcon(
42 const std::string& key,
43 const base::string16& display_label,
44 const gfx::Image& icon) {
45 AddKeyedItem(key, display_label);
46 SetIcon(items_.size() - 1, icon);
49 void SuggestionsMenuModel::AddKeyedItemWithMinorText(
50 const std::string& key,
51 const base::string16& display_label,
52 const base::string16& display_minor_text) {
53 AddKeyedItem(key, display_label);
54 SetMinorText(items_.size() - 1, display_minor_text);
57 void SuggestionsMenuModel::AddKeyedItemWithMinorTextAndIcon(
58 const std::string& key,
59 const base::string16& display_label,
60 const base::string16& display_minor_text,
61 const gfx::Image& icon) {
62 AddKeyedItemWithIcon(key, display_label, icon);
63 SetMinorText(items_.size() - 1, display_minor_text);
66 void SuggestionsMenuModel::Reset() {
72 std::string SuggestionsMenuModel::GetItemKeyAt(int index) const {
73 return items_[index].key;
76 std::string SuggestionsMenuModel::GetItemKeyForCheckedItem() const {
80 return items_[checked_item_].key;
83 void SuggestionsMenuModel::SetCheckedItem(const std::string& item_key) {
84 for (size_t i = 0; i < items_.size(); ++i) {
85 if (items_[i].key == item_key) {
93 void SuggestionsMenuModel::SetCheckedIndex(size_t index) {
94 DCHECK_LT(index, items_.size());
95 checked_item_ = index;
98 void SuggestionsMenuModel::SetEnabled(const std::string& item_key,
100 items_[GetItemIndex(item_key)].enabled = enabled;
103 bool SuggestionsMenuModel::IsCommandIdChecked(
104 int command_id) const {
105 return checked_item_ == command_id;
108 bool SuggestionsMenuModel::IsCommandIdEnabled(
109 int command_id) const {
110 // Please note: command_id is same as the 0-based index in |items_|.
111 DCHECK_GE(command_id, 0);
112 DCHECK_LT(static_cast<size_t>(command_id), items_.size());
113 return items_[command_id].enabled;
116 bool SuggestionsMenuModel::GetAcceleratorForCommandId(
118 ui::Accelerator* accelerator) {
122 void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
123 delegate_->SuggestionItemSelected(this, command_id);
126 size_t SuggestionsMenuModel::GetItemIndex(const std::string& item_key) {
127 for (size_t i = 0; i < items_.size(); ++i) {
128 if (items_[i].key == item_key)
136 // MonthComboboxModel ----------------------------------------------------------
138 MonthComboboxModel::MonthComboboxModel() {}
140 MonthComboboxModel::~MonthComboboxModel() {}
142 int MonthComboboxModel::GetItemCount() const {
143 // 12 months plus the empty entry.
148 base::string16 MonthComboboxModel::FormatMonth(int index) {
149 return base::ASCIIToUTF16(base::StringPrintf("%.2d", index));
152 base::string16 MonthComboboxModel::GetItemAt(int index) {
154 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) :
158 // YearComboboxModel -----------------------------------------------------------
160 YearComboboxModel::YearComboboxModel() : this_year_(0) {
161 base::Time time = base::Time::Now();
162 base::Time::Exploded exploded;
163 time.LocalExplode(&exploded);
164 this_year_ = exploded.year;
167 YearComboboxModel::~YearComboboxModel() {}
169 int YearComboboxModel::GetItemCount() const {
170 // 10 years plus the empty entry.
174 base::string16 YearComboboxModel::GetItemAt(int index) {
176 return l10n_util::GetStringUTF16(
177 IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR);
180 return base::IntToString16(this_year_ + index - 1);
183 } // namespace autofill