tizen 2.3.1 release
[apps/home/ug-wifi-direct.git] / ug-wifidirect / src / wfd_ug.c
1 /*
2 *  WiFi-Direct UG
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
21 #ifndef UG_MODULE_API
22 #define UG_MODULE_API __attribute__ ((visibility("default")))
23 #endif
24
25
26 #include <sys/time.h>
27 #include <libintl.h>
28 #include <sys/utsname.h>
29
30 #include <vconf.h>
31 #include <Elementary.h>
32 #include <ui-gadget-module.h>
33 #include <app_control.h>
34
35 #include <wifi-direct.h>
36 #include "wfd_ug.h"
37 #include "wfd_ug_view.h"
38 #include "wfd_client.h"
39 #ifdef MOTION_CONTROL_ENABLE
40 #include "wfd_motion_control.h"
41 #endif
42
43 void initialize_gen_item_class();
44
45 struct ug_data *global_ugd = NULL;
46
47 struct ug_data *wfd_get_ug_data()
48 {
49         return global_ugd;
50 }
51
52 static void __wfd_main_vconf_change_cb(keynode_t *key, void *data)
53 {
54         __FUNC_ENTER__;
55         WFD_RET_IF(NULL == key, "ERROR : key is NULL !!\n");
56         WFD_RET_IF(NULL == data, "ERROR : data is NULL");
57         struct ug_data *ugd = (struct ug_data *) data;
58
59         char *vconf_name = vconf_keynode_get_name(key);
60
61         if (!g_strcmp0(vconf_name, VCONFKEY_SETAPPL_DEVICE_NAME_STR)){
62                 char *name_value = NULL;
63                 name_value = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
64                 WFD_RET_IF (!name_value, "Get string is failed");
65                 DBG(LOG_INFO,"name : %s", name_value);
66
67                 if (ugd->device_name_item && g_strcmp0(ugd->dev_name, name_value))
68                         wfd_ug_view_refresh_glitem(ugd->device_name_item);
69
70                 free(name_value);
71         } else {
72                 DBG(LOG_ERROR, "vconf_name is error");
73         }
74         __FUNC_EXIT__;
75 }
76
77 #ifdef WIFI_STATE_CB
78 static void _wifi_on_state_cb(keynode_t *key, void *data)
79 {
80         WFD_RET_IF(NULL == key, "ERROR : key is NULL !!\n");
81         WFD_RET_IF(NULL == data, "ERROR : data is NULL");
82
83         struct ug_data *ugd = (struct ug_data *)data;
84         int wifi_state = 0;
85
86         if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
87                 DBG(LOG_ERROR, "Failed to get vconf VCONFKEY_WIFI_STATE\n");
88                 return;
89         }
90
91         DBG(LOG_INFO, "WiFi State [%d]\n", wifi_state);
92         if (wifi_state > VCONFKEY_WIFI_OFF && wifi_state < VCONFKEY_WIFI_STATE_MAX) {
93                 if (WIFI_DIRECT_ERROR_NONE != wifi_direct_get_state(&ugd->wfd_status)) {
94                         DBG(LOG_ERROR, "Failed to Get WiFi Direct State");
95                         return;
96                 }
97                 if (ugd->wfd_status <= WIFI_DIRECT_STATE_DEACTIVATING) {
98                         DBG(LOG_INFO, "Activate WiFi Direct...");
99                         if (FALSE != wfd_client_switch_on(ugd)) {
100                                 DBG(LOG_ERROR, "Failed to Activate WiFi Direct");
101                         }
102                 }
103         }
104 }
105 #endif
106
107
108 static void __wfd_hotspot_mode_vconf_change_cb(keynode_t *key, void *data)
109 {
110         DBG(LOG_INFO, "__wfd_hotspot_mode_vconf_change_cb");
111         if (NULL == key || NULL == data) {
112                 DBG(LOG_INFO, "Invalid parameters \n");
113                 return;
114         }
115         struct ug_data *ugd = (struct ug_data *) data;
116         int hotspot_mode = 0;
117         int res = 0;
118
119         res = vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &hotspot_mode);
120         if (res) {
121                 WDS_LOGE("Failed to get vconf value for PLMN(%d)", res);
122                 return;
123         }
124         DBG(LOG_INFO, "__wfd_hotspot_mode_vconf_change_cb mode %d", hotspot_mode);
125
126         if (VCONFKEY_MOBILE_HOTSPOT_MODE_NONE == hotspot_mode) {
127                 if (NULL != ugd->act_popup) {
128                         evas_object_del(ugd->act_popup);
129                 }
130 #ifdef WFD_ON_OFF_GENLIST
131                 wfd_ug_refresh_on_off_check(ugd);
132 #endif
133         } else if (hotspot_mode == VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI ||
134                                 hotspot_mode == VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP) {
135                 if (NULL != ugd->warn_popup) {
136                         evas_object_del(ugd->warn_popup);
137                         ugd->warn_popup = NULL;
138                 }
139         }
140
141 }
142
143 Ea_Theme_Color_Table *_color_table_set(void)
144 {
145         Ea_Theme_Color_Table *table;
146
147         table = ea_theme_color_table_new(COLOR_TABLE);
148         DBG(LOG_INFO, "Wi-Fi direct color table : %p", table);
149         ea_theme_colors_set(table, EA_THEME_STYLE_DEFAULT);
150
151         return table;
152 }
153
154 Ea_Theme_Font_Table *_font_table_set(void)
155 {
156         Ea_Theme_Font_Table *table;
157
158         table = ea_theme_color_table_new(FONT_TABLE);
159         DBG(LOG_INFO, "Wi-Fi direct font table : %p", table);
160         ea_theme_fonts_set(table);
161
162         return table;
163 }
164
165 /**
166  *      This function let the ug create backgroud
167  *      @return  backgroud
168  *      @param[in] ugd the pointer to the parent object
169  *      @param[in] ugd the pointer to the main data structure
170  */
171 static Evas_Object *_create_bg(Evas_Object *parent, char *style)
172 {
173         __FUNC_ENTER__;
174         Evas_Object *bg;
175
176         bg = elm_bg_add(parent);
177         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
178         elm_object_style_set(bg, style);
179         evas_object_show(bg);
180
181         __FUNC_EXIT__;
182         return bg;
183 }
184
185 /**
186  *      This function let the ug create full view
187  *      @return  full view
188  *      @param[in] ugd the pointer to the parent object
189  *      @param[in] ugd the pointer to the main data structure
190  */
191 static Evas_Object *_create_fullview(Evas_Object *parent, struct ug_data *ugd)
192 {
193         __FUNC_ENTER__;
194         Evas_Object *base;
195
196         if (parent == NULL) {
197                 DBG(LOG_ERROR, "Incorrenct parameter");
198                 return NULL;
199         }
200
201         /* Create Full view */
202         base = elm_layout_add(parent);
203         if (!base) {
204                 DBG(LOG_ERROR, "Failed to add layout");
205                 return NULL;
206         }
207
208         elm_layout_theme_set(base, "layout", "application", "default");
209         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
210         evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);
211
212         __FUNC_EXIT__;
213         return base;
214 }
215
216 /**
217  *      This function let the ug create frame view
218  *      @return  frame view
219  *      @param[in] ugd the pointer to the parent object
220  *      @param[in] ugd the pointer to the main data structure
221  */
222 static Evas_Object *_create_frameview(Evas_Object *parent, struct ug_data *ugd)
223 {
224         __FUNC_ENTER__;
225         Evas_Object *base;
226
227         if (parent == NULL) {
228                 DBG(LOG_ERROR, "Incorrenct parameter");
229                 return NULL;
230         }
231
232         /* Create Frame view */
233         base = elm_layout_add(parent);
234         if (!base) {
235                 DBG(LOG_ERROR, "Failed to add layout");
236                 return NULL;
237         }
238
239         elm_layout_theme_set(base, "layout", "application", "default");
240         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
241         evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);
242
243         __FUNC_EXIT__;
244         return base;
245 }
246
247 /**
248  *      This function let the ug destroy the ug
249  *      @return   void
250  *      @param[in] data the pointer to the main data structure
251  */
252 void wfd_destroy_ug(void *data)
253 {
254         __FUNC_ENTER__;
255         struct ug_data *ugd = (struct ug_data *) data;
256
257 #ifdef WFD_DBUS_LAUNCH
258         if (ugd->dbus_cancellable != NULL) {
259                 g_cancellable_cancel(ugd->dbus_cancellable);
260                 g_object_unref(ugd->dbus_cancellable);
261                 ugd->dbus_cancellable = NULL;
262                 if (ugd->conn) {
263                         g_object_unref(ugd->conn);
264                         ugd->conn = NULL;
265                 }
266                 DBG(LOG_INFO, "Cancelled dbus call");
267                 return;
268         } else
269 #endif
270         {
271                 DBG(LOG_INFO, "dbus_cancellable is NULL");
272                 ug_destroy_me(ugd->ug);
273         }
274
275         __FUNC_EXIT__;
276         return;
277 }
278
279 static void wfd_ug_layout_del_cb(void *data , Evas *e, Evas_Object *obj, void *event_info)
280 {
281         __FUNC_ENTER__;
282
283         struct ug_data *ugd = (struct ug_data *) data;
284         if (ugd == NULL) {
285                 DBG(LOG_ERROR, "Incorrect parameter(NULL)");
286                 return;
287         }
288
289         wfd_client_free_raw_discovered_peers(ugd);
290         wfd_ug_view_free_peers(ugd);
291         destroy_wfd_ug_view(ugd);
292
293         __FUNC_EXIT__;
294 }
295
296 #ifdef WFD_DBUS_LAUNCH
297 /**
298  *      This function let the ug initialize wfd
299  *      @return   void
300  *      @param[in] data the pointer to the main data structure
301  *      @param[in] evas the pointer to the evas canvas
302  *      @param[in] obj the pointer to the evas object
303  *      @param[in] event_info the pointer to the event information
304  */
305 static void _wfd_init_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
306 {
307         __FUNC_ENTER__;
308         int res = -1;
309         struct ug_data *ugd = (struct ug_data *)data;
310         WFD_RET_IF(ugd == NULL || ugd->base == NULL, "Incorrect parameter(NULL)\n");
311
312         evas_object_event_callback_del(ugd->base, EVAS_CALLBACK_SHOW, _wfd_init_cb);
313
314         res = launch_wifi_direct_manager(ugd);
315         if (res != 0) {
316                 DBG(LOG_ERROR, "Failed to launch wifi direct manager\n");
317         }
318
319         __FUNC_EXIT__;
320         return;
321 }
322 #endif
323
324 static void *on_create(ui_gadget_h ug, enum ug_mode mode, app_control_h control, void *priv)
325 {
326         __FUNC_ENTER__;
327         struct ug_data *ugd;
328         int wfd_status = -1;
329         int rots[4] = { 0, 90, 180, 270 };
330         int ret;
331
332         if (!ug || !priv) {
333                 return NULL;
334         }
335
336         ugd = priv;
337         ugd->ug = ug;
338
339         bindtextdomain(PACKAGE, LOCALEDIR);
340
341         ugd->win = ug_get_window();
342         if (!ugd->win) {
343                 return NULL;
344         }
345
346         elm_win_wm_rotation_available_rotations_set(ugd->win, rots, 1);
347
348
349         /* check the input parameters from app at first */
350         ugd->wfds = NULL;
351         ugd->device_filter = -1; /* show all devices */
352         ugd->is_auto_exit = false;
353         ugd->is_multi_connect = true;
354         ugd->is_init_ok = false;
355         ugd->title = strdup(_("IDS_WIFI_BODY_WI_FI_DIRECT_ABB"));
356
357         if (control) {
358                 char *wfds = NULL;
359                 char *device_filter = NULL;
360                 char *auto_exit = NULL;
361                 char *multi_connect = NULL;
362                 char *title = NULL;
363                 char* viewtype = NULL;
364
365                 /*
366                 * get the control name
367                 * default value: Wi-Fi Direct
368                 */
369                 ret = app_control_get_extra_data(control, "wfds", &wfds);
370                 if (ret == APP_CONTROL_ERROR_NONE && wfds) {
371                         DBG_SECURE(LOG_INFO, "Wfds name: %s", wfds);
372                         ugd->wfds = strdup(wfds);
373                         WFD_IF_FREE_MEM(wfds);
374                 }
375
376                 ret = app_control_get_extra_data(control, "viewtype", &viewtype);
377                 if(ret == APP_CONTROL_ERROR_NONE && viewtype) {
378                         DBG(LOG_INFO, "viewtype: %s\n", viewtype);
379                         ugd->view_type = strdup(viewtype);
380                         WFD_IF_FREE_MEM(viewtype);
381                 }
382
383                 /*
384                 * get the device filter
385                 * default value: NULL
386                 */
387                 ret = app_control_get_extra_data(control, "device_filter", &device_filter);
388                 if (ret == APP_CONTROL_ERROR_NONE && device_filter) {
389                         DBG(LOG_INFO, "Device filter: %s", device_filter);
390                         if (0 == strncmp(device_filter, "computer", 8)) {
391                                 ugd->device_filter = WFD_DEVICE_TYPE_COMPUTER;
392                         } else if (0 == strncmp(device_filter, "input_device", 12)) {
393                                 ugd->device_filter = WFD_DEVICE_TYPE_INPUT_DEVICE;
394                         } else if (0 == strncmp(device_filter, "printer", 6)) {
395                                 ugd->device_filter = WFD_DEVICE_TYPE_PRINTER;
396                         } else if (0 == strncmp(device_filter, "camera", 6)) {
397                                 ugd->device_filter = WFD_DEVICE_TYPE_CAMERA;
398                         } else if (0 == strncmp(device_filter, "storage", 7)) {
399                                 ugd->device_filter = WFD_DEVICE_TYPE_STORAGE;
400                         } else if (0 == strncmp(device_filter, "network_infra", 13)) {
401                                 ugd->device_filter = WFD_DEVICE_TYPE_NW_INFRA;
402                         } else if (0 == strncmp(device_filter, "display", 7)) {
403                                 ugd->device_filter = WFD_DEVICE_TYPE_DISPLAYS;
404                         } else if (0 == strncmp(device_filter, "multimedia_device", 17)) {
405                                 ugd->device_filter = WFD_DEVICE_TYPE_MM_DEVICES;
406                         } else if (0 == strncmp(device_filter, "game_device", 11)) {
407                                 ugd->device_filter = WFD_DEVICE_TYPE_GAME_DEVICES;
408                         } else if (0 == strncmp(device_filter, "telephone", 9)) {
409                                 ugd->device_filter = WFD_DEVICE_TYPE_TELEPHONE;
410                         } else if (0 == strncmp(device_filter, "audio", 5)) {
411                                 ugd->device_filter = WFD_DEVICE_TYPE_AUDIO;
412                         } else {
413                                 ugd->device_filter = WFD_DEVICE_TYPE_OTHER;
414                         }
415                         WFD_IF_FREE_MEM(device_filter);
416                 }
417
418                 /*
419                 * get whether support auto exit after connection
420                 * default value: false
421                 */
422                 ret = app_control_get_extra_data(control, "auto_exit", &auto_exit);
423                 if (ret == APP_CONTROL_ERROR_NONE && auto_exit) {
424                         DBG(LOG_INFO, "Auto exit: %s", auto_exit);
425                         if (0 == strncmp(auto_exit, "on", 2)) {
426                                 ugd->is_auto_exit = true;
427                         } else {
428                                 ugd->is_auto_exit = false;
429                         }
430                         WFD_IF_FREE_MEM(auto_exit);
431                 }
432
433                 /*
434                 * get whether support multi connection,
435                 * default value: true
436                 */
437                 ret = app_control_get_extra_data(control, "multi_connect", &multi_connect);
438                 if (ret == APP_CONTROL_ERROR_NONE && multi_connect) {
439                         DBG(LOG_INFO, "Multi connection: %s", multi_connect);
440                         if (0 == strncmp(multi_connect, "off", 2)) {
441                                 ugd->is_multi_connect = false;
442                         } else {
443                                 ugd->is_multi_connect = true;
444                         }
445                         WFD_IF_FREE_MEM(multi_connect);
446                 }
447
448                 /*
449                 * get the title of UG
450                 * default value: Wi-Fi Direct
451                 */
452                 ret = app_control_get_extra_data(control, "title_string", &title);
453                 if (ret == APP_CONTROL_ERROR_NONE && title) {
454                         DBG(LOG_INFO, "Title of UG: %s", title);
455                         WFD_IF_FREE_MEM(ugd->title);
456                         ugd->title = strdup(title);
457                         WFD_IF_FREE_MEM(title);
458                 }
459         }
460
461         if (mode == UG_MODE_FULLVIEW) {
462                 ugd->base = _create_fullview(ugd->win, ugd);
463         } else {
464                 ugd->base = _create_frameview(ugd->win, ugd);
465         }
466
467         if (ugd->base) {
468                 evas_object_event_callback_add(ugd->base, EVAS_CALLBACK_DEL, wfd_ug_layout_del_cb, ugd);
469                 ugd->bg = _create_bg(ugd->win, "group_list");
470                 elm_object_part_content_set(ugd->base, "elm.swallow.bg", ugd->bg);
471         } else {
472                 DBG(LOG_ERROR, "Failed to create base layout\n");
473                 return NULL;
474         }
475
476         /* check status of wifi-direct from vconf */
477         wfd_status = wfd_get_vconf_status();
478         if (wfd_status < 0) {
479                 return NULL;
480         }
481
482         /* Enablee Changeable UI feature */
483         ea_theme_changeable_ui_enabled_set(EINA_TRUE);
484
485         ugd->color_table = _color_table_set();
486         ugd->font_table = _font_table_set();
487
488         /* draw UI */
489         initialize_gen_item_class();
490         create_wfd_ug_view(ugd);
491         wfd_ug_view_init_genlist(ugd, true);
492 #ifdef MOTION_CONTROL_ENABLE
493         motion_create(ugd);
494 #endif
495
496         /*
497         * if not deactivated, do initialization at once;
498         * otherwise, do initialization later
499         */
500         if (wfd_status != VCONFKEY_WIFI_DIRECT_DEACTIVATED) {
501                 init_wfd_client(ugd);
502         } else {
503                 ugd->wfd_status = WIFI_DIRECT_STATE_DEACTIVATED;
504
505 #ifdef WFD_DBUS_LAUNCH
506                 evas_object_event_callback_add(ugd->base, EVAS_CALLBACK_SHOW, _wfd_init_cb, ugd);
507 #else
508                 ret = init_wfd_client(ugd);
509                 WFD_RETV_IF(ret != 0, NULL,  "Failed to initialize WFD client library\n");
510
511                 /* Activate WiFi Direct */
512                 DBG(LOG_INFO, "Activating WiFi Direct...");
513                 if (ugd->wfd_status <= WIFI_DIRECT_STATE_DEACTIVATING) {
514                         ret = wfd_client_switch_on(ugd);
515                         WFD_RETV_IF(ret != 0, NULL, "Failed to activate WFD\n");
516                 }
517 #endif
518         }
519         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
520                         __wfd_main_vconf_change_cb, ugd);
521         if (ret) {
522                 DBG(LOG_ERROR, "Failed to set vconf notification callback(VCONFKEY_SETAPPL_DEVICE_NAME_STR)");
523         }
524
525 #ifdef WIFI_STATE_CB
526         ret = vconf_notify_key_changed(VCONFKEY_WIFI_STATE, _wifi_on_state_cb, ugd);
527         if (ret) {
528                 DBG(LOG_ERROR, "Failed to set vconf notification callback(VCONFKEY_WIFI_STATE)");
529
530         }
531 #endif
532
533         ret = vconf_notify_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE,
534                 __wfd_hotspot_mode_vconf_change_cb, ugd);
535         if (ret) {
536                 DBG(LOG_ERROR, "Failed to set vconf notification callback(MOBILE_HOTSPOT_MODE)");
537         }
538
539         __FUNC_EXIT__;
540         return ugd->base;
541 }
542
543 static void on_start(ui_gadget_h ug, app_control_h control, void *priv)
544 {
545         __FUNC_ENTER__;
546         struct ug_data *ugd;
547         int res;
548
549         if (!ug || !priv) {
550                 return;
551         }
552
553         ugd = priv;
554
555         struct utsname kernel_info;
556         res = uname(&kernel_info);
557         if (res != 0) {
558                 DBG(LOG_ERROR, "Failed to detect target type\n");
559         } else {
560                 DBG_SECURE(LOG_INFO, "HW ID of this device [%s]\n", kernel_info.machine);
561                 if (strncmp(kernel_info.machine, "arm", 3) != 0) {
562                         wfd_ug_warn_popup(ugd, _("IDS_ST_POP_NOT_SUPPORTED"), POPUP_TYPE_TERMINATE_NOT_SUPPORT);
563                         return;
564                 }
565         }
566
567         __FUNC_EXIT__;
568 }
569
570 static void on_pause(ui_gadget_h ug, app_control_h control, void *priv)
571 {
572         __FUNC_ENTER__;
573
574         WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");
575         struct ug_data *ugd = priv;
576         ugd->is_paused = true;
577
578         wfd_refresh_wifi_direct_state(ugd);
579         DBG(LOG_INFO, "on pause, wfd status: %d\n", ugd->wfd_status);
580
581         if ((WIFI_DIRECT_STATE_DISCOVERING == ugd->wfd_status) &&
582                 (WIFI_DIRECT_ERROR_NONE != wifi_direct_cancel_discovery())) {
583                 DBG(LOG_ERROR, "Failed to send cancel discovery state [%d]\n", ugd->wfd_status);
584                 __FUNC_EXIT__;
585                 return;
586         }
587
588         ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_STOPPED;
589
590         __FUNC_EXIT__;
591 }
592
593 static void on_resume(ui_gadget_h ug, app_control_h control, void *priv)
594 {
595         __FUNC_ENTER__;
596
597         WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");
598         struct ug_data *ugd = priv;
599         ugd->is_paused = false;
600         int ret;
601         wfd_refresh_wifi_direct_state(ugd);
602         DBG(LOG_INFO, "on resume, status: %d\n", ugd->wfd_status);
603         ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_STOPPED;
604
605         elm_genlist_realized_items_update(ugd->genlist);
606
607         if (ugd->wfd_status > WIFI_DIRECT_STATE_DEACTIVATED && ugd->wfd_status < WIFI_DIRECT_STATE_CONNECTED) {
608                 DBG(LOG_INFO, "Start discovery again\n");
609                 ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL_START;
610                 ret = wifi_direct_start_discovery_specific_channel(false, 1, WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL);
611                 if (ret != WIFI_DIRECT_ERROR_NONE) {
612                         ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_NONE;
613                         DBG(LOG_ERROR, "Failed to start discovery. [%d]\n", ret);
614                         wifi_direct_cancel_discovery();
615                 }
616         }
617
618         __FUNC_EXIT__;
619 }
620
621 static void on_destroy(ui_gadget_h ug, app_control_h control, void *priv)
622 {
623         __FUNC_ENTER__;
624
625         WFD_RET_IF(ug == NULL || priv == NULL, "The param is NULL\n");
626
627         struct ug_data *ugd = priv;
628         int ret;
629         WFD_RET_IF(ugd->base == NULL, "The param is NULL\n");
630
631 #ifdef MOTION_CONTROL_ENABLE
632         motion_destroy();
633 #endif
634
635         /* DeInit WiFi Direct */
636         ret = deinit_wfd_client(ugd);
637         if (ret) {
638                 DBG(LOG_ERROR,"Failed to DeInit WiFi Direct");
639         }
640
641         if (ugd->scan_toolbar) {
642                 wfd_ug_view_refresh_button(ugd->scan_toolbar,
643                         _("IDS_WIFI_SK4_SCAN"), FALSE);
644         }
645
646         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
647                         __wfd_main_vconf_change_cb);
648         if (ret == -1) {
649                 DBG(LOG_ERROR,"Failed to ignore vconf key callback\n");
650         }
651
652 #ifdef WIFI_STATE_CB
653         ret = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wifi_on_state_cb);
654         if (ret == -1) {
655                 DBG(LOG_ERROR,"Failed to ignore vconf key callback\n");
656         }
657 #endif
658
659         ret = vconf_ignore_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE,
660                         __wfd_hotspot_mode_vconf_change_cb);
661         if (ret == -1) {
662                 DBG(LOG_ERROR,"Failed to ignore vconf key callback MOBILE_HOTSPOT_MODE\n");
663         }
664
665         wfd_ug_view_free_peers(ugd);
666         WFD_IF_FREE_MEM(ugd->title);
667         WFD_IF_FREE_MEM(ugd->wfds);
668         WFD_IF_FREE_MEM(ugd->view_type);
669
670         WFD_IF_DEL_OBJ(ugd->bg);
671
672 #ifdef WFD_DBUS_LAUNCH
673         if (ugd->base)
674                 evas_object_event_callback_del(ugd->base, EVAS_CALLBACK_SHOW, _wfd_init_cb);
675 #endif
676         WFD_IF_DEL_OBJ(ugd->base);
677         DBG(LOG_INFO, "WFD client deregistered");
678
679         __FUNC_EXIT__;
680         return;
681 }
682
683 static void on_message(ui_gadget_h ug, app_control_h msg, app_control_h control, void *priv)
684 {
685         __FUNC_ENTER__;
686         char* app_msg = NULL;
687         struct ug_data *ugd = priv;
688
689         if (!ug || !priv) {
690                 DBG(LOG_ERROR, "The param is NULL\n");
691                 return;
692         }
693
694         if (msg) {
695                 app_control_get_extra_data(msg, "msg", &app_msg);
696                 DBG(LOG_DEBUG, "Msg from app: %s", app_msg);
697
698                 if (!strcmp(app_msg, "destroy")) {
699                         if(!ugd->rename_popup) {
700                                 DBG(LOG_INFO, "Destroying UG.");
701                                 wfd_ug_view_free_peers(ugd);
702                                 wfd_destroy_ug(ugd);
703                         }
704                 }
705                 WFD_IF_FREE_MEM(app_msg);
706         }
707         __FUNC_EXIT__;
708 }
709
710 static void on_event(ui_gadget_h ug, enum ug_event event, app_control_h control, void *priv)
711 {
712         __FUNC_ENTER__;
713
714         if (!ug || !priv) {
715                 DBG(LOG_ERROR, "The param is NULL\n");
716                 return;
717         }
718
719         switch (event) {
720         case UG_EVENT_LOW_MEMORY:
721                 DBG(LOG_INFO, "UG_EVENT_LOW_MEMORY\n");
722                 break;
723         case UG_EVENT_LOW_BATTERY:
724                 DBG(LOG_INFO, "UG_EVENT_LOW_BATTERY\n");
725                 break;
726         case UG_EVENT_LANG_CHANGE:
727                 DBG(LOG_INFO, "UG_EVENT_LANG_CHANGE\n");
728                 break;
729         case UG_EVENT_ROTATE_PORTRAIT:
730                 _ctxpopup_move();
731                 DBG(LOG_INFO, "UG_EVENT_ROTATE_PORTRAIT\n");
732                 break;
733         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
734                 DBG(LOG_INFO, "UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN\n");
735                 break;
736         case UG_EVENT_ROTATE_LANDSCAPE:
737                 _ctxpopup_move();
738                 DBG(LOG_INFO, "UG_EVENT_ROTATE_LANDSCAPE\n");
739                 break;
740         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
741                 _ctxpopup_move();
742                 DBG(LOG_INFO, "UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN\n");
743                 break;
744         default:
745                 DBG(LOG_INFO, "default\n");
746                 break;
747         }
748
749         __FUNC_EXIT__;
750 }
751
752 static void on_key_event(ui_gadget_h ug, enum ug_key_event event, app_control_h control, void *priv)
753 {
754         __FUNC_ENTER__;
755
756         if (!ug || !priv) {
757                 DBG(LOG_ERROR, "The param is NULL\n");
758                 return;
759         }
760
761         switch (event) {
762         case UG_KEY_EVENT_END:
763                 DBG(LOG_INFO, "UG_KEY_EVENT_END\n");
764                 break;
765         default:
766                 break;
767         }
768
769         __FUNC_EXIT__;
770 }
771
772 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
773 {
774         __FUNC_ENTER__;
775         struct ug_data *ugd;
776
777         if (!ops) {
778                 DBG(LOG_ERROR, "The param is NULL\n");
779                 return -1;
780         }
781
782         ugd = calloc(1, sizeof(struct ug_data));
783         if (ugd == NULL) {
784                 DBG(LOG_ERROR, "Failed to allocate memory for UG data\n");
785                 return -1;
786         }
787
788         global_ugd = ugd;
789
790         ops->create = on_create;
791         ops->start = on_start;
792         ops->pause = on_pause;
793         ops->resume = on_resume;
794         ops->destroy = on_destroy;
795         ops->message = on_message;
796         ops->event = on_event;
797         ops->key_event = on_key_event;
798         ops->priv = ugd;
799         ops->opt = UG_OPT_INDICATOR_ENABLE;
800
801         __FUNC_EXIT__;
802         return 0;
803 }
804
805 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
806 {
807         __FUNC_ENTER__;
808
809         struct ug_data *ugd;
810
811         if (!ops) {
812                 DBG(LOG_ERROR, "The param is NULL\n");
813                 return;
814         }
815
816         ugd = ops->priv;
817
818         WFD_IF_FREE_MEM(ugd);
819
820         __FUNC_EXIT__;
821 }
822
823 UG_MODULE_API int setting_plugin_reset(app_control_h control, void *priv)
824 {
825         __FUNC_ENTER__;
826         int res = -1;
827         wifi_direct_state_e state;
828
829         res = wifi_direct_initialize();
830         if (res != WIFI_DIRECT_ERROR_NONE) {
831                 DBG(LOG_ERROR, "Failed to initialize wifi direct. [%d]\n", res);
832                 return -1;
833         }
834
835         res = wifi_direct_get_state(&state);
836         if (res != WIFI_DIRECT_ERROR_NONE) {
837                 DBG(LOG_ERROR, "Failed to get link status. [%d]\n", res);
838                 return -1;
839         }
840
841         if (state < WIFI_DIRECT_STATE_ACTIVATING) {
842                 DBG(LOG_INFO, "No need to reset Wi-Fi Direct.\n");
843         } else {
844                 /*if connected, disconnect all devices*/
845                 if (WIFI_DIRECT_STATE_CONNECTED == state) {
846                         res = wifi_direct_disconnect_all();
847                         if (res != WIFI_DIRECT_ERROR_NONE) {
848                                 DBG(LOG_ERROR, "Failed to send disconnection request to all. [%d]\n", res);
849                                 return -1;
850                         }
851                 }
852
853                 res = wifi_direct_deactivate();
854                 if (res != WIFI_DIRECT_ERROR_NONE) {
855                         DBG(LOG_ERROR, "Failed to reset Wi-Fi Direct. [%d]\n", res);
856                         return -1;
857                 }
858         }
859
860         res = wifi_direct_deinitialize();
861         if (res != WIFI_DIRECT_ERROR_NONE) {
862                 DBG(LOG_ERROR, "Failed to deinitialize wifi direct. [%d]\n", res);
863                 return -1;
864         }
865
866         __FUNC_EXIT__;
867         return 0;
868 }
869
870 UG_MODULE_API int setting_plugin_search_init(app_control_h control, void *priv, char** applocale)
871 {
872         __FUNC_ENTER__;
873
874         *applocale = strdup("ug-setting-wifidirect-efl");
875         void *node = NULL;
876
877         Eina_List **pplist = (Eina_List**)priv;
878
879         node = setting_plugin_search_item_add("IDS_WIFI_BUTTON_MULTI_CONNECT", "viewtype:IDS_WIFI_BUTTON_MULTI_CONNECT", NULL, 5, NULL);
880         *pplist = eina_list_append(*pplist, node);
881
882          __FUNC_EXIT__;
883         return 0;
884 }
885