Deallocate memory before setting new value
[platform/core/uifw/voice-control.git] / server / vcd_client_data.c
1 /*
2 * Copyright (c) 2011-2015 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 <aul.h>
19 #include <aul_window.h>
20
21 #include "vcd_client_data.h"
22 #include "vcd_config.h"
23 #include "vcd_main.h"
24
25
26 /* Client list */
27 static GSList* g_client_list = NULL;
28
29 static GSList* g_widget_list = NULL;
30
31 static manager_info_s g_manager;
32
33 /* Client IPC info list */
34 static GSList* g_client_tidl_info_list = NULL;
35
36 /* Manager IPC info */
37 static manager_tidl_info_s* g_mgr_tidl_info = NULL;
38
39 /* Widget IPC info */
40 static GSList* g_widget_tidl_info_list = NULL;
41
42 /* Setting IPC info list */
43 static GSList* g_setting_tidl_info_list = NULL;
44
45 /* Command list */
46 static current_commands_list_s g_cur_cmd_list;
47
48 /* Demandable client list */
49 static GSList* g_demandable_client = NULL;
50
51 /* Runtime info */
52 static bool g_silence_detection;
53 static vcd_recognition_mode_e g_recognition_mode;
54 static char* g_result_text = NULL;
55 static bool g_is_waiting_recording = false;
56 static int g_waiting_recording_pid = -1;
57
58 /* Function definitions */
59 widget_info_s* __widget_get_element(int pid);
60
61 vc_client_info_s* __client_get_element(int pid);
62
63
64 int vcd_client_manager_set(int pid)
65 {
66         // Get appid by pid using app control
67         char* appid = NULL;
68         int ret = app_manager_get_app_id(pid, &appid);
69         if (0 != ret || NULL == appid) {
70                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
71                 return VCD_ERROR_OPERATION_FAILED;
72         }
73
74         if (-1 != g_manager.pid && NULL != g_manager.appid && 0 == strncmp(g_manager.appid, appid, strlen(g_manager.appid))) {
75                 SLOG(LOG_WARN, TAG_VCD, "Same manager has already registered. It doesn't need to set manager again.");
76                 if (appid)
77                         free(appid);
78                 return VCD_ERROR_NONE;
79         }
80
81         g_manager.pid = pid;
82         g_manager.manager_cmd = false;
83         g_manager.exclusive_cmd_option = false;
84         g_manager.appid = NULL;
85
86         g_manager.appid = strdup(appid);
87
88         free(appid);
89         appid = NULL;
90         return VCD_ERROR_NONE;
91 }
92
93 int vcd_client_manager_unset()
94 {
95         g_manager.pid = -1;
96         g_manager.manager_cmd = false;
97         g_manager.exclusive_cmd_option = false;
98
99         return VCD_ERROR_NONE;
100 }
101
102 int vcd_client_manager_unset_appid()
103 {
104         if (NULL != g_manager.appid) {
105                 free(g_manager.appid);
106                 g_manager.appid = NULL;
107         }
108         return VCD_ERROR_NONE;
109 }
110
111 bool vcd_client_manager_is_valid(int pid)
112 {
113         if (-1 == g_manager.pid || pid == g_manager.pid) {
114                 return true;
115         }
116         return false;
117 }
118
119 int vcd_client_manager_set_command(int pid)
120 {
121         if (pid != g_manager.pid) {
122                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
123                 return VCD_ERROR_INVALID_PARAMETER;
124         }
125         g_manager.manager_cmd = true;
126         return VCD_ERROR_NONE;
127 }
128
129 int vcd_client_manager_unset_command(int pid)
130 {
131         if (pid != g_manager.pid) {
132                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
133                 return VCD_ERROR_INVALID_PARAMETER;
134         }
135         g_manager.manager_cmd = false;
136         return VCD_ERROR_NONE;
137 }
138
139 bool vcd_client_manager_is_system_command_valid(int pid)
140 {
141         if (pid != g_manager.pid) {
142                 SLOG(LOG_WARN, TAG_VCD, "[Client Data WARNING] pid(%d) is NOT valid", pid);
143                 return false;
144         }
145
146         if (true == g_manager.manager_cmd)
147                 return true;
148
149         return false;
150 }
151
152 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
153 {
154         if (0 != g_slist_length(g_demandable_client)) {
155                 /* release data */
156                 GSList *iter = NULL;
157                 vc_demandable_client_s* temp_client;
158                 iter = g_slist_nth(g_demandable_client, 0);
159
160                 while (NULL != iter) {
161                         temp_client = iter->data;
162
163                         if (NULL != temp_client) {
164                                 if (NULL != temp_client->appid)         free(temp_client->appid);
165                                 free(temp_client);
166                         }
167
168                         iter = g_slist_next(iter);
169                 }
170                 g_demandable_client = NULL;
171         }
172
173         g_demandable_client = client_list;
174
175         return VCD_ERROR_NONE;
176 }
177
178 bool vcd_client_manager_check_demandable_client(int pid)
179 {
180         if (0 == g_slist_length(g_demandable_client)) {
181                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] All client is available to request start");
182                 return true;
183         }
184
185         /* Check demandable appid */
186         char appid[1024] = {0, };
187         if (0 == aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
188                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] %s(%d) requests start", appid, pid);
189         } else {
190                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] daemon(%d) requests start", pid);
191         }
192
193         /* Compare appid */
194         GSList *iter = NULL;
195         vc_demandable_client_s* temp_client;
196         iter = g_slist_nth(g_demandable_client, 0);
197
198         while (NULL != iter) {
199                 temp_client = iter->data;
200
201                 if (NULL != temp_client) {
202
203                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] demandable appid(%s)", temp_client->appid);
204
205                         if (NULL != temp_client->appid) {
206                                 if (0 == strcmp(temp_client->appid, appid)) {
207                                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] pid(%d) is available", pid);
208                                         return true;
209                                 }
210                         } else {
211                                 if (0 == strlen(appid)) {
212                                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] pid(%d) is available", pid);
213                                         return true;
214                                 }
215                         }
216                 }
217
218                 iter = g_slist_next(iter);
219         }
220
221         return false;
222 }
223
224 bool vcd_client_manager_get_exclusive()
225 {
226         return g_manager.exclusive_cmd_option;
227 }
228
229 int vcd_client_manager_set_exclusive(bool value)
230 {
231         g_manager.exclusive_cmd_option = value;
232         return VCD_ERROR_NONE;
233 }
234
235 int vcd_client_manager_get_pid()
236 {
237         return g_manager.pid;
238 }
239
240 int vcd_client_manager_get_appid(char** appid)
241 {
242         if (NULL != g_manager.appid)
243                 *appid = strdup(g_manager.appid);
244
245         return VCD_ERROR_NONE;
246 }
247
248 int vcd_client_manager_set_result_text(const char* result)
249 {
250         if (NULL != g_result_text) {
251                 free(g_result_text);
252                 g_result_text = NULL;
253         }
254
255         if (NULL != result) {
256                 g_result_text = strdup(result);
257         }
258
259         return VCD_ERROR_NONE;
260 }
261
262 char* vcd_client_manager_get_result_text()
263 {
264         return g_result_text;
265 }
266
267 int vcd_client_manager_create_tidl_info()
268 {
269         /*Check already created*/
270         if (NULL != g_mgr_tidl_info) {
271                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Manager tidl info pid is already registered");
272                 return VCD_ERROR_NONE;
273         }
274
275         SLOG(LOG_INFO, TAG_VCD, "[Client Data] There is no manager tidl info. Create new one.");
276         g_mgr_tidl_info = (manager_tidl_info_s*)calloc(1, sizeof(manager_tidl_info_s));
277         if (NULL == g_mgr_tidl_info) {
278                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
279                 return VCD_ERROR_OUT_OF_MEMORY;
280         }
281
282         g_mgr_tidl_info->notify_cb = NULL;
283         g_mgr_tidl_info->notify_cb_user_data = NULL;
284         g_mgr_tidl_info->send_buffer_cb = NULL;
285         g_mgr_tidl_info->send_buffer_cb_user_data = NULL;
286
287         g_mgr_tidl_info->connected = false;
288         g_mgr_tidl_info->connection_requesting = false;
289         g_mgr_tidl_info->rpc_h = NULL;
290
291         SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new manager tidl info.");
292
293         return VCD_ERROR_NONE;
294 }
295
296 int vcd_client_manager_set_tidl_notify_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_h callback, void* user_data)
297 {
298         if (NULL == g_mgr_tidl_info) {
299                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
300                 return VCD_ERROR_INVALID_PARAMETER;
301         }
302
303         int ret = -1;
304         ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_clone(callback, &(g_mgr_tidl_info->notify_cb));
305         if (0 != ret) {
306                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone notify callback. ret(%d)", ret);
307         } else {
308                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone notify callback. ret(%d)", ret);
309         }
310         g_mgr_tidl_info->notify_cb_user_data = user_data;
311
312         return VCD_ERROR_NONE;
313 }
314
315 int vcd_client_manager_unset_tidl_notify_cb()
316 {
317         if (NULL == g_mgr_tidl_info) {
318                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
319                 return VCD_ERROR_INVALID_PARAMETER;
320         }
321
322         int ret = -1;
323         ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_destroy(g_mgr_tidl_info->notify_cb);
324         if (0 != ret) {
325                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy notify callback. ret(%d)", ret);
326         } else {
327                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy notify callback. ret(%d)", ret);
328         }
329         g_mgr_tidl_info->notify_cb = NULL;
330         g_mgr_tidl_info->notify_cb_user_data = NULL;
331
332         return VCD_ERROR_NONE;
333 }
334
335 int vcd_client_manager_set_tidl_send_buffer_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_h callback, void* user_data)
336 {
337         if (NULL == g_mgr_tidl_info) {
338                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
339                 return VCD_ERROR_INVALID_PARAMETER;
340         }
341
342         int ret = -1;
343         ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_clone(callback, &(g_mgr_tidl_info->send_buffer_cb));
344         if (0 != ret) {
345                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone feedback callback. ret(%d)", ret);
346         } else {
347                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone feedback callback. ret(%d)", ret);
348         }
349         g_mgr_tidl_info->send_buffer_cb_user_data = user_data;
350
351         return VCD_ERROR_NONE;
352 }
353
354 int vcd_client_manager_unset_tidl_send_buffer_cb()
355 {
356         if (NULL == g_mgr_tidl_info) {
357                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
358                 return VCD_ERROR_INVALID_PARAMETER;
359         }
360
361         int ret = -1;
362         ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_destroy(g_mgr_tidl_info->send_buffer_cb);
363         if (0 != ret) {
364                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy feedback callback. ret(%d)", ret);
365         } else {
366                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy feedback callback. ret(%d)", ret);
367         }
368         g_mgr_tidl_info->send_buffer_cb = NULL;
369         g_mgr_tidl_info->send_buffer_cb_user_data = NULL;
370
371         return VCD_ERROR_NONE;
372 }
373
374 int vcd_client_manager_delete_tidl_info()
375 {
376         if (NULL == g_mgr_tidl_info) {
377                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no manager tidl info");
378                 return VCD_ERROR_INVALID_PARAMETER;
379         }
380
381         if (0 != rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_destroy(g_mgr_tidl_info->rpc_h)) {
382                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to destroy tidl handle");
383         }
384
385         g_mgr_tidl_info->rpc_h = NULL;
386         free(g_mgr_tidl_info);
387         g_mgr_tidl_info = NULL;
388
389         return VCD_ERROR_NONE;
390 }
391
392 manager_tidl_info_s* vcd_client_manager_get_tidl_info()
393 {
394         return g_mgr_tidl_info;
395 }
396
397 static void __vcd_client_release_each_commands(GSList** cmds)
398 {
399         GSList *iter = NULL;
400         vc_cmd_s* temp_cmd;
401
402         if (0 < g_slist_length(*cmds)) {
403                 iter = g_slist_nth(*cmds, 0);
404                 while (NULL != iter) {
405                         temp_cmd = iter->data;
406
407                         if (NULL != temp_cmd) {
408                                 if (NULL != temp_cmd->command) {
409                                         free(temp_cmd->command);
410                                         temp_cmd->command = NULL;
411                                 }
412                                 if (NULL != temp_cmd->appid) {
413                                         free(temp_cmd->appid);
414                                         temp_cmd->appid = NULL;
415                                 }
416                                 if (NULL != temp_cmd->parameter) {
417                                         free(temp_cmd->parameter);
418                                         temp_cmd->parameter = NULL;
419                                 }
420                                 if (NULL != temp_cmd->invocation_name) {
421                                         free(temp_cmd->invocation_name);
422                                         temp_cmd->invocation_name = NULL;
423                                 }
424                                 if (NULL != temp_cmd->fixed) {
425                                         free(temp_cmd->fixed);
426                                         temp_cmd->fixed = NULL;
427                                 }
428                                 free(temp_cmd);
429                                 temp_cmd = NULL;
430                         }
431                         *cmds = g_slist_remove_link(*cmds, iter);
432
433                         iter = g_slist_nth(*cmds, 0);
434                 }
435                 *cmds = NULL;
436         }
437 }
438
439 int __vcd_client_release_commands()
440 {
441         g_cur_cmd_list.total_cmd_count = 0;
442         g_cur_cmd_list.foreground = VC_NO_FOREGROUND_PID;
443
444         __vcd_client_release_each_commands(&(g_cur_cmd_list.widget_cmds));
445         __vcd_client_release_each_commands(&(g_cur_cmd_list.system_cmds));
446         __vcd_client_release_each_commands(&(g_cur_cmd_list.system_background_cmds));
447         __vcd_client_release_each_commands(&(g_cur_cmd_list.exclusive_system_cmds));
448         __vcd_client_release_each_commands(&(g_cur_cmd_list.foreground_cmds));
449         __vcd_client_release_each_commands(&(g_cur_cmd_list.background_cmds));
450
451         return VCD_ERROR_NONE;
452 }
453
454 int vcd_client_command_collect_command()
455 {
456         /* 1. Get foreground pid */
457         int fg_pid = 0;
458         int ret = -1;
459         if (0 != vcd_config_get_foreground(&fg_pid)) {
460                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
461                 /* There is no foreground app for voice control */
462         }
463
464         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Foreground pid(%d)", fg_pid);
465
466         /* 2. Clean up command list */
467         __vcd_client_release_commands();
468
469         /* Check exclusive system command */
470         if (true == g_manager.exclusive_cmd_option && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_EXCLUSIVE)) {
471                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Exclusive option of manager is ON");
472
473                 GSList* ex_sys_cmd_list = NULL;
474                 if (true == g_manager.manager_cmd) {
475                         ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_EXCLUSIVE, &ex_sys_cmd_list);
476                         if (0 != ret) {
477                                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
478                         } else {
479                                 g_cur_cmd_list.exclusive_system_cmds = ex_sys_cmd_list;
480                         }
481                 } else {
482                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] No exclusive system commands");
483                 }
484
485                 return VCD_ERROR_NONE;
486         }
487
488         /* 3. Set system command */
489         GSList* sys_cmd_list = NULL;
490         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM)) {
491                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM, &sys_cmd_list);
492                 if (0 != ret) {
493                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
494                 } else {
495                         g_cur_cmd_list.system_cmds = sys_cmd_list;
496                 }
497         } else {
498                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system commands");
499         }
500
501         GSList* sys_back_cmd_list = NULL;
502         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_SYSTEM_BACKGROUND)) {
503                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM_BACKGROUND, &sys_back_cmd_list);
504                 if (0 != ret) {
505                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
506                 } else {
507                         g_cur_cmd_list.system_background_cmds = sys_back_cmd_list;
508                 }
509         } else {
510                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system background commands");
511         }
512
513         /* 4. Set foreground commands and widget */
514         GSList* fg_cmd_list = NULL;
515         if (VC_NO_FOREGROUND_PID != fg_pid) {
516                 GSList* widget_cmd_list = NULL;
517
518                 g_cur_cmd_list.foreground = fg_pid;
519
520                 /* 4-1. Set widget command */
521                 widget_info_s* widget_info = NULL;
522                 widget_info = __widget_get_element(fg_pid);
523                 if (NULL != widget_info && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
524                         if (true == widget_info->widget_cmd) {
525                                 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_WIDGET, &widget_cmd_list);
526                                 if (0 != ret) {
527                                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the WIDGET command list");
528                                 } else {
529                                         g_cur_cmd_list.widget_cmds = widget_cmd_list;
530                                 }
531                         }
532                 } else {
533                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] No widget commands");
534                 }
535
536                 /* 4-2. Set foreground command of foreground app */
537                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
538                         ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
539                         if (0 != ret) {
540                                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the fg command list");
541                         } else {
542                                 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
543                         }
544                 }
545         } else {
546                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No foreground app");
547         }
548         if (true == g_manager.manager_cmd && 1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_FOREGROUND)) {
549                 /* 4-3. Set foreground command by manager */
550                 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
551                 if (0 != ret) {
552                         SLOG(LOG_INFO, TAG_VCD, "[Client Data] No foreground commands of manager app");
553                 } else {
554                         g_cur_cmd_list.foreground_cmds = fg_cmd_list;
555                 }
556         }
557
558         /* 5. Set background commands */
559         GSList* bg_cmd_list = NULL;
560         if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_BACKGROUND)) {
561                 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_BACKGROUND, &bg_cmd_list);
562                 if (0 != ret) {
563                         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the bg command list");
564                 } else {
565                         /* Add item to global command list */
566                         g_cur_cmd_list.background_cmds = bg_cmd_list;
567                 }
568         }
569         return VCD_ERROR_NONE;
570 }
571
572 int vcd_client_get_length()
573 {
574         int command_count = 0;
575         command_count += g_slist_length(g_cur_cmd_list.widget_cmds);
576         command_count += g_slist_length(g_cur_cmd_list.system_cmds);
577         command_count += g_slist_length(g_cur_cmd_list.exclusive_system_cmds);
578         command_count += g_slist_length(g_cur_cmd_list.system_background_cmds);
579         command_count += g_slist_length(g_cur_cmd_list.foreground_cmds);
580         command_count += g_slist_length(g_cur_cmd_list.background_cmds);
581
582         g_cur_cmd_list.total_cmd_count = command_count;
583
584         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Command count : %d ", g_cur_cmd_list.total_cmd_count);
585         return command_count;
586 }
587
588 int vcd_client_foreach_command(client_foreach_command_cb callback, void* user_data)
589 {
590         if (NULL == callback) {
591                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] input parameter is NULL");
592                 return VCD_ERROR_INVALID_PARAMETER;
593         }
594
595         if (0 != vcd_client_command_collect_command()) {
596                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
597                 return VCD_ERROR_OPERATION_FAILED;
598         }
599
600         if (0 >= vcd_client_get_length()) {
601                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] No current command");
602                 return VCD_ERROR_OPERATION_FAILED;
603         }
604
605 //      int id_count = 1;
606         GSList *iter = NULL;
607         vc_cmd_s* temp_cmd;
608
609         if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
610                 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
611                 while (NULL != iter) {
612                         temp_cmd = iter->data;
613
614                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Widget : id(%d) type(%d) format(%d) command(%s) domain(%d)"
615                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->domain);
616
617                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
618
619                         iter = g_slist_next(iter);
620                 }
621         } else {
622                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No widget commands");
623         }
624
625         if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
626                 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
627                 while (NULL != iter) {
628                         temp_cmd = iter->data;
629
630                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Fore : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d) fixed(%s)"
631                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, temp_cmd->fixed);
632
633                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
634
635                         iter = g_slist_next(iter);
636                 }
637         } else {
638                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No foreground commands");
639         }
640
641         if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
642                 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
643                 while (NULL != iter) {
644                         temp_cmd = iter->data;
645
646                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
647                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
648
649                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
650
651                         iter = g_slist_next(iter);
652                 }
653         } else {
654                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system commands");
655         }
656
657         if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
658                 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
659                 while (NULL != iter) {
660                         temp_cmd = iter->data;
661
662                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System background : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
663                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
664
665                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
666
667                         iter = g_slist_next(iter);
668                 }
669         } else {
670                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system background commands");
671         }
672
673         if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
674                 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
675                 while (NULL != iter) {
676                         temp_cmd = iter->data;
677
678                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive system : id(%d) type(%d) format(%d) command(%s) param(%s) domain(%d)"
679                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
680
681                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
682
683                         iter = g_slist_next(iter);
684                 }
685         } else {
686                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No exclusive system commands");
687         }
688
689         if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
690                 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
691                 while (NULL != iter) {
692                         temp_cmd = iter->data;
693
694                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Back : id(%d) format(%d) type(%d) command(%s) param(%s) domain(%d) appid(%s) invocation(%s) fixed(%s)"
695                                  , temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, temp_cmd->appid, temp_cmd->invocation_name, temp_cmd->fixed);
696
697                         callback(temp_cmd->id, temp_cmd->type, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
698
699                         iter = g_slist_next(iter);
700                 }
701         } else {
702                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No background commands");
703         }
704         return VCD_ERROR_NONE;
705 }
706
707 static vc_cmd_s* __command_copy(vc_cmd_s* src_cmd)
708 {
709         if (NULL == src_cmd) {
710                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Input command is NULL");
711                 return NULL;
712         }
713
714         vc_cmd_s* temp_cmd = NULL;
715         temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
716         if (NULL == temp_cmd) {
717                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
718                 return NULL;
719         }
720
721         temp_cmd->id = src_cmd->id;
722         temp_cmd->pid = src_cmd->pid;
723         temp_cmd->index = src_cmd->index;
724         temp_cmd->type = src_cmd->type;
725         temp_cmd->format = src_cmd->format;
726         temp_cmd->domain = src_cmd->domain;
727
728         if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type)           temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
729         else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
730         else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
731         else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
732         else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
733         else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
734
735         if (NULL != src_cmd->command) {
736                 temp_cmd->command = strdup(src_cmd->command);
737         }
738
739         if (NULL != src_cmd->parameter) {
740                 temp_cmd->parameter = strdup(src_cmd->parameter);
741         }
742
743         if (NULL != src_cmd->appid) {
744                 temp_cmd->appid = strdup(src_cmd->appid);
745         }
746
747         if (NULL != src_cmd->invocation_name) {
748                 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
749         }
750
751         if (NULL != src_cmd->fixed) {
752                 temp_cmd->fixed = strdup(src_cmd->fixed);
753         }
754
755         temp_cmd->key = src_cmd->key;
756         temp_cmd->modifier = src_cmd->modifier;
757
758         return temp_cmd;
759 }
760
761 int vcd_client_get_cmd_from_result_id(int result_id, vc_cmd_s** result)
762 {
763         GSList *iter = NULL;
764         vc_cmd_s* temp_cmd;
765
766         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Result id(%d)", result_id);
767
768         if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
769                 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
770                 while (NULL != iter) {
771                         temp_cmd = iter->data;
772
773                         if (result_id == temp_cmd->id) {
774                                 /**pid = g_cur_cmd_list.foreground; */
775                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_UI_CONTROL; */
776                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
777
778                                 *result = __command_copy(temp_cmd);
779
780                                 return VCD_ERROR_NONE;
781                         }
782
783                         iter = g_slist_next(iter);
784                 }
785         } else {
786                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No widget commands");
787         }
788
789         if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
790                 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
791
792                 while (NULL != iter) {
793                         temp_cmd = iter->data;
794
795                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
796
797                         if (result_id == temp_cmd->id) {
798                                 /**pid = g_cur_cmd_list.foreground; */
799                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_FOREGROUND; */
800                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
801
802                                 *result = __command_copy(temp_cmd);
803                                 return VCD_ERROR_NONE;
804                         }
805
806                         iter = g_slist_next(iter);
807                 }
808         } else {
809                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No foreground commands");
810         }
811
812         if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
813                 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
814                 while (NULL != iter) {
815                         temp_cmd = iter->data;
816
817                         if (result_id == temp_cmd->id) {
818                                 /**pid = g_manager.pid; */
819                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM; */
820                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
821
822                                 *result = __command_copy(temp_cmd);
823                                 return VCD_ERROR_NONE;
824                         }
825
826                         iter = g_slist_next(iter);
827                 }
828         } else {
829                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system commands");
830         }
831
832         if (0 < g_slist_length(g_cur_cmd_list.system_background_cmds)) {
833                 iter = g_slist_nth(g_cur_cmd_list.system_background_cmds, 0);
834                 while (NULL != iter) {
835                         temp_cmd = iter->data;
836
837                         if (result_id == temp_cmd->id) {
838                                 /**pid = g_manager.pid; */
839                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_BACKGROUND; */
840                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
841
842                                 *result = __command_copy(temp_cmd);
843                                 return VCD_ERROR_NONE;
844                         }
845
846                         iter = g_slist_next(iter);
847                 }
848         } else {
849                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No system background commands");
850         }
851
852         if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
853                 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
854                 while (NULL != iter) {
855                         temp_cmd = iter->data;
856
857                         if (result_id == temp_cmd->id) {
858                                 /**pid = g_manager.pid; */
859                                 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_EXCLUSIVE; */
860                                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
861
862                                 *result = __command_copy(temp_cmd);
863                                 return VCD_ERROR_NONE;
864                         }
865
866                         iter = g_slist_next(iter);
867                 }
868         } else {
869                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No exclusive system commands");
870         }
871
872         if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
873                 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
874
875                 while (NULL != iter) {
876                         temp_cmd = iter->data;
877
878                         SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
879
880                         if (result_id == temp_cmd->id) {
881                                 *result = __command_copy(temp_cmd);
882                                 return VCD_ERROR_NONE;
883                         }
884
885                         iter = g_slist_next(iter);
886                 }
887         } else {
888                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] No background commands");
889         }
890
891         SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Not find matched result");
892         return VCD_ERROR_INVALID_PARAMETER;
893 }
894
895 int vcd_client_set_slience_detection(bool value)
896 {
897         g_silence_detection = value;
898         return VCD_ERROR_NONE;
899 }
900
901 bool vcd_client_get_slience_detection()
902 {
903         return g_silence_detection;
904 }
905
906 int vcd_client_set_recognition_mode(vcd_recognition_mode_e mode)
907 {
908         g_recognition_mode = mode;
909         return VCD_ERROR_NONE;
910 }
911
912 vcd_recognition_mode_e vcd_client_get_recognition_mode()
913 {
914         return g_recognition_mode;
915 }
916
917 int vcd_client_set_server_dialog(int pid, bool is_server_dialog)
918 {
919         vc_client_info_s* client_info = NULL;
920
921         client_info = __client_get_element(pid);
922         if (NULL == client_info) {
923                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
924                 return VCD_ERROR_INVALID_PARAMETER;
925         }
926
927         client_info->server_dialog = is_server_dialog;
928
929         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Set server dialog, client pid(%d), server_dialog(%d)", pid, client_info->server_dialog);
930         return VCD_ERROR_NONE;
931 }
932
933 int vcd_client_get_server_dialog(int pid, bool* is_server_dialog)
934 {
935         vc_client_info_s* client_info = NULL;
936
937         client_info = __client_get_element(pid);
938         if (NULL == client_info) {
939                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
940                 return VCD_ERROR_INVALID_PARAMETER;
941         }
942
943         *is_server_dialog = client_info->server_dialog;
944
945         SLOG(LOG_INFO, TAG_VCD, "[Client Data] Get server dialog, client pid(%d), server_dialog(%d)", pid, *is_server_dialog);
946         return VCD_ERROR_NONE;
947 }
948
949 int vcd_client_append_cmd_from_type(int type, vc_cmd_list_h list)
950 {
951         GSList *item = NULL;
952
953         if (NULL == list) {
954                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid list parameter");
955                 return VCD_ERROR_INVALID_PARAMETER;
956         }
957
958         switch (type) {
959         case VC_COMMAND_TYPE_SYSTEM:
960                 item = g_cur_cmd_list.system_cmds;
961                 break;
962         case VC_COMMAND_TYPE_FOREGROUND:
963                 item = g_cur_cmd_list.foreground_cmds;
964                 break;
965         case VC_COMMAND_TYPE_WIDGET:
966                 item = g_cur_cmd_list.widget_cmds;
967                 break;
968         case VC_COMMAND_TYPE_SYSTEM_BACKGROUND:
969                 item = g_cur_cmd_list.system_background_cmds;
970                 break;
971         case VC_COMMAND_TYPE_BACKGROUND:
972                 item = g_cur_cmd_list.background_cmds;
973                 break;
974         case VC_COMMAND_TYPE_EXCLUSIVE:
975                 item = g_cur_cmd_list.exclusive_system_cmds;
976                 break;
977         default:
978                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid type parameter");
979                 return VCD_ERROR_INVALID_PARAMETER;
980         }
981
982         while (NULL != item) {
983                 vc_cmd_h temp_cmd = NULL;
984                 vc_cmd_h target_cmd = (vc_cmd_h)item->data;
985
986                 temp_cmd = (vc_cmd_h)__command_copy((vc_cmd_s*)target_cmd);
987
988                 if (NULL == temp_cmd) {
989                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to copy the command");
990                         return VCD_ERROR_OUT_OF_MEMORY;
991                 }
992
993                 if (0 != vc_cmd_list_add(list, temp_cmd)) {
994                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add the command in the list");
995                         if (NULL != temp_cmd) {
996                                 vc_cmd_destroy(temp_cmd);
997                                 temp_cmd = NULL;
998                         }
999
1000                         return VCD_ERROR_OPERATION_FAILED;
1001                 }
1002
1003                 item = item->next;
1004         }
1005
1006         return VCD_ERROR_NONE;
1007 }
1008
1009 int __show_client_list()
1010 {
1011         GSList *iter = NULL;
1012         vc_client_info_s *data = NULL;
1013
1014         SLOG(LOG_DEBUG, TAG_VCD, "@@@ client list");
1015
1016         int count = g_slist_length(g_client_list);
1017         int i;
1018
1019         if (0 == count) {
1020                 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
1021         } else {
1022                 iter = g_slist_nth(g_client_list, 0);
1023                 for (i = 0; i < count; i++) {
1024                         if (NULL == iter)
1025                                 break;
1026
1027                         data = iter->data;
1028
1029                         if (NULL != data) {
1030                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
1031                         }
1032                         iter = g_slist_next(iter);
1033                 }
1034         }
1035
1036         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1037
1038         SLOG(LOG_DEBUG, TAG_VCD, "@@@ widget list");
1039
1040         widget_info_s *widget_data = NULL;
1041
1042         count = g_slist_length(g_widget_list);
1043
1044         if (0 == count) {
1045                 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
1046         } else {
1047                 iter = g_slist_nth(g_widget_list, 0);
1048                 for (i = 0; i < count; i++) {
1049                         if (NULL == iter)
1050                                 break;
1051
1052                         widget_data = iter->data;
1053
1054                         if (NULL != widget_data) {
1055                                 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
1056                         }
1057                         iter = g_slist_next(iter);
1058                 }
1059         }
1060
1061         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1062
1063         return VCD_ERROR_NONE;
1064 }
1065
1066 int __show_command_list(GSList* cmd_group)
1067 {
1068         GSList *iter = NULL;
1069         vc_cmd_s *data = NULL;
1070
1071         SLOG(LOG_DEBUG, TAG_VCD, "@@@ command group");
1072
1073         int count = g_slist_length(cmd_group);
1074         int i;
1075
1076         if (0 == count) {
1077                 SLOG(LOG_DEBUG, TAG_VCD, "No command");
1078         } else {
1079                 iter = g_slist_nth(cmd_group, 0);
1080                 for (i = 0; i < count; i++) {
1081                         if (NULL == iter)
1082                                 break;
1083
1084                         data = iter->data;
1085                         if (NULL != data) {
1086                                 if (NULL != data->parameter) {
1087                                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) parameter(%s) key(%d)", i, data->command, data->parameter, data->key);
1088                                 } else {
1089                                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
1090                                 }
1091                         }
1092                         iter = g_slist_next(iter);
1093                 }
1094         }
1095
1096         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1097
1098         return VCD_ERROR_NONE;
1099 }
1100
1101 GSList* __client_get_item(const int pid)
1102 {
1103         GSList *iter = NULL;
1104         vc_client_info_s *data = NULL;
1105
1106         int count = g_slist_length(g_client_list);
1107         int i;
1108
1109         if (0 < count) {
1110                 iter = g_slist_nth(g_client_list, 0);
1111                 for (i = 0; i < count; i++) {
1112                         if (NULL == iter)
1113                                 break;
1114
1115                         data = iter->data;
1116                         if (NULL != data) {
1117                                 if (pid == data->pid)
1118                                         return iter;
1119                         }
1120
1121                         iter = g_slist_next(iter);
1122                 }
1123         }
1124
1125         return NULL;
1126 }
1127
1128 vc_client_info_s* __client_get_element(int pid)
1129 {
1130         GSList *iter = NULL;
1131         vc_client_info_s *data = NULL;
1132
1133         int count = g_slist_length(g_client_list);
1134         int i;
1135
1136         if (0 < count) {
1137                 iter = g_slist_nth(g_client_list, 0);
1138                 for (i = 0; i < count; i++) {
1139                         if (NULL == iter)
1140                                 break;
1141
1142                         data = iter->data;
1143
1144                         if (NULL != data) {
1145                                 if (pid == data->pid)
1146                                         return data;
1147                         }
1148
1149                         iter = g_slist_next(iter);
1150                 }
1151         }
1152
1153         return NULL;
1154 }
1155
1156 int vcd_client_add(int pid)
1157 {
1158         /*Check pid is duplicated*/
1159         GSList *tmp = NULL;
1160         tmp = __client_get_item(pid);
1161
1162         if (NULL != tmp) {
1163                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
1164                 return VCD_ERROR_INVALID_PARAMETER;
1165         }
1166
1167         vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
1168         if (NULL == info) {
1169                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1170                 return VCD_ERROR_OUT_OF_MEMORY;
1171         }
1172
1173         info->pid = pid;
1174
1175         info->fg_cmd = false;
1176         info->bg_cmd = false;
1177         info->exclusive_cmd = false;
1178         info->server_dialog = false;
1179
1180         /* Add item to global list */
1181         g_client_list = g_slist_append(g_client_list, info);
1182
1183         if (NULL == g_client_list) {
1184                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
1185
1186                 free(info);
1187                 info = NULL;
1188
1189                 return VCD_ERROR_OUT_OF_MEMORY;
1190         } else {
1191                 SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new client");
1192         }
1193
1194 #ifdef CLIENT_DATA_DEBUG
1195         __show_client_list();
1196 #endif
1197         return VCD_ERROR_NONE;
1198 }
1199
1200 int vcd_client_delete(int pid)
1201 {
1202         GSList *tmp = NULL;
1203         vc_client_info_s* client_info = NULL;
1204
1205         /*Get handle*/
1206         tmp = __client_get_item(pid);
1207         if (NULL == tmp) {
1208                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1209                 return VCD_ERROR_INVALID_PARAMETER;
1210         }
1211
1212         /*Free client structure*/
1213         client_info = tmp->data;
1214         if (NULL != client_info) {
1215                 free(client_info);
1216         }
1217
1218         /*Remove handle from list*/
1219         g_client_list = g_slist_remove_link(g_client_list, tmp);
1220
1221
1222 #ifdef CLIENT_DATA_DEBUG
1223         __show_client_list();
1224 #endif
1225
1226         return VCD_ERROR_NONE;
1227 }
1228
1229 GSList* __get_client_tidl_info_item(const int pid)
1230 {
1231         GSList *iter = NULL;
1232         client_tidl_info_s *data = NULL;
1233
1234         int count = g_slist_length(g_client_tidl_info_list);
1235         int i;
1236
1237         if (0 < count) {
1238                 iter = g_slist_nth(g_client_tidl_info_list, 0);
1239                 for (i = 0; i < count; i++) {
1240                         if (NULL == iter)
1241                                 break;
1242
1243                         data = iter->data;
1244                         if (NULL != data) {
1245                                 if (pid == data->pid)
1246                                         return iter;
1247                         }
1248
1249                         iter = g_slist_next(iter);
1250                 }
1251         }
1252
1253         return NULL;
1254 }
1255
1256 client_tidl_info_s* __get_client_tidl_info_element(int pid)
1257 {
1258         GSList *iter = NULL;
1259         client_tidl_info_s *data = NULL;
1260
1261         int count = g_slist_length(g_client_tidl_info_list);
1262         int i;
1263
1264         if (0 < count) {
1265                 iter = g_slist_nth(g_client_tidl_info_list, 0);
1266                 for (i = 0; i < count; i++) {
1267                         if (NULL == iter)
1268                                 break;
1269
1270                         data = iter->data;
1271
1272                         if (NULL != data) {
1273                                 if (pid == data->pid)
1274                                         return data;
1275                         }
1276
1277                         iter = g_slist_next(iter);
1278                 }
1279         }
1280
1281         return NULL;
1282 }
1283
1284 int vcd_client_add_tidl_info(int pid)
1285 {
1286         /*Check pid is duplicated*/
1287         client_tidl_info_s* info = NULL;
1288         info = __get_client_tidl_info_element(pid);
1289
1290         if (NULL != info) {
1291                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client tidl info pid is already registered");
1292                 return VCD_ERROR_NONE;
1293         }
1294
1295         SLOG(LOG_INFO, TAG_VCD, "[Client Data] There is no tidl info of pid(%d). Create new one.", pid);
1296         info = (client_tidl_info_s*)calloc(1, sizeof(client_tidl_info_s));
1297         if (NULL == info) {
1298                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1299                 return VCD_ERROR_OUT_OF_MEMORY;
1300         }
1301
1302         info->pid = pid;
1303         info->notify_cb = NULL;
1304         info->notify_cb_user_data = NULL;
1305         info->feedback_cb = NULL;
1306         info->feedback_cb_user_data = NULL;
1307
1308         g_client_tidl_info_list = g_slist_append(g_client_tidl_info_list, info);
1309         if (NULL == g_client_tidl_info_list) {
1310                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client tidl info");
1311
1312                 free(info);
1313                 info = NULL;
1314
1315                 return VCD_ERROR_OUT_OF_MEMORY;
1316         } else {
1317                 SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new client tidl info. pid(%d)", pid);
1318         }
1319
1320         return VCD_ERROR_NONE;
1321 }
1322
1323 int vcd_client_set_tidl_notify_cb(int pid, rpc_port_stub_vcd_stub_vc_notify_cb_h callback, void* user_data)
1324 {
1325         /*Check pid*/
1326         client_tidl_info_s* info = NULL;
1327         info = __get_client_tidl_info_element(pid);
1328
1329         if (NULL == info) {
1330                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
1331                 return VCD_ERROR_INVALID_PARAMETER;
1332         }
1333
1334         int ret = -1;
1335         ret = rpc_port_stub_vcd_stub_vc_notify_cb_clone(callback, &(info->notify_cb));
1336         if (0 != ret) {
1337                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone notify callback. ret(%d)", ret);
1338         } else {
1339                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone notify callback. ret(%d)", ret);
1340         }
1341         info->notify_cb_user_data = user_data;
1342
1343         return VCD_ERROR_NONE;
1344 }
1345
1346 int vcd_client_unset_tidl_notify_cb(int pid)
1347 {
1348         /*Check pid*/
1349         client_tidl_info_s* info = NULL;
1350         info = __get_client_tidl_info_element(pid);
1351
1352         if (NULL == info) {
1353                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
1354                 return VCD_ERROR_INVALID_PARAMETER;
1355         }
1356
1357         int ret = -1;
1358         ret = rpc_port_stub_vcd_stub_vc_notify_cb_destroy(info->notify_cb);
1359         if (0 != ret) {
1360                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy notify callback. ret(%d)", ret);
1361         } else {
1362                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy notify callback. ret(%d)", ret);
1363         }
1364         info->notify_cb = NULL;
1365         info->notify_cb_user_data = NULL;
1366
1367         return VCD_ERROR_NONE;
1368 }
1369
1370 int vcd_client_set_tidl_feedback_cb(int pid, rpc_port_stub_vcd_stub_vc_feedback_cb_h callback, void* user_data)
1371 {
1372         /*Check pid*/
1373         client_tidl_info_s* info = NULL;
1374         info = __get_client_tidl_info_element(pid);
1375
1376         if (NULL == info) {
1377                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
1378                 return VCD_ERROR_INVALID_PARAMETER;
1379         }
1380
1381         int ret = -1;
1382         ret = rpc_port_stub_vcd_stub_vc_feedback_cb_clone(callback, &(info->feedback_cb));
1383         if (0 != ret) {
1384                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone feedback callback. ret(%d)", ret);
1385         } else {
1386                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone feedback callback. ret(%d)", ret);
1387         }
1388         info->feedback_cb_user_data = user_data;
1389
1390         return VCD_ERROR_NONE;
1391 }
1392
1393 int vcd_client_unset_tidl_feedback_cb(int pid)
1394 {
1395         /*Check pid*/
1396         client_tidl_info_s* info = NULL;
1397         info = __get_client_tidl_info_element(pid);
1398
1399         if (NULL == info) {
1400                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
1401                 return VCD_ERROR_INVALID_PARAMETER;
1402         }
1403
1404         int ret = -1;
1405         ret = rpc_port_stub_vcd_stub_vc_feedback_cb_destroy(info->feedback_cb);
1406         if (0 != ret) {
1407                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy feedback callback. ret(%d)", ret);
1408         } else {
1409                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy feedback callback. ret(%d)", ret);
1410         }
1411         info->feedback_cb = NULL;
1412         info->feedback_cb_user_data = NULL;
1413
1414         return VCD_ERROR_NONE;
1415 }
1416
1417 int vcd_client_delete_tidl_info(int pid)
1418 {
1419         GSList *tmp = NULL;
1420         client_tidl_info_s* client_tidl_info = NULL;
1421
1422         /*Get handle*/
1423         tmp = __get_client_tidl_info_item(pid);
1424         if (NULL == tmp) {
1425                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1426                 return VCD_ERROR_INVALID_PARAMETER;
1427         }
1428
1429         /*Free client structure*/
1430         client_tidl_info = tmp->data;
1431         if (NULL != client_tidl_info) {
1432                 free(client_tidl_info);
1433         }
1434
1435         /*Delete handle from list*/
1436         g_client_tidl_info_list = g_slist_delete_link(g_client_tidl_info_list, tmp);
1437
1438         return VCD_ERROR_NONE;
1439 }
1440
1441 client_tidl_info_s* vcd_client_get_tidl_info(int pid)
1442 {
1443         return __get_client_tidl_info_element(pid);
1444 }
1445
1446 int vcd_client_get_tidl_list(int** pids, int* pid_count)
1447 {
1448         if (NULL == pids || NULL == pid_count)
1449                 return VCD_ERROR_INVALID_PARAMETER;
1450
1451         int count = g_slist_length(g_client_tidl_info_list);
1452
1453         if (0 == count)
1454                 return VCD_ERROR_OUT_OF_MEMORY;
1455
1456         int *tmp;
1457         tmp = (int*)calloc(count, sizeof(int));
1458         if (NULL == tmp) {
1459                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1460                 return VCD_ERROR_OUT_OF_MEMORY;
1461         }
1462
1463         GSList *iter = NULL;
1464         client_tidl_info_s *data = NULL;
1465         int i = 0;
1466
1467         iter = g_slist_nth(g_client_tidl_info_list, 0);
1468
1469         while (NULL != iter) {
1470                 data = iter->data;
1471
1472                 if (NULL != data) {
1473                         tmp[i] = data->pid;
1474                 }
1475
1476                 iter = g_slist_next(iter);
1477                 i++;
1478         }
1479
1480         *pids = tmp;
1481         *pid_count = count;
1482
1483         return VCD_ERROR_NONE;
1484 }
1485
1486 bool vcd_client_is_available(int pid)
1487 {
1488         vc_client_info_s* client_info = NULL;
1489
1490         client_info = __client_get_element(pid);
1491         if (NULL == client_info) {
1492                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1493                 return false;
1494         }
1495
1496         return true;
1497 }
1498
1499 int vcd_client_get_ref_count()
1500 {
1501         int count = 0;
1502
1503         count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1504         if (0 < g_manager.pid) {
1505                 count++;
1506         }
1507
1508         SLOG(LOG_INFO, TAG_VCD, "[Client Data] client count : %d", count);
1509
1510         return count;
1511 }
1512
1513 int vcd_client_get_list(int** pids, int* pid_count)
1514 {
1515         if (NULL == pids || NULL == pid_count)
1516                 return VCD_ERROR_INVALID_PARAMETER;
1517
1518         int count = g_slist_length(g_client_list);
1519
1520         if (0 == count)
1521                 return VCD_ERROR_OUT_OF_MEMORY;
1522
1523         int *tmp;
1524         tmp = (int*)calloc(count, sizeof(int));
1525         if (NULL == tmp) {
1526                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1527                 return VCD_ERROR_OUT_OF_MEMORY;
1528         }
1529
1530         GSList *iter = NULL;
1531         vc_client_info_s *data = NULL;
1532         int i = 0;
1533
1534         iter = g_slist_nth(g_client_list, 0);
1535
1536         while (NULL != iter) {
1537                 data = iter->data;
1538
1539                 if (NULL != data) {
1540                         tmp[i] = data->pid;
1541                 }
1542
1543                 iter = g_slist_next(iter);
1544                 i++;
1545         }
1546
1547         *pids = tmp;
1548         *pid_count = count;
1549
1550         return VCD_ERROR_NONE;
1551 }
1552
1553 int vcd_client_set_command_type(int pid, int type)
1554 {
1555         vc_client_info_s* client_info = NULL;
1556
1557         client_info = __client_get_element(pid);
1558         if (NULL == client_info) {
1559                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1560                 return VCD_ERROR_INVALID_PARAMETER;
1561         }
1562
1563         switch (type) {
1564         case VC_COMMAND_TYPE_FOREGROUND:
1565                 client_info->fg_cmd = true;
1566                 break;
1567         case VC_COMMAND_TYPE_BACKGROUND:
1568                 client_info->bg_cmd = true;
1569                 break;
1570         default:
1571                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1572                 return VCD_ERROR_INVALID_PARAMETER;
1573         }
1574
1575         return VCD_ERROR_NONE;
1576 }
1577
1578 int vcd_client_unset_command_type(int pid, int type)
1579 {
1580         vc_client_info_s* client_info = NULL;
1581
1582         client_info = __client_get_element(pid);
1583         if (NULL == client_info) {
1584                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1585                 return VCD_ERROR_INVALID_PARAMETER;
1586         }
1587
1588         switch (type) {
1589         case VC_COMMAND_TYPE_FOREGROUND:
1590                 client_info->fg_cmd = false;
1591                 break;
1592         case VC_COMMAND_TYPE_BACKGROUND:
1593                 client_info->bg_cmd = false;
1594                 break;
1595         default:
1596                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1597                 return VCD_ERROR_INVALID_PARAMETER;
1598         }
1599
1600         return VCD_ERROR_NONE;
1601 }
1602
1603 int vcd_client_set_exclusive_command(int pid)
1604 {
1605         vc_client_info_s* client_info = NULL;
1606
1607         client_info = __client_get_element(pid);
1608         if (NULL == client_info) {
1609                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1610                 return VCD_ERROR_INVALID_PARAMETER;
1611         }
1612
1613         client_info->exclusive_cmd = true;
1614
1615         return VCD_ERROR_NONE;
1616 }
1617
1618 int vcd_client_unset_exclusive_command(int pid)
1619 {
1620         vc_client_info_s* client_info = NULL;
1621
1622         client_info = __client_get_element(pid);
1623         if (NULL == client_info) {
1624                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1625                 return VCD_ERROR_INVALID_PARAMETER;
1626         }
1627
1628         client_info->exclusive_cmd = false;
1629
1630         return VCD_ERROR_NONE;
1631 }
1632
1633 int vcd_client_save_client_info()
1634 {
1635         if (0 != vc_info_parser_set_client_info(g_client_list)) {
1636                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1637                 return VCD_ERROR_OPERATION_FAILED;
1638         }
1639
1640         return VCD_ERROR_NONE;
1641 }
1642
1643 /*
1644 * Functions for widget
1645 */
1646 GSList* __widget_get_item(int pid)
1647 {
1648         GSList *iter = NULL;
1649         widget_info_s *data = NULL;
1650
1651         int count = g_slist_length(g_widget_list);
1652         int i;
1653
1654         if (0 < count) {
1655                 iter = g_slist_nth(g_widget_list, 0);
1656                 for (i = 0; i < count; i++) {
1657                         if (NULL == iter)
1658                                 break;
1659
1660                         data = iter->data;
1661
1662                         if (NULL != data) {
1663                                 if (pid == data->pid)
1664                                         return iter;
1665                         }
1666
1667                         iter = g_slist_next(iter);
1668                 }
1669         }
1670
1671         return NULL;
1672 }
1673
1674 widget_info_s* __widget_get_element(int pid)
1675 {
1676         GSList *iter = NULL;
1677         widget_info_s *data = NULL;
1678
1679         int count = g_slist_length(g_widget_list);
1680         int i;
1681
1682         if (0 < count) {
1683                 iter = g_slist_nth(g_widget_list, 0);
1684                 i = 0;
1685
1686                 while (iter && i < count) {
1687                         data = iter->data;
1688
1689                         if (NULL != data) {
1690                                 if (pid == data->pid)
1691                                         return data;
1692                         }
1693
1694                         iter = g_slist_next(iter);
1695
1696                         i++;
1697                 }
1698         }
1699
1700         return NULL;
1701 }
1702
1703 int vcd_client_widget_get_list(int** pids, int* pid_count)
1704 {
1705         if (NULL == pids || NULL == pid_count)
1706                 return VCD_ERROR_INVALID_PARAMETER;
1707
1708         int count = g_slist_length(g_widget_list);
1709
1710         if (0 == count)
1711                 return VCD_ERROR_OUT_OF_MEMORY;
1712
1713         int *tmp;
1714         tmp = (int*)calloc(count, sizeof(int));
1715         if (NULL == tmp) {
1716                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1717                 return VCD_ERROR_OUT_OF_MEMORY;
1718         }
1719
1720         GSList *iter = NULL;
1721         widget_info_s *data = NULL;
1722         int i = 0;
1723
1724         iter = g_slist_nth(g_widget_list, 0);
1725         while (NULL != iter) {
1726                 data = iter->data;
1727
1728                 if (NULL != data) {
1729                         tmp[i] = data->pid;
1730                 }
1731
1732                 iter = g_slist_next(iter);
1733                 i++;
1734         }
1735
1736         *pids = tmp;
1737         *pid_count = count;
1738
1739         return VCD_ERROR_NONE;
1740 }
1741
1742 int vcd_client_widget_add(int pid)
1743 {
1744         /*Check pid is duplicated*/
1745         GSList *tmp = NULL;
1746         tmp = __widget_get_item(pid);
1747
1748         if (NULL != tmp) {
1749                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1750                 return VCD_ERROR_INVALID_PARAMETER;
1751         }
1752
1753         widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1754         if (NULL == info) {
1755                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1756                 return VCD_ERROR_OUT_OF_MEMORY;
1757         }
1758
1759         info->pid = pid;
1760         info->widget_cmd = false;
1761
1762         /* Add item to global list */
1763         g_widget_list = g_slist_append(g_widget_list, info);
1764
1765         if (NULL == g_widget_list) {
1766                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1767
1768                 free(info);
1769                 info = NULL;
1770
1771                 return VCD_ERROR_OUT_OF_MEMORY;
1772         } else {
1773                 SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1774         }
1775
1776 #ifdef CLIENT_DATA_DEBUG
1777         __show_client_list();
1778 #endif
1779         return VCD_ERROR_NONE;
1780 }
1781
1782 int vcd_client_widget_delete(int pid)
1783 {
1784         GSList *tmp = NULL;
1785         widget_info_s* widget_info = NULL;
1786
1787         /*Get handle*/
1788         tmp = __widget_get_item(pid);
1789         if (NULL == tmp) {
1790                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1791                 return VCD_ERROR_INVALID_PARAMETER;
1792         }
1793
1794         /*Free client structure*/
1795         widget_info = tmp->data;
1796         if (NULL != widget_info) {
1797                 free(widget_info);
1798         }
1799
1800         /*Remove handle from list*/
1801         g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1802
1803
1804 #ifdef CLIENT_DATA_DEBUG
1805         __show_client_list();
1806 #endif
1807
1808         return VCD_ERROR_NONE;
1809 }
1810
1811 int vcd_client_widget_get_foreground_pid()
1812 {
1813         /* 1. Get foreground pid */
1814         int fg_pid = 0;
1815         if (0 != vcd_config_get_foreground(&fg_pid)) {
1816                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
1817                 /* There is no foreground app for voice control */
1818         }
1819
1820         widget_info_s* widget = NULL;
1821
1822         widget = __widget_get_element(fg_pid);
1823         if (NULL == widget) {
1824                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Not found foreground pid of widget");
1825                 return -1;
1826         }
1827
1828         return widget->pid;
1829 }
1830
1831
1832 bool vcd_client_widget_is_available(int pid)
1833 {
1834         widget_info_s* widget_info = NULL;
1835
1836         widget_info = __widget_get_element(pid);
1837         if (NULL == widget_info) {
1838                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1839                 return false;
1840         }
1841
1842         return true;
1843 }
1844
1845 int vcd_client_widget_set_command(int pid)
1846 {
1847         widget_info_s *info = NULL;
1848         info = __widget_get_element(pid);
1849         if (NULL == info) {
1850                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1851                 return VCD_ERROR_INVALID_PARAMETER;
1852         }
1853
1854         info->widget_cmd = true;
1855
1856         return VCD_ERROR_NONE;
1857 }
1858
1859 int vcd_client_widget_unset_command(int pid)
1860 {
1861         widget_info_s *info = NULL;
1862         info = __widget_get_element(pid);
1863         if (NULL == info) {
1864                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1865                 return VCD_ERROR_INVALID_PARAMETER;
1866         }
1867
1868         info->widget_cmd = false;
1869
1870         return VCD_ERROR_NONE;
1871 }
1872
1873 int vcd_client_widget_set_asr_result_enabled(int pid, bool enable)
1874 {
1875         widget_info_s *info = NULL;
1876         info = __widget_get_element(pid);
1877         if (NULL == info) {
1878                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1879                 return VCD_ERROR_INVALID_PARAMETER;
1880         }
1881
1882         info->asr_result_enabled = enable;
1883
1884         return VCD_ERROR_NONE;
1885 }
1886
1887 int vcd_client_widget_get_asr_result_enabled(int pid, bool* enable)
1888 {
1889         widget_info_s *info = NULL;
1890         info = __widget_get_element(pid);
1891         if (NULL == info) {
1892                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1893                 return VCD_ERROR_INVALID_PARAMETER;
1894         }
1895
1896         *enable = info->asr_result_enabled;
1897
1898         return VCD_ERROR_NONE;
1899 }
1900
1901 int vcd_client_widget_set_waiting_for_recording(int pid, bool waiting)
1902 {
1903
1904         if (TRUE == waiting && pid != vcd_client_widget_get_foreground_pid()) {
1905                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT foreground pid", pid);
1906                 return VCD_ERROR_INVALID_PARAMETER;
1907         }
1908
1909         g_is_waiting_recording = waiting;
1910         g_waiting_recording_pid = pid;
1911         SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to set waiting for recording, pid(%d), waiting(%d)", pid, waiting);
1912         return VCD_ERROR_NONE;
1913 }
1914
1915 int vcd_client_widget_get_waiting_for_recording(int pid, bool* waiting)
1916 {
1917         if (pid != g_waiting_recording_pid) {
1918                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT waiting pid", pid);
1919                 return VCD_ERROR_INVALID_PARAMETER;
1920         }
1921
1922         *waiting = g_is_waiting_recording;
1923         SLOG(LOG_ERROR, TAG_VCD, "[INFO] Success to get waiting for recording, waiting(%d)", *waiting);
1924         return VCD_ERROR_NONE;
1925 }
1926
1927 GSList* __get_widget_tidl_info_item(const int pid)
1928 {
1929         GSList *iter = NULL;
1930         widget_tidl_info_s *data = NULL;
1931
1932         int count = g_slist_length(g_widget_tidl_info_list);
1933         int i;
1934
1935         if (0 < count) {
1936                 iter = g_slist_nth(g_widget_tidl_info_list, 0);
1937                 for (i = 0; i < count; i++) {
1938                         if (NULL == iter)
1939                                 break;
1940
1941                         data = iter->data;
1942                         if (NULL != data) {
1943                                 if (pid == data->pid)
1944                                         return iter;
1945                         }
1946
1947                         iter = g_slist_next(iter);
1948                 }
1949         }
1950
1951         return NULL;
1952 }
1953
1954 widget_tidl_info_s* __get_widget_tidl_info_element(int pid)
1955 {
1956         GSList *iter = NULL;
1957         widget_tidl_info_s *data = NULL;
1958
1959         int count = g_slist_length(g_widget_tidl_info_list);
1960         int i;
1961
1962         if (0 < count) {
1963                 iter = g_slist_nth(g_widget_tidl_info_list, 0);
1964                 for (i = 0; i < count; i++) {
1965                         if (NULL == iter)
1966                                 break;
1967
1968                         data = iter->data;
1969
1970                         if (NULL != data) {
1971                                 if (pid == data->pid)
1972                                         return data;
1973                         }
1974
1975                         iter = g_slist_next(iter);
1976                 }
1977         }
1978
1979         return NULL;
1980 }
1981
1982 int vcd_client_widget_add_tidl_info(int pid)
1983 {
1984         /*Check pid is duplicated*/
1985         widget_tidl_info_s* info = NULL;
1986         info = __get_widget_tidl_info_element(pid);
1987
1988         if (NULL != info) {
1989                 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Widget tidl info pid is already registered");
1990                 return VCD_ERROR_NONE;
1991         }
1992
1993         SLOG(LOG_INFO, TAG_VCD, "[Client Data] There is no tidl info of pid(%d). Create new one.", pid);
1994         info = (widget_tidl_info_s*)calloc(1, sizeof(widget_tidl_info_s));
1995         if (NULL == info) {
1996                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1997                 return VCD_ERROR_OUT_OF_MEMORY;
1998         }
1999
2000         info->pid = pid;
2001         info->notify_cb = NULL;
2002         info->notify_cb_user_data = NULL;
2003
2004         info->connected = false;
2005         info->connection_requesting = false;
2006         info->rpc_h = NULL;
2007
2008         g_widget_tidl_info_list = g_slist_append(g_widget_tidl_info_list, info);
2009         if (NULL == g_widget_tidl_info_list) {
2010                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget tidl info");
2011
2012                 free(info);
2013                 info = NULL;
2014
2015                 return VCD_ERROR_OUT_OF_MEMORY;
2016         } else {
2017                 SLOG(LOG_INFO, TAG_VCD, "[Client Data SUCCESS] Add new widget tidl info. pid(%d)", pid);
2018         }
2019
2020         return VCD_ERROR_NONE;
2021 }
2022
2023 int vcd_client_widget_set_tidl_notify_cb(int pid, rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_h callback, void* user_data)
2024 {
2025         /*Check pid*/
2026         widget_tidl_info_s* info = NULL;
2027         info = __get_widget_tidl_info_element(pid);
2028
2029         if (NULL == info) {
2030                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
2031                 return VCD_ERROR_INVALID_PARAMETER;
2032         }
2033
2034         int ret = -1;
2035         ret = rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_clone(callback, &(info->notify_cb));
2036         if (0 != ret) {
2037                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to clone notify callback. ret(%d)", ret);
2038         } else {
2039                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to clone notify callback. ret(%d)", ret);
2040         }
2041         info->notify_cb_user_data = user_data;
2042
2043         return VCD_ERROR_NONE;
2044 }
2045
2046 int vcd_client_widget_unset_tidl_notify_cb(int pid)
2047 {
2048         /*Check pid*/
2049         widget_tidl_info_s* info = NULL;
2050         info = __get_widget_tidl_info_element(pid);
2051
2052         if (NULL == info) {
2053                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] There is no tidl info in the list. pid(%d)", pid);
2054                 return VCD_ERROR_INVALID_PARAMETER;
2055         }
2056
2057         int ret = -1;
2058         ret = rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_destroy(info->notify_cb);
2059         if (0 != ret) {
2060                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to destroy notify callback. ret(%d)", ret);
2061         } else {
2062                 SLOG(LOG_INFO, TAG_VCD, "[Client Data] Succeed to destroy notify callback. ret(%d)", ret);
2063         }
2064         info->notify_cb = NULL;
2065         info->notify_cb_user_data = NULL;
2066
2067         return VCD_ERROR_NONE;
2068 }
2069
2070 int vcd_client_widget_delete_tidl_info(int pid)
2071 {
2072         GSList *tmp = NULL;
2073         widget_tidl_info_s* widget_tidl_info = NULL;
2074
2075         /*Get handle*/
2076         tmp = __get_widget_tidl_info_item(pid);
2077         if (NULL == tmp) {
2078                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
2079                 return VCD_ERROR_INVALID_PARAMETER;
2080         }
2081
2082         /*Free widget structure*/
2083         widget_tidl_info = tmp->data;
2084         if (NULL != widget_tidl_info) {
2085                 free(widget_tidl_info);
2086         }
2087
2088         /*Delete handle from list*/
2089         g_widget_tidl_info_list = g_slist_delete_link(g_widget_tidl_info_list, tmp);
2090
2091         return VCD_ERROR_NONE;
2092 }
2093
2094 widget_tidl_info_s* vcd_client_widget_get_tidl_info(int pid)
2095 {
2096         return __get_widget_tidl_info_element(pid);
2097 }
2098
2099 GSList* vcd_client_widget_get_tidl_info_list()
2100 {
2101         return g_widget_tidl_info_list;
2102 }
2103
2104
2105 void vcd_client_update_foreground_pid()
2106 {
2107         int tmp_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
2108         vcd_config_get_foreground(&tmp_pid);
2109
2110         int pid = VC_RUNTIME_INFO_NO_FOREGROUND;
2111         int ret = aul_window_get_focused_pid(&pid);
2112         if (0 != ret) {
2113                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get focused pid, ret(%d)", ret);
2114         }
2115         SLOG(LOG_INFO, TAG_VCD, "[INFO] Foreground pid (%d), Focused pid (%d)", tmp_pid, pid);
2116
2117         GSList *iter = NULL;
2118         vc_client_info_s *data = NULL;
2119
2120         int count = g_slist_length(g_client_list);
2121         int i;
2122
2123         if (0 == count) {
2124                 SLOG(LOG_INFO, TAG_VCD, "No Client");
2125         } else {
2126                 iter = g_slist_nth(g_client_list, 0);
2127                 for (i = 0; i < count; i++) {
2128                         if (NULL == iter)
2129                                 break;
2130
2131                         data = iter->data;
2132
2133                         if (NULL != data) {
2134                                 SLOG(LOG_INFO, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
2135                                 if (pid == data->pid) {
2136                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", data->pid);
2137                                         vcd_config_set_foreground(data->pid, true);
2138                                         if (tmp_pid != data->pid) {
2139                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is different");
2140                                         }
2141                                         return;
2142                                 }
2143                         }
2144                         iter = g_slist_next(iter);
2145                 }
2146         }
2147
2148         widget_info_s *widget_data = NULL;
2149         count = g_slist_length(g_widget_list);
2150
2151         if (0 == count) {
2152                 SLOG(LOG_INFO, TAG_VCD, "No widget");
2153         } else {
2154                 iter = g_slist_nth(g_widget_list, 0);
2155                 for (i = 0; i < count; i++) {
2156                         if (NULL == iter)
2157                                 break;
2158
2159                         widget_data = iter->data;
2160
2161                         if (NULL != widget_data) {
2162                                 SLOG(LOG_INFO, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
2163                                 if (pid == widget_data->pid) {
2164                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid (%d)", widget_data->pid);
2165                                         vcd_config_set_foreground(widget_data->pid, true);
2166                                         if (tmp_pid != widget_data->pid) {
2167                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] foreground pid is changed");
2168                                         }
2169                                         return;
2170                                 }
2171                         }
2172                         iter = g_slist_next(iter);
2173                 }
2174         }
2175
2176         SLOG(LOG_INFO, TAG_VCD, "[INFO] No foreground");
2177         vcd_config_set_foreground(VC_RUNTIME_INFO_NO_FOREGROUND, true);
2178         return;
2179 }
2180
2181 /*
2182 * setting API
2183 */
2184 GSList* __get_setting_tidl_info_item(const int pid)
2185 {
2186         GSList *iter = NULL;
2187         setting_tidl_info_s *data = NULL;
2188
2189         int count = g_slist_length(g_setting_tidl_info_list);
2190         int i;
2191
2192         if (0 < count) {
2193                 iter = g_slist_nth(g_setting_tidl_info_list, 0);
2194                 for (i = 0; i < count; i++) {
2195                         if (NULL == iter)
2196                                 break;
2197
2198                         data = iter->data;
2199                         if (NULL != data) {
2200                                 if (pid == data->pid)
2201                                         return iter;
2202                         }
2203
2204                         iter = g_slist_next(iter);
2205                 }
2206         }
2207
2208         return NULL;
2209 }
2210
2211 setting_tidl_info_s* __get_setting_tidl_info_element(int pid)
2212 {
2213         GSList *iter = NULL;
2214         setting_tidl_info_s *data = NULL;
2215
2216         int count = g_slist_length(g_setting_tidl_info_list);
2217         int i;
2218
2219         if (0 < count) {
2220                 iter = g_slist_nth(g_setting_tidl_info_list, 0);
2221                 for (i = 0; i < count; i++) {
2222                         if (NULL == iter)
2223                                 break;
2224
2225                         data = iter->data;
2226
2227                         if (NULL != data) {
2228                                 if (pid == data->pid)
2229                                         return data;
2230                         }
2231
2232                         iter = g_slist_next(iter);
2233                 }
2234         }
2235
2236         return NULL;
2237 }
2238
2239 int vcd_client_setting_add_tidl_info(int pid)
2240 {
2241         /*Check pid is duplicated*/
2242         setting_tidl_info_s* info = NULL;
2243         info = __get_setting_tidl_info_element(pid);
2244
2245         if (NULL != info) {
2246                 SLOG(LOG_WARN, TAG_VCD, "[Setting Data] Setting tidl info pid is already registered");
2247                 return VCD_ERROR_NONE;
2248         }
2249
2250         SLOG(LOG_INFO, TAG_VCD, "[Setting Data] There is no tidl info of pid(%d). Create new one.", pid);
2251         info = (setting_tidl_info_s*)calloc(1, sizeof(setting_tidl_info_s));
2252         if (NULL == info) {
2253                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
2254                 return VCD_ERROR_OUT_OF_MEMORY;
2255         }
2256
2257         info->pid = pid;
2258         info->notify_cb = NULL;
2259         info->notify_cb_user_data = NULL;
2260
2261         g_setting_tidl_info_list = g_slist_append(g_setting_tidl_info_list, info);
2262         if (NULL == g_setting_tidl_info_list) {
2263                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] Fail to add new setting client tidl info");
2264
2265                 free(info);
2266                 info = NULL;
2267
2268                 return VCD_ERROR_OUT_OF_MEMORY;
2269         } else {
2270                 SLOG(LOG_INFO, TAG_VCD, "[Setting Data SUCCESS] Add new setting client tidl info. pid(%d)", pid);
2271         }
2272
2273         return VCD_ERROR_NONE;
2274 }
2275
2276 int vcd_client_setting_set_tidl_notify_cb(int pid, rpc_port_stub_vcd_setting_stub_vc_setting_notify_cb_h callback, void* user_data)
2277 {
2278         /*Check pid*/
2279         setting_tidl_info_s* info = NULL;
2280         info = __get_setting_tidl_info_element(pid);
2281
2282         if (NULL == info) {
2283                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] There is no tidl info in the list. pid(%d)", pid);
2284                 return VCD_ERROR_INVALID_PARAMETER;
2285         }
2286
2287         int ret = -1;
2288         ret = rpc_port_stub_vcd_setting_stub_vc_setting_notify_cb_clone(callback, &(info->notify_cb));
2289         if (0 != ret) {
2290                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] Fail to clone notify callback. ret(%d)", ret);
2291         } else {
2292                 SLOG(LOG_INFO, TAG_VCD, "[Setting Data] Succeed to clone notify callback. ret(%d)", ret);
2293         }
2294         info->notify_cb_user_data = user_data;
2295
2296         return VCD_ERROR_NONE;
2297 }
2298
2299 int vcd_client_setting_unset_tidl_notify_cb(int pid)
2300 {
2301         /*Check pid*/
2302         setting_tidl_info_s* info = NULL;
2303         info = __get_setting_tidl_info_element(pid);
2304
2305         if (NULL == info) {
2306                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] There is no tidl info in the list. pid(%d)", pid);
2307                 return VCD_ERROR_INVALID_PARAMETER;
2308         }
2309
2310         int ret = -1;
2311         ret = rpc_port_stub_vcd_setting_stub_vc_setting_notify_cb_destroy(info->notify_cb);
2312         if (0 != ret) {
2313                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] Fail to destroy notify callback. ret(%d)", ret);
2314         } else {
2315                 SLOG(LOG_INFO, TAG_VCD, "[Setting Data] Succeed to destroy notify callback. ret(%d)", ret);
2316         }
2317         info->notify_cb = NULL;
2318         info->notify_cb_user_data = NULL;
2319
2320         return VCD_ERROR_NONE;
2321 }
2322
2323 int vcd_client_setting_delete_tidl_info(int pid)
2324 {
2325         GSList *tmp = NULL;
2326         setting_tidl_info_s* setting_tidl_info = NULL;
2327
2328         /*Get handle*/
2329         tmp = __get_setting_tidl_info_item(pid);
2330         if (NULL == tmp) {
2331                 SLOG(LOG_ERROR, TAG_VCD, "[Setting Data ERROR] pid(%d) is NOT valid", pid);
2332                 return VCD_ERROR_INVALID_PARAMETER;
2333         }
2334
2335         /*Free setting client structure*/
2336         setting_tidl_info = tmp->data;
2337         if (NULL != setting_tidl_info) {
2338                 free(setting_tidl_info);
2339         }
2340
2341         /*Delete handle from list*/
2342         g_setting_tidl_info_list = g_slist_delete_link(g_setting_tidl_info_list, tmp);
2343
2344         return VCD_ERROR_NONE;
2345 }
2346
2347 setting_tidl_info_s* vcd_client_setting_get_tidl_info(int pid)
2348 {
2349         return __get_setting_tidl_info_element(pid);
2350 }
2351
2352 int vcd_client_setting_get_tidl_list(int** pids, int* pid_count)
2353 {
2354         if (NULL == pids || NULL == pid_count)
2355                 return VCD_ERROR_INVALID_PARAMETER;
2356
2357         int count = g_slist_length(g_setting_tidl_info_list);
2358
2359         if (0 == count)
2360                 return VCD_ERROR_OUT_OF_MEMORY;
2361
2362         int *tmp;
2363         tmp = (int*)calloc(count, sizeof(int));
2364         if (NULL == tmp) {
2365                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
2366                 return VCD_ERROR_OUT_OF_MEMORY;
2367         }
2368
2369         GSList *iter = NULL;
2370         setting_tidl_info_s *data = NULL;
2371         int i = 0;
2372
2373         iter = g_slist_nth(g_setting_tidl_info_list, 0);
2374
2375         while (NULL != iter) {
2376                 data = iter->data;
2377
2378                 if (NULL != data) {
2379                         tmp[i] = data->pid;
2380                 }
2381
2382                 iter = g_slist_next(iter);
2383                 i++;
2384         }
2385
2386         *pids = tmp;
2387         *pid_count = count;
2388
2389         return VCD_ERROR_NONE;
2390 }