Reduce logs
[platform/core/uifw/voice-control.git] / common / vc_cmd_db.c
1 /*
2 * Copyright (c) 2011-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 <app_manager.h>
18 #include <db-util.h>
19 #include <dlog.h>
20 #include <sqlite3.h>
21 #include <tzplatform_config.h>
22
23 #include "vc_cmd_db.h"
24 #include "vc_main.h"
25 #include "vc_command.h"
26 #include "voice_control_command_expand.h"
27
28
29 const char* vc_db_tag()
30 {
31         return TAG_VCDB;
32 }
33
34 /*!
35  * \note
36  * DB Table schema
37  *
38  * vc_info
39  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
40  * | id  | pid  | type | format | domain | command | parameter | appid | invocation_name | fixed |
41  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
42  *
43  * CREATE TABLE vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, appid TEXT);
44  *
45  * vc_result
46  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
47  * | id  | result | event | msg | exclusive | pid | type | format | domain | command | parameter | appid | invocation_name | fixed |
48  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
49  *
50  * CREATE TABLE vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,
51  *                                      pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT,\
52  *
53  */
54
55 #define FREE_MEM(X) {if (NULL != X) {free(X); X = NULL; } }
56
57
58 //#define DB_PATH tzplatform_mkpath(TZ_USER_DB, ".vc_info.db")
59 static sqlite3* db_handle = NULL;
60 char* path = NULL;
61 int g_fpid = -1;
62 int g_ref_cnt = 0;
63
64 static int __vc_db_transaction(const char* transaction)
65 {
66         sqlite3_stmt* pStmt = NULL;
67
68         int ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
69         if (ret != SQLITE_OK) {
70                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
71                 return VC_DB_ERROR_OPERATION_FAILED;
72         }
73
74         if (sqlite3_step(pStmt) != SQLITE_DONE) {
75                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
76                 sqlite3_finalize(pStmt);
77                 return VC_DB_ERROR_OPERATION_FAILED;
78         }
79
80         sqlite3_finalize(pStmt);
81         return VC_DB_ERROR_NONE;
82 }
83
84 static int __vc_db_begin_transaction(void)
85 {
86         int ret = __vc_db_transaction("BEGIN TRANSACTION");
87         return ret;
88 }
89
90 static int __vc_db_rollback_transaction(void)
91 {
92         int ret = __vc_db_transaction("ROLLBACK TRANSACTION");
93         return ret;
94 }
95
96 static int __vc_db_commit_transaction(void)
97 {
98         int ret = __vc_db_transaction("COMMIT TRANSACTION");
99         return ret;
100 }
101
102 static int __vc_db_exec_query(const char* sql)
103 {
104         char* err_msg = NULL;
105
106         int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
107         if (ret != SQLITE_OK) {
108                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
109                 sqlite3_free(err_msg);
110                 return VC_DB_ERROR_OPERATION_FAILED;
111         }
112         return VC_DB_ERROR_NONE;
113 }
114
115 static int __vc_db_delete_commands(int pid, vc_cmd_type_e type, const char* appid)
116 {
117         sqlite3_stmt* stmt = NULL;
118         char* sql = NULL;
119
120         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid)
121                 sql = strdup("DELETE FROM vc_info WHERE type = ?;");
122         else if (NULL != appid)
123                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND appid = ?;");
124         else
125                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
126
127         if (NULL == sql) {
128                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
129                 return VC_DB_ERROR_OUT_OF_MEMORY;
130         }
131
132         int ret = 0;
133         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
134         if (ret != SQLITE_OK) {
135                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
136                 free(sql);
137                 sql = NULL;
138                 return VC_DB_ERROR_OPERATION_FAILED;
139         }
140         ret = sqlite3_bind_int(stmt, 1, (int)type);
141         if (ret != SQLITE_OK) {
142                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
143                 sqlite3_finalize(stmt);
144                 free(sql);
145                 sql = NULL;
146                 return VC_DB_ERROR_OPERATION_FAILED;
147         }
148
149         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid) {
150                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d;", type);
151         } else if (NULL != appid) {
152                 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
153                 if (ret != SQLITE_OK) {
154                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
155                         sqlite3_clear_bindings(stmt);
156                         sqlite3_finalize(stmt);
157                         free(sql);
158                         sql = NULL;
159                         return VC_DB_ERROR_OPERATION_FAILED;
160                 }
161                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
162         } else  {
163                 ret = sqlite3_bind_int(stmt, 2, pid);
164                 if (ret != SQLITE_OK) {
165                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
166                         sqlite3_clear_bindings(stmt);
167                         sqlite3_finalize(stmt);
168                         free(sql);
169                         sql = NULL;
170                         return VC_DB_ERROR_OPERATION_FAILED;
171                 }
172                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
173         }
174
175         ret = sqlite3_step(stmt);
176         if (ret != SQLITE_DONE) {
177                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned, sql(%s), ret(%d), err(%s)", sql, ret, sqlite3_errmsg(db_handle));
178                 sqlite3_clear_bindings(stmt);
179                 sqlite3_finalize(stmt);
180                 free(sql);
181                 sql = NULL;
182                 return VC_DB_ERROR_OPERATION_FAILED;
183         }
184
185         sqlite3_reset(stmt);
186         sqlite3_clear_bindings(stmt);
187         sqlite3_finalize(stmt);
188
189         free(sql);
190         sql = NULL;
191         return VC_DB_ERROR_NONE;
192 }
193
194 static int __vc_db_insert_commands(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
195 {
196         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
197
198         sqlite3_stmt* stmt = NULL;
199         const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
200         int ret = 0;
201         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
202         if (ret != SQLITE_OK) {
203                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
204                 return VC_DB_ERROR_OPERATION_FAILED;
205         }
206         ret = sqlite3_bind_int(stmt, 2, pid);
207         if (ret != SQLITE_OK) {
208                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
209                 sqlite3_finalize(stmt);
210                 return VC_DB_ERROR_OPERATION_FAILED;
211         }
212         ret = sqlite3_bind_int(stmt, 3, cmd->type);
213         if (ret != SQLITE_OK) {
214                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
215                 sqlite3_clear_bindings(stmt);
216                 sqlite3_finalize(stmt);
217                 return VC_DB_ERROR_OPERATION_FAILED;
218         }
219         ret = sqlite3_bind_int(stmt, 4, cmd->format);
220         if (ret != SQLITE_OK) {
221                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
222                 sqlite3_clear_bindings(stmt);
223                 sqlite3_finalize(stmt);
224                 return VC_DB_ERROR_OPERATION_FAILED;
225         }
226         ret = sqlite3_bind_int(stmt, 5, cmd->domain);
227         if (ret != SQLITE_OK) {
228                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
229                 sqlite3_clear_bindings(stmt);
230                 sqlite3_finalize(stmt);
231                 return VC_DB_ERROR_OPERATION_FAILED;
232         }
233         ret = sqlite3_bind_text(stmt, 6, cmd->command, -1, SQLITE_TRANSIENT);
234         if (ret != SQLITE_OK) {
235                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
236                 sqlite3_clear_bindings(stmt);
237                 sqlite3_finalize(stmt);
238                 return VC_DB_ERROR_OPERATION_FAILED;
239         }
240
241         char* appid = NULL;
242         if (NULL == cmd->appid) {
243                 // Get appid by pid using app control
244                 ret = app_manager_get_app_id(pid, &appid);
245                 if (APP_MANAGER_ERROR_NONE != ret) {
246                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
247                 }
248                 if (NULL != appid) {
249                         cmd->appid = strdup(appid);
250                         FREE_MEM(appid);
251                 }
252         }
253         ret = sqlite3_bind_text(stmt, 8, cmd->appid, -1, SQLITE_TRANSIENT);
254         if (ret != SQLITE_OK) {
255                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
256                 sqlite3_clear_bindings(stmt);
257                 sqlite3_finalize(stmt);
258                 return VC_DB_ERROR_OPERATION_FAILED;
259         }
260         if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
261                 ret = sqlite3_bind_text(stmt, 9, cmd->invocation_name, -1, SQLITE_TRANSIENT);
262                 if (ret != SQLITE_OK) {
263                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
264                         sqlite3_clear_bindings(stmt);
265                         sqlite3_finalize(stmt);
266                         return VC_DB_ERROR_OPERATION_FAILED;
267                 }
268         }
269         ret = sqlite3_bind_text(stmt, 10, cmd->fixed, -1, SQLITE_TRANSIENT);
270         if (ret != SQLITE_OK) {
271                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
272                 sqlite3_clear_bindings(stmt);
273                 sqlite3_finalize(stmt);
274                 return VC_DB_ERROR_OPERATION_FAILED;
275         }
276         ret = sqlite3_step(stmt);
277         if (ret != SQLITE_DONE) {
278                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
279                 sqlite3_clear_bindings(stmt);
280                 sqlite3_finalize(stmt);
281                 return VC_DB_ERROR_OPERATION_FAILED;
282         }
283
284         SLOG(LOG_INFO, vc_db_tag(), "[SQL] INSERT INTO vc_info (id, pid(%d), type(%d), format(%d), domain(%d), command(%s)", pid, cmd->type, cmd->format, cmd->domain, cmd->command);
285         SLOG(LOG_INFO, vc_db_tag(), "[SQL] @@ appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
286
287         sqlite3_reset(stmt);
288         sqlite3_clear_bindings(stmt);
289         sqlite3_finalize(stmt);
290         return VC_DB_ERROR_NONE;
291 }
292
293 static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
294 {
295         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
296
297         int ret = 0;
298         sqlite3_stmt* stmt = NULL;
299         char* sql = NULL;
300         char* appid = NULL;
301
302         if (VC_COMMAND_TYPE_BACKGROUND == type) {
303                 /* For background command */
304                 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
305         } else {
306                 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
307         }
308
309         if (NULL == sql) {
310                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
311                 return VC_DB_ERROR_OUT_OF_MEMORY;
312         }
313         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
314         if (ret != SQLITE_OK) {
315                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
316                 free(sql);
317                 sql = NULL;
318                 return VC_DB_ERROR_OPERATION_FAILED;
319         }
320
321         ret = sqlite3_bind_int(stmt, 1, (int)type);
322         if (ret != SQLITE_OK) {
323                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
324                 sqlite3_finalize(stmt);
325                 free(sql);
326                 sql = NULL;
327                 return VC_DB_ERROR_OPERATION_FAILED;
328         }
329         if (VC_COMMAND_TYPE_BACKGROUND != type) {
330                 ret = sqlite3_bind_int(stmt, 2, pid);
331                 if (ret != SQLITE_OK) {
332                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
333                         sqlite3_clear_bindings(stmt);
334                         sqlite3_finalize(stmt);
335                         free(sql);
336                         sql = NULL;
337                         return VC_DB_ERROR_OPERATION_FAILED;
338                 }
339         }
340
341         if (VC_COMMAND_TYPE_BACKGROUND == type)
342                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
343         else
344                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
345
346         ret = sqlite3_step(stmt);
347         if (ret == SQLITE_DONE) {
348                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
349                 sqlite3_clear_bindings(stmt);
350                 sqlite3_finalize(stmt);
351                 free(sql);
352                 sql = NULL;
353                 return VC_DB_ERROR_NONE;
354         }
355
356         if (VC_COMMAND_TYPE_BACKGROUND == type && -1 != pid) {
357                 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
358                         SLOG(LOG_WARN, vc_db_tag(), "[WARN] fail to get app id, pid(%d)", pid);
359                 }
360         }
361
362         while (SQLITE_ROW == ret) {
363                 int temp = 0;
364                 int ret = -1;
365                 char* temp_text = NULL;
366                 vc_cmd_h temp_cmd;
367                 ret = vc_cmd_create(&temp_cmd);
368
369                 if (0 != ret) {
370                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
371                         if (NULL != appid) {
372                                 free(appid);
373                                 appid = NULL;
374                         }
375                         sqlite3_reset(stmt);
376                         sqlite3_clear_bindings(stmt);
377                         sqlite3_finalize(stmt);
378                         free(sql);
379                         sql = NULL;
380                         return VC_DB_ERROR_OUT_OF_MEMORY;
381                 }
382
383                 temp_text = (char*)sqlite3_column_text(stmt, 7);
384                 if (NULL != temp_text)
385                         vc_cmd_set_appid(temp_cmd, temp_text);
386
387                 ret = vc_cmd_get_appid(temp_cmd, &temp_text);
388                 if (NULL != appid && 0 == ret) {
389                         if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_text, strlen(appid))) {
390                                 SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
391
392                                 free(temp_text);
393                                 temp_text = NULL;
394
395                                 ret = sqlite3_step(stmt);
396                                 if (SQLITE_DONE == ret)
397                                         break;
398
399                                 continue;
400                         }
401                 }
402
403                 if (NULL != temp_text) {
404                         free(temp_text);
405                         temp_text = NULL;
406                 }
407
408                 temp = sqlite3_column_int(stmt, 0);
409                 vc_cmd_set_id(temp_cmd, temp);
410
411                 temp = sqlite3_column_int(stmt, 1);
412                 vc_cmd_set_pid(temp_cmd, temp);
413
414                 temp = sqlite3_column_int(stmt, 2);
415                 vc_cmd_set_type(temp_cmd, temp);
416
417                 temp = sqlite3_column_int(stmt, 3);
418                 vc_cmd_set_format(temp_cmd, temp);
419
420                 temp  = sqlite3_column_int(stmt, 4);
421                 vc_cmd_set_domain(temp_cmd, temp);
422
423                 temp_text = (char*)sqlite3_column_text(stmt, 5);
424                 if (NULL != temp_text)
425                         vc_cmd_set_command(temp_cmd, temp_text);
426
427                 temp_text = (char*)sqlite3_column_text(stmt, 6);
428                 if (NULL != temp_text)
429                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
430
431                 temp_text = (char*)sqlite3_column_text(stmt, 8);
432                 if (NULL != temp_text)
433                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
434
435                 temp_text = (char*)sqlite3_column_text(stmt, 9);
436                 if (NULL != temp_text)
437                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
438
439                 ret = vc_cmd_get_type(temp_cmd, &temp);
440                 if (0 == ret && type == temp) {
441                         *cmd_list = g_slist_append(*cmd_list, temp_cmd);
442                 } else {
443                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp, type);
444
445                         vc_cmd_destroy((vc_cmd_h)temp_cmd);
446                 }
447                 ret = sqlite3_step(stmt);
448                 if (SQLITE_DONE == ret)
449                         break;
450         }
451
452         if (NULL != appid) {
453                 free(appid);
454                 appid = NULL;
455         }
456
457         sqlite3_reset(stmt);
458         sqlite3_clear_bindings(stmt);
459         sqlite3_finalize(stmt);
460
461         free(sql);
462         sql = NULL;
463         return VC_DB_ERROR_NONE;
464 }
465
466 static int __vc_db_get_pid(const char* appid, int* pid)
467 {
468         bool running = false;
469         int ret = app_manager_is_running(appid, &running);
470         if (APP_MANAGER_ERROR_NONE != ret) {
471                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to check running with appid(%s)", appid);
472                 return VC_DB_ERROR_OPERATION_FAILED;
473         }
474         if (true == running) {
475                 app_context_h app_context = NULL;
476                 ret = app_manager_get_app_context(appid, &app_context);
477                 if (APP_MANAGER_ERROR_NONE != ret) {
478                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
479                         return VC_DB_ERROR_OPERATION_FAILED;
480                 }
481
482                 ret = app_context_get_pid(app_context, pid);
483                 if (APP_MANAGER_ERROR_NONE != ret) {
484                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get pid, ret(%d), appid(%s)", ret, appid);
485                         return VC_DB_ERROR_OPERATION_FAILED;
486                 }
487
488                 ret = app_context_destroy(app_context);
489                 if (APP_MANAGER_ERROR_NONE != ret) {
490                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to destroy app context, ret(%d), appid(%s)", ret, appid);
491                         return VC_DB_ERROR_OPERATION_FAILED;
492                 }
493         } else {
494                 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
495         }
496         return VC_DB_ERROR_NONE;
497 }
498
499 static int __vc_db_insert_result(const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
500 {
501         SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
502
503         sqlite3_stmt* stmt = NULL;
504         const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
505         int ret = 0;
506         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
507         if (ret != SQLITE_OK) {
508                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
509                 return VC_DB_ERROR_OPERATION_FAILED;
510         }
511         ret = sqlite3_bind_text(stmt, 2, result_text, -1, SQLITE_TRANSIENT);
512         if (ret != SQLITE_OK) {
513                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
514                 sqlite3_finalize(stmt);
515                 return VC_DB_ERROR_OPERATION_FAILED;
516         }
517         ret = sqlite3_bind_int(stmt, 3, event);
518         if (ret != SQLITE_OK) {
519                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
520                 sqlite3_clear_bindings(stmt);
521                 sqlite3_finalize(stmt);
522                 return VC_DB_ERROR_OPERATION_FAILED;
523         }
524         ret = sqlite3_bind_text(stmt, 4, msg, -1, SQLITE_TRANSIENT);
525         if (ret != SQLITE_OK) {
526                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
527                 sqlite3_clear_bindings(stmt);
528                 sqlite3_finalize(stmt);
529                 return VC_DB_ERROR_OPERATION_FAILED;
530         }
531         ret = sqlite3_bind_int(stmt, 5, exclusive);
532         if (ret != SQLITE_OK) {
533                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
534                 sqlite3_clear_bindings(stmt);
535                 sqlite3_finalize(stmt);
536                 return VC_DB_ERROR_OPERATION_FAILED;
537         }
538
539         if (NULL == cmd) {
540                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d))", result_text, event, msg, exclusive);
541
542                 ret = sqlite3_step(stmt);
543                 if (ret != SQLITE_DONE) {
544                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
545                         sqlite3_clear_bindings(stmt);
546                         sqlite3_finalize(stmt);
547                         return VC_DB_ERROR_OPERATION_FAILED;
548                 }
549                 sqlite3_reset(stmt);
550                 sqlite3_clear_bindings(stmt);
551                 sqlite3_finalize(stmt);
552                 return VC_DB_ERROR_NONE;
553         }
554
555         if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
556                 int pid = -1;
557                 ret = __vc_db_get_pid(cmd->appid, &pid);
558                 if (0 != ret) {
559                         SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
560                 } else {
561                         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
562                         if (-1 != pid)
563                                 cmd->pid = pid;
564                 }
565         }
566         ret = sqlite3_bind_int(stmt, 6, cmd->pid);
567         if (ret != SQLITE_OK) {
568                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
569                 sqlite3_clear_bindings(stmt);
570                 sqlite3_finalize(stmt);
571                 return VC_DB_ERROR_OPERATION_FAILED;
572         }
573         ret = sqlite3_bind_int(stmt, 7, cmd->type);
574         if (ret != SQLITE_OK) {
575                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
576                 sqlite3_clear_bindings(stmt);
577                 sqlite3_finalize(stmt);
578                 return VC_DB_ERROR_OPERATION_FAILED;
579         }
580         ret = sqlite3_bind_int(stmt, 8, cmd->format);
581         if (ret != SQLITE_OK) {
582                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
583                 sqlite3_clear_bindings(stmt);
584                 sqlite3_finalize(stmt);
585                 return VC_DB_ERROR_OPERATION_FAILED;
586         }
587         ret = sqlite3_bind_int(stmt, 9, cmd->domain);
588         if (ret != SQLITE_OK) {
589                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
590                 sqlite3_clear_bindings(stmt);
591                 sqlite3_finalize(stmt);
592                 return VC_DB_ERROR_OPERATION_FAILED;
593         }
594         ret = sqlite3_bind_text(stmt, 10, cmd->command, -1, SQLITE_TRANSIENT);
595         if (ret != SQLITE_OK) {
596                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
597                 sqlite3_clear_bindings(stmt);
598                 sqlite3_finalize(stmt);
599                 return VC_DB_ERROR_OPERATION_FAILED;
600         }
601         ret = sqlite3_bind_text(stmt, 11, cmd->parameter, -1, SQLITE_TRANSIENT);
602         if (ret != SQLITE_OK) {
603                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
604                 sqlite3_clear_bindings(stmt);
605                 sqlite3_finalize(stmt);
606                 return VC_DB_ERROR_OPERATION_FAILED;
607         }
608         ret = sqlite3_bind_text(stmt, 12, cmd->appid, -1, SQLITE_TRANSIENT);
609         if (ret != SQLITE_OK) {
610                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
611                 sqlite3_clear_bindings(stmt);
612                 sqlite3_finalize(stmt);
613                 return VC_DB_ERROR_OPERATION_FAILED;
614         }
615         ret = sqlite3_bind_text(stmt, 13, cmd->invocation_name, -1, SQLITE_TRANSIENT);
616         if (ret != SQLITE_OK) {
617                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
618                 sqlite3_clear_bindings(stmt);
619                 sqlite3_finalize(stmt);
620                 return VC_DB_ERROR_OPERATION_FAILED;
621         }
622         ret = sqlite3_bind_text(stmt, 14, cmd->fixed, -1, SQLITE_TRANSIENT);
623         if (ret != SQLITE_OK) {
624                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
625                 sqlite3_clear_bindings(stmt);
626                 sqlite3_finalize(stmt);
627                 return VC_DB_ERROR_OPERATION_FAILED;
628         }
629         ret = sqlite3_step(stmt);
630         if (ret != SQLITE_DONE) {
631                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
632                 sqlite3_clear_bindings(stmt);
633                 sqlite3_finalize(stmt);
634                 return VC_DB_ERROR_OPERATION_FAILED;
635         }
636
637         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d), pid(%d), type(%d), format(%d), domain(%d), command(%s), parameter(%s), appid(%s), invocation(%s), fixed(%s)",
638                                 result_text, event, msg, exclusive, cmd->pid, cmd->type, cmd->format, cmd->domain, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
639
640         sqlite3_reset(stmt);
641         sqlite3_clear_bindings(stmt);
642         sqlite3_finalize(stmt);
643
644         return VC_DB_ERROR_NONE;
645 }
646
647 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
648 {
649         if (NULL == org_cmd || NULL == new_cmd) {
650                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
651                 return VC_DB_ERROR_INVALID_PARAMETER;
652         }
653
654         if (NULL == invocation_name) {
655                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, invocation name is NULL, org_cmd(%s)", org_cmd);
656                 return VC_DB_ERROR_INVALID_PARAMETER;
657         }
658
659         if (strlen(org_cmd) <= strlen(invocation_name)) {
660                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
661                 return VC_DB_ERROR_INVALID_PARAMETER;
662         }
663
664         if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
665                 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
666         }
667         SLOG(LOG_DEBUG, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, *new_cmd, invocation_name);
668         return VC_DB_ERROR_NONE;
669 }
670
671 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
672 {
673         if (NULL == command || NULL == temp_unfixed) {
674                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
675                 return VC_DB_ERROR_INVALID_PARAMETER;
676         }
677
678         if (NULL == fixed) {
679                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, fixed cmd is NULL, org_cmd(%s)", command);
680                 return VC_DB_ERROR_INVALID_PARAMETER;
681         }
682
683         if (strlen(command) <= strlen(fixed)) {
684                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to extract unfixed command, cmd(%s) fixed(%s)", command, fixed);
685                 return VC_DB_ERROR_INVALID_PARAMETER;
686         }
687
688         char* temp = (char*)calloc(256, sizeof(char));
689         if (0 == strncasecmp(command, fixed, strlen(fixed))) {
690                 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
691                 SLOG(LOG_WARN, vc_db_tag(), "@@@");
692         } else  if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
693                 strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
694                 SLOG(LOG_WARN, vc_db_tag(), "@@@");
695         }
696
697         SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
698         if (NULL != temp) {
699                 *temp_unfixed = strdup(temp);
700                 free(temp);
701                 temp = NULL;
702         }
703         return VC_DB_ERROR_NONE;
704 }
705
706 static int __vc_db_get_result(char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
707 {
708         int ret = 0;
709         sqlite3_stmt* stmt = NULL;
710         char* sql = NULL;
711         if (-1 == pid)
712                 sql = strdup("SELECT * FROM vc_result;");
713         else if (NULL != appid)
714                 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
715         else
716                 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
717
718         if (NULL == sql) {
719                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
720                 return VC_DB_ERROR_OUT_OF_MEMORY;
721         }
722
723         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
724         if (ret != SQLITE_OK) {
725                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
726                 free(sql);
727                 sql = NULL;
728                 return VC_DB_ERROR_OPERATION_FAILED;
729         }
730         if (NULL != appid) {
731                 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
732                 if (ret != SQLITE_OK) {
733                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
734                         sqlite3_finalize(stmt);
735                         free(sql);
736                         sql = NULL;
737                         return VC_DB_ERROR_OPERATION_FAILED;
738                 }
739         } else if (-1 != pid) {
740                 ret = sqlite3_bind_int(stmt, 1, pid);
741                 if (ret != SQLITE_OK) {
742                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
743                         sqlite3_finalize(stmt);
744                         free(sql);
745                         sql = NULL;
746                         return VC_DB_ERROR_OPERATION_FAILED;
747                 }
748         }
749         ret = sqlite3_step(stmt);
750         if (ret == SQLITE_DONE) {
751                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
752                 sqlite3_clear_bindings(stmt);
753                 sqlite3_finalize(stmt);
754                 free(sql);
755                 sql = NULL;
756                 return VC_DB_ERROR_NONE;
757         }
758
759         if (-1 == pid)
760                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
761         else if (NULL != appid)
762                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %s;", appid);
763
764         else
765                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
766
767         vc_cmd_h temp_cmd = NULL;
768         while (SQLITE_ROW == ret) {
769                 int temp = 0;
770                 char* temp_text = NULL;
771                 const char* invocation_name = NULL;
772
773                 temp_text = (char*)sqlite3_column_text(stmt, 1);
774                 if (NULL != temp_text)
775                         *result_text = strdup(temp_text);
776
777                 temp = sqlite3_column_int(stmt, 2);
778                 *event = temp;
779
780                 if (NULL != msg) {
781                         temp_text = (char*)sqlite3_column_text(stmt, 3);
782                         if (NULL != temp_text)
783                                 *msg = strdup(temp_text);
784                 }
785
786                 if (0 != vc_cmd_create(&temp_cmd)) {
787                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
788                         if (NULL != *result_text) {
789                                 free(*result_text);
790                                 *result_text = NULL;
791                         }
792
793                         if (NULL != msg && NULL != *msg) {
794                                 free(*msg);
795                                 *msg = NULL;
796                         }
797
798                         sqlite3_reset(stmt);
799                         sqlite3_clear_bindings(stmt);
800                         sqlite3_finalize(stmt);
801                         free(sql);
802                         sql = NULL;
803                         return -1;
804                 }
805
806                 temp = sqlite3_column_int(stmt, 0);
807                 vc_cmd_set_id(temp_cmd, temp);
808
809                 temp = sqlite3_column_int(stmt, 5);
810                 vc_cmd_set_pid(temp_cmd, temp);
811
812                 temp = sqlite3_column_int(stmt, 6);
813                 vc_cmd_set_type(temp_cmd, temp);
814
815                 temp = sqlite3_column_int(stmt, 7);
816                 vc_cmd_set_format(temp_cmd, temp);
817
818                 temp = sqlite3_column_int(stmt, 8);
819                 vc_cmd_set_domain(temp_cmd, temp);
820
821                 // invocation name
822                 invocation_name = (char*)sqlite3_column_text(stmt, 12);
823                 // command name
824                 temp_text = (char*)sqlite3_column_text(stmt, 9);
825                 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
826
827                 if (NULL != temp_text) {
828                         char* temp_command = NULL;
829                         char* temp_unfixed = NULL;
830                         char* temp_fixed = NULL;
831
832                         if (NULL != invocation_name)
833                                 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
834
835                         if (NULL == temp_command) {
836                                 temp_command = strdup(temp_text);
837                         } else {
838                                 // remove invocation name from result_text
839                                 free(*result_text);
840                                 *result_text = strdup(temp_command);
841                         }
842
843                         // fixed
844                         temp_fixed = (char*)sqlite3_column_text(stmt, 13);
845                         if (NULL != temp_fixed)
846                                 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
847
848                         // unfixed
849                         temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
850
851                         if (NULL != temp_unfixed) {
852                                 char merge_result[256] = {0, };
853                                 vc_cmd_set_command(temp_cmd, temp_command);
854                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
855                                 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
856                                 if (NULL != *result_text)
857                                         free(*result_text);
858                                 *result_text = strdup(merge_result);
859                         } else if (NULL != temp_fixed) {
860                                 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
861                                 vc_cmd_set_command(temp_cmd, temp_fixed);
862                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
863                                 if (NULL != temp_unfixed) {
864                                         free(temp_unfixed);
865                                         temp_unfixed = NULL;
866                                 }
867                         } else {
868                                 vc_cmd_set_command(temp_cmd, temp_command);
869                         }
870                         free(temp_command);
871                         temp_command = NULL;
872                 }
873
874                 temp_text = (char*)sqlite3_column_text(stmt, 11);
875                 if (NULL != temp_text)
876                         vc_cmd_set_appid(temp_cmd, temp_text);
877
878                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
879                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
880                         vc_cmd_destroy(temp_cmd);
881                         vc_cmd_list_destroy(vc_cmd_list, true);
882                         if (NULL != *result_text) {
883                                 free(*result_text);
884                                 *result_text = NULL;
885                         }
886
887                         if (NULL != msg && NULL != *msg) {
888                                 free(*msg);
889                                 *msg = NULL;
890                         }
891
892                         sqlite3_reset(stmt);
893                         sqlite3_clear_bindings(stmt);
894                         sqlite3_finalize(stmt);
895                         free(sql);
896                         sql = NULL;
897                         return VC_DB_ERROR_OPERATION_FAILED;
898                 }
899
900                 ret = sqlite3_step(stmt);
901                 if (SQLITE_DONE == ret)
902                         break;
903         }
904
905         sqlite3_reset(stmt);
906         sqlite3_clear_bindings(stmt);
907         sqlite3_finalize(stmt);
908
909         free(sql);
910         sql = NULL;
911         return VC_DB_ERROR_NONE;
912 }
913
914 void __vc_db_demandable_client_free(void* data)
915 {
916         vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data;
917
918         if (NULL != d_app) {
919                 if (NULL != d_app->appid) {
920                         free(d_app->appid);
921                         d_app->appid = NULL;
922                 }
923
924                 free(d_app);
925         }
926 }
927
928 static int __vc_db_get_appid(const char* result, GSList** app_list)
929 {
930         GSList* temp_app_list = NULL;
931
932         int ret = 0;
933         sqlite3_stmt* stmt = NULL;
934         const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
935
936         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
937         if (ret != SQLITE_OK) {
938                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
939                 return VC_DB_ERROR_OPERATION_FAILED;
940         }
941         ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
942         if (ret != SQLITE_OK) {
943                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
944                 sqlite3_finalize(stmt);
945                 return VC_DB_ERROR_OPERATION_FAILED;
946         }
947         ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
948         if (ret != SQLITE_OK) {
949                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
950                 sqlite3_clear_bindings(stmt);
951                 sqlite3_finalize(stmt);
952                 return VC_DB_ERROR_OPERATION_FAILED;
953         }
954         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
955         ret = sqlite3_step(stmt);
956         if (ret == SQLITE_DONE) {
957                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
958                 sqlite3_clear_bindings(stmt);
959                 sqlite3_finalize(stmt);
960                 return VC_DB_ERROR_NONE;
961         }
962
963         while (SQLITE_ROW == ret) {
964                 char* temp_text = NULL;
965                 vc_deactivated_app_s* temp_app = NULL;
966                 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
967                 if (NULL == temp_app) {
968                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
969
970                         if (NULL != temp_app_list) {
971                                 g_slist_free_full(temp_app_list, __vc_db_demandable_client_free);
972                                 temp_app_list = NULL;
973                         }
974
975                         sqlite3_reset(stmt);
976                         sqlite3_clear_bindings(stmt);
977                         sqlite3_finalize(stmt);
978                         return VC_DB_ERROR_OUT_OF_MEMORY;
979                 }
980
981                 temp_text = (char*)sqlite3_column_text(stmt, 11);
982                 if (NULL != temp_text)
983                         temp_app->appid = strdup(temp_text);
984
985                 temp_app_list = g_slist_append(temp_app_list, temp_app);
986
987                 ret = sqlite3_step(stmt);
988                 if (SQLITE_DONE == ret)
989                         break;
990         }
991
992         *app_list = temp_app_list;
993
994         sqlite3_reset(stmt);
995         sqlite3_clear_bindings(stmt);
996         sqlite3_finalize(stmt);
997         return VC_DB_ERROR_NONE;
998 }
999
1000 int __vc_db_get_result_pid_list(const char* result, GSList** pid_list)
1001 {
1002         GSList* temp_pid_list = NULL;
1003
1004         int ret = 0;
1005         sqlite3_stmt* stmt = NULL;
1006         const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
1007
1008
1009         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1010         if (ret != SQLITE_OK) {
1011                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1012                 return VC_DB_ERROR_OPERATION_FAILED;
1013         }
1014         ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
1015         if (ret != SQLITE_OK) {
1016                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1017                 sqlite3_finalize(stmt);
1018                 return VC_DB_ERROR_OPERATION_FAILED;
1019         }
1020         ret = sqlite3_step(stmt);
1021         if (ret == SQLITE_DONE) {
1022                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1023                 sqlite3_clear_bindings(stmt);
1024                 sqlite3_finalize(stmt);
1025                 return VC_DB_ERROR_NONE;
1026         }
1027
1028         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
1029
1030         while (SQLITE_ROW == ret) {
1031                 int temp = 0;
1032                 vc_cmd_s* temp_cmd = NULL;
1033                 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
1034                 if (NULL == temp_cmd) {
1035                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
1036
1037                         if (NULL != temp_pid_list) {
1038                                 g_slist_free_full(temp_pid_list, free);
1039                                 temp_pid_list = NULL;
1040                         }
1041
1042                         sqlite3_reset(stmt);
1043                         sqlite3_clear_bindings(stmt);
1044                         sqlite3_finalize(stmt);
1045                         return VC_DB_ERROR_OUT_OF_MEMORY;
1046                 }
1047
1048                 temp = sqlite3_column_int(stmt, 0);
1049                 temp_cmd->pid = temp;
1050
1051                 temp = sqlite3_column_int(stmt, 1);
1052                 temp_cmd->type = temp;
1053
1054                 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
1055
1056                 ret = sqlite3_step(stmt);
1057                 if (SQLITE_DONE == ret)
1058                         break;
1059         }
1060
1061         *pid_list = temp_pid_list;
1062
1063         sqlite3_reset(stmt);
1064         sqlite3_clear_bindings(stmt);
1065         sqlite3_finalize(stmt);
1066         return VC_DB_ERROR_NONE;
1067 }
1068
1069 static int __vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
1070 {
1071         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
1072
1073         int ret = 0;
1074         sqlite3_stmt* stmt = NULL;
1075         const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
1076
1077         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1078         if (ret != SQLITE_OK) {
1079                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1080                 return VC_DB_ERROR_OPERATION_FAILED;
1081         }
1082         ret = sqlite3_bind_int(stmt, 1, pid);
1083         if (ret != SQLITE_OK) {
1084                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1085                 sqlite3_finalize(stmt);
1086                 return VC_DB_ERROR_OPERATION_FAILED;
1087         }
1088         ret = sqlite3_bind_int(stmt, 2, type);
1089         if (ret != SQLITE_OK) {
1090                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1091                 sqlite3_clear_bindings(stmt);
1092                 sqlite3_finalize(stmt);
1093                 return VC_DB_ERROR_OPERATION_FAILED;
1094         }
1095         ret = sqlite3_step(stmt);
1096         if (ret == SQLITE_DONE) {
1097                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1098                 sqlite3_clear_bindings(stmt);
1099                 sqlite3_finalize(stmt);
1100                 return VC_DB_ERROR_NONE;
1101         }
1102
1103         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
1104
1105         vc_cmd_h temp_cmd = NULL;
1106
1107         while (SQLITE_ROW == ret) {
1108                 int temp = 0;
1109                 char* temp_text = NULL;
1110
1111                 if (0 != vc_cmd_create(&temp_cmd)) {
1112                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
1113                         sqlite3_reset(stmt);
1114                         sqlite3_clear_bindings(stmt);
1115                         sqlite3_finalize(stmt);
1116                         return -1;
1117                 }
1118                 temp = sqlite3_column_int(stmt, 0);
1119                 vc_cmd_set_id(temp_cmd, temp);
1120
1121                 temp = sqlite3_column_int(stmt, 1);
1122                 vc_cmd_set_pid(temp_cmd, temp);
1123
1124                 temp = sqlite3_column_int(stmt, 2);
1125                 vc_cmd_set_type(temp_cmd, temp);
1126
1127                 temp = sqlite3_column_int(stmt, 3);
1128                 vc_cmd_set_format(temp_cmd, temp);
1129
1130                 temp = sqlite3_column_int(stmt, 4);
1131                 vc_cmd_set_domain(temp_cmd, temp);
1132
1133                 temp_text = (char*)sqlite3_column_text(stmt, 5);
1134                 if (NULL != temp_text)
1135                         vc_cmd_set_command(temp_cmd, temp_text);
1136
1137                 temp_text = (char*)sqlite3_column_text(stmt, 6);
1138                 if (NULL != temp_text)
1139                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
1140
1141                 temp_text = (char*)sqlite3_column_text(stmt, 7);
1142                 if (NULL != temp_text)
1143                         vc_cmd_set_appid(temp_cmd, temp_text);
1144
1145                 temp_text = (char*)sqlite3_column_text(stmt, 8);
1146                 if (NULL != temp_text)
1147                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
1148
1149                 temp_text = (char*)sqlite3_column_text(stmt, 9);
1150                 if (NULL != temp_text)
1151                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
1152
1153                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1154                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1155                         vc_cmd_destroy(temp_cmd);
1156                         vc_cmd_list_destroy(vc_cmd_list, true);
1157                         sqlite3_reset(stmt);
1158                         sqlite3_clear_bindings(stmt);
1159                         sqlite3_finalize(stmt);
1160                         return VC_DB_ERROR_OPERATION_FAILED;
1161                 }
1162
1163                 ret = sqlite3_step(stmt);
1164                 if (SQLITE_DONE == ret)
1165                         break;
1166         }
1167
1168         sqlite3_reset(stmt);
1169         sqlite3_clear_bindings(stmt);
1170         sqlite3_finalize(stmt);
1171         return VC_DB_ERROR_NONE;
1172 }
1173
1174 static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
1175 {
1176         if (NULL == src_cmd) {
1177                 SLOG(LOG_WARN, vc_db_tag(), "[Client Data] Input command is NULL");
1178                 return NULL;
1179         }
1180
1181         vc_cmd_s* temp_cmd = NULL;
1182         temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
1183         if (NULL == temp_cmd) {
1184                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1185                 return NULL;
1186         }
1187
1188         temp_cmd->id = src_cmd->id;
1189         temp_cmd->pid = src_cmd->pid;
1190         temp_cmd->index = src_cmd->index;
1191         temp_cmd->type = src_cmd->type;
1192         temp_cmd->format = src_cmd->format;
1193         temp_cmd->domain = src_cmd->domain;
1194
1195         if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type)           temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
1196         else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
1197         else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1198         else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1199         else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
1200         else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
1201
1202         if (NULL != src_cmd->command) {
1203                 temp_cmd->command = strdup(src_cmd->command);
1204         }
1205
1206         if (NULL != src_cmd->parameter) {
1207                 temp_cmd->parameter = strdup(src_cmd->parameter);
1208         }
1209
1210         if (NULL != src_cmd->appid) {
1211                 temp_cmd->appid = strdup(src_cmd->appid);
1212         }
1213
1214         if (NULL != src_cmd->invocation_name) {
1215                 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
1216         }
1217
1218         if (NULL != src_cmd->fixed) {
1219                 temp_cmd->fixed = strdup(src_cmd->fixed);
1220         }
1221
1222         temp_cmd->key = src_cmd->key;
1223         temp_cmd->modifier = src_cmd->modifier;
1224
1225         return temp_cmd;
1226 }
1227
1228 int vc_db_initialize(void)
1229 {
1230         SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
1231
1232         if (0 < g_ref_cnt) {
1233                 g_ref_cnt++;
1234                 return VC_DB_ERROR_NONE;
1235         }
1236
1237         path = (char*)calloc(256, sizeof(char));
1238         if (NULL == path) {
1239                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1240                 return VC_DB_ERROR_OUT_OF_MEMORY;
1241         }
1242
1243         /* This should be changed to general DB space - TZ_USER_DB */
1244         snprintf(path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1245
1246         struct stat stat;
1247         int ret = db_util_open(path, &db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1248         if (ret != SQLITE_OK) {
1249                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", path, ret, sqlite3_errmsg(db_handle));
1250                 if (db_handle) {
1251                         db_util_close(db_handle);
1252                         db_handle = NULL;
1253                 }
1254
1255                 free(path);
1256                 path = NULL;
1257                 return VC_DB_ERROR_OPERATION_FAILED;
1258         }
1259
1260         if (lstat(path, &stat) < 0) {
1261                 char buf_err[256];
1262                 SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
1263                 if (db_handle)
1264                         db_util_close(db_handle);
1265                 db_handle = NULL;
1266
1267                 free(path);
1268                 path = NULL;
1269                 return VC_DB_ERROR_OPERATION_FAILED;
1270         }
1271
1272         if (!S_ISREG(stat.st_mode)) {
1273                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1274                 if (db_handle)
1275                         db_util_close(db_handle);
1276                 db_handle = NULL;
1277
1278                 free(path);
1279                 path = NULL;
1280                 return VC_DB_ERROR_OPERATION_FAILED;
1281         }
1282
1283         if (!stat.st_size) {
1284             vc_db_create_table();
1285         }
1286
1287         if (db_handle) {
1288                 char* err_msg = NULL;
1289                 static const const char* sql = "PRAGMA journal_mode = WAL";
1290                 int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
1291                 if (ret != SQLITE_OK) {
1292                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1293                 }
1294         }
1295         g_ref_cnt++;
1296         return VC_DB_ERROR_NONE;
1297 }
1298
1299 int vc_db_finalize(void)
1300 {
1301         if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
1302         if (0 != --g_ref_cnt)
1303                 return VC_DB_ERROR_NONE;
1304
1305         if (NULL != path) {
1306                 free(path);
1307                 path = NULL;
1308         }
1309
1310         if (!db_handle)
1311                 return 0;
1312
1313         db_util_close(db_handle);
1314
1315         db_handle = NULL;
1316         return VC_DB_ERROR_NONE;
1317 }
1318
1319 int vc_db_create_table(void)
1320 {
1321         __vc_db_begin_transaction();
1322
1323         const char* vc_info_sql = "CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
1324                                         command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1325         const char* vc_result_sql = "CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
1326                                         pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1327
1328         int ret = __vc_db_exec_query(vc_info_sql);
1329         if (ret != VC_DB_ERROR_NONE) {
1330                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1331                 __vc_db_rollback_transaction();
1332                 return VC_DB_ERROR_OPERATION_FAILED;
1333         }
1334         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_info_sql);
1335
1336         ret = __vc_db_exec_query(vc_result_sql);
1337         if (ret != VC_DB_ERROR_NONE) {
1338                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1339                 __vc_db_rollback_transaction();
1340                 return ret;
1341         }
1342         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_result_sql);
1343
1344         __vc_db_commit_transaction();
1345         return VC_DB_ERROR_NONE;
1346 }
1347
1348 int vc_db_delete_table(const char* table)
1349 {
1350         char* sql = NULL;
1351         if (0 == strncmp(table, "result", strlen(table))) {
1352                 sql = strdup("DELETE FROM vc_result;");
1353         } else if (0 == strncmp(table, "command", strlen(table))) {
1354                 sql = strdup("DELETE_FROM vc_info;");
1355         } else {
1356                 return VC_DB_ERROR_INVALID_PARAMETER;
1357         }
1358
1359         if (NULL == sql) {
1360                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1361                 return VC_DB_ERROR_OUT_OF_MEMORY;
1362         }
1363
1364         __vc_db_begin_transaction();
1365
1366         int ret = __vc_db_exec_query(sql);
1367         if (ret != VC_DB_ERROR_NONE) {
1368                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1369                 __vc_db_rollback_transaction();
1370                 free(sql);
1371                 sql = NULL;
1372                 return ret;
1373         }
1374
1375         free(sql);
1376         sql = NULL;
1377
1378         if (0 == strncmp(table, "result", strlen(table))) {
1379                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1380         } else if (0 == strncmp(table, "command", strlen(table))) {
1381                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1382         }
1383
1384         if (NULL == sql) {
1385                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1386                 return VC_DB_ERROR_OUT_OF_MEMORY;
1387         }
1388
1389         ret = __vc_db_exec_query(sql);
1390         if (ret != VC_DB_ERROR_NONE) {
1391                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1392                 __vc_db_rollback_transaction();
1393                 free(sql);
1394                 sql = NULL;
1395                 return ret;
1396         }
1397
1398         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1399         __vc_db_commit_transaction();
1400
1401         free(sql);
1402         sql = NULL;
1403         return VC_DB_ERROR_NONE;
1404 }
1405
1406 int vc_db_begin_transaction(void)
1407 {
1408         int ret = __vc_db_begin_transaction();
1409         return ret;
1410 }
1411
1412 int vc_db_rollback_transaction(void)
1413 {
1414         int ret = __vc_db_rollback_transaction();
1415         return ret;
1416 }
1417
1418 int vc_db_commit_transaction(void)
1419 {
1420         int ret = __vc_db_commit_transaction();
1421         return ret;
1422 }
1423
1424 static void __vc_db_remove_space(char** string)
1425 {
1426         if (NULL == string || NULL == *string)
1427                 return;
1428
1429         char* temp = *string;
1430
1431         //remove previous space
1432         if (' ' == temp[0])
1433                 strncpy(temp, temp + 1, strlen(temp));
1434
1435         // remove next space
1436         if (' ' == temp[strlen(temp) - 1])
1437                 temp[strlen(temp) - 1] = '\0';
1438 }
1439
1440 static bool __vc_db_is_valid_vfixed_string(char* string)
1441 {
1442         char* temp = strchr(string, '}');
1443         if (NULL == temp)
1444                 return false;
1445
1446         temp = strchr(string, '{');
1447         if (NULL == temp)
1448                 return false;
1449
1450         temp = strchr(string, '|');
1451         if (NULL == temp)
1452                 return false;
1453         return true;
1454 }
1455
1456 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1457 {
1458         if (NULL == cmd || NULL == cmd->command) {
1459                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
1460                 return VC_DB_ERROR_INVALID_PARAMETER;
1461         }
1462
1463         GSList* temp_list = NULL;
1464         char* temp = NULL;
1465         char* src_cmd = strdup(cmd->command);
1466         char* dst_cmd = NULL;
1467         char merge_cmd[256] = {0, };
1468
1469         if (NULL == src_cmd) {
1470                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1471                 return VC_DB_ERROR_OUT_OF_MEMORY;
1472         }
1473
1474         if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
1475                 // check string validation
1476                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1477                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1478                         free(src_cmd);
1479                         src_cmd = NULL;
1480                         return VC_DB_ERROR_INVALID_PARAMETER;
1481                 }
1482
1483                 // remove close brace, '}'
1484                 char* temp_close = strchr(src_cmd, '}');
1485                 if (NULL != temp_close) {
1486                         temp_close[0] = '\0';
1487                 }
1488
1489                 // extract fixed command and remove space in front of  '{'
1490                 char *tok_ptr = NULL;
1491                 temp = strtok_r(src_cmd, "{", &tok_ptr);
1492                 __vc_db_remove_space(&temp);
1493                 *fixed_cmd = strdup(temp);
1494
1495                 // merge command with fixed and vfixed
1496                 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1497                         __vc_db_remove_space(&temp);
1498
1499                         snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
1500                         dst_cmd = strdup(merge_cmd);
1501                         temp_list = g_slist_append(temp_list, dst_cmd);
1502                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1503                 }
1504         } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
1505                 // check string validation
1506                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1507                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1508                         free(src_cmd);
1509                         src_cmd = NULL;
1510                         return VC_DB_ERROR_INVALID_PARAMETER;
1511                 }
1512
1513                 // extract fixed command
1514                 char* temp_fixed = strchr(src_cmd, '}') + 1;
1515                 __vc_db_remove_space(&temp_fixed);
1516                 *fixed_cmd = strdup(temp_fixed);
1517
1518                 // remove close brace, '}'
1519                 char *tok_ptr = NULL;
1520                 temp = strtok_r(src_cmd, "}", &tok_ptr);
1521
1522                 // remove open brace, '{'
1523                 temp = strchr(src_cmd, '{') + 1;
1524
1525                 tok_ptr = NULL;
1526                 temp = strtok_r(temp, "|", &tok_ptr);
1527                 __vc_db_remove_space(&temp);
1528
1529                 // merge command with fixed and vfixed
1530                 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1531                 dst_cmd = strdup(merge_cmd);
1532                 temp_list = g_slist_append(temp_list, dst_cmd);
1533                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1534
1535                 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1536                         __vc_db_remove_space(&temp);
1537
1538                         // merge command with fixed and vfixed
1539                         snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1540                         dst_cmd = strdup(merge_cmd);
1541                         temp_list = g_slist_append(temp_list, dst_cmd);
1542                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1543                 }
1544         } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
1545                 dst_cmd = strdup(src_cmd);
1546                 temp_list = g_slist_append(temp_list, dst_cmd);
1547                 *fixed_cmd = strdup(src_cmd);
1548
1549         } else {
1550                 dst_cmd = strdup(src_cmd);
1551                 temp_list = g_slist_append(temp_list, dst_cmd);
1552         }
1553
1554         *cmd_list = temp_list;
1555
1556         free(src_cmd);
1557         src_cmd = NULL;
1558         return VC_DB_ERROR_NONE;
1559 }
1560
1561 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
1562 {
1563         GSList* cmd_list = NULL;
1564         char* fixed_cmd = NULL;
1565         vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
1566
1567         int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
1568         if (0 != ret) {
1569                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
1570
1571                 if (NULL != tmp_cmd) {
1572                         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1573                 }
1574                 return ret;
1575         }
1576
1577         if (0 != g_slist_length(cmd_list)) {
1578                 GSList *iter = NULL;
1579                 char* temp_command = NULL;
1580                 iter = g_slist_nth(cmd_list, 0);
1581
1582                 while (NULL != iter) {
1583                         temp_command = iter->data;
1584
1585                         if (NULL != temp_command) {
1586                                 if (NULL != tmp_cmd->command) {
1587                                         free(tmp_cmd->command);
1588                                         tmp_cmd->command = NULL;
1589                                 }
1590                                 tmp_cmd->command = strdup(temp_command);
1591                                 if (NULL != fixed_cmd)
1592                                         tmp_cmd->fixed = strdup(fixed_cmd);
1593                                 else
1594                                         tmp_cmd->fixed = NULL;
1595
1596                                 ret = __vc_db_insert_commands(pid, type, tmp_cmd);
1597                                 if (ret != VC_DB_ERROR_NONE) {
1598                                         if (NULL != fixed_cmd) {
1599                                                 free(fixed_cmd);
1600                                                 fixed_cmd = NULL;
1601                                         }
1602                                         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1603                                         break;
1604                                 }
1605
1606                                 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name) {
1607                                         char temp[256] = {0, };
1608                                         snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
1609                                         if (NULL != tmp_cmd->command)
1610                                                 free(tmp_cmd->command);
1611
1612                                         tmp_cmd->command = strdup(temp);
1613
1614                                         ret = __vc_db_insert_commands(pid, type, tmp_cmd);
1615                                         if (ret != VC_DB_ERROR_NONE) {
1616                                                 if (NULL != fixed_cmd) {
1617                                                         free(fixed_cmd);
1618                                                         fixed_cmd = NULL;
1619                                                 }
1620                                                 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1621                                                 break;
1622                                         }
1623                                 }
1624                                 cmd_list = g_slist_remove(cmd_list, temp_command);
1625                                 free(temp_command);
1626                                 temp_command = NULL;
1627                         }
1628                         iter = g_slist_nth(cmd_list, 0);
1629                 }
1630
1631                 if (VC_DB_ERROR_NONE != ret) {
1632                         while (NULL != iter) {
1633                                 temp_command = iter->data;
1634
1635                                 if (NULL != temp_command) {
1636                                         cmd_list = g_slist_remove(cmd_list, temp_command);
1637                                         free(temp_command);
1638                                         temp_command = NULL;
1639                                 }
1640
1641                                 iter = g_slist_nth(cmd_list, 0);
1642                         }
1643
1644                         g_slist_free(cmd_list);
1645                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
1646
1647                         return ret;
1648                 }
1649                 cmd_list = NULL;
1650         }
1651
1652         if (NULL != fixed_cmd) {
1653                 free(fixed_cmd);
1654                 fixed_cmd = NULL;
1655         }
1656         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1657         return VC_DB_ERROR_NONE;
1658 }
1659
1660 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name)
1661 {
1662         GSList *iter = NULL;
1663         vc_cmd_s *temp_cmd;
1664
1665         int i;
1666         int count = g_slist_length(cmd_list);
1667         iter = g_slist_nth(cmd_list, 0);
1668
1669         SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
1670
1671         __vc_db_begin_transaction();
1672
1673         for (i = 0; i < count; i++) {
1674                 if (NULL == iter)
1675                         break;
1676
1677                 temp_cmd = iter->data;
1678
1679                 if (NULL == temp_cmd) {
1680                         SLOG(LOG_ERROR, vc_db_tag(), "comamnd is NULL");
1681                         break;
1682                 }
1683
1684                 if (type == temp_cmd->type) {
1685                         if (NULL != invocation_name)
1686                                 temp_cmd->invocation_name = strdup(invocation_name);
1687
1688                         int ret = vc_db_insert_command(pid, type, temp_cmd);
1689                         if (ret != VC_DB_ERROR_NONE) {
1690                                 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
1691                                 __vc_db_rollback_transaction();
1692                                 return ret;
1693                         }
1694                 } else {
1695                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
1696                 }
1697                 iter = g_slist_next(iter);
1698         }
1699
1700         __vc_db_commit_transaction();
1701
1702         return VC_DB_ERROR_NONE;
1703 }
1704
1705 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
1706 {
1707         __vc_db_begin_transaction();
1708
1709         int ret = __vc_db_get_commands(pid, type, cmd_list);
1710         if (ret != VC_DB_ERROR_NONE) {
1711                 __vc_db_rollback_transaction();
1712                 return ret;
1713         }
1714
1715         __vc_db_commit_transaction();
1716         return VC_DB_ERROR_NONE;
1717 }
1718
1719 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
1720 {
1721         if (NULL == result_text) {
1722                 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
1723                 return VC_DB_ERROR_INVALID_PARAMETER;
1724         }
1725
1726         int ret = vc_db_delete_table("result");
1727         if (0 != ret)
1728                 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1729
1730         if (NULL == vc_cmd_list) {
1731                 __vc_db_begin_transaction();
1732                 int ret = __vc_db_insert_result(result_text, event, msg, exclusive, NULL);
1733                 if (ret != VC_DB_ERROR_NONE) {
1734                         __vc_db_rollback_transaction();
1735                         return ret;
1736                 }
1737                 __vc_db_commit_transaction();
1738                 return VC_DB_ERROR_NONE;
1739         }
1740
1741         /* Make client list node */
1742         vc_cmd_h vc_command = NULL;
1743         vc_cmd_list_first(vc_cmd_list);
1744
1745         while (VC_ERROR_ITERATION_END != ret) {
1746                 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
1747                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
1748                         break;
1749                 }
1750
1751                 if (NULL == vc_command) {
1752                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
1753                         break;
1754                 }
1755
1756                 vc_cmd_s* temp_cmd = NULL;
1757                 temp_cmd = (vc_cmd_s*)vc_command;
1758
1759                 __vc_db_begin_transaction();
1760                 ret = __vc_db_insert_result(result_text, event, msg, exclusive, temp_cmd);
1761                 if (ret != VC_DB_ERROR_NONE) {
1762                         __vc_db_rollback_transaction();
1763                         return ret;
1764                 }
1765                 __vc_db_commit_transaction();
1766
1767                 ret = vc_cmd_list_next(vc_cmd_list);
1768         }
1769
1770         return VC_DB_ERROR_NONE;
1771 }
1772
1773 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
1774 {
1775         __vc_db_begin_transaction();
1776
1777         int ret = __vc_db_get_result(result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
1778         if (ret != VC_DB_ERROR_NONE) {
1779                 __vc_db_rollback_transaction();
1780                 return VC_DB_ERROR_OPERATION_FAILED;
1781         }
1782
1783         if (NULL == msg) {
1784                 int count = 0;
1785                 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
1786                 if (ret != VC_DB_ERROR_NONE) {
1787                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
1788                 }
1789                 if (0 == count) {
1790                         char* appid = NULL;
1791                         // Get appid by pid using app control
1792                         ret = app_manager_get_app_id(pid, &appid);
1793                         if (APP_MANAGER_ERROR_NONE != ret) {
1794                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
1795                         }
1796                         ret = __vc_db_get_result(result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
1797                         if (ret != VC_DB_ERROR_NONE) {
1798                                 __vc_db_rollback_transaction();
1799                                 return ret;
1800                         }
1801                         if (NULL != appid) {
1802                                 free(appid);
1803                                 appid = NULL;
1804                         }
1805                 }
1806         }
1807         __vc_db_commit_transaction();
1808         return VC_DB_ERROR_NONE;
1809 }
1810
1811 int vc_db_get_appid_list(const char* result, GSList** app_list)
1812 {
1813         __vc_db_begin_transaction();
1814
1815         int ret = __vc_db_get_appid(result, app_list);
1816         if (ret != VC_DB_ERROR_NONE) {
1817                 __vc_db_rollback_transaction();
1818                 return ret;
1819         }
1820
1821         __vc_db_commit_transaction();
1822         return VC_DB_ERROR_NONE;
1823 }
1824
1825 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
1826 {
1827         __vc_db_begin_transaction();
1828
1829         int ret = __vc_db_get_result_pid_list(result, pid_list);
1830         if (ret != VC_DB_ERROR_NONE) {
1831                 __vc_db_rollback_transaction();
1832                 return ret;
1833         }
1834
1835         __vc_db_commit_transaction();
1836         return VC_DB_ERROR_NONE;
1837 }
1838
1839 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
1840 {
1841         __vc_db_begin_transaction();
1842
1843         int ret = __vc_db_append_commands(pid, type, vc_cmd_list);
1844         if (ret != VC_DB_ERROR_NONE) {
1845                 __vc_db_rollback_transaction();
1846                 return ret;
1847         }
1848
1849         __vc_db_commit_transaction();
1850         return VC_DB_ERROR_NONE;
1851 }
1852
1853 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
1854 {
1855         __vc_db_begin_transaction();
1856
1857         int ret = 0;
1858         ret = __vc_db_delete_commands(pid, type, appid);
1859         if (ret != VC_DB_ERROR_NONE) {
1860                 __vc_db_rollback_transaction();
1861                 return ret;
1862         }
1863
1864         __vc_db_commit_transaction();
1865         return VC_DB_ERROR_NONE;
1866 }