Remove unused vconf value
[platform/core/connectivity/bluetooth-share.git] / bt-share / src / bt-share-main.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 <glib-object.h>
22 #include <string.h>
23 #include <appcore-efl.h>
24 #include <privilege-control.h>
25 #include <vconf.h>
26 #include "applog.h"
27 #include "bt-share-main.h"
28 #include "bluetooth-api.h"
29 #include "obex-event-handler.h"
30 #include "bt-share-ipc.h"
31 #include "bt-share-noti-handler.h"
32 #include "bt-share-resource.h"
33 #include "bt-share-notification.h"
34 #include "bt-share-common.h"
35
36 #include "bluetooth-share-api.h"
37
38 #define BLUETOOTH_SHARE_BUS             "org.projectx.bluetooth.share"
39
40 static gboolean terminated;
41
42 GMainLoop *main_loop = NULL;
43 struct bt_appdata *app_state = NULL;
44
45 static void __bt_release_service(struct bt_appdata *ad)
46 {
47         if (ad == NULL)
48                 return;
49
50         _bt_deinit_vconf_notification();
51         _bt_delete_notification(ad->send_noti);
52         _bt_delete_notification(ad->receive_noti);
53         _bt_delete_notification(ad->opc_noti);
54         _bt_clear_receive_noti_list();
55         ad->send_noti = NULL;
56         ad->receive_noti = NULL;
57         ad->opc_noti = NULL;
58
59         bluetooth_opc_deinit();
60         bluetooth_obex_server_deinit();
61         _bt_unregister_notification_cb(ad);
62
63         DBG("Terminating bluetooth-share daemon");
64 }
65
66 static void __bt_sigterm_handler(int signo)
67 {
68         DBG("+");
69
70         if (main_loop) {
71                 g_main_loop_quit(main_loop);
72         } else {
73                 terminated = TRUE;
74         }
75
76         DBG("-");
77 }
78
79 static void __bt_update_notification_status_values()
80 {
81         struct bt_appdata *ad = app_state;
82         GSList *tr_data_list = NULL;
83         GSList *list_iter = NULL;
84         bt_tr_data_t *info = NULL;;
85         char str[NOTIFICATION_TEXT_LEN_MAX] = { 0 };
86         notification_h noti = NULL;
87         sqlite3 *db = NULL;
88
89         db = bt_share_open_db();
90         if (!db)
91                 return;
92
93         tr_data_list = bt_share_get_all_tr_data_list(db, BT_DB_OUTBOUND);
94         if (NULL != tr_data_list) {
95                 list_iter = tr_data_list;
96
97                 while (NULL != list_iter) {
98                         info = list_iter->data;
99                         if (NULL == info)
100                                 break;
101
102                         if (info->tr_status == BT_TR_SUCCESS) {
103                                 ad->send_data.tr_success++;
104                         } else if (info->tr_status == BT_TR_FAIL) {
105                                 ad->send_data.tr_fail++;
106                         } else if (info->tr_status == BT_TR_ONGOING) {
107                         /* In case of ongoing file transfer if bluetooth is switched off
108                         we need to update the status to fail for these transaction */
109                                 ad->send_data.tr_fail++;
110                                 info->tr_status = BT_TR_FAIL;
111                                 bt_share_update_tr_data(db, BT_DB_OUTBOUND, info->id, info);
112                         } else {
113                                 ERR("Invalid status\n");
114                         }
115
116                         list_iter = g_slist_next(list_iter);
117                 }
118
119                 if ((ad->send_data.tr_success + ad->send_data.tr_fail) != 0) {
120                         snprintf(str, sizeof(str), BT_TR_STATUS,
121                         ad->send_data.tr_success, ad->send_data.tr_fail);
122
123                         noti = _bt_create_notification(BT_NOTI_T);
124                         _bt_set_notification_app_launch(noti,
125                                 CREATE_TR_LIST,
126                                 NOTI_TR_TYPE_OUT, NULL, NULL);
127                         _bt_set_notification_property(noti, QP_NO_DELETE | QP_NO_TICKER);
128                         _bt_insert_notification(noti,
129                                 BT_STR_SENT, str,
130                                 BT_ICON_QP_SEND);
131                         ad->send_noti = noti;
132                 }
133
134                 bt_share_release_tr_data_list(tr_data_list);
135                 tr_data_list = NULL;
136                 list_iter = NULL;
137         }
138
139         tr_data_list = bt_share_get_all_tr_data_list(db, BT_DB_INBOUND);
140         if (NULL != tr_data_list) {
141                 list_iter = tr_data_list;
142
143                 while (NULL != list_iter) {
144                         info = list_iter->data;
145                         if (NULL == info)
146                                 break;
147
148                         if (info->tr_status == BT_TR_SUCCESS)
149                                 ad->recv_data.tr_success++;
150                         else
151                                 ad->recv_data.tr_fail++;
152
153                         list_iter = g_slist_next(list_iter);
154                 }
155
156                 if ((ad->recv_data.tr_success + ad->recv_data.tr_fail) != 0) {
157
158                         snprintf(str, sizeof(str), BT_TR_STATUS,
159                                 ad->recv_data.tr_success, ad->recv_data.tr_fail);
160                         DBG("str = [%s] \n", str);
161
162                         noti  = _bt_create_notification(BT_NOTI_T);
163                         _bt_set_notification_app_launch(noti, CREATE_TR_LIST,
164                                 NOTI_TR_TYPE_IN, NULL, NULL);
165                         _bt_set_notification_property(noti, QP_NO_DELETE | QP_NO_TICKER);
166                         _bt_insert_notification(noti,
167                                 BT_STR_RECEIVED, str,
168                                 BT_ICON_QP_RECEIVE);
169                         ad->receive_noti = noti;
170                 }
171
172                 bt_share_release_tr_data_list(tr_data_list);
173                 tr_data_list = NULL;
174                 list_iter = NULL;
175         }
176
177         bt_share_close_db(db);
178
179         return;
180 }
181
182 static notification_h __bt_update_notification_adapter_status(void)
183 {
184         notification_h noti;
185         notification_error_e ret;
186
187         noti  = _bt_create_notification(BT_NOTI_T);
188         if (!noti)
189                 return NULL;
190
191         ret = notification_set_property(noti, QP_NO_DELETE | QP_NO_TICKER);
192         if (ret != NOTIFICATION_ERROR_NONE) {
193                 goto failed;
194         }
195
196         ret = notification_set_application(noti, "ug-bluetooth-efl");
197         if (ret != NOTIFICATION_ERROR_NONE) {
198                 goto failed;
199         }
200
201         ret = notification_set_display_applist(noti,
202                          NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
203         if (ret != NOTIFICATION_ERROR_NONE) {
204                 goto failed;
205         }
206
207         _bt_insert_notification(noti,
208                         BT_STR_BLUETOOTH_ON, BT_STR_BLUETOOTH_AVAILABLE,
209                         BT_ICON_QP_BT_ON);
210         return noti;
211
212 failed:
213         ERR("Fail to register notification");
214         notification_free(noti);
215         return NULL;
216
217 }
218
219 static gboolean __bt_dbus_request_name(void)
220 {
221         int ret_code = 0;
222         DBusConnection *conn;
223         DBusError err;
224
225         dbus_error_init(&err);
226
227         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
228
229         if (dbus_error_is_set(&err))
230                 goto failed;
231
232         ret_code = dbus_bus_request_name(conn,
233                                         BLUETOOTH_SHARE_BUS,
234                                         DBUS_NAME_FLAG_DO_NOT_QUEUE,
235                                         &err);
236         if (dbus_error_is_set(&err))
237                 goto failed;
238
239         if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER == ret_code) {
240                 dbus_connection_unref(conn);
241                 return TRUE;
242         }
243
244 failed:
245         if (dbus_error_is_set(&err)) {
246                 ERR("D-Bus Error: %s\n", err.message);
247                 dbus_error_free(&err);
248         }
249
250         if (!conn)
251                 dbus_connection_unref(conn);
252
253
254         return FALSE;
255 }
256
257 int _bt_init_obex_server(void)
258 {
259         char storage[STORAGE_PATH_LEN_MAX];
260
261         _bt_get_default_storage(storage);
262         if (bluetooth_obex_server_init(storage) !=
263                                         BLUETOOTH_ERROR_NONE) {
264                 DBG("Fail to init obex server");
265                 return BT_SHARE_FAIL;
266         }
267
268         bluetooth_obex_server_set_root(BT_FTP_FOLDER);
269
270         return BT_SHARE_ERROR_NONE;
271 }
272
273 void _bt_terminate_app(void)
274 {
275         if (main_loop) {
276                 g_main_loop_quit(main_loop);
277         }
278 }
279
280 int main(void)
281 {
282         int ret;
283         struct bt_appdata ad;
284         DBG("Starting bluetooth-share daemon");
285         memset(&ad, 0, sizeof(struct bt_appdata));
286         app_state = &ad;
287
288         signal(SIGTERM, __bt_sigterm_handler);
289
290         g_type_init();
291
292         if (__bt_dbus_request_name() == FALSE) {
293                 DBG("Aleady dbus instance existed\n");
294                 exit(0);
295         }
296
297         /* init internationalization */
298         if (appcore_set_i18n(BT_COMMON_PKG, BT_COMMON_RES) < 0)
299                 return -1;
300
301         /* Set the uid / gid to 5000 */
302         if (set_app_privilege("com.samsung.bluetooth-share", NULL, NULL) !=
303                                                         PC_OPERATION_SUCCESS)
304                 ERR("Failed to set app privilege.\n");
305
306         bluetooth_register_callback(_bt_share_event_handler, NULL);
307         ret = bluetooth_opc_init();
308         if (ret != BLUETOOTH_ERROR_NONE) {
309                 ERR("bluetooth_opc_init failed!!\n");
310                 return -1;
311         }
312
313         _bt_init_dbus_signal();
314         _bt_init_vconf_notification();
315         __bt_update_notification_status_values();
316         _bt_register_notification_cb(&ad);
317
318         if (_bt_init_obex_server() == BT_SHARE_ERROR_NONE)
319                 ad.obex_server_init = TRUE;
320
321         if (terminated == TRUE) {
322                 __bt_release_service(&ad);
323                 return -1;
324         }
325
326         notification_h noti;
327         noti = __bt_update_notification_adapter_status();
328
329         main_loop = g_main_loop_new(NULL, FALSE);
330         g_main_loop_run(main_loop);
331
332         _bt_delete_notification(noti);
333         __bt_release_service(&ad);
334
335         return 0;
336 }