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/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"
17
18 namespace autofill {
19
~SuggestionsMenuModelDelegate()20 SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
21
22 // SuggestionsMenuModel ----------------------------------------------------
23
SuggestionsMenuModel(SuggestionsMenuModelDelegate * delegate)24 SuggestionsMenuModel::SuggestionsMenuModel(
25 SuggestionsMenuModelDelegate* delegate)
26 : ui::SimpleMenuModel(this),
27 delegate_(delegate),
28 checked_item_(0) {}
29
~SuggestionsMenuModel()30 SuggestionsMenuModel::~SuggestionsMenuModel() {}
31
AddKeyedItem(const std::string & key,const base::string16 & display_label)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);
37 }
38
AddKeyedItemWithIcon(const std::string & key,const base::string16 & display_label,const gfx::Image & icon)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);
45 }
46
AddKeyedItemWithMinorText(const std::string & key,const base::string16 & display_label,const base::string16 & display_minor_text)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);
53 }
54
AddKeyedItemWithMinorTextAndIcon(const std::string & key,const base::string16 & display_label,const base::string16 & display_minor_text,const gfx::Image & icon)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);
62 }
63
Reset()64 void SuggestionsMenuModel::Reset() {
65 checked_item_ = 0;
66 items_.clear();
67 Clear();
68 }
69
GetItemKeyAt(int index) const70 std::string SuggestionsMenuModel::GetItemKeyAt(int index) const {
71 return items_[index].key;
72 }
73
GetItemKeyForCheckedItem() const74 std::string SuggestionsMenuModel::GetItemKeyForCheckedItem() const {
75 if (items_.empty())
76 return std::string();
77
78 return items_[checked_item_].key;
79 }
80
SetCheckedItem(const std::string & 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) {
84 if (IsEnabledAt(i))
85 checked_item_ = i;
86 break;
87 }
88 }
89 }
90
SetCheckedIndex(size_t index)91 void SuggestionsMenuModel::SetCheckedIndex(size_t index) {
92 DCHECK_LT(index, items_.size());
93 checked_item_ = index;
94 }
95
SetEnabled(const std::string & item_key,bool enabled)96 void SuggestionsMenuModel::SetEnabled(const std::string& item_key,
97 bool enabled) {
98 items_[GetItemIndex(item_key)].enabled = enabled;
99 }
100
IsCommandIdChecked(int command_id) const101 bool SuggestionsMenuModel::IsCommandIdChecked(
102 int command_id) const {
103 return checked_item_ == command_id;
104 }
105
IsCommandIdEnabled(int command_id) const106 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;
112 }
113
GetAcceleratorForCommandId(int command_id,ui::Accelerator * accelerator)114 bool SuggestionsMenuModel::GetAcceleratorForCommandId(
115 int command_id,
116 ui::Accelerator* accelerator) {
117 return false;
118 }
119
ExecuteCommand(int command_id,int event_flags)120 void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
121 delegate_->SuggestionItemSelected(this, command_id);
122 }
123
GetItemIndex(const std::string & item_key)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)
127 return i;
128 }
129
130 NOTREACHED();
131 return 0;
132 }
133
134 // MonthComboboxModel ----------------------------------------------------------
135
MonthComboboxModel()136 MonthComboboxModel::MonthComboboxModel() {}
137
~MonthComboboxModel()138 MonthComboboxModel::~MonthComboboxModel() {}
139
GetItemCount() const140 int MonthComboboxModel::GetItemCount() const {
141 // 12 months plus the empty entry.
142 return 13;
143 }
144
145 // static
FormatMonth(int index)146 base::string16 MonthComboboxModel::FormatMonth(int index) {
147 return base::ASCIIToUTF16(base::StringPrintf("%.2d", index));
148 }
149
GetItemAt(int index)150 base::string16 MonthComboboxModel::GetItemAt(int index) {
151 return index == 0 ?
152 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) :
153 FormatMonth(index);
154 }
155
156 // YearComboboxModel -----------------------------------------------------------
157
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;
163 }
164
~YearComboboxModel()165 YearComboboxModel::~YearComboboxModel() {}
166
GetItemCount() const167 int YearComboboxModel::GetItemCount() const {
168 // 10 years plus the empty entry.
169 return 11;
170 }
171
GetItemAt(int index)172 base::string16 YearComboboxModel::GetItemAt(int index) {
173 if (index == 0) {
174 return l10n_util::GetStringUTF16(
175 IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR);
176 }
177
178 return base::IntToString16(this_year_ + index - 1);
179 }
180
181 } // namespace autofill
182