[cleanup] revise file location
[platform/core/connectivity/bluetooth-share.git] / app / bt-share-noti-handler.c
1 /*
2  * bluetooth-share
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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 <glib.h>
21 #include <vconf.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <storage.h>
27 /* For multi-user support */
28 #include <tzplatform_config.h>
29
30 #include "applog.h"
31 #include "bt-share-common.h"
32 #include "bluetooth-api.h"
33 #include "bt-share-noti-handler.h"
34 #include "bt-share-main.h"
35 #include "bt-share-notification.h"
36 #include "obex-event-handler.h"
37
38 /* LCOV_EXCL_START */
39 static void __bt_default_memory_changed_cb(keynode_t *node, void *data)
40 {
41         FN_START;
42         ret_if(node == NULL);
43
44         int default_memory = 0;
45         char *root_path = NULL;
46         char *download_path = NULL;
47
48         DBG_SECURE("key=%s\n", vconf_keynode_get_name(node));
49
50         if (vconf_keynode_get_type(node) == VCONF_TYPE_INT) {
51                 /* Phone memory is 0, MMC is 1 */
52                 default_memory = vconf_keynode_get_int(node);
53
54                 if (default_memory == BT_DEFAULT_MEM_MMC) /* MMC */ {
55                         download_path = _bt_share_get_storage_path(default_memory);
56                         root_path = _bt_share_get_storage_path(default_memory);
57
58                         if (download_path == NULL)
59                                 download_path = g_strdup(BT_DOWNLOAD_DEFAULT_MEDIA_FOLDER);
60
61                         if (root_path == NULL)
62                                 root_path = g_strdup(BT_DOWNLOAD_DEFAULT_MEDIA_FOLDER);
63
64                 } else {
65                         download_path = g_strdup(BT_DOWNLOAD_DEFAULT_MEDIA_FOLDER);
66                         root_path = g_strdup(BT_DOWNLOAD_DEFAULT_MEDIA_FOLDER);
67                 }
68
69                 if (access(download_path, W_OK) != 0) {
70                         warn_if(mkdir(download_path, 0755) < 0,
71                                         "mkdir fail![%s]", download_path);
72                 }
73
74                 bluetooth_obex_server_set_root(root_path);
75                 bluetooth_obex_server_set_destination_path(download_path);
76
77                 g_free(download_path);
78                 g_free(root_path);
79         }
80
81         FN_END;
82 }
83 /* LCOV_EXCL_STOP */
84
85 /* LCOV_EXCL_START */
86 static void __bt_mmc_status_changed_cb(keynode_t *node, void *data)
87 {
88         FN_START;
89         retm_if(!node || !data, "invalid param!");
90
91         int mmc_status = 0;
92         int default_memory = 0;
93         int ret = NOTIFICATION_ERROR_NONE;
94         char *root_path = NULL;
95         char *download_path = NULL;
96
97         DBG_SECURE("key=%s", vconf_keynode_get_name(node));
98         bt_appdata_t *ad = (bt_appdata_t *)data;
99
100         if (vconf_keynode_get_type(node) == VCONF_TYPE_INT) {
101                 /* Phone memory is 0, MMC is 1 */
102                 mmc_status = vconf_keynode_get_int(node);
103
104                 if (mmc_status == VCONFKEY_SYSMAN_MMC_REMOVED ||
105                         mmc_status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
106                         retm_if(vconf_get_int(VCONFKEY_SETAPPL_DEFAULT_MEM_BLUETOOTH_INT,
107                                                         &default_memory) != 0,
108                                                         "vconf_get_int failed");
109
110                         if (ad->opc_noti) {     /* sending case */
111                                 DBG("cancel outbound transfer");
112                                 bluetooth_opc_cancel_push();
113                                 ret = _bt_delete_notification(ad->opc_noti);
114                                 if (ret == NOTIFICATION_ERROR_NONE) {
115                                         ad->opc_noti = NULL;
116                                         ad->opc_noti_id = 0;
117                                 }
118                         } else {                /* receiving case */
119                                 DBG("cancel inbound transfer");
120                                 if (default_memory == BT_DEFAULT_MEM_MMC) {
121                                         _bt_obex_cancel_transfer(data);
122                                         retm_if(vconf_set_int(
123                                                 VCONFKEY_SETAPPL_DEFAULT_MEM_BLUETOOTH_INT,
124                                                 BT_DEFAULT_MEM_PHONE) != 0, "vconf_set_int failed");
125                                         DBG_SECURE("Default Memory set to Phone");
126                                 }
127                         }
128                 } else if (mmc_status == VCONFKEY_SYSMAN_MMC_MOUNTED) {
129                         vconf_get_int(VCONFKEY_SETAPPL_DEFAULT_MEM_BLUETOOTH_INT, &default_memory);
130                         if (default_memory == BT_DEFAULT_MEM_MMC) /* MMC */ {
131                                 download_path = _bt_share_get_storage_path(default_memory);
132                                 root_path = _bt_share_get_storage_path(default_memory);
133
134                                 if (download_path == NULL)
135                                         download_path = g_strdup(BT_DOWNLOAD_DEFAULT_MEDIA_FOLDER);
136
137                                 if (root_path == NULL)
138                                         root_path = g_strdup(BT_DOWNLOAD_DEFAULT_MEDIA_FOLDER);
139
140                         } else {
141                                 download_path = g_strdup(BT_DOWNLOAD_DEFAULT_MEDIA_FOLDER);
142                                 root_path = g_strdup(BT_DOWNLOAD_DEFAULT_MEDIA_FOLDER);
143                         }
144
145                         if (access(download_path, W_OK) != 0) {
146                                 warn_if(mkdir(download_path, 0755) < 0,
147                                                 "mkdir fail![%s]", download_path);
148                         }
149
150                         bluetooth_obex_server_set_root(root_path);
151                         bluetooth_obex_server_set_destination_path(download_path);
152
153                         g_free(download_path);
154                         g_free(root_path);
155                 }
156         }
157
158         FN_END;
159 }
160 /* LCOV_EXCL_STOP */
161
162 void _bt_init_vconf_notification(void *data)
163 {
164         retm_if(!data, "invalid param!");
165
166         int ret = VCONF_OK;
167
168         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEFAULT_MEM_BLUETOOTH_INT,
169                         __bt_default_memory_changed_cb, NULL);
170         warn_if(ret != VCONF_OK, "vconf_notify_key_changed init failed");
171
172         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS,
173                         __bt_mmc_status_changed_cb, data);
174         warn_if(ret != VCONF_OK, "vconf_notify_key_changed init failed");
175 }
176
177 void _bt_deinit_vconf_notification(void)
178 {
179         int ret = VCONF_OK;
180         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEFAULT_MEM_BLUETOOTH_INT,
181                         (vconf_callback_fn) __bt_default_memory_changed_cb);
182         warn_if(ret != VCONF_OK, "vconf_notify_key_changed deinit failed");
183
184         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_STATUS,
185                         (vconf_callback_fn) __bt_mmc_status_changed_cb);
186         warn_if(ret != VCONF_OK, "vconf_notify_key_changed deinit failed");
187 }
188