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#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_LANGUAGE_OPTIONS_UTIL_H_
6#define CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_LANGUAGE_OPTIONS_UTIL_H_
7#pragma once
8
9#include <string>
10
11#include "base/utf_string_conversions.h"
12#include "base/values.h"
13#include "chrome/browser/chromeos/language_preferences.h"
14#include "ui/base/l10n/l10n_util.h"
15
16class ListValue;
17
18namespace chromeos {
19
20// Returns an i18n-content value corresponding to |preference|.
21template <typename T>
22std::string GetI18nContentValue(const T& preference, const char* prefix) {
23  return std::string(prefix) + preference.ibus_config_name;
24}
25
26// Returns a property name of templateData corresponding to |preference|.
27template <typename T>
28std::string GetTemplateDataPropertyName(const T& preference,
29                                        const char* prefix) {
30  return std::string(prefix) + preference.ibus_config_name + "Value";
31}
32
33// Returns an property name of templateData corresponding the value of the min
34// attribute.
35template <typename T>
36std::string GetTemplateDataMinName(const T& preference, const char* prefix) {
37  return std::string(prefix) + preference.ibus_config_name + "Min";
38}
39
40// Returns an property name of templateData corresponding the value of the max
41// attribute.
42template <typename T>
43std::string GetTemplateDataMaxName(const T& preference, const char* prefix) {
44  return std::string(prefix) + preference.ibus_config_name + "Max";
45}
46
47// Creates a Value object from the given value. Here we use function
48// overloading to handle string and integer preferences in
49// CreateMultipleChoiceList.
50Value* CreateValue(const char* in_value);
51Value* CreateValue(int in_value);
52
53// Creates a multiple choice list from the given preference.
54template <typename T>
55ListValue* CreateMultipleChoiceList(
56    const language_prefs::LanguageMultipleChoicePreference<T>& preference) {
57  int list_length = 0;
58  for (size_t i = 0;
59       i < language_prefs::LanguageMultipleChoicePreference<T>::kMaxItems;
60       ++i) {
61    if (preference.values_and_ids[i].item_message_id == 0)
62      break;
63    ++list_length;
64  }
65  DCHECK_GT(list_length, 0);
66
67  ListValue* list_value = new ListValue();
68  for (int i = 0; i < list_length; ++i) {
69    ListValue* option = new ListValue();
70    option->Append(CreateValue(
71        preference.values_and_ids[i].ibus_config_value));
72    option->Append(Value::CreateStringValue(l10n_util::GetStringUTF16(
73        preference.values_and_ids[i].item_message_id)));
74    list_value->Append(option);
75  }
76  return list_value;
77}
78
79}  // namespace chromeos
80
81#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_LANGUAGE_OPTIONS_UTIL_H_
82