OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/options/language_config_view.h" | 5 #include "chrome/browser/chromeos/options/language_config_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 807 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 if (activated) { | 818 if (activated) { |
819 // Add |id| if it's not already added. | 819 // Add |id| if it's not already added. |
820 input_method_id_set.insert(input_method_id); | 820 input_method_id_set.insert(input_method_id); |
821 } else { | 821 } else { |
822 input_method_id_set.erase(input_method_id); | 822 input_method_id_set.erase(input_method_id); |
823 } | 823 } |
824 | 824 |
825 // Update Chrome's preference. | 825 // Update Chrome's preference. |
826 std::vector<std::string> new_input_method_ids(input_method_id_set.begin(), | 826 std::vector<std::string> new_input_method_ids(input_method_id_set.begin(), |
827 input_method_id_set.end()); | 827 input_method_id_set.end()); |
| 828 |
| 829 // Note: Since |new_input_method_ids| is alphabetically sorted and the sort |
| 830 // function below uses stable sort, the relateve order of input methods that |
| 831 // belong to the same language (e.g. "mozc" and "xkb:jp::jpn") is maintained. |
| 832 SortInputMethodIdsByNames(id_to_language_code_map_, &new_input_method_ids); |
828 preload_engines_.SetValue(UTF8ToWide(JoinString(new_input_method_ids, ','))); | 833 preload_engines_.SetValue(UTF8ToWide(JoinString(new_input_method_ids, ','))); |
829 } | 834 } |
830 | 835 |
831 bool LanguageConfigView::InputMethodIsActivated( | 836 bool LanguageConfigView::InputMethodIsActivated( |
832 const std::string& input_method_id) { | 837 const std::string& input_method_id) { |
833 std::vector<std::string> input_method_ids; | 838 std::vector<std::string> input_method_ids; |
834 GetActiveInputMethodIds(&input_method_ids); | 839 GetActiveInputMethodIds(&input_method_ids); |
835 return (std::find(input_method_ids.begin(), input_method_ids.end(), | 840 return (std::find(input_method_ids.begin(), input_method_ids.end(), |
836 input_method_id) != input_method_ids.end()); | 841 input_method_id) != input_method_ids.end()); |
837 } | 842 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 | 938 |
934 std::wstring LanguageConfigView::GetLanguageDisplayNameFromCode( | 939 std::wstring LanguageConfigView::GetLanguageDisplayNameFromCode( |
935 const std::string& language_code) { | 940 const std::string& language_code) { |
936 return MaybeRewriteLanguageName(UTF16ToWide( | 941 return MaybeRewriteLanguageName(UTF16ToWide( |
937 l10n_util::GetDisplayNameForLocale( | 942 l10n_util::GetDisplayNameForLocale( |
938 language_code, g_browser_process->GetApplicationLocale(), | 943 language_code, g_browser_process->GetApplicationLocale(), |
939 true))); | 944 true))); |
940 } | 945 } |
941 | 946 |
942 namespace { | 947 namespace { |
| 948 |
943 // The comparator is used for sorting language codes by their | 949 // The comparator is used for sorting language codes by their |
944 // corresponding language names, using the ICU collator. | 950 // corresponding language names, using the ICU collator. |
945 struct CompareByLanguageName : std::binary_function<const std::string&, | 951 struct CompareLanguageCodesByLanguageName |
946 const std::string&, | 952 : std::binary_function<const std::string&, const std::string&, bool> { |
947 bool> { | 953 explicit CompareLanguageCodesByLanguageName(icu::Collator* collator) |
948 CompareByLanguageName(icu::Collator* collator) | |
949 : collator_(collator) { | 954 : collator_(collator) { |
950 } | 955 } |
951 | 956 |
952 // Calling GetLanguageDisplayNameFromCode() in the comparator is not | 957 // Calling GetLanguageDisplayNameFromCode() in the comparator is not |
953 // efficient, but acceptable as the function is cheap, and the language | 958 // efficient, but acceptable as the function is cheap, and the language |
954 // list is short (about 40 at most). | 959 // list is short (about 40 at most). |
955 bool operator()(const std::string& s1, const std::string& s2) const { | 960 bool operator()(const std::string& s1, const std::string& s2) const { |
956 const std::wstring key1 = | 961 const std::wstring key1 = |
957 LanguageConfigView::GetLanguageDisplayNameFromCode(s1); | 962 LanguageConfigView::GetLanguageDisplayNameFromCode(s1); |
958 const std::wstring key2 = | 963 const std::wstring key2 = |
959 LanguageConfigView::GetLanguageDisplayNameFromCode(s2); | 964 LanguageConfigView::GetLanguageDisplayNameFromCode(s2); |
960 return l10n_util::StringComparator<std::wstring>(collator_)(key1, key2); | 965 return l10n_util::StringComparator<std::wstring>(collator_)(key1, key2); |
961 } | 966 } |
| 967 |
962 icu::Collator* collator_; | 968 icu::Collator* collator_; |
963 }; | 969 }; |
| 970 |
| 971 // The comparator is used for sorting input method ids by their |
| 972 // corresponding language names, using the ICU collator. |
| 973 struct CompareInputMethodIdsByLanguageName |
| 974 : std::binary_function<const std::string&, const std::string&, bool> { |
| 975 CompareInputMethodIdsByLanguageName( |
| 976 icu::Collator* collator, |
| 977 const std::map<std::string, std::string>& id_to_language_code_map) |
| 978 : comparator_(collator), |
| 979 id_to_language_code_map_(id_to_language_code_map) { |
| 980 } |
| 981 |
| 982 bool operator()(const std::string& s1, const std::string& s2) const { |
| 983 std::string language_code_1; |
| 984 std::map<std::string, std::string>::const_iterator iter = |
| 985 id_to_language_code_map_.find(s1); |
| 986 if (iter != id_to_language_code_map_.end()) { |
| 987 language_code_1 = iter->second; |
| 988 } |
| 989 std::string language_code_2; |
| 990 iter = id_to_language_code_map_.find(s2); |
| 991 if (iter != id_to_language_code_map_.end()) { |
| 992 language_code_2 = iter->second; |
| 993 } |
| 994 return comparator_(language_code_1, language_code_2); |
| 995 } |
| 996 |
| 997 const CompareLanguageCodesByLanguageName comparator_; |
| 998 const std::map<std::string, std::string>& id_to_language_code_map_; |
| 999 }; |
| 1000 |
964 } // namespace | 1001 } // namespace |
965 | 1002 |
966 void LanguageConfigView::SortLanguageCodesByNames( | 1003 void LanguageConfigView::SortLanguageCodesByNames( |
967 std::vector<std::string>* language_codes) { | 1004 std::vector<std::string>* language_codes) { |
968 // We should build collator outside of the comparator. We cannot have | 1005 // We should build collator outside of the comparator. We cannot have |
969 // scoped_ptr<> in the comparator for a subtle STL reason. | 1006 // scoped_ptr<> in the comparator for a subtle STL reason. |
970 UErrorCode error = U_ZERO_ERROR; | 1007 UErrorCode error = U_ZERO_ERROR; |
971 icu::Locale locale(g_browser_process->GetApplicationLocale().c_str()); | 1008 icu::Locale locale(g_browser_process->GetApplicationLocale().c_str()); |
972 scoped_ptr<icu::Collator> collator( | 1009 scoped_ptr<icu::Collator> collator( |
973 icu::Collator::createInstance(locale, error)); | 1010 icu::Collator::createInstance(locale, error)); |
974 if (U_FAILURE(error)) | 1011 if (U_FAILURE(error)) { |
975 collator.reset(); | 1012 collator.reset(); |
| 1013 } |
976 std::sort(language_codes->begin(), language_codes->end(), | 1014 std::sort(language_codes->begin(), language_codes->end(), |
977 CompareByLanguageName(collator.get())); | 1015 CompareLanguageCodesByLanguageName(collator.get())); |
| 1016 } |
| 1017 |
| 1018 void LanguageConfigView::SortInputMethodIdsByNames( |
| 1019 const std::map<std::string, std::string>& id_to_language_code_map, |
| 1020 std::vector<std::string>* input_method_ids) { |
| 1021 UErrorCode error = U_ZERO_ERROR; |
| 1022 icu::Locale locale(g_browser_process->GetApplicationLocale().c_str()); |
| 1023 scoped_ptr<icu::Collator> collator( |
| 1024 icu::Collator::createInstance(locale, error)); |
| 1025 if (U_FAILURE(error)) { |
| 1026 collator.reset(); |
| 1027 } |
| 1028 std::stable_sort(input_method_ids->begin(), input_method_ids->end(), |
| 1029 CompareInputMethodIdsByLanguageName( |
| 1030 collator.get(), id_to_language_code_map)); |
978 } | 1031 } |
979 | 1032 |
980 void LanguageConfigView::ReorderInputMethodIdsForLanguageCode( | 1033 void LanguageConfigView::ReorderInputMethodIdsForLanguageCode( |
981 const std::string& language_code, | 1034 const std::string& language_code, |
982 std::vector<std::string>* input_method_ids) { | 1035 std::vector<std::string>* input_method_ids) { |
983 for (size_t i = 0; i < arraysize(kLanguageDefaultInputMethodIds); ++i) { | 1036 for (size_t i = 0; i < arraysize(kLanguageDefaultInputMethodIds); ++i) { |
984 if (language_code == kLanguageDefaultInputMethodIds[i].language_code) { | 1037 if (language_code == kLanguageDefaultInputMethodIds[i].language_code) { |
985 std::vector<std::string>::iterator iter = | 1038 std::vector<std::string>::iterator iter = |
986 std::find(input_method_ids->begin(), input_method_ids->end(), | 1039 std::find(input_method_ids->begin(), input_method_ids->end(), |
987 kLanguageDefaultInputMethodIds[i].input_method_id); | 1040 kLanguageDefaultInputMethodIds[i].input_method_id); |
988 // If it's not on the top of |input_method_id|, swap it with the top one. | 1041 // If it's not on the top of |input_method_id|, swap it with the top one. |
989 if (iter != input_method_ids->end() && | 1042 if (iter != input_method_ids->end() && |
990 iter != input_method_ids->begin()) { | 1043 iter != input_method_ids->begin()) { |
991 std::swap(*input_method_ids->begin(), *iter); | 1044 std::swap(*input_method_ids->begin(), *iter); |
992 } | 1045 } |
993 break; // Don't have to check other language codes. | 1046 break; // Don't have to check other language codes. |
994 } | 1047 } |
995 } | 1048 } |
996 } | 1049 } |
997 | 1050 |
998 } // namespace chromeos | 1051 } // namespace chromeos |
OLD | NEW |