merge with master
[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://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 "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                 ret = tethering_enable(ad->handle, TETHERING_TYPE_USB);
157                 if (ret != TETHERING_ERROR_NONE) {
158                         ERR("Error enable usb tethering : %d\n", ret);
159                         _update_usb_item(ad, MH_STATE_NONE);
160                 }
161                 break;
162
163         case MH_POP_ENTER_TO_WIFI_SETUP_CONF:
164                 _update_wifi_item(ad, MH_STATE_PROCESS);
165                 ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
166                 if (ret != TETHERING_ERROR_NONE) {
167                         ERR("wifi tethering off is failed : %d\n", ret);
168                         _update_wifi_item(ad, MH_STATE_NONE);
169                 } else
170                         ad->main.old_wifi_state = true;
171
172                 mh_draw_wifi_setup_view(ad);
173                 break;
174
175         default:
176                 DBG("Unknown popup_type : %d\n", ad->popup_type);
177                 break;
178         }
179
180         __MOBILE_AP_FUNC_EXIT__;
181 }
182
183 static void __popup_resp_no(void *data, Evas_Object *obj, void *event_info)
184 {
185         __MOBILE_AP_FUNC_ENTER__;
186
187         if (data == NULL || obj == NULL) {
188                 ERR("The param is NULL\n");
189                 return;
190         }
191
192         mh_appdata_t *ad = (mh_appdata_t *)data;
193
194         evas_object_del(ad->popup);
195         ad->popup = NULL;
196
197         DBG("popup_type : %d\n", ad->popup_type);
198         switch (ad->popup_type) {
199         case MH_POP_WIFI_ON_CONF:
200                 _update_wifi_item(ad, MH_STATE_NONE);
201                 break;
202
203         case MH_POP_WIFI_OFF_CONF:
204                 _update_wifi_item(ad, MH_STATE_NONE);
205                 break;
206
207         case MH_POP_BT_ON_CONF:
208                 _update_bt_item(ad, MH_STATE_NONE);
209                 break;
210
211         case MH_POP_USB_ON_CONF:
212                 _update_usb_item(ad, MH_STATE_NONE);
213                 break;
214
215         case MH_POP_ENTER_TO_WIFI_SETUP_CONF:
216                 break;
217
218         default:
219                 DBG("Unknown popup_type : %d\n", ad->popup_type);
220                 break;
221         }
222
223         __MOBILE_AP_FUNC_EXIT__;
224 }
225
226 static bool _count_connected_clients_cb(tethering_client_h client, void *user_data)
227 {
228         if (user_data == NULL) {
229                 ERR("user_data is NULL\n");
230                 return true;
231         }
232
233         int *count = (int *)user_data;
234
235         *count += 1;
236
237         return true;
238 }
239
240 void _prepare_popup(mh_appdata_t *ad, int type, const char *str)
241 {
242         __MOBILE_AP_FUNC_ENTER__;
243
244         if (ad == NULL || str == NULL) {
245                 ERR("param is NULL : ad[%x], str[%x]\n", ad, str);
246                 return;
247         }
248
249         int nLen = 0;
250
251         nLen = strlen(str);
252         if (sizeof(ad->popup_string) <= nLen)
253                 ERR("We should make the message[%s] less than %d",
254                                 str, sizeof(ad->popup_string));
255
256         ad->popup_type = type;
257         snprintf(ad->popup_string, sizeof(ad->popup_string), "%s", str);
258
259         __MOBILE_AP_FUNC_EXIT__;
260 }
261
262 Eina_Bool _create_popup(mh_appdata_t *ad)
263 {
264         __MOBILE_AP_FUNC_ENTER__;
265
266         Evas_Object *btn = NULL;
267
268         if (ad == NULL) {
269                 ERR("The param is NULL\n");
270                 return 0;
271         }
272
273         if (ad->popup != NULL) {
274                 DBG("Pop-up already exists. Delete it.\n");
275                 evas_object_del(ad->popup);
276                 ad->popup = NULL;
277         }
278
279         DBG("Create_popup %d\n", ad->popup_type);
280         switch (ad->popup_type) {
281         case MH_POP_WIFI_ON_CONF:
282                 ad->popup = elm_popup_add(ad->win);
283                 evas_object_size_hint_weight_set(ad->popup,
284                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
285
286                 elm_object_text_set(ad->popup, ad->popup_string);
287
288                 btn = elm_button_add(ad->popup);
289                 elm_object_style_set(btn, "popup_button/default");
290                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
291                 elm_object_part_content_set(ad->popup, "button1", btn);
292                 evas_object_smart_callback_add(btn, "clicked",
293                                 __popup_resp_yes, (void *)ad);
294
295                 btn = elm_button_add(ad->popup);
296                 elm_object_style_set(btn, "popup_button/default");
297                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
298                 elm_object_part_content_set(ad->popup, "button2", btn);
299                 evas_object_smart_callback_add(btn, "clicked",
300                                 __popup_resp_no, (void *)ad);
301
302                 evas_object_show(ad->popup);
303                 break;
304
305         case MH_POP_WIFI_OFF_CONF:
306                 ad->popup = elm_popup_add(ad->win);
307                 evas_object_size_hint_weight_set(ad->popup,
308                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
309
310                 elm_object_text_set(ad->popup, ad->popup_string);
311
312                 btn = elm_button_add(ad->popup);
313                 elm_object_style_set(btn, "popup_button/default");
314                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
315                 elm_object_part_content_set(ad->popup, "button1", btn);
316                 evas_object_smart_callback_add(btn, "clicked",
317                                 __popup_resp_yes, (void *)ad);
318
319                 btn = elm_button_add(ad->popup);
320                 elm_object_style_set(btn, "popup_button/default");
321                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
322                 elm_object_part_content_set(ad->popup, "button2", btn);
323                 evas_object_smart_callback_add(btn, "clicked",
324                                 __popup_resp_no, (void *)ad);
325
326                 evas_object_show(ad->popup);
327                 break;
328
329         case MH_POP_USB_CONNECT:
330                 ad->popup = elm_popup_add(ad->win);
331                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
332                                 EVAS_HINT_EXPAND);
333
334                 elm_object_text_set(ad->popup, ad->popup_string);
335
336                 btn = elm_button_add(ad->popup);
337                 elm_object_style_set(btn, "popup_button/default");
338                 elm_object_text_set(btn, S_("IDS_COM_SK_CANCEL"));
339                 elm_object_part_content_set(ad->popup, "button1", btn);
340                 evas_object_smart_callback_add(btn, "clicked",
341                                 __one_btn_popup_resp, (void *)ad);
342
343                 evas_object_show(ad->popup);
344                 break;
345
346         case MH_POP_BT_ON_CONF:
347                 ad->popup = elm_popup_add(ad->win);
348                 evas_object_size_hint_weight_set(ad->popup,
349                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
350
351                 elm_object_text_set(ad->popup, ad->popup_string);
352
353                 btn = elm_button_add(ad->popup);
354                 elm_object_style_set(btn, "popup_button/default");
355                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
356                 elm_object_part_content_set(ad->popup, "button1", btn);
357                 evas_object_smart_callback_add(btn, "clicked",
358                                 __popup_resp_yes, (void *)ad);
359
360                 btn = elm_button_add(ad->popup);
361                 elm_object_style_set(btn, "popup_button/default");
362                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
363                 elm_object_part_content_set(ad->popup, "button2", btn);
364                 evas_object_smart_callback_add(btn, "clicked",
365                                 __popup_resp_no, (void *)ad);
366
367                 evas_object_show(ad->popup);
368                 break;
369
370         case MH_POP_USB_ON_CONF:
371                 ad->popup = elm_popup_add(ad->win);
372                 evas_object_size_hint_weight_set(ad->popup,
373                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
374
375                 elm_object_text_set(ad->popup, ad->popup_string);
376
377                 btn = elm_button_add(ad->popup);
378                 elm_object_style_set(btn, "popup_button/default");
379                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
380                 elm_object_part_content_set(ad->popup, "button1", btn);
381                 evas_object_smart_callback_add(btn, "clicked",
382                                 __popup_resp_yes, (void *)ad);
383
384                 btn = elm_button_add(ad->popup);
385                 elm_object_style_set(btn, "popup_button/default");
386                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
387                 elm_object_part_content_set(ad->popup, "button2", btn);
388                 evas_object_smart_callback_add(btn, "clicked",
389                                 __popup_resp_no, (void *)ad);
390
391                 evas_object_show(ad->popup);
392                 break;
393
394         case MH_POP_INFORMATION:
395                 ad->popup = elm_popup_add(ad->win);
396                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
397                                 EVAS_HINT_EXPAND);
398
399                 elm_object_text_set(ad->popup, ad->popup_string);
400
401                 btn = elm_button_add(ad->popup);
402                 elm_object_style_set(btn, "popup_button/default");
403                 elm_object_text_set(btn, S_("IDS_COM_POP_CLOSE"));
404                 elm_object_part_content_set(ad->popup, "button1", btn);
405                 evas_object_smart_callback_add(btn, "clicked",
406                                 __one_btn_popup_resp, (void *)ad);
407
408                 evas_object_show(ad->popup);
409                 break;
410
411         case MH_POP_WIFI_PASSWORD_SHORT:
412                 ad->popup = elm_popup_add(ad->win);
413                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
414                                 EVAS_HINT_EXPAND);
415
416                 elm_object_text_set(ad->popup, ad->popup_string);
417
418                 btn = elm_button_add(ad->popup);
419                 elm_object_style_set(btn, "popup_button/default");
420                 elm_object_text_set(btn, S_("IDS_COM_POP_CLOSE"));
421                 elm_object_part_content_set(ad->popup, "button1", btn);
422                 evas_object_smart_callback_add(btn, "clicked",
423                                 __one_btn_popup_resp, (void *)ad);
424
425                 evas_object_show(ad->popup);
426                 break;
427
428         case MH_POP_INFORMATION_WO_BUTTON:
429                 ad->popup = elm_popup_add(ad->win);
430                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
431                                 EVAS_HINT_EXPAND);
432
433                 elm_object_text_set(ad->popup, ad->popup_string);
434
435                 elm_popup_timeout_set(ad->popup, MH_POPUP_TIMEOUT);
436                 evas_object_smart_callback_add(ad->popup, "timeout",
437                                 __alert_popup_resp, (void *)ad);
438                 evas_object_smart_callback_add(ad->popup, "block,clicked",
439                                 __alert_popup_resp, (void *)ad);
440
441                 evas_object_show(ad->popup);
442                 break;
443
444         case MH_POP_ENTER_TO_WIFI_SETUP_CONF:
445                 ad->popup = elm_popup_add(ad->win);
446                 evas_object_size_hint_weight_set(ad->popup,
447                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
448
449                 elm_object_text_set(ad->popup, ad->popup_string);
450
451                 btn = elm_button_add(ad->popup);
452                 elm_object_style_set(btn, "popup_button/default");
453                 elm_object_text_set(btn, S_("IDS_COM_SK_OK"));
454                 elm_object_part_content_set(ad->popup, "button1", btn);
455                 evas_object_smart_callback_add(btn, "clicked",
456                                 __popup_resp_yes, (void *)ad);
457
458                 btn = elm_button_add(ad->popup);
459                 elm_object_style_set(btn, "popup_button/default");
460                 elm_object_text_set(btn, S_("IDS_COM_SK_CANCEL"));
461                 elm_object_part_content_set(ad->popup, "button2", btn);
462                 evas_object_smart_callback_add(btn, "clicked",
463                                 __popup_resp_no, (void *)ad);
464
465                 evas_object_show(ad->popup);
466                 break;
467         }
468
469         __MOBILE_AP_FUNC_EXIT__;
470
471         return TRUE;
472 }
473
474 void _destroy_popup(mh_appdata_t *ad)
475 {
476         __MOBILE_AP_FUNC_ENTER__;
477
478         if (ad == NULL) {
479                 ERR("ad is NULL\n");
480                 return;
481         }
482
483         if (ad->popup) {
484                 evas_object_del(ad->popup);
485                 ad->popup = NULL;
486         }
487
488         ad->popup_type = MH_POPUP_NONE;
489
490         __MOBILE_AP_FUNC_EXIT__;
491
492         return;
493 }
494
495 Evas_Object *_create_bg(Evas_Object *parent, const char *style)
496 {
497         __MOBILE_AP_FUNC_ENTER__;
498
499         if (parent == NULL || style == NULL) {
500                 ERR("The param is NULL\n");
501                 return NULL;
502         }
503
504         Evas_Object *bg = NULL;
505
506         bg = elm_bg_add(parent);
507         if (bg == NULL) {
508                 ERR("bg is NULL\n");
509                 return NULL;
510         }
511
512         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
513         elm_object_style_set(bg, style);
514         evas_object_show(bg);
515
516         return bg;
517 }
518
519 Evas_Object *_create_win_layout(mh_appdata_t *ad)
520 {
521         __MOBILE_AP_FUNC_ENTER__;
522
523         if (ad->win == NULL) {
524                 ERR("There is no main window\n");
525                 return NULL;
526         }
527
528         Evas_Object *layout;
529         Evas_Object *bg;
530
531         layout = elm_layout_add(ad->win);
532         if (layout == NULL) {
533                 ERR("layout is NULL\n");
534                 return NULL;
535         }
536
537         elm_layout_theme_set(layout, "layout", "application", "default");
538         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND,
539                         EVAS_HINT_EXPAND);
540
541         bg = _create_bg(layout, "group_list");
542         if (bg == NULL) {
543                 ERR("bg is NULL\n");
544                 evas_object_del(layout);
545                 return NULL;
546         }
547         elm_object_part_content_set(layout, "elm.swallow.bg", bg);
548
549         evas_object_show(layout);
550
551         ad->layout = layout;
552         ad->bg = bg;
553
554         __MOBILE_AP_FUNC_EXIT__;
555
556         return layout;
557 }
558
559 Evas_Object *_create_naviframe(Evas_Object *parent)
560 {
561         __MOBILE_AP_FUNC_ENTER__;
562
563         if (parent == NULL) {
564                 ERR("parent is NULL\n");
565                 return NULL;
566         }
567
568         Evas_Object *naviframe;
569
570         naviframe = elm_naviframe_add(parent);
571         if (naviframe == NULL) {
572                 ERR("naviframe is NULL\n");
573                 return NULL;
574         }
575         evas_object_show(naviframe);
576
577         __MOBILE_AP_FUNC_EXIT__;
578
579         return naviframe;
580 }
581
582 void _handle_usb_mode_change(keynode_t *key, void *data)
583 {
584         mh_appdata_t *ad = (mh_appdata_t *)data;
585         int vconf_key = 0;
586
587         if (!data) {
588                 ERR("The param is NULL\n");
589                 return;
590         }
591
592         if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
593                 ERR("Invalid vconf key\n");
594                 return;
595         }
596
597         vconf_key = vconf_keynode_get_int(key);
598         DBG("vconf_key : %d\n", vconf_key);
599         if (vconf_key == SETTING_USB_NONE_MODE) {
600                 DBG("usb-utilties is processing mode change\n");
601                 return;
602         }
603
604         vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
605                                 _handle_usb_mode_change);
606
607         evas_object_del(ad->popup);
608         ad->popup = NULL;
609
610         if (tethering_enable(ad->handle, TETHERING_TYPE_USB) != TETHERING_ERROR_NONE) {
611                 DBG("Error enable usb tethering\n");
612                 _update_usb_item(ad, MH_STATE_NONE);
613         }
614 }
615
616 Eina_Bool _hide_imf(Evas_Object *entry)
617 {
618         __MOBILE_AP_FUNC_ENTER__;
619
620         if (entry == NULL) {
621                 ERR("Invalid param\n");
622                 return EINA_FALSE;
623         }
624
625         Ecore_IMF_Context *context = NULL;
626
627         context = elm_entry_imf_context_get(entry);
628         if (context == NULL) {
629                 ERR("context is NULL\n");
630                 return EINA_FALSE;
631         }
632         ecore_imf_context_input_panel_hide(context);
633
634         elm_object_focus_set(entry, EINA_FALSE);
635
636         __MOBILE_AP_FUNC_EXIT__;
637
638         return EINA_TRUE;
639 }
640
641 int _get_vconf_hotspot_mode(void)
642 {
643         int value = VCONFKEY_MOBILE_HOTSPOT_MODE_NONE;
644
645         if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &value) < 0) {
646                 ERR("vconf_get_int is failed\n");
647                 return 0;
648         }
649
650         DBG("%s : %d\n", VCONFKEY_MOBILE_HOTSPOT_MODE, value);
651
652         return value;
653 }
654
655 Eina_Bool _get_no_of_connected_device(tethering_h handle, int *no, tethering_type_e type)
656 {
657         if (handle == NULL || no == NULL)
658                 return FALSE;
659
660         tethering_foreach_connected_clients(handle, type,
661                         _count_connected_clients_cb, (void *)no);
662
663         return TRUE;
664 }