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) {
86 for (int i = 0; i < len; i++) {
87 struct TemplateData data;
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);
95 g_input_template.template_list.push_back(data);
103 g_input_template.user_template = true;
108 static bool input_template_init_app_control(app_control_h app_control)
112 if (app_control == NULL)
115 ret = input_template_init_app_control_list(app_control);
122 static bool input_template_init_db_util(void)
124 sqlite3 *g_tt_db = NULL;
125 sqlite3_stmt *stmt = NULL;
129 char query[INPUT_TEMPLATE_QUERY_LEN + 1] = {0, };
131 ret = db_util_open(INPUT_TEMPLATE_DB_PATH, &g_tt_db, 0);
133 if (ret != SQLITE_OK) {
134 PRINTFUNC(DLOG_ERROR, "db_util open failed");
138 snprintf(query, INPUT_TEMPLATE_QUERY_LEN,
139 "select sortId, itemId, checked, message from %s",
142 ret = sqlite3_prepare_v2(g_tt_db, query, strlen(query), &stmt, NULL);
144 if (ret != SQLITE_OK) {
145 PRINTFUNC(DLOG_ERROR, "Can not get query");
149 ret = sqlite3_step(stmt);
151 while(ret == SQLITE_ROW) {
152 struct TemplateData data;
156 // it's default template string that requires gettext.
157 text_mode = sqlite3_column_int(stmt, 2);
159 data.use_gettext = false;
161 data.use_gettext = true;
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);
167 ret = sqlite3_step(stmt);
170 ret = sqlite3_finalize(stmt);
171 if (ret != SQLITE_OK)
172 PRINTFUNC(DLOG_ERROR, "Can not finalize sqlite");
174 ret = db_util_close(g_tt_db);
175 if (ret != SQLITE_OK)
176 PRINTFUNC(DLOG_ERROR, "Can not close db_util");
178 /* set vconf callback for DB update */
179 ret = vconf_notify_key_changed(INPUT_TEMPLATE_DB_VCONF,
180 _input_template_db_vconf_changed, NULL);
183 PRINTFUNC(DLOG_ERROR, "Can not create vconf notify");
185 g_input_template.user_template = false;
189 static bool input_template_init_default(void)
191 struct TemplateData data;
193 data.text = "IDS_WMGR_MBODY_HOWS_IT_GOING_Q_M_TEXT_TEMPLATE";
194 data.use_gettext = true;
196 g_input_template.template_list.push_back(data);
198 data.text = "IDS_WMGR_MBODY_WHATS_UP_Q_M_TEXT_TEMPLATE";
199 data.use_gettext = true;
201 g_input_template.template_list.push_back(data);
203 data.text = "IDS_WMGR_MBODY_ILL_TALK_TO_YOU_SOON_M_TEXT_TEMPLATE";
204 data.use_gettext = true;
206 g_input_template.template_list.push_back(data);
208 data.text = "IDS_WMGR_MBODY_ILL_CALL_YOU_LATER_M_TEXT_TEMPLATE";
209 data.use_gettext = true;
211 g_input_template.template_list.push_back(data);
213 data.text = "IDS_MSG_BODY_WHERE_ARE_YOU_Q_M_TEXT_TEMPLATE";
214 data.use_gettext = true;
216 g_input_template.template_list.push_back(data);
218 data.text = "IDS_MSG_BODY_WHEN_CAN_WE_MEET_Q_M_TEXT_TEMPLATE";
219 data.use_gettext = true;
221 g_input_template.template_list.push_back(data);
223 data.text = "WDS_WMGR_MBODY_CALL_ME_LATER";
224 data.use_gettext = true;
226 g_input_template.template_list.push_back(data);
227 g_input_template.user_template = false;
232 static void input_template_deinit_db_util(void)
234 vconf_ignore_key_changed(INPUT_TEMPLATE_DB_VCONF,
235 _input_template_db_vconf_changed);
238 void input_template_init(app_control_h app_control)
240 input_template_unset_notify();
241 g_input_template.template_list.clear();
242 g_input_template.user_template = false;
246 ret = input_template_init_app_control(app_control);
250 ret = input_template_init_db_util();
254 input_template_init_default();
257 void input_template_deinit(void)
259 input_template_unset_notify();
260 input_template_deinit_db_util();
262 g_input_template.template_list.clear();
263 g_input_template.user_template = false;
266 const std::vector<TemplateData> input_template_get_list(void)
268 return g_input_template.template_list;
271 void input_template_set_notify(input_template_changed callback,
274 if (callback == NULL) {
275 PRINTFUNC(DLOG_ERROR, "empty callback function");
279 g_input_template.callback = callback;
280 g_input_template.user_data = user_data;
283 void input_template_unset_notify(void)
285 g_input_template.callback = NULL;
286 g_input_template.user_data = NULL;
289 bool input_template_is_user_template(void)
291 return (g_input_template.user_template);