tizen beta release
[platform/core/uifw/stt.git] / server / sttd_client_data.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved 
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14
15 #include "sttd_main.h"
16 #include "sttd_client_data.h"
17
18 /* Client list */
19 static GList *g_client_list = NULL;
20
21 int client_show_list()
22 {
23         GList *iter = NULL;
24         client_info_s *data = NULL;
25
26         SLOG(LOG_DEBUG, TAG_STTD, "----- client list"); 
27
28         if (g_list_length(g_client_list) > 0) {
29                 /* Get a first item */
30                 iter = g_list_first(g_client_list);
31
32                 int i = 1;      
33                 while (NULL != iter) {
34                         /*Get handle data from list*/
35                         data = iter->data;
36
37                         SLOG(LOG_DEBUG, TAG_STTD, "[%dth] uid(%d), state(%d)", i, data->uid, data->state); 
38                         
39                         /*Get next item*/
40                         iter = g_list_next(iter);
41                         i++;
42                 }
43         } else {
44                 SLOG(LOG_DEBUG, TAG_STTD, "No Client"); 
45         }
46
47         SLOG(LOG_DEBUG, TAG_STTD, "-----"); 
48
49         return 0;
50 }
51
52 GList* sttd_client_get_item(const int uid)
53 {
54         GList *iter = NULL;
55         client_info_s *data = NULL;
56
57         if (0 < g_list_length(g_client_list)) {
58                 iter = g_list_first(g_client_list);
59
60                 while (NULL != iter) {
61                         /* Get handle data from list */
62                         data = iter->data;
63
64                         if (uid == data->uid) 
65                                 return iter;
66                         
67                         iter = g_list_next(iter);
68                 }
69         }
70
71         return NULL;
72 }
73
74 int sttd_client_add(const int pid, const int uid)
75 {
76         /*Check uid is duplicated*/
77         GList *tmp = NULL;
78         tmp = sttd_client_get_item(uid);
79         
80         if (NULL != tmp) {
81                 SLOG(LOG_WARN, TAG_STTD, "[Client Data] Client uid is already registered"); 
82                 return STTD_ERROR_INVALID_PARAMETER;
83         }
84
85         client_info_s *info = (client_info_s*)g_malloc0(sizeof(client_info_s));
86
87         info->pid = pid;
88         info->uid = uid;
89         info->state = APP_STATE_READY;
90
91         /* Add item to global list */
92         g_client_list = g_list_append(g_client_list, info);
93         
94         if (NULL == g_client_list) {
95                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to add new client"); 
96                 return -1;
97         } else {
98                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data SUCCESS] Add new client"); 
99         }
100
101 #ifdef CLIENT_DATA_DEBUG
102         client_show_list();
103 #endif 
104
105         return 0;
106 }
107
108 int sttd_client_delete(const int uid)
109 {
110         GList *tmp = NULL;
111         client_info_s* hnd = NULL;
112
113         /*Get handle*/
114         tmp = sttd_client_get_item(uid);
115         if (NULL == tmp) {
116                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid); 
117                 return STTD_ERROR_INVALID_PARAMETER;
118         }
119
120         /*Free client structure*/
121         hnd = tmp->data;
122         if (NULL != hnd) {
123                 g_free(hnd);
124         }
125
126         /*Remove handle from list*/
127         g_client_list = g_list_remove_link(g_client_list, tmp);
128
129 #ifdef CLIENT_DATA_DEBUG
130         client_show_list();
131 #endif 
132
133         return 0;
134 }
135
136 int sttd_client_get_state(const int uid, app_state_e* state)
137 {
138         GList *tmp = NULL;
139         client_info_s* hnd = NULL;
140
141         tmp = sttd_client_get_item(uid);
142         if (NULL == tmp) {
143                 return STTD_ERROR_INVALID_PARAMETER;
144         }
145
146         hnd = tmp->data;
147         *state = hnd->state;
148
149         SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Get state : uid(%d), state(%d)", uid, *state);
150
151         return 0;
152 }
153
154 int sttd_client_set_state(const int uid, const app_state_e state)
155 {
156         GList *tmp = NULL;
157         client_info_s* hnd = NULL;
158
159         tmp = sttd_client_get_item(uid);
160         if (NULL == tmp) {
161                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid); 
162                 return STTD_ERROR_INVALID_PARAMETER;
163         }
164
165         hnd = tmp->data;
166         hnd->state = state ;
167
168         SLOG(LOG_DEBUG, TAG_STTD, "[Client Data SUCCESS] Set state : uid(%d), state(%d)", uid, state);
169
170         return 0;
171 }
172
173 int sttd_client_get_ref_count()
174 {
175         return g_list_length(g_client_list);
176 }
177
178 int sttd_client_get_pid(const int uid)
179 {
180         GList *tmp = NULL;
181         client_info_s* hnd = NULL;
182
183         tmp = sttd_client_get_item(uid);
184         if (NULL == tmp) {
185                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] sttd_client_get_pid : uid(%d) is not found", uid); 
186                 return STTD_ERROR_INVALID_PARAMETER;
187         }
188
189         hnd = tmp->data;
190
191         return hnd->pid;
192 }
193
194 int sttd_client_get_current_recording()
195 {
196         GList *iter = NULL;
197         client_info_s *data = NULL;
198
199         if (0 < g_list_length(g_client_list)) {
200                 iter = g_list_first(g_client_list);
201
202                 while (NULL != iter) {
203                         /* Get handle data from list */
204                         data = iter->data;
205
206                         if (APP_STATE_RECORDING == data->state) 
207                                 return data->uid;
208
209                         iter = g_list_next(iter);
210                 }
211         }
212
213         return -1;
214 }
215
216 int sttd_client_get_current_thinking()
217 {
218         GList *iter = NULL;
219         client_info_s *data = NULL;
220
221         if (0 < g_list_length(g_client_list)) {
222                 iter = g_list_first(g_client_list);
223
224                 while (NULL != iter) {
225                         /* Get handle data from list */
226                         data = iter->data;
227
228                         if (APP_STATE_PROCESSING == data->state) 
229                                 return data->uid;
230
231                         iter = g_list_next(iter);
232                 }
233         }
234
235         return -1;
236 }