This source file includes following definitions.
- checked_item_
- AddKeyedItem
- AddKeyedItemWithIcon
- AddKeyedItemWithMinorText
- AddKeyedItemWithMinorTextAndIcon
- Reset
- GetItemKeyAt
- GetItemKeyForCheckedItem
- SetCheckedItem
- SetCheckedIndex
- SetEnabled
- MenuWillShow
- IsCommandIdChecked
- IsCommandIdEnabled
- GetAcceleratorForCommandId
- ExecuteCommand
- MenuWillShow
- GetItemIndex
- GetItemCount
- FormatMonth
- GetItemAt
- GetItemCount
- GetItemAt
#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"
#include "ui/base/resource/resource_bundle.h"
namespace autofill {
SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
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;
}
void SuggestionsMenuModel::MenuWillShow() {
ui::SimpleMenuModel::MenuWillShow();
}
bool SuggestionsMenuModel::IsCommandIdChecked(
int command_id) const {
return checked_item_ == command_id;
}
bool SuggestionsMenuModel::IsCommandIdEnabled(
int command_id) const {
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);
}
void SuggestionsMenuModel::MenuWillShow(ui::SimpleMenuModel* source) {
delegate_->SuggestionsMenuWillShow();
}
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() {}
int MonthComboboxModel::GetItemCount() const {
return 13;
}
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() : 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 {
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);
}
}