Tizen 2.1 base
[apps/home/call-setting.git] / src / cst-forwarding-ime.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <Elementary.h>
18
19 #include "cst-common.h"
20 #include "cst-common-string.h"
21
22 #include "cst-widget.h"
23 #include "cst-util.h"
24 #include "cst-forwarding.h"
25
26 static Elm_Genlist_Item_Class *itc_ime = NULL;
27 static Elm_Genlist_Item_Class *itc_sep = NULL;
28 static char *def_ime_number = NULL;
29
30 static Elm_Genlist_Item_Class *itc_waiting_time = NULL;
31 static Elm_Genlist_Item_Class *itc_waiting_time_expand = NULL;
32 static CstGlItemData_t *waitingtime_item = NULL;
33 static int curr_wait_time = -1;
34 static Evas_Object *waitingtime_radio_btn = NULL;
35 static CstGlItemDisplayInfo_t list_cf_ime_dep2_waiting_time[] = {
36         {2, CST_STR_WAITING_TIME_5_SEC, ELM_GENLIST_ITEM_NONE, CST_GL_ITEM_RADIO_1TEXT, (void *)CST_CF_WAIT_TIME_IDX_5_SEC},
37         {2, CST_STR_WAITING_TIME_10_SEC, ELM_GENLIST_ITEM_NONE, CST_GL_ITEM_RADIO_1TEXT, (void *)CST_CF_WAIT_TIME_IDX_10_SEC},
38         {2, CST_STR_WAITING_TIME_15_SEC, ELM_GENLIST_ITEM_NONE, CST_GL_ITEM_RADIO_1TEXT, (void *)CST_CF_WAIT_TIME_IDX_15_SEC},
39         {2, CST_STR_WAITING_TIME_20_SEC, ELM_GENLIST_ITEM_NONE, CST_GL_ITEM_RADIO_1TEXT, (void *)CST_CF_WAIT_TIME_IDX_20_SEC},
40         {2, CST_STR_WAITING_TIME_25_SEC, ELM_GENLIST_ITEM_NONE, CST_GL_ITEM_RADIO_1TEXT, (void *)CST_CF_WAIT_TIME_IDX_25_SEC},
41         {2, CST_STR_WAITING_TIME_30_SEC, ELM_GENLIST_ITEM_NONE, CST_GL_ITEM_RADIO_1TEXT, (void *)CST_CF_WAIT_TIME_IDX_30_SEC},
42
43         {2, -1, ELM_GENLIST_ITEM_NONE, CST_GL_ITEM_NONE, NULL},
44 };
45
46 static int __cst_cf_ime_get_wait_time_index(int wait_time);
47 static int __cst_cf_ime_get_wait_time(int wait_time_idx);
48 static void __cst_cf_ime_gl_sel_waiting_time(void *data, Evas_Object *obj, void *event_info);
49 static void __cst_cf_ime_waiting_time_collapse_req(void *data, Evas_Object *obj, void *event_info);
50 static void __cst_cf_ime_waiting_time_collapse(int selected_time, Evas_Object *obj, void *event_info);
51 static void __cst_cf_ime_waiting_time_expand_req(void *data, Evas_Object *obj, void *event_info);
52 static void __cst_cf_ime_waiting_time_expand(void *data, Evas_Object *obj, void *event_info);
53 static void __cst_cf_ime_on_click_waitingtime_expand(void *data, Evas_Object *obj, void *event_info);
54 static void __cst_cf_ime_gl_del(void *data, Evas_Object *obj);
55 static Evas_Object *__cst_cf_ime_create_wait_time_radio_icon(Evas_Object *obj, Evas_Object *rdg, int value, int offset);
56 static Evas_Object *__cst_cf_ime_get_wait_time_sub_exp_icon(void *data, Evas_Object *obj, const char *part);
57 static char *__cst_cf_ime_get_wait_time_sub_exp_label(void *data, Evas_Object *obj, const char *part);
58 static char *__cst_cf_ime_cf_onoff_get_waiting_time_label(void *data, Evas_Object *obj, const char *part);
59 static Eina_Bool __cst_cf_is_waiting_time_available(int wait_time);
60
61 int _cst_get_input_number(char *buffer, void *data)
62 {
63         ENTER(_cst_get_input_number);
64         CstUgData_t *ugd = (CstUgData_t *)data;
65         const char *entry_input;
66         Evas_Object *error;
67         Ecore_IMF_Context *imf_ctx;
68
69         entry_input = elm_entry_entry_get(ugd->dg_entry);
70         retv_if(NULL == entry_input, -1);
71         if (strlen(entry_input) == 0) {
72                 imf_ctx = elm_entry_imf_context_get(ugd->dg_entry);
73                 if (imf_ctx) {
74                         ecore_imf_context_input_panel_hide(imf_ctx);
75                 }
76                 error = _cst_create_error_popup(ugd->nf, CST_ERROR_ENTER_NUMBER);
77                 ugd->popup = error;
78                 evas_object_show(error);
79                 return -1;
80         }
81
82         DBG("IME input string = %s length=%d", entry_input, strlen(entry_input));
83
84         snprintf(buffer, CST_MAX_PHONE_NUMBER_LEN, "%s", entry_input);
85         return strlen(entry_input);
86 }
87
88 void _cst_cf_ime_destroy_genlist_item_class(void)
89 {
90         ENTER(_cst_cf_ime_destroy_genlist_item_class);
91         DBG("\n\n *** Testing: CF-IME screen *** \n\n");
92
93         if (itc_ime) {
94                 elm_genlist_item_class_free(itc_ime);
95                 itc_ime = NULL;
96         }
97         if (itc_sep) {
98                 elm_genlist_item_class_free(itc_sep);
99                 itc_sep = NULL;
100         }
101         if (itc_waiting_time) {
102                 elm_genlist_item_class_free(itc_waiting_time);
103                 itc_waiting_time = NULL;
104         }
105         if (itc_waiting_time_expand) {
106                 elm_genlist_item_class_free(itc_waiting_time_expand);
107                 itc_waiting_time_expand = NULL;
108         }
109 }
110
111 static Evas_Object *__cst_gl_icon_get_cf_ime(void *data, Evas_Object *obj, const char *part)
112 {
113         retv_if(data == NULL, NULL);
114         CstUgData_t *ugd = (CstUgData_t *)data;
115
116         if (!strcmp(part, "elm.icon")) {
117                 Evas_Object *ef = _cst_create_ime_editfield(ugd, obj, CST_IME_CALL_FORWARD, def_ime_number);
118                 retv_if(ef == NULL, NULL);
119
120                 return _cst_create_ime_btn_layout(obj, ef,
121                                                                                   _cst_on_click_ime_contact_btn, (void *)ugd);
122         }
123
124         return NULL;
125 }
126
127 static int __cst_cf_ime_get_wait_time_index(int wait_time)
128 {
129         ENTER(__cst_cf_ime_get_wait_time_index);
130         int index = -1;
131
132         if (wait_time == CST_CF_WAIT_TIME_5_SEC) {
133                 index = CST_CF_WAIT_TIME_IDX_5_SEC;
134         } else if (wait_time == CST_CF_WAIT_TIME_10_SEC) {
135                 index = CST_CF_WAIT_TIME_IDX_10_SEC;
136         } else if (wait_time == CST_CF_WAIT_TIME_15_SEC) {
137                 index = CST_CF_WAIT_TIME_IDX_15_SEC;
138         } else if (wait_time == CST_CF_WAIT_TIME_20_SEC) {
139                 index = CST_CF_WAIT_TIME_IDX_20_SEC;
140         }  else if (wait_time == CST_CF_WAIT_TIME_25_SEC) {
141                 index = CST_CF_WAIT_TIME_IDX_25_SEC;
142         }  else {
143                 index = CST_CF_WAIT_TIME_IDX_30_SEC;
144         }
145
146         return index;
147 }
148
149 static int __cst_cf_ime_get_wait_time(int wait_time_idx)
150 {
151         ENTER(__cst_cf_ime_get_wait_time);
152         int wait_time = -1;
153
154         if (wait_time_idx == CST_CF_WAIT_TIME_IDX_5_SEC) {
155                 wait_time = CST_CF_WAIT_TIME_5_SEC;
156         } else if (wait_time_idx == CST_CF_WAIT_TIME_IDX_10_SEC) {
157                 wait_time = CST_CF_WAIT_TIME_10_SEC;
158         } else if (wait_time_idx == CST_CF_WAIT_TIME_IDX_15_SEC) {
159                 wait_time = CST_CF_WAIT_TIME_15_SEC;
160         } else if (wait_time_idx == CST_CF_WAIT_TIME_IDX_20_SEC) {
161                 wait_time = CST_CF_WAIT_TIME_20_SEC;
162         }  else if (wait_time_idx == CST_CF_WAIT_TIME_IDX_25_SEC) {
163                 wait_time = CST_CF_WAIT_TIME_25_SEC;
164         }  else {
165                 wait_time = CST_CF_WAIT_TIME_30_SEC;
166         }
167
168         return wait_time;
169 }
170
171 static void __cst_cf_ime_gl_sel_waiting_time(void *data, Evas_Object *obj, void *event_info)
172 {
173         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
174         ret_if(NULL == data);
175         CstGlItemData_t *item_data = (CstGlItemData_t *)data;
176
177         curr_wait_time = __cst_cf_ime_get_wait_time((int)list_cf_ime_dep2_waiting_time[item_data->index].func);
178
179         if (waitingtime_radio_btn) {
180                 elm_radio_value_set(waitingtime_radio_btn,
181                                                         (int)list_cf_ime_dep2_waiting_time[item_data->index].func);
182                 elm_genlist_item_update(item_data->parent_gl_item);
183         }
184         return;
185 }
186
187 static void __cst_cf_ime_waiting_time_collapse_req(void *data, Evas_Object *obj, void *event_info)
188 {
189         ENTER(__cst_cf_ime_waiting_time_collapse_req);
190         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
191         elm_genlist_item_expanded_set(item, 0);
192 }
193
194 static void __cst_cf_ime_waiting_time_collapse(int selected_time, Evas_Object *obj, void *event_info)
195 {
196         ENTER(__cst_cf_ime_waiting_time_collapse);
197         Elm_Object_Item *item = event_info;
198         int cf_wait_time_index = -1;
199         elm_genlist_item_subitems_clear(item);
200
201         cf_wait_time_index = __cst_cf_ime_get_wait_time_index(curr_wait_time);
202
203         if (selected_time != cf_wait_time_index) {
204                 if (waitingtime_item) {
205                         elm_genlist_item_update(waitingtime_item->gl_item);
206                 }
207         }
208 }
209
210 static void __cst_cf_ime_waiting_time_expand_req(void *data, Evas_Object *obj, void *event_info)
211 {
212         ENTER(__cst_cf_ime_waiting_time_expand_req);
213         Elm_Object_Item *item = event_info;
214         elm_genlist_item_expanded_set(item, 1);
215 }
216
217 static void __cst_cf_ime_waiting_time_expand(void *data, Evas_Object *obj, void *event_info)
218 {
219         ENTER(__cst_cf_ime_waiting_time_expand);
220         ret_if(NULL == data);
221         CstGlItemData_t *item_data = (CstGlItemData_t *)data;
222         CstUgData_t *ugd = (CstUgData_t *)item_data->ugd;
223         Elm_Object_Item *it = (Elm_Object_Item *)event_info;
224         ret_if(NULL == it);
225         Evas_Object *genlist = elm_object_item_widget_get(it);
226         ret_if(NULL == genlist);
227         CstGlItemData_t *sub_item_data;
228         int i = 0;
229         Ecore_IMF_Context *imf_ctx;
230
231         /* Hide the IMF if exists */
232         imf_ctx = elm_entry_imf_context_get(ugd->dg_entry);
233         if (imf_ctx) {
234                 ecore_imf_context_input_panel_hide(imf_ctx);
235         }
236
237         waitingtime_radio_btn = elm_radio_add(genlist);
238         elm_radio_value_set(waitingtime_radio_btn, -1);
239         for (i = 0; list_cf_ime_dep2_waiting_time[i].style != CST_GL_ITEM_NONE; ++i) {
240                 sub_item_data = (CstGlItemData_t *)calloc(1, sizeof(CstGlItemData_t));
241                 ret_if(sub_item_data == NULL);
242                 sub_item_data->index = i;
243                 sub_item_data->ugd = ugd;
244                 sub_item_data->parent_gl_item = it;
245                 elm_genlist_item_append(genlist, itc_waiting_time_expand,
246                                                                 (const void *)sub_item_data, it,
247                                                                 list_cf_ime_dep2_waiting_time[i].flags,
248                                                                 __cst_cf_ime_gl_sel_waiting_time, sub_item_data);
249         }
250 }
251
252 static void __cst_cf_ime_on_click_waitingtime_expand(void *data, Evas_Object *obj, void *event_info)
253 {
254         ENTER(__cst_cf_ime_on_click_waitingtime_expand);
255         ret_if(NULL == data);
256         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
257         static int selected_waittime = -1;
258
259         elm_genlist_item_selected_set(item, EINA_FALSE);
260         if (elm_genlist_item_expanded_get(item)) {
261                 __cst_cf_ime_waiting_time_collapse_req(data, obj, event_info);
262                 __cst_cf_ime_waiting_time_collapse(selected_waittime, obj, event_info);
263         } else {
264                 selected_waittime = __cst_cf_ime_get_wait_time_index(curr_wait_time);
265                 __cst_cf_ime_waiting_time_expand_req(data, obj, event_info);
266                 __cst_cf_ime_waiting_time_expand(data, obj, event_info);
267         }
268 }
269
270 static void __cst_cf_ime_gl_del(void *data, Evas_Object *obj)
271 {
272         ret_if(NULL == data);
273         CstGlItemData_t *item_data = (CstGlItemData_t *)data;
274         if (item_data) {
275                 free(item_data);
276         }
277         return;
278 }
279
280 static Evas_Object *__cst_cf_ime_create_wait_time_radio_icon(Evas_Object *obj, Evas_Object *rdg, int value, int offset)
281 {
282         retv_if(NULL == obj, NULL);
283
284         int rd_value = 0;
285         Evas_Object *radio = elm_radio_add(obj);
286         elm_radio_state_value_set(radio, value);
287         elm_radio_group_add(radio, rdg);
288
289         rd_value = __cst_cf_ime_get_wait_time_index(curr_wait_time);
290         elm_radio_value_set(rdg, (rd_value + offset));
291         return radio;
292 }
293
294 static Evas_Object *__cst_cf_ime_get_wait_time_sub_exp_icon(void *data, Evas_Object *obj, const char *part)
295 {
296         retv_if(NULL == data, NULL);
297         CstGlItemData_t *item_data = (CstGlItemData_t *)data;
298
299         if (!strcmp(part, "elm.icon")) {
300                 Evas_Object *radio = NULL;
301                 if (NULL == waitingtime_radio_btn) {
302                         waitingtime_radio_btn = elm_radio_add(obj);
303                         elm_radio_value_set(waitingtime_radio_btn, -1);
304                 }
305                 radio = __cst_cf_ime_create_wait_time_radio_icon(obj, waitingtime_radio_btn,
306                                 (int)list_cf_ime_dep2_waiting_time[item_data->index].func, 0);
307                 return radio;
308         }
309         return NULL;
310 }
311
312 static char *__cst_cf_ime_get_wait_time_sub_exp_label(void *data, Evas_Object *obj, const char *part)
313 {
314         retv_if(NULL == data, NULL);
315         CstGlItemData_t *item_data = (CstGlItemData_t *)data;
316         if (strcmp(part, "elm.text") == 0) {
317                 return strdup(T_(list_cf_ime_dep2_waiting_time[item_data->index].str_id));
318         }
319         return NULL;
320 }
321
322 static char *__cst_cf_ime_cf_onoff_get_waiting_time_label(void *data, Evas_Object *obj, const char *part)
323 {
324         retv_if(NULL == data, NULL);
325
326         if (strcmp(part, "elm.text.1") == 0) {
327                 return strdup(T_(CST_STR_WAITING_TIME));
328         } else if (strcmp(part, "elm.text.2") == 0) {
329                 int cf_wait_time_index = __cst_cf_ime_get_wait_time_index(curr_wait_time);
330                 if (list_cf_ime_dep2_waiting_time[cf_wait_time_index].str_id != -1) {
331                         return strdup(T_(list_cf_ime_dep2_waiting_time[cf_wait_time_index].str_id));
332                 }
333         }
334         return NULL;
335 }
336
337 static Evas_Object *__cst_create_cf_ime_genlist(CstUgData_t *ugd, int cf_flavour)
338 {
339         ENTER(__cst_create_cf_ime_genlist);
340         Evas_Object *genlist;
341
342         genlist = elm_genlist_add(ugd->nf);
343         _cst_create_genlist_separator(genlist, EINA_FALSE);
344
345         ugd->genlist_editfield_initialized = 0;
346
347         if (!itc_ime) {
348                 itc_ime = elm_genlist_item_class_new();
349         }
350         if (!itc_sep) {
351                 itc_sep = elm_genlist_item_class_new();
352         }
353         if (cf_flavour == CST_SSTYPE_CF_NO_REPLY) {
354                 if (!itc_waiting_time) {
355                         itc_waiting_time = elm_genlist_item_class_new();
356                 }
357                 if (!itc_waiting_time_expand) {
358                         itc_waiting_time_expand = elm_genlist_item_class_new();
359                 }
360         }
361
362         itc_ime->item_style = "dialogue/1icon";
363         itc_ime->func.text_get = NULL;
364         itc_ime->func.content_get = __cst_gl_icon_get_cf_ime;
365         itc_ime->func.state_get = NULL;
366         itc_ime->func.del = NULL;
367
368         itc_sep->item_style = "dialogue/seperator.4";
369         itc_sep->func.text_get = NULL;
370         itc_sep->func.content_get = NULL;
371         itc_sep->func.state_get = NULL;
372         itc_sep->func.del = NULL;
373
374         if (cf_flavour == CST_SSTYPE_CF_NO_REPLY) {
375                 itc_waiting_time->item_style = "dialogue/2text.3/expandable";
376                 itc_waiting_time->func.text_get = __cst_cf_ime_cf_onoff_get_waiting_time_label;
377                 itc_waiting_time->func.content_get = NULL;
378                 itc_waiting_time->func.state_get = NULL;
379                 itc_waiting_time->func.del = NULL;
380
381                 itc_waiting_time_expand->item_style = "dialogue/1text.1icon/expandable2";
382                 itc_waiting_time_expand->func.text_get = __cst_cf_ime_get_wait_time_sub_exp_label;
383                 itc_waiting_time_expand->func.content_get = __cst_cf_ime_get_wait_time_sub_exp_icon;
384                 itc_waiting_time_expand->func.state_get = NULL;
385                 itc_waiting_time_expand->func.del = __cst_cf_ime_gl_del;
386                 waitingtime_item = (CstGlItemData_t *)calloc(1, sizeof(CstGlItemData_t));
387                 retv_if(waitingtime_item == NULL, NULL);
388                 waitingtime_item->ugd = ugd;
389                 waitingtime_item->gl_item = NULL;
390         }
391
392         Elm_Object_Item *item = elm_genlist_item_append(genlist, itc_ime,
393                                            (void *)ugd, NULL, ELM_GENLIST_ITEM_NONE,
394                                            NULL, NULL);
395
396         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
397
398 #ifdef _CALL_SET_OLD_SEPERATOR
399         elm_genlist_item_append(genlist, itc_sep,
400                                            NULL, NULL, ELM_GENLIST_ITEM_NONE,
401                                            NULL, NULL);
402 #endif
403         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
404
405         if (cf_flavour == CST_SSTYPE_CF_NO_REPLY) {
406 #ifdef _CALL_SET_OLD_SEPERATOR
407                 elm_genlist_item_append(genlist, itc_sep,
408                                                    NULL, NULL, ELM_GENLIST_ITEM_NONE,
409                                                    NULL, NULL);
410 #endif
411                 waitingtime_item->gl_item = elm_genlist_item_append(genlist, itc_waiting_time,
412                                                                         (const void *)waitingtime_item, NULL, ELM_GENLIST_ITEM_TREE,
413                                                                         __cst_cf_ime_on_click_waitingtime_expand, waitingtime_item);
414         }
415
416         return genlist;
417 }
418
419 int _cst_cf_ime_fetch_latest_wait_time(void)
420 {
421         ENTER(_cst_cf_ime_fetch_latest_wait_time);
422
423         return curr_wait_time;
424 }
425
426 static Eina_Bool __cst_cf_is_waiting_time_available(int wait_time)
427 {
428         Eina_Bool ret= EINA_FALSE;
429
430         switch (wait_time)
431         {
432         case CST_CF_WAIT_TIME_5_SEC:
433         case CST_CF_WAIT_TIME_10_SEC:
434         case CST_CF_WAIT_TIME_15_SEC:
435         case CST_CF_WAIT_TIME_20_SEC:
436         case CST_CF_WAIT_TIME_25_SEC:
437         case CST_CF_WAIT_TIME_30_SEC:
438                 ret = EINA_TRUE;
439                 break;
440
441         case CST_CF_WAIT_TIME_NONE:
442         default:
443                 ret = EINA_FALSE;
444                 break;
445         }
446
447         return ret;
448 }
449
450 Evas_Object *_cst_create_cf_ime(Evas_Object *parent, char *edit_string,
451                                                                 int wait_time, int cf_flavour, void *data)
452 {
453         ENTER(_cst_create_cf_ime);
454         retv_if(data == NULL, NULL);
455         CstUgData_t *ugd = (CstUgData_t *)data;
456         Eina_Bool ret = EINA_FALSE;
457
458         if ((edit_string != NULL) && (strlen(edit_string) > 0)) {
459                 def_ime_number = edit_string;
460         } else {
461                 def_ime_number = NULL;
462         }
463
464         ret = __cst_cf_is_waiting_time_available(wait_time);
465         if (ret == EINA_TRUE) {
466                 curr_wait_time = wait_time;
467         } else {
468                 /* set default as 30 seconds */
469                 curr_wait_time = CST_CF_WAIT_TIME_30_SEC;
470         }
471
472         Evas_Object *genlist = __cst_create_cf_ime_genlist(ugd, cf_flavour);
473         return genlist;
474 }