Update package version to 0.9.12
[platform/core/uifw/libscl-core.git] / src / web_helper_agent.cpp
1 /*
2  * Copyright (c) 2012 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <dlog.h>
20
21 #include "web_helper_agent.h"
22
23 #include "websocket.h"
24 #include "sclcoreimpl.h"
25
26 using namespace scl;
27
28 typedef struct {
29     int major_version;
30     WEB_HELPER_AGENT_TYPE agent_type;
31 } WEB_HELPER_AGENT_TYPES_FOR_VERSIONS;
32
33 static WEB_HELPER_AGENT_TYPES_FOR_VERSIONS web_helper_agent_types_for_versions[] = {
34     {1, WEB_HELPER_AGENT_WEBSOCKET}, /* Major version 1 indicates that it uses websocket for communication */
35 };
36
37 static int WEB_HELPER_AGENT_TYPES_FOR_VERSIONS_NUM = \
38     sizeof(web_helper_agent_types_for_versions) / sizeof(WEB_HELPER_AGENT_TYPES_FOR_VERSIONS);
39
40 WEB_HELPER_AGENT_TYPE CWebHelperAgent::get_web_helper_agent_type_from_major_version(int version)
41 {
42     for (int loop = 0;loop < WEB_HELPER_AGENT_TYPES_FOR_VERSIONS_NUM;loop++) {
43         if (web_helper_agent_types_for_versions[loop].major_version == version) {
44             return web_helper_agent_types_for_versions[loop].agent_type;
45         }
46     }
47     return WEB_HELPER_AGENT_UNKNOWN;
48 }
49
50 CWebHelperAgent* CWebHelperAgent::create_web_helper_agent(WEB_HELPER_AGENT_TYPE type)
51 {
52     CWebHelperAgent *ret = NULL;
53     if (type == WEB_HELPER_AGENT_WEBSOCKET) {
54         ret = new CWebHelperAgentWebSocket;
55     }
56     return ret;
57 }
58
59 void CWebHelperAgent::destroy_web_helper_agent(CWebHelperAgent* agent)
60 {
61     if (agent) delete agent;
62 }
63
64 CWebHelperAgent::CWebHelperAgent()
65 {
66 }
67
68 CWebHelperAgent::~CWebHelperAgent()
69 {
70 }
71
72 bool CWebHelperAgent::init()
73 {
74     return true;
75 }
76
77 bool CWebHelperAgent::exit()
78 {
79     return true;
80 }
81
82 void CWebHelperAgent::signal(int sig)
83 {
84 }
85
86 void CWebHelperAgent::log(const char *str)
87 {
88     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
89     if (impl) {
90         //impl->logise_log(str);
91     }
92 }
93
94 void CWebHelperAgent::commit_string(const char *str)
95 {
96     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
97     if (impl) {
98         impl->commit_string(-1, "", str);
99     }
100 }
101
102 void CWebHelperAgent::update_preedit_string(const char *str)
103 {
104     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
105     if (impl) {
106         impl->update_preedit_string(-1, "", str);
107     }
108 }
109
110 void CWebHelperAgent::send_key_event(unsigned int key, unsigned int key_mask)
111 {
112     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
113     if (impl) {
114         impl->send_key_event(-1, "", key, key_mask);
115     }
116 }
117
118 void CWebHelperAgent::forward_key_event(unsigned int key)
119 {
120     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
121     if (impl) {
122         impl->forward_key_event(-1, "", key, scim::SCIM_KEY_NullMask);
123     }
124 }
125
126 void CWebHelperAgent::set_keyboard_sizes(int portrait_width, int portrait_height, int landscape_width, int landscape_height)
127 {
128     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
129     if (impl) {
130         SclSize portrait, landscape;
131         portrait.width = portrait_width;
132         portrait.height = portrait_height;
133         landscape.width = landscape_width;
134         landscape.height = landscape_height;
135         impl->set_keyboard_size_hints(portrait, landscape);
136     }
137 }
138
139 void CWebHelperAgent::set_selection(int start_index, int end_index)
140 {
141     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
142     if (impl) {
143         impl->set_selection(start_index, end_index);
144     }
145 }
146
147 void CWebHelperAgent::get_selection()
148 {
149     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
150     if (impl) {
151         //impl->get_selection(-1, "", );
152     }
153 }
154
155 void CWebHelperAgent::get_surrounding_text(int maxlen_before, int maxlen_after)
156 {
157     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
158     if (impl) {
159         impl->get_surrounding_text("", maxlen_before, maxlen_after);
160     }
161 }
162
163 void CWebHelperAgent::delete_surrounding_text(int offset, int len)
164 {
165     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
166     if (impl) {
167         impl->delete_surrounding_text(offset, len);
168     }
169 }
170
171 std::string CMagicKeyManager::get_magic_key()
172 {
173     static std::string current_magic_key;
174
175     /* If we don't have magic key generated yet */
176     if (current_magic_key.length() != MAGIC_KEY_LENGTH) {
177         char magic_key[MAGIC_KEY_LENGTH + 1];
178         /* We are going to generate a magic key that contains ascii characters in the range of '0' to 'z' */
179         const char magic_key_range_lower = '0';
180         const char magic_key_range_upper = 'Z';
181
182         unsigned int seed = time(NULL);
183         for (int loop = 0;loop < MAGIC_KEY_LENGTH;loop++) {
184             magic_key[loop] = (rand_r(&seed) % (magic_key_range_upper - magic_key_range_lower)) + magic_key_range_lower;
185         }
186         magic_key[MAGIC_KEY_LENGTH] = '\0';
187
188         current_magic_key = magic_key;
189     }
190
191     return current_magic_key;
192 }