Update package version to 0.1.5
[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                 free(list);
80                 return false;
81         }
82
83         if (list == NULL)
84                 return false;
85
86         for (int i = 0; i < len; i++) {
87                 struct TemplateData data;
88
89                 if (*(list + i)) {
90                         // if use_gettext is true,
91                         // it's default template string that requires gettext.
92                         data.use_gettext = false;
93                         data.text = *(list + i);
94
95                         g_input_template.template_list.push_back(data);
96
97                         free(*(list + i));
98                 }
99         }
100
101         free(list);
102
103         g_input_template.user_template = true;
104
105         return true;
106 }
107
108 static bool input_template_init_app_control(app_control_h app_control)
109 {
110         bool ret;
111
112         if (app_control == NULL)
113                 return false;
114
115         ret = input_template_init_app_control_list(app_control);
116         if (ret)
117                 return true;
118
119         return false;
120 }
121
122 static bool input_template_init_db_util(void)
123 {
124         sqlite3 *g_tt_db = NULL;
125         sqlite3_stmt *stmt = NULL;
126
127         int ret = -1;
128
129         char query[INPUT_TEMPLATE_QUERY_LEN + 1] = {0, };
130
131         ret = db_util_open(INPUT_TEMPLATE_DB_PATH, &g_tt_db, 0);
132
133         if (ret != SQLITE_OK) {
134                 PRINTFUNC(DLOG_ERROR, "db_util open failed");
135                 return false;
136         }
137
138         snprintf(query, INPUT_TEMPLATE_QUERY_LEN,
139                         "select sortId, itemId, checked, message from %s",
140                         "tmpl_msg_table");
141
142         ret = sqlite3_prepare_v2(g_tt_db, query, strlen(query), &stmt, NULL);
143
144         if (ret != SQLITE_OK) {
145                 PRINTFUNC(DLOG_ERROR, "Can not get query");
146                 return false;
147         }
148
149         ret = sqlite3_step(stmt);
150
151         while(ret == SQLITE_ROW) {
152                 struct TemplateData data;
153                 int text_mode;
154
155                 // if checked is 0,
156                 // it's default template string that requires gettext.
157                 text_mode = sqlite3_column_int(stmt, 2);
158                 if (text_mode)
159                         data.use_gettext = false;
160                 else
161                         data.use_gettext = true;
162
163                 data.text = (char *)sqlite3_column_text(stmt, 3);
164                 SECURE_LOGD("db text %s", data.text.c_str());
165                 g_input_template.template_list.push_back(data);
166
167                 ret = sqlite3_step(stmt);
168         }
169
170         ret = sqlite3_finalize(stmt);
171         if (ret != SQLITE_OK)
172                 PRINTFUNC(DLOG_ERROR, "Can not finalize sqlite");
173
174         ret = db_util_close(g_tt_db);
175         if (ret != SQLITE_OK)
176                 PRINTFUNC(DLOG_ERROR, "Can not close db_util");
177
178         /* set vconf callback  for DB update */
179         ret = vconf_notify_key_changed(INPUT_TEMPLATE_DB_VCONF,
180                                 _input_template_db_vconf_changed, NULL);
181
182         if (ret < 0)
183                 PRINTFUNC(DLOG_ERROR, "Can not create vconf notify");
184
185         g_input_template.user_template = false;
186         return true;
187 }
188
189 static bool input_template_init_default(void)
190 {
191         struct TemplateData data;
192
193         data.text = "IDS_WMGR_MBODY_HOWS_IT_GOING_Q_M_TEXT_TEMPLATE";
194         data.use_gettext = true;
195
196         g_input_template.template_list.push_back(data);
197
198         data.text = "IDS_WMGR_MBODY_WHATS_UP_Q_M_TEXT_TEMPLATE";
199         data.use_gettext = true;
200
201         g_input_template.template_list.push_back(data);
202
203         data.text = "IDS_WMGR_MBODY_ILL_TALK_TO_YOU_SOON_M_TEXT_TEMPLATE";
204         data.use_gettext = true;
205
206         g_input_template.template_list.push_back(data);
207
208         data.text = "IDS_WMGR_MBODY_ILL_CALL_YOU_LATER_M_TEXT_TEMPLATE";
209         data.use_gettext = true;
210
211         g_input_template.template_list.push_back(data);
212
213         data.text = "IDS_MSG_BODY_WHERE_ARE_YOU_Q_M_TEXT_TEMPLATE";
214         data.use_gettext = true;
215
216         g_input_template.template_list.push_back(data);
217
218         data.text = "IDS_MSG_BODY_WHEN_CAN_WE_MEET_Q_M_TEXT_TEMPLATE";
219         data.use_gettext = true;
220
221         g_input_template.template_list.push_back(data);
222
223         data.text = "WDS_WMGR_MBODY_CALL_ME_LATER";
224         data.use_gettext = true;
225
226         g_input_template.template_list.push_back(data);
227         g_input_template.user_template = false;
228
229         return true;
230 }
231
232 static void input_template_deinit_db_util(void)
233 {
234         vconf_ignore_key_changed(INPUT_TEMPLATE_DB_VCONF,
235                         _input_template_db_vconf_changed);
236 }
237
238 void input_template_init(app_control_h app_control)
239 {
240         input_template_unset_notify();
241         g_input_template.template_list.clear();
242         g_input_template.user_template = false;
243
244         bool ret;
245
246         ret = input_template_init_app_control(app_control);
247         if (ret)
248                 return;
249
250         ret = input_template_init_db_util();
251         if (ret)
252                 return;
253
254         input_template_init_default();
255 }
256
257 void input_template_deinit(void)
258 {
259         input_template_unset_notify();
260         input_template_deinit_db_util();
261
262         g_input_template.template_list.clear();
263         g_input_template.user_template = false;
264 }
265
266 const std::vector<TemplateData> input_template_get_list(void)
267 {
268         return g_input_template.template_list;
269 }
270
271 void input_template_set_notify(input_template_changed callback,
272                         void *user_data)
273 {
274         if (callback == NULL) {
275                 PRINTFUNC(DLOG_ERROR, "empty callback function");
276                 return;
277         }
278
279         g_input_template.callback = callback;
280         g_input_template.user_data = user_data;
281 }
282
283 void input_template_unset_notify(void)
284 {
285         g_input_template.callback = NULL;
286         g_input_template.user_data = NULL;
287 }
288
289 bool input_template_is_user_template(void)
290 {
291         return (g_input_template.user_template);
292 }