b94a65a8e57c7db2e6d34603e4c3502a4ad1315b
[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 <net_connection.h>
21 #include <dbus/dbus.h>
22 #include <gio/gio.h>
23
24 #include "mh_common_utility.h"
25 #include "mobile_hotspot.h"
26
27 void _handle_network_cellular_state_changed_cb(keynode_t *key, void *data)
28 {
29         if (key == NULL || data == NULL) {
30                 ERR("Parameter is NULL\n");
31                 return;
32         }
33         mh_appdata_t *ad = (mh_appdata_t *)data;
34         int vconf_key = 0;
35
36         if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
37                 ERR("Invalid vconf key type\n");
38                 return;
39         }
40
41         vconf_key = vconf_keynode_get_int(key);
42         SDBG("key = %s, value = %d(int)\n",
43                         vconf_keynode_get_name(key), vconf_key);
44
45         if (vconf_key != VCONFKEY_NETWORK_CELLULAR_FLIGHT_MODE)
46                 return;
47
48         if (ad->popup) {
49                 evas_object_del(ad->popup);
50                 ad->popup = NULL;
51         }
52         if (ad->is_wifi_teth_enabling) {
53                 _update_wifi_item(ad, MH_STATE_NONE);
54                 ad->is_wifi_teth_enabling = false;
55         }
56         if (ad->is_bt_teth_enabling) {
57                 _update_bt_item(ad, MH_STATE_NONE);
58                 ad->is_bt_teth_enabling = false;
59         }
60         if (ad->is_usb_teth_enabling) {
61                 _update_usb_item(ad, MH_STATE_NONE);
62                 ad->is_usb_teth_enabling = false;
63         }
64         return;
65 }
66
67 void _device_name_changed_cb(keynode_t *key, void *data)
68 {
69         if (key == NULL || data == NULL) {
70                 ERR("Parameter is NULL\n");
71                 return;
72         }
73
74         mh_appdata_t *ad = (mh_appdata_t *)data;
75         char *dev_name = NULL;
76
77         if (vconf_keynode_get_type(key) != VCONF_TYPE_STRING) {
78                 ERR("Invalid vconf key type\n");
79                 return;
80         }
81         dev_name = vconf_keynode_get_str(key);
82         if (ad->setup.name_item != NULL)
83                 elm_genlist_item_update(ad->setup.name_item);
84
85         if (ad->main.help_item != NULL)
86                 elm_genlist_item_update(ad->main.help_item);
87
88         if (ad->rename_popup) {
89                 elm_entry_entry_set(ad->rename_entry, dev_name);
90                 elm_entry_cursor_end_set(ad->rename_entry);
91         }
92         if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
93                 _update_wifi_item(ad, MH_STATE_PROCESS);
94
95         return;
96 }
97
98 Evas_Object *_create_progressbar(Evas_Object *parent, const char *style)
99 {
100         if (parent == NULL || style == NULL) {
101                 ERR("Invalid param\n");
102                 return NULL;
103         }
104
105         Evas_Object *progressbar;
106
107         progressbar = elm_progressbar_add(parent);
108         if (progressbar == NULL) {
109                 ERR("progressbar is NULL\n");
110                 return NULL;
111         }
112
113         elm_object_style_set(progressbar, style);
114         elm_progressbar_horizontal_set(progressbar, EINA_TRUE);
115         elm_progressbar_pulse(progressbar, EINA_TRUE);
116         evas_object_size_hint_align_set(progressbar, EVAS_HINT_FILL, EVAS_HINT_FILL);
117         evas_object_size_hint_weight_set(progressbar, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
118         evas_object_show(progressbar);
119
120         return progressbar;
121 }
122
123 Evas_Object *_create_bg(Evas_Object *parent, const char *style)
124 {
125         __MOBILE_AP_FUNC_ENTER__;
126
127         if (parent == NULL || style == NULL) {
128                 ERR("The param is NULL\n");
129                 return NULL;
130         }
131
132         Evas_Object *bg = NULL;
133
134         bg = elm_bg_add(parent);
135         if (bg == NULL) {
136                 ERR("bg is NULL\n");
137                 return NULL;
138         }
139
140         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
141         elm_object_style_set(bg, style);
142         evas_object_show(bg);
143
144         return bg;
145 }
146
147 Evas_Object *_create_win_layout(mh_appdata_t *ad)
148 {
149         __MOBILE_AP_FUNC_ENTER__;
150
151         if (ad->win == NULL) {
152                 ERR("There is no main window\n");
153                 return NULL;
154         }
155
156         Evas_Object *layout;
157         Evas_Object *bg;
158
159         layout = elm_layout_add(ad->win);
160         if (layout == NULL) {
161                 ERR("layout is NULL\n");
162                 return NULL;
163         }
164
165         elm_layout_theme_set(layout, "layout", "application", "default");
166         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND,
167                         EVAS_HINT_EXPAND);
168
169         bg = _create_bg(layout, "group_list");
170         if (bg == NULL) {
171                 ERR("bg is NULL\n");
172                 evas_object_del(layout);
173                 return NULL;
174         }
175         elm_object_part_content_set(layout, "elm.swallow.bg", bg);
176
177         evas_object_show(layout);
178
179         ad->layout = layout;
180         ad->bg = bg;
181
182         __MOBILE_AP_FUNC_EXIT__;
183
184         return layout;
185 }
186
187 Evas_Object *_create_naviframe(Evas_Object *parent)
188 {
189         __MOBILE_AP_FUNC_ENTER__;
190
191         if (parent == NULL) {
192                 ERR("parent is NULL\n");
193                 return NULL;
194         }
195
196         Evas_Object *naviframe;
197
198         naviframe = elm_naviframe_add(parent);
199         if (naviframe == NULL) {
200                 ERR("naviframe is NULL\n");
201                 return NULL;
202         }
203         evas_object_show(naviframe);
204
205         __MOBILE_AP_FUNC_EXIT__;
206
207         return naviframe;
208 }
209
210 Evas_Object *_create_button(Evas_Object *parent, const char *text, const char *part,
211                 Evas_Smart_Cb func, void *user_data)
212 {
213         if (parent == NULL || text == NULL || part == NULL || func == NULL) {
214                 ERR("Invalid param\n");
215                 return NULL;
216         }
217
218         Evas_Object *btn;
219
220         btn = elm_button_add(parent);
221         elm_object_style_set(btn, "popup");
222         elm_object_domain_translatable_text_set(btn, PACKAGE, text);
223         elm_object_part_content_set(parent, part, btn);
224         evas_object_smart_callback_add(btn, "clicked", func, user_data);
225         evas_object_show(btn);
226
227         return btn;
228 }
229
230 void _handle_mobileap_syspopup_popup_response(keynode_t *key, void *data)
231 {
232         if (!data) {
233                 ERR("The param is NULL\n");
234                 return;
235         }
236
237         if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
238                 ERR("Invalid vconf key\n");
239                 return;
240         }
241
242         mh_appdata_t *ad = (mh_appdata_t *)data;
243         int vconf_key = 0;
244
245         vconf_key = vconf_keynode_get_int(key);
246
247         if (vconf_key) {
248                 if (vconf_set_int(VCONF_KEY_MOBILEAP_SYSPOPUP_RESPONSE, 0) < 0)
249                         ERR("vconf_set_int is failed\n");
250
251                 if (ad->type != TETHERING_TYPE_WIFI) {
252                         DBG("no need to handle user response\n");
253                         return;
254                 }
255                 if (ad->popup) {
256                         evas_object_del(ad->popup);
257                         ad->popup = NULL;
258                 }
259                 if (ad->popup_checkbox) {
260                         evas_object_del(ad->popup_checkbox);
261                         ad->popup_checkbox = NULL;
262                 }
263                 if (vconf_key == 1)
264                         _update_wifi_item(ad, MH_STATE_NONE);
265         }
266 }
267
268 void _handle_usb_status_change(keynode_t *key, void *data)
269 {
270         int vconf_key = 0;
271
272         if (!data) {
273                 ERR("The param is NULL\n");
274                 return;
275         }
276
277         mh_appdata_t *ad = (mh_appdata_t *)data;
278
279         if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
280                 ERR("Invalid vconf key\n");
281                 return;
282         }
283
284         vconf_key = vconf_keynode_get_int(key);
285         if (vconf_key == SETTING_USB_NONE_MODE)
286                 return;
287
288         if (vconf_key != VCONFKEY_SYSMAN_USB_AVAILABLE) {
289                 if (ad->type == TETHERING_TYPE_USB && ad->popup) {
290                         evas_object_del(ad->popup);
291                         ad->popup = NULL;
292                         _update_tethering_item(ad, MH_STATE_NONE);
293                 }
294                 if (ad->main.usb_item)
295                         elm_object_item_disabled_set(ad->main.usb_item, EINA_TRUE);
296         } else {
297                 if (ad->main.usb_item)
298                         elm_object_item_disabled_set(ad->main.usb_item, EINA_FALSE);
299         }
300 }
301
302 int _get_vconf_hotspot_mode(void)
303 {
304         int value = VCONFKEY_MOBILE_HOTSPOT_MODE_NONE;
305
306         if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &value) < 0) {
307                 ERR("vconf_get_int is failed\n");
308                 return 0;
309         }
310         DBG("%s : %d\n", VCONFKEY_MOBILE_HOTSPOT_MODE, value);
311
312         return value;
313 }
314
315 Eina_Bool _get_no_of_connected_device(mh_appdata_t *ad, int *no, tethering_type_e type)
316 {
317         if (ad == NULL) {
318                 ERR("Invalid param\n");
319                 return EINA_FALSE;
320         }
321
322         if (ad->client_list == NULL) {
323                 *no = 0;
324                 return EINA_TRUE;
325         }
326
327         GSList *l = NULL;
328         tethering_client_h handle;
329         tethering_type_e type2;
330         tethering_error_e ret;
331         int count = 0;
332
333         for (l = ad->client_list; l != NULL; l = g_slist_next(l)) {
334                 handle = l->data;
335                 if (handle == NULL)
336                         continue;
337
338                 ret = tethering_client_get_tethering_type(handle, &type2);
339                 if (ret != TETHERING_ERROR_NONE) {
340                         ERR("tethering_client_get_tethering_type is failed [0x%X]\n", ret);
341                         continue;
342                 }
343
344                 if (type != TETHERING_TYPE_ALL && type != type2)
345                         continue;
346
347                 count++;
348         }
349         *no = count;
350
351         return EINA_TRUE;
352 }
353
354 void _append_list_client_handle(mh_appdata_t *ad, tethering_client_h client)
355 {
356         if (ad == NULL || client == NULL) {
357                 ERR("Invalid param\n");
358                 return;
359         }
360
361         int ret;
362         tethering_client_h handle;
363
364         ret = tethering_client_clone(&handle, client);
365         if (ret != TETHERING_ERROR_NONE) {
366                 ERR("Unable to clone client handle");
367                 return;
368         }
369
370         ad->client_list = g_slist_append(ad->client_list, handle);
371
372         return;
373 }
374
375 void _release_list_client_handle(mh_appdata_t *ad)
376 {
377         if (ad == NULL) {
378                 ERR("Invalid param\n");
379                 return;
380         }
381
382         if (ad->client_list == NULL)
383                 return;
384
385         GSList *l = NULL;
386         tethering_client_h handle;
387
388         for (l = ad->client_list; l != NULL; l = g_slist_next(l)) {
389                 handle = l->data;
390                 if (handle == NULL)
391                         continue;
392
393                 tethering_client_destroy(handle);
394         }
395
396         g_slist_free(ad->client_list);
397         ad->client_list = NULL;
398
399         return;
400 }
401
402 static int __find_mac_address(gconstpointer a, gconstpointer b)
403 {
404         tethering_client_h handle = (tethering_client_h)a;
405         const char *udn = (const char *)b;
406         char *mac_addr;
407         int ret;
408
409         ret = tethering_client_get_mac_address(handle, &mac_addr);
410         if (ret != TETHERING_ERROR_NONE)
411                 return -1;
412
413         ret = g_ascii_strcasecmp(mac_addr, udn);
414
415         if (mac_addr)
416                 free(mac_addr);
417
418         return ret;
419 }
420
421 void _delete_list_client_handle(mh_appdata_t *ad, const char *mac_addr)
422 {
423         if (ad == NULL || mac_addr == NULL) {
424                 ERR("Invalid param\n");
425                 return;
426         }
427
428         GSList *l = NULL;
429         tethering_client_h handle = NULL;
430
431         l = g_slist_find_custom(ad->client_list, mac_addr, __find_mac_address);
432         if (!l) {
433                 ERR("Not found\n");
434                 return;
435         }
436
437         handle = (tethering_client_h)l->data;
438         ad->client_list = g_slist_delete_link(ad->client_list, l);
439         if (handle == NULL)
440                 return;
441
442         tethering_client_destroy(handle);
443         return;
444 }
445
446 int _get_list_clients_count(mh_appdata_t *ad)
447 {
448         int client_list_counts;
449
450         if (ad == NULL) {
451                 ERR("Invalid param\n");
452                 return 0;
453         }
454
455         client_list_counts = g_slist_length(ad->client_list);
456         if (client_list_counts > TETHERING_WIFI_MAX_CONNECTED_STA) {
457                 INFO("client_list_counts = [%d]\n", client_list_counts);
458                 return TETHERING_WIFI_MAX_CONNECTED_STA;
459         } else
460                 return client_list_counts;
461 }
462
463 void _get_list_clients_informations(mh_appdata_t *ad)
464 {
465         if (ad == NULL) {
466                 ERR("Invalid param\n");
467                 return;
468         }
469
470         GSList *l = NULL;
471         tethering_client_h handle;
472         int i = 1;
473         int ret;
474         tethering_type_e type = 0;
475         char *name = NULL;
476         char *mac_addr = NULL;
477
478         for (l = ad->client_list; l != NULL; l = g_slist_next(l), i++) {
479                 handle = (tethering_client_h)l->data;
480
481                 ret = tethering_client_get_name(handle, &name);
482                 if (ret != TETHERING_ERROR_NONE)
483                         ERR("tethering_client_get_name is failed : %d\n", ret);
484
485                 ret = tethering_client_get_mac_address(handle, &mac_addr);
486                 if (ret != TETHERING_ERROR_NONE)
487                         ERR("tethering_client_get_mac_address is failed : %d\n", ret);
488
489                 ret = tethering_client_get_tethering_type(handle, &type);
490                 if (ret != TETHERING_ERROR_NONE)
491                         ERR("tethering_client_get_tethering_type is failed : %d\n", ret);
492                 if (name) {
493                         SDBG("Client[%d] : %s\n", i, name);
494                         free(name);
495                         name = NULL;
496                 }
497
498                 if (mac_addr) {
499                         SDBG("MAC[%d] : %s\n", i, mac_addr);
500                         free(mac_addr);
501                         mac_addr = NULL;
502                 }
503
504                 SDBG("Type[%d] : %d\n", i, type);
505         }
506
507         return;
508 }
509
510 void _free_genlist_item(Elm_Object_Item **item)
511 {
512         if (*item == NULL)
513                 return;
514
515         elm_object_item_del(*item);
516         *item = NULL;
517 }
518
519 void _free_genlist_itc(Elm_Genlist_Item_Class **itc)
520 {
521         if (*itc == NULL)
522                 return;
523
524         elm_genlist_item_class_free(*itc);
525         *itc = NULL;
526 }
527
528 int _get_sim_state(void)
529 {
530         int value = 0;
531
532         if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &value) < 0)
533                 ERR("vconf_get_int is failed\n");
534
535         DBG("%s : %d\n", VCONFKEY_TELEPHONY_SIM_SLOT, value);
536         if (value == VCONFKEY_TELEPHONY_SIM_INSERTED)
537                 return value;
538
539 #if defined TIZEN_FEATURE_DUALSIM_ENABLE
540         if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT2, &value) < 0) {
541                 ERR("vconf_get_int is failed\n");;
542         }
543         DBG("%s : %d\n", VCONFKEY_TELEPHONY_SIM_SLOT2, value);
544 #endif
545
546         return value;
547 }
548
549 connection_cellular_state_e _get_cellular_state(void)
550 {
551         int ret;
552         connection_h handle = NULL;
553         connection_cellular_state_e cellular_state;
554
555         ret = connection_create(&handle);
556         if (ret != CONNECTION_ERROR_NONE) {
557                 ERR("Connection create failed");
558                 return CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
559         }
560         ret = connection_get_cellular_state(handle, &cellular_state);
561         if (ret != CONNECTION_ERROR_NONE) {
562                 ERR("connection_get_cellular_state() is failed : %d\n");
563                 connection_destroy(handle);
564                 return CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
565         }
566
567         connection_destroy(handle);
568         return cellular_state;
569 }
570
571 int _get_checkbox_status(tethering_type_e type)
572 {
573         int value = 0;
574         char *vconf_key = NULL;
575
576         if (type == TETHERING_TYPE_WIFI)
577                 vconf_key = VCONF_MOBILE_AP_WIFI_POPUP_CHECKBOX_STATUS;
578         else if (type == TETHERING_TYPE_BT)
579                 vconf_key = VCONF_MOBILE_AP_BT_POPUP_CHECKBOX_STATUS;
580         else if (type == TETHERING_TYPE_USB)
581                 vconf_key = VCONF_MOBILE_AP_USB_POPUP_CHECKBOX_STATUS;
582         else
583                 return 0;
584         if (vconf_get_int(vconf_key, &value) < 0) {
585                 ERR("vconf_get_int() is failed\n");
586                 return 0;
587         }
588         DBG("%s : %d\n", vconf_key, value);
589         return value;
590 }
591
592 bool _set_checkbox_status(tethering_type_e type, int value)
593 {
594         char *vconf_key = NULL;
595
596         if (type == TETHERING_TYPE_WIFI)
597                 vconf_key = VCONF_MOBILE_AP_WIFI_POPUP_CHECKBOX_STATUS;
598         else if (type == TETHERING_TYPE_BT)
599                 vconf_key = VCONF_MOBILE_AP_BT_POPUP_CHECKBOX_STATUS;
600         else if (type == TETHERING_TYPE_USB)
601                 vconf_key = VCONF_MOBILE_AP_USB_POPUP_CHECKBOX_STATUS;
602         else
603                 return false;
604
605         if (vconf_set_int(vconf_key, value) < 0) {
606                 ERR("vconf_set_int is failed\n");
607                 return false;
608         }
609         return true;
610 }
611
612 bool _set_vconf_prev_wifi_state(bool value)
613 {
614         if (vconf_set_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, value) < 0) {
615                 ERR("vconf_set_bool failed\n");
616                 return false;
617         }
618         return true;
619 }
620
621 bool _get_vconf_prev_wifi_state()
622 {
623         int value = 0;
624
625         if (vconf_get_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, &value) < 0) {
626                 ERR("vconf_get_bool is failed\n");
627                 return false;
628         }
629         DBG("%s : %d\n", VCONF_MOBILE_AP_PREV_WIFI_STATUS, value);
630
631         return value ? true : false;
632 }
633
634 int _send_signal_qp(const char *cmd)
635 {
636         DBG("+\n");
637
638         if (cmd == NULL) {
639                 ERR("Invalid param");
640                 return -1;
641         }
642
643         GDBusConnection *conn = NULL;
644         int ret = 0;
645         GVariant *message = NULL;
646         GError *err = NULL;
647
648         DBG("Sent dbus signal : %s\n", cmd);
649
650         conn = g_bus_get_sync(DBUS_BUS_SYSTEM, NULL, &err);
651         if (err != NULL) {
652                 ERR("Failed connection to system bus[%s]", err->message);
653                 g_error_free(err);
654                 err = NULL;
655                 return -1;
656         }
657         message = g_variant_new("(ss)", "wifi_hotspot", cmd);
658         g_dbus_connection_emit_signal(conn, NULL, "/Org/Tizen/Quickpanel",
659                         "org.tizen.quickpanel", "ACTIVITY", message, &err);
660         if (err) {
661                 ERR("g_dbus_connection_emit_signal is failed and error is %s\n", err->message);
662                 g_error_free(err);
663                 ret = -1;
664         }
665         g_variant_unref(message);
666
667         if (conn)
668                 g_object_unref(conn);
669
670         DBG("-\n");
671         return ret;
672 }