autofill_dialog_models.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
19258b6bc66e09368ada54001f619d53b4fc976d5ager@chromium.org// Copyright (c) 2012 The Chromium Authors. All rights reserved.
29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Use of this source code is governed by a BSD-style license that can be
39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// found in the LICENSE file.
49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "chrome/browser/ui/autofill/autofill_dialog_models.h"
69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/bind.h"
89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/prefs/pref_service.h"
99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/stringprintf.h"
109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/strings/string_number_conversions.h"
119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/time.h"
129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/utf_string_conversions.h"
139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "chrome/common/pref_names.h"
149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "components/autofill/browser/autofill_country.h"
159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "grit/generated_resources.h"
169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "grit/theme_resources.h"
179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "ui/base/l10n/l10n_util.h"
189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "ui/base/resource/resource_bundle.h"
199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comnamespace autofill {
219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comSuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// SuggestionsMenuModel ----------------------------------------------------
259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comSuggestionsMenuModel::SuggestionsMenuModel(
279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    SuggestionsMenuModelDelegate* delegate)
289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    : ui::SimpleMenuModel(this),
299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      delegate_(delegate),
309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      checked_item_(0) {}
319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
32b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.orgSuggestionsMenuModel::~SuggestionsMenuModel() {}
33b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org
34b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.orgvoid SuggestionsMenuModel::AddKeyedItem(
35b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org    const std::string& key, const string16& item) {
36b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org  items_.push_back(std::make_pair(key, item));
37b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org  AddCheckItem(items_.size() - 1, item);
38b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org}
39b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org
40b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.orgvoid SuggestionsMenuModel::AddKeyedItemWithIcon(
41b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org    const std::string& key, const string16& item, const gfx::Image& icon) {
42b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org  AddKeyedItem(key, item);
43b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org  SetIcon(items_.size() - 1, icon);
44b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org}
459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvoid SuggestionsMenuModel::AddKeyedItemWithSublabel(
479a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    const std::string& key,
489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    const string16& display_label, const string16& display_sublabel) {
499a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  AddKeyedItem(key, display_label);
509a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  SetSublabel(items_.size() - 1, display_sublabel);
519a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
529a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
539a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvoid SuggestionsMenuModel::AddKeyedItemWithSublabelAndIcon(
549a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    const std::string& key,
559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    const string16& display_label, const string16& display_sublabel,
569a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    const gfx::Image& icon) {
579a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  AddKeyedItemWithIcon(key, display_label, icon);
589a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  SetSublabel(items_.size() - 1, display_sublabel);
599a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
609a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
619a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvoid SuggestionsMenuModel::Reset() {
629a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  checked_item_ = 0;
639a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  items_.clear();
649a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  Clear();
6525156ded31ef771a2d799ed902483d83b3ebcbdclrn@chromium.org}
6625156ded31ef771a2d799ed902483d83b3ebcbdclrn@chromium.org
6725156ded31ef771a2d799ed902483d83b3ebcbdclrn@chromium.orgstd::string SuggestionsMenuModel::GetItemKeyAt(int index) const {
6825156ded31ef771a2d799ed902483d83b3ebcbdclrn@chromium.org  return items_[index].first;
699a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
709a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstd::string SuggestionsMenuModel::GetItemKeyForCheckedItem() const {
729a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  if (items_.empty())
739a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    return std::string();
749a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
759a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return items_[checked_item_].first;
769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
779a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
789a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvoid SuggestionsMenuModel::SetCheckedItem(const std::string& item_key) {
799a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  for (size_t i = 0; i < items_.size(); ++i) {
80b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org    if (items_[i].first == item_key) {
819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      checked_item_ = i;
829a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      return;
839a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
849a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
859a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
869a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  NOTREACHED();
879a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
889a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
899a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvoid SuggestionsMenuModel::SetCheckedIndex(size_t index) {
909a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  DCHECK_LE(index, items_.size());
919a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  checked_item_ = index;
929a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
939a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
949a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.combool SuggestionsMenuModel::IsCommandIdChecked(
959a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    int command_id) const {
969a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return checked_item_ == command_id;
979a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
989a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
999a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.combool SuggestionsMenuModel::IsCommandIdEnabled(
1009a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    int command_id) const {
1019a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return true;
1029a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1039a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1049a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.combool SuggestionsMenuModel::GetAcceleratorForCommandId(
1059a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    int command_id,
1069a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    ui::Accelerator* accelerator) {
1079a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return false;
1089a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1099a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvoid SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
1119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  delegate_->SuggestionItemSelected(this, command_id);
1129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// MonthComboboxModel ----------------------------------------------------------
1159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comMonthComboboxModel::MonthComboboxModel() {}
1179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comMonthComboboxModel::~MonthComboboxModel() {}
1199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comint MonthComboboxModel::GetItemCount() const {
1219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // 12 months plus the empty entry.
1229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return 13;
1239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// static
1269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstring16 MonthComboboxModel::FormatMonth(int index) {
1279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return ASCIIToUTF16(base::StringPrintf("%.2d", index));
1289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstring16 MonthComboboxModel::GetItemAt(int index) {
1319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return index == 0 ? string16() : FormatMonth(index);
1329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// YearComboboxModel -----------------------------------------------------------
1359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comYearComboboxModel::YearComboboxModel() : this_year_(0) {
1379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  base::Time time = base::Time::Now();
1389a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  base::Time::Exploded exploded;
1399a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  time.LocalExplode(&exploded);
1409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  this_year_ = exploded.year;
1419a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1429a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1439a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comYearComboboxModel::~YearComboboxModel() {}
1449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comint YearComboboxModel::GetItemCount() const {
1469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // 10 years plus the empty entry.
1479a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return 11;
1489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1499a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
150b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.orgstring16 YearComboboxModel::GetItemAt(int index) {
1519a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  if (index == 0)
1529a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    return string16();
1539a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1549a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return base::IntToString16(this_year_ + index - 1);
1559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
15625156ded31ef771a2d799ed902483d83b3ebcbdclrn@chromium.org
157b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org}  // autofill
158b26c50a70863498de657ad44be2cffa49ccdcbeaager@chromium.org