ea255412df266743a640df01c1c604aca4f7b190
[apps/native/ug-wifi-efl.git] / sources / wifi-syspopup / viewer-popups / view-main.c
1 /*
2  * Wi-Fi
3  *
4  * Copyright 2012 Samsung Electronics Co., Ltd
5  *
6  * Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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 "view-main.h"
22 #include "common_pswd_popup.h"
23 #include "common_generate_pin.h"
24 #include "common_utils.h"
25 #include "view-alerts.h"
26 #include "i18nmanager.h"
27 #include "common_eap_connect.h"
28 #define VCONF_SORT_BY "file/private/wifi/sort_by"
29
30 #define QS_POPUP_CONNECTION_STATE       "qs_popup_connection_state"
31 #define WIFI_DEVPKR_EDJ "/usr/apps/wifi-efl-ug/res/edje/wifi-qs/wifi-syspopup-custom.edj"
32 #define WIFI_SYSPOPUP_EMPTY_GRP "devpkr_no_wifi_networks"
33
34 struct connecting_cancel_popup_data {
35         Evas_Object *popup;
36         wifi_ap_h ap;
37 };
38
39 static struct connecting_cancel_popup_data *g_disconnect_popup = NULL;
40
41 extern wifi_object* devpkr_app_state;
42
43 static Evas_Object* list = NULL;
44 static Elm_Genlist_Item_Class itc;
45 static int profiles_list_size = 0;
46 static Elm_Genlist_Item_Class grouptitle_itc;
47 static Elm_Object_Item *grouptitle = NULL;
48
49 static GList *wifi_device_list = NULL;
50
51 int view_main_get_profile_count(void)
52 {
53         return profiles_list_size;
54 }
55
56 static ITEM_CONNECTION_MODES view_main_state_get(void)
57 {
58         ITEM_CONNECTION_MODES state;
59
60         state = (ITEM_CONNECTION_MODES)evas_object_data_get(
61                         list, QS_POPUP_CONNECTION_STATE);
62
63         return state;
64 }
65
66 static void view_main_state_set(ITEM_CONNECTION_MODES state)
67 {
68         evas_object_data_set(list, QS_POPUP_CONNECTION_STATE, (const void *)state);
69 }
70
71 static void __popup_ok_cb(void *data, Evas_Object *obj, void *event_info)
72 {
73         __COMMON_FUNC_ENTER__;
74
75         wifi_ap_h ap = NULL;
76         int password_len = 0;
77         const char* password = NULL;
78         wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
79
80         if (devpkr_app_state->passpopup == NULL) {
81                 return;
82         }
83
84         ap = passwd_popup_get_ap(devpkr_app_state->passpopup);
85         password = passwd_popup_get_txt(devpkr_app_state->passpopup);
86         if (password != NULL)
87                 password_len = strlen(password);
88
89         wifi_ap_get_security_type(ap, &sec_type);
90
91         switch (sec_type) {
92         case WIFI_SECURITY_TYPE_WEP:
93                 if (password_len != 5 && password_len != 13 &&
94                                 password_len != 26 && password_len != 10) {
95                         common_utils_send_message_to_net_popup(
96                                         "Network connection popup",
97                                         "wrong password", "toast_popup", NULL);
98
99                         if (devpkr_app_state->passpopup->entry) {
100                                 elm_object_focus_set(
101                                                 devpkr_app_state->passpopup->entry,
102                                                 EINA_TRUE);
103                         }
104                         goto popup_ok_exit;
105                 }
106                 break;
107
108         case WIFI_SECURITY_TYPE_WPA_PSK:
109         case WIFI_SECURITY_TYPE_WPA2_PSK:
110                 if (password_len < 8 || password_len > 64) {
111                         common_utils_send_message_to_net_popup(
112                                         "Network connection popup",
113                                         "wrong password", "toast_popup", NULL);
114
115                         if (devpkr_app_state->passpopup->entry) {
116                                 elm_object_focus_set(
117                                                 devpkr_app_state->passpopup->entry,
118                                                 EINA_TRUE);
119                         }
120                         goto popup_ok_exit;
121                 }
122                 break;
123
124         default:
125                 ERROR_LOG(SP_NAME_ERR, "Wrong security mode: %d", sec_type);
126                 passwd_popup_free(devpkr_app_state->passpopup);
127                 break;
128         }
129
130         wlan_manager_connect_with_password(ap, password);
131
132         passwd_popup_free(devpkr_app_state->passpopup);
133         devpkr_app_state->passpopup = NULL;
134
135 popup_ok_exit:
136         g_free((gpointer)password);
137
138         __COMMON_FUNC_EXIT__;
139 }
140
141 static void __popup_cancel_cb(void *data, Evas_Object *obj, void *event_info)
142 {
143         __COMMON_FUNC_ENTER__;
144
145         if (devpkr_app_state->passpopup == NULL) {
146                 return;
147         }
148
149         passwd_popup_free(devpkr_app_state->passpopup);
150         devpkr_app_state->passpopup = NULL;
151
152         elm_object_focus_set(devpkr_app_state->popup , EINA_TRUE);
153
154         __COMMON_FUNC_EXIT__;
155 }
156
157 static void __wps_pbc_popup_cancel_connecting(void *data, Evas_Object *obj,
158                 void *event_info)
159 {
160         if (devpkr_app_state->passpopup == NULL) {
161                 return;
162         }
163
164         wifi_ap_h ap = passwd_popup_get_ap(devpkr_app_state->passpopup);
165
166         int ret = wlan_manager_disconnect(ap);
167         if (ret != WLAN_MANAGER_ERR_NONE) {
168                 ERROR_LOG(SP_NAME_ERR, "Failed WPS PBC cancellation [0x%x]", ap);
169         }
170
171         passwd_popup_free(devpkr_app_state->passpopup);
172         devpkr_app_state->passpopup = NULL;
173 }
174
175 static void _wps_btn_cb(void* data, Evas_Object* obj, void* event_info)
176 {
177         __COMMON_FUNC_ENTER__;
178
179         if (!devpkr_app_state->passpopup) {
180                 return;
181         }
182
183         wifi_ap_h ap = passwd_popup_get_ap(devpkr_app_state->passpopup);
184         int ret = wlan_manager_wps_connect(ap);
185         if (ret == WLAN_MANAGER_ERR_NONE) {
186                 create_pbc_popup(devpkr_app_state->passpopup,
187                                 __wps_pbc_popup_cancel_connecting, NULL,
188                                 POPUP_WPS_BTN, NULL);
189         } else {
190                 ERROR_LOG(SP_NAME_ERR, "wlan_manager_wps_connect failed");
191                 wifi_ap_destroy(ap);
192
193                 passwd_popup_free(devpkr_app_state->passpopup);
194                 devpkr_app_state->passpopup = NULL;
195         }
196
197         __COMMON_FUNC_EXIT__;
198 }
199
200 static void _wps_cancel_cb(void* data, Evas_Object* obj, void* event_info)
201 {
202         __COMMON_FUNC_ENTER__;
203
204         if (devpkr_app_state->passpopup == NULL) {
205                 return;
206         }
207
208         current_popup_free(devpkr_app_state->passpopup, POPUP_WPS_OPTIONS);
209
210         __COMMON_FUNC_EXIT__;
211 }
212
213 static void _wps_pin_cb(void* data, Evas_Object* obj, void* event_info)
214 {
215         __COMMON_FUNC_ENTER__;
216
217         unsigned int rpin = 0;
218         char npin[9] = { '\0' };
219         int pin_len = 0;
220         int ret = WLAN_MANAGER_ERR_NONE;
221         wifi_ap_h ap = NULL;
222
223         if (!devpkr_app_state->passpopup) {
224                 return;
225         }
226
227         /* Generate WPS pin */
228         rpin = wps_generate_pin();
229         if (rpin > 0)
230                 g_snprintf(npin, sizeof(npin), "%08d", rpin);
231
232         pin_len = strlen(npin);
233         if (pin_len != 8) {
234                 view_alerts_popup_show(sc(PACKAGE, I18N_TYPE_Invalid_pin));
235
236                 __COMMON_FUNC_EXIT__;
237                 return;
238         }
239
240         ap = passwd_popup_get_ap(devpkr_app_state->passpopup);
241
242         ret = wlan_manager_wps_pin_connect(ap, npin);
243         if (ret == WLAN_MANAGER_ERR_NONE) {
244                 INFO_LOG(UG_NAME_NORMAL, "wlan_manager_wps_pin_connect successful");
245
246                 create_pbc_popup(devpkr_app_state->passpopup,
247                                 __wps_pbc_popup_cancel_connecting, NULL,
248                                 POPUP_WPS_PIN, npin);
249         } else {
250                 ERROR_LOG(UG_NAME_NORMAL, "wlan_manager_wps_pin_connect failed");
251
252                 passwd_popup_free(devpkr_app_state->passpopup);
253                 devpkr_app_state->passpopup = NULL;
254         }
255
256         __COMMON_FUNC_EXIT__;
257 }
258
259 static void __popup_wps_options_cb(void* data, Evas_Object* obj, void* event_info)
260 {
261         __COMMON_FUNC_ENTER__;
262         pswd_popup_create_req_data_t popup_info;
263
264         if (!devpkr_app_state->passpopup) {
265                 return;
266         }
267
268         memset(&popup_info, 0, sizeof(pswd_popup_create_req_data_t));
269
270         popup_info.title = g_strdup(sc(PACKAGE, I18N_TYPE_Select_WPS_Method));
271         popup_info.ok_cb = NULL;
272         popup_info.cancel_cb = _wps_cancel_cb;
273         popup_info.show_wps_btn = EINA_FALSE;
274         popup_info.wps_btn_cb = _wps_btn_cb;
275         popup_info.wps_pin_cb = _wps_pin_cb;
276         popup_info.ap = passwd_popup_get_ap(devpkr_app_state->passpopup);
277         popup_info.cb_data = NULL;
278         create_wps_options_popup(devpkr_app_state->layout_main,
279                         devpkr_app_state->passpopup, &popup_info);
280
281         __COMMON_FUNC_EXIT__;
282 }
283
284 void view_main_eap_view_deref(void)
285 {
286         devpkr_app_state->eap_popup = NULL;
287 }
288
289 void view_main_wifi_reconnect(devpkr_gl_data_t *gdata)
290 {
291         wifi_device_info_t *device_info;
292         pswd_popup_create_req_data_t popup_info;
293         wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
294
295         retm_if(NULL == gdata);
296
297         device_info = gdata->dev_info;
298         retm_if(NULL == device_info);
299
300         if (devpkr_app_state->passpopup != NULL ||
301                         devpkr_app_state->eap_popup != NULL) {
302                 INFO_LOG(SP_NAME_ERR, "already launched popup");
303                 return;
304         }
305
306         wifi_ap_get_security_type(device_info->ap, &sec_type);
307
308         switch (sec_type) {
309         case WIFI_SECURITY_TYPE_WEP:
310         case WIFI_SECURITY_TYPE_WPA_PSK:
311         case WIFI_SECURITY_TYPE_WPA2_PSK:
312                 memset(&popup_info, 0, sizeof(pswd_popup_create_req_data_t));
313
314                 popup_info.title = gdata->dev_info->ssid;
315                 popup_info.ok_cb = __popup_ok_cb;
316                 popup_info.cancel_cb = __popup_cancel_cb;
317                 popup_info.show_wps_btn = gdata->dev_info->wps_mode;
318                 popup_info.wps_btn_cb = __popup_wps_options_cb;
319                 popup_info.ap = gdata->dev_info->ap;
320                 popup_info.cb_data = NULL;
321                 popup_info.sec_type = sec_type;
322
323                 devpkr_app_state->passpopup = create_passwd_popup(
324                                 devpkr_app_state->conformant,
325                                 devpkr_app_state->layout_main, PACKAGE,
326                                 &popup_info);
327                 if (devpkr_app_state->passpopup == NULL) {
328                         ERROR_LOG(SP_NAME_NORMAL, "Password popup creation failed");
329                 }
330                 break;
331
332         case WIFI_SECURITY_TYPE_EAP:
333                 devpkr_app_state->eap_popup = create_eap_view(
334                                 devpkr_app_state->layout_main,
335                                 devpkr_app_state->win_main,
336                                 devpkr_app_state->conformant,
337                                 PACKAGE, gdata->dev_info, view_main_eap_view_deref);
338                 break;
339
340         default:
341                 ERROR_LOG(SP_NAME_NORMAL, "Unknown security type [%d]", sec_type);
342                 break;
343         }
344 }
345
346 void view_main_wifi_connect(devpkr_gl_data_t *gdata)
347 {
348         bool favorite = false;
349         wifi_device_info_t *device_info;
350         pswd_popup_create_req_data_t popup_info;
351         wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
352
353         retm_if(NULL == gdata);
354
355         device_info = gdata->dev_info;
356         retm_if(NULL == device_info);
357
358         wifi_ap_is_favorite(device_info->ap, &favorite);
359
360         if (favorite == true) {
361                 wlan_manager_connect(device_info->ap);
362                 return;
363         }
364
365         if (devpkr_app_state->passpopup != NULL ||
366                         devpkr_app_state->eap_popup != NULL) {
367                 INFO_LOG(SP_NAME_ERR, "already launched popup");
368                 return;
369         }
370
371         wifi_ap_get_security_type(device_info->ap, &sec_type);
372
373         switch (sec_type) {
374         case WIFI_SECURITY_TYPE_NONE:
375                 wlan_manager_connect(device_info->ap);
376                 break;
377
378         case WIFI_SECURITY_TYPE_WEP:
379         case WIFI_SECURITY_TYPE_WPA_PSK:
380         case WIFI_SECURITY_TYPE_WPA2_PSK:
381                 memset(&popup_info, 0, sizeof(pswd_popup_create_req_data_t));
382
383                 popup_info.title = gdata->dev_info->ssid;
384                 popup_info.ok_cb = __popup_ok_cb;
385                 popup_info.cancel_cb = __popup_cancel_cb;
386                 popup_info.show_wps_btn = gdata->dev_info->wps_mode;
387                 popup_info.wps_btn_cb = __popup_wps_options_cb;
388                 popup_info.ap = gdata->dev_info->ap;
389                 popup_info.cb_data = NULL;
390                 popup_info.sec_type = sec_type;
391
392                 devpkr_app_state->passpopup = create_passwd_popup(
393                                 devpkr_app_state->conformant,
394                                 devpkr_app_state->layout_main, PACKAGE,
395                                 &popup_info);
396                 if (devpkr_app_state->passpopup == NULL) {
397                         ERROR_LOG(SP_NAME_NORMAL, "Password popup creation failed");
398                 }
399                 break;
400
401         case WIFI_SECURITY_TYPE_EAP:
402                 devpkr_app_state->eap_popup = create_eap_view(
403                                 devpkr_app_state->layout_main,
404                                 devpkr_app_state->win_main,
405                                 devpkr_app_state->conformant,
406                                 PACKAGE, gdata->dev_info, view_main_eap_view_deref);
407                 break;
408
409         default:
410                 ERROR_LOG(SP_NAME_NORMAL, "Unknown security type [%d]", sec_type);
411                 break;
412         }
413 }
414
415 Elm_Object_Item *view_main_item_get_for_ap(wifi_ap_h ap)
416 {
417         __COMMON_FUNC_ENTER__;
418         if (!ap || !list) {
419                 __COMMON_FUNC_EXIT__;
420                 return NULL;
421         }
422
423         char *essid = NULL;
424         wifi_security_type_e type = WIFI_SECURITY_TYPE_NONE;
425
426         if (WIFI_ERROR_NONE != wifi_ap_get_essid(ap, &essid)) {
427                 __COMMON_FUNC_EXIT__;
428                 return NULL;
429         } else if (WIFI_ERROR_NONE != wifi_ap_get_security_type(ap, &type)) {
430                 __COMMON_FUNC_EXIT__;
431                 return NULL;
432         }
433
434         Elm_Object_Item *it = elm_genlist_first_item_get(list);
435         wlan_security_mode_type_t sec_mode = common_utils_get_sec_mode(type);
436         while (it) {
437                 devpkr_gl_data_t* gdata = elm_object_item_data_get(it);
438                 wifi_device_info_t *device_info = NULL;
439                 if (gdata && (device_info = gdata->dev_info)) {
440                         if (!g_strcmp0(device_info->ssid, essid) && device_info->security_mode == sec_mode) {
441                                 break;
442                         }
443                 }
444
445                 it = elm_genlist_item_next_get(it);
446         }
447
448         g_free(essid);
449         __COMMON_FUNC_EXIT__;
450         return it;
451 }
452
453 #if 0
454 /* Unused function */
455 Elm_Object_Item *__view_main_get_item_in_mode(ITEM_CONNECTION_MODES mode)
456 {
457         Elm_Object_Item* it = NULL;
458         it = elm_genlist_first_item_get(list);
459         __COMMON_FUNC_ENTER__;
460         while (it) {
461                 devpkr_gl_data_t *gdata = (devpkr_gl_data_t *)elm_object_item_data_get(it);
462                 if (gdata && gdata->connection_mode == mode) {
463                         SECURE_INFO_LOG( SP_NAME_NORMAL, "Found Item [%s] in mode[%d]", gdata->dev_info->ssid, mode);
464                         __COMMON_FUNC_EXIT__;
465                         return it;
466                 }
467                 it = elm_genlist_item_next_get(it);
468         }
469
470         __COMMON_FUNC_EXIT__;
471         return NULL;
472 }
473 #endif
474
475 void view_main_clear_disconnect_popup(wifi_ap_h ap)
476 {
477         if (!g_disconnect_popup)
478                 return;
479
480         if (ap && g_disconnect_popup->ap &&
481                         wlan_manager_is_same_network(g_disconnect_popup->ap, ap) != TRUE)
482                 return;
483
484         if (g_disconnect_popup->popup)
485                 evas_object_del(g_disconnect_popup->popup);
486
487         if (g_disconnect_popup->ap)
488                 wifi_ap_destroy(g_disconnect_popup->ap);
489
490         g_free(g_disconnect_popup);
491         g_disconnect_popup = NULL;
492 }
493
494 static void __view_main_disconnect_cancel_cb(void *data,
495                 Evas_Object *obj, void *event_info)
496 {
497         struct connecting_cancel_popup_data *popup =
498                         (struct connecting_cancel_popup_data *)data;
499         if (!popup)
500                 return;
501
502         if (popup->popup)
503                 evas_object_del(popup->popup);
504
505         if (popup->ap)
506                 wifi_ap_destroy(popup->ap);
507
508         g_free(popup);
509         g_disconnect_popup = NULL;
510 }
511
512 static gboolean __view_main_update_ap(gpointer data)
513 {
514         wifi_ap_h ap = (wifi_ap_h)data;
515
516         wifi_ap_destroy(ap);
517
518         return FALSE;
519 }
520
521 static void __view_main_disconnect_ok_cb(void *data,
522                 Evas_Object *obj, void *event_info)
523 {
524         guint id;
525         struct connecting_cancel_popup_data *popup =
526                         (struct connecting_cancel_popup_data *)data;
527         if (!popup)
528                 return;
529
530         wlan_manager_disconnect(popup->ap);
531         wlan_manager_forget(popup->ap);
532
533         if (popup->popup)
534                 evas_object_del(popup->popup);
535
536         id = common_util_managed_idle_add(__view_main_update_ap, (gpointer)popup->ap);
537         if (!id) {
538                 wifi_ap_destroy(popup->ap);
539         }
540
541         g_free(popup);
542         g_disconnect_popup = NULL;
543 }
544
545 static void __view_main_disconnect_popup(wifi_device_info_t *device_info,
546                 Evas_Object *win_main)
547 {
548         popup_btn_info_t popup_data;
549         struct connecting_cancel_popup_data *popup = NULL;
550
551         memset(&popup_data, 0, sizeof(popup_data));
552
553         popup = g_try_new0(struct connecting_cancel_popup_data, 1);
554         if (!popup)
555                 return;
556
557         g_disconnect_popup = popup;
558         wifi_ap_clone(&popup->ap, device_info->ap);
559
560         popup_data.title_txt = "IDS_WIFI_OPT_FORGET_NETWORK";
561         popup_data.info_txt = "IDS_WIFI_POP_CURRENT_NETWORK_WILL_BE_DISCONNECTED";
562         popup_data.btn1_cb = __view_main_disconnect_cancel_cb;
563         popup_data.btn1_txt = "IDS_WIFI_SK_CANCEL";
564         popup_data.btn1_data = popup;
565         popup_data.btn2_cb = __view_main_disconnect_ok_cb;
566         popup_data.btn2_txt = "IDS_WIFI_SK_FORGET";
567         popup_data.btn2_data = popup;
568
569         popup->popup = common_utils_show_info_popup(win_main, &popup_data);
570 }
571
572 static void __gl_sel(void *data, Evas_Object *obj, void *event_info)
573 {
574         __COMMON_FUNC_ENTER__;
575
576         assertm_if(NULL == obj, "obj is NULL!!");
577         assertm_if(NULL == event_info, "event_info is NULL!!");
578
579         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
580         devpkr_gl_data_t *gdata = (devpkr_gl_data_t *)elm_object_item_data_get(item);
581         if (!gdata || !gdata->dev_info) {
582                 __COMMON_FUNC_EXIT__;
583                 return;
584         }
585
586         switch (gdata->connection_mode) {
587         case ITEM_CONNECTION_MODE_OFF:
588                 view_main_wifi_connect(gdata);
589                 break;
590
591         case ITEM_CONNECTION_MODE_CONNECTING:
592         case ITEM_CONNECTION_MODE_CONFIGURATION:
593                 __view_main_disconnect_popup(gdata->dev_info, devpkr_app_state->layout_main);
594                 break;
595
596         default:
597                 break;
598         }
599
600         elm_genlist_item_selected_set(item, EINA_FALSE);
601
602         __COMMON_FUNC_EXIT__;
603 }
604
605 static char *_gl_text_get(void *data, Evas_Object *obj, const char *part)
606 {
607         char *ret = NULL;
608         char *txt = NULL;
609
610         assertm_if(NULL == data, "data param is NULL!!");
611         assertm_if(NULL == obj, "obj param is NULL!!");
612         assertm_if(NULL == part, "part param is NULL!!");
613
614         devpkr_gl_data_t *gdata = (devpkr_gl_data_t *) data;
615         retvm_if(NULL == gdata, NULL);
616
617         if (!strcmp("elm.text", part)) {
618                 txt = evas_textblock_text_utf8_to_markup(NULL, gdata->dev_info->ssid);
619                 ret = g_strdup(txt);
620                 if (ret == NULL) {
621                         ERROR_LOG(SP_NAME_NORMAL, "ssid name is NULL!!");
622                 }
623                 g_free(txt);
624         } else if (!strcmp("elm.text.sub", part)) {
625                 if (ITEM_CONNECTION_MODE_CONNECTING == gdata->connection_mode) {
626                         ret = g_strdup(sc(PACKAGE, I18N_TYPE_Connecting));
627                 } else if (ITEM_CONNECTION_MODE_CONFIGURATION == gdata->connection_mode) {
628                         ret = g_strdup(sc(PACKAGE, I18N_TYPE_Obtaining_IP_addr));
629                 } else {
630                         ret = g_strdup(gdata->dev_info->ap_status_txt);
631                 }
632
633                 if (ret == NULL) {
634                         ERROR_LOG(SP_NAME_NORMAL, "ap_status_txt is NULL!!");
635                 }
636         }
637
638         return ret;
639 }
640
641 static Evas_Object *_gl_content_get(void *data, Evas_Object *obj, const char *part)
642 {
643         if (data == NULL) {
644                 return NULL;
645         }
646
647         devpkr_gl_data_t *gdata = (devpkr_gl_data_t *) data;
648
649         Evas_Object* icon = NULL;
650         Evas_Object *ic = NULL;
651
652         if (!strcmp("elm.swallow.icon", part)) {
653                 char *temp_str = NULL;
654                 ic = elm_layout_add(obj);
655
656                 icon = elm_image_add(ic);
657                 retvm_if(NULL == icon, NULL);
658
659                 elm_layout_theme_set(ic, "layout", "list/B/type.3", "default");
660
661                 temp_str = g_strdup_printf("%s.png", gdata->dev_info->ap_image_path);
662                 elm_image_file_set(icon, CUSTOM_EDITFIELD_PATH, temp_str);
663                 g_free(temp_str);
664
665                 evas_object_color_set(icon, 2, 61, 132, 204);
666
667                 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
668                 evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
669                 elm_layout_content_set(ic, "elm.swallow.content", icon);
670         } else if (!strcmp("elm.swallow.end", part)) {
671                 if (gdata->connection_mode == ITEM_CONNECTION_MODE_CONNECTING ||
672                                 gdata->connection_mode == ITEM_CONNECTION_MODE_CONFIGURATION) {
673                         ic = elm_layout_add(obj);
674                         elm_layout_theme_set(ic, "layout", "list/C/type.2", "default");
675
676                         icon = elm_progressbar_add(obj);
677                         elm_object_style_set(icon, "process_medium");
678                         evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, 0.5);
679                         evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
680                         elm_progressbar_pulse(icon, TRUE);
681
682                         elm_layout_content_set(ic, "elm.swallow.content", icon);
683                 }
684         }
685
686         return ic;
687 }
688
689 static void _gl_list_del(void* data, Evas_Object* obj)
690 {
691         if (data == NULL) {
692                 return;
693         }
694
695         devpkr_gl_data_t* gdata = (devpkr_gl_data_t *) data;
696
697         if (gdata->dev_info) {
698                 SECURE_DEBUG_LOG(UG_NAME_NORMAL, "del target ssid: [%s]", gdata->dev_info->ssid);
699                 g_free(gdata->dev_info->ap_image_path);
700                 g_free(gdata->dev_info->ap_status_txt);
701                 g_free(gdata->dev_info->ssid);
702                 wifi_ap_destroy(gdata->dev_info->ap);
703                 g_free(gdata->dev_info);
704         }
705
706         g_free(gdata);
707
708         return;
709 }
710
711 static Evas_Object *_create_genlist(Evas_Object* parent)
712 {
713         __COMMON_FUNC_ENTER__;
714         assertm_if(NULL == parent, "parent is NULL!!");
715
716         list = elm_genlist_add(parent);
717         assertm_if(NULL == list, "list allocation fail!!");
718         elm_genlist_mode_set(list, ELM_LIST_COMPRESS);
719         elm_genlist_homogeneous_set(list, EINA_TRUE);
720
721         evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
722         evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
723
724         itc.item_style = WIFI_GENLIST_2LINE_TOP_TEXT_ICON_STYLE;
725         itc.func.text_get = _gl_text_get;
726         itc.func.content_get = _gl_content_get;
727         itc.func.state_get = NULL;
728         itc.func.del = _gl_list_del;
729
730         __COMMON_FUNC_EXIT__;
731
732         return list;
733 }
734
735 static void view_main_scan_ui_clear(void)
736 {
737         __COMMON_FUNC_ENTER__;
738
739         if (list == NULL) {
740                 return;
741         }
742         elm_genlist_clear(list);
743
744         __COMMON_FUNC_EXIT__;
745 }
746
747 void view_main_item_state_set(wifi_ap_h ap, ITEM_CONNECTION_MODES state)
748 {
749         __COMMON_FUNC_ENTER__;
750
751         char *item_ssid = NULL;
752         wifi_security_type_e sec_type;
753         wlan_security_mode_type_t item_sec_mode;
754         Elm_Object_Item* it = NULL;
755
756         it = elm_genlist_first_item_get(list);
757         if (!it ||
758                 !ap ||
759                 (WIFI_ERROR_NONE != wifi_ap_get_essid(ap, &item_ssid)) ||
760                 (WIFI_ERROR_NONE != wifi_ap_get_security_type(ap, &sec_type))) {
761                 ERROR_LOG(SP_NAME_NORMAL, "Invalid params");
762                 __COMMON_FUNC_EXIT__;
763                 return;
764         }
765         item_sec_mode = common_utils_get_sec_mode(sec_type);
766         SECURE_INFO_LOG(SP_NAME_NORMAL, "item state set for AP[%s] with sec mode[%d]", item_ssid, item_sec_mode);
767         while (it) {
768                 devpkr_gl_data_t *gdata = (devpkr_gl_data_t *)elm_object_item_data_get(it);
769                 if (gdata != NULL) {
770                         SECURE_INFO_LOG(SP_NAME_NORMAL, "gdata AP[%s] with sec mode[%d]",
771                                         gdata->dev_info->ssid, gdata->dev_info->security_mode);
772                 }
773
774                 if (gdata && gdata->dev_info->security_mode == item_sec_mode &&
775                         !g_strcmp0(gdata->dev_info->ssid, item_ssid)) {
776                         if (gdata->connection_mode != state) {
777                                 gdata->connection_mode = state;
778                                 INFO_LOG(SP_NAME_NORMAL, "State transition from [%d] --> [%d]", view_main_state_get(), state);
779                                 view_main_state_set(state);
780                                 if(it != NULL)
781                                         elm_genlist_item_update(it);
782                         }
783                         break;
784                 }
785
786                 it = elm_genlist_item_next_get(it);
787         }
788         g_free(item_ssid);
789         __COMMON_FUNC_EXIT__;
790         return;
791 }
792
793 static wifi_device_info_t *view_main_item_device_info_create(wifi_ap_h ap)
794 {
795         wifi_device_info_t *wifi_device = g_try_new0(wifi_device_info_t, 1);
796         wifi_security_type_e sec_type;
797
798         if (WIFI_ERROR_NONE != wifi_ap_clone(&(wifi_device->ap), ap)) {
799                 g_free(wifi_device);
800                 return NULL;
801         } else if (WIFI_ERROR_NONE != wifi_ap_get_essid(ap, &(wifi_device->ssid))) {
802                 g_free(wifi_device);
803                 return NULL;
804         } else if (WIFI_ERROR_NONE != wifi_ap_get_rssi(ap, &(wifi_device->rssi))) {
805                 g_free(wifi_device->ssid);
806                 g_free(wifi_device);
807                 return NULL;
808         } else if (WIFI_ERROR_NONE != wifi_ap_get_security_type(ap, &sec_type)) {
809                 g_free(wifi_device->ssid);
810                 g_free(wifi_device);
811                 return NULL;
812         } else if (WIFI_ERROR_NONE != wifi_ap_is_wps_supported(ap, &(wifi_device->wps_mode))) {
813                 g_free(wifi_device->ssid);
814                 g_free(wifi_device);
815                 return NULL;
816         }
817
818         wifi_device->security_mode = common_utils_get_sec_mode(sec_type);
819         wifi_device->ap_status_txt = common_utils_get_ap_security_type_info_txt(PACKAGE,
820                 wifi_device, true);
821         common_utils_get_device_icon(wifi_device, &wifi_device->ap_image_path);
822
823         return wifi_device;
824 }
825
826 static bool view_main_wifi_insert_found_ap(wifi_device_info_t *wifi_device)
827 {
828         devpkr_gl_data_t *gdata = g_try_new0(devpkr_gl_data_t, 1);
829         wifi_connection_state_e state;
830
831         assertm_if(NULL == list, "list is NULL");
832
833         if (gdata == NULL)
834                 return false;
835
836         gdata->dev_info = wifi_device;
837         if (gdata->dev_info == NULL) {
838                 g_free(gdata);
839                 return true;
840         }
841
842         wifi_ap_get_connection_state(wifi_device->ap, &state);
843
844         if (WIFI_CONNECTION_STATE_ASSOCIATION == state ||
845                         WIFI_CONNECTION_STATE_CONFIGURATION == state) {
846                 gdata->connection_mode = ITEM_CONNECTION_MODE_CONNECTING;
847                 gdata->it = elm_genlist_item_append(list, &itc, gdata,
848                                 NULL, ELM_GENLIST_ITEM_NONE, __gl_sel,
849                                 NULL);
850                 view_main_state_set(ITEM_CONNECTION_MODE_CONNECTING);
851
852                 return true;
853         }
854
855         gdata->connection_mode = ITEM_CONNECTION_MODE_OFF;
856
857         gdata->it = elm_genlist_item_append(list, &itc, gdata, NULL,
858                         ELM_GENLIST_ITEM_NONE, __gl_sel, NULL);
859
860         return true;
861 }
862
863 static gint compare(gconstpointer a, gconstpointer b)
864 {
865         bool favorite1 = false, favorite2 = false;
866         wifi_connection_state_e state1 = 0, state2 = 0;
867
868         wifi_device_info_t *wifi_device1 = (wifi_device_info_t*)a;
869         wifi_device_info_t *wifi_device2 = (wifi_device_info_t*)b;
870
871         wifi_ap_get_connection_state(wifi_device1->ap, &state1);
872         wifi_ap_get_connection_state(wifi_device2->ap, &state2);
873
874         if (state1 != state2) {
875                 if (state1 == WIFI_CONNECTION_STATE_CONNECTED)
876                         return -1;
877                 if (state2 == WIFI_CONNECTION_STATE_CONNECTED)
878                         return 1;
879
880                 if (state1 == WIFI_CONNECTION_STATE_CONFIGURATION)
881                         return -1;
882                 if (state2 == WIFI_CONNECTION_STATE_CONFIGURATION)
883                         return 1;
884
885                 if (state1 == WIFI_CONNECTION_STATE_ASSOCIATION)
886                         return -1;
887                 if (state2 == WIFI_CONNECTION_STATE_ASSOCIATION)
888                         return 1;
889         }
890
891         wifi_ap_is_favorite(wifi_device1->ap, &favorite1);
892         wifi_ap_is_favorite(wifi_device2->ap, &favorite2);
893
894         if (favorite1 != favorite2) {
895                 if (favorite1 == true)
896                         return -1;
897                 if (favorite2 == true)
898                         return 1;
899         }
900         if (devpkr_app_state->sort_type == I18N_TYPE_Signal_Strength) {
901                 /*Sort in descending order of signal strength*/
902                 return wifi_device2->rssi - wifi_device1->rssi;
903         }
904         return strcasecmp((const char *)wifi_device1->ssid,
905                         (const char *)wifi_device2->ssid);
906 }
907
908 static bool view_main_wifi_found_ap_cb(wifi_ap_h ap, void* user_data)
909 {
910         int *profile_size = (int *)user_data;
911         wifi_device_info_t *wifi_device = NULL;
912
913         wifi_device = view_main_item_device_info_create(ap);
914         if (wifi_device == NULL)
915                 return true;
916
917         wifi_device_list = g_list_insert_sorted(wifi_device_list, wifi_device, compare);
918         (*profile_size)++;
919
920         return true;
921 }
922
923 static Evas_Object *_gl_content_title_get(void *data, Evas_Object *obj, const char *part)
924 {
925         Evas_Object *title_progressbar = NULL;
926
927         if (FALSE == wifi_devpkr_get_scan_status())
928                 return NULL;
929
930         title_progressbar = elm_progressbar_add(obj);
931         elm_object_style_set(title_progressbar, "process_small");
932         elm_progressbar_horizontal_set(title_progressbar, EINA_TRUE);
933         elm_progressbar_pulse(title_progressbar, EINA_TRUE);
934
935         return title_progressbar;
936 }
937
938 static char* _gl_text_title_get(void *data, Evas_Object *obj,const char *part)
939 {
940         if (!strcmp("elm.text", part)) {
941                 return (char*) g_strdup(sc(PACKAGE, I18N_TYPE_Available_networks));
942         }
943
944         return NULL;
945 }
946
947 static void view_main_add_group_title(void)
948 {
949         grouptitle_itc.item_style = WIFI_GENLIST_GROUP_INDEX_STYLE;
950         grouptitle_itc.func.text_get = _gl_text_title_get;
951         grouptitle_itc.func.content_get = _gl_content_title_get;
952
953         grouptitle = elm_genlist_item_append(list,
954                         &grouptitle_itc,
955                         NULL,
956                         NULL,
957                         ELM_GENLIST_ITEM_NONE,
958                         NULL,
959                         NULL);
960         assertm_if(NULL == grouptitle, "NULL!!");
961
962         elm_genlist_item_select_mode_set(grouptitle,
963                         ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
964 }
965
966 void view_main_update_group_title(gboolean is_bg_scan)
967 {
968         Evas_Object *box = NULL;
969         Evas_Object *main_list = NULL;
970
971         if (list != NULL) {
972                 if (!is_bg_scan) {
973                         Elm_Object_Item *it = elm_genlist_first_item_get(list);
974
975                         while (it) {
976                                 elm_object_item_disabled_set(it, EINA_TRUE);
977                                 it = elm_genlist_item_next_get(it);
978                         }
979                 }
980
981                 if(grouptitle != NULL)
982                         elm_genlist_item_update(grouptitle);
983         } else {
984                 box = elm_object_content_get(devpkr_app_state->popup);
985
986                 main_list = _create_genlist(box);
987                 view_main_add_group_title();
988
989                 elm_box_pack_start(box, main_list);
990
991                 evas_object_show(main_list);
992                 evas_object_show(box);
993
994                 wifi_devpkr_redraw();
995
996                 evas_object_show(devpkr_app_state->popup);
997         }
998
999         return;
1000 }
1001
1002 static void view_main_create_empty_layout(void)
1003 {
1004         __COMMON_FUNC_ENTER__;
1005
1006         Evas_Object *box = NULL;
1007         Evas_Object *layout = NULL;
1008         Evas_Object *prev_box = NULL;
1009
1010         prev_box = elm_object_content_get(devpkr_app_state->popup);
1011         if (prev_box != NULL) {
1012                 evas_object_del(prev_box);
1013                 list = NULL;
1014                 grouptitle = NULL;
1015         }
1016
1017         box = elm_box_add(devpkr_app_state->popup);
1018         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1019         evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
1020
1021         layout = elm_layout_add(devpkr_app_state->popup);
1022         elm_layout_file_set(layout, WIFI_DEVPKR_EDJ, WIFI_SYSPOPUP_EMPTY_GRP);
1023         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1024         evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
1025
1026         elm_object_domain_translatable_part_text_set(layout, "text", PACKAGE,
1027                 sc(PACKAGE, I18N_TYPE_No_Wi_Fi_AP_Found));
1028
1029         elm_box_pack_end(box, layout);
1030         evas_object_show(layout);
1031         evas_object_show(box);
1032         elm_object_content_set(devpkr_app_state->popup, box);
1033
1034         __COMMON_FUNC_EXIT__;
1035 }
1036
1037 void view_main_create_main_list(void)
1038 {
1039         __COMMON_FUNC_ENTER__;
1040
1041         Evas_Object *box = NULL;
1042         Evas_Object *main_list = NULL;
1043         Evas_Object *prev_box = NULL;
1044
1045         prev_box = elm_object_content_get(devpkr_app_state->popup);
1046         if (prev_box != NULL) {
1047                 evas_object_del(prev_box);
1048                 list = NULL;
1049                 grouptitle = NULL;
1050         }
1051
1052         box = elm_box_add(devpkr_app_state->popup);
1053         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1054         evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
1055
1056         main_list = _create_genlist(box);
1057         view_main_add_group_title();
1058
1059         elm_box_pack_end(box, main_list);
1060         evas_object_show(main_list);
1061         evas_object_show(box);
1062         elm_object_content_set(devpkr_app_state->popup, box);
1063
1064         __COMMON_FUNC_EXIT__;
1065 }
1066
1067 void view_main_refresh_ap_info(Elm_Object_Item *item)
1068 {
1069         if (!item) {
1070                 return;
1071         }
1072
1073         devpkr_gl_data_t *gdata = elm_object_item_data_get(item);
1074         if (!gdata) {
1075                 return;
1076         }
1077         wifi_device_info_t *wifi_device = gdata->dev_info;
1078         if (!wifi_device) {
1079                 return;
1080         }
1081
1082         wifi_ap_refresh(wifi_device->ap);
1083 }
1084
1085 static gboolean __view_main_scroll_to_top(void *data)
1086 {
1087         if (data)
1088                 elm_genlist_item_bring_in((Elm_Object_Item *)data,
1089                                 ELM_GENLIST_ITEM_SCROLLTO_TOP);
1090
1091         return FALSE;
1092 }
1093
1094 Elm_Object_Item *view_main_move_item_to_top(Elm_Object_Item *old_item)
1095 {
1096         __COMMON_FUNC_ENTER__;
1097
1098         Elm_Object_Item *title_item = elm_genlist_first_item_get(list);
1099         Elm_Object_Item *first_item = elm_genlist_item_next_get(title_item);
1100         devpkr_gl_data_t *old_it_gdata = NULL, *first_it_gdata = NULL;
1101
1102         if (!old_item || !first_item) {
1103                 __COMMON_FUNC_EXIT__;
1104                 return NULL;
1105         }
1106
1107         old_it_gdata = elm_object_item_data_get(old_item);
1108         if (!old_it_gdata || !old_it_gdata->dev_info) {
1109                 __COMMON_FUNC_EXIT__;
1110                 return NULL;
1111         }
1112
1113         if (old_item != first_item) {
1114                 first_it_gdata = elm_object_item_data_get(first_item);
1115                 elm_object_item_data_set(first_item, old_it_gdata);
1116                 elm_object_item_data_set(old_item, first_it_gdata);
1117
1118                 elm_genlist_item_update(first_item);
1119                 elm_genlist_item_update(old_item);
1120         }
1121
1122         common_util_managed_idle_add(__view_main_scroll_to_top, title_item);
1123
1124         __COMMON_FUNC_EXIT__;
1125         return first_item;
1126 }
1127
1128 gboolean view_main_show(void *data)
1129 {
1130         __COMMON_FUNC_ENTER__;
1131
1132         int i;
1133         wifi_device_info_t *wifi_device = NULL;
1134         GList* list_of_device = NULL;
1135
1136         int state = wlan_manager_state_get();
1137         if (WLAN_MANAGER_ERROR == state || WLAN_MANAGER_OFF == state) {
1138                 INFO_LOG(SP_NAME_NORMAL, "Wi-Fi state is OFF");
1139                 view_main_create_empty_layout();
1140                 goto exit;
1141         } else if (WLAN_MANAGER_CONNECTED == state) {
1142                 __COMMON_FUNC_EXIT__;
1143                 return FALSE;
1144         }
1145
1146         wifi_devpkr_enable_scan_btn();
1147
1148         /* If previous profile list exists then just clear the genlist */
1149         if (profiles_list_size) {
1150                 view_main_scan_ui_clear();
1151                 view_main_add_group_title();
1152         } else {
1153                 view_main_create_main_list();
1154         }
1155
1156         view_main_state_set(ITEM_CONNECTION_MODE_OFF);
1157
1158         profiles_list_size = 0;
1159
1160         devpkr_app_state->sort_type = I18N_TYPE_Alphabetical;
1161         if (common_util_get_system_registry(VCONF_SORT_BY) == 1)
1162                 devpkr_app_state->sort_type = I18N_TYPE_Signal_Strength;
1163
1164         wifi_foreach_found_aps(view_main_wifi_found_ap_cb, &profiles_list_size);
1165         INFO_LOG(SP_NAME_NORMAL, "profiles list count [%d]\n", profiles_list_size);
1166
1167         list_of_device = wifi_device_list;
1168         for (i = 0; i < profiles_list_size && list_of_device != NULL; i++) {
1169                 wifi_device = (wifi_device_info_t*)list_of_device->data;
1170
1171                 view_main_wifi_insert_found_ap(wifi_device);
1172
1173                 list_of_device = list_of_device->next;
1174         }
1175
1176         if (wifi_device_list != NULL) {
1177                 g_list_free(wifi_device_list);
1178                 wifi_device_list = NULL;
1179         }
1180
1181         if (profiles_list_size <= 0)
1182                 view_main_create_empty_layout();
1183         else
1184                 evas_object_show(list);
1185
1186 exit:
1187         wifi_devpkr_redraw();
1188
1189         if (devpkr_app_state->passpopup == NULL &&
1190                         devpkr_app_state->eap_popup == NULL)
1191                 evas_object_show(devpkr_app_state->popup);
1192         evas_object_show(devpkr_app_state->win_main);
1193
1194         __COMMON_FUNC_EXIT__;
1195         return FALSE;
1196 }