Tizen 2.0 Release
[framework/system/oma-dm-agent.git] / src / agent / framework / ui-event-handler / user-interaction / user_interaction.c
1 /*
2  * oma-dm-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <unistd.h>
19
20 /*sync-agent*/
21 #include <sync_agent.h>
22
23 /*dm-agent*/
24 #include "common/dm_common.h"
25 #include "common/util/util.h"
26 #include "framework/ui-event-handler/user-interaction/user_interaction.h"
27 #include "serviceadapter/sa_util.h"
28 #include "ipc_agent.h"
29
30 #ifndef OMADM_AGENT_LOG
31 #undef LOG_TAG
32 #define LOG_TAG "OMA_DM_UI"
33 #endif
34
35 #define TIMEOUT_MIN_VAL 10
36 #define TIMEOUT_MAX_VAL 60
37 #define TIEM_ZERO "0"
38
39 void free_uic_value(uic_data ** uic_value)
40 {
41         _EXTERN_FUNC_ENTER;
42
43         if (uic_value != NULL) {
44                 if (*uic_value != NULL) {
45                         str_free(&((*uic_value)->display));
46                         if ((*uic_value)->multi_data != NULL) {
47                                 GList *iter = NULL;
48                                 char *data = NULL;
49                                 for (iter = (*uic_value)->multi_data; iter != NULL; iter = g_list_next(iter)) {
50                                         data = (char *)(iter->data);
51                                         str_free(&(data));
52
53                                 }
54                         }
55                         if ((*uic_value)->pUic_option != NULL) {
56                                 str_free(&((*uic_value)->pUic_option->default_data));
57                                 str_free(&((*uic_value)->pUic_option->echo_type));
58                                 str_free(&((*uic_value)->pUic_option->input_type));
59                                 str_free(&((*uic_value)->pUic_option->max_t));
60                                 str_free(&((*uic_value)->pUic_option->maxlen));
61                                 str_free(&((*uic_value)->pUic_option->min_t));
62                                 free((*uic_value)->pUic_option);
63                                 (*uic_value)->pUic_option = NULL;
64                         }
65                         if ((*uic_value)->res_data != NULL) {
66                                 free_res_data((*uic_value)->res_data);
67                                 (*uic_value)->res_data = NULL;
68                         }
69                         free((*uic_value));
70                         (*uic_value) = NULL;
71                 }
72         }
73
74         _EXTERN_FUNC_EXIT;
75 }
76
77 void free_res_data(uic_res_data * res_data)
78 {
79         _EXTERN_FUNC_ENTER;
80
81         retm_if((res_data) == NULL, "res data is NULL!!");
82
83         res_data->status = 0;
84         res_data->type = UIC_NONE_TYPE;
85
86         str_free(&(res_data->input_text));
87
88         if (res_data->res_multi_data != NULL) {
89                 GList *iter = NULL;
90                 char *data = NULL;
91                 for (iter = res_data->res_multi_data; iter != NULL; iter = g_list_next(iter)) {
92                         data = (char *)(iter->data);
93                         str_free(&(data));
94                 }
95         }
96         free(res_data);
97
98         _EXTERN_FUNC_EXIT;
99 }
100
101 DM_ERROR exec_userinteration_option(char *data, uic_option ** uic_value)
102 {
103         _EXTERN_FUNC_ENTER;
104
105         DM_ERROR ret = DM_OK;
106         char *findValue = NULL;
107
108         if (data == NULL) {
109                 _DEBUG_INFO("uic_data null");
110                 (*uic_value)->min_t = strdup("0");
111                 (*uic_value)->max_t = g_strdup_printf("%d", TIMEOUT_MAX_VAL);
112                 (*uic_value)->default_data = strdup("0");
113                 (*uic_value)->maxlen = strdup("0");
114                 (*uic_value)->input_type = strdup("0");
115                 (*uic_value)->echo_type = strdup("0");
116
117         } else {
118
119                 ret = findStgringValue(data, UI_OP_MINDT, UI_OP_EQUAL, &findValue);
120                 if (findValue != NULL) {
121                         if (!strncmp(findValue, TIEM_ZERO, strlen(TIEM_ZERO))) {
122                                 (*uic_value)->min_t = g_strdup_printf("%d", TIMEOUT_MIN_VAL);
123                         } else {
124                                 (*uic_value)->min_t = strdup(findValue);
125                         }
126                         str_free(&findValue);
127                 } else {
128                         (*uic_value)->min_t = g_strdup_printf("%d", TIMEOUT_MIN_VAL);
129                 }
130                 _DEBUG_INFO("min time : %s", (*uic_value)->min_t);
131                 /*UI value setting */
132                 ret = findStgringValue(data, UI_OP_MAXDT, UI_OP_EQUAL, &findValue);
133                 if (findValue != NULL) {
134                         _DEBUG_INFO("max time : %s", findValue);
135                         if (!strncmp(findValue, TIEM_ZERO, strlen(TIEM_ZERO))) {
136                                 (*uic_value)->max_t = g_strdup_printf("%d", TIMEOUT_MAX_VAL);
137                         } else {
138                                 (*uic_value)->max_t = strdup(findValue);
139                         }
140                         str_free(&findValue);
141                 } else {
142                         (*uic_value)->max_t = g_strdup_printf("%d", TIMEOUT_MAX_VAL);
143                 }
144                 _DEBUG_INFO("max time : %s", (*uic_value)->max_t);
145                 /*UI value setting */
146                 ret = findStgringValue(data, UI_OP_DR, UI_OP_EQUAL, &findValue);
147                 if (findValue != NULL) {
148                         _DEBUG_INFO("default data: %s", findValue);
149                         (*uic_value)->default_data = strdup(findValue);
150                         str_free(&findValue);
151                 } else {
152                         (*uic_value)->default_data = strdup("0");
153                 }
154                 /*UI value setting */
155                 ret = findStgringValue(data, UI_OP_MAXLEN, UI_OP_EQUAL, &findValue);
156                 if (findValue != NULL) {
157                         _DEBUG_INFO("max len : %s", findValue);
158                         (*uic_value)->maxlen = strdup(findValue);
159                         str_free(&findValue);
160                 } else {
161                         (*uic_value)->maxlen = strdup("0");
162                 }
163                 /*UI value setting */
164                 ret = findStgringValue(data, UI_OP_IT, UI_OP_EQUAL, &findValue);
165                 if (findValue != NULL) {
166                         _DEBUG_INFO("input type : %s", findValue);
167                         (*uic_value)->input_type = strdup(findValue);
168                         str_free(&findValue);
169                 } else {
170                         (*uic_value)->input_type = strdup("0");
171                 }
172
173                 /*UI value setting */
174                 ret = findStgringValue(data, UI_OP_ET, UI_OP_EQUAL, &findValue);
175                 if (findValue != NULL) {
176                         _DEBUG_INFO("echo_type : %s", findValue);
177                         (*uic_value)->echo_type = strdup(findValue);
178                         str_free(&findValue);
179                 } else {
180                         (*uic_value)->echo_type = strdup("0");
181                 }
182         }
183         _EXTERN_FUNC_EXIT;
184         return ret;
185 }
186
187 DM_ERROR ui_display(uic_data * uic_value)
188 {
189         _EXTERN_FUNC_ENTER;
190
191         DM_ERROR ret = DM_OK;
192
193         retvm_if((uic_value) == NULL, COMMON_ERR_IS_NULL, "uic_value is NULL!!");
194
195         int ui_ret = 0;
196
197         /* ui_ret = launch_om_dm_fumo_alert_ui();
198            if(ui_ret != 1) {
199            ret = DM_ALERT_UI_LAUNCH_ERROR;
200            goto error;
201            } else {
202            sleep(1);
203          */
204         ui_ret = alert_uic_display(uic_value);
205         if (ui_ret != 1) {
206                 uic_value->res_data->status = DM_ERR_NOT_EXECUTED;
207                 _DEBUG_INFO("display alert fail : %d", DM_ERR_NOT_EXECUTED);
208         } else {
209                 uic_value->res_data->status = DM_OK;
210                 _DEBUG_INFO("display alert success : %d", uic_value->res_data->status);
211         }
212         /*} */
213
214         _EXTERN_FUNC_EXIT;
215         return ret;
216 /*error:
217         _EXTERN_FUNC_EXIT;
218         return ret;*/
219 }
220
221 DM_ERROR ui_confirm_or_reject(uic_data * uic_value)
222 {
223         _EXTERN_FUNC_ENTER;
224
225         DM_ERROR ret = DM_OK;
226
227         retvm_if((uic_value) == NULL, COMMON_ERR_IS_NULL, "uic_value is NULL!!");
228
229         /* cancel -> atomic, sequence */
230         int ui_ret = 0;
231         /* ui_ret = launch_om_dm_fumo_alert_ui();
232            if(ui_ret != 1) {
233            ret = DM_ALERT_UI_LAUNCH_ERROR;
234            goto error;
235            } else {
236            sleep(1);
237          */
238         ui_ret = alert_uic_confirmation(uic_value);
239
240         if (ui_ret == 1) {
241                 //ok
242                 uic_value->res_data->status = DM_OK;
243                 _DEBUG_INFO("display alert success : %d", DM_OK);
244         } else {
245                 /*calcel or app launch fail or timeout */
246                 uic_value->res_data->status = DM_ERR_NOT_MODIFIED;
247                 _DEBUG_INFO("confirm alert fail : %d", DM_ERR_NOT_MODIFIED);
248         }
249         /*} */
250
251         _EXTERN_FUNC_EXIT;
252         return ret;
253 /*error:
254         _EXTERN_FUNC_EXIT;
255         return ret;*/
256 }
257
258 DM_ERROR ui_text_input(uic_data * uic_value, char **result_data)
259 {
260         _EXTERN_FUNC_ENTER;
261
262         DM_ERROR ret = DM_OK;
263
264         retvm_if((uic_value) == NULL, COMMON_ERR_IS_NULL, "uic_value is NULL!!");
265
266         int ui_ret = 0;
267         /* ui_ret = launch_om_dm_fumo_alert_ui();
268            if(ui_ret != 1) {
269            ret = DM_ALERT_UI_LAUNCH_ERROR;
270            goto error;
271            } else {
272            sleep(1);
273          */
274         ui_ret = alert_uic_inputText(uic_value);
275
276         if (ui_ret == 1) {
277
278                 if (uic_value->res_data->input_text != NULL) {
279                         (*result_data) = strdup(uic_value->res_data->input_text);
280                         uic_value->res_data->status = DM_OK;
281                         _DEBUG_INFO("test input alert success : %d", DM_OK);
282                 } else {
283                         (*result_data) = strdup("NULL");
284                         uic_value->res_data->status = DM_ERR_NOT_EXECUTED;
285                         _DEBUG_INFO("input alert fail : %d", DM_ERR_NOT_EXECUTED);
286                 }
287         } else {
288                 /*calcel or app launch fail */
289                 (*result_data) = strdup("NULL");
290                 uic_value->res_data->status = DM_ERR_NOT_EXECUTED;
291                 _DEBUG_INFO("input alert fail : %d", DM_ERR_NOT_EXECUTED);
292         }
293         /* } */
294
295         _EXTERN_FUNC_EXIT;
296         return ret;
297 /*error:
298         _EXTERN_FUNC_EXIT;
299         return ret;*/
300 }
301
302 DM_ERROR ui_single_choice(uic_data * uic_value, GList ** responseItems)
303 {
304         _EXTERN_FUNC_ENTER;
305
306         DM_ERROR ret = DM_OK;
307
308         retvm_if((uic_value) == NULL, COMMON_ERR_IS_NULL, "uic_value is NULL!!");
309
310         int ui_ret = 0;
311         /* ui_ret = launch_om_dm_fumo_alert_ui();
312            if(ui_ret != 1) {
313            ret = DM_ALERT_UI_LAUNCH_ERROR;
314            goto error;
315            } else {
316            sleep(1);
317          */
318         ui_ret = alert_uic_single_choice(uic_value);
319         if (ui_ret == 1) {
320                 if (uic_value->res_data->res_multi_data != NULL) {
321                         GList *iter = NULL;
322                         for (iter = uic_value->res_data->res_multi_data; iter != NULL; iter = g_list_next(iter)) {
323                                 char *data = NULL;
324                                 _DEBUG_INFO("choice data : %s", (char *)(iter->data));
325                                 data = strdup((char *)(iter->data));
326                                 (*responseItems) = g_list_append((*responseItems), data);
327                         }
328                         uic_value->res_data->status = DM_OK;
329                         _DEBUG_INFO("single choice alert success : %d", DM_OK);
330                 } else {
331                         /*calcel or app launch fail */
332                         char *data = NULL;
333                         data = strdup("NULL");
334                         (*responseItems) = g_list_append((*responseItems), data);
335                         uic_value->res_data->status = DM_ERR_NOT_EXECUTED;
336                         _DEBUG_INFO("single choice alert fail : %d", DM_ERR_NOT_EXECUTED);
337                 }
338         } else {
339                 /*calcel or app launch fail or timeout */
340                 char *data = NULL;
341                 data = strdup("NULL");
342                 (*responseItems) = g_list_append((*responseItems), data);
343                 uic_value->res_data->status = DM_ERR_NOT_EXECUTED;
344                 _DEBUG_INFO("single choice alert fail : %d", DM_ERR_NOT_EXECUTED);
345         }
346         //}
347
348         _EXTERN_FUNC_EXIT;
349         return ret;
350 /*error:
351         _EXTERN_FUNC_EXIT;
352         return ret;*/
353 }
354
355 DM_ERROR ui_multiple_choice(uic_data * uic_value, GList ** responseItems)
356 {
357         _EXTERN_FUNC_ENTER;
358
359         DM_ERROR ret = DM_OK;
360
361         retvm_if((uic_value) == NULL, COMMON_ERR_IS_NULL, "uic_value is NULL!!");
362
363         int ui_ret = 0;
364         /* ui_ret = launch_om_dm_fumo_alert_ui();
365            if(ui_ret != 1) {
366            ret = DM_ALERT_UI_LAUNCH_ERROR;
367            goto error;
368            } else { */
369         sleep(1);
370         ui_ret = alert_uic_multiple_choice(uic_value);
371         if (ui_ret == 1) {
372                 if (uic_value->res_data->res_multi_data != NULL) {
373                         GList *iter = NULL;
374                         for (iter = uic_value->res_data->res_multi_data; iter != NULL; iter = g_list_next(iter)) {
375                                 char *data = NULL;
376                                 _DEBUG_INFO("choice data : %s", (char *)(iter->data));
377                                 data = strdup((char *)(iter->data));
378                                 (*responseItems) = g_list_append((*responseItems), data);
379                         }
380                         uic_value->res_data->status = DM_OK;
381                         _DEBUG_INFO("multiple choice alert success : %d", DM_OK);
382                 } else {
383                         /*calcel or app launch fail */
384                         char *data = NULL;
385                         data = strdup("NULL");
386                         (*responseItems) = g_list_append((*responseItems), data);
387                         uic_value->res_data->status = DM_ERR_NOT_EXECUTED;
388                         _DEBUG_INFO("multiple choice alert fail : %d", DM_ERR_NOT_EXECUTED);
389                 }
390
391         } else {
392                 /*calcel or app launch fail */
393                 char *data = NULL;
394                 data = strdup("NULL");
395                 (*responseItems) = g_list_append((*responseItems), data);
396                 uic_value->res_data->status = DM_ERR_NOT_EXECUTED;
397                 _DEBUG_INFO("multiple choice alert fail : %d", DM_ERR_NOT_EXECUTED);
398         }
399 //       }
400
401         _EXTERN_FUNC_EXIT;
402         return ret;
403 /*error:
404
405         _EXTERN_FUNC_EXIT;
406         return ret;*/
407 }