Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: chrome/browser/chromeos/options/language_config_model.cc

Issue 6336005: Remove unused language options code, which has been superseded by DOMUI. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/options/language_config_model.h"
6
7 #include <algorithm>
8 #include <functional>
9 #include <utility>
10
11 #include "app/l10n_util.h"
12 #include "base/string_split.h"
13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/chromeos/cros/cros_library.h"
15 #include "chrome/browser/chromeos/cros/input_method_library.h"
16 #include "chrome/browser/chromeos/input_method/input_method_util.h"
17 #include "chrome/browser/chromeos/preferences.h"
18 #include "chrome/common/notification_type.h"
19 #include "chrome/common/pref_names.h"
20 #include "grit/generated_resources.h"
21
22 namespace chromeos {
23
24 AddLanguageComboboxModel::AddLanguageComboboxModel(
25 Profile* profile,
26 const std::vector<std::string>& locale_codes)
27 : LanguageComboboxModel(profile, locale_codes) {
28 }
29
30 int AddLanguageComboboxModel::GetItemCount() {
31 // +1 for "Add language".
32 return get_languages_count() + 1 - ignore_set_.size();
33 }
34
35 string16 AddLanguageComboboxModel::GetItemAt(int index) {
36 // Show "Add language" as the first item.
37 if (index == 0) {
38 return l10n_util::GetStringUTF16(
39 IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_LANGUAGE_COMBOBOX);
40 }
41 return GetLanguageNameAt(GetLanguageIndex(index));
42 }
43
44 int AddLanguageComboboxModel::GetLanguageIndex(int index) const {
45 // The adjusted_index is counted while ignoring languages in ignore_set_.
46 int adjusted_index = 0;
47 for (int i = 0; i < get_languages_count(); ++i) {
48 if (ignore_set_.count(GetLocaleFromIndex(i)) > 0) {
49 continue;
50 }
51 // -1 for "Add language".
52 if (adjusted_index == index - 1) {
53 return i;
54 }
55 ++adjusted_index;
56 }
57 return 0;
58 }
59
60 void AddLanguageComboboxModel::SetIgnored(
61 const std::string& language_code, bool ignored) {
62 if (ignored) {
63 // Add to the ignore_set_ if the language code is known (i.e. reject
64 // unknown language codes just in case).
65 if (GetIndexFromLocale(language_code) != -1) {
66 ignore_set_.insert(language_code);
67 } else {
68 LOG(ERROR) << "Unknown language code: " << language_code;
69 }
70 } else {
71 ignore_set_.erase(language_code);
72 }
73 }
74
75 LanguageConfigModel::LanguageConfigModel(PrefService* pref_service)
76 : pref_service_(pref_service) {
77 // Initialize the maps and vectors.
78 InitInputMethodIdVectors();
79
80 preferred_languages_pref_.Init(
81 prefs::kLanguagePreferredLanguages, pref_service_, this);
82 preload_engines_pref_.Init(
83 prefs::kLanguagePreloadEngines, pref_service_, this);
84 // TODO(yusukes): It might be safer to call GetActiveLanguages() cros API
85 // here and compare the result and preload_engines_pref_.GetValue().
86 // If there's a discrepancy between IBus setting and Chrome prefs, we
87 // can resolve it by calling preload_engines_pref_SetValue() here.
88
89 // Initialize the language codes currently activated.
90 NotifyPrefChanged();
91 }
92
93 size_t LanguageConfigModel::CountNumActiveInputMethods(
94 const std::string& language_code) {
95 int num_selected_active_input_methods = 0;
96 std::vector<std::string> input_method_ids;
97 input_method::GetInputMethodIdsFromLanguageCode(
98 language_code, input_method::kAllInputMethods, &input_method_ids);
99 for (size_t i = 0; i < input_method_ids.size(); ++i) {
100 if (InputMethodIsActivated(input_method_ids[i])) {
101 ++num_selected_active_input_methods;
102 }
103 }
104 return num_selected_active_input_methods;
105 }
106
107 bool LanguageConfigModel::HasLanguageCode(
108 const std::string& language_code) const {
109 return std::find(preferred_language_codes_.begin(),
110 preferred_language_codes_.end(),
111 language_code) != preferred_language_codes_.end();
112 }
113
114 size_t LanguageConfigModel::AddLanguageCode(
115 const std::string& language_code) {
116 preferred_language_codes_.push_back(language_code);
117 // Sort the language codes by names. This is not efficient, but
118 // acceptable as the language list is about 40 item long at most. In
119 // theory, we could find the position to insert rather than sorting, but
120 // it would be complex as we need to use unicode string comparator.
121 input_method::SortLanguageCodesByNames(&preferred_language_codes_);
122 // Find the language code just added in the sorted language codes.
123 const int added_at =
124 std::distance(preferred_language_codes_.begin(),
125 std::find(preferred_language_codes_.begin(),
126 preferred_language_codes_.end(),
127 language_code));
128 preferred_languages_pref_.SetValue(
129 JoinString(preferred_language_codes_, ','));
130 return added_at;
131 }
132
133 void LanguageConfigModel::RemoveLanguageAt(size_t row) {
134 preferred_language_codes_.erase(preferred_language_codes_.begin() + row);
135 preferred_languages_pref_.SetValue(
136 JoinString(preferred_language_codes_, ','));
137 }
138
139 void LanguageConfigModel::UpdateInputMethodPreferences(
140 const std::vector<std::string>& in_new_input_method_ids) {
141 std::vector<std::string> new_input_method_ids = in_new_input_method_ids;
142 // Note: Since |new_input_method_ids| is alphabetically sorted and the sort
143 // function below uses stable sort, the relateve order of input methods that
144 // belong to the same language (e.g. "mozc" and "xkb:jp::jpn") is maintained.
145 input_method::SortInputMethodIdsByNames(&new_input_method_ids);
146 preload_engines_pref_.SetValue(JoinString(new_input_method_ids, ','));
147 }
148
149 void LanguageConfigModel::DeactivateInputMethodsFor(
150 const std::string& language_code) {
151 for (size_t i = 0; i < num_supported_input_method_ids(); ++i) {
152 if (input_method::GetLanguageCodeFromInputMethodId(
153 supported_input_method_id_at(i)) ==
154 language_code) {
155 // What happens if we disable the input method currently active?
156 // IBus should take care of it, so we don't do anything special
157 // here. See crosbug.com/2443.
158 SetInputMethodActivated(supported_input_method_id_at(i), false);
159 // Do not break; here in order to disable all engines that belong to
160 // |language_code|.
161 }
162 }
163 }
164
165 void LanguageConfigModel::SetInputMethodActivated(
166 const std::string& input_method_id, bool activated) {
167 DCHECK(!input_method_id.empty());
168 std::vector<std::string> input_method_ids;
169 GetActiveInputMethodIds(&input_method_ids);
170
171 std::set<std::string> input_method_id_set(input_method_ids.begin(),
172 input_method_ids.end());
173 if (activated) {
174 // Add |id| if it's not already added.
175 input_method_id_set.insert(input_method_id);
176 } else {
177 input_method_id_set.erase(input_method_id);
178 }
179
180 // Update Chrome's preference.
181 std::vector<std::string> new_input_method_ids(input_method_id_set.begin(),
182 input_method_id_set.end());
183 UpdateInputMethodPreferences(new_input_method_ids);
184 }
185
186 bool LanguageConfigModel::InputMethodIsActivated(
187 const std::string& input_method_id) {
188 std::vector<std::string> input_method_ids;
189 GetActiveInputMethodIds(&input_method_ids);
190 return (std::find(input_method_ids.begin(), input_method_ids.end(),
191 input_method_id) != input_method_ids.end());
192 }
193
194 void LanguageConfigModel::GetActiveInputMethodIds(
195 std::vector<std::string>* out_input_method_ids) {
196 const std::string value = preload_engines_pref_.GetValue();
197 out_input_method_ids->clear();
198 if (!value.empty())
199 base::SplitString(value, ',', out_input_method_ids);
200 }
201
202 void LanguageConfigModel::GetPreferredLanguageCodes(
203 std::vector<std::string>* out_language_codes) {
204 const std::string value = preferred_languages_pref_.GetValue();
205 out_language_codes->clear();
206 if (!value.empty())
207 base::SplitString(value, ',', out_language_codes);
208 }
209
210 void LanguageConfigModel::GetInputMethodIdsFromLanguageCode(
211 const std::string& language_code,
212 std::vector<std::string>* input_method_ids) const {
213 DCHECK(input_method_ids);
214 input_method_ids->clear();
215 input_method::GetInputMethodIdsFromLanguageCode(
216 language_code, input_method::kAllInputMethods, input_method_ids);
217 }
218
219 void LanguageConfigModel::NotifyPrefChanged() {
220 GetPreferredLanguageCodes(&preferred_language_codes_);
221 }
222
223 void LanguageConfigModel::Observe(NotificationType type,
224 const NotificationSource& source,
225 const NotificationDetails& details) {
226 if (type == NotificationType::PREF_CHANGED) {
227 NotifyPrefChanged();
228 }
229 }
230
231 void LanguageConfigModel::InitInputMethodIdVectors() {
232 // The two sets are used to build lists without duplication.
233 std::set<std::string> supported_language_code_set;
234 std::set<std::string> supported_input_method_id_set;
235 // Build the id to descriptor map for handling kExtraLanguages later.
236 std::map<std::string, const InputMethodDescriptor*> id_to_descriptor_map;
237
238 // GetSupportedLanguages() never return NULL.
239 scoped_ptr<InputMethodDescriptors> supported_input_methods(
240 CrosLibrary::Get()->GetInputMethodLibrary()->GetSupportedInputMethods());
241 for (size_t i = 0; i < supported_input_methods->size(); ++i) {
242 const InputMethodDescriptor& input_method = supported_input_methods->at(i);
243 const std::string language_code =
244 input_method::GetLanguageCodeFromDescriptor(input_method);
245 // Add the language code and the input method id to the sets.
246 supported_language_code_set.insert(language_code);
247 supported_input_method_id_set.insert(input_method.id);
248 // Remember the pair.
249 id_to_descriptor_map.insert(
250 std::make_pair(input_method.id, &input_method));
251 }
252
253 // Go through the languages listed in kExtraLanguages.
254 for (size_t i = 0; i < arraysize(input_method::kExtraLanguages); ++i) {
255 const char* language_code = input_method::kExtraLanguages[i].language_code;
256 const char* input_method_id =
257 input_method::kExtraLanguages[i].input_method_id;
258 std::map<std::string, const InputMethodDescriptor*>::const_iterator iter =
259 id_to_descriptor_map.find(input_method_id);
260 // If the associated input method descriptor is found, add the
261 // language code and the input method.
262 if (iter != id_to_descriptor_map.end()) {
263 const InputMethodDescriptor& input_method = *(iter->second);
264 // Add the language code and the input method id to the sets.
265 supported_language_code_set.insert(language_code);
266 supported_input_method_id_set.insert(input_method.id);
267 }
268 }
269
270 // Build the vectors from the sets.
271 supported_language_codes_.assign(supported_language_code_set.begin(),
272 supported_language_code_set.end());
273 supported_input_method_ids_.assign(supported_input_method_id_set.begin(),
274 supported_input_method_id_set.end());
275 }
276
277 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698