diff options
author | yusukes@google.com <yusukes@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-30 03:28:00 +0000 |
---|---|---|
committer | yusukes@google.com <yusukes@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-30 03:28:00 +0000 |
commit | c1edcb29b49b191116aba51849b643fc3595d7f5 (patch) | |
tree | f9f31b75629ef58629fe0ad3ed5a4c9e6df226d3 /chrome | |
parent | 2349bd664f65c22cb4dd360888bd4fed34093048 (diff) | |
download | chromium_src-c1edcb29b49b191116aba51849b643fc3595d7f5.zip chromium_src-c1edcb29b49b191116aba51849b643fc3595d7f5.tar.gz chromium_src-c1edcb29b49b191116aba51849b643fc3595d7f5.tar.bz2 |
Add EnableInputMethodsAndKeyboardLayouts function.
I'll use the function to implement crosbug.com/2619 (Implement input language selector on the log in page)
BUG=none
Review URL: http://codereview.chromium.org/2861022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51220 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
5 files changed, 111 insertions, 15 deletions
diff --git a/chrome/browser/chromeos/input_method/input_method_util.cc b/chrome/browser/chromeos/input_method/input_method_util.cc index d7c7279..925c4d2 100644 --- a/chrome/browser/chromeos/input_method/input_method_util.cc +++ b/chrome/browser/chromeos/input_method/input_method_util.cc @@ -4,6 +4,7 @@ #include "chrome/browser/chromeos/input_method/input_method_util.h" +#include <algorithm> #include <map> #include <utility> @@ -19,6 +20,7 @@ #include "chrome/browser/chrome_thread.h" #include "chrome/browser/chromeos/cros/cros_library.h" #include "chrome/browser/chromeos/cros/keyboard_library.h" +#include "chrome/browser/chromeos/language_preferences.h" #include "grit/generated_resources.h" #include "third_party/icu/public/common/unicode/uloc.h" @@ -546,7 +548,17 @@ void ReorderInputMethodIdsForLanguageCode( bool GetInputMethodIdsFromLanguageCode( const std::string& normalized_language_code, - bool keyboard_layout_only, + InputMethodType type, + std::vector<std::string>* out_input_method_ids) { + return GetInputMethodIdsFromLanguageCodeInternal( + *Singleton<IdMaps>::get()->language_code_to_ids, + normalized_language_code, type, out_input_method_ids); +} + +bool GetInputMethodIdsFromLanguageCodeInternal( + const std::multimap<std::string, std::string>& language_code_to_ids, + const std::string& normalized_language_code, + InputMethodType type, std::vector<std::string>* out_input_method_ids) { DCHECK(out_input_method_ids); out_input_method_ids->clear(); @@ -554,12 +566,11 @@ bool GetInputMethodIdsFromLanguageCode( bool result = false; std::pair<LanguageCodeToIdsMap::const_iterator, LanguageCodeToIdsMap::const_iterator> range = - Singleton<IdMaps>::get()->language_code_to_ids->equal_range( - normalized_language_code); + language_code_to_ids.equal_range(normalized_language_code); for (LanguageCodeToIdsMap::const_iterator iter = range.first; iter != range.second; ++iter) { const std::string& input_method_id = iter->second; - if ((!keyboard_layout_only) || IsKeyboardLayout(input_method_id)) { + if ((type == kAllInputMethods) || IsKeyboardLayout(input_method_id)) { out_input_method_ids->push_back(input_method_id); result = true; } @@ -570,5 +581,29 @@ bool GetInputMethodIdsFromLanguageCode( return result; } +void EnableInputMethods(const std::string& language_code, InputMethodType type, + const std::string& initial_input_method_id) { + std::vector<std::string> input_method_ids; + GetInputMethodIdsFromLanguageCode(language_code, type, &input_method_ids); + + if (std::count(input_method_ids.begin(), input_method_ids.end(), + kHardwareKeyboardLayout) == 0) { + input_method_ids.push_back(kHardwareKeyboardLayout); + } + // First, sort the vector by input method id, then by its display name. + std::sort(input_method_ids.begin(), input_method_ids.end()); + SortInputMethodIdsByNames(&input_method_ids); + + // Update ibus-daemon setting. + ImeConfigValue value; + value.type = ImeConfigValue::kValueTypeStringList; + value.string_list_value = input_method_ids; + InputMethodLibrary* library = CrosLibrary::Get()->GetInputMethodLibrary(); + library->SetImeConfig(kGeneralSectionName, kPreloadEnginesConfigName, value); + if (!initial_input_method_id.empty()) { + library->ChangeInputMethod(initial_input_method_id); + } +} + } // namespace input_method } // namespace chromeos diff --git a/chrome/browser/chromeos/input_method/input_method_util.h b/chrome/browser/chromeos/input_method/input_method_util.h index 9958a9c..72d86ea 100644 --- a/chrome/browser/chromeos/input_method/input_method_util.h +++ b/chrome/browser/chromeos/input_method/input_method_util.h @@ -107,11 +107,6 @@ void SortLanguageCodesByNames(std::vector<std::string>* language_codes); // using the unicode string comparator. Uses stable sorting. void SortInputMethodIdsByNames(std::vector<std::string>* input_method_ids); -// This function is only for unit tests. Do not use this. -void SortInputMethodIdsByNamesInternal( - const std::map<std::string, std::string>& id_to_language_code_map, - std::vector<std::string>* input_method_ids); - // Reorders the given input method ids for the language code. For // example, if |language_codes| is "fr" and |input_method_ids| contains // ["xkb:be::fra", and "xkb:fr::fra"], the list is reordered to @@ -121,14 +116,44 @@ void ReorderInputMethodIdsForLanguageCode( const std::string& language_code, std::vector<std::string>* input_method_ids); +enum InputMethodType { + kKeyboardLayoutsOnly, + kAllInputMethods, +}; + // Gets input method ids that belong to |language_code|. -// If |keyboard_layout_only| is true, the function does not return input methods -// that are not for keybord layout switching. Returns true on success. Note that -// the function might return false if ibus-daemon is not running, or +// If |type| is |kKeyboardLayoutsOnly|, the function does not return input +// methods that are not for keybord layout switching. Returns true on success. +// Note that the function might return false if ibus-daemon is not running, or // |language_code| is unknown. bool GetInputMethodIdsFromLanguageCode( const std::string& language_code, - bool keyboard_layout_only, + InputMethodType type, + std::vector<std::string>* out_input_method_ids); + +// Enables input methods (e.g. Chinese, Japanese) and keyboard layouts (e.g. +// US qwerty, US dvorak, French azerty) that are necessary for the language code +// and then switches to |initial_input_method_id| if the string is not empty. +// For example, if |language_code| is "en-US", US qwerty and US dvorak layouts +// would be enabled. Likewise, for Germany locale, US qwerty layout and several +// keyboard layouts for Germany would be enabled. +// If |type| is kAllInputMethods, all keyboard layouts and all input methods +// are enabled. If it's kKeyboardLayoutsOnly, only keyboard layouts are enabled. +// For example, for Japanese, xkb:jp::jpn is enabled when kKeyboardLayoutsOnly, +// and xkb:jp::jpn, mozc, mozc-jp, mozc-dv are enabled when kAllInputMethods. +void EnableInputMethods(const std::string& language_code, InputMethodType type, + const std::string& initial_input_method_id); + + +// DO NOT USE Functions below. These are only exported for unit tests. +void SortInputMethodIdsByNamesInternal( + const std::map<std::string, std::string>& id_to_language_code_map, + std::vector<std::string>* input_method_ids); + +bool GetInputMethodIdsFromLanguageCodeInternal( + const std::multimap<std::string, std::string>& language_code_to_ids, + const std::string& normalized_language_code, + InputMethodType type, std::vector<std::string>* out_input_method_ids); } // namespace input_method diff --git a/chrome/browser/chromeos/input_method/input_method_util_unittest.cc b/chrome/browser/chromeos/input_method/input_method_util_unittest.cc index a46a021..b4a3c95 100644 --- a/chrome/browser/chromeos/input_method/input_method_util_unittest.cc +++ b/chrome/browser/chromeos/input_method/input_method_util_unittest.cc @@ -270,5 +270,36 @@ TEST(InputMethodUtilTest, ReorderInputMethodIdsForLanguageCode_Noop) { EXPECT_EQ("xkb:be::fra", input_method_ids[1]); } +TEST(LanguageConfigModelTest, GetInputMethodIdsForLanguageCode) { + std::multimap<std::string, std::string> language_code_to_ids_map; + language_code_to_ids_map.insert(std::make_pair("ja", "mozc")); + language_code_to_ids_map.insert(std::make_pair("ja", "mozc-jp")); + language_code_to_ids_map.insert(std::make_pair("ja", "xkb:jp:jpn")); + language_code_to_ids_map.insert(std::make_pair("fr", "xkb:fr:fra")); + + std::vector<std::string> result; + EXPECT_TRUE(GetInputMethodIdsFromLanguageCodeInternal( + language_code_to_ids_map, "ja", kAllInputMethods, &result)); + EXPECT_EQ(3U, result.size()); + EXPECT_TRUE(GetInputMethodIdsFromLanguageCodeInternal( + language_code_to_ids_map, "ja", kKeyboardLayoutsOnly, &result)); + ASSERT_EQ(1U, result.size()); + EXPECT_EQ("xkb:jp:jpn", result[0]); + + EXPECT_TRUE(GetInputMethodIdsFromLanguageCodeInternal( + language_code_to_ids_map, "fr", kAllInputMethods, &result)); + ASSERT_EQ(1U, result.size()); + EXPECT_EQ("xkb:fr:fra", result[0]); + EXPECT_TRUE(GetInputMethodIdsFromLanguageCodeInternal( + language_code_to_ids_map, "fr", kKeyboardLayoutsOnly, &result)); + ASSERT_EQ(1U, result.size()); + EXPECT_EQ("xkb:fr:fra", result[0]); + + EXPECT_FALSE(GetInputMethodIdsFromLanguageCodeInternal( + language_code_to_ids_map, "invalid_lang", kAllInputMethods, &result)); + EXPECT_FALSE(GetInputMethodIdsFromLanguageCodeInternal( + language_code_to_ids_map, "invalid_lang", kKeyboardLayoutsOnly, &result)); +} + } // namespace input_method } // namespace chromeos diff --git a/chrome/browser/chromeos/language_preferences.h b/chrome/browser/chromeos/language_preferences.h index 69d8313..93a266f 100644 --- a/chrome/browser/chromeos/language_preferences.h +++ b/chrome/browser/chromeos/language_preferences.h @@ -392,6 +392,11 @@ const size_t kNumMozcIntegerPrefs = ARRAYSIZE_UNSAFE(kMozcIntegerPrefs); // For Traditional Chinese input methods (ibus-pinyin-bopomofo and ibus-chewing) // TODO(yusukes): Add constants for Traditional Chinese input methods. + +// A input method name that corresponds the hardware keyboard layout. +// TODO(yusukes): just assuming US qwerty keyboard is not always correct. +const char kHardwareKeyboardLayout[] = "xkb:us::eng"; + } // chromeos #endif // CHROME_BROWSER_CHROMEOS_LANGUAGE_PREFERENCES_H_ diff --git a/chrome/browser/chromeos/options/language_config_model.cc b/chrome/browser/chromeos/options/language_config_model.cc index 4b256ba..6955c43 100644 --- a/chrome/browser/chromeos/options/language_config_model.cc +++ b/chrome/browser/chromeos/options/language_config_model.cc @@ -92,7 +92,7 @@ size_t LanguageConfigModel::CountNumActiveInputMethods( int num_selected_active_input_methods = 0; std::vector<std::string> input_method_ids; input_method::GetInputMethodIdsFromLanguageCode( - language_code, false /* keyboard_layout_only */, &input_method_ids); + language_code, input_method::kKeyboardLayoutsOnly, &input_method_ids); for (size_t i = 0; i < input_method_ids.size(); ++i) { if (InputMethodIsActivated(input_method_ids[i])) { ++num_selected_active_input_methods; @@ -198,7 +198,7 @@ void LanguageConfigModel::GetInputMethodIdsFromLanguageCode( DCHECK(input_method_ids); input_method_ids->clear(); input_method::GetInputMethodIdsFromLanguageCode( - language_code, false /* keyboard_layout_only */, input_method_ids); + language_code, input_method::kAllInputMethods, input_method_ids); // Reorder the input methods. input_method::ReorderInputMethodIdsForLanguageCode( |