Apply consistent log messages.
[platform/core/system/system-popup.git] / src / usb / usb-mobile.c
1 /*
2  *  system-popup
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 "popup-common.h"
21
22 static const struct popup_ops usb_error_ops;
23 static const struct popup_ops usb_restrict_ops;
24
25 static int state_handler = -1;
26
27 static void usb_state_changed(keynode_t *key, void *data)
28 {
29         int state;
30         const struct popup_ops *ops = data;
31
32         if (vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &state) != 0)
33                 return;
34
35         switch (state) {
36         case VCONFKEY_SYSMAN_USB_DISCONNECTED:
37                 _I("USB cabel is disconnected.");
38                 unload_simple_popup(ops);
39                 if (state_handler == 0) {
40                         vconf_ignore_key_changed(
41                                         VCONFKEY_SYSMAN_USB_STATUS,
42                                         usb_state_changed);
43                         state_handler = -1;
44                 }
45                 terminate_if_no_popup();
46                 break;
47         case VCONFKEY_SYSMAN_USB_CONNECTED:
48         case VCONFKEY_SYSMAN_USB_AVAILABLE:
49         default:
50                 break;
51         }
52 }
53
54 static int usb_launch(bundle *b, const struct popup_ops *ops)
55 {
56         const struct popup_ops *remove;
57
58         if (ops == &usb_error_ops)
59                 remove = &usb_restrict_ops;
60         else if (ops == &usb_restrict_ops)
61                 remove = &usb_error_ops;
62         else
63                 remove = NULL;
64
65         if (remove)
66                 unload_simple_popup(remove);
67
68         if (state_handler != 0) {
69                 state_handler = vconf_notify_key_changed(
70                                 VCONFKEY_SYSMAN_USB_STATUS,
71                                 usb_state_changed, (void *)ops);
72                 if (state_handler != 0)
73                         _E("Failed to register usb state change event().");
74         }
75
76         return 0;
77 }
78
79 static void usb_terminate(const struct popup_ops *ops)
80 {
81         if (state_handler == 0) {
82                 vconf_ignore_key_changed(
83                                 VCONFKEY_SYSMAN_USB_STATUS,
84                                 usb_state_changed);
85                 state_handler = -1;
86         }
87         terminate_if_no_popup();
88 }
89
90 static const struct popup_ops usb_error_ops = {
91         .name                   = "usb_error",
92         .pattern                = FEEDBACK_PATTERN_LOWBATT,
93         .show                   = load_simple_popup,
94         .content                = "IDS_USB_POP_USB_CONNECTION_FAILED",
95         .left_text              = "IDS_COM_SK_OK",
96         .pre                    = usb_launch,
97         .terminate              = usb_terminate,
98 };
99
100 static const struct popup_ops usb_restrict_ops = {
101         .name                   = "usb_restrict",
102         .pattern        = FEEDBACK_PATTERN_LOWBATT,
103         .show                   = load_simple_popup,
104         .content                = "IDS_ST_POP_SECURITY_POLICY_PREVENTS_USE_OF_DESKTOP_SYNC",
105         .left_text              = "IDS_COM_SK_OK",
106         .pre                    = usb_launch,
107         .terminate              = usb_terminate,
108 };
109
110
111 /* Constructor to register mount_failed button */
112 static __attribute__ ((constructor)) void usb_register_popup(void)
113 {
114         register_popup(&usb_error_ops);
115         register_popup(&usb_restrict_ops);
116 }