Tizen 2.1 base
[apps/native/ug-mobile-ap.git] / src / mh_common_utility.c
1 /*
2 * ug-mobile-ap
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://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 "mh_common_utility.h"
21 #include "mobile_hotspot.h"
22
23 static void __one_btn_popup_resp(void *data, Evas_Object *obj, void *event_info)
24 {
25         __MOBILE_AP_FUNC_ENTER__;
26
27         if (data == NULL || obj == NULL) {
28                 ERR("The param is NULL\n");
29                 return;
30         }
31
32         mh_appdata_t *ad = (mh_appdata_t *)data;
33
34         evas_object_del(ad->popup);
35         ad->popup = NULL;
36
37         DBG("popup_type : %d\n", ad->popup_type);
38         switch (ad->popup_type) {
39         case MH_POP_USB_CONNECT:
40                 _update_usb_item(ad, MH_STATE_NONE);
41                 vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
42                                 _handle_usb_mode_change);
43                 break;
44
45         case MH_POP_INFORMATION:
46                 break;
47
48         case MH_POP_WIFI_PASSWORD_SHORT:
49                 if (ad->setup.pw_entry == NULL)
50                         break;
51
52                 evas_object_show(ad->setup.pw_entry);
53                 elm_object_focus_set(ad->setup.pw_entry, EINA_TRUE);
54                 break;
55
56         default:
57                 DBG("Unknown popup_type : %d\n", ad->popup_type);
58                 break;
59         }
60
61         __MOBILE_AP_FUNC_EXIT__;
62 }
63
64 static void __alert_popup_resp(void *data, Evas_Object *obj, void *event_info)
65 {
66         __MOBILE_AP_FUNC_ENTER__;
67
68         if (data == NULL || obj == NULL) {
69                 ERR("The param is NULL\n");
70                 return;
71         }
72
73         mh_appdata_t *ad = (mh_appdata_t *)data;
74
75         evas_object_del(ad->popup);
76         ad->popup = NULL;
77
78         DBG("popup_type : %d\n", ad->popup_type);
79         switch (ad->popup_type) {
80         case MH_POP_INFORMATION_WO_BUTTON:
81                 break;
82
83         default:
84                 DBG("Unknown popup_type : %d\n", ad->popup_type);
85                 break;
86         }
87
88         __MOBILE_AP_FUNC_EXIT__;
89 }
90
91 static void __popup_resp_yes(void *data, Evas_Object *obj, void *event_info)
92 {
93         __MOBILE_AP_FUNC_ENTER__;
94
95         if (data == NULL || obj == NULL) {
96                 ERR("The param is NULL\n");
97                 return;
98         }
99
100         mh_appdata_t *ad = (mh_appdata_t *)data;
101         bool wifi_state;
102         int ret = 0;
103
104         evas_object_del(ad->popup);
105         ad->popup = NULL;
106
107         DBG("popup_type : %d\n", ad->popup_type);
108         switch (ad->popup_type) {
109         case MH_POP_WIFI_ON_CONF:
110                 wifi_is_activated(&wifi_state);
111                 if (wifi_state == true) {
112                         if (_turn_off_wifi(ad) != 0) {
113                                 ERR("_turn_off_wifi is failed\n");
114                                 _update_wifi_item(ad, MH_STATE_NONE);
115                         }
116                 } else if (_is_wifi_direct_on() == true) {
117                         if (_turn_off_wifi_direct(ad) != 0) {
118                                 ERR("_turn_off_wifi_direct is failed\n");
119                                 _update_wifi_item(ad, MH_STATE_NONE);
120                         }
121                 } else {
122                         ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
123                         if (ret != TETHERING_ERROR_NONE) {
124                                 ERR("wifi tethering on is failed : %d\n", ret);
125                                 _update_wifi_item(ad, MH_STATE_NONE);
126                         }
127                 }
128                 break;
129
130         case MH_POP_WIFI_OFF_CONF:
131                 ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
132                 if (ret != TETHERING_ERROR_NONE) {
133                         ERR("wifi tethering off is failed : %d\n", ret);
134                         _update_wifi_item(ad, MH_STATE_NONE);
135                 }
136                 break;
137
138         case MH_POP_BT_ON_CONF:
139                 ret = tethering_enable(ad->handle, TETHERING_TYPE_BT);
140                 if (ret != TETHERING_ERROR_NONE) {
141                         ERR("Error enable bt tethering : %d\n", ret);
142                         _update_bt_item(ad, MH_STATE_NONE);
143                 }
144                 break;
145
146         case MH_POP_USB_ON_CONF:
147                 if (_get_vconf_usb_state() != VCONFKEY_SYSMAN_USB_AVAILABLE) {
148                         _prepare_popup(ad, MH_POP_USB_CONNECT,
149                                         _("IDS_MOBILEAP_POP_CONNECT_USB_CABLE"));
150                         _create_popup(ad);
151                         vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
152                                         _handle_usb_mode_change, (void *)ad);
153                         break;
154                 }
155
156                 _prepare_popup(ad, MH_POP_USB_ON_PREVCONN_CONF,
157                                 _("IDS_MOBILEAP_POP_ENABLING_USB_TETHERING_WILL_DISCONNECT_PREVIOUS_USB_CONNECTION"));
158                 _create_popup(ad);
159                 break;
160
161         case MH_POP_USB_ON_PREVCONN_CONF:
162                 ret = tethering_enable(ad->handle, TETHERING_TYPE_USB);
163                 if (ret != TETHERING_ERROR_NONE) {
164                         ERR("Error enable usb tethering : %d\n", ret);
165                         _update_usb_item(ad, MH_STATE_NONE);
166                 }
167                 break;
168
169         case MH_POP_ENTER_TO_WIFI_SETUP_CONF:
170                 _update_wifi_item(ad, MH_STATE_PROCESS);
171                 ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
172                 if (ret != TETHERING_ERROR_NONE) {
173                         ERR("Wi-Fi tethering off is failed : %d\n", ret);
174                         _update_wifi_item(ad, MH_STATE_NONE);
175                 } else
176                         ad->main.need_recover_wifi_tethering = true;
177
178                 mh_draw_wifi_setup_view(ad);
179                 break;
180
181         default:
182                 DBG("Unknown popup_type : %d\n", ad->popup_type);
183                 break;
184         }
185
186         __MOBILE_AP_FUNC_EXIT__;
187 }
188
189 static void __popup_resp_no(void *data, Evas_Object *obj, void *event_info)
190 {
191         __MOBILE_AP_FUNC_ENTER__;
192
193         if (data == NULL || obj == NULL) {
194                 ERR("The param is NULL\n");
195                 return;
196         }
197
198         mh_appdata_t *ad = (mh_appdata_t *)data;
199
200         evas_object_del(ad->popup);
201         ad->popup = NULL;
202
203         DBG("popup_type : %d\n", ad->popup_type);
204         switch (ad->popup_type) {
205         case MH_POP_WIFI_ON_CONF:
206                 _update_wifi_item(ad, MH_STATE_NONE);
207                 break;
208
209         case MH_POP_WIFI_OFF_CONF:
210                 _update_wifi_item(ad, MH_STATE_NONE);
211                 break;
212
213         case MH_POP_BT_ON_CONF:
214                 _update_bt_item(ad, MH_STATE_NONE);
215                 break;
216
217         case MH_POP_USB_ON_CONF:
218                 _update_usb_item(ad, MH_STATE_NONE);
219                 break;
220
221         case MH_POP_USB_ON_PREVCONN_CONF:
222                 _update_usb_item(ad, MH_STATE_NONE);
223                 break;
224
225         case MH_POP_ENTER_TO_WIFI_SETUP_CONF:
226                 break;
227
228         default:
229                 DBG("Unknown popup_type : %d\n", ad->popup_type);
230                 break;
231         }
232
233         __MOBILE_AP_FUNC_EXIT__;
234 }
235
236 static bool _count_connected_clients_cb(tethering_client_h client, void *user_data)
237 {
238         if (user_data == NULL) {
239                 ERR("user_data is NULL\n");
240                 return true;
241         }
242
243         int *count = (int *)user_data;
244
245         *count += 1;
246
247         return true;
248 }
249
250 void _prepare_popup(mh_appdata_t *ad, int type, const char *str)
251 {
252         __MOBILE_AP_FUNC_ENTER__;
253
254         if (ad == NULL || str == NULL) {
255                 ERR("param is NULL : ad[%x], str[%x]\n", ad, str);
256                 return;
257         }
258
259         int nLen = 0;
260
261         nLen = strlen(str);
262         if (sizeof(ad->popup_string) <= nLen)
263                 ERR("We should make the message[%s] less than %d",
264                                 str, sizeof(ad->popup_string));
265
266         ad->popup_type = type;
267         snprintf(ad->popup_string, sizeof(ad->popup_string), "%s", str);
268
269         __MOBILE_AP_FUNC_EXIT__;
270 }
271
272 Eina_Bool _create_popup(mh_appdata_t *ad)
273 {
274         __MOBILE_AP_FUNC_ENTER__;
275
276         Evas_Object *btn = NULL;
277
278         if (ad == NULL) {
279                 ERR("The param is NULL\n");
280                 return 0;
281         }
282
283         if (ad->popup != NULL) {
284                 DBG("Pop-up already exists. Delete it.\n");
285                 evas_object_del(ad->popup);
286                 ad->popup = NULL;
287         }
288
289         DBG("Create_popup %d\n", ad->popup_type);
290         switch (ad->popup_type) {
291         case MH_POP_WIFI_ON_CONF:
292                 ad->popup = elm_popup_add(ad->win);
293                 evas_object_size_hint_weight_set(ad->popup,
294                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
295
296                 elm_object_text_set(ad->popup, ad->popup_string);
297
298                 btn = elm_button_add(ad->popup);
299                 elm_object_style_set(btn, "popup_button/default");
300                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
301                 elm_object_part_content_set(ad->popup, "button1", btn);
302                 evas_object_smart_callback_add(btn, "clicked",
303                                 __popup_resp_yes, (void *)ad);
304
305                 btn = elm_button_add(ad->popup);
306                 elm_object_style_set(btn, "popup_button/default");
307                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
308                 elm_object_part_content_set(ad->popup, "button2", btn);
309                 evas_object_smart_callback_add(btn, "clicked",
310                                 __popup_resp_no, (void *)ad);
311
312                 evas_object_show(ad->popup);
313                 break;
314
315         case MH_POP_WIFI_OFF_CONF:
316                 ad->popup = elm_popup_add(ad->win);
317                 evas_object_size_hint_weight_set(ad->popup,
318                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
319
320                 elm_object_text_set(ad->popup, ad->popup_string);
321
322                 btn = elm_button_add(ad->popup);
323                 elm_object_style_set(btn, "popup_button/default");
324                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
325                 elm_object_part_content_set(ad->popup, "button1", btn);
326                 evas_object_smart_callback_add(btn, "clicked",
327                                 __popup_resp_yes, (void *)ad);
328
329                 btn = elm_button_add(ad->popup);
330                 elm_object_style_set(btn, "popup_button/default");
331                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
332                 elm_object_part_content_set(ad->popup, "button2", btn);
333                 evas_object_smart_callback_add(btn, "clicked",
334                                 __popup_resp_no, (void *)ad);
335
336                 evas_object_show(ad->popup);
337                 break;
338
339         case MH_POP_USB_CONNECT:
340                 ad->popup = elm_popup_add(ad->win);
341                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
342                                 EVAS_HINT_EXPAND);
343
344                 elm_object_text_set(ad->popup, ad->popup_string);
345
346                 btn = elm_button_add(ad->popup);
347                 elm_object_style_set(btn, "popup_button/default");
348                 elm_object_text_set(btn, S_("IDS_COM_SK_CANCEL"));
349                 elm_object_part_content_set(ad->popup, "button1", btn);
350                 evas_object_smart_callback_add(btn, "clicked",
351                                 __one_btn_popup_resp, (void *)ad);
352
353                 evas_object_show(ad->popup);
354                 break;
355
356         case MH_POP_BT_ON_CONF:
357                 ad->popup = elm_popup_add(ad->win);
358                 evas_object_size_hint_weight_set(ad->popup,
359                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
360
361                 elm_object_text_set(ad->popup, ad->popup_string);
362
363                 btn = elm_button_add(ad->popup);
364                 elm_object_style_set(btn, "popup_button/default");
365                 elm_object_text_set(btn, S_("IDS_COM_SK_OK"));
366                 elm_object_part_content_set(ad->popup, "button1", btn);
367                 evas_object_smart_callback_add(btn, "clicked",
368                                 __popup_resp_yes, (void *)ad);
369
370                 btn = elm_button_add(ad->popup);
371                 elm_object_style_set(btn, "popup_button/default");
372                 elm_object_text_set(btn, S_("IDS_COM_SK_CANCEL"));
373                 elm_object_part_content_set(ad->popup, "button2", btn);
374                 evas_object_smart_callback_add(btn, "clicked",
375                                 __popup_resp_no, (void *)ad);
376
377                 evas_object_show(ad->popup);
378                 break;
379
380         case MH_POP_USB_ON_CONF:
381         case MH_POP_USB_ON_PREVCONN_CONF:
382                 ad->popup = elm_popup_add(ad->win);
383                 evas_object_size_hint_weight_set(ad->popup,
384                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
385
386                 elm_object_text_set(ad->popup, ad->popup_string);
387
388                 btn = elm_button_add(ad->popup);
389                 elm_object_style_set(btn, "popup_button/default");
390                 elm_object_text_set(btn, S_("IDS_COM_SK_OK"));
391                 elm_object_part_content_set(ad->popup, "button1", btn);
392                 evas_object_smart_callback_add(btn, "clicked",
393                                 __popup_resp_yes, (void *)ad);
394
395                 btn = elm_button_add(ad->popup);
396                 elm_object_style_set(btn, "popup_button/default");
397                 elm_object_text_set(btn, S_("IDS_COM_SK_CANCEL"));
398                 elm_object_part_content_set(ad->popup, "button2", btn);
399                 evas_object_smart_callback_add(btn, "clicked",
400                                 __popup_resp_no, (void *)ad);
401
402                 evas_object_show(ad->popup);
403                 break;
404
405         case MH_POP_INFORMATION:
406                 ad->popup = elm_popup_add(ad->win);
407                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
408                                 EVAS_HINT_EXPAND);
409
410                 elm_object_text_set(ad->popup, ad->popup_string);
411
412                 btn = elm_button_add(ad->popup);
413                 elm_object_style_set(btn, "popup_button/default");
414                 elm_object_text_set(btn, S_("IDS_COM_POP_CLOSE"));
415                 elm_object_part_content_set(ad->popup, "button1", btn);
416                 evas_object_smart_callback_add(btn, "clicked",
417                                 __one_btn_popup_resp, (void *)ad);
418
419                 evas_object_show(ad->popup);
420                 break;
421
422         case MH_POP_WIFI_PASSWORD_SHORT:
423                 ad->popup = elm_popup_add(ad->win);
424                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
425                                 EVAS_HINT_EXPAND);
426
427                 elm_object_text_set(ad->popup, ad->popup_string);
428
429                 btn = elm_button_add(ad->popup);
430                 elm_object_style_set(btn, "popup_button/default");
431                 elm_object_text_set(btn, S_("IDS_COM_POP_CLOSE"));
432                 elm_object_part_content_set(ad->popup, "button1", btn);
433                 evas_object_smart_callback_add(btn, "clicked",
434                                 __one_btn_popup_resp, (void *)ad);
435
436                 evas_object_show(ad->popup);
437                 break;
438
439         case MH_POP_INFORMATION_WO_BUTTON:
440                 ad->popup = elm_popup_add(ad->win);
441                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
442                                 EVAS_HINT_EXPAND);
443
444                 elm_object_text_set(ad->popup, ad->popup_string);
445
446                 elm_popup_timeout_set(ad->popup, MH_POPUP_TIMEOUT);
447                 evas_object_smart_callback_add(ad->popup, "timeout",
448                                 __alert_popup_resp, (void *)ad);
449                 evas_object_smart_callback_add(ad->popup, "block,clicked",
450                                 __alert_popup_resp, (void *)ad);
451
452                 evas_object_show(ad->popup);
453                 break;
454
455         case MH_POP_ENTER_TO_WIFI_SETUP_CONF:
456                 ad->popup = elm_popup_add(ad->win);
457                 evas_object_size_hint_weight_set(ad->popup,
458                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
459
460                 elm_object_text_set(ad->popup, ad->popup_string);
461
462                 btn = elm_button_add(ad->popup);
463                 elm_object_style_set(btn, "popup_button/default");
464                 elm_object_text_set(btn, S_("IDS_COM_SK_OK"));
465                 elm_object_part_content_set(ad->popup, "button1", btn);
466                 evas_object_smart_callback_add(btn, "clicked",
467                                 __popup_resp_yes, (void *)ad);
468
469                 btn = elm_button_add(ad->popup);
470                 elm_object_style_set(btn, "popup_button/default");
471                 elm_object_text_set(btn, S_("IDS_COM_SK_CANCEL"));
472                 elm_object_part_content_set(ad->popup, "button2", btn);
473                 evas_object_smart_callback_add(btn, "clicked",
474                                 __popup_resp_no, (void *)ad);
475
476                 evas_object_show(ad->popup);
477                 break;
478         }
479
480         __MOBILE_AP_FUNC_EXIT__;
481
482         return TRUE;
483 }
484
485 void _destroy_popup(mh_appdata_t *ad)
486 {
487         __MOBILE_AP_FUNC_ENTER__;
488
489         if (ad == NULL) {
490                 ERR("ad is NULL\n");
491                 return;
492         }
493
494         if (ad->popup) {
495                 evas_object_del(ad->popup);
496                 ad->popup = NULL;
497         }
498
499         ad->popup_type = MH_POPUP_NONE;
500
501         __MOBILE_AP_FUNC_EXIT__;
502
503         return;
504 }
505
506 Evas_Object *_create_bg(Evas_Object *parent, const char *style)
507 {
508         __MOBILE_AP_FUNC_ENTER__;
509
510         if (parent == NULL || style == NULL) {
511                 ERR("The param is NULL\n");
512                 return NULL;
513         }
514
515         Evas_Object *bg = NULL;
516
517         bg = elm_bg_add(parent);
518         if (bg == NULL) {
519                 ERR("bg is NULL\n");
520                 return NULL;
521         }
522
523         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
524         elm_object_style_set(bg, style);
525         evas_object_show(bg);
526
527         return bg;
528 }
529
530 Evas_Object *_create_win_layout(mh_appdata_t *ad)
531 {
532         __MOBILE_AP_FUNC_ENTER__;
533
534         if (ad->win == NULL) {
535                 ERR("There is no main window\n");
536                 return NULL;
537         }
538
539         Evas_Object *layout;
540         Evas_Object *bg;
541
542         layout = elm_layout_add(ad->win);
543         if (layout == NULL) {
544                 ERR("layout is NULL\n");
545                 return NULL;
546         }
547
548         elm_layout_theme_set(layout, "layout", "application", "default");
549         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND,
550                         EVAS_HINT_EXPAND);
551
552         bg = _create_bg(layout, "group_list");
553         if (bg == NULL) {
554                 ERR("bg is NULL\n");
555                 evas_object_del(layout);
556                 return NULL;
557         }
558         elm_object_part_content_set(layout, "elm.swallow.bg", bg);
559
560         evas_object_show(layout);
561
562         ad->layout = layout;
563         ad->bg = bg;
564
565         __MOBILE_AP_FUNC_EXIT__;
566
567         return layout;
568 }
569
570 Evas_Object *_create_naviframe(Evas_Object *parent)
571 {
572         __MOBILE_AP_FUNC_ENTER__;
573
574         if (parent == NULL) {
575                 ERR("parent is NULL\n");
576                 return NULL;
577         }
578
579         Evas_Object *naviframe;
580
581         naviframe = elm_naviframe_add(parent);
582         if (naviframe == NULL) {
583                 ERR("naviframe is NULL\n");
584                 return NULL;
585         }
586         evas_object_show(naviframe);
587
588         __MOBILE_AP_FUNC_EXIT__;
589
590         return naviframe;
591 }
592
593 void _handle_usb_mode_change(keynode_t *key, void *data)
594 {
595         mh_appdata_t *ad = (mh_appdata_t *)data;
596         int vconf_key = 0;
597
598         if (!data) {
599                 ERR("The param is NULL\n");
600                 return;
601         }
602
603         if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
604                 ERR("Invalid vconf key\n");
605                 return;
606         }
607
608         vconf_key = vconf_keynode_get_int(key);
609         DBG("vconf_key : %d\n", vconf_key);
610         if (vconf_key == SETTING_USB_NONE_MODE) {
611                 DBG("usb-utilties is processing mode change\n");
612                 return;
613         }
614
615         vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
616                                 _handle_usb_mode_change);
617
618         evas_object_del(ad->popup);
619         ad->popup = NULL;
620
621         if (tethering_enable(ad->handle, TETHERING_TYPE_USB) != TETHERING_ERROR_NONE) {
622                 DBG("Error enable usb tethering\n");
623                 _update_usb_item(ad, MH_STATE_NONE);
624         }
625 }
626
627 int _get_vconf_hotspot_mode(void)
628 {
629         int value = VCONFKEY_MOBILE_HOTSPOT_MODE_NONE;
630
631         if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &value) < 0) {
632                 ERR("vconf_get_int is failed\n");
633                 return 0;
634         }
635
636         DBG("%s : %d\n", VCONFKEY_MOBILE_HOTSPOT_MODE, value);
637
638         return value;
639 }
640
641 Eina_Bool _get_no_of_connected_device(tethering_h handle, int *no, tethering_type_e type)
642 {
643         if (handle == NULL || no == NULL)
644                 return FALSE;
645
646         tethering_foreach_connected_clients(handle, type,
647                         _count_connected_clients_cb, (void *)no);
648
649         return TRUE;
650 }