1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Simple word patterns matcher which will be used in composer objects
31 // for auto swtiching input mode.
32 
33 #include "composer/internal/mode_switching_handler.h"
34 
35 #include <cctype>
36 #include <string>
37 
38 #include "base/logging.h"
39 #include "base/singleton.h"
40 
41 namespace mozc {
42 namespace composer {
43 
ModeSwitchingHandler()44 ModeSwitchingHandler::ModeSwitchingHandler() {
45   // Default patterns are fixed right now.
46   // AddRule(key, display_mode, input_mode);
47   AddRule("google", PREFERRED_ALPHANUMERIC, REVERT_TO_PREVIOUS_MODE);
48   AddRule("Google", PREFERRED_ALPHANUMERIC, REVERT_TO_PREVIOUS_MODE);
49   AddRule("Chrome", PREFERRED_ALPHANUMERIC, REVERT_TO_PREVIOUS_MODE);
50   AddRule("chrome", PREFERRED_ALPHANUMERIC, REVERT_TO_PREVIOUS_MODE);
51   AddRule("Android", PREFERRED_ALPHANUMERIC, REVERT_TO_PREVIOUS_MODE);
52   AddRule("android", PREFERRED_ALPHANUMERIC, REVERT_TO_PREVIOUS_MODE);
53   AddRule("http", HALF_ALPHANUMERIC, HALF_ALPHANUMERIC);
54   AddRule("www.", HALF_ALPHANUMERIC, HALF_ALPHANUMERIC);
55   AddRule("\\\\", HALF_ALPHANUMERIC, HALF_ALPHANUMERIC);
56 }
57 
~ModeSwitchingHandler()58 ModeSwitchingHandler::~ModeSwitchingHandler() {}
59 
GetModeSwitchingRule(const string & key,ModeSwitching * display_mode,ModeSwitching * input_mode) const60 bool ModeSwitchingHandler::GetModeSwitchingRule(
61     const string &key,
62     ModeSwitching *display_mode,
63     ModeSwitching *input_mode) const {
64   if (display_mode == nullptr || input_mode == nullptr) {
65     LOG(ERROR) << "display_mode/input_mode is nullptr.";
66     return false;
67   }
68 
69   auto it = patterns_.find(key);
70   if (it != patterns_.end()) {
71     *display_mode = it->second.first;
72     *input_mode = it->second.second;
73     return true;
74   }
75 
76   if (IsDriveLetter(key)) {
77     *display_mode = HALF_ALPHANUMERIC;
78     *input_mode = HALF_ALPHANUMERIC;
79     return true;
80   }
81 
82   *display_mode = NO_CHANGE;
83   *input_mode = NO_CHANGE;
84   return false;
85 }
86 
IsDriveLetter(const string & key)87 bool ModeSwitchingHandler::IsDriveLetter(const string &key) {
88   return key.size() == 3 &&
89          isalpha(key[0]) && key[1] == ':' && key[2] == '\\';
90 }
91 
AddRule(const string & key,const ModeSwitching display_mode,const ModeSwitching input_mode)92 void ModeSwitchingHandler::AddRule(const string &key,
93                                    const ModeSwitching display_mode,
94                                    const ModeSwitching input_mode) {
95   patterns_.emplace(key, std::make_pair(display_mode, input_mode));
96 }
97 
GetModeSwitchingHandler()98 ModeSwitchingHandler *ModeSwitchingHandler::GetModeSwitchingHandler() {
99   return Singleton<ModeSwitchingHandler>::get();
100 }
101 
102 }  // namespace composer
103 }  // namespace mozc
104