afe2410f9eeb71ffb409a7ea5f5bd9bf1bd5db42
[platform/core/uifw/inputdelegator.git] / src / w-input-template.cpp
1 /*
2  * Copyright (c) 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 #include "Debug.h"
18
19 #include <dlog.h>
20 #include <db-util.h>
21 #include <vconf.h>
22
23 #include "w-input-template.h"
24
25
26 #define INPUT_TEMPLATE_DB_VCONF "memory/wms/update_text_template_db"
27
28 #define INPUT_TEMPLATE_DB_PATH "/opt/usr/dbspace/.WearableManagerService.db"
29 #define INPUT_TEMPLATE_QUERY_LEN 1024
30
31 struct InputTemplate {
32         std::vector<TemplateData> template_list;
33         bool notify;
34         bool user_template;
35         input_template_changed callback;
36         void *user_data;
37 };
38
39 static struct InputTemplate g_input_template;
40
41
42 static void _input_template_db_vconf_changed(keynode_t *key, void *data);
43
44 static bool input_template_init_app_control_list(app_control_h app_control);
45 static bool input_template_init_app_control(app_control_h app_control);
46 static bool input_template_init_db_util(void);
47 static bool input_template_init_default(void);
48
49
50 static void _input_template_db_vconf_changed(keynode_t *key, void *data)
51 {
52         bool db_status = vconf_keynode_get_bool(key);
53
54         PRINTFUNC(DLOG_DEBUG, "starts :: db_status = %d", db_status);
55
56         if(db_status != 0)
57                 return;
58
59         if (g_input_template.callback) {
60                 // Clear template list and reset to template_list from DB
61                 g_input_template.template_list.clear();
62                 input_template_init_db_util();
63
64                 g_input_template.callback(g_input_template.user_data);
65         }
66 }
67
68 static bool input_template_init_app_control_list(app_control_h app_control)
69 {
70         int ret;
71         int len;
72
73         char **list = NULL;
74
75         ret = app_control_get_extra_data_array(app_control,
76                         "template_list", &list, &len);
77
78         if (ret != APP_CONTROL_ERROR_NONE)
79                 return false;
80
81         if (list == NULL)
82                 return false;
83
84         for (int i = 0; i < len; i++) {
85                 struct TemplateData data;
86
87                 if (*(list + i)) {
88                         // if use_gettext is true,
89                         // it's default template string that requires gettext.
90                         data.use_gettext = false;
91                         data.text = *(list + i);
92
93                         g_input_template.template_list.push_back(data);
94
95                         free(*(list + i));
96                 }
97         }
98
99         free(list);
100
101         g_input_template.user_template = true;
102
103         return true;
104 }
105
106 static bool input_template_init_app_control(app_control_h app_control)
107 {
108         bool ret;
109
110         if (app_control == NULL)
111                 return false;
112
113         ret = input_template_init_app_control_list(app_control);
114         if (ret)
115                 return true;
116
117         return false;
118 }
119
120 static bool input_template_init_db_util(void)
121 {
122         sqlite3 *g_tt_db = NULL;
123         sqlite3_stmt *stmt = NULL;
124
125         int ret = -1;
126
127         char query[INPUT_TEMPLATE_QUERY_LEN + 1] = {0, };
128
129         ret = db_util_open(INPUT_TEMPLATE_DB_PATH, &g_tt_db, 0);
130
131         if (ret != SQLITE_OK) {
132                 PRINTFUNC(DLOG_ERROR, "db_util open failed");
133                 return false;
134         }
135
136         snprintf(query, INPUT_TEMPLATE_QUERY_LEN,
137                         "select sortId, itemId, checked, message from %s",
138                         "tmpl_msg_table");
139
140         sqlite3_prepare_v2(g_tt_db, query, strlen(query), &stmt, NULL);
141
142         if (ret != SQLITE_OK) {
143                 PRINTFUNC(DLOG_ERROR, "Can not get query");
144                 return false;
145         }
146
147         ret = sqlite3_step(stmt);
148
149         while(ret == SQLITE_ROW) {
150                 struct TemplateData data;
151                 int text_mode;
152
153                 // if checked is 0,
154                 // it's default template string that requires gettext.
155                 text_mode = sqlite3_column_int(stmt, 2);
156                 if (text_mode)
157                         data.use_gettext = false;
158                 else
159                         data.use_gettext = true;
160
161                 data.text = (char *)sqlite3_column_text(stmt, 3);
162                 PRINTFUNC(DLOG_DEBUG, "db text %s", data.text.c_str());
163                 g_input_template.template_list.push_back(data);
164
165                 ret = sqlite3_step(stmt);
166         }
167
168         ret = sqlite3_finalize(stmt);
169         if (ret != SQLITE_OK)
170                 PRINTFUNC(DLOG_ERROR, "Can not finalize sqlite");
171
172         ret = db_util_close(g_tt_db);
173         if (ret != SQLITE_OK)
174                 PRINTFUNC(DLOG_ERROR, "Can not close db_util");
175
176         /* set vconf callback  for DB update */
177         ret = vconf_notify_key_changed(INPUT_TEMPLATE_DB_VCONF,
178                                 _input_template_db_vconf_changed, NULL);
179
180         if (ret < 0)
181                 PRINTFUNC(DLOG_ERROR, "Can not create vconf notify");
182
183         g_input_template.user_template = false;
184         return true;
185 }
186
187 static bool input_template_init_default(void)
188 {
189         struct TemplateData data;
190
191         data.text = "IDS_WMGR_MBODY_HOWS_IT_GOING_Q_M_TEXT_TEMPLATE";
192         data.use_gettext = true;
193
194         g_input_template.template_list.push_back(data);
195
196         data.text = "IDS_WMGR_MBODY_WHATS_UP_Q_M_TEXT_TEMPLATE";
197         data.use_gettext = true;
198
199         g_input_template.template_list.push_back(data);
200
201         data.text = "IDS_WMGR_MBODY_ILL_TALK_TO_YOU_SOON_M_TEXT_TEMPLATE";
202         data.use_gettext = true;
203
204         g_input_template.template_list.push_back(data);
205
206         data.text = "IDS_WMGR_MBODY_ILL_CALL_YOU_LATER_M_TEXT_TEMPLATE";
207         data.use_gettext = true;
208
209         g_input_template.template_list.push_back(data);
210
211         data.text = "IDS_MSG_BODY_WHERE_ARE_YOU_Q_M_TEXT_TEMPLATE";
212         data.use_gettext = true;
213
214         g_input_template.template_list.push_back(data);
215
216         data.text = "IDS_MSG_BODY_WHEN_CAN_WE_MEET_Q_M_TEXT_TEMPLATE";
217         data.use_gettext = true;
218
219         g_input_template.template_list.push_back(data);
220
221         data.text = "WDS_WMGR_MBODY_CALL_ME_LATER";
222         data.use_gettext = true;
223
224         g_input_template.template_list.push_back(data);
225         g_input_template.user_template = false;
226
227         return true;
228 }
229
230 static void input_template_deinit_db_util(void)
231 {
232         vconf_ignore_key_changed(INPUT_TEMPLATE_DB_VCONF,
233                         _input_template_db_vconf_changed);
234 }
235
236 void input_template_init(app_control_h app_control)
237 {
238         input_template_unset_notify();
239         g_input_template.template_list.clear();
240         g_input_template.user_template = false;
241
242         bool ret;
243
244         ret = input_template_init_app_control(app_control);
245         if (ret)
246                 return;
247
248         ret = input_template_init_db_util();
249         if (ret)
250                 return;
251
252         input_template_init_default();
253 }
254
255 void input_template_deinit(void)
256 {
257         input_template_unset_notify();
258         input_template_deinit_db_util();
259
260         g_input_template.template_list.clear();
261         g_input_template.user_template = false;
262 }
263
264 const std::vector<TemplateData> input_template_get_list(void)
265 {
266         return g_input_template.template_list;
267 }
268
269 void input_template_set_notify(input_template_changed callback,
270                         void *user_data)
271 {
272         if (callback == NULL) {
273                 PRINTFUNC(DLOG_ERROR, "empty callback function");
274                 return;
275         }
276
277         g_input_template.callback = callback;
278         g_input_template.user_data = user_data;
279 }
280
281 void input_template_unset_notify(void)
282 {
283         g_input_template.callback = NULL;
284         g_input_template.user_data = NULL;
285 }
286
287 bool input_template_is_user_template(void)
288 {
289         return (g_input_template.user_template);
290 }