autofill_dialog_models.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 "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" 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::AddKeyedItemWithMinorText( 50 const std::string& key, 51 const string16& display_label, 52 const string16& display_minor_text) { 53 AddKeyedItem(key, display_label); 54 SetMinorText(items_.size() - 1, display_minor_text); 55} 56 57void SuggestionsMenuModel::AddKeyedItemWithMinorTextAndIcon( 58 const std::string& key, 59 const string16& display_label, 60 const string16& display_minor_text, 61 const gfx::Image& icon) { 62 AddKeyedItemWithIcon(key, display_label, icon); 63 SetMinorText(items_.size() - 1, display_minor_text); 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 for (size_t i = 0; i < items_.size(); ++i) { 85 if (items_[i].key == item_key) { 86 checked_item_ = i; 87 break; 88 } 89 } 90} 91 92void SuggestionsMenuModel::SetCheckedIndex(size_t index) { 93 DCHECK_LT(index, items_.size()); 94 checked_item_ = index; 95} 96 97void SuggestionsMenuModel::SetEnabled(const std::string& item_key, 98 bool enabled) { 99 items_[GetItemIndex(item_key)].enabled = enabled; 100} 101 102bool SuggestionsMenuModel::IsCommandIdChecked( 103 int command_id) const { 104 return checked_item_ == command_id; 105} 106 107bool SuggestionsMenuModel::IsCommandIdEnabled( 108 int command_id) const { 109 // Please note: command_id is same as the 0-based index in |items_|. 110 DCHECK_GE(command_id, 0); 111 DCHECK_LT(static_cast<size_t>(command_id), items_.size()); 112 return items_[command_id].enabled; 113} 114 115bool SuggestionsMenuModel::GetAcceleratorForCommandId( 116 int command_id, 117 ui::Accelerator* accelerator) { 118 return false; 119} 120 121void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) { 122 delegate_->SuggestionItemSelected(this, command_id); 123} 124 125size_t SuggestionsMenuModel::GetItemIndex(const std::string& item_key) { 126 for (size_t i = 0; i < items_.size(); ++i) { 127 if (items_[i].key == item_key) 128 return i; 129 } 130 131 NOTREACHED(); 132 return 0; 133} 134 135// MonthComboboxModel ---------------------------------------------------------- 136 137MonthComboboxModel::MonthComboboxModel() {} 138 139MonthComboboxModel::~MonthComboboxModel() {} 140 141int MonthComboboxModel::GetItemCount() const { 142 // 12 months plus the empty entry. 143 return 13; 144} 145 146// static 147string16 MonthComboboxModel::FormatMonth(int index) { 148 return ASCIIToUTF16(base::StringPrintf("%.2d", index)); 149} 150 151string16 MonthComboboxModel::GetItemAt(int index) { 152 return index == 0 ? 153 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) : 154 FormatMonth(index); 155} 156 157// YearComboboxModel ----------------------------------------------------------- 158 159YearComboboxModel::YearComboboxModel() : this_year_(0) { 160 base::Time time = base::Time::Now(); 161 base::Time::Exploded exploded; 162 time.LocalExplode(&exploded); 163 this_year_ = exploded.year; 164} 165 166YearComboboxModel::~YearComboboxModel() {} 167 168int YearComboboxModel::GetItemCount() const { 169 // 10 years plus the empty entry. 170 return 11; 171} 172 173string16 YearComboboxModel::GetItemAt(int index) { 174 if (index == 0) { 175 return l10n_util::GetStringUTF16( 176 IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR); 177 } 178 179 return base::IntToString16(this_year_ + index - 1); 180} 181 182} // namespace autofill 183