diff options
-rw-r--r-- | chrome/browser/language_combobox_model.cc | 36 | ||||
-rw-r--r-- | chrome/browser/language_combobox_model.h | 61 |
2 files changed, 64 insertions, 33 deletions
diff --git a/chrome/browser/language_combobox_model.cc b/chrome/browser/language_combobox_model.cc index 922e5b84..bf98442 100644 --- a/chrome/browser/language_combobox_model.cc +++ b/chrome/browser/language_combobox_model.cc @@ -13,25 +13,23 @@ #include "unicode/uloc.h" /////////////////////////////////////////////////////////////////////////////// -// LanguageComboboxModel used to populate a combobox with native names -// corresponding to the language code (e.g. English (United States) for en-US) +// LanguageList used to enumerate native names corresponding to the +// language code (e.g. English (United States) for en-US) // -LanguageComboboxModel::LanguageComboboxModel() - : profile_(NULL) { +LanguageList::LanguageList() { // Enumerate the languages we know about. const std::vector<std::string>& locale_codes = l10n_util::GetAvailableLocales(); InitNativeNames(locale_codes); } -LanguageComboboxModel::LanguageComboboxModel( - Profile* profile, const std::vector<std::string>& locale_codes) - : profile_(profile) { +LanguageList::LanguageList( + const std::vector<std::string>& locale_codes) { InitNativeNames(locale_codes); } -void LanguageComboboxModel::InitNativeNames( +void LanguageList::InitNativeNames( const std::vector<std::string>& locale_codes) { const std::string app_locale = g_browser_process->GetApplicationLocale(); for (size_t i = 0; i < locale_codes.size(); ++i) { @@ -57,11 +55,11 @@ void LanguageComboboxModel::InitNativeNames( } // Overridden from ComboboxModel: -int LanguageComboboxModel::GetItemCount() { +int LanguageList::get_languages_count() const { return static_cast<int>(locale_names_.size()); } -std::wstring LanguageComboboxModel::GetItemAt(int index) { +std::wstring LanguageList::GetLanguageNameAt(int index) const { DCHECK(static_cast<int>(locale_names_.size()) > index); LocaleDataMap::const_iterator it = native_names_.find(locale_names_[index]); @@ -106,7 +104,7 @@ std::wstring LanguageComboboxModel::GetItemAt(int index) { } // Return the locale for the given index. E.g., may return pt-BR. -std::string LanguageComboboxModel::GetLocaleFromIndex(int index) { +std::string LanguageList::GetLocaleFromIndex(int index) const { DCHECK(static_cast<int>(locale_names_.size()) > index); LocaleDataMap::const_iterator it = native_names_.find(locale_names_[index]); @@ -115,7 +113,7 @@ std::string LanguageComboboxModel::GetLocaleFromIndex(int index) { return it->second.locale_code; } -int LanguageComboboxModel::GetIndexFromLocale(const std::string& locale) { +int LanguageList::GetIndexFromLocale(const std::string& locale) const { for (size_t i = 0; i < locale_names_.size(); ++i) { LocaleDataMap::const_iterator it = native_names_.find(locale_names_[i]); @@ -126,6 +124,20 @@ int LanguageComboboxModel::GetIndexFromLocale(const std::string& locale) { return -1; } +/////////////////////////////////////////////////////////////////////////////// +// LanguageComboboxModel used to populate a combobox with native names +// + +LanguageComboboxModel::LanguageComboboxModel() + : profile_(NULL) { +} + +LanguageComboboxModel::LanguageComboboxModel( + Profile* profile, const std::vector<std::string>& locale_codes) + : LanguageList(locale_codes), + profile_(profile) { +} + // Returns the index of the language currently specified in the user's // preference file. Note that it's possible for language A to be picked // while chrome is currently in language B if the user specified language B diff --git a/chrome/browser/language_combobox_model.h b/chrome/browser/language_combobox_model.h index 98f74a0..8a58d0e 100644 --- a/chrome/browser/language_combobox_model.h +++ b/chrome/browser/language_combobox_model.h @@ -13,9 +13,10 @@ #include "chrome/browser/profile.h" /////////////////////////////////////////////////////////////////////////////// -// LanguageComboboxModel -// The model that fills the dropdown of valid UI languages. -class LanguageComboboxModel : public ComboboxModel { +// LanguageList +// Provides code to enumerate locale names for language selection lists. +// To be used by combobox, menu or other models. +class LanguageList { public: struct LocaleData { LocaleData() { } @@ -27,27 +28,51 @@ class LanguageComboboxModel : public ComboboxModel { }; typedef std::map<std::wstring, LocaleData> LocaleDataMap; - LanguageComboboxModel(); - - // Temporary compatibility constructor. - LanguageComboboxModel(Profile* profile, - const std::vector<std::string>& locale_codes); + LanguageList(); - virtual ~LanguageComboboxModel() {} + explicit LanguageList(const std::vector<std::string>& locale_codes); - void InitNativeNames(const std::vector<std::string>& locale_codes); + virtual ~LanguageList() {} - // Overridden from ComboboxModel: - virtual int GetItemCount(); + virtual int get_languages_count() const; - virtual std::wstring GetItemAt(int index); + virtual std::wstring GetLanguageNameAt(int index) const; // Return the locale for the given index. E.g., may return pt-BR. - std::string GetLocaleFromIndex(int index); + std::string GetLocaleFromIndex(int index) const; // Returns the index for the given locale. Returns -1 if the locale is not // in the combobox model. - int GetIndexFromLocale(const std::string& locale); + int GetIndexFromLocale(const std::string& locale) const; + + private: + // The names of all the locales in the current application locale. + std::vector<std::wstring> locale_names_; + + // A map of some extra data (LocaleData) keyed off the name of the locale. + LocaleDataMap native_names_; + + void InitNativeNames(const std::vector<std::string>& locale_codes); + + DISALLOW_COPY_AND_ASSIGN(LanguageList); +}; + +/////////////////////////////////////////////////////////////////////////////// +// LanguageComboboxModel +// The combobox model implementation. +class LanguageComboboxModel : public LanguageList, public ComboboxModel { + public: + LanguageComboboxModel(); + + // Temporary compatibility constructor. + LanguageComboboxModel(Profile* profile, + const std::vector<std::string>& locale_codes); + + virtual ~LanguageComboboxModel() {} + + virtual int GetItemCount() { return get_languages_count(); } + + virtual std::wstring GetItemAt(int index) { return GetLanguageNameAt(index); } // Returns the index of the language currently specified in the user's // preference file. Note that it's possible for language A to be picked @@ -59,12 +84,6 @@ class LanguageComboboxModel : public ComboboxModel { int GetSelectedLanguageIndex(const std::wstring& prefs); private: - // The names of all the locales in the current application locale. - std::vector<std::wstring> locale_names_; - - // A map of some extra data (LocaleData) keyed off the name of the locale. - LocaleDataMap native_names_; - // Profile. Profile* profile_; |