Initial code
[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                                 elm_object_item_disabled_set(ad->main.setup_item,
116                                                 EINA_FALSE);
117                         }
118                 } else if (_is_wifi_direct_on() == true) {
119                         if (_turn_off_wifi_direct(ad) != 0) {
120                                 ERR("_turn_off_wifi_direct is failed\n");
121                                 _update_wifi_item(ad, MH_STATE_NONE);
122                                 elm_object_item_disabled_set(ad->main.setup_item,
123                                                 EINA_FALSE);
124
125                         }
126                 } else {
127                         ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
128                         if (ret != TETHERING_ERROR_NONE) {
129                                 ERR("wifi tethering on is failed : %d\n", ret);
130                                 _update_wifi_item(ad, MH_STATE_NONE);
131                                 elm_object_item_disabled_set(ad->main.setup_item,
132                                                 EINA_FALSE);
133                         }
134                 }
135                 break;
136
137         case MH_POP_WIFI_OFF_CONF:
138                 ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
139                 if (ret != TETHERING_ERROR_NONE) {
140                         ERR("wifi tethering off is failed : %d\n", ret);
141                         _update_wifi_item(ad, MH_STATE_NONE);
142                         elm_object_item_disabled_set(ad->main.setup_item,
143                                         EINA_TRUE);
144                 }
145                 break;
146
147         case MH_POP_USB_ON_CONF:
148                 ret = tethering_enable(ad->handle, TETHERING_TYPE_USB);
149                 if (ret != TETHERING_ERROR_NONE) {
150                         ERR("Error enable usb tethering : %d\n", ret);
151                         elm_check_state_set(ad->main.usb_btn, EINA_FALSE);
152                         _update_usb_item(ad, MH_STATE_NONE);
153                 }
154                 break;
155
156         default:
157                 DBG("Unknown popup_type : %d\n", ad->popup_type);
158                 break;
159         }
160
161         __MOBILE_AP_FUNC_EXIT__;
162 }
163
164 static void __popup_resp_no(void *data, Evas_Object *obj, void *event_info)
165 {
166         __MOBILE_AP_FUNC_ENTER__;
167
168         if (data == NULL || obj == NULL) {
169                 ERR("The param is NULL\n");
170                 return;
171         }
172
173         mh_appdata_t *ad = (mh_appdata_t *)data;
174
175         evas_object_del(ad->popup);
176         ad->popup = NULL;
177
178         DBG("popup_type : %d\n", ad->popup_type);
179         switch (ad->popup_type) {
180         case MH_POP_WIFI_ON_CONF:
181                 _update_wifi_item(ad, MH_STATE_NONE);
182                 elm_object_item_disabled_set(ad->main.setup_item, EINA_FALSE);
183                 break;
184
185         case MH_POP_WIFI_OFF_CONF:
186                 _update_wifi_item(ad, MH_STATE_NONE);
187                 elm_object_item_disabled_set(ad->main.setup_item, EINA_TRUE);
188                 break;
189
190         case MH_POP_USB_ON_CONF:
191                 elm_check_state_set(ad->main.usb_btn, EINA_FALSE);
192                 _update_usb_item(ad, MH_STATE_NONE);
193                 break;
194
195         default:
196                 DBG("Unknown popup_type : %d\n", ad->popup_type);
197                 break;
198         }
199
200         __MOBILE_AP_FUNC_EXIT__;
201 }
202
203 void _prepare_popup(mh_appdata_t *ad, int type, const char *str)
204 {
205         __MOBILE_AP_FUNC_ENTER__;
206
207         if (ad == NULL || str == NULL) {
208                 ERR("param is NULL : ad[%x], str[%x]\n", ad, str);
209                 return;
210         }
211
212         int nLen = 0;
213
214         nLen = strlen(str);
215         if (sizeof(ad->popup_string) <= nLen)
216                 ERR("We should make the message[%s] less than %d",
217                                 str, sizeof(ad->popup_string));
218
219         ad->popup_type = type;
220         snprintf(ad->popup_string, sizeof(ad->popup_string), "%s", str);
221
222         __MOBILE_AP_FUNC_EXIT__;
223 }
224
225 Eina_Bool _create_popup(mh_appdata_t *ad)
226 {
227         __MOBILE_AP_FUNC_ENTER__;
228
229         Evas_Object *btn = NULL;
230
231         if (ad == NULL) {
232                 ERR("The param is NULL\n");
233                 return 0;
234         }
235
236         if (ad->popup != NULL) {
237                 DBG("Pop-up already exists. Delete it.\n");
238                 evas_object_del(ad->popup);
239                 ad->popup = NULL;
240         }
241
242         DBG("Create_popup %d\n", ad->popup_type);
243         switch (ad->popup_type) {
244         case MH_POP_WIFI_ON_CONF:
245                 ad->popup = elm_popup_add(ad->win);
246                 evas_object_size_hint_weight_set(ad->popup,
247                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
248
249                 elm_object_text_set(ad->popup, ad->popup_string);
250
251                 btn = elm_button_add(ad->popup);
252                 elm_object_style_set(btn, "popup_button/default");
253                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
254                 elm_object_part_content_set(ad->popup, "button1", btn);
255                 evas_object_smart_callback_add(btn, "clicked",
256                                 __popup_resp_yes, (void *)ad);
257
258                 btn = elm_button_add(ad->popup);
259                 elm_object_style_set(btn, "popup_button/default");
260                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
261                 elm_object_part_content_set(ad->popup, "button2", btn);
262                 evas_object_smart_callback_add(btn, "clicked",
263                                 __popup_resp_no, (void *)ad);
264
265                 evas_object_show(ad->popup);
266                 break;
267
268         case MH_POP_WIFI_OFF_CONF:
269                 ad->popup = elm_popup_add(ad->win);
270                 evas_object_size_hint_weight_set(ad->popup,
271                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
272
273                 elm_object_text_set(ad->popup, ad->popup_string);
274
275                 btn = elm_button_add(ad->popup);
276                 elm_object_style_set(btn, "popup_button/default");
277                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
278                 elm_object_part_content_set(ad->popup, "button1", btn);
279                 evas_object_smart_callback_add(btn, "clicked",
280                                 __popup_resp_yes, (void *)ad);
281
282                 btn = elm_button_add(ad->popup);
283                 elm_object_style_set(btn, "popup_button/default");
284                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
285                 elm_object_part_content_set(ad->popup, "button2", btn);
286                 evas_object_smart_callback_add(btn, "clicked",
287                                 __popup_resp_no, (void *)ad);
288
289                 evas_object_show(ad->popup);
290                 break;
291
292         case MH_POP_USB_CONNECT:
293                 ad->popup = elm_popup_add(ad->win);
294                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
295                                 EVAS_HINT_EXPAND);
296
297                 elm_object_text_set(ad->popup, ad->popup_string);
298
299                 btn = elm_button_add(ad->popup);
300                 elm_object_style_set(btn, "popup_button/default");
301                 elm_object_text_set(btn, S_("IDS_COM_SK_CANCEL"));
302                 elm_object_part_content_set(ad->popup, "button1", btn);
303                 evas_object_smart_callback_add(btn, "clicked",
304                                 __one_btn_popup_resp, (void *)ad);
305
306                 evas_object_show(ad->popup);
307                 break;
308
309         case MH_POP_USB_ON_CONF:
310                 ad->popup = elm_popup_add(ad->win);
311                 evas_object_size_hint_weight_set(ad->popup,
312                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
313
314                 elm_object_text_set(ad->popup, ad->popup_string);
315
316                 btn = elm_button_add(ad->popup);
317                 elm_object_style_set(btn, "popup_button/default");
318                 elm_object_text_set(btn, S_("IDS_COM_SK_YES"));
319                 elm_object_part_content_set(ad->popup, "button1", btn);
320                 evas_object_smart_callback_add(btn, "clicked",
321                                 __popup_resp_yes, (void *)ad);
322
323                 btn = elm_button_add(ad->popup);
324                 elm_object_style_set(btn, "popup_button/default");
325                 elm_object_text_set(btn, S_("IDS_COM_SK_NO"));
326                 elm_object_part_content_set(ad->popup, "button2", btn);
327                 evas_object_smart_callback_add(btn, "clicked",
328                                 __popup_resp_no, (void *)ad);
329
330                 evas_object_show(ad->popup);
331                 break;
332
333         case MH_POP_INFORMATION:
334                 ad->popup = elm_popup_add(ad->win);
335                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
336                                 EVAS_HINT_EXPAND);
337
338                 elm_object_text_set(ad->popup, ad->popup_string);
339
340                 btn = elm_button_add(ad->popup);
341                 elm_object_style_set(btn, "popup_button/default");
342                 elm_object_text_set(btn, S_("IDS_COM_POP_CLOSE"));
343                 elm_object_part_content_set(ad->popup, "button1", btn);
344                 evas_object_smart_callback_add(btn, "clicked",
345                                 __one_btn_popup_resp, (void *)ad);
346
347                 evas_object_show(ad->popup);
348                 break;
349
350         case MH_POP_WIFI_PASSWORD_SHORT:
351                 ad->popup = elm_popup_add(ad->win);
352                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
353                                 EVAS_HINT_EXPAND);
354
355                 elm_object_text_set(ad->popup, ad->popup_string);
356
357                 btn = elm_button_add(ad->popup);
358                 elm_object_style_set(btn, "popup_button/default");
359                 elm_object_text_set(btn, S_("IDS_COM_POP_CLOSE"));
360                 elm_object_part_content_set(ad->popup, "button1", btn);
361                 evas_object_smart_callback_add(btn, "clicked",
362                                 __one_btn_popup_resp, (void *)ad);
363
364                 evas_object_show(ad->popup);
365                 break;
366
367         case MH_POP_INFORMATION_WO_BUTTON:
368                 ad->popup = elm_popup_add(ad->win);
369                 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND,
370                                 EVAS_HINT_EXPAND);
371
372                 elm_object_text_set(ad->popup, ad->popup_string);
373
374                 elm_popup_timeout_set(ad->popup, MH_POPUP_TIMEOUT);
375                 evas_object_smart_callback_add(ad->popup, "timeout",
376                                 __alert_popup_resp, (void *)ad);
377                 evas_object_smart_callback_add(ad->popup, "block,clicked",
378                                 __alert_popup_resp, (void *)ad);
379
380                 evas_object_show(ad->popup);
381                 break;
382         }
383
384         __MOBILE_AP_FUNC_EXIT__;
385
386         return TRUE;
387 }
388
389 void _destroy_popup(mh_appdata_t *ad)
390 {
391         __MOBILE_AP_FUNC_ENTER__;
392
393         if (ad == NULL) {
394                 ERR("ad is NULL\n");
395                 return;
396         }
397
398         if (ad->popup) {
399                 evas_object_del(ad->popup);
400                 ad->popup = NULL;
401         }
402
403         ad->popup_type = MH_POPUP_NONE;
404
405         __MOBILE_AP_FUNC_EXIT__;
406
407         return;
408 }
409
410 Evas_Object *_create_bg(Evas_Object *parent, const char *style)
411 {
412         __MOBILE_AP_FUNC_ENTER__;
413
414         if (parent == NULL || style == NULL) {
415                 ERR("The param is NULL\n");
416                 return NULL;
417         }
418
419         Evas_Object *bg = NULL;
420
421         bg = elm_bg_add(parent);
422         if (bg == NULL) {
423                 ERR("bg is NULL\n");
424                 return NULL;
425         }
426
427         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
428         elm_object_style_set(bg, style);
429
430         return bg;
431 }
432
433 Evas_Object *_create_layout(Evas_Object *parent)
434 {
435         __MOBILE_AP_FUNC_ENTER__;
436
437         if (parent == NULL) {
438                 ERR("The param is NULL\n");
439                 return NULL;
440         }
441
442         Evas_Object *layout = NULL;
443
444         layout = elm_layout_add(parent);
445         if (layout == NULL) {
446                 ERR("layout is NULL\n");
447                 return NULL;
448         }
449
450         elm_layout_theme_set(layout, "layout", "application", "default");
451         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND,
452                         EVAS_HINT_EXPAND);
453
454         evas_object_show(layout);
455
456         __MOBILE_AP_FUNC_EXIT__;
457
458         return layout;
459 }
460
461 Evas_Object *_create_naviframe(Evas_Object *parent)
462 {
463         __MOBILE_AP_FUNC_ENTER__;
464
465         if (parent == NULL) {
466                 ERR("parent is NULL\n");
467                 return NULL;
468         }
469
470         Evas_Object *naviframe;
471
472         naviframe = elm_naviframe_add(parent);
473         if (naviframe == NULL) {
474                 ERR("naviframe is NULL\n");
475                 return NULL;
476         }
477         evas_object_show(naviframe);
478
479         __MOBILE_AP_FUNC_EXIT__;
480
481         return naviframe;
482 }
483
484 void _handle_usb_mode_change(keynode_t *key, void *data)
485 {
486         mh_appdata_t *ad = (mh_appdata_t *)data;
487         int vconf_key = 0;
488
489         if (!data) {
490                 ERR("The param is NULL\n");
491                 return;
492         }
493
494         if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
495                 ERR("Invalid vconf key\n");
496                 return;
497         }
498
499         vconf_key = vconf_keynode_get_int(key);
500         DBG("vconf_key : %d\n", vconf_key);
501         if (vconf_key == SETTING_USB_NONE_MODE) {
502                 DBG("usb-utilties is processing mode change\n");
503                 return;
504         }
505
506         vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
507                                 _handle_usb_mode_change);
508
509         evas_object_del(ad->popup);
510         ad->popup = NULL;
511
512         if (tethering_enable(ad->handle, TETHERING_TYPE_USB) != TETHERING_ERROR_NONE) {
513                 DBG("Error enable usb tethering\n");
514                 elm_check_state_set(ad->main.usb_btn, EINA_FALSE);
515                 _update_usb_item(ad, MH_STATE_NONE);
516         }
517 }
518
519 Eina_Bool _hide_imf(Evas_Object *entry)
520 {
521         __MOBILE_AP_FUNC_ENTER__;
522
523         if (entry == NULL) {
524                 ERR("Invalid param\n");
525                 return EINA_FALSE;
526         }
527
528         Ecore_IMF_Context *context = NULL;
529
530         context = elm_entry_imf_context_get(entry);
531         if (context == NULL) {
532                 ERR("context is NULL\n");
533                 return EINA_FALSE;
534         }
535         ecore_imf_context_input_panel_hide(context);
536
537         elm_object_focus_set(entry, EINA_FALSE);
538
539         __MOBILE_AP_FUNC_EXIT__;
540
541         return EINA_TRUE;
542 }
543
544 int _get_vconf_hotspot_mode(void)
545 {
546         int value = VCONFKEY_MOBILE_HOTSPOT_MODE_NONE;
547
548         if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &value) < 0) {
549                 ERR("vconf_get_int is failed\n");
550                 return 0;
551         }
552
553         DBG("%s : %d\n", VCONFKEY_MOBILE_HOTSPOT_MODE, value);
554
555         return value;
556 }
557