527ecec78210f818338132f75c360d011ca9de48
[apps/core/preloaded/settings.git] / setting-connectivity / src / setting-connectivity-usb.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-connectivity-usb.h>
22 #include <app_manager.h>
23
24 #define DEBUG_MODE_POPUP_TEXT \
25         "USB debugging is intended for development purposes only. " \
26         "It can be used to copy data between your computer and your " \
27         "device, install application on your device without " \
28         "notification, and read log data"
29
30 static int setting_connectivity_usb_create(void *cb);
31 static int setting_connectivity_usb_destroy(void *cb);
32 static int setting_connectivity_usb_update(void *cb);
33 static int setting_connectivity_usb_cleanup(void *cb);
34
35 static void debug_mode_change_vconf_cb(keynode_t *key, void *data);
36 static void in_mode_change_vconf_cb(keynode_t *key, void *data);
37 static void unload_popup(void* data);
38 void load_usb_connection_popup(void* data);
39
40 /* This is pointer to indicate progressbars*/
41 static button_status button[NUM_PROGRESSBAR];
42
43 /* These variables are for the popup */
44 static Evas_Object *win_main, *popup_eo;
45 static char *popup_msg, *popup_title;
46
47 setting_view setting_view_connectivity_usb = {
48         .create = setting_connectivity_usb_create,
49         .destroy = setting_connectivity_usb_destroy,
50         .update = setting_connectivity_usb_update,
51         .cleanup = setting_connectivity_usb_cleanup,
52 };
53
54 static int setting_connectivity_change_debug_mode_toggle(SettingConnectivityUG *ad)
55 {
56         SETTING_TRACE_BEGIN;
57         if(!ad) return -1;
58
59         int ret = -1;
60         int debugMode = 0;
61         ret = vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, &debugMode);
62         if (ret != 0) {
63                 SETTING_TRACE("FAIL: vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL)");
64                 return -1;
65         }
66         SETTING_TRACE("debugMode: %d", debugMode);
67
68         elm_check_state_set(ad->debug_mode->eo_check, EINA_FALSE);
69         if (debugMode == 0) {
70                 load_usb_connection_popup(ad);
71         } else {
72                 ret = vconf_set_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, 0);
73                 if (ret != 0) {
74                         SETTING_TRACE("FAIL: vconf_set_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL)");
75                         return -1;
76                 }
77         }
78
79         SETTING_TRACE_END;
80         return 0;
81 }
82
83 static void setting_connectivity_main_chk_usb_debug_cb(void *data,
84                                                 Evas_Object * obj, void *event_info)
85 {
86         SETTING_TRACE_BEGIN;
87         retm_if(data == NULL, "Data parameter is NULL");
88         Setting_GenGroupItem_Data *list_item = (Setting_GenGroupItem_Data *) data;
89         SettingConnectivityUG *ad = list_item->userdata;
90
91         if (0 > setting_connectivity_change_debug_mode_toggle(ad)) {
92                 SETTING_TRACE("FAIL: setting_connectivity_change_debug_mode_toggle()");
93         }
94
95         SETTING_TRACE_END;
96 }
97
98 static void setting_connectivity_usb_mouse_up_Gendial_list_cb(void *data,
99                                                     Evas_Object *obj, void *event_info)
100 {
101         SETTING_TRACE_BEGIN;
102         retm_if(data == NULL, "Invalid argument: data is NULL");
103         retm_if(event_info == NULL, "Invalid argument: event_info is NULL");
104         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
105         SettingConnectivityUG *ad = (SettingConnectivityUG *)data;
106
107         elm_genlist_item_selected_set(item, 0);
108
109         if (0 > setting_connectivity_change_debug_mode_toggle(ad)) {
110                 SETTING_TRACE("FAIL: setting_connectivity_change_debug_mode_toggle()");
111         }
112
113         SETTING_TRACE_END;
114 }
115
116 /**
117  * To create the usb main view
118  *
119  *@param cb
120  *
121  *@return
122  */
123 static int setting_connectivity_usb_create(void *cb)
124 {
125         SETTING_TRACE_BEGIN;
126         SETTING_TRACE("Start USB utilities\n");
127         /* error check */
128         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
129
130         SettingConnectivityUG *ad = (SettingConnectivityUG *) cb;
131         Evas_Object *scroller;
132
133         int vconf_ret = -1;
134         int err = -1;
135
136         /* win_main of the popup */
137         win_main = ad->win_get;
138 #ifdef HELP_UG_EXIST
139         ad->ly_usb =
140             setting_create_layout_navi_bar_genlist(ad->win_main_layout,
141                                                    ad->win_get,
142                                                    _(KeyStr_DeveloperOption),
143                                                    dgettext("sys_string", "IDS_COM_BODY_BACK"),
144                                                    _("IDS_COM_BODY_HELP"),
145                                                    setting_connectivity_usb_click_softkey_cancel_cb,
146                                                    setting_connectivity_usb_click_softkey_set_cb,
147                                                    ad, &scroller,
148                                                    &ad->navi_bar);
149 #else
150         ad->ly_usb =
151             setting_create_layout_navi_bar_genlist(ad->win_main_layout,
152                                                    ad->win_get,
153                                                    _(KeyStr_DeveloperOption),
154                                                    dgettext("sys_string", "IDS_COM_BODY_BACK"),
155                                                    NULL,
156                                                    setting_connectivity_usb_click_softkey_cancel_cb,
157                                                    NULL,
158                                                    ad, &scroller,
159                                                    &ad->navi_bar);
160 #endif
161
162         SETTING_TRACE("before init\n");
163
164         button[SETTING_USB_DEBUG_MODE].item = NULL;
165         button[SETTING_USB_DEBUG_MODE].pstate = STATE_NONE;
166
167         Elm_Object_Item *item = NULL;;
168 #if 1
169         // [UI] separator
170         item = elm_genlist_item_append(scroller, &(ad->itc_seperator), NULL, NULL,
171                                                                 ELM_GENLIST_ITEM_NONE, NULL, NULL);
172         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
173
174         // [UI] USB debugging
175         int vconf_val;
176         int toggle_dbg = 0;
177
178         err = vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, &toggle_dbg);
179         if (err != 0) {
180                 SETTING_TRACE_ERROR("FAIL: vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL)");
181                 /* set debug mode to true to find the problem*/
182                 toggle_dbg = 1;
183         }
184         SETTING_TRACE("toggle_dbg: %d", toggle_dbg);
185
186         ad->debug_mode =
187                 setting_create_Gendial_field_def(scroller, &(ad->itc_1text_1icon_gen),
188                         setting_connectivity_usb_mouse_up_Gendial_list_cb,
189                         ad, SWALLOW_Type_1TOGGLE, NULL,
190                         NULL, toggle_dbg, "IDS_ST_BODY_USB_DEBUGGING",
191                         NULL, setting_connectivity_main_chk_usb_debug_cb);
192
193         if (ad->debug_mode) {
194                 ad->debug_mode->userdata = ad;
195                 button[SETTING_USB_DEBUG_MODE].item = ad->debug_mode->item;
196         } else {
197                 SETTING_TRACE_ERROR("ad->debug_mode is NULL");
198                 return SETTING_RETURN_FAIL;
199         }
200
201         setting_create_Gendial_field_def(scroller, &(ad->itc_help_style), NULL,
202                 ad, SWALLOW_Type_LAYOUT_SPECIALIZTION_X,
203                 NULL, NULL, 0, SETTING_USB_DEBUGGING_DESC, NULL,
204                 NULL);
205         elm_genlist_item_select_mode_set(elm_genlist_item_append(scroller, &(itc_seperator), NULL, NULL,ELM_GENLIST_ITEM_NONE, NULL, NULL),
206                                          ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
207
208 #endif
209
210         SETTING_TRACE("Second item of Second group is set\n");
211
212         //----------------------------------------------------------------------------------------------
213         /* Registering callback function for VCONFKEY_SETAPPL_USB_MODE_INT */
214         vconf_ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL,
215                                                 debug_mode_change_vconf_cb, ad);
216         if(vconf_ret != 0)
217         {
218                 SETTING_TRACE("FAIL: vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL)");
219                 return SETTING_RETURN_FAIL;
220         }
221
222         /* Registering callback function for VCONFKEY_SETAPPL_USB_IN_MODE_CHANGE */
223         vconf_ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_IN_MODE_CHANGE,
224                                                 in_mode_change_vconf_cb, ad);
225         if(vconf_ret != 0)
226         {
227                 SETTING_TRACE("FAIL: vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_IN_MODE_CHANGE)\n");
228                 return SETTING_RETURN_FAIL;
229         }
230
231         int val;
232         vconf_ret = vconf_get_int(VCONFKEY_SETAPPL_USB_IN_MODE_CHANGE, &val);
233         if (vconf_ret == 0 && val == IN_MODE_CHANGE)
234         {
235                 SETTING_TRACE("ad->blockUI: EINA_TRUE\n");
236                 ad->blockUI = EINA_TRUE;
237         }
238         else    /* val == CHANGE_COMPLETE */
239         {
240                 SETTING_TRACE("ad->blockUI: EINA_FALSE\n");
241                 ad->blockUI = EINA_FALSE;
242         }
243
244         setting_view_connectivity_usb.is_create = 1;
245         setting_view_update(&setting_view_connectivity_usb, ad);
246
247         SETTING_TRACE_END;
248         return SETTING_RETURN_SUCCESS;
249 }
250
251 /**
252  * This callback function is called when USB-setting changes VCONFKEY_SETAPPL_USB_IN_MODE_CHANGE
253  *
254  *@param data
255  */
256 static void in_mode_change_vconf_cb(keynode_t *key, void *data)
257 {
258         SETTING_TRACE_BEGIN;
259         setting_retm_if(NULL == data, "ERROR:The parameter(data) is NULL\n");
260         SettingConnectivityUG *ad = (SettingConnectivityUG *) data;
261         SETTING_TRACE("ad->blockUI: %d\n", ad->blockUI);
262
263         int in_mode_change;
264         int ret = -1;
265
266         ret = vconf_get_int(VCONFKEY_SETAPPL_USB_IN_MODE_CHANGE, &in_mode_change);
267         setting_retm_if (ret != 0, "vconf_get_int(VCONFKEY_SETAPPL_USB_IN_MODE_CHANGE\n");
268
269         if (CHANGE_COMPLETE == in_mode_change) {
270                 SETTING_TRACE("ad->blockUI: EINA_FALSE\n");
271                 ad->blockUI = EINA_FALSE;
272         } else if (IN_MODE_CHANGE == in_mode_change) {
273                 SETTING_TRACE("ad->blockUI: EINA_TRUE\n");
274                 ad->blockUI = EINA_TRUE;
275         } else {
276                 SETTING_TRACE("Getting in_mode_change failed\n");
277         }
278
279         SETTING_TRACE_END;
280 }
281
282 /**
283  * When VCONFKEY_SETAPPL_USB_MODE_INT is changed, all buttons are enabled
284  *
285  *@param key
286  *@param data
287  */
288 static void debug_mode_change_vconf_cb(keynode_t *key, void *data)
289 {
290         SETTING_TRACE_BEGIN;
291         setting_retm_if (NULL == data, "ERROR:The parameter(data) is NULL\n");
292         SettingConnectivityUG *ad = (SettingConnectivityUG *)data;
293
294         int ret = -1;
295         int debugMode = 0;
296
297         ret = vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, &debugMode);
298         setting_retm_if (ret != 0, "ERROR: vconf_get_int(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL)");
299         SETTING_TRACE("debugMode: %d", debugMode);
300
301         if (debugMode == 1) {
302                 elm_check_state_set(ad->debug_mode->eo_check, EINA_TRUE);
303         } else if (debugMode == 0) {
304                 elm_check_state_set(ad->debug_mode->eo_check, EINA_FALSE);
305         } else {
306                 SETTING_TRACE("ERROR: debugMode value is improper");
307         }
308
309         /* Unblock setting UI */
310         SETTING_TRACE("ad->blockUI: EINA_FALSE\n");
311         ad->blockUI = EINA_FALSE;
312
313         SETTING_TRACE_END;
314 }
315
316 /**
317  * To destory the view of usb
318  *
319  *@param cb
320  *
321  *@return
322  */
323 static int setting_connectivity_usb_destroy(void *cb)
324 {
325         SETTING_TRACE_BEGIN;
326         /* error check */
327         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
328
329         SettingConnectivityUG *ad = (SettingConnectivityUG *) cb;
330         int vconf_ret = -1;
331         int num_err = 0;
332
333         /* Removing the popup if the popup is loaded */
334         unload_popup(ad);
335         win_main = NULL;
336
337         /* Closing vconf trace */
338         vconf_ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL,
339                                                 debug_mode_change_vconf_cb);
340         if (vconf_ret != 0)
341         {
342                 SETTING_TRACE("FAIL: vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL)");
343                 num_err++;
344         }
345
346         vconf_ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_IN_MODE_CHANGE, in_mode_change_vconf_cb);
347         if (vconf_ret != 0)
348         {
349                 SETTING_TRACE("FAIL: vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT)\n");
350                 num_err++;
351         }
352
353         if (ad->ly_usb != NULL) {
354                 evas_object_del(ad->ly_usb);
355                 setting_view_connectivity_usb.is_create = 0;
356         }
357
358         SETTING_TRACE_END;
359         if (num_err > 0)
360         {
361                 SETTING_TRACE("ERROR: the number of errors to destroy is %d\n", num_err);
362                 return SETTING_RETURN_FAIL;
363         }
364         else
365         {
366                 return SETTING_RETURN_SUCCESS;
367         }
368
369 }
370
371 /**
372  * To update the view of usb
373  *
374  *@param cb
375  *
376  *@return
377  */
378 static int setting_connectivity_usb_update(void *cb)
379 {
380         SETTING_TRACE_BEGIN;
381         /* error check */
382         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
383         SettingConnectivityUG *ad = (SettingConnectivityUG *) cb;
384
385         if (ad->ly_usb != NULL) {
386                 evas_object_show(ad->ly_usb);
387         }
388         SETTING_TRACE_END;
389         return SETTING_RETURN_SUCCESS;
390 }
391
392 /**
393  * To cleanup the view of usb
394  *
395  *@param cb
396  *
397  *@return
398  */
399 static int setting_connectivity_usb_cleanup(void *cb)
400 {
401         SETTING_TRACE_BEGIN;
402         SETTING_TRACE_END;
403         return SETTING_RETURN_SUCCESS;
404 }
405
406 /**
407  * Do process when clicking '<-' button
408  *
409  *@param data
410  *@param obj
411  *@param event_info
412  */
413 static void
414 setting_connectivity_usb_click_softkey_cancel_cb(void *data, Evas_Object *obj,
415                                                  void *event_info)
416 {
417         SETTING_TRACE_BEGIN;
418         /* error check */
419         setting_retm_if(data == NULL, "Data parameter is NULL");
420
421         SettingConnectivityUG *ad = (SettingConnectivityUG *) data;
422
423         /* Not to block back(->) button */
424         /* Send destroy request */
425         ug_destroy_me(ad->ug);
426
427         SETTING_TRACE_END;
428 }
429
430 /**
431  * Do process when clicking 'Help' button
432  *
433  *@param data
434  *@param obj
435  *@param event_info
436  */
437 #ifdef HELP_UG_EXIST
438 static void
439 setting_connectivity_usb_click_softkey_set_cb(void *data, Evas_Object *obj,
440                                               void *event_info)
441 {
442         SETTING_TRACE_BEGIN;
443         /* error check */
444         setting_retm_if(data == NULL, "Data parameter is NULL");
445
446         SettingConnectivityUG *ad = (SettingConnectivityUG *) data;
447
448         /* Send destroy request */
449         setting_view_change(&setting_view_connectivity_usb,
450                             &setting_view_connectivity_usb_help, ad);
451         SETTING_TRACE_END;
452 }
453 #endif
454
455
456 /**
457  * Here is for loading popup
458  */
459
460 /**
461  * unload popup
462  */
463 static void unload_popup(void* data)
464 {
465         SETTING_TRACE_BEGIN;
466         SettingConnectivityUG *ad = (SettingConnectivityUG *)data;
467
468         //ad->pop
469         if (ad->pop)
470         {
471                 evas_object_del(ad->pop);
472                 ad->pop = NULL;
473         } else {
474                 SETTING_TRACE("ad->pop == NULL\n");
475         }
476
477         if (popup_eo != NULL)
478         {
479                 SETTING_TRACE("evas_object_del(popup_eo)\n");
480                 evas_object_del(popup_eo);
481                 popup_eo = NULL;
482         }
483         else
484         {
485                 SETTING_TRACE("popup_eo == NULL\n");
486         }
487
488         SETTING_TRACE_END;
489 }
490
491 /**
492  * Callback function to respond pushing cancel button of the usb connection popup
493  */
494 static void debug_mode_resp_cb(void *data, Evas_Object *obj, void *event_info)
495 {
496         SETTING_TRACE_BEGIN;
497         int ret = -1;
498         SettingConnectivityUG *ad = (SettingConnectivityUG *)data;
499
500         int resp_type = btn_type(obj);
501         if (resp_type == POPUP_RESPONSE_OK) {
502                 ret = vconf_set_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, 1);
503                 if (ret != 0) {
504                         SETTING_TRACE("FAIL: vconf_set_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL)");
505                         return;
506                 }
507         }
508
509         unload_popup(ad);
510         SETTING_TRACE_END;
511 }
512
513 /**
514  * When a button on USB utilities is pushed,
515  * this function makes a popup if USB cable is not connected
516  */
517 void load_usb_connection_popup(void* data)
518 {
519         SETTING_TRACE_BEGIN;
520         //int ret;
521         SettingConnectivityUG *ad = (SettingConnectivityUG *)data;
522
523         unload_popup(ad);
524         popup_msg = _(DEBUG_MODE_POPUP_TEXT);
525         popup_title = _("Allow USB debugging?");
526
527         ad->pop = setting_create_popup_with_btn(ad, win_main,//ad->win_get,
528                                         popup_title,
529                                         popup_msg,
530                                         debug_mode_resp_cb,
531                                         0, 2,
532                                         _("IDS_COM_SK_OK"),
533                                         _("IDS_COM_SK_CANCEL"));
534         if (!(ad->pop)) SETTING_TRACE("FAIL: setting_create_popup_with_btn()");
535
536         SETTING_TRACE_END;
537 }
538
539