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