Fix genlist grouptitle initialize issue
[apps/native/ug-wifi-efl.git] / sources / ui-gadget / viewers-layout / wifi_viewer_list.c
1 /*
2  * Wi-Fi
3  *
4  * Copyright 2012-2013 Samsung Electronics Co., Ltd
5  *
6  * Licensed under the Flora License, Version 1.1 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://floralicense.org/license
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "common.h"
21 #include "common_utils.h"
22 #include "ug_wifi.h"
23 #include "wlan_manager.h"
24 #include "view_detail.h"
25 #include "viewer_list.h"
26 #include "viewer_manager.h"
27 #include "appcoreWrapper.h"
28 #include "i18nmanager.h"
29
30 #define LIST_ITEM_CONNECTED_AP_FONT_SIZE                28
31 #define LIST_ITEM_CONNECTED_AP_FONT_COLOR               "#3B73B6"
32 #define LIST_ITEM_CONNECTED_AP_FONT_COLOR_HL    "#FFFFFF"
33 #define FIRST_ITEM_NUMBER                                               8
34
35
36 static Evas_Object* viewer_list = NULL;
37 static Elm_Object_Item* first_item = NULL;
38 static Elm_Object_Item* last_item = NULL;
39
40 static Elm_Genlist_Item_Class itc;
41 static Elm_Genlist_Item_Class grouptitle_itc;
42 static Elm_Object_Item* grouptitle = NULL;
43
44 extern wifi_appdata *ug_app_state;
45
46 static void _gl_listview_more_btn_cb(void *data, Evas_Object *obj, void *event_info)
47 {
48         __COMMON_FUNC_ENTER__;
49
50         INFO_LOG(UG_NAME_NORMAL,"=================\n");
51         INFO_LOG(UG_NAME_NORMAL," %s %d\n", __func__ ,__LINE__);
52         INFO_LOG(UG_NAME_NORMAL,"=================\n");
53
54         if (data == NULL) {
55                 __COMMON_FUNC_EXIT__;
56                 return;
57         }
58
59         wifi_device_info_t *device_info = (wifi_device_info_t *)data;
60
61         DEBUG_LOG(UG_NAME_NORMAL, "ssid [%s]", device_info->ssid);
62
63         view_detail(device_info, ug_app_state->layout_main);
64
65         __COMMON_FUNC_EXIT__;
66 }
67
68 static char* _gl_listview_text_get(void *data, Evas_Object *obj, const char *part)
69 {
70         char* det = NULL;
71
72         ug_genlist_data_t* gdata = (ug_genlist_data_t*) data;
73         retvm_if(NULL == gdata || NULL == gdata->device_info, NULL);
74
75         assertm_if(NULL == gdata->device_info->ssid, "NULL!!");
76         assertm_if(NULL == gdata->device_info->ap_status_txt, "NULL!!");
77
78         if (!strncmp(part, "elm.text.1", strlen(part))) {
79                 det = g_strdup(gdata->device_info->ssid);
80                 assertm_if(NULL == det, "NULL!!");
81         } else if (!strncmp(part, "elm.text.2", strlen(part))) {
82                 if (VIEWER_ITEM_RADIO_MODE_CONNECTED == gdata->radio_mode) {
83                         if (FALSE == gdata->highlighted)
84                                 det = g_strdup_printf("<color=%s><b>%s</b></color>", LIST_ITEM_CONNECTED_AP_FONT_COLOR, gdata->device_info->ap_status_txt);
85                         else
86                                 det = g_strdup_printf("<color=%s><b>%s</b></color>", LIST_ITEM_CONNECTED_AP_FONT_COLOR_HL, gdata->device_info->ap_status_txt);
87                 } else
88                         det = g_strdup(gdata->device_info->ap_status_txt);
89                 assertm_if(NULL == det, "NULL!!");
90         }
91         return det;
92 }
93
94 static Evas_Object *_gl_listview_content_get(void *data, Evas_Object *obj, const char *part)
95 {
96         ug_genlist_data_t *gdata = (ug_genlist_data_t *)data;
97         retvm_if(NULL == gdata, NULL);
98
99         Evas_Object *icon = NULL;
100
101         if (gdata->device_info->ap_image_path == NULL) {
102                 /* if there is no ap_image_path (NO AP Found situation) */
103                 DEBUG_LOG(UG_NAME_ERR, "Fatal: Image path is NULL");
104         } else if (!strncmp(part, "elm.icon.1", strlen(part))) {
105                 /* for strength */
106                 char *temp_str = NULL;
107                 icon = elm_image_add(obj);
108                 retvm_if(NULL == icon, NULL);
109
110                 if (FALSE == gdata->highlighted)
111                         temp_str = g_strdup_printf("%s.png", gdata->device_info->ap_image_path);
112                 else
113                         temp_str = g_strdup_printf("%s_press.png", gdata->device_info->ap_image_path);
114                 elm_image_file_set(icon, temp_str, NULL);
115                 g_free(temp_str);
116         } else if (!strncmp(part, "elm.icon.2", strlen(part))) {
117                 if (VIEWER_ITEM_RADIO_MODE_CONNECTING == gdata->radio_mode) {
118                         icon = elm_progressbar_add(obj);
119                         retvm_if(NULL == icon, NULL);
120
121                         elm_object_style_set(icon, "list_process");
122                         evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, 0.5);
123                         evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
124                         elm_progressbar_pulse(icon, TRUE);
125                 } else {
126                         icon = elm_button_add(obj);
127                         retvm_if(NULL == icon, NULL);
128
129                         elm_object_style_set(icon, "reveal");
130                         evas_object_smart_callback_add(icon, "clicked", _gl_listview_more_btn_cb, gdata->device_info);
131                         evas_object_propagate_events_set(icon, EINA_FALSE);
132                 }
133         }
134
135         return icon;
136 }
137
138 static void _gl_listview_del(void* data, Evas_Object* obj)
139 {
140         if (data == NULL)
141                 return;
142
143         ug_genlist_data_t* gdata = (ug_genlist_data_t*) data;
144         retm_if(NULL == gdata || NULL == gdata->device_info);
145
146         DEBUG_LOG(UG_NAME_NORMAL, "del target ssid:[%s]", gdata->device_info->ssid);
147
148         g_free(gdata->device_info->ap_image_path);
149         g_free(gdata->device_info->ap_status_txt);
150         g_free(gdata->device_info->ssid);
151         wifi_ap_destroy(gdata->device_info->ap);
152         g_free(gdata->device_info);
153         g_free(gdata);
154
155         return;
156 }
157
158 static char* _gl_text_title_get(void *data, Evas_Object *obj, const char *part)
159 {
160         assertm_if(NULL == obj, "NULL!!");
161         assertm_if(NULL == part, "NULL!!");
162
163         return (char*) g_strdup(sc(PACKAGE, I18N_TYPE_WiFi_network));
164 }
165
166 static Evas_Object *_gl_content_title_get(void *data, Evas_Object *obj, const char *part)
167 {
168         __COMMON_FUNC_ENTER__;
169
170         Evas_Object *title_progressbar = NULL;
171
172         if (HEADER_MODE_SEARCHING == viewer_manager_header_mode_get()) {
173                 if (!strcmp(part, "elm.icon")) {
174                         title_progressbar  = elm_progressbar_add(obj);
175                         elm_object_style_set(title_progressbar, "list_process_small");
176                         elm_progressbar_horizontal_set(title_progressbar, EINA_TRUE);
177                         elm_progressbar_pulse(title_progressbar, EINA_TRUE);
178                 }
179         }
180
181         __COMMON_FUNC_EXIT__;
182         return title_progressbar;
183 }
184
185 Elm_Object_Item* viewer_list_get_first_item(void)
186 {
187         return first_item;
188 }
189
190 static Elm_Object_Item* viewer_list_get_last_item(void)
191 {
192         return last_item;
193 }
194
195 static char *_access_info_cb(void *data, Evas_Object *obj)
196 {
197         char *strength = NULL;
198         char *buf = NULL;
199
200         ug_genlist_data_t *gdata = (ug_genlist_data_t *)data;
201         if (!gdata)
202                 return NULL;
203
204         strength = common_utils_get_rssi_text(PACKAGE, gdata->device_info->rssi);
205         buf = g_strdup_printf("%s %s %s", strength, gdata->device_info->ssid, gdata->device_info->ap_status_txt);
206
207         g_free(strength);
208         return buf;
209 }
210
211 static void _gl_realized(void *data, Evas_Object *obj, void *event_info)
212 {
213         HEADER_MODES header_mode = viewer_manager_header_mode_get();
214
215         if (header_mode == HEADER_MODE_ACTIVATING ||
216                 header_mode == HEADER_MODE_DEACTIVATING ||
217                 header_mode == HEADER_MODE_OFF)
218                 return ;
219
220         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
221         int index = (int)elm_genlist_item_index_get(item);
222         int first_item_index = (int)elm_genlist_item_index_get(viewer_list_get_first_item());
223         int last_item_index = (int)elm_genlist_item_index_get(viewer_list_get_last_item());
224
225         if (last_item_index == FIRST_ITEM_NUMBER)
226                 return ;
227
228         if (first_item_index == -1) {
229                 int group_index = (int)elm_genlist_item_index_get(grouptitle);
230                 first_item_index = group_index+1;
231         }
232
233         if (first_item_index <= index && first_item_index - last_item_index) {
234                 if(index == first_item_index)
235                         elm_object_item_signal_emit(item, "elm,state,top", "");
236                 else if (index == last_item_index)
237                         elm_object_item_signal_emit(item, "elm,state,bottom", "");
238                 else
239                         elm_object_item_signal_emit(item, "elm,state,center", "");
240
241                 Evas_Object *ao = elm_object_item_access_object_get(item);
242                 elm_access_info_cb_set(ao, ELM_ACCESS_INFO, _access_info_cb, elm_object_item_data_get(item));
243                 elm_access_info_set(ao, ELM_ACCESS_TYPE, "double tap to connect device");
244                 elm_access_info_set(ao, ELM_ACCESS_STATE, "More button");
245         }
246
247 }
248
249 static void _gl_highlighted(void *data, Evas_Object *obj, void *event_info)
250 {
251         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
252         if (item) {
253                 ug_genlist_data_t *gdata = (ug_genlist_data_t *)elm_object_item_data_get(item);
254                 if (gdata) {
255                         gdata->highlighted = TRUE;
256                         elm_genlist_item_fields_update(item, "elm.icon.1", ELM_GENLIST_ITEM_FIELD_CONTENT);
257                         elm_genlist_item_fields_update(item, "elm.text.2", ELM_GENLIST_ITEM_FIELD_TEXT);
258                 }
259         }
260 }
261
262 static void _gl_unhighlighted(void *data, Evas_Object *obj, void *event_info)
263 {
264         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
265         if (item) {
266                 ug_genlist_data_t *gdata = (ug_genlist_data_t *)elm_object_item_data_get(item);
267                 if (gdata) {
268                         gdata->highlighted = FALSE;
269                         elm_genlist_item_fields_update(item, "elm.icon.1", ELM_GENLIST_ITEM_FIELD_CONTENT);
270                         elm_genlist_item_fields_update(item, "elm.text.2", ELM_GENLIST_ITEM_FIELD_TEXT);
271                 }
272         }
273 }
274
275 static void __passwd_popup_cancel_cb(void *data,  Evas_Object *obj,
276                 void *event_info)
277 {
278         __COMMON_FUNC_ENTER__;
279
280         if (ug_app_state->passpopup == NULL)
281                 return;
282
283         passwd_popup_free(ug_app_state->passpopup);
284         ug_app_state->passpopup = NULL;
285
286         __COMMON_FUNC_EXIT__;
287 }
288
289 static void __passwd_popup_ok_cb(void *data, Evas_Object *obj, void *event_info)
290 {
291         __COMMON_FUNC_ENTER__;
292
293         wifi_ap_h ap = NULL;
294         int password_len = 0;
295         const char *password = NULL;
296         wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
297
298         if (ug_app_state->passpopup == NULL)
299                 return;
300
301         ap = passwd_popup_get_ap(ug_app_state->passpopup);
302         password = passwd_popup_get_txt(ug_app_state->passpopup);
303         password_len = strlen(password);
304
305         wifi_ap_get_security_type(ap, &sec_type);
306
307         switch (sec_type) {
308         case WIFI_SECURITY_TYPE_WEP:
309                 if (password_len != 5 && password_len != 13 &&
310                                 password_len != 26 && password_len != 10) {
311                         winset_popup_mode_set(ug_app_state->popup_manager,
312                                         POPUP_OPTION_WEP_PSWD_LEN_ERROR, NULL);
313
314                         goto popup_ok_cb_exit;
315                 }
316                 break;
317
318         case WIFI_SECURITY_TYPE_WPA_PSK:
319         case WIFI_SECURITY_TYPE_WPA2_PSK:
320                 if (password_len < 8 || password_len > 63) {
321                         winset_popup_mode_set(ug_app_state->popup_manager,
322                                         POPUP_OPTION_WPA_PSWD_LEN_ERROR, NULL);
323
324                         goto popup_ok_cb_exit;
325                 }
326                 break;
327
328         default:
329                 ERROR_LOG(UG_NAME_NORMAL, "Wrong security mode: %d", sec_type);
330                 passwd_popup_free(ug_app_state->passpopup);
331                 ug_app_state->passpopup = NULL;
332
333                 goto popup_ok_cb_exit;
334         }
335
336         wlan_manager_connect_with_password(ap, password);
337
338         passwd_popup_free(ug_app_state->passpopup);
339         ug_app_state->passpopup = NULL;
340
341 popup_ok_cb_exit:
342         g_free((gpointer)password);
343
344         __COMMON_FUNC_EXIT__;
345 }
346
347 static void __wps_pbc_popup_cancel_connecting(void *data, Evas_Object *obj,
348                 void *event_info)
349 {
350         if (ug_app_state->passpopup == NULL)
351                 return;
352
353         wifi_ap_h ap = passwd_popup_get_ap(ug_app_state->passpopup);;
354
355         int ret = wlan_manager_disconnect(ap);
356         if (ret != WLAN_MANAGER_ERR_NONE)
357                 ERROR_LOG(UG_NAME_NORMAL, "Failed WPS PBC cancellation [0x%x]", ap);
358
359         passwd_popup_free(ug_app_state->passpopup);
360         ug_app_state->passpopup = NULL;
361
362         viewer_manager_header_mode_set(HEADER_MODE_ON);
363 }
364
365 static void _wps_btn_cb(void* data, Evas_Object* obj, void* event_info)
366 {
367         __COMMON_FUNC_ENTER__;
368         if (!ug_app_state->passpopup) {
369                 return;
370         }
371
372         wifi_ap_h ap = passwd_popup_get_ap(ug_app_state->passpopup);
373         int ret = wlan_manager_wps_connect(ap);
374         if (ret == WLAN_MANAGER_ERR_NONE) {
375                 create_pbc_popup(ug_app_state->passpopup, __wps_pbc_popup_cancel_connecting, NULL);
376         } else {
377                 ERROR_LOG(UG_NAME_NORMAL, "wlan_manager_wps_connect failed");
378                 passwd_popup_free(ug_app_state->passpopup);
379                 ug_app_state->passpopup = NULL;
380         }
381
382         __COMMON_FUNC_EXIT__;
383 }
384
385 static void __viewer_list_wifi_connect(wifi_device_info_t *device_info)
386 {
387         int rv;
388         bool favorite = false;
389         Evas_Object* navi_frame = NULL;
390         pswd_popup_create_req_data_t popup_info;
391         wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
392
393         if (device_info == NULL)
394                 return;
395
396         wifi_ap_is_favorite(device_info->ap, &favorite);
397
398         if (favorite == true) {
399                 rv = wlan_manager_connect(device_info->ap);
400                 return;
401         }
402
403         wifi_ap_get_security_type(device_info->ap, &sec_type);
404
405         switch (sec_type) {
406         case WIFI_SECURITY_TYPE_NONE:
407                 rv = wlan_manager_connect(device_info->ap);
408                 break;
409
410         case WIFI_SECURITY_TYPE_WEP:
411         case WIFI_SECURITY_TYPE_WPA_PSK:
412         case WIFI_SECURITY_TYPE_WPA2_PSK:
413                 memset(&popup_info, 0, sizeof(pswd_popup_create_req_data_t));
414
415                 popup_info.title = device_info->ssid;
416                 popup_info.ok_cb = __passwd_popup_ok_cb;
417                 popup_info.cancel_cb = __passwd_popup_cancel_cb;
418                 popup_info.show_wps_btn = device_info->wps_mode;
419                 popup_info.wps_btn_cb = _wps_btn_cb;
420                 popup_info.ap = device_info->ap;
421                 popup_info.cb_data = NULL;
422
423                 /* TODO: parameter with device_info */
424                 /* TODO: finally parameter with wifi_ap_h, WPA, EAP */
425                 ug_app_state->passpopup = create_passwd_popup(
426                                 ug_app_state->layout_main, PACKAGE, &popup_info);
427
428                 if (ug_app_state->passpopup == NULL)
429                         INFO_LOG(UG_NAME_ERR, "Fail to create password popup");
430
431                 break;
432
433         case WIFI_SECURITY_TYPE_EAP:
434                 navi_frame = viewer_manager_get_naviframe();
435                 ug_app_state->eap_view = create_eap_view(
436                                 ug_app_state->layout_main, navi_frame, PACKAGE, device_info);
437                 break;
438
439         default:
440                 ERROR_LOG(UG_NAME_NORMAL, "Unknown security type [%d]", sec_type);
441                 break;
442         }
443 }
444
445 static void __viewer_list_item_clicked_cb(void *data, Evas_Object *obj,
446                 void *event_info)
447 {
448         __COMMON_FUNC_ENTER__;
449
450         assertm_if(NULL == event_info, "event_info is NULL!!");
451         assertm_if(NULL == obj, "obj is NULL!!");
452
453         Elm_Object_Item *it = (Elm_Object_Item *)event_info;
454         ug_genlist_data_t *gdata = NULL;
455         wifi_device_info_t *device_info = NULL;
456
457         gdata = (ug_genlist_data_t *)elm_object_item_data_get(it);
458         retm_if(NULL == gdata);
459
460         device_info = gdata->device_info;
461         retm_if(NULL == device_info || NULL == device_info->ssid);
462
463         int item_state = gdata->radio_mode;
464         int current_state = 0;
465
466         INFO_LOG(UG_NAME_NORMAL, "ssid --- %s", device_info->ssid);
467         INFO_LOG(UG_NAME_NORMAL, "ap --- 0x%x", device_info->ap);
468         INFO_LOG(UG_NAME_NORMAL, "current item_state state is --- %d\n", item_state);
469
470         switch (item_state) {
471         case VIEWER_ITEM_RADIO_MODE_OFF:
472                 current_state = viewer_manager_header_mode_get();
473
474                 INFO_LOG(UG_NAME_NORMAL, "Clicked AP information\n");
475                 INFO_LOG(UG_NAME_NORMAL, "header mode [%d]", current_state);
476
477                 switch (current_state) {
478                 case HEADER_MODE_ON:
479                 case HEADER_MODE_CONNECTED:
480                 case HEADER_MODE_CONNECTING:
481                         __viewer_list_wifi_connect(device_info);
482                         break;
483
484                 case HEADER_MODE_OFF:
485                 case HEADER_MODE_SEARCHING:
486                 case HEADER_MODE_ACTIVATING:
487                 case HEADER_MODE_DEACTIVATING:
488                 default:
489                         INFO_LOG(UG_NAME_NORMAL, "Ignore click");
490                         break;
491                 }
492                 break;
493
494         case VIEWER_ITEM_RADIO_MODE_CONNECTING:
495         case VIEWER_ITEM_RADIO_MODE_CONNECTED:
496         default:
497                 INFO_LOG(UG_NAME_NORMAL, "Ignore click");
498                 break;
499         }
500
501         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
502
503         __COMMON_FUNC_EXIT__;
504 }
505
506 static char *viewer_list_get_device_status_txt(wifi_device_info_t *wifi_device, VIEWER_ITEM_RADIO_MODES mode)
507 {
508         char *status_txt = NULL;
509         /* The strings are currently hard coded. It will be replaced with string ids later */
510         if (VIEWER_ITEM_RADIO_MODE_CONNECTING == mode) {
511                 status_txt = g_strdup(sc(PACKAGE, I18N_TYPE_Connecting));
512         } else if (VIEWER_ITEM_RADIO_MODE_CONNECTED == mode) {
513                 status_txt = g_strdup(sc(PACKAGE, I18N_TYPE_Connected));
514         } else if (VIEWER_ITEM_RADIO_MODE_OFF == mode) {
515                 status_txt = common_utils_get_ap_security_type_info_txt(PACKAGE, wifi_device);
516         } else {
517                 status_txt = g_strdup(WIFI_UNKNOWN_DEVICE_STATUS_STR);
518                 INFO_LOG(UG_NAME_NORMAL, "Invalid mode: %d", mode);
519         }
520         return status_txt;
521 }
522
523 Evas_Object* viewer_list_create(Evas_Object *win)
524 {
525         __COMMON_FUNC_ENTER__;
526
527         viewer_list = NULL;
528         first_item = NULL;
529         last_item = NULL;
530         grouptitle = NULL;
531
532         assertm_if(NULL == win, "NULL!!");
533         viewer_list = elm_genlist_add(win);
534         assertm_if(NULL == viewer_list, "NULL!!");
535
536         elm_object_style_set(viewer_list, "dialogue");
537         elm_genlist_mode_set(viewer_list, ELM_LIST_LIMIT);
538
539         evas_object_size_hint_weight_set(viewer_list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
540         evas_object_size_hint_align_set(viewer_list, EVAS_HINT_FILL, EVAS_HINT_FILL);
541
542         itc.item_style = "dialogue/2text.2icon.3.tb";
543         itc.func.text_get = _gl_listview_text_get;
544         itc.func.content_get = _gl_listview_content_get;
545         itc.func.state_get = NULL;
546         itc.func.del = _gl_listview_del;
547
548         first_item = last_item = NULL;
549
550         evas_object_smart_callback_add(viewer_list, "realized", _gl_realized, NULL);
551         evas_object_smart_callback_add(viewer_list, "highlighted", _gl_highlighted, NULL);
552         evas_object_smart_callback_add(viewer_list, "unhighlighted", _gl_unhighlighted, NULL);
553
554         __COMMON_FUNC_EXIT__;
555         return viewer_list;
556 }
557
558 int viewer_list_destroy(void)
559 {
560         __COMMON_FUNC_ENTER__;
561
562         assertm_if(NULL == viewer_list, "NULL!!");
563         viewer_list_title_item_del();
564         viewer_list_item_clear();
565         evas_object_del(viewer_list);
566         viewer_list = NULL;
567
568         __COMMON_FUNC_EXIT__;
569         return TRUE;
570 }
571
572 void viewer_list_title_item_del(void)
573 {
574         if (grouptitle) {
575                 elm_object_item_del(grouptitle);
576                 grouptitle = NULL;
577         }
578 }
579
580 void viewer_list_title_item_update(void)
581 {
582         elm_genlist_item_update(grouptitle);
583 }
584
585 void viewer_list_title_item_set(void)
586 {
587         if (grouptitle != NULL || viewer_list_item_size_get() != 0)
588                 return;
589
590         // To use multiline textblock/entry/editfield in genlist, set height_for_width mode
591         // then the item's height is calculated while the item's width fits to genlist width.
592         elm_genlist_mode_set(viewer_list, ELM_LIST_COMPRESS);
593
594         grouptitle_itc.item_style = "dialogue/title";
595         grouptitle_itc.func.text_get = _gl_text_title_get;
596         grouptitle_itc.func.content_get = _gl_content_title_get;
597         grouptitle_itc.func.state_get = NULL;
598         grouptitle_itc.func.del = NULL;
599
600         assertm_if(NULL != grouptitle, "Err!!");
601
602         grouptitle = elm_genlist_item_append(viewer_list,
603                         &grouptitle_itc,
604                         NULL,
605                         NULL,
606                         ELM_GENLIST_ITEM_NONE,
607                         NULL,
608                         NULL);
609
610         assertm_if(NULL == grouptitle, "NULL!!");
611
612         elm_genlist_item_select_mode_set(grouptitle, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
613 }
614
615 int viewer_list_item_radio_mode_set(Elm_Object_Item* item,
616                 VIEWER_ITEM_RADIO_MODES mode)
617 {
618         __COMMON_FUNC_ENTER__;
619
620         if (NULL == item) {
621                 INFO_LOG(COMMON_NAME_ERR, "item is NULL");
622                 return FALSE;
623         }
624
625         ug_genlist_data_t* gdata = (ug_genlist_data_t *) elm_object_item_data_get(item);
626         if (NULL == gdata || NULL == gdata->device_info) {
627                 INFO_LOG(COMMON_NAME_ERR, "gdata or device_info is NULL");
628                 return FALSE;
629         }
630
631         if (gdata->radio_mode == mode) {
632                 INFO_LOG(UG_NAME_NORMAL, "[%s] is already in requested state", gdata->device_info->ssid);
633                 return FALSE;
634         }
635
636         INFO_LOG(UG_NAME_NORMAL, "[%s] AP Item State Transition from [%d] --> [%d]", gdata->device_info->ssid, gdata->radio_mode, mode);
637         gdata->radio_mode = mode;
638         if (gdata->device_info->ap_status_txt) {
639                 g_free(gdata->device_info->ap_status_txt);
640                 gdata->device_info->ap_status_txt = viewer_list_get_device_status_txt(gdata->device_info, mode);
641         }
642
643         elm_genlist_item_update(item);
644
645         __COMMON_FUNC_EXIT__;
646         return TRUE;
647 }
648
649 Elm_Object_Item* viewer_list_item_insert_after(wifi_ap_h ap, Elm_Object_Item *after)
650 {
651         Elm_Object_Item* ret = NULL;
652         wifi_security_type_e sec_type;
653
654         retvm_if(NULL == viewer_list, NULL);
655
656         wifi_device_info_t *wifi_device = NULL;
657
658         if (ap == NULL) {
659                 wifi_device = wlan_manager_profile_device_info_blank_create();
660                 retvm_if(NULL == wifi_device, NULL);
661         } else {
662                 wifi_device = g_new0(wifi_device_info_t, 1);
663                 retvm_if(NULL == wifi_device, NULL);
664
665                 if (WIFI_ERROR_NONE != wifi_ap_clone(&(wifi_device->ap), ap)) {
666                         goto FREE_DEVICE_INFO;
667                 } else if (WIFI_ERROR_NONE != wifi_ap_get_essid(ap, &(wifi_device->ssid))) {
668                         goto FREE_DEVICE_INFO;
669                 } else if (WIFI_ERROR_NONE != wifi_ap_get_rssi(ap, &(wifi_device->rssi))) {
670                         goto FREE_DEVICE_INFO;
671                 } else if (WIFI_ERROR_NONE != wifi_ap_get_security_type(ap, &sec_type)) {
672                         goto FREE_DEVICE_INFO;
673                 } else if (WIFI_ERROR_NONE != wifi_ap_is_wps_supported(ap, &(wifi_device->wps_mode))) {
674                         goto FREE_DEVICE_INFO;
675                 }
676                 wifi_device->security_mode = common_utils_get_sec_mode(sec_type);
677                 wifi_device->ap_status_txt = viewer_list_get_device_status_txt(wifi_device, VIEWER_ITEM_RADIO_MODE_OFF);
678                 common_utils_get_device_icon(WIFI_APP_IMAGE_DIR,
679                                 wifi_device,
680                                 &wifi_device->ap_image_path);
681
682         }
683         ug_genlist_data_t* gdata = g_new0(ug_genlist_data_t, 1);
684         retvm_if(NULL == gdata, NULL);
685
686         gdata->device_info = wifi_device;
687         gdata->radio_mode = VIEWER_ITEM_RADIO_MODE_OFF;
688
689         if (!after) {   /* If the after item is NULL then insert it as first item */
690                 after = grouptitle;
691         }
692
693         ret = elm_genlist_item_insert_after(
694                         viewer_list, /*obj*/
695                         &itc,/*itc*/
696                         gdata,/*data*/
697                         NULL,/*parent*/
698                         after, /*after than*/
699                         ELM_GENLIST_ITEM_NONE, /*flags*/
700                         __viewer_list_item_clicked_cb,/*func*/
701                         NULL);/*func_data*/
702
703         if (!ret) {
704                 assertm_if(NULL == ret, "NULL!!");
705                 g_free(gdata);
706         } else {
707                 DEBUG_LOG(UG_NAME_NORMAL,
708                                 "* item add complete item [0x%x] ssid:[%s] security[%d] size:[%d]",
709                                 ret,
710                                 wifi_device->ssid,
711                                 wifi_device->security_mode,
712                                 viewer_list_item_size_get());
713
714                 if (after == grouptitle) {
715                         first_item = ret;
716                         if (!last_item)
717                                 last_item = ret;
718                 } else {
719                         last_item = ret;
720                         if (!first_item)
721                                 first_item = ret;
722                 }
723
724                 elm_genlist_item_update(ret);
725         }
726
727 FREE_DEVICE_INFO:
728         if (!ret && wifi_device) {
729                 wifi_ap_destroy(wifi_device->ap);
730                 g_free(wifi_device->ap_image_path);
731                 g_free(wifi_device->ap_status_txt);
732                 g_free(wifi_device->ssid);
733                 g_free(wifi_device);
734         }
735
736         return ret;
737 }
738
739 void viewer_list_item_del(Elm_Object_Item *it)
740 {
741         if (it == NULL)
742                 return;
743
744         if (it == first_item) {
745                 first_item = elm_genlist_item_next_get(first_item);
746         } else if (it == last_item) {
747                 last_item = elm_genlist_item_prev_get(last_item);
748         }
749         elm_object_item_del(it);
750 }
751
752 int viewer_list_item_size_get()
753 {
754         __COMMON_FUNC_ENTER__;
755         int ret = 0;
756         Elm_Object_Item *it = first_item;
757
758         while(it) {
759                 ret++;
760                 if (it == last_item)
761                         break;
762                 it = elm_genlist_item_next_get(it);
763         }
764
765         __COMMON_FUNC_EXIT__;
766         return ret;
767 }
768
769 void viewer_list_item_clear(void)
770 {
771         __COMMON_FUNC_ENTER__;
772
773         Elm_Object_Item *it = first_item;
774         Elm_Object_Item *nxt = NULL;
775
776         while(it) {
777                 nxt = elm_genlist_item_next_get(it);
778                 elm_object_item_del(it);
779                 if (it == last_item)
780                         break;
781                 it = nxt;
782         }
783
784         first_item = last_item = NULL;
785
786         __COMMON_FUNC_EXIT__;
787 }
788
789 void viewer_list_item_enable_all(void)
790 {
791         __COMMON_FUNC_ENTER__;
792
793         Elm_Object_Item *it = first_item;
794
795         while(it) {
796                 elm_object_item_disabled_set(it, EINA_FALSE);
797
798                 if (it == last_item)
799                         break;
800
801                 it = elm_genlist_item_next_get(it);
802         }
803
804         __COMMON_FUNC_EXIT__;
805 }
806
807 void viewer_list_item_disable_all(void)
808 {
809         __COMMON_FUNC_ENTER__;
810
811         Elm_Object_Item *it = first_item;
812
813         while(it) {
814                 elm_object_item_disabled_set(it, EINA_TRUE);
815
816                 if (it == last_item)
817                         break;
818
819                 it = elm_genlist_item_next_get(it);
820         }
821
822         __COMMON_FUNC_EXIT__;
823 }
824
825 Elm_Object_Item* item_get_for_ap(wifi_ap_h ap)
826 {
827         __COMMON_FUNC_ENTER__;
828         if (!ap) {
829                 __COMMON_FUNC_EXIT__;
830                 return NULL;
831         }
832         
833         char *essid = NULL;
834         wifi_security_type_e type = WIFI_SECURITY_TYPE_NONE;
835
836         if (WIFI_ERROR_NONE != wifi_ap_get_essid(ap, &essid)) {
837                 __COMMON_FUNC_EXIT__;
838                 return NULL;
839         } else if (WIFI_ERROR_NONE != wifi_ap_get_security_type(ap, &type)) {
840                 __COMMON_FUNC_EXIT__;
841                 return NULL;
842         }
843
844         Elm_Object_Item *it = first_item;
845         wlan_security_mode_type_t sec_mode = common_utils_get_sec_mode(type);
846         while(it) {
847                 ug_genlist_data_t* gdata = elm_object_item_data_get(it);
848                 wifi_device_info_t *device_info = NULL;
849                 if (gdata && (device_info = gdata->device_info)) {
850                         if (!g_strcmp0(device_info->ssid, essid) && device_info->security_mode == sec_mode)
851                                 break;
852                 }
853                 if (it == last_item) {
854                         it = NULL;
855                         break;
856                 }
857                 it = elm_genlist_item_next_get(it);
858         }
859
860         g_free(essid);
861         __COMMON_FUNC_EXIT__;
862         return it;
863 }