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/chromeos/dom_ui/language_options_handler.h" | |
6 | |
7 #include <map> | |
8 #include <set> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "app/l10n_util.h" | |
14 #include "base/basictypes.h" | |
15 #include "base/utf_string_conversions.h" | |
16 #include "base/values.h" | |
17 #include "chrome/app/chrome_command_ids.h" | |
18 #include "chrome/browser/browser_process.h" | |
19 #include "chrome/browser/chromeos/cros/cros_library.h" | |
20 #include "chrome/browser/chromeos/cros/input_method_library.h" | |
21 #include "chrome/browser/chromeos/input_method/input_method_util.h" | |
22 #include "chrome/browser/metrics/user_metrics.h" | |
23 #include "chrome/browser/profiles/profile.h" | |
24 #include "chrome/browser/tab_contents/tab_contents.h" | |
25 #include "chrome/browser/ui/browser.h" | |
26 #include "chrome/common/pref_names.h" | |
27 #include "chrome/common/spellcheck_common.h" | |
28 #include "grit/chromium_strings.h" | |
29 #include "grit/generated_resources.h" | |
30 | |
31 namespace chromeos { | |
32 | |
33 LanguageOptionsHandler::LanguageOptionsHandler() { | |
34 } | |
35 | |
36 LanguageOptionsHandler::~LanguageOptionsHandler() { | |
37 } | |
38 | |
39 void LanguageOptionsHandler::GetLocalizedValues( | |
40 DictionaryValue* localized_strings) { | |
41 DCHECK(localized_strings); | |
42 localized_strings->SetString("languagePage", | |
43 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_DIALOG_TITLE)); | |
44 localized_strings->SetString("add_button", | |
45 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_BUTTON)); | |
46 localized_strings->SetString("configure", | |
47 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_CONFIGURE)); | |
48 localized_strings->SetString("input_method", | |
49 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD)); | |
50 localized_strings->SetString("languages", | |
51 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_LANGUAGES)); | |
52 localized_strings->SetString("please_add_another_input_method", | |
53 l10n_util::GetStringUTF16( | |
54 IDS_OPTIONS_SETTINGS_LANGUAGES_PLEASE_ADD_ANOTHER_INPUT_METHOD)); | |
55 localized_strings->SetString("please_add_another_language", | |
56 l10n_util::GetStringUTF16( | |
57 IDS_OPTIONS_SETTINGS_LANGUAGES_PLEASE_ADD_ANOTHER_LANGUAGE)); | |
58 localized_strings->SetString("ok_button", l10n_util::GetStringUTF16(IDS_OK)); | |
59 localized_strings->SetString("remove_button", | |
60 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_REMOVE_BUTTON)); | |
61 localized_strings->SetString("sign_out_button", | |
62 l10n_util::GetStringUTF16( | |
63 IDS_OPTIONS_SETTINGS_LANGUAGES_SIGN_OUT_BUTTON)); | |
64 localized_strings->SetString("add_language_instructions", | |
65 l10n_util::GetStringUTF16( | |
66 IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_LANGUAGE_INSTRUCTIONS)); | |
67 localized_strings->SetString("input_method_instructions", | |
68 l10n_util::GetStringUTF16( | |
69 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_INSTRUCTIONS)); | |
70 localized_strings->SetString("switch_input_methods_hint", | |
71 l10n_util::GetStringFUTF16( | |
72 IDS_OPTIONS_SETTINGS_LANGUAGES_SWITCH_INPUT_METHODS_HINT, | |
73 ASCIIToUTF16("alt+shift"))); | |
74 localized_strings->SetString("select_previous_input_method_hint", | |
75 l10n_util::GetStringFUTF16( | |
76 IDS_OPTIONS_SETTINGS_LANGUAGES_SELECT_PREVIOUS_INPUT_METHOD_HINT, | |
77 ASCIIToUTF16("ctrl+space"))); | |
78 localized_strings->SetString("cannot_be_displayed_in_this_language", | |
79 l10n_util::GetStringFUTF16( | |
80 IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE, | |
81 l10n_util::GetStringUTF16(IDS_PRODUCT_OS_NAME))); | |
82 localized_strings->SetString("is_displayed_in_this_language", | |
83 l10n_util::GetStringFUTF16( | |
84 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE, | |
85 l10n_util::GetStringUTF16(IDS_PRODUCT_OS_NAME))); | |
86 localized_strings->SetString("display_in_this_language", | |
87 l10n_util::GetStringFUTF16( | |
88 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE, | |
89 l10n_util::GetStringUTF16(IDS_PRODUCT_OS_NAME))); | |
90 localized_strings->SetString("sign_out_required", | |
91 l10n_util::GetStringUTF16(IDS_OPTIONS_RESTART_REQUIRED)); | |
92 localized_strings->SetString("this_language_is_currently_in_use", | |
93 l10n_util::GetStringFUTF16( | |
94 IDS_OPTIONS_SETTINGS_LANGUAGES_THIS_LANGUAGE_IS_CURRENTLY_IN_USE, | |
95 l10n_util::GetStringUTF16(IDS_PRODUCT_OS_NAME))); | |
96 localized_strings->SetString("use_this_for_spell_checking", | |
97 l10n_util::GetStringUTF16( | |
98 IDS_OPTIONS_SETTINGS_USE_THIS_FOR_SPELL_CHECKING)); | |
99 localized_strings->SetString("cannot_be_used_for_spell_checking", | |
100 l10n_util::GetStringUTF16( | |
101 IDS_OPTIONS_SETTINGS_CANNOT_BE_USED_FOR_SPELL_CHECKING)); | |
102 localized_strings->SetString("is_used_for_spell_checking", | |
103 l10n_util::GetStringUTF16( | |
104 IDS_OPTIONS_SETTINGS_IS_USED_FOR_SPELL_CHECKING)); | |
105 | |
106 // GetSupportedInputMethods() never return NULL. | |
107 scoped_ptr<InputMethodDescriptors> descriptors( | |
108 CrosLibrary::Get()->GetInputMethodLibrary()->GetSupportedInputMethods()); | |
109 | |
110 // The followigns are resources, rather than local strings. | |
111 localized_strings->SetString("currentUiLanguageCode", | |
112 g_browser_process->GetApplicationLocale()); | |
113 localized_strings->Set("inputMethodList", GetInputMethodList(*descriptors)); | |
114 localized_strings->Set("languageList", GetLanguageList(*descriptors)); | |
115 localized_strings->Set("spellCheckLanguageCodeSet", | |
116 GetSpellCheckLanguageCodeSet()); | |
117 localized_strings->Set("uiLanguageCodeSet", GetUiLanguageCodeSet()); | |
118 } | |
119 | |
120 void LanguageOptionsHandler::RegisterMessages() { | |
121 DCHECK(dom_ui_); | |
122 dom_ui_->RegisterMessageCallback("inputMethodDisable", | |
123 NewCallback(this, &LanguageOptionsHandler::InputMethodDisableCallback)); | |
124 dom_ui_->RegisterMessageCallback("inputMethodEnable", | |
125 NewCallback(this, &LanguageOptionsHandler::InputMethodEnableCallback)); | |
126 dom_ui_->RegisterMessageCallback("inputMethodOptionsOpen", | |
127 NewCallback(this, | |
128 &LanguageOptionsHandler::InputMethodOptionsOpenCallback)); | |
129 dom_ui_->RegisterMessageCallback("languageOptionsOpen", | |
130 NewCallback(this, &LanguageOptionsHandler::LanguageOptionsOpenCallback)); | |
131 dom_ui_->RegisterMessageCallback("spellCheckLanguageChange", | |
132 NewCallback(this, | |
133 &LanguageOptionsHandler::SpellCheckLanguageChangeCallback)); | |
134 dom_ui_->RegisterMessageCallback("uiLanguageChange", | |
135 NewCallback(this, &LanguageOptionsHandler::UiLanguageChangeCallback)); | |
136 dom_ui_->RegisterMessageCallback("uiLanguageSignOut", | |
137 NewCallback(this, &LanguageOptionsHandler::SignOutCallback)); | |
138 } | |
139 | |
140 ListValue* LanguageOptionsHandler::GetInputMethodList( | |
141 const InputMethodDescriptors& descriptors) { | |
142 ListValue* input_method_list = new ListValue(); | |
143 | |
144 for (size_t i = 0; i < descriptors.size(); ++i) { | |
145 const InputMethodDescriptor& descriptor = descriptors[i]; | |
146 const std::string language_code = | |
147 input_method::GetLanguageCodeFromDescriptor(descriptor); | |
148 const std::string display_name = | |
149 input_method::GetInputMethodDisplayNameFromId(descriptor.id); | |
150 | |
151 DictionaryValue* dictionary = new DictionaryValue(); | |
152 dictionary->SetString("id", descriptor.id); | |
153 dictionary->SetString("displayName", display_name); | |
154 | |
155 // One input method can be associated with multiple languages, hence | |
156 // we use a dictionary here. | |
157 DictionaryValue* language_codes = new DictionaryValue(); | |
158 language_codes->SetBoolean(language_code, true); | |
159 // Check kExtraLanguages to see if there are languages associated with | |
160 // this input method. If these are present, add these. | |
161 for (size_t j = 0; j < arraysize(input_method::kExtraLanguages); ++j) { | |
162 const std::string extra_input_method_id = | |
163 input_method::kExtraLanguages[j].input_method_id; | |
164 const std::string extra_language_code = | |
165 input_method::kExtraLanguages[j].language_code; | |
166 if (extra_input_method_id == descriptor.id) { | |
167 language_codes->SetBoolean(extra_language_code, true); | |
168 } | |
169 } | |
170 dictionary->Set("languageCodeSet", language_codes); | |
171 | |
172 input_method_list->Append(dictionary); | |
173 } | |
174 | |
175 return input_method_list; | |
176 } | |
177 | |
178 ListValue* LanguageOptionsHandler::GetLanguageList( | |
179 const InputMethodDescriptors& descriptors) { | |
180 std::set<std::string> language_codes; | |
181 // Collect the language codes from the supported input methods. | |
182 for (size_t i = 0; i < descriptors.size(); ++i) { | |
183 const InputMethodDescriptor& descriptor = descriptors[i]; | |
184 const std::string language_code = | |
185 input_method::GetLanguageCodeFromDescriptor(descriptor); | |
186 language_codes.insert(language_code); | |
187 } | |
188 // Collect the language codes from kExtraLanguages. | |
189 for (size_t i = 0; i < arraysize(input_method::kExtraLanguages); ++i) { | |
190 const char* language_code = | |
191 input_method::kExtraLanguages[i].language_code; | |
192 language_codes.insert(language_code); | |
193 } | |
194 | |
195 // Map of display name -> {language code, native_display_name}. | |
196 // In theory, we should be able to create a map that is sorted by | |
197 // display names using ICU comparator, but doing it is hard, thus we'll | |
198 // use an auxiliary vector to achieve the same result. | |
199 typedef std::pair<std::string, string16> LanguagePair; | |
200 typedef std::map<string16, LanguagePair> LanguageMap; | |
201 LanguageMap language_map; | |
202 // The auxiliary vector mentioned above. | |
203 std::vector<string16> display_names; | |
204 | |
205 // Build the list of display names, and build the language map. | |
206 for (std::set<std::string>::const_iterator iter = language_codes.begin(); | |
207 iter != language_codes.end(); ++iter) { | |
208 const string16 display_name = | |
209 input_method::GetLanguageDisplayNameFromCode(*iter); | |
210 const string16 native_display_name = | |
211 input_method::GetLanguageNativeDisplayNameFromCode(*iter); | |
212 display_names.push_back(display_name); | |
213 language_map[display_name] = | |
214 std::make_pair(*iter, native_display_name); | |
215 } | |
216 DCHECK_EQ(display_names.size(), language_map.size()); | |
217 | |
218 // Sort display names using locale specific sorter. | |
219 l10n_util::SortStrings16(g_browser_process->GetApplicationLocale(), | |
220 &display_names); | |
221 | |
222 // Build the language list from the language map. | |
223 ListValue* language_list = new ListValue(); | |
224 for (size_t i = 0; i < display_names.size(); ++i) { | |
225 const LanguagePair& pair = language_map[display_names[i]]; | |
226 DictionaryValue* dictionary = new DictionaryValue(); | |
227 dictionary->SetString("code", pair.first); | |
228 dictionary->SetString("displayName", display_names[i]); | |
229 dictionary->SetString("nativeDisplayName", pair.second); | |
230 language_list->Append(dictionary); | |
231 } | |
232 | |
233 return language_list; | |
234 } | |
235 | |
236 DictionaryValue* LanguageOptionsHandler::GetUiLanguageCodeSet() { | |
237 DictionaryValue* dictionary = new DictionaryValue(); | |
238 const std::vector<std::string>& available_locales = | |
239 l10n_util::GetAvailableLocales(); | |
240 for (size_t i = 0; i < available_locales.size(); ++i) { | |
241 dictionary->SetBoolean(available_locales[i], true); | |
242 } | |
243 return dictionary; | |
244 } | |
245 | |
246 DictionaryValue* LanguageOptionsHandler::GetSpellCheckLanguageCodeSet() { | |
247 DictionaryValue* dictionary = new DictionaryValue(); | |
248 std::vector<std::string> spell_check_languages; | |
249 SpellCheckCommon::SpellCheckLanguages(&spell_check_languages); | |
250 for (size_t i = 0; i < spell_check_languages.size(); ++i) { | |
251 dictionary->SetBoolean(spell_check_languages[i], true); | |
252 } | |
253 return dictionary; | |
254 } | |
255 | |
256 void LanguageOptionsHandler::InputMethodDisableCallback( | |
257 const ListValue* args) { | |
258 const std::string input_method_id = WideToASCII(ExtractStringValue(args)); | |
259 const std::string action = StringPrintf( | |
260 "LanguageOptions_DisableInputMethod_%s", input_method_id.c_str()); | |
261 UserMetrics::RecordComputedAction(action); | |
262 } | |
263 | |
264 void LanguageOptionsHandler::InputMethodEnableCallback( | |
265 const ListValue* args) { | |
266 const std::string input_method_id = WideToASCII(ExtractStringValue(args)); | |
267 const std::string action = StringPrintf( | |
268 "LanguageOptions_EnableInputMethod_%s", input_method_id.c_str()); | |
269 UserMetrics::RecordComputedAction(action); | |
270 } | |
271 | |
272 void LanguageOptionsHandler::InputMethodOptionsOpenCallback( | |
273 const ListValue* args) { | |
274 const std::string input_method_id = WideToASCII(ExtractStringValue(args)); | |
275 const std::string action = StringPrintf( | |
276 "InputMethodOptions_Open_%s", input_method_id.c_str()); | |
277 UserMetrics::RecordComputedAction(action); | |
278 } | |
279 | |
280 void LanguageOptionsHandler::LanguageOptionsOpenCallback( | |
281 const ListValue* args) { | |
282 UserMetrics::RecordAction(UserMetricsAction("LanguageOptions_Open")); | |
283 } | |
284 | |
285 void LanguageOptionsHandler::UiLanguageChangeCallback( | |
286 const ListValue* args) { | |
287 const std::string language_code = WideToASCII(ExtractStringValue(args)); | |
288 CHECK(!language_code.empty()); | |
289 const std::string action = StringPrintf( | |
290 "LanguageOptions_UiLanguageChange_%s", language_code.c_str()); | |
291 UserMetrics::RecordComputedAction(action); | |
292 dom_ui_->GetProfile()->ChangeApplicationLocale(language_code, false); | |
293 dom_ui_->CallJavascriptFunction( | |
294 L"options.LanguageOptions.uiLanguageSaved"); | |
295 } | |
296 | |
297 void LanguageOptionsHandler::SpellCheckLanguageChangeCallback( | |
298 const ListValue* args) { | |
299 const std::string language_code = WideToASCII(ExtractStringValue(args)); | |
300 CHECK(!language_code.empty()); | |
301 const std::string action = StringPrintf( | |
302 "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str()); | |
303 UserMetrics::RecordComputedAction(action); | |
304 } | |
305 | |
306 void LanguageOptionsHandler::SignOutCallback(const ListValue* args) { | |
307 UserMetrics::RecordAction(UserMetricsAction("LanguageOptions_SignOut")); | |
308 | |
309 Browser* browser = Browser::GetBrowserForController( | |
310 &dom_ui_->tab_contents()->controller(), NULL); | |
311 if (browser) | |
312 browser->ExecuteCommand(IDC_EXIT); | |
313 } | |
314 | |
315 } // namespace chromeos | |
OLD | NEW |