| 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 <map> | 8 #include <map> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
| 11 #include "app/l10n_util_collator.h" | 12 #include "app/l10n_util_collator.h" |
| 12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 13 #include "base/hash_tables.h" | 14 #include "base/hash_tables.h" |
| 14 #include "base/scoped_ptr.h" | 15 #include "base/scoped_ptr.h" |
| 15 #include "base/singleton.h" | 16 #include "base/singleton.h" |
| 16 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/chrome_thread.h" | 20 #include "chrome/browser/chrome_thread.h" |
| 20 #include "chrome/browser/chromeos/cros/cros_library.h" | 21 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 21 #include "chrome/browser/chromeos/cros/keyboard_library.h" | 22 #include "chrome/browser/chromeos/cros/keyboard_library.h" |
| 23 #include "chrome/browser/chromeos/language_preferences.h" |
| 22 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 23 #include "third_party/icu/public/common/unicode/uloc.h" | 25 #include "third_party/icu/public/common/unicode/uloc.h" |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 // Map from language code to associated input method IDs, etc. | 29 // Map from language code to associated input method IDs, etc. |
| 28 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; | 30 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; |
| 29 struct IdMaps { | 31 struct IdMaps { |
| 30 LanguageCodeToIdsMap* language_code_to_ids; | 32 LanguageCodeToIdsMap* language_code_to_ids; |
| 31 std::map<std::string, std::string>* id_to_language_code; | 33 std::map<std::string, std::string>* id_to_language_code; |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 iter != input_method_ids->begin()) { | 541 iter != input_method_ids->begin()) { |
| 540 std::swap(*input_method_ids->begin(), *iter); | 542 std::swap(*input_method_ids->begin(), *iter); |
| 541 } | 543 } |
| 542 break; // Don't have to check other language codes. | 544 break; // Don't have to check other language codes. |
| 543 } | 545 } |
| 544 } | 546 } |
| 545 } | 547 } |
| 546 | 548 |
| 547 bool GetInputMethodIdsFromLanguageCode( | 549 bool GetInputMethodIdsFromLanguageCode( |
| 548 const std::string& normalized_language_code, | 550 const std::string& normalized_language_code, |
| 549 bool keyboard_layout_only, | 551 InputMethodType type, |
| 552 std::vector<std::string>* out_input_method_ids) { |
| 553 return GetInputMethodIdsFromLanguageCodeInternal( |
| 554 *Singleton<IdMaps>::get()->language_code_to_ids, |
| 555 normalized_language_code, type, out_input_method_ids); |
| 556 } |
| 557 |
| 558 bool GetInputMethodIdsFromLanguageCodeInternal( |
| 559 const std::multimap<std::string, std::string>& language_code_to_ids, |
| 560 const std::string& normalized_language_code, |
| 561 InputMethodType type, |
| 550 std::vector<std::string>* out_input_method_ids) { | 562 std::vector<std::string>* out_input_method_ids) { |
| 551 DCHECK(out_input_method_ids); | 563 DCHECK(out_input_method_ids); |
| 552 out_input_method_ids->clear(); | 564 out_input_method_ids->clear(); |
| 553 | 565 |
| 554 bool result = false; | 566 bool result = false; |
| 555 std::pair<LanguageCodeToIdsMap::const_iterator, | 567 std::pair<LanguageCodeToIdsMap::const_iterator, |
| 556 LanguageCodeToIdsMap::const_iterator> range = | 568 LanguageCodeToIdsMap::const_iterator> range = |
| 557 Singleton<IdMaps>::get()->language_code_to_ids->equal_range( | 569 language_code_to_ids.equal_range(normalized_language_code); |
| 558 normalized_language_code); | |
| 559 for (LanguageCodeToIdsMap::const_iterator iter = range.first; | 570 for (LanguageCodeToIdsMap::const_iterator iter = range.first; |
| 560 iter != range.second; ++iter) { | 571 iter != range.second; ++iter) { |
| 561 const std::string& input_method_id = iter->second; | 572 const std::string& input_method_id = iter->second; |
| 562 if ((!keyboard_layout_only) || IsKeyboardLayout(input_method_id)) { | 573 if ((type == kAllInputMethods) || IsKeyboardLayout(input_method_id)) { |
| 563 out_input_method_ids->push_back(input_method_id); | 574 out_input_method_ids->push_back(input_method_id); |
| 564 result = true; | 575 result = true; |
| 565 } | 576 } |
| 566 } | 577 } |
| 567 if (!result) { | 578 if (!result) { |
| 568 LOG(ERROR) << "Unknown language code: " << normalized_language_code; | 579 LOG(ERROR) << "Unknown language code: " << normalized_language_code; |
| 569 } | 580 } |
| 570 return result; | 581 return result; |
| 571 } | 582 } |
| 572 | 583 |
| 584 void EnableInputMethods(const std::string& language_code, InputMethodType type, |
| 585 const std::string& initial_input_method_id) { |
| 586 std::vector<std::string> input_method_ids; |
| 587 GetInputMethodIdsFromLanguageCode(language_code, type, &input_method_ids); |
| 588 |
| 589 if (std::count(input_method_ids.begin(), input_method_ids.end(), |
| 590 kHardwareKeyboardLayout) == 0) { |
| 591 input_method_ids.push_back(kHardwareKeyboardLayout); |
| 592 } |
| 593 // First, sort the vector by input method id, then by its display name. |
| 594 std::sort(input_method_ids.begin(), input_method_ids.end()); |
| 595 SortInputMethodIdsByNames(&input_method_ids); |
| 596 |
| 597 // Update ibus-daemon setting. |
| 598 ImeConfigValue value; |
| 599 value.type = ImeConfigValue::kValueTypeStringList; |
| 600 value.string_list_value = input_method_ids; |
| 601 InputMethodLibrary* library = CrosLibrary::Get()->GetInputMethodLibrary(); |
| 602 library->SetImeConfig(kGeneralSectionName, kPreloadEnginesConfigName, value); |
| 603 if (!initial_input_method_id.empty()) { |
| 604 library->ChangeInputMethod(initial_input_method_id); |
| 605 } |
| 606 } |
| 607 |
| 573 } // namespace input_method | 608 } // namespace input_method |
| 574 } // namespace chromeos | 609 } // namespace chromeos |
| OLD | NEW |