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/input_method/input_method_util.h" | 5 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 { "xkb:tr::tur", "tr" }, | 102 { "xkb:tr::tur", "tr" }, |
103 { "xkb:ua::ukr", "uk" }, | 103 { "xkb:ua::ukr", "uk" }, |
104 }; | 104 }; |
105 | 105 |
106 // Map from language code to associated input method IDs, etc. | 106 // Map from language code to associated input method IDs, etc. |
107 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; | 107 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; |
108 struct IdMaps { | 108 struct IdMaps { |
109 scoped_ptr<LanguageCodeToIdsMap> language_code_to_ids; | 109 scoped_ptr<LanguageCodeToIdsMap> language_code_to_ids; |
110 scoped_ptr<std::map<std::string, std::string> > id_to_language_code; | 110 scoped_ptr<std::map<std::string, std::string> > id_to_language_code; |
111 scoped_ptr<std::map<std::string, std::string> > id_to_display_name; | 111 scoped_ptr<std::map<std::string, std::string> > id_to_display_name; |
112 scoped_ptr<std::map<std::string, std::string> > id_to_keyboard_overlay_id; | |
113 | 112 |
114 // Returns the singleton instance. | 113 // Returns the singleton instance. |
115 static IdMaps* GetInstance() { | 114 static IdMaps* GetInstance() { |
116 return Singleton<IdMaps>::get(); | 115 return Singleton<IdMaps>::get(); |
117 } | 116 } |
118 | 117 |
119 void ReloadMaps() { | 118 void ReloadMaps() { |
120 chromeos::InputMethodLibrary* library = | 119 chromeos::InputMethodLibrary* library = |
121 chromeos::CrosLibrary::Get()->GetInputMethodLibrary(); | 120 chromeos::CrosLibrary::Get()->GetInputMethodLibrary(); |
122 scoped_ptr<chromeos::InputMethodDescriptors> supported_input_methods( | 121 scoped_ptr<chromeos::InputMethodDescriptors> supported_input_methods( |
123 library->GetSupportedInputMethods()); | 122 library->GetSupportedInputMethods()); |
124 if (supported_input_methods->size() <= 1) { | 123 if (supported_input_methods->size() <= 1) { |
125 LOG(ERROR) << "GetSupportedInputMethods returned a fallback ID"; | 124 LOG(ERROR) << "GetSupportedInputMethods returned a fallback ID"; |
126 // TODO(yusukes): Handle this error in nicer way. | 125 // TODO(yusukes): Handle this error in nicer way. |
127 } | 126 } |
128 | 127 |
129 language_code_to_ids->clear(); | 128 language_code_to_ids->clear(); |
130 id_to_language_code->clear(); | 129 id_to_language_code->clear(); |
131 id_to_display_name->clear(); | 130 id_to_display_name->clear(); |
132 id_to_keyboard_overlay_id->clear(); | |
133 | 131 |
134 // Build the id to descriptor map for handling kExtraLanguages later. | 132 // Build the id to descriptor map for handling kExtraLanguages later. |
135 typedef std::map<std::string, | 133 typedef std::map<std::string, |
136 const chromeos::InputMethodDescriptor*> DescMap; | 134 const chromeos::InputMethodDescriptor*> DescMap; |
137 DescMap id_to_descriptor_map; | 135 DescMap id_to_descriptor_map; |
138 | 136 |
139 for (size_t i = 0; i < supported_input_methods->size(); ++i) { | 137 for (size_t i = 0; i < supported_input_methods->size(); ++i) { |
140 const chromeos::InputMethodDescriptor& input_method = | 138 const chromeos::InputMethodDescriptor& input_method = |
141 supported_input_methods->at(i); | 139 supported_input_methods->at(i); |
142 const std::string language_code = | 140 const std::string language_code = |
143 chromeos::input_method::GetLanguageCodeFromDescriptor(input_method); | 141 chromeos::input_method::GetLanguageCodeFromDescriptor(input_method); |
144 AddInputMethodToMaps(language_code, input_method); | 142 AddInputMethodToMaps(language_code, input_method); |
145 // Remember the pair. | 143 // Remember the pair. |
146 id_to_descriptor_map.insert( | 144 id_to_descriptor_map.insert( |
147 std::make_pair(input_method.id, &input_method)); | 145 std::make_pair(input_method.id, &input_method)); |
148 } | 146 } |
149 | 147 |
150 for (size_t i = 0; i < arraysize(kInputMethodIdToKeyboardOverlayId); ++i) { | |
151 InputMethodIdToKeyboardOverlayId id_pair = | |
152 kInputMethodIdToKeyboardOverlayId[i]; | |
153 id_to_keyboard_overlay_id->insert( | |
154 std::make_pair(id_pair.input_method_id, id_pair.keyboard_overlay_id)); | |
155 } | |
156 | |
157 // Go through the languages listed in kExtraLanguages. | 148 // Go through the languages listed in kExtraLanguages. |
158 using chromeos::input_method::kExtraLanguages; | 149 using chromeos::input_method::kExtraLanguages; |
159 for (size_t i = 0; i < arraysize(kExtraLanguages); ++i) { | 150 for (size_t i = 0; i < arraysize(kExtraLanguages); ++i) { |
160 const char* language_code = kExtraLanguages[i].language_code; | 151 const char* language_code = kExtraLanguages[i].language_code; |
161 const char* input_method_id = kExtraLanguages[i].input_method_id; | 152 const char* input_method_id = kExtraLanguages[i].input_method_id; |
162 DescMap::const_iterator iter = id_to_descriptor_map.find(input_method_id); | 153 DescMap::const_iterator iter = id_to_descriptor_map.find(input_method_id); |
163 // If the associated input method descriptor is found, add the | 154 // If the associated input method descriptor is found, add the |
164 // language code and the input method. | 155 // language code and the input method. |
165 if (iter != id_to_descriptor_map.end()) { | 156 if (iter != id_to_descriptor_map.end()) { |
166 const chromeos::InputMethodDescriptor& input_method = *(iter->second); | 157 const chromeos::InputMethodDescriptor& input_method = *(iter->second); |
167 AddInputMethodToMaps(language_code, input_method); | 158 AddInputMethodToMaps(language_code, input_method); |
168 } | 159 } |
169 } | 160 } |
170 } | 161 } |
171 | 162 |
172 private: | 163 private: |
173 IdMaps() : language_code_to_ids(new LanguageCodeToIdsMap), | 164 IdMaps() : language_code_to_ids(new LanguageCodeToIdsMap), |
174 id_to_language_code(new std::map<std::string, std::string>), | 165 id_to_language_code(new std::map<std::string, std::string>), |
175 id_to_display_name(new std::map<std::string, std::string>), | 166 id_to_display_name(new std::map<std::string, std::string>) { |
176 id_to_keyboard_overlay_id(new std::map<std::string, std::string>) { | |
177 ReloadMaps(); | 167 ReloadMaps(); |
178 } | 168 } |
179 | 169 |
180 void AddInputMethodToMaps( | 170 void AddInputMethodToMaps( |
181 const std::string& language_code, | 171 const std::string& language_code, |
182 const chromeos::InputMethodDescriptor& input_method) { | 172 const chromeos::InputMethodDescriptor& input_method) { |
183 language_code_to_ids->insert( | 173 language_code_to_ids->insert( |
184 std::make_pair(language_code, input_method.id)); | 174 std::make_pair(language_code, input_method.id)); |
185 id_to_language_code->insert( | 175 id_to_language_code->insert( |
186 std::make_pair(input_method.id, language_code)); | 176 std::make_pair(input_method.id, language_code)); |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 std::string GetKeyboardLayoutName(const std::string& input_method_id) { | 544 std::string GetKeyboardLayoutName(const std::string& input_method_id) { |
555 if (!StartsWithASCII(input_method_id, "xkb:", true)) { | 545 if (!StartsWithASCII(input_method_id, "xkb:", true)) { |
556 return ""; | 546 return ""; |
557 } | 547 } |
558 | 548 |
559 std::vector<std::string> splitted_id; | 549 std::vector<std::string> splitted_id; |
560 base::SplitString(input_method_id, ':', &splitted_id); | 550 base::SplitString(input_method_id, ':', &splitted_id); |
561 return (splitted_id.size() > 1) ? splitted_id[1] : ""; | 551 return (splitted_id.size() > 1) ? splitted_id[1] : ""; |
562 } | 552 } |
563 | 553 |
564 std::string GetKeyboardOverlayId(const std::string& input_method_id) { | |
565 const std::map<std::string, std::string>& id_map = | |
566 *(IdMaps::GetInstance()->id_to_keyboard_overlay_id); | |
567 std::map<std::string, std::string>::const_iterator iter = | |
568 id_map.find(input_method_id); | |
569 return (iter == id_map.end() ? "" : iter->second); | |
570 } | |
571 | |
572 std::string GetInputMethodDisplayNameFromId( | 554 std::string GetInputMethodDisplayNameFromId( |
573 const std::string& input_method_id) { | 555 const std::string& input_method_id) { |
574 static const char kDefaultDisplayName[] = "USA"; | 556 static const char kDefaultDisplayName[] = "USA"; |
575 std::map<std::string, std::string>::const_iterator iter | 557 std::map<std::string, std::string>::const_iterator iter |
576 = IdMaps::GetInstance()->id_to_display_name->find(input_method_id); | 558 = IdMaps::GetInstance()->id_to_display_name->find(input_method_id); |
577 return (iter == IdMaps::GetInstance()->id_to_display_name->end()) ? | 559 return (iter == IdMaps::GetInstance()->id_to_display_name->end()) ? |
578 kDefaultDisplayName : iter->second; | 560 kDefaultDisplayName : iter->second; |
579 } | 561 } |
580 | 562 |
581 std::wstring GetLanguageDisplayNameFromCode(const std::string& language_code) { | 563 std::wstring GetLanguageDisplayNameFromCode(const std::string& language_code) { |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 library->ChangeInputMethod(initial_input_method_id); | 675 library->ChangeInputMethod(initial_input_method_id); |
694 } | 676 } |
695 } | 677 } |
696 | 678 |
697 void OnLocaleChanged() { | 679 void OnLocaleChanged() { |
698 IdMaps::GetInstance()->ReloadMaps(); | 680 IdMaps::GetInstance()->ReloadMaps(); |
699 } | 681 } |
700 | 682 |
701 } // namespace input_method | 683 } // namespace input_method |
702 } // namespace chromeos | 684 } // namespace chromeos |
OLD | NEW |