6552181cbeb6d8aeb5a350a94b730bb06e8a8d5b
[apps/native/ug-mobile-ap.git] / src / mh_main_ug.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 #ifndef UG_MODULE_API
21 #define UG_MODULE_API __attribute__ ((visibility("default")))
22 #endif
23
24 #include "mobile_hotspot.h"
25 #include "mh_view_main.h"
26 #include "mh_func_onoff.h"
27 #include "mh_common_utility.h"
28 #include "mh_popup.h"
29 #include "mh_string.h"
30 #include "mh_view_wifi_setup.h"
31
32 static Evas_Object *create_content(mh_appdata_t *ad)
33 {
34         __MOBILE_AP_FUNC_ENTER__;
35
36         ad->naviframe = _create_naviframe(ad->layout);
37         _main_draw_contents(ad);
38
39         __MOBILE_AP_FUNC_EXIT__;
40
41         return ad->naviframe;
42 }
43
44 static void __set_callbacks(tethering_h handle, void *user_data)
45 {
46         DBG("+\n");
47
48         int ret;
49
50         ret = tethering_set_enabled_cb(handle, TETHERING_TYPE_ALL,
51                         _enabled_cb, user_data);
52         if (ret != TETHERING_ERROR_NONE)
53                 ERR("tethering_set_enabled_cb [0x%X]\n", ret);
54
55         ret = tethering_wifi_set_passphrase_changed_cb(handle,
56                         _passphrase_changed_cb, user_data);
57         if (ret != TETHERING_ERROR_NONE)
58                 ERR("tethering_wifi_set_passphrase_changed_cb [0x%X]\n", ret);
59
60         ret = tethering_wifi_set_ssid_visibility_changed_cb(handle,
61                         _visibility_changed_cb, user_data);
62         if (ret != TETHERING_ERROR_NONE)
63                 ERR("tethering_wifi_set_ssid_visibility_changed_cb [0x%X]\n", ret);
64
65         ret = tethering_wifi_set_security_type_changed_cb(handle,
66                         _security_type_changed_cb, user_data);
67         if (ret != TETHERING_ERROR_NONE)
68                 ERR("tethering_wifi_set_security_type_changed_cb [0x%X]\n", ret);
69
70         ret = tethering_set_disabled_cb(handle, TETHERING_TYPE_ALL,
71                         _disabled_cb, user_data);
72         if (ret != TETHERING_ERROR_NONE)
73                 ERR("tethering_set_disabled_cb [0x%X]\n", ret);
74
75         ret = tethering_set_connection_state_changed_cb(handle,
76                         TETHERING_TYPE_ALL,
77                         _connection_changed_cb, user_data);
78         if (ret != TETHERING_ERROR_NONE)
79                 ERR("tethering_set_connection_state_changed_cb [0x%X]\n", ret);
80
81         ret = wifi_set_device_state_changed_cb(_wifi_state_changed_cb, user_data);
82         if (ret != WIFI_ERROR_NONE)
83                 ERR("wifi_set_device_state_changed_cb [0x%X]\n", ret);
84         vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
85                                 _device_name_changed_cb, user_data);
86
87         vconf_notify_key_changed(VCONFKEY_NETWORK_CELLULAR_STATE,
88                         _handle_network_cellular_state_changed_cb, user_data);
89
90         vconf_notify_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
91                         _handle_usb_status_change, (void *)user_data);
92
93         vconf_notify_key_changed(VCONF_KEY_MOBILEAP_SYSPOPUP_RESPONSE,
94                         _handle_mobileap_syspopup_popup_response, (void *)user_data);
95
96         DBG("-\n");
97 }
98
99 static void __unset_callbacks(tethering_h handle)
100 {
101         DBG("+\n");
102
103         int ret;
104
105         ret = wifi_unset_device_state_changed_cb();
106         if (ret != WIFI_ERROR_NONE)
107                 ERR("wifi_unset_device_state_changed_cb [0x%X]\n", ret);
108
109         ret = tethering_unset_connection_state_changed_cb(handle,
110                         TETHERING_TYPE_ALL);
111         if (ret != TETHERING_ERROR_NONE)
112                 ERR("tethering_unset_connection_state_changed_cb[0x%X]\n", ret);
113
114         ret = tethering_unset_disabled_cb(handle, TETHERING_TYPE_ALL);
115         if (ret != TETHERING_ERROR_NONE)
116                 ERR("tethering_unset_disabled_cb [0x%X]\n", ret);
117
118         ret = tethering_wifi_unset_security_type_changed_cb(handle);
119         if (ret != TETHERING_ERROR_NONE)
120                 ERR("tethering_wifi_unset_security_type_changed_cb [0x%X]\n", ret);
121
122         ret = tethering_wifi_unset_ssid_visibility_changed_cb(handle);
123         if (ret != TETHERING_ERROR_NONE)
124                 ERR("tethering_wifi_unset_ssid_visibility_changed_cb [0x%X]\n", ret);
125
126         ret = tethering_wifi_unset_passphrase_changed_cb(handle);
127         if (ret != TETHERING_ERROR_NONE)
128                 ERR("tethering_wifi_unset_passphrase_changed_cb [0x%X]\n", ret);
129
130         ret = tethering_unset_enabled_cb(handle, TETHERING_TYPE_ALL);
131         if (ret != TETHERING_ERROR_NONE)
132                 ERR("tethering_unset_enabled [0x%X]\n", ret);
133
134         vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
135                         _device_name_changed_cb);
136
137         vconf_ignore_key_changed(VCONFKEY_NETWORK_CELLULAR_STATE,
138                         _handle_network_cellular_state_changed_cb);
139
140         vconf_ignore_key_changed(VCONFKEY_SYSMAN_USB_STATUS, _handle_usb_status_change);
141
142         vconf_ignore_key_changed(VCONF_KEY_MOBILEAP_SYSPOPUP_RESPONSE,
143                         _handle_mobileap_syspopup_popup_response);
144
145         DBG("-\n");
146 }
147
148 static void *on_create(ui_gadget_h ug, enum ug_mode mode,
149                 app_control_h app_control, void *priv)
150 {
151         DBG("+\n");
152
153         if (!ug || !priv) {
154                 ERR("The param is NULL\n");
155                 return NULL;
156         }
157
158         if (mode != UG_MODE_FULLVIEW) {
159                 ERR("Only Fullview is supported\n");
160                 return NULL;
161         }
162
163         Evas_Object *layout;
164         Evas_Object *content;
165         mh_ugdata_t *ugd;
166         mh_appdata_t *ad;
167         int ret;
168
169         bindtextdomain(MH_TEXT_DOMAIN, MH_LOCALEDIR);
170         dgettext(PACKAGE, LOCALEDIR);
171
172         ad = (mh_appdata_t *)malloc(sizeof(mh_appdata_t));
173         if (ad == NULL) {
174                 ERR("Error!!! failed to allocate memory()\n");
175                 return NULL;
176         }
177         memset(ad, 0x0, sizeof(mh_appdata_t));
178
179         ugd = (mh_ugdata_t *)priv;
180         ugd->ad = ad;
181         ugd->ug = ug;
182         ad->gadget = ugd;
183
184         _send_signal_qp(QP_SIGNAL_PROGRESS_RESET);
185
186         ecore_imf_init();
187
188         ret = tethering_create(&ad->handle);
189         if (ret != TETHERING_ERROR_NONE) {
190                 ERR("tethering_create is failed : %d\n", ret);
191                 free(ad);
192                 ugd->ad = NULL;
193                 return NULL;
194         }
195
196         ad->win = ug_get_parent_layout(ug);
197         if (!ad->win) {
198                 ERR("ad->win is NULL\n");
199                 free(ad);
200                 ugd->ad = NULL;
201                 return NULL;
202         }
203
204         /* set rotation */
205         if (elm_win_wm_rotation_supported_get(ad->win)) {
206                 int rots[4] = {0, 90, 180, 270};
207                 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
208         }
209
210         layout = _create_win_layout(ad);
211         if (layout == NULL) {
212                 ERR("_create_win_layout is failed\n");
213                 free(ad);
214                 ugd->ad = NULL;
215                 return NULL;
216         }
217
218         content = create_content(ad);
219         if (content == NULL) {
220                 ERR("create_content is failed\n");
221                 free(ad);
222                 ugd->ad = NULL;
223                 return NULL;
224         }
225
226         elm_object_part_content_set(layout, "elm.swallow.content", content);
227         evas_object_show(layout);
228         ret = connection_create(&ad->conn_handle);
229         if (ret != CONNECTION_ERROR_NONE)
230                 ERR("connection_create() is failed : %d\n", ret);
231
232         ret = wifi_initialize();
233         if (ret != WIFI_ERROR_NONE)
234                 ERR("wifi_initialize() is failed : %d\n", ret);
235
236         __set_callbacks(ad->handle, (void *)ad);
237
238         DBG("-\n");
239         return layout;
240 }
241
242 static void on_start(ui_gadget_h ug, app_control_h app_control, void *priv)
243 {
244         DBG("+\n");
245
246         char * viewtype = NULL;
247
248         mh_ugdata_t *ugd = (mh_ugdata_t *)priv;
249         mh_appdata_t *ad = ugd->ad;
250
251         ad->is_foreground = true;
252
253         if (_get_list_clients_count(ad) > 0) {
254 #ifdef TETHERING_DATA_USAGE_SUPPORT
255                 _start_update_data_packet_usage(ad);
256 #endif
257                 if (ad->navi_item)
258                         _start_update_device_conn_time(ad);
259         }
260
261         app_control_get_extra_data(app_control, "viewtype", &viewtype);
262
263         if (viewtype != NULL) {
264                 if (strcmp(viewtype, "wifisettings") == 0)
265                         mh_draw_wifi_setup_view(ad);
266                 g_free(viewtype);
267         }
268
269         DBG("-\n");
270         return;
271 }
272
273 static void on_pause(ui_gadget_h ug, app_control_h app_control, void *priv)
274 {
275         DBG("+\n");
276
277         mh_ugdata_t *ugd = (mh_ugdata_t *)priv;
278         mh_appdata_t *ad = ugd->ad;
279
280 #ifdef TETHERING_DATA_USAGE_SUPPORT
281         _stop_update_data_packet_usage(ad);
282 #endif
283         _stop_update_device_conn_time(ad);
284         ad->is_foreground = false;
285
286         DBG("-\n");
287 }
288
289 static void on_resume(ui_gadget_h ug, app_control_h app_control, void *priv)
290 {
291         DBG("+\n");
292
293         mh_ugdata_t *ugd = (mh_ugdata_t *)priv;
294         mh_appdata_t *ad = ugd->ad;
295         Elm_Object_Item *item = ad->main.device_item;
296         GSList *l = NULL;
297
298         ad->is_foreground = true;
299
300         if (item && elm_genlist_item_expanded_get(item)) {
301                 for (l = ad->client_list; l != NULL; l = g_slist_next(l)) {
302                         item = elm_genlist_item_next_get(item);
303                         elm_genlist_item_fields_update(item, "elm.text", ELM_GENLIST_ITEM_FIELD_TEXT);
304                 }
305         }
306
307         if (_get_list_clients_count(ad) > 0) {
308 #ifdef TETHERING_DATA_USAGE_SUPPORT
309                 _start_update_data_packet_usage(ad);
310 #endif
311                 if (ad->navi_item)
312                         _start_update_device_conn_time(ad);
313         }
314         DBG("-\n");
315 }
316
317 static void on_destroy(ui_gadget_h ug, app_control_h app_control, void *priv)
318 {
319         DBG("+\n");
320
321         if (priv == NULL) {
322                 ERR("The param is NULL\n");
323                 return;
324         }
325
326         mh_ugdata_t *ugd = priv;
327         mh_appdata_t *ad = ugd->ad;
328         int ret = 0;
329
330         if (ad == NULL) {
331                 ERR("The param is NULL\n");
332                 return;
333         }
334
335         __unset_callbacks(ad->handle);
336
337 #ifdef TETHERING_DATA_USAGE_SUPPORT
338         _stop_update_data_packet_usage(ad);
339 #endif
340         _stop_update_device_conn_time(ad);
341
342         if (vconf_set_int(VCONF_MOBILE_AP_CONNECT_USB_POPUP_STATUS, 0) < 0)
343                 ERR("vconf_set_int is failed\n");
344
345         ret = wifi_deinitialize();
346         if (ret != WIFI_ERROR_NONE)
347                 ERR("wifi_deinitialize() is failed : %d\n", ret);
348
349         ret = connection_destroy(ad->conn_handle);
350         if (ret != CONNECTION_ERROR_NONE)
351                 ERR("connection_destroy() is failed : %d\n", ret);
352
353         ret = tethering_destroy(ad->handle);
354         if (ret != TETHERING_ERROR_NONE)
355                 ERR("tethering_destroy() is failed : %d\n", ret);
356
357         if (ad->layout == NULL) {
358                 ERR("ad->layout is NULL\n");
359                 free(ugd->ad);
360                 ugd->ad = NULL;
361                 return;
362         }
363
364         if (ad->popup) {
365                 evas_object_del(ad->popup);
366                 ad->popup = NULL;
367         }
368
369         if (ad->ps_recheck_timer_id > 0) {
370                 g_source_remove(ad->ps_recheck_timer_id);
371                 ad->ps_recheck_timer_id = 0;
372         }
373
374         evas_object_del(ad->bg);
375         ad->bg = NULL;
376
377         evas_object_del(ad->layout);
378         ad->layout = NULL;
379
380         _main_free_genlist_itc(ad);
381
382         ecore_imf_shutdown();
383
384         free(ugd->ad);
385         ugd->ad = NULL;
386
387         DBG("-\n");
388         return;
389 }
390
391 static void on_message(ui_gadget_h ug, app_control_h msg,
392                 app_control_h app_control, void *priv)
393 {
394 }
395
396 static void on_event(ui_gadget_h ug, enum ug_event event,
397                 app_control_h app_control, void *priv)
398 {
399         __MOBILE_AP_FUNC_ENTER__;
400
401         if (!ug || !priv) {
402                 ERR("The param is NULL\n");
403                 return;
404         }
405
406         switch (event) {
407         case UG_EVENT_LOW_MEMORY:
408                 break;
409         case UG_EVENT_LOW_BATTERY:
410                 break;
411         case UG_EVENT_LANG_CHANGE:
412                 break;
413         case UG_EVENT_ROTATE_PORTRAIT:
414         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
415                 DBG("UG_EVENT_ROTATE_PORTRAIT[_UPSIDEDOWN]\n");
416                 break;
417         case UG_EVENT_ROTATE_LANDSCAPE:
418         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
419                 DBG("UG_EVENT_ROTATE_LANDSCAPE[_UPSIDEDOWN]\n");
420                 break;
421         default:
422                 break;
423         }
424
425         __MOBILE_AP_FUNC_EXIT__;
426 }
427
428 static void on_key_event(ui_gadget_h ug, enum ug_key_event event,
429                 app_control_h app_control, void *priv)
430 {
431         __MOBILE_AP_FUNC_ENTER__;
432
433         if (priv == NULL || ug == NULL) {
434                 ERR("The param is NULL\n");
435                 return;
436         }
437
438         mh_ugdata_t *ugd = (mh_ugdata_t *)priv;
439         mh_appdata_t *ad = ugd->ad;
440
441         if (ad == NULL) {
442                 ERR("ad is NULL\n");
443                 return;
444         }
445
446         switch (event) {
447         case UG_KEY_EVENT_END:
448                 DBG("UG_KEY_EVENT_END is received : %p\n", ad->popup);
449                 if (NULL == ad->popup)
450                         ug_destroy_me(ug);
451                 else
452                         _destroy_popup(ad);
453                 break;
454
455         default:
456                 break;
457         }
458
459         __MOBILE_AP_FUNC_EXIT__;
460 }
461
462 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
463 {
464         DBG("+\n");
465
466         if (!ops) {
467                 ERR("The param is NULL\n");
468                 return -1;
469         }
470
471         mh_ugdata_t *ugd;
472         ugd = calloc(1, sizeof(mh_ugdata_t));
473         if (!ugd) {
474                 ERR("Quit : calloc failed(ugd)\n");
475                 return -1;
476         }
477
478         ops->create = on_create;
479         ops->start = on_start;
480         ops->pause = on_pause;
481         ops->resume = on_resume;
482         ops->destroy = on_destroy;
483         ops->message = on_message;
484         ops->event = on_event;
485         ops->key_event = on_key_event;
486         ops->priv = ugd;
487         ops->opt = UG_OPT_INDICATOR_ENABLE;
488
489         return 0;
490 }
491
492 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
493 {
494         DBG("+\n");
495
496         if (!ops) {
497                 ERR("The param is NULL\n");
498                 return;
499         }
500
501         mh_ugdata_t *ugd = (mh_ugdata_t *)ops->priv;
502
503         if (ugd)
504                 free(ugd);
505 }
506
507 static void __mh_reset_vconf(tethering_h handle)
508 {
509         int ret = 0;
510
511         ret = vconf_set_int(VCONF_MOBILE_AP_WIFI_POPUP_CHECKBOX_STATUS, 0);
512         if (ret != 0)
513                 ERR("vconf_set_int failed\n");
514
515         ret = vconf_set_int(VCONF_MOBILE_AP_BT_POPUP_CHECKBOX_STATUS, 0);
516         if (ret != 0)
517                 ERR("vconf_set_int failed\n");
518
519         ret = vconf_set_int(VCONF_MOBILE_AP_USB_POPUP_CHECKBOX_STATUS, 0);
520         if (ret != 0)
521                 ERR("vconf_set_int failed\n");
522
523         ret = vconf_set_int(VCONF_MOBILE_AP_PREV_WIFI_STATUS, 0);
524         if (ret != 0)
525                 ERR("vconf_set_int failed\n");
526
527         ret = vconf_set_int(VCONF_MOBILE_AP_CONNECT_USB_POPUP_STATUS, 0);
528         if (ret != 0)
529                 ERR("vconf_set_int is failed\n");
530
531         ret = tethering_wifi_set_ssid_visibility(handle, true);
532         if (ret != TETHERING_ERROR_NONE)
533                 ERR("tethering_wifi_set_ssid_visibility failed\n");
534
535         ret = tethering_wifi_set_security_type(handle, TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK);
536         if (ret != TETHERING_ERROR_NONE)
537                 ERR("tethering_wifi_set_security_type failed\n");
538
539         return;
540 }
541
542 UG_MODULE_API int setting_plugin_reset(app_control_h app_control, void *priv)
543 {
544         DBG("+\n");
545
546         int ret = -1;
547         tethering_h handle = NULL;
548
549         if (tethering_create(&handle) != TETHERING_ERROR_NONE) {
550                 ERR("tethering_create failed\n");
551                 return -1;
552         }
553
554         if (tethering_is_enabled(handle, TETHERING_TYPE_USB) ||
555                         tethering_is_enabled(handle, TETHERING_TYPE_WIFI) ||
556                         tethering_is_enabled(handle, TETHERING_TYPE_BT)) {
557                 ret = tethering_disable(handle, TETHERING_TYPE_ALL);
558                 if (ret != TETHERING_ERROR_NONE) {
559                         ERR("tethering_disable failed : %d\n", ret);
560                         tethering_destroy(handle);
561                         return -1;
562                 }
563
564         }
565
566         __mh_reset_vconf(handle);
567
568         tethering_destroy(handle);
569
570         return 0;
571 }