4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "popup-common.h"
22 #include <app_control.h>
25 #define MYFILES_APPNAME "org.tizen.myfile"
26 #define GALLERY_APPNAME "org.tizen.gallery"
28 #define USB_MOUNT_ROOT_PATH "/opt/storage"
29 #define USB_MOUNT_PATH "_DEVICE_PATH_"
33 #define DEVICED_PATH "/Org/Tizen/System/DeviceD"
34 #define DEVICED_IFACE "org.tizen.system.deviced"
35 #define DEVICED_PATH_USBHOST DEVICED_PATH"/Usbhost"
36 #define DEVICED_IFACE_USBHOST DEVICED_IFACE".Usbhost"
37 #define SIGNAL_NAME_UNMOUNT "unmount_storage"
39 static char added_path[BUF_MAX];
40 static char removed_path[BUF_MAX];
47 static const struct popup_ops storage_mounted_ops;
48 static const struct popup_ops unmount_storage_ops;
49 static const struct popup_ops camera_added_ops;
50 static const struct popup_ops storage_mount_failed_ops;
51 static const struct popup_ops storage_removed_unsafe_ops;
53 static void remove_otg_popup(const struct popup_ops *ops, char *path)
58 if (ops == &storage_mounted_ops)
59 popup_path = added_path;
60 else if (ops == &unmount_storage_ops)
61 popup_path = removed_path;
68 len = strlen(popup_path);
69 if (len != strlen(path))
71 if (strncmp(popup_path, path, len))
75 unload_simple_popup(ops);
78 static void set_myfiles_param(bundle *b)
83 if (strlen(added_path) > 0)
84 appsvc_add_data(b, "path", added_path);
86 appsvc_add_data(b, "path", USB_MOUNT_ROOT_PATH);
87 appsvc_set_pkgname(b, MYFILES_APPNAME);
90 static void set_gallery_param(bundle *b)
94 appsvc_add_data(b, "album-id", "GALLERY_ALBUM_PTP_ID");
95 appsvc_set_pkgname(b, GALLERY_APPNAME);
98 static struct ext_app_type {
100 void (*set_param)(bundle *b);
102 { EXT_MYFILES , set_myfiles_param },
103 { EXT_GALLERY , set_gallery_param },
106 static void launch_app(int type)
108 app_control_h app_control;
110 int ret, i, type_len;
112 type_len = ARRAY_SIZE(app_type);
113 for (i = 0 ; i < ARRAY_SIZE(app_type) ; i++) {
114 if (type == app_type[i].type)
118 _E("Invalid type (%d)", type);
122 ret = app_control_create(&app_control);
123 if (ret != APP_CONTROL_ERROR_NONE)
126 ret = app_control_set_operation(app_control, APP_CONTROL_OPERATION_VIEW);
127 if (ret != APP_CONTROL_ERROR_NONE) {
128 (void)app_control_destroy(app_control);
132 ret = app_control_send_launch_request(app_control, NULL, NULL);
133 if (ret != APP_CONTROL_ERROR_NONE)
134 _E("Failed to send launch request");
136 (void)app_control_destroy(app_control);
139 static void storage_browse(const struct popup_ops *ops)
141 unload_simple_popup(ops);
142 launch_app(EXT_MYFILES);
143 terminate_if_no_popup();
146 static void camera_browse(const struct popup_ops *ops)
148 unload_simple_popup(ops);
149 launch_app(EXT_GALLERY);
150 terminate_if_no_popup();
153 static void storage_unmount(const struct popup_ops *ops)
158 unload_simple_popup(ops);
160 param[0] = removed_path;
162 ret = broadcast_dbus_signal(DEVICED_PATH_USBHOST,
163 DEVICED_IFACE_USBHOST,
164 SIGNAL_NAME_UNMOUNT, "s", param);
166 _E("FAIL: broadcast_dbus_signal()");
168 memset(removed_path, 0, sizeof(removed_path));
170 terminate_if_no_popup();
174 static int storage_mounted_launch(bundle *b, const struct popup_ops *ops)
177 struct object_ops *obj;
183 ret = get_object_by_ops(ops, &obj);
185 _E("Failed to get object (%d)", ret);
189 path = (char *)bundle_get_val(obj->b, USB_MOUNT_PATH);
191 _E("Failed to get mount path");
195 _I("USB storage mount path (%s)", path);
196 snprintf(added_path, sizeof(added_path), "%s", path);
200 static int unmount_storage_launch(bundle *b, const struct popup_ops *ops)
203 struct object_ops *obj;
209 ret = get_object_by_ops(ops, &obj);
210 _D("ops = %s obj = %s",ops, obj);
212 _E("Failed to get object (%d)", ret);
216 path = (char *)bundle_get_val(obj->b, USB_MOUNT_PATH);
218 _E("Failed to get mount path");
222 remove_otg_popup(&storage_mounted_ops, path);
224 snprintf(removed_path, sizeof(removed_path), "%s", path);
228 static int storage_unmounted(bundle *b, const struct popup_ops *ops)
235 path = (char *)bundle_get_val(b, USB_MOUNT_PATH);
237 _E("Failed to get mount path");
241 remove_otg_popup(&storage_mounted_ops, path);
242 remove_otg_popup(&unmount_storage_ops, path);
244 terminate_if_no_popup();
248 static int camera_removed(bundle *b, const struct popup_ops *ops)
250 remove_otg_popup(&camera_added_ops, NULL);
251 terminate_if_no_popup();
255 static const struct popup_ops storage_mounted_ops = {
256 .name = "usbotg_storage_mounted",
257 .show = load_simple_popup,
258 .title = "IDS_ST_BODY_USB_STORAGE_ABB",
259 .content = "IDS_USB_BODY_BROWSE_STORAGE_CONNECTED_VIA_USB_Q",
260 .left_text = "IDS_COM_SK_CANCEL",
261 .right_text = "IDS_BT_SK_BROWSE",
262 .right = storage_browse,
263 .pre = storage_mounted_launch,
266 static const struct popup_ops storage_unmounted_ops = {
267 .name = "usbotg_storage_unmounted",
268 .show = storage_unmounted,
271 static const struct popup_ops unmount_storage_ops = {
272 .name = "usbotg_unmount_storage",
273 .show = load_simple_popup,
274 .title = "IDS_ST_BODY_USB_STORAGE_ABB",
275 .content = "IDS_COM_POP_UNMOUNT_USB_MASS_STORAGE_BEFORE_REMOVING_TO_AVOID_DATA_LOSS",
276 .left_text = "IDS_COM_SK_CANCEL",
277 .right_text = "IDS_USB_BUTTON_UNMOUNT",
278 .right = storage_unmount,
279 .pre = unmount_storage_launch,
282 static const struct popup_ops camera_added_ops = {
283 .name = "usbotg_camera_added",
284 .show = load_simple_popup,
285 .title = "IDS_CAM_HEADER_CAMERA_M_APPLICATION",
286 .content = "IDS_USB_BODY_BROWSE_CAMERA_CONNECTED_VIA_USB_Q",
287 .left_text = "IDS_COM_SK_CANCEL",
288 .right_text = "IDS_BT_SK_BROWSE",
289 .right = camera_browse,
292 static const struct popup_ops camera_removed_ops = {
293 .name = "usbotg_camera_removed",
294 .show = camera_removed,
297 static const struct popup_ops storage_mount_failed_ops = {
298 .name = "usbotg_storage_mount_failed",
299 .show = load_simple_popup,
300 .title = "IDS_COM_HEADER_ATTENTION",
301 .content = "IDS_COM_BODY_USB_STORAGE_BLANK_OR_HAS_UNSUPPORTED_FILE_SYSTEM",
302 .left_text = "IDS_COM_SK_OK",
305 static const struct popup_ops storage_removed_unsafe_ops = {
306 .name = "usbotg_storage_removed_unsafe",
307 .show = load_simple_popup,
308 .title = "IDS_COM_HEADER_ATTENTION",
309 .content = "IDS_COM_POP_USB_MASS_STORAGE_UNEXPECTEDLY_REMOVED",
310 .left_text = "IDS_COM_SK_OK",
314 /* Constructor to register mount_failed button */
315 static __attribute__ ((constructor)) void usbotg_register_popup(void)
317 register_popup(&storage_mounted_ops);
318 register_popup(&storage_unmounted_ops);
319 register_popup(&unmount_storage_ops);
320 register_popup(&camera_added_ops);
321 register_popup(&camera_removed_ops);
322 register_popup(&storage_mount_failed_ops);
323 register_popup(&storage_removed_unsafe_ops);