summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/autofill/autofill_dialog_models.cc
blob: dbe26f235d771da85603f42499d9e26ee2398a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/autofill/autofill_dialog_models.h"

#include "base/bind.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/common/pref_names.h"
#include "components/autofill/core/browser/autofill_country.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace autofill {

SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}

// SuggestionsMenuModel ----------------------------------------------------

SuggestionsMenuModel::SuggestionsMenuModel(
    SuggestionsMenuModelDelegate* delegate)
    : ui::SimpleMenuModel(this),
      delegate_(delegate),
      checked_item_(0) {}

SuggestionsMenuModel::~SuggestionsMenuModel() {}

void SuggestionsMenuModel::AddKeyedItem(
    const std::string& key, const base::string16& display_label) {
  Item item = { key, true };
  items_.push_back(item);
  AddCheckItem(items_.size() - 1, display_label);
}

void SuggestionsMenuModel::AddKeyedItemWithIcon(
    const std::string& key,
    const base::string16& display_label,
    const gfx::Image& icon) {
  AddKeyedItem(key, display_label);
  SetIcon(items_.size() - 1, icon);
}

void SuggestionsMenuModel::AddKeyedItemWithMinorText(
    const std::string& key,
    const base::string16& display_label,
    const base::string16& display_minor_text) {
  AddKeyedItem(key, display_label);
  SetMinorText(items_.size() - 1, display_minor_text);
}

void SuggestionsMenuModel::AddKeyedItemWithMinorTextAndIcon(
    const std::string& key,
    const base::string16& display_label,
    const base::string16& display_minor_text,
    const gfx::Image& icon) {
  AddKeyedItemWithIcon(key, display_label, icon);
  SetMinorText(items_.size() - 1, display_minor_text);
}

void SuggestionsMenuModel::Reset() {
  checked_item_ = 0;
  items_.clear();
  Clear();
}

std::string SuggestionsMenuModel::GetItemKeyAt(int index) const {
  return items_[index].key;
}

std::string SuggestionsMenuModel::GetItemKeyForCheckedItem() const {
  if (items_.empty())
    return std::string();

  return items_[checked_item_].key;
}

void SuggestionsMenuModel::SetCheckedItem(const std::string& item_key) {
  for (size_t i = 0; i < items_.size(); ++i) {
    if (items_[i].key == item_key) {
      if (IsEnabledAt(i))
        checked_item_ = i;
      break;
    }
  }
}

void SuggestionsMenuModel::SetCheckedIndex(size_t index) {
  DCHECK_LT(index, items_.size());
  checked_item_ = index;
}

void SuggestionsMenuModel::SetEnabled(const std::string& item_key,
                                      bool enabled) {
  items_[GetItemIndex(item_key)].enabled = enabled;
}

bool SuggestionsMenuModel::IsCommandIdChecked(
    int command_id) const {
  return checked_item_ == command_id;
}

bool SuggestionsMenuModel::IsCommandIdEnabled(
    int command_id) const {
  // Please note: command_id is same as the 0-based index in |items_|.
  DCHECK_GE(command_id, 0);
  DCHECK_LT(static_cast<size_t>(command_id), items_.size());
  return items_[command_id].enabled;
}

bool SuggestionsMenuModel::GetAcceleratorForCommandId(
    int command_id,
    ui::Accelerator* accelerator) {
  return false;
}

void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
  delegate_->SuggestionItemSelected(this, command_id);
}

size_t SuggestionsMenuModel::GetItemIndex(const std::string& item_key) {
  for (size_t i = 0; i < items_.size(); ++i) {
    if (items_[i].key == item_key)
      return i;
  }

  NOTREACHED();
  return 0;
}

// MonthComboboxModel ----------------------------------------------------------

MonthComboboxModel::MonthComboboxModel() {}

MonthComboboxModel::~MonthComboboxModel() {}

int MonthComboboxModel::GetItemCount() const {
  // 12 months plus the empty entry.
  return 13;
}

// static
base::string16 MonthComboboxModel::FormatMonth(int index) {
  return base::ASCIIToUTF16(base::StringPrintf("%.2d", index));
}

base::string16 MonthComboboxModel::GetItemAt(int index) {
  return index == 0 ?
      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) :
      FormatMonth(index);
}

// YearComboboxModel -----------------------------------------------------------

YearComboboxModel::YearComboboxModel() : this_year_(0) {
  base::Time time = base::Time::Now();
  base::Time::Exploded exploded;
  time.LocalExplode(&exploded);
  this_year_ = exploded.year;
}

YearComboboxModel::~YearComboboxModel() {}

int YearComboboxModel::GetItemCount() const {
  // 10 years plus the empty entry.
  return 11;
}

base::string16 YearComboboxModel::GetItemAt(int index) {
  if (index == 0) {
    return l10n_util::GetStringUTF16(
        IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR);
  }

  return base::IntToString16(this_year_ + index - 1);
}

}  // namespace autofill