country_combobox_model.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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/country_combobox_model.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/browser_process.h"
9#include "components/autofill/core/browser/autofill_country.h"
10#include "ui/base/l10n/l10n_util_collator.h"
11
12namespace autofill {
13
14CountryComboboxModel::CountryComboboxModel() {
15  // Insert the default country at the top as well as in the ordered list.
16  std::string app_locale = g_browser_process->GetApplicationLocale();
17  std::string default_country_code =
18      AutofillCountry::CountryCodeForLocale(app_locale);
19  countries_.push_back(new AutofillCountry(default_country_code, app_locale));
20  // The separator item.
21  countries_.push_back(NULL);
22
23  // The sorted list of countries.
24  std::vector<std::string> country_codes;
25  AutofillCountry::GetAvailableCountries(&country_codes);
26  std::vector<AutofillCountry*> sorted_countries;
27
28  for (std::vector<std::string>::iterator iter = country_codes.begin();
29       iter != country_codes.end(); ++iter) {
30    sorted_countries.push_back(new AutofillCountry(*iter, app_locale));
31  }
32
33  l10n_util::SortStringsUsingMethod(app_locale,
34                                    &sorted_countries,
35                                    &AutofillCountry::name);
36  countries_.insert(countries_.end(),
37                    sorted_countries.begin(),
38                    sorted_countries.end());
39}
40
41CountryComboboxModel::~CountryComboboxModel() {}
42
43int CountryComboboxModel::GetItemCount() const {
44  return countries_.size();
45}
46
47string16 CountryComboboxModel::GetItemAt(int index) {
48  AutofillCountry* country = countries_[index];
49  if (country)
50    return countries_[index]->name();
51
52  // The separator item. Implemented for platforms that don't yet support
53  // IsItemSeparatorAt().
54  return ASCIIToUTF16("---");
55}
56
57bool CountryComboboxModel::IsItemSeparatorAt(int index) {
58  return !countries_[index];
59}
60
61}  // namespace autofill
62