2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "w-input-template.h"
26 #define INPUT_TEMPLATE_DB_VCONF "memory/wms/update_text_template_db"
28 #define INPUT_TEMPLATE_DB_PATH "/opt/usr/dbspace/.WearableManagerService.db"
29 #define INPUT_TEMPLATE_QUERY_LEN 1024
31 struct InputTemplate {
32 std::vector<TemplateData> template_list;
35 input_template_changed callback;
39 static struct InputTemplate g_input_template;
42 static void _input_template_db_vconf_changed(keynode_t *key, void *data);
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);
50 static void _input_template_db_vconf_changed(keynode_t *key, void *data)
52 bool db_status = vconf_keynode_get_bool(key);
54 PRINTFUNC(DLOG_DEBUG, "starts :: db_status = %d", db_status);
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();
64 g_input_template.callback(g_input_template.user_data);
68 static bool input_template_init_app_control_list(app_control_h app_control)
75 ret = app_control_get_extra_data_array(app_control,
76 "template_list", &list, &len);
78 if (ret != APP_CONTROL_ERROR_NONE)
84 for (int i = 0; i < len; i++) {
85 struct TemplateData data;
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);
93 g_input_template.template_list.push_back(data);
101 g_input_template.user_template = true;
106 static bool input_template_init_app_control(app_control_h app_control)
110 if (app_control == NULL)
113 ret = input_template_init_app_control_list(app_control);
120 static bool input_template_init_db_util(void)
122 sqlite3 *g_tt_db = NULL;
123 sqlite3_stmt *stmt = NULL;
127 char query[INPUT_TEMPLATE_QUERY_LEN + 1] = {0, };
129 ret = db_util_open(INPUT_TEMPLATE_DB_PATH, &g_tt_db, 0);
131 if (ret != SQLITE_OK) {
132 PRINTFUNC(DLOG_ERROR, "db_util open failed");
136 snprintf(query, INPUT_TEMPLATE_QUERY_LEN,
137 "select sortId, itemId, checked, message from %s",
140 ret = sqlite3_prepare_v2(g_tt_db, query, strlen(query), &stmt, NULL);
142 if (ret != SQLITE_OK) {
143 PRINTFUNC(DLOG_ERROR, "Can not get query");
147 ret = sqlite3_step(stmt);
149 while(ret == SQLITE_ROW) {
150 struct TemplateData data;
154 // it's default template string that requires gettext.
155 text_mode = sqlite3_column_int(stmt, 2);
157 data.use_gettext = false;
159 data.use_gettext = true;
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);
165 ret = sqlite3_step(stmt);
168 ret = sqlite3_finalize(stmt);
169 if (ret != SQLITE_OK)
170 PRINTFUNC(DLOG_ERROR, "Can not finalize sqlite");
172 ret = db_util_close(g_tt_db);
173 if (ret != SQLITE_OK)
174 PRINTFUNC(DLOG_ERROR, "Can not close db_util");
176 /* set vconf callback for DB update */
177 ret = vconf_notify_key_changed(INPUT_TEMPLATE_DB_VCONF,
178 _input_template_db_vconf_changed, NULL);
181 PRINTFUNC(DLOG_ERROR, "Can not create vconf notify");
183 g_input_template.user_template = false;
187 static bool input_template_init_default(void)
189 struct TemplateData data;
191 data.text = "IDS_WMGR_MBODY_HOWS_IT_GOING_Q_M_TEXT_TEMPLATE";
192 data.use_gettext = true;
194 g_input_template.template_list.push_back(data);
196 data.text = "IDS_WMGR_MBODY_WHATS_UP_Q_M_TEXT_TEMPLATE";
197 data.use_gettext = true;
199 g_input_template.template_list.push_back(data);
201 data.text = "IDS_WMGR_MBODY_ILL_TALK_TO_YOU_SOON_M_TEXT_TEMPLATE";
202 data.use_gettext = true;
204 g_input_template.template_list.push_back(data);
206 data.text = "IDS_WMGR_MBODY_ILL_CALL_YOU_LATER_M_TEXT_TEMPLATE";
207 data.use_gettext = true;
209 g_input_template.template_list.push_back(data);
211 data.text = "IDS_MSG_BODY_WHERE_ARE_YOU_Q_M_TEXT_TEMPLATE";
212 data.use_gettext = true;
214 g_input_template.template_list.push_back(data);
216 data.text = "IDS_MSG_BODY_WHEN_CAN_WE_MEET_Q_M_TEXT_TEMPLATE";
217 data.use_gettext = true;
219 g_input_template.template_list.push_back(data);
221 data.text = "WDS_WMGR_MBODY_CALL_ME_LATER";
222 data.use_gettext = true;
224 g_input_template.template_list.push_back(data);
225 g_input_template.user_template = false;
230 static void input_template_deinit_db_util(void)
232 vconf_ignore_key_changed(INPUT_TEMPLATE_DB_VCONF,
233 _input_template_db_vconf_changed);
236 void input_template_init(app_control_h app_control)
238 input_template_unset_notify();
239 g_input_template.template_list.clear();
240 g_input_template.user_template = false;
244 ret = input_template_init_app_control(app_control);
248 ret = input_template_init_db_util();
252 input_template_init_default();
255 void input_template_deinit(void)
257 input_template_unset_notify();
258 input_template_deinit_db_util();
260 g_input_template.template_list.clear();
261 g_input_template.user_template = false;
264 const std::vector<TemplateData> input_template_get_list(void)
266 return g_input_template.template_list;
269 void input_template_set_notify(input_template_changed callback,
272 if (callback == NULL) {
273 PRINTFUNC(DLOG_ERROR, "empty callback function");
277 g_input_template.callback = callback;
278 g_input_template.user_data = user_data;
281 void input_template_unset_notify(void)
283 g_input_template.callback = NULL;
284 g_input_template.user_data = NULL;
287 bool input_template_is_user_template(void)
289 return (g_input_template.user_template);