1// Copyright (c) 2009 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#ifndef CHROME_BROWSER_LANGUAGE_COMBOBOX_MODEL_H_
6#define CHROME_BROWSER_LANGUAGE_COMBOBOX_MODEL_H_
7#pragma once
8
9#include <map>
10#include <string>
11#include <vector>
12
13#include "base/basictypes.h"
14#include "base/string16.h"
15#include "ui/base/models/combobox_model.h"
16
17class Profile;
18
19///////////////////////////////////////////////////////////////////////////////
20// LanguageList
21//  Provides code to enumerate locale names for language selection lists.
22//  To be used by combobox, menu or other models.
23class LanguageList {
24 public:
25  struct LocaleData {
26    LocaleData() { }
27    LocaleData(const string16& name, const std::string& code)
28        : native_name(name), locale_code(code) { }
29
30    string16 native_name;
31    std::string locale_code;  // E.g., en-us.
32  };
33  typedef std::map<string16, LocaleData> LocaleDataMap;
34
35  LanguageList();
36
37  explicit LanguageList(const std::vector<std::string>& locale_codes);
38
39  virtual ~LanguageList();
40
41  // Duplicates specified languages at the beginning of the list for
42  // easier access.
43  void CopySpecifiedLanguagesUp(const std::string& locale_codes);
44
45  int get_languages_count() const;
46
47  string16 GetLanguageNameAt(int index) const;
48
49  // Return the locale for the given index.  E.g., may return pt-BR.
50  std::string GetLocaleFromIndex(int index) const;
51
52  // Returns the index for the given locale.  Returns -1 if the locale is not
53  // in the combobox model.
54  int GetIndexFromLocale(const std::string& locale) const;
55
56 private:
57  // The names of all the locales in the current application locale.
58  std::vector<string16> locale_names_;
59
60  // A map of some extra data (LocaleData) keyed off the name of the locale.
61  LocaleDataMap native_names_;
62
63  void InitNativeNames(const std::vector<std::string>& locale_codes);
64
65  DISALLOW_COPY_AND_ASSIGN(LanguageList);
66};
67
68///////////////////////////////////////////////////////////////////////////////
69// LanguageComboboxModel
70//  The combobox model implementation.
71class LanguageComboboxModel : public LanguageList, public ui::ComboboxModel {
72 public:
73  LanguageComboboxModel();
74
75  // Temporary compatibility constructor.
76  LanguageComboboxModel(Profile* profile,
77                        const std::vector<std::string>& locale_codes);
78
79  virtual ~LanguageComboboxModel();
80
81  virtual int GetItemCount();
82  virtual string16 GetItemAt(int index);
83
84  // Returns the index of the language currently specified in the user's
85  // preference file.  Note that it's possible for language A to be picked
86  // while chrome is currently in language B if the user specified language B
87  // via --lang.  Since --lang is not a persistent setting, it seems that it
88  // shouldn't be reflected in this combo box.  We return -1 if the value in
89  // the pref doesn't map to a know language (possible if the user edited the
90  // prefs file manually).
91  int GetSelectedLanguageIndex(const std::string& prefs);
92
93 private:
94  // Profile.
95  Profile* profile_;
96
97  DISALLOW_COPY_AND_ASSIGN(LanguageComboboxModel);
98};
99
100#endif  // CHROME_BROWSER_LANGUAGE_COMBOBOX_MODEL_H_
101