1// Copyright (c) 2012 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/options/chromeos/keyboard_handler.h"
6
7#include "ash/new_window_delegate.h"
8#include "ash/shell.h"
9#include "base/bind.h"
10#include "base/bind_helpers.h"
11#include "base/command_line.h"
12#include "base/values.h"
13#include "chromeos/chromeos_switches.h"
14#include "chromeos/ime/ime_keyboard.h"
15#include "content/public/browser/web_ui.h"
16#include "grit/generated_resources.h"
17#include "ui/base/l10n/l10n_util.h"
18
19namespace {
20const struct ModifierKeysSelectItem {
21  int message_id;
22  chromeos::input_method::ModifierKey value;
23} kModifierKeysSelectItems[] = {
24  { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_SEARCH,
25    chromeos::input_method::kSearchKey },
26  { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_LEFT_CTRL,
27    chromeos::input_method::kControlKey },
28  { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_LEFT_ALT,
29    chromeos::input_method::kAltKey },
30  { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_VOID,
31    chromeos::input_method::kVoidKey },
32  { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_CAPS_LOCK,
33    chromeos::input_method::kCapsLockKey },
34  { IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_ESCAPE,
35    chromeos::input_method::kEscapeKey },
36};
37
38const char* kDataValuesNames[] = {
39  "remapSearchKeyToValue",
40  "remapControlKeyToValue",
41  "remapAltKeyToValue",
42  "remapCapsLockKeyToValue",
43  "remapDiamondKeyToValue",
44};
45}  // namespace
46
47namespace chromeos {
48namespace options {
49
50KeyboardHandler::KeyboardHandler() {
51}
52
53KeyboardHandler::~KeyboardHandler() {
54}
55
56void KeyboardHandler::GetLocalizedValues(
57    base::DictionaryValue* localized_strings) {
58  DCHECK(localized_strings);
59
60  localized_strings->SetString("keyboardOverlayTitle",
61      l10n_util::GetStringUTF16(IDS_OPTIONS_KEYBOARD_OVERLAY_TITLE));
62  localized_strings->SetString("remapSearchKeyToContent",
63      l10n_util::GetStringUTF16(
64          IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_SEARCH_LABEL));
65  localized_strings->SetString("remapControlKeyToContent",
66      l10n_util::GetStringUTF16(
67          IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_LEFT_CTRL_LABEL));
68  localized_strings->SetString("remapAltKeyToContent",
69      l10n_util::GetStringUTF16(
70          IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_LEFT_ALT_LABEL));
71  localized_strings->SetString("remapCapsLockKeyToContent",
72      l10n_util::GetStringUTF16(
73          IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_CAPS_LOCK_LABEL));
74  localized_strings->SetString("remapDiamondKeyToContent",
75      l10n_util::GetStringUTF16(
76          IDS_OPTIONS_SETTINGS_LANGUAGES_KEY_DIAMOND_KEY_LABEL));
77  localized_strings->SetString("sendFunctionKeys",
78      l10n_util::GetStringUTF16(
79          IDS_OPTIONS_SETTINGS_LANGUAGES_SEND_FUNCTION_KEYS));
80  localized_strings->SetString("sendFunctionKeysDescription",
81      l10n_util::GetStringUTF16(
82          IDS_OPTIONS_SETTINGS_LANGUAGES_SEND_FUNCTION_KEYS_DESCRIPTION));
83  localized_strings->SetString("changeLanguageAndInputSettings",
84      l10n_util::GetStringUTF16(
85          IDS_OPTIONS_SETTINGS_CHANGE_LANGUAGE_AND_INPUT_SETTINGS));
86  localized_strings->SetString("showKeyboardShortcuts",
87      l10n_util::GetStringUTF16(
88          IDS_OPTIONS_SETTINGS_SHOW_KEYBOARD_SHORTCUTS));
89
90  for (size_t i = 0; i < arraysize(kDataValuesNames); ++i) {
91    base::ListValue* list_value = new base::ListValue();
92    for (size_t j = 0; j < arraysize(kModifierKeysSelectItems); ++j) {
93      const input_method::ModifierKey value =
94          kModifierKeysSelectItems[j].value;
95      const int message_id = kModifierKeysSelectItems[j].message_id;
96      // Only the seach key can be remapped to the caps lock key.
97      if (kDataValuesNames[i] != std::string("remapSearchKeyToValue") &&
98          kDataValuesNames[i] != std::string("remapCapsLockKeyToValue") &&
99          value == input_method::kCapsLockKey) {
100        continue;
101      }
102      base::ListValue* option = new base::ListValue();
103      option->Append(new base::FundamentalValue(value));
104      option->Append(new base::StringValue(l10n_util::GetStringUTF16(
105          message_id)));
106      list_value->Append(option);
107    }
108    localized_strings->Set(kDataValuesNames[i], list_value);
109  }
110}
111
112void KeyboardHandler::InitializePage() {
113  bool chromeos_keyboard = CommandLine::ForCurrentProcess()->HasSwitch(
114      chromeos::switches::kHasChromeOSKeyboard);
115  const base::FundamentalValue show_caps_lock_options(!chromeos_keyboard);
116
117  bool has_diamond_key = CommandLine::ForCurrentProcess()->HasSwitch(
118      chromeos::switches::kHasChromeOSDiamondKey);
119  const base::FundamentalValue show_diamond_key_options(has_diamond_key);
120
121  web_ui()->CallJavascriptFunction(
122      "options.KeyboardOverlay.showCapsLockOptions",
123      show_caps_lock_options);
124  web_ui()->CallJavascriptFunction(
125      "options.KeyboardOverlay.showDiamondKeyOptions",
126      show_diamond_key_options);
127}
128
129void KeyboardHandler::RegisterMessages() {
130  // Callback to show keyboard overlay.
131  web_ui()->RegisterMessageCallback(
132      "showKeyboardShortcuts",
133      base::Bind(&KeyboardHandler::HandleShowKeyboardShortcuts,
134                 base::Unretained(this)));
135}
136
137void KeyboardHandler::HandleShowKeyboardShortcuts(const base::ListValue* args) {
138  ash::Shell::GetInstance()->new_window_delegate()->ShowKeyboardOverlay();
139}
140
141}  // namespace options
142}  // namespace chromeos
143