Implement async connect
[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         assertm_if(NULL == item, "item is NULL!!");
222
223         int index = (int)elm_genlist_item_index_get(item);
224         int first_item_index = (int)elm_genlist_item_index_get(viewer_list_get_first_item());
225         int last_item_index = (int)elm_genlist_item_index_get(viewer_list_get_last_item());
226
227         if (last_item_index == FIRST_ITEM_NUMBER)
228                 return ;
229
230         if (first_item_index == -1) {
231                 int group_index = (int)elm_genlist_item_index_get(grouptitle);
232                 first_item_index = group_index+1;
233         }
234
235         if (first_item_index <= index && first_item_index - last_item_index) {
236                 if(index == first_item_index)
237                         elm_object_item_signal_emit(item, "elm,state,top", "");
238                 else if (index == last_item_index)
239                         elm_object_item_signal_emit(item, "elm,state,bottom", "");
240                 else
241                         elm_object_item_signal_emit(item, "elm,state,center", "");
242
243                 Evas_Object *ao = elm_object_item_access_object_get(item);
244                 elm_access_info_cb_set(ao, ELM_ACCESS_INFO, _access_info_cb, elm_object_item_data_get(item));
245                 elm_access_info_set(ao, ELM_ACCESS_TYPE, "double tap to connect device");
246                 elm_access_info_set(ao, ELM_ACCESS_STATE, "More button");
247         }
248
249 }
250
251 static void _gl_highlighted(void *data, Evas_Object *obj, void *event_info)
252 {
253         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
254         if (item) {
255                 ug_genlist_data_t *gdata = (ug_genlist_data_t *)elm_object_item_data_get(item);
256                 if (gdata) {
257                         gdata->highlighted = TRUE;
258                         elm_genlist_item_fields_update(item, "elm.icon.1", ELM_GENLIST_ITEM_FIELD_CONTENT);
259                         elm_genlist_item_fields_update(item, "elm.text.2", ELM_GENLIST_ITEM_FIELD_TEXT);
260                 }
261         }
262 }
263
264 static void _gl_unhighlighted(void *data, Evas_Object *obj, void *event_info)
265 {
266         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
267         if (item) {
268                 ug_genlist_data_t *gdata = (ug_genlist_data_t *)elm_object_item_data_get(item);
269                 if (gdata) {
270                         gdata->highlighted = FALSE;
271                         elm_genlist_item_fields_update(item, "elm.icon.1", ELM_GENLIST_ITEM_FIELD_CONTENT);
272                         elm_genlist_item_fields_update(item, "elm.text.2", ELM_GENLIST_ITEM_FIELD_TEXT);
273                 }
274         }
275 }
276
277 static void __passwd_popup_cancel_cb(void *data,  Evas_Object *obj,
278                 void *event_info)
279 {
280         __COMMON_FUNC_ENTER__;
281
282         if (ug_app_state->passpopup == NULL)
283                 return;
284
285         passwd_popup_free(ug_app_state->passpopup);
286         ug_app_state->passpopup = NULL;
287
288         __COMMON_FUNC_EXIT__;
289 }
290
291 static void __passwd_popup_ok_cb(void *data, Evas_Object *obj, void *event_info)
292 {
293         __COMMON_FUNC_ENTER__;
294
295         wifi_ap_h ap = NULL;
296         int password_len = 0;
297         const char *password = NULL;
298         wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
299
300         if (ug_app_state->passpopup == NULL)
301                 return;
302
303         ap = passwd_popup_get_ap(ug_app_state->passpopup);
304         password = passwd_popup_get_txt(ug_app_state->passpopup);
305         password_len = strlen(password);
306
307         wifi_ap_get_security_type(ap, &sec_type);
308
309         switch (sec_type) {
310         case WIFI_SECURITY_TYPE_WEP:
311                 if (password_len != 5 && password_len != 13 &&
312                                 password_len != 26 && password_len != 10) {
313                         winset_popup_mode_set(ug_app_state->popup_manager,
314                                         POPUP_OPTION_WEP_PSWD_LEN_ERROR, NULL);
315
316                         goto popup_ok_cb_exit;
317                 }
318                 break;
319
320         case WIFI_SECURITY_TYPE_WPA_PSK:
321         case WIFI_SECURITY_TYPE_WPA2_PSK:
322                 if (password_len < 8 || password_len > 63) {
323                         winset_popup_mode_set(ug_app_state->popup_manager,
324                                         POPUP_OPTION_WPA_PSWD_LEN_ERROR, NULL);
325
326                         goto popup_ok_cb_exit;
327                 }
328                 break;
329
330         default:
331                 ERROR_LOG(UG_NAME_NORMAL, "Wrong security mode: %d", sec_type);
332                 passwd_popup_free(ug_app_state->passpopup);
333                 ug_app_state->passpopup = NULL;
334
335                 goto popup_ok_cb_exit;
336         }
337
338         wlan_manager_connect_with_password(ap, password);
339
340         passwd_popup_free(ug_app_state->passpopup);
341         ug_app_state->passpopup = NULL;
342
343 popup_ok_cb_exit:
344         g_free((gpointer)password);
345
346         __COMMON_FUNC_EXIT__;
347 }
348
349 static void __wps_pbc_popup_cancel_connecting(void *data, Evas_Object *obj,
350                 void *event_info)
351 {
352         if (ug_app_state->passpopup == NULL)
353                 return;
354
355         wifi_ap_h ap = passwd_popup_get_ap(ug_app_state->passpopup);;
356
357         int ret = wlan_manager_disconnect(ap);
358         if (ret != WLAN_MANAGER_ERR_NONE)
359                 ERROR_LOG(UG_NAME_NORMAL, "Failed WPS PBC cancellation [0x%x]", ap);
360
361         passwd_popup_free(ug_app_state->passpopup);
362         ug_app_state->passpopup = NULL;
363
364         viewer_manager_header_mode_set(HEADER_MODE_ON);
365 }
366
367 static void _wps_btn_cb(void* data, Evas_Object* obj, void* event_info)
368 {
369         __COMMON_FUNC_ENTER__;
370         if (!ug_app_state->passpopup) {
371                 return;
372         }
373
374         wifi_ap_h ap = passwd_popup_get_ap(ug_app_state->passpopup);
375         int ret = wlan_manager_wps_connect(ap);
376         if (ret == WLAN_MANAGER_ERR_NONE) {
377                 create_pbc_popup(ug_app_state->passpopup, __wps_pbc_popup_cancel_connecting, NULL);
378         } else {
379                 ERROR_LOG(UG_NAME_NORMAL, "wlan_manager_wps_connect failed");
380                 passwd_popup_free(ug_app_state->passpopup);
381                 ug_app_state->passpopup = NULL;
382         }
383
384         __COMMON_FUNC_EXIT__;
385 }
386
387 static void __viewer_list_wifi_connect(wifi_device_info_t *device_info)
388 {
389         int rv;
390         bool favorite = false;
391         Evas_Object* navi_frame = NULL;
392         pswd_popup_create_req_data_t popup_info;
393         wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
394
395         if (device_info == NULL)
396                 return;
397
398         wifi_ap_is_favorite(device_info->ap, &favorite);
399
400         if (favorite == true) {
401                 rv = wlan_manager_connect(device_info->ap);
402                 return;
403         }
404
405         wifi_ap_get_security_type(device_info->ap, &sec_type);
406
407         switch (sec_type) {
408         case WIFI_SECURITY_TYPE_NONE:
409                 rv = wlan_manager_connect(device_info->ap);
410                 break;
411
412         case WIFI_SECURITY_TYPE_WEP:
413         case WIFI_SECURITY_TYPE_WPA_PSK:
414         case WIFI_SECURITY_TYPE_WPA2_PSK:
415                 memset(&popup_info, 0, sizeof(pswd_popup_create_req_data_t));
416
417                 popup_info.title = device_info->ssid;
418                 popup_info.ok_cb = __passwd_popup_ok_cb;
419                 popup_info.cancel_cb = __passwd_popup_cancel_cb;
420                 popup_info.show_wps_btn = device_info->wps_mode;
421                 popup_info.wps_btn_cb = _wps_btn_cb;
422                 popup_info.ap = device_info->ap;
423                 popup_info.cb_data = NULL;
424
425                 /* TODO: parameter with device_info */
426                 /* TODO: finally parameter with wifi_ap_h, WPA, EAP */
427                 ug_app_state->passpopup = create_passwd_popup(
428                                 ug_app_state->layout_main, PACKAGE, &popup_info);
429
430                 if (ug_app_state->passpopup == NULL)
431                         INFO_LOG(UG_NAME_ERR, "Fail to create password popup");
432
433                 break;
434
435         case WIFI_SECURITY_TYPE_EAP:
436                 navi_frame = viewer_manager_get_naviframe();
437                 ug_app_state->eap_view = create_eap_view(
438                                 ug_app_state->layout_main, navi_frame, PACKAGE, device_info);
439                 break;
440
441         default:
442                 ERROR_LOG(UG_NAME_NORMAL, "Unknown security type [%d]", sec_type);
443                 break;
444         }
445 }
446
447 Elm_Object_Item *viewer_list_target_item_get(VIEWER_ITEM_RADIO_MODES mode)
448 {
449         __COMMON_FUNC_ENTER__;
450
451         Elm_Object_Item *target = viewer_list_get_first_item();
452
453         while(target) {
454                 ug_genlist_data_t *gdata = (ug_genlist_data_t *)elm_object_item_data_get(target);
455                 retvm_if(NULL == gdata, NULL);
456                 if (gdata->radio_mode == mode)
457                         return target;
458
459                 if (target == viewer_list_get_last_item())
460                         break;
461                 else
462                         target = elm_genlist_item_next_get(target);
463         }
464
465         __COMMON_FUNC_EXIT__;
466         return NULL;
467 }
468
469 static void __viewer_list_force_set_wifi_status_connecting(Elm_Object_Item *target)
470 {
471         __COMMON_FUNC_ENTER__;
472
473         Elm_Object_Item *connecting_target = viewer_list_target_item_get(VIEWER_ITEM_RADIO_MODE_CONNECTING);
474
475         ug_genlist_data_t *target_data = NULL;
476
477         if (viewer_manager_header_mode_get() == HEADER_MODE_CONNECTING) {
478                 target_data = (ug_genlist_data_t *)elm_object_item_data_get(connecting_target);
479                 if (target_data)
480                         wlan_manager_disconnect(target_data->device_info->ap);
481                 viewer_list_item_radio_mode_set(connecting_target, VIEWER_ITEM_RADIO_MODE_OFF);
482         }
483
484         __COMMON_FUNC_EXIT__;
485 }
486
487 static void __viewer_list_item_clicked_cb(void *data, Evas_Object *obj,
488                 void *event_info)
489 {
490         __COMMON_FUNC_ENTER__;
491
492         assertm_if(NULL == event_info, "event_info is NULL!!");
493         assertm_if(NULL == obj, "obj is NULL!!");
494
495         Elm_Object_Item *it = (Elm_Object_Item *)event_info;
496         ug_genlist_data_t *gdata = NULL;
497         wifi_device_info_t *device_info = NULL;
498
499         gdata = (ug_genlist_data_t *)elm_object_item_data_get(it);
500         retm_if(NULL == gdata);
501
502         device_info = gdata->device_info;
503         retm_if(NULL == device_info || NULL == device_info->ssid);
504
505         int item_state = gdata->radio_mode;
506         int current_state = 0;
507         int ret = -1;
508
509         INFO_LOG(UG_NAME_NORMAL, "ssid --- %s", device_info->ssid);
510         INFO_LOG(UG_NAME_NORMAL, "ap --- 0x%x", device_info->ap);
511         INFO_LOG(UG_NAME_NORMAL, "current item_state state is --- %d\n", item_state);
512
513         switch (item_state) {
514         case VIEWER_ITEM_RADIO_MODE_OFF:
515                 current_state = viewer_manager_header_mode_get();
516
517                 INFO_LOG(UG_NAME_NORMAL, "Clicked AP information\n");
518                 INFO_LOG(UG_NAME_NORMAL, "header mode [%d]", current_state);
519
520                 switch (current_state) {
521                 case HEADER_MODE_CONNECTED:
522                 case HEADER_MODE_ON:
523                 case HEADER_MODE_CONNECTING:
524                         __viewer_list_wifi_connect(device_info);
525                         __viewer_list_force_set_wifi_status_connecting(it);
526                         viewer_list_item_radio_mode_set(it, VIEWER_ITEM_RADIO_MODE_CONNECTING);
527                         break;
528
529                 case HEADER_MODE_OFF:
530                 case HEADER_MODE_SEARCHING:
531                 case HEADER_MODE_ACTIVATING:
532                 case HEADER_MODE_DEACTIVATING:
533                 default:
534                         INFO_LOG(UG_NAME_NORMAL, "Ignore click");
535                         break;
536                 }
537                 break;
538
539         case VIEWER_ITEM_RADIO_MODE_CONNECTING:
540                 ret = wlan_manager_disconnect(device_info->ap);
541                 if (ret != WLAN_MANAGER_ERR_NONE)
542                         ERROR_LOG(UG_NAME_NORMAL, "Failed connect cancel [0x%x]", device_info);
543                 break;
544         case VIEWER_ITEM_RADIO_MODE_CONNECTED:
545         default:
546                 INFO_LOG(UG_NAME_NORMAL, "Ignore click");
547                 break;
548         }
549
550         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
551
552         __COMMON_FUNC_EXIT__;
553 }
554
555 static char *viewer_list_get_device_status_txt(wifi_device_info_t *wifi_device, VIEWER_ITEM_RADIO_MODES mode)
556 {
557         char *status_txt = NULL;
558         /* The strings are currently hard coded. It will be replaced with string ids later */
559         if (VIEWER_ITEM_RADIO_MODE_CONNECTING == mode) {
560                 status_txt = g_strdup(sc(PACKAGE, I18N_TYPE_Connecting));
561         } else if (VIEWER_ITEM_RADIO_MODE_DISCONNECTING == mode) {
562                 status_txt = g_strdup(sc(PACKAGE, I18N_TYPE_Disconnecting));
563         } else if (VIEWER_ITEM_RADIO_MODE_CONNECTED == mode) {
564                 status_txt = g_strdup(sc(PACKAGE, I18N_TYPE_Connected));
565         } else if (VIEWER_ITEM_RADIO_MODE_OFF == mode) {
566                 status_txt = common_utils_get_ap_security_type_info_txt(PACKAGE, wifi_device);
567         } else {
568                 status_txt = g_strdup(sc(PACKAGE, I18N_TYPE_Unknown));
569                 INFO_LOG(UG_NAME_NORMAL, "Invalid mode: %d", mode);
570         }
571         return status_txt;
572 }
573
574 Evas_Object* viewer_list_create(Evas_Object *win)
575 {
576         __COMMON_FUNC_ENTER__;
577
578         viewer_list = NULL;
579         first_item = NULL;
580         last_item = NULL;
581         grouptitle = NULL;
582
583         assertm_if(NULL == win, "NULL!!");
584         viewer_list = elm_genlist_add(win);
585         assertm_if(NULL == viewer_list, "NULL!!");
586
587         elm_object_style_set(viewer_list, "dialogue");
588         elm_genlist_mode_set(viewer_list, ELM_LIST_LIMIT);
589
590         evas_object_size_hint_weight_set(viewer_list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
591         evas_object_size_hint_align_set(viewer_list, EVAS_HINT_FILL, EVAS_HINT_FILL);
592
593         itc.item_style = "dialogue/2text.2icon.3.tb";
594         itc.func.text_get = _gl_listview_text_get;
595         itc.func.content_get = _gl_listview_content_get;
596         itc.func.state_get = NULL;
597         itc.func.del = _gl_listview_del;
598
599         first_item = last_item = NULL;
600
601         evas_object_smart_callback_add(viewer_list, "realized", _gl_realized, NULL);
602         evas_object_smart_callback_add(viewer_list, "highlighted", _gl_highlighted, NULL);
603         evas_object_smart_callback_add(viewer_list, "unhighlighted", _gl_unhighlighted, NULL);
604
605         __COMMON_FUNC_EXIT__;
606         return viewer_list;
607 }
608
609 int viewer_list_destroy(void)
610 {
611         __COMMON_FUNC_ENTER__;
612
613         assertm_if(NULL == viewer_list, "NULL!!");
614         viewer_list_title_item_del();
615         viewer_list_item_clear();
616         evas_object_del(viewer_list);
617         viewer_list = NULL;
618
619         __COMMON_FUNC_EXIT__;
620         return TRUE;
621 }
622
623 void viewer_list_title_item_del(void)
624 {
625         if (grouptitle) {
626                 elm_object_item_del(grouptitle);
627                 grouptitle = NULL;
628         }
629 }
630
631 void viewer_list_title_item_update(void)
632 {
633         elm_genlist_item_update(grouptitle);
634 }
635
636 void viewer_list_title_item_set(void)
637 {
638         if (grouptitle != NULL || viewer_list_item_size_get() != 0)
639                 return;
640
641         // To use multiline textblock/entry/editfield in genlist, set height_for_width mode
642         // then the item's height is calculated while the item's width fits to genlist width.
643         elm_genlist_mode_set(viewer_list, ELM_LIST_COMPRESS);
644
645         grouptitle_itc.item_style = "dialogue/title";
646         grouptitle_itc.func.text_get = _gl_text_title_get;
647         grouptitle_itc.func.content_get = _gl_content_title_get;
648         grouptitle_itc.func.state_get = NULL;
649         grouptitle_itc.func.del = NULL;
650
651         assertm_if(NULL != grouptitle, "Err!!");
652
653         grouptitle = elm_genlist_item_append(viewer_list,
654                         &grouptitle_itc,
655                         NULL,
656                         NULL,
657                         ELM_GENLIST_ITEM_NONE,
658                         NULL,
659                         NULL);
660
661         assertm_if(NULL == grouptitle, "NULL!!");
662
663         elm_genlist_item_select_mode_set(grouptitle, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
664 }
665
666 int viewer_list_item_radio_mode_set(Elm_Object_Item* item,
667                 VIEWER_ITEM_RADIO_MODES mode)
668 {
669         __COMMON_FUNC_ENTER__;
670
671         if (NULL == item) {
672                 INFO_LOG(COMMON_NAME_ERR, "item is NULL");
673                 return FALSE;
674         }
675
676         ug_genlist_data_t* gdata = (ug_genlist_data_t *) elm_object_item_data_get(item);
677         if (NULL == gdata || NULL == gdata->device_info) {
678                 INFO_LOG(COMMON_NAME_ERR, "gdata or device_info is NULL");
679                 return FALSE;
680         }
681
682         if (gdata->radio_mode == VIEWER_ITEM_RADIO_MODE_CONNECTED)
683                 mode = VIEWER_ITEM_RADIO_MODE_DISCONNECTING;
684
685         if (gdata->radio_mode == mode) {
686                 INFO_LOG(UG_NAME_NORMAL, "[%s] is already in requested state", gdata->device_info->ssid);
687                 return FALSE;
688         }
689
690         INFO_LOG(UG_NAME_NORMAL, "[%s] AP Item State Transition from [%d] --> [%d]", gdata->device_info->ssid, gdata->radio_mode, mode);
691         gdata->radio_mode = mode;
692         if (gdata->device_info->ap_status_txt) {
693                 g_free(gdata->device_info->ap_status_txt);
694                 gdata->device_info->ap_status_txt = viewer_list_get_device_status_txt(gdata->device_info, mode);
695         }
696
697         elm_genlist_item_update(item);
698
699         __COMMON_FUNC_EXIT__;
700         return TRUE;
701 }
702
703 Elm_Object_Item* viewer_list_item_insert_after(wifi_ap_h ap, Elm_Object_Item *after)
704 {
705         Elm_Object_Item* ret = NULL;
706         wifi_security_type_e sec_type;
707
708         retvm_if(NULL == viewer_list, NULL);
709
710         wifi_device_info_t *wifi_device = NULL;
711
712         if (ap == NULL) {
713                 wifi_device = wlan_manager_profile_device_info_blank_create();
714                 retvm_if(NULL == wifi_device, NULL);
715         } else {
716                 wifi_device = g_new0(wifi_device_info_t, 1);
717                 retvm_if(NULL == wifi_device, NULL);
718
719                 if (WIFI_ERROR_NONE != wifi_ap_clone(&(wifi_device->ap), ap)) {
720                         goto FREE_DEVICE_INFO;
721                 } else if (WIFI_ERROR_NONE != wifi_ap_get_essid(ap, &(wifi_device->ssid))) {
722                         goto FREE_DEVICE_INFO;
723                 } else if (WIFI_ERROR_NONE != wifi_ap_get_rssi(ap, &(wifi_device->rssi))) {
724                         goto FREE_DEVICE_INFO;
725                 } else if (WIFI_ERROR_NONE != wifi_ap_get_security_type(ap, &sec_type)) {
726                         goto FREE_DEVICE_INFO;
727                 } else if (WIFI_ERROR_NONE != wifi_ap_is_wps_supported(ap, &(wifi_device->wps_mode))) {
728                         goto FREE_DEVICE_INFO;
729                 }
730                 wifi_device->security_mode = common_utils_get_sec_mode(sec_type);
731                 wifi_device->ap_status_txt = viewer_list_get_device_status_txt(wifi_device, VIEWER_ITEM_RADIO_MODE_OFF);
732                 common_utils_get_device_icon(WIFI_APP_IMAGE_DIR,
733                                 wifi_device,
734                                 &wifi_device->ap_image_path);
735
736         }
737         ug_genlist_data_t* gdata = g_new0(ug_genlist_data_t, 1);
738         retvm_if(NULL == gdata, NULL);
739
740         gdata->device_info = wifi_device;
741         gdata->radio_mode = VIEWER_ITEM_RADIO_MODE_OFF;
742
743         if (!after) {   /* If the after item is NULL then insert it as first item */
744                 after = grouptitle;
745         }
746
747         ret = elm_genlist_item_insert_after(
748                         viewer_list, /*obj*/
749                         &itc,/*itc*/
750                         gdata,/*data*/
751                         NULL,/*parent*/
752                         after, /*after than*/
753                         ELM_GENLIST_ITEM_NONE, /*flags*/
754                         __viewer_list_item_clicked_cb,/*func*/
755                         NULL);/*func_data*/
756
757         if (!ret) {
758                 assertm_if(NULL == ret, "NULL!!");
759                 g_free(gdata);
760         } else {
761                 DEBUG_LOG(UG_NAME_NORMAL,
762                                 "* item add complete item [0x%x] ssid:[%s] security[%d] size:[%d]",
763                                 ret,
764                                 wifi_device->ssid,
765                                 wifi_device->security_mode,
766                                 viewer_list_item_size_get());
767
768                 if (after == grouptitle) {
769                         first_item = ret;
770                         if (!last_item)
771                                 last_item = ret;
772                 } else {
773                         last_item = ret;
774                         if (!first_item)
775                                 first_item = ret;
776                 }
777
778                 elm_genlist_item_update(ret);
779         }
780
781 FREE_DEVICE_INFO:
782         if (!ret && wifi_device) {
783                 wifi_ap_destroy(wifi_device->ap);
784                 g_free(wifi_device->ap_image_path);
785                 g_free(wifi_device->ap_status_txt);
786                 g_free(wifi_device->ssid);
787                 g_free(wifi_device);
788         }
789
790         return ret;
791 }
792
793 void viewer_list_item_del(Elm_Object_Item *it)
794 {
795         if (it == NULL)
796                 return;
797
798         if (it == first_item) {
799                 first_item = elm_genlist_item_next_get(first_item);
800         } else if (it == last_item) {
801                 last_item = elm_genlist_item_prev_get(last_item);
802         }
803         elm_object_item_del(it);
804 }
805
806 int viewer_list_item_size_get()
807 {
808         __COMMON_FUNC_ENTER__;
809         int ret = 0;
810         Elm_Object_Item *it = first_item;
811
812         while(it) {
813                 ret++;
814                 if (it == last_item)
815                         break;
816                 it = elm_genlist_item_next_get(it);
817         }
818
819         __COMMON_FUNC_EXIT__;
820         return ret;
821 }
822
823 void viewer_list_item_clear(void)
824 {
825         __COMMON_FUNC_ENTER__;
826
827         Elm_Object_Item *it = first_item;
828         Elm_Object_Item *nxt = NULL;
829
830         while(it) {
831                 nxt = elm_genlist_item_next_get(it);
832                 elm_object_item_del(it);
833                 if (it == last_item)
834                         break;
835                 it = nxt;
836         }
837
838         first_item = last_item = NULL;
839
840         __COMMON_FUNC_EXIT__;
841 }
842
843 void viewer_list_item_enable_all(void)
844 {
845         __COMMON_FUNC_ENTER__;
846
847         Elm_Object_Item *it = first_item;
848
849         while(it) {
850                 elm_object_item_disabled_set(it, EINA_FALSE);
851
852                 if (it == last_item)
853                         break;
854
855                 it = elm_genlist_item_next_get(it);
856         }
857
858         __COMMON_FUNC_EXIT__;
859 }
860
861 void viewer_list_item_disable_all(void)
862 {
863         __COMMON_FUNC_ENTER__;
864
865         Elm_Object_Item *it = first_item;
866
867         while(it) {
868                 elm_object_item_disabled_set(it, EINA_TRUE);
869
870                 if (it == last_item)
871                         break;
872
873                 it = elm_genlist_item_next_get(it);
874         }
875
876         __COMMON_FUNC_EXIT__;
877 }
878
879 Elm_Object_Item* item_get_for_ap(wifi_ap_h ap)
880 {
881         __COMMON_FUNC_ENTER__;
882         if (!ap) {
883                 __COMMON_FUNC_EXIT__;
884                 return NULL;
885         }
886         
887         char *essid = NULL;
888         wifi_security_type_e type = WIFI_SECURITY_TYPE_NONE;
889
890         if (WIFI_ERROR_NONE != wifi_ap_get_essid(ap, &essid)) {
891                 __COMMON_FUNC_EXIT__;
892                 return NULL;
893         } else if (WIFI_ERROR_NONE != wifi_ap_get_security_type(ap, &type)) {
894                 __COMMON_FUNC_EXIT__;
895                 return NULL;
896         }
897
898         Elm_Object_Item *it = first_item;
899         wlan_security_mode_type_t sec_mode = common_utils_get_sec_mode(type);
900         while(it) {
901                 ug_genlist_data_t* gdata = elm_object_item_data_get(it);
902                 wifi_device_info_t *device_info = NULL;
903                 if (gdata && (device_info = gdata->device_info)) {
904                         if (!g_strcmp0(device_info->ssid, essid) && device_info->security_mode == sec_mode)
905                                 break;
906                 }
907                 if (it == last_item) {
908                         it = NULL;
909                         break;
910                 }
911                 it = elm_genlist_item_next_get(it);
912         }
913
914         g_free(essid);
915         __COMMON_FUNC_EXIT__;
916         return it;
917 }