Remove executable flag from non-executable files
[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("FAIL: 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("FAIL: 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, *app_id;
144         pid_t pid;
145
146         if (!b) {
147                 _E("bundle is null return -EINVAL");
148                 return -EINVAL;
149         }
150
151         app_id = (char *)bundle_get_val(b, "_APP_PID_");
152         if (!app_id) {
153                 _E("Failed to get pid");
154                 return -ENOENT;
155         }
156
157         pid = atoi(app_id);
158         ret = app_manager_get_app_id(pid, &app_id);
159         if (ret != APP_MANAGER_ERROR_NONE) {
160                 _E("Failed to get app id(%d)", ret);
161                 return -ENOENT;
162         }
163
164         text = gl_text_get(0);
165         if (text) {
166                 snprintf(content, len, text, app_id);
167                 free(text);
168         }
169
170         free(app_id);
171         return 0;
172 }
173
174 static void
175 check_changed_cb(void *data, Evas_Object *obj, void *event_info)
176 {
177         Eina_Bool state = elm_check_state_get(obj);
178
179         App_confirm_box = state;
180 }
181
182 static int usbhost_show(bundle *b, const struct popup_ops *ops)
183 {
184         Evas_Object *popup;
185         Evas_Object *layout;
186         Evas_Object *btn;
187         Evas_Object *scroller;
188         Evas_Object *label;
189         Evas_Object *check;
190         Evas_Object *win;
191         struct object_ops *obj;
192         int ret;
193         char content[BUF_MAX] = {'\0',};
194
195         ret = get_object_by_ops(ops, &obj);
196         if (ret < 0) {
197                 _E("Failed to get object (%d)", ret);
198                 return -EINVAL;
199         }
200
201         win = get_window();
202         if (!win)
203                 return -ENOMEM;
204
205         evas_object_show(win);
206
207         popup = elm_popup_add(win);
208         if (!popup)
209                 return -ENOMEM;
210
211         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
212         elm_object_part_text_set(popup, "title,text", "USB Host Test");
213         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
214
215         /* back key */
216         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, event_back_key_up, (void*)ops);
217
218         /* layout */
219         layout = elm_layout_add(popup);
220         elm_layout_file_set(layout, ELM_USB_EDC, "popup_checkview_layout");
221         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
222         elm_object_part_text_set(layout, "elm.text", "Description text");
223
224         /* cancel button */
225         btn = elm_button_add(popup);
226         elm_object_style_set(btn, "bottom");
227         elm_object_text_set(btn, gl_text_get(3));
228         elm_object_part_content_set(popup, "button1", btn);
229         evas_object_smart_callback_add(btn, "clicked", usbhost_confirm_cancel_clicked, ops);
230
231         /* ok button */
232         btn = elm_button_add(popup);
233         elm_object_style_set(btn, "bottom");
234         elm_object_text_set(btn, gl_text_get(2));
235         elm_object_part_content_set(popup, "button2", btn);
236         evas_object_smart_callback_add(btn, "clicked", usbhost_confirm_ok_clicked, ops);
237
238         /* check */
239         check = elm_check_add(popup);
240         elm_object_text_set(check, gl_text_get(1));
241         elm_object_part_content_set(layout, "elm.swallow.end", check);
242         evas_object_smart_callback_add(check, "changed", check_changed_cb, NULL);
243
244         /* scroller */
245         scroller = elm_scroller_add(layout);
246         elm_scroller_bounce_set(scroller, EINA_TRUE, EINA_TRUE);
247         elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
248         elm_object_part_content_set(layout, "elm.swallow.content", scroller);
249
250         /* label */
251         label = elm_label_add(scroller);
252         elm_object_style_set(label, "popup/default");
253         elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
254
255         ret = usbhost_get_contents(b, content, sizeof(content));
256         if (ret < 0)
257                 return -EINVAL;
258
259         elm_object_text_set(label, content);
260         evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
261         evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
262         evas_object_show(label);
263         elm_object_content_set(scroller, label);
264
265         elm_object_content_set(popup, layout);
266
267         evas_object_show(popup);
268         obj->popup = popup;
269
270         return 0;
271 }
272
273 static const struct popup_ops usb_device_confirm_ops = {
274         .name           = "usb_device_confirm",
275         .show           = load_simple_popup,
276         .content        = "Do you use this usb device?", /* TODO */
277         .left_text      = "IDS_COM_SK_CANCEL",
278         .left           = usb_device_confirm_cancel_clicked,
279         .right_text     = "IDS_COM_SK_OK",
280         .right          = usb_device_confirm_ok_clicked,
281         .terminate      = usb_device_confirm_terminate,
282 };
283
284 static const struct popup_ops usbhost_confirm_ops = {
285         .name           = "usbhost",
286         .pattern        = FEEDBACK_PATTERN_LOWBATT,
287         .show           = usbhost_show,
288         .terminate      = usbhost_confirm_terminate,
289 };
290
291 /* Constructor to register usb_device popup */
292 static __attribute__ ((constructor)) void usb_device_register_popup(void)
293 {
294         register_popup(&usb_device_confirm_ops);
295         register_popup(&usbhost_confirm_ops);
296 }