Apply consistent log messages.
[platform/core/system/system-popup.git] / src / usb / usb-device.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 #include "popup-ui.h"
22 #include <pkgmgr-info.h>
23 #include <app_manager.h>
24
25 enum button_selected_e {
26         USB_DEVICE_CONFIRM_NOK,
27         USB_DEVICE_CONFIRM_OK
28 };
29
30 #define USD_PATH                                "/Org/Tizen/System/USD"
31 #define USD_INTERFACE                           "org.tizen.system.usd"
32 #define USD_USB_DEVICE_CONFIRM_SIGNAL           "USBDeviceOpenResult"
33
34 #define USBHOST_PATH                            "/Org/Tizen/System/Popup/System"
35 #define USBHOST_INTERFACE                               "org.tizen.system.popup.System"
36 #define USBHOST_APP_CONFIRM_SIGNAL              "USBHostResult"
37
38 #define BUF_MAX 512
39
40 static const struct popup_ops usb_device_confirm_ops;
41 static Eina_Bool App_confirm_box = 0;
42
43 static char *items[] = {
44         "IDS_COM_POP_ALLOW_APPLICATION_P1SS_TO_ACCESS_USB_DEVICE_Q_ABB",
45         "IDS_COM_BODY_ALLOWS_AN_APPLICATION_TO_ACCESS_USB_DEVICES",
46         "IDS_COM_SK_OK",
47         "IDS_COM_SK_CANCEL"
48 };
49
50 char* gl_text_get(int index)
51 {
52
53         return strdup(_(items[index]));
54 }
55
56 static void send_result_dbus_signal(int result)
57 {
58         int ret;
59         char buf[8];
60         char *param[1];
61
62         snprintf(buf, sizeof(buf), "%d", result);
63         param[0] = buf;
64         ret = broadcast_dbus_signal(USD_PATH,
65                         USD_INTERFACE,
66                         USD_USB_DEVICE_CONFIRM_SIGNAL,
67                         "i", param);
68         if (ret < 0)
69                 _E("Failed to broadcast_dbus_signal: %d", ret);
70 }
71
72 static void send_usbhost_result_dbus_signal(int result)
73 {
74         int ret;
75         char buf1[8], buf2[8];
76         char *param[2];
77
78         snprintf(buf1, sizeof(buf1), "%d", result);
79         param[0] = buf1;
80         snprintf(buf2, sizeof(buf2), "%d", App_confirm_box);
81         param[1] = buf2;
82
83         ret = broadcast_dbus_signal(USBHOST_PATH,
84                 USBHOST_INTERFACE,
85                 USBHOST_APP_CONFIRM_SIGNAL,
86                 "ii", param);
87         if (ret < 0)
88                 _E("Failed to broadcast_dbus_signal: %d", ret);
89 }
90
91 static void usb_device_confirm_ok_clicked(const struct popup_ops *ops)
92 {
93         _I("OK is selected.");
94         unload_simple_popup(ops);
95         send_result_dbus_signal(USB_DEVICE_CONFIRM_OK);
96         terminate_if_no_popup();
97 }
98
99 static void usb_device_confirm_cancel_clicked(const struct popup_ops *ops)
100 {
101         _I("CANCEL is selected.");
102         unload_simple_popup(ops);
103         send_result_dbus_signal(USB_DEVICE_CONFIRM_NOK);
104         terminate_if_no_popup();
105 }
106
107 static void usb_device_confirm_terminate(const struct popup_ops *ops)
108 {
109         _I("Terminate usb device confirm popup.");
110         send_result_dbus_signal(USB_DEVICE_CONFIRM_NOK);
111         terminate_if_no_popup();
112 }
113
114 static void usbhost_confirm_terminate(const struct popup_ops *ops)
115 {
116         _I("Terminate usb device confirm popup.");
117         terminate_if_no_popup();
118 }
119
120 static void usbhost_confirm_ok_clicked(void *data, Evas_Object *obj, void *event_info)
121 {
122         const struct popup_ops *ops = data;
123
124         _I("OK is selected.");
125         unload_simple_popup(ops);
126         send_usbhost_result_dbus_signal(USB_DEVICE_CONFIRM_OK);
127         terminate_if_no_popup();
128 }
129
130 static void usbhost_confirm_cancel_clicked(void *data, Evas_Object *obj, void *event_info)
131 {
132         const struct popup_ops *ops = data;
133
134         _I("CANCEL is selected.");
135         unload_simple_popup(ops);
136         send_usbhost_result_dbus_signal(USB_DEVICE_CONFIRM_NOK);
137         terminate_if_no_popup();
138 }
139
140 static int usbhost_get_contents(bundle *b, char *content, unsigned int len)
141 {
142         int ret;
143         char *text = NULL, *app_pid = NULL, *app_name = NULL;
144         pid_t pid;
145
146         if (!b) {
147                 _E("Bundle is null return -EINVAL.");
148                 return -EINVAL;
149         }
150
151         app_pid = (char *)bundle_get_val(b, "_APP_PID_");
152         if (!app_pid) {
153                 _E("Failed to get pid.");
154                 return -ENOENT;
155         }
156
157         pid = atoi(app_pid);
158         free(app_pid);
159
160         ret = app_manager_get_app_id(pid, &app_name);
161         if (ret != APP_MANAGER_ERROR_NONE) {
162                 _E("Failed to get app id: %d", ret);
163                 return -ENOENT;
164         }
165
166         text = gl_text_get(0);
167         if (text) {
168                 snprintf(content, len, text, app_name);
169                 free(text);
170         }
171
172         free(app_name);
173         return 0;
174 }
175
176 static void
177 check_changed_cb(void *data, Evas_Object *obj, void *event_info)
178 {
179         Eina_Bool state = elm_check_state_get(obj);
180
181         App_confirm_box = state;
182 }
183
184 static int usbhost_show(bundle *b, const struct popup_ops *ops)
185 {
186         Evas_Object *popup;
187         Evas_Object *layout;
188         Evas_Object *btn;
189         Evas_Object *scroller;
190         Evas_Object *label;
191         Evas_Object *check;
192         Evas_Object *win;
193         struct object_ops *obj;
194         int ret;
195         char content[BUF_MAX] = {'\0',};
196
197         ret = get_object_by_ops(ops, &obj);
198         if (ret < 0) {
199                 _E("Failed to get object: %d", ret);
200                 return -EINVAL;
201         }
202
203         win = get_window();
204         if (!win)
205                 return -ENOMEM;
206
207         evas_object_show(win);
208
209         popup = elm_popup_add(win);
210         if (!popup)
211                 return -ENOMEM;
212
213         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
214         elm_object_part_text_set(popup, "title,text", "USB Host Test");
215         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
216
217         /* back key */
218         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, event_back_key_up, (void*)ops);
219
220         /* layout */
221         layout = elm_layout_add(popup);
222         elm_layout_file_set(layout, ELM_USB_EDC, "popup_checkview_layout");
223         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
224         elm_object_part_text_set(layout, "elm.text", "Description text");
225
226         /* cancel button */
227         btn = elm_button_add(popup);
228         elm_object_style_set(btn, "bottom");
229         elm_object_text_set(btn, gl_text_get(3));
230         elm_object_part_content_set(popup, "button1", btn);
231         evas_object_smart_callback_add(btn, "clicked", usbhost_confirm_cancel_clicked, ops);
232
233         /* ok button */
234         btn = elm_button_add(popup);
235         elm_object_style_set(btn, "bottom");
236         elm_object_text_set(btn, gl_text_get(2));
237         elm_object_part_content_set(popup, "button2", btn);
238         evas_object_smart_callback_add(btn, "clicked", usbhost_confirm_ok_clicked, ops);
239
240         /* check */
241         check = elm_check_add(popup);
242         elm_object_text_set(check, gl_text_get(1));
243         elm_object_part_content_set(layout, "elm.swallow.end", check);
244         evas_object_smart_callback_add(check, "changed", check_changed_cb, NULL);
245
246         /* scroller */
247         scroller = elm_scroller_add(layout);
248         elm_scroller_bounce_set(scroller, EINA_TRUE, EINA_TRUE);
249         elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
250         elm_object_part_content_set(layout, "elm.swallow.content", scroller);
251
252         /* label */
253         label = elm_label_add(scroller);
254         elm_object_style_set(label, "popup/default");
255         elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
256
257         ret = usbhost_get_contents(b, content, sizeof(content));
258         if (ret < 0)
259                 return -EINVAL;
260
261         elm_object_text_set(label, content);
262         evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
263         evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
264         evas_object_show(label);
265         elm_object_content_set(scroller, label);
266
267         elm_object_content_set(popup, layout);
268
269         evas_object_show(popup);
270         obj->popup = popup;
271
272         return 0;
273 }
274
275 static const struct popup_ops usb_device_confirm_ops = {
276         .name           = "usb_device_confirm",
277         .show           = load_simple_popup,
278         .content        = "Do you use this usb device?", /* TODO */
279         .left_text      = "IDS_COM_SK_CANCEL",
280         .left           = usb_device_confirm_cancel_clicked,
281         .right_text     = "IDS_COM_SK_OK",
282         .right          = usb_device_confirm_ok_clicked,
283         .terminate      = usb_device_confirm_terminate,
284 };
285
286 static const struct popup_ops usbhost_confirm_ops = {
287         .name           = "usbhost",
288         .pattern        = FEEDBACK_PATTERN_LOWBATT,
289         .show           = usbhost_show,
290         .terminate      = usbhost_confirm_terminate,
291 };
292
293 /* Constructor to register usb_device popup */
294 static __attribute__ ((constructor)) void usb_device_register_popup(void)
295 {
296         register_popup(&usb_device_confirm_ops);
297         register_popup(&usbhost_confirm_ops);
298 }