OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/options2/chromeos/cros_language_options_handle
r.h" |
| 6 |
| 7 #include <map> |
| 8 #include <set> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" |
| 15 #include "base/values.h" |
| 16 #include "chrome/app/chrome_command_ids.h" |
| 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/chromeos/input_method/input_method_manager.h" |
| 19 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" |
| 22 #include "content/browser/tab_contents/tab_contents.h" |
| 23 #include "content/public/browser/user_metrics.h" |
| 24 #include "grit/chromium_strings.h" |
| 25 #include "grit/generated_resources.h" |
| 26 #include "ui/base/l10n/l10n_util.h" |
| 27 |
| 28 namespace chromeos { |
| 29 |
| 30 CrosLanguageOptionsHandler::CrosLanguageOptionsHandler() { |
| 31 } |
| 32 |
| 33 CrosLanguageOptionsHandler::~CrosLanguageOptionsHandler() { |
| 34 } |
| 35 |
| 36 void CrosLanguageOptionsHandler::GetLocalizedValues( |
| 37 DictionaryValue* localized_strings) { |
| 38 LanguageOptionsHandlerCommon::GetLocalizedValues(localized_strings); |
| 39 |
| 40 RegisterTitle(localized_strings, "languagePage", |
| 41 IDS_OPTIONS_SETTINGS_LANGUAGES_AND_INPUT_DIALOG_TITLE); |
| 42 localized_strings->SetString("ok_button", l10n_util::GetStringUTF16(IDS_OK)); |
| 43 localized_strings->SetString("configure", |
| 44 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_CONFIGURE)); |
| 45 localized_strings->SetString("input_method", |
| 46 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD)); |
| 47 localized_strings->SetString("please_add_another_input_method", |
| 48 l10n_util::GetStringUTF16( |
| 49 IDS_OPTIONS_SETTINGS_LANGUAGES_PLEASE_ADD_ANOTHER_INPUT_METHOD)); |
| 50 localized_strings->SetString("input_method_instructions", |
| 51 l10n_util::GetStringUTF16( |
| 52 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_INSTRUCTIONS)); |
| 53 localized_strings->SetString("switch_input_methods_hint", |
| 54 l10n_util::GetStringUTF16( |
| 55 IDS_OPTIONS_SETTINGS_LANGUAGES_SWITCH_INPUT_METHODS_HINT)); |
| 56 localized_strings->SetString("select_previous_input_method_hint", |
| 57 l10n_util::GetStringUTF16( |
| 58 IDS_OPTIONS_SETTINGS_LANGUAGES_SELECT_PREVIOUS_INPUT_METHOD_HINT)); |
| 59 localized_strings->SetString("restart_button", |
| 60 l10n_util::GetStringUTF16( |
| 61 IDS_OPTIONS_SETTINGS_LANGUAGES_SIGN_OUT_BUTTON)); |
| 62 localized_strings->SetString("virtual_keyboard_button", |
| 63 l10n_util::GetStringUTF16( |
| 64 IDS_OPTIONS_SETTINGS_LANGUAGES_VIRTUAL_KEYBOARD_BUTTON)); |
| 65 |
| 66 input_method::InputMethodManager* manager = |
| 67 input_method::InputMethodManager::GetInstance(); |
| 68 // GetSupportedInputMethods() never return NULL. |
| 69 scoped_ptr<input_method::InputMethodDescriptors> descriptors( |
| 70 manager->GetSupportedInputMethods()); |
| 71 localized_strings->Set("languageList", GetLanguageList(*descriptors)); |
| 72 localized_strings->Set("inputMethodList", GetInputMethodList(*descriptors)); |
| 73 } |
| 74 |
| 75 void CrosLanguageOptionsHandler::RegisterMessages() { |
| 76 LanguageOptionsHandlerCommon::RegisterMessages(); |
| 77 |
| 78 web_ui_->RegisterMessageCallback("inputMethodDisable", |
| 79 base::Bind(&CrosLanguageOptionsHandler::InputMethodDisableCallback, |
| 80 base::Unretained(this))); |
| 81 web_ui_->RegisterMessageCallback("inputMethodEnable", |
| 82 base::Bind(&CrosLanguageOptionsHandler::InputMethodEnableCallback, |
| 83 base::Unretained(this))); |
| 84 web_ui_->RegisterMessageCallback("inputMethodOptionsOpen", |
| 85 base::Bind(&CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback, |
| 86 base::Unretained(this))); |
| 87 web_ui_->RegisterMessageCallback("uiLanguageRestart", |
| 88 base::Bind(&CrosLanguageOptionsHandler::RestartCallback, |
| 89 base::Unretained(this))); |
| 90 } |
| 91 |
| 92 ListValue* CrosLanguageOptionsHandler::GetInputMethodList( |
| 93 const input_method::InputMethodDescriptors& descriptors) { |
| 94 input_method::InputMethodManager* manager = |
| 95 input_method::InputMethodManager::GetInstance(); |
| 96 |
| 97 ListValue* input_method_list = new ListValue(); |
| 98 |
| 99 for (size_t i = 0; i < descriptors.size(); ++i) { |
| 100 const input_method::InputMethodDescriptor& descriptor = |
| 101 descriptors[i]; |
| 102 const std::string language_code = |
| 103 manager->GetInputMethodUtil()->GetLanguageCodeFromDescriptor( |
| 104 descriptor); |
| 105 const std::string display_name = |
| 106 manager->GetInputMethodUtil()->GetInputMethodDisplayNameFromId( |
| 107 descriptor.id()); |
| 108 |
| 109 DictionaryValue* dictionary = new DictionaryValue(); |
| 110 dictionary->SetString("id", descriptor.id()); |
| 111 dictionary->SetString("displayName", display_name); |
| 112 |
| 113 // One input method can be associated with multiple languages, hence |
| 114 // we use a dictionary here. |
| 115 DictionaryValue* language_codes = new DictionaryValue(); |
| 116 language_codes->SetBoolean(language_code, true); |
| 117 // Check kExtraLanguages to see if there are languages associated with |
| 118 // this input method. If these are present, add these. |
| 119 for (size_t j = 0; j < input_method::kExtraLanguagesLength; ++j) { |
| 120 const std::string extra_input_method_id = |
| 121 input_method::kExtraLanguages[j].input_method_id; |
| 122 const std::string extra_language_code = |
| 123 input_method::kExtraLanguages[j].language_code; |
| 124 if (extra_input_method_id == descriptor.id()) { |
| 125 language_codes->SetBoolean(extra_language_code, true); |
| 126 } |
| 127 } |
| 128 dictionary->Set("languageCodeSet", language_codes); |
| 129 |
| 130 input_method_list->Append(dictionary); |
| 131 } |
| 132 |
| 133 return input_method_list; |
| 134 } |
| 135 |
| 136 ListValue* CrosLanguageOptionsHandler::GetLanguageList( |
| 137 const input_method::InputMethodDescriptors& descriptors) { |
| 138 input_method::InputMethodManager* manager = |
| 139 input_method::InputMethodManager::GetInstance(); |
| 140 |
| 141 std::set<std::string> language_codes; |
| 142 // Collect the language codes from the supported input methods. |
| 143 for (size_t i = 0; i < descriptors.size(); ++i) { |
| 144 const input_method::InputMethodDescriptor& descriptor = descriptors[i]; |
| 145 const std::string language_code = |
| 146 manager->GetInputMethodUtil()->GetLanguageCodeFromDescriptor( |
| 147 descriptor); |
| 148 language_codes.insert(language_code); |
| 149 } |
| 150 // Collect the language codes from kExtraLanguages. |
| 151 for (size_t i = 0; i < input_method::kExtraLanguagesLength; ++i) { |
| 152 const char* language_code = |
| 153 input_method::kExtraLanguages[i].language_code; |
| 154 language_codes.insert(language_code); |
| 155 } |
| 156 |
| 157 // Map of display name -> {language code, native_display_name}. |
| 158 // In theory, we should be able to create a map that is sorted by |
| 159 // display names using ICU comparator, but doing it is hard, thus we'll |
| 160 // use an auxiliary vector to achieve the same result. |
| 161 typedef std::pair<std::string, string16> LanguagePair; |
| 162 typedef std::map<string16, LanguagePair> LanguageMap; |
| 163 LanguageMap language_map; |
| 164 // The auxiliary vector mentioned above. |
| 165 std::vector<string16> display_names; |
| 166 |
| 167 // Build the list of display names, and build the language map. |
| 168 for (std::set<std::string>::const_iterator iter = language_codes.begin(); |
| 169 iter != language_codes.end(); ++iter) { |
| 170 const string16 display_name = |
| 171 input_method::InputMethodUtil::GetLanguageDisplayNameFromCode(*iter); |
| 172 const string16 native_display_name = |
| 173 input_method::InputMethodUtil::GetLanguageNativeDisplayNameFromCode( |
| 174 *iter); |
| 175 display_names.push_back(display_name); |
| 176 language_map[display_name] = |
| 177 std::make_pair(*iter, native_display_name); |
| 178 } |
| 179 DCHECK_EQ(display_names.size(), language_map.size()); |
| 180 |
| 181 // Sort display names using locale specific sorter. |
| 182 l10n_util::SortStrings16(g_browser_process->GetApplicationLocale(), |
| 183 &display_names); |
| 184 |
| 185 // Build the language list from the language map. |
| 186 ListValue* language_list = new ListValue(); |
| 187 for (size_t i = 0; i < display_names.size(); ++i) { |
| 188 const LanguagePair& pair = language_map[display_names[i]]; |
| 189 DictionaryValue* dictionary = new DictionaryValue(); |
| 190 dictionary->SetString("code", pair.first); |
| 191 dictionary->SetString("displayName", display_names[i]); |
| 192 dictionary->SetString("nativeDisplayName", pair.second); |
| 193 language_list->Append(dictionary); |
| 194 } |
| 195 |
| 196 return language_list; |
| 197 } |
| 198 |
| 199 string16 CrosLanguageOptionsHandler::GetProductName() { |
| 200 return l10n_util::GetStringUTF16(IDS_PRODUCT_OS_NAME); |
| 201 } |
| 202 |
| 203 void CrosLanguageOptionsHandler::SetApplicationLocale( |
| 204 const std::string& language_code) { |
| 205 Profile::FromWebUI(web_ui_)->ChangeAppLocale( |
| 206 language_code, Profile::APP_LOCALE_CHANGED_VIA_SETTINGS); |
| 207 } |
| 208 |
| 209 void CrosLanguageOptionsHandler::RestartCallback(const ListValue* args) { |
| 210 UserMetrics::RecordAction(UserMetricsAction("LanguageOptions_SignOut")); |
| 211 |
| 212 Browser* browser = Browser::GetBrowserForController( |
| 213 &web_ui_->tab_contents()->controller(), NULL); |
| 214 if (browser) |
| 215 browser->ExecuteCommand(IDC_EXIT); |
| 216 } |
| 217 |
| 218 void CrosLanguageOptionsHandler::InputMethodDisableCallback( |
| 219 const ListValue* args) { |
| 220 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args)); |
| 221 const std::string action = base::StringPrintf( |
| 222 "LanguageOptions_DisableInputMethod_%s", input_method_id.c_str()); |
| 223 UserMetrics::RecordComputedAction(action); |
| 224 } |
| 225 |
| 226 void CrosLanguageOptionsHandler::InputMethodEnableCallback( |
| 227 const ListValue* args) { |
| 228 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args)); |
| 229 const std::string action = base::StringPrintf( |
| 230 "LanguageOptions_EnableInputMethod_%s", input_method_id.c_str()); |
| 231 UserMetrics::RecordComputedAction(action); |
| 232 } |
| 233 |
| 234 void CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback( |
| 235 const ListValue* args) { |
| 236 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args)); |
| 237 const std::string action = base::StringPrintf( |
| 238 "InputMethodOptions_Open_%s", input_method_id.c_str()); |
| 239 UserMetrics::RecordComputedAction(action); |
| 240 } |
| 241 |
| 242 } // namespace chromeos |
OLD | NEW |