autofill_dialog_models.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/stringprintf.h" 10#include "base/strings/string_number_conversions.h" 11#include "base/time.h" 12#include "base/utf_string_conversions.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 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 29 delegate_(delegate), 30 checked_item_(0) {} 31 32SuggestionsMenuModel::~SuggestionsMenuModel() {} 33 34void SuggestionsMenuModel::AddKeyedItem( 35 const std::string& key, const string16& item) { 36 items_.push_back(std::make_pair(key, item)); 37 AddCheckItem(items_.size() - 1, item); 38} 39 40void SuggestionsMenuModel::AddKeyedItemWithIcon( 41 const std::string& key, const string16& item, const gfx::Image& icon) { 42 AddKeyedItem(key, item); 43 SetIcon(items_.size() - 1, icon); 44} 45 46void SuggestionsMenuModel::AddKeyedItemWithSublabel( 47 const std::string& key, 48 const string16& display_label, const string16& display_sublabel) { 49 AddKeyedItem(key, display_label); 50 SetSublabel(items_.size() - 1, display_sublabel); 51} 52 53void SuggestionsMenuModel::AddKeyedItemWithSublabelAndIcon( 54 const std::string& key, 55 const string16& display_label, const string16& display_sublabel, 56 const gfx::Image& icon) { 57 AddKeyedItemWithIcon(key, display_label, icon); 58 SetSublabel(items_.size() - 1, display_sublabel); 59} 60 61void SuggestionsMenuModel::Reset() { 62 checked_item_ = 0; 63 items_.clear(); 64 Clear(); 65} 66 67std::string SuggestionsMenuModel::GetItemKeyAt(int index) const { 68 return items_[index].first; 69} 70 71std::string SuggestionsMenuModel::GetItemKeyForCheckedItem() const { 72 if (items_.empty()) 73 return std::string(); 74 75 return items_[checked_item_].first; 76} 77 78bool SuggestionsMenuModel::IsCommandIdChecked( 79 int command_id) const { 80 return checked_item_ == command_id; 81} 82 83bool SuggestionsMenuModel::IsCommandIdEnabled( 84 int command_id) const { 85 return true; 86} 87 88bool SuggestionsMenuModel::GetAcceleratorForCommandId( 89 int command_id, 90 ui::Accelerator* accelerator) { 91 return false; 92} 93 94void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) { 95 checked_item_ = command_id; 96 delegate_->SuggestionItemSelected(*this); 97} 98 99// AccountChooserModel --------------------------------------------------------- 100 101const int AccountChooserModel::kWalletItemId = 0; 102const int AccountChooserModel::kAutofillItemId = 1; 103 104AccountChooserModelDelegate::~AccountChooserModelDelegate() {} 105 106AccountChooserModel::AccountChooserModel( 107 AccountChooserModelDelegate* delegate, 108 PrefService* prefs) 109 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 110 account_delegate_(delegate), 111 prefs_(prefs), 112 checked_item_(kWalletItemId), 113 had_wallet_error_(false) { 114 pref_change_registrar_.Init(prefs); 115 pref_change_registrar_.Add( 116 prefs::kAutofillDialogPayWithoutWallet, 117 base::Bind(&AccountChooserModel::PrefChanged, base::Unretained(this))); 118 119 // TODO(estade): proper strings and l10n. 120 AddCheckItem(kWalletItemId, ASCIIToUTF16("Google Wallet")); 121 SetIcon( 122 kWalletItemId, 123 ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON)); 124 AddCheckItemWithStringId(kAutofillItemId, 125 IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET); 126 UpdateCheckmarkFromPref(); 127} 128 129AccountChooserModel::~AccountChooserModel() { 130} 131 132bool AccountChooserModel::IsCommandIdChecked(int command_id) const { 133 return command_id == checked_item_; 134} 135 136bool AccountChooserModel::IsCommandIdEnabled(int command_id) const { 137 if (command_id == kWalletItemId && had_wallet_error_) 138 return false; 139 140 return true; 141} 142 143bool AccountChooserModel::GetAcceleratorForCommandId( 144 int command_id, 145 ui::Accelerator* accelerator) { 146 return false; 147} 148 149void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) { 150 if (checked_item_ == command_id) 151 return; 152 153 checked_item_ = command_id; 154 account_delegate_->AccountChoiceChanged(); 155} 156 157void AccountChooserModel::SetHadWalletError() { 158 had_wallet_error_ = true; 159 checked_item_ = kAutofillItemId; 160 account_delegate_->AccountChoiceChanged(); 161} 162 163bool AccountChooserModel::WalletIsSelected() const { 164 return checked_item_ == kWalletItemId; 165} 166 167void AccountChooserModel::PrefChanged(const std::string& pref) { 168 DCHECK(pref == prefs::kAutofillDialogPayWithoutWallet); 169 UpdateCheckmarkFromPref(); 170 account_delegate_->AccountChoiceChanged(); 171} 172 173void AccountChooserModel::UpdateCheckmarkFromPref() { 174 if (prefs_->GetBoolean(prefs::kAutofillDialogPayWithoutWallet)) 175 checked_item_ = kAutofillItemId; 176 else 177 checked_item_ = kWalletItemId; 178} 179 180// MonthComboboxModel ---------------------------------------------------------- 181 182MonthComboboxModel::MonthComboboxModel() {} 183 184MonthComboboxModel::~MonthComboboxModel() {} 185 186int MonthComboboxModel::GetItemCount() const { 187 // 12 months plus the empty entry. 188 return 13; 189} 190 191// static 192string16 MonthComboboxModel::FormatMonth(int index) { 193 return ASCIIToUTF16(base::StringPrintf("%2d", index)); 194} 195 196string16 MonthComboboxModel::GetItemAt(int index) { 197 return index == 0 ? string16() : FormatMonth(index); 198} 199 200// YearComboboxModel ----------------------------------------------------------- 201 202YearComboboxModel::YearComboboxModel() : this_year_(0) { 203 base::Time time = base::Time::Now(); 204 base::Time::Exploded exploded; 205 time.LocalExplode(&exploded); 206 this_year_ = exploded.year; 207} 208 209YearComboboxModel::~YearComboboxModel() {} 210 211int YearComboboxModel::GetItemCount() const { 212 // 10 years plus the empty entry. 213 return 11; 214} 215 216string16 YearComboboxModel::GetItemAt(int index) { 217 if (index == 0) 218 return string16(); 219 220 return base::IntToString16(this_year_ + index - 1); 221} 222 223} // autofill 224