[cleanup] revise file location
[platform/core/connectivity/bluetooth-share.git] / app / bt-share-ipc.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 <time.h>
22 #include <sys/stat.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <notification.h>
26 #include <aul.h>
27 #include <vconf.h>
28 #include <bundle_internal.h>
29
30 #include "applog.h"
31 #include "bt-share-main.h"
32 #include "bt-share-ipc.h"
33 #include "bt-share-syspopup.h"
34 #include "bt-share-notification.h"
35 #include "bt-share-resource.h"
36 #include "obex-event-handler.h"
37 #include "bluetooth-share-api.h"
38 #include "bt-share-common.h"
39 #include "bt-share-cynara.h"
40
41 #define FILEPATH_LEN_MAX 4096
42
43 #define SIGNAL_COUNT_TO_SUBSCRIBE 7
44
45 GSList *bt_transfer_list = NULL;
46 static GDBusConnection *gdbus_connection = NULL;
47 static GDBusProxy *core_proxy = NULL;
48
49 extern bt_appdata_t *app_state;
50
51 static void __bt_create_send_data(opc_transfer_info_t *node);
52 static void __bt_create_send_failed_data(char *filepath, char *dev_name,
53                                                 char *addr, char *type, unsigned int size);
54 static void __bt_share_update_tr_info(int tr_uid, int tr_type);
55 static void __free_transfer_info(opc_transfer_info_t *node);
56 static int __request_file_send(opc_transfer_info_t *node);
57 static GDBusConnection *__bt_get_system_gconn(void);
58
59 static void __bt_tr_data_free(bt_tr_data_t *data)
60 {
61         retm_if(data == NULL, "Invalid param");
62
63         g_free(data->file_path);
64         g_free(data->dev_name);
65         g_free(data->addr);
66         g_free(data->type);
67         g_free(data->content);
68         g_free(data->db_sid);
69         g_free(data);
70 }
71
72 /* LCOV_EXCL_START */
73 static void __popup_res_cb(int res)
74 {
75         FN_START;
76         bt_appdata_t *ad = app_state;
77
78         retm_if(ad->popups.syspopup_request == FALSE, "This event is not mine\n");
79
80         DBG(" res : %d", res);
81         /* Inorder to support calling popup from callback, we have to make
82          * syspopup_request false here and also should not assign
83          * ad->popups.popup_cb = NULL */
84
85         ad->popups.syspopup_request = FALSE;
86
87         if (NULL != ad->popups.popup_cb) {
88                 if (res == 0)
89                         ad->popups.popup_cb(ad->popups.popup_cb_data,
90                                             NULL, (void *)POPUP_RESPONSE_OK);
91                 else if (res == 1)
92                         ad->popups.popup_cb(ad->popups.popup_cb_data,
93                                             NULL, (void *)POPUP_RESPONSE_CANCEL);
94                 else if (res == 2)
95                         ad->popups.popup_cb(ad->popups.popup_cb_data,
96                                             NULL, (void *)POPUP_RESPONSE_TIMEOUT);
97         }
98         FN_END;
99 }
100 /* LCOV_EXCL_STOP */
101
102 /* LCOV_EXCL_START */
103 static char *__bt_transfer_folder_path(char *dest_path)
104 {
105         char *dst_path = (char *)g_malloc0(STORAGE_PATH_LEN_MAX);
106         if (dst_path == NULL) {
107                 ERR("Not enough memory!");
108                 return NULL;
109         }
110
111         if (g_str_has_prefix(dest_path, BT_OBEX_PATH_PREFIX))
112                 snprintf(dst_path, STORAGE_PATH_LEN_MAX, BT_OBEX_DEFAULT_PATH"%s", dest_path + strlen(BT_OBEX_PATH_PREFIX));
113         else
114                 snprintf(dst_path, STORAGE_PATH_LEN_MAX, "%s", dest_path);
115
116         DBG("obex transfed path : %s", dst_path);
117         return dst_path;
118 }
119 /* LCOV_EXCL_STOP */
120
121 /* LCOV_EXCL_START */
122 static opc_transfer_info_t *__add_transfer_info(GVariant *msg)
123 {
124         FN_START;
125         retv_if(msg == NULL, NULL);
126
127         int i = 0;
128         int cnt = 0;
129         unsigned int file_size = 0;
130         char *name = NULL;
131         char *type = NULL;
132         char byte = 0;
133         char *file_path = NULL;
134         opc_transfer_info_t *data = NULL;
135         bt_appdata_t *ad = app_state;
136         GSList *list = NULL;
137         char addr[BLUETOOTH_ADDRESS_LENGTH] = {0, };
138         char mime_type[BT_MIME_TYPE_MAX_LEN] = {0, };
139         char bd_addr[BT_ADDRESS_STRING_SIZE] = {0, };
140         char file[FILEPATH_LEN_MAX] = {0, };
141         struct stat file_attr = {0, };
142
143         GVariantIter *iter_addr;
144         GVariantIter *iter_filepath;
145         GVariantIter *iter_file;
146
147         g_variant_get(msg, "(ayssaay)", &iter_addr, &name, &type, &iter_filepath);
148
149         while (g_variant_iter_loop(iter_addr, "y", &byte))
150                 addr[i++] = byte;
151         g_variant_iter_free(iter_addr);
152
153         while (g_variant_iter_loop(iter_filepath, "ay", &iter_file)) {
154                 char *dst_path = NULL;
155                 i = 0;
156                 memset(file, 0, FILEPATH_LEN_MAX);
157
158                 while (g_variant_iter_loop(iter_file, "y", &byte))
159                         file[i++] = byte;
160
161                 file_path = g_strdup(file);
162
163                 dst_path = __bt_transfer_folder_path(file_path);
164                 if (dst_path != NULL) {
165                         g_free(file_path);
166                         file_path = dst_path;
167                 }
168
169                 if (aul_get_mime_from_file(file_path, mime_type,
170                                 BT_MIME_TYPE_MAX_LEN) == AUL_R_OK)
171                         INFO("mime type = %s", mime_type);
172
173                 if (g_utf8_validate(file_path, -1, NULL)) {
174                         if (stat(file_path, &file_attr) == 0)
175                                 file_size = file_attr.st_size;
176                         else
177                                 file_size = 0;
178
179                         INFO("%d", file_size);
180                         list = g_slist_append(list, file_path);
181                 } else {
182                         DBG("Invalid filepath");
183                         ad->send_data.tr_fail++;
184                         __bt_create_send_failed_data(file_path, name, addr,
185                                                                 mime_type, file_size);
186
187                         _bt_convert_addr_type_to_string(bd_addr, addr);
188                         DBG_SECURE("bd_addr = [%s]", bd_addr);
189                         if (ad->send_noti == NULL) {
190                                 ad->send_noti = _bt_insert_notification(ad,
191                                                         BT_SENT_NOTI, name,
192                                                         bd_addr, NULL);
193                         } else {
194                                 _bt_update_notification(ad, ad->send_noti,
195                                                 NULL, NULL, NULL, NULL, NULL);
196                         }
197
198                         g_free(file_path);
199                         file_path = NULL;
200                 }
201         }
202
203         g_variant_iter_free(iter_filepath);
204
205         cnt = g_slist_length(list);
206         INFO("cnt = %d", cnt);
207
208         if (cnt == 0) {
209                 /* Show unable to send popup */
210                 _bt_create_warning_popup(BLUETOOTH_ERROR_INTERNAL,
211                         BT_STR_UNABLE_TO_SEND);
212
213                 g_slist_free_full(list, g_free);
214                 g_free(name);
215                 g_free(type);
216                 return NULL;
217         }
218
219         INFO("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", addr[0],
220             addr[1], addr[2], addr[3], addr[4], addr[5]);
221         INFO(" cnt( %d )", cnt);
222         DBG_SECURE(" name ( %s )", name);
223
224         data = g_new0(opc_transfer_info_t, 1);
225         data->content = g_new0(char *, cnt + 1);
226         data->file_path = g_new0(char *, cnt + 1);
227         data->file_cnt = cnt;
228         data->type = g_new0(char *, cnt + 1);
229         data->size = (unsigned int *)g_new0(unsigned int *, cnt + 1);
230         memcpy(data->addr, addr, BLUETOOTH_ADDRESS_LENGTH);
231         memcpy(data->name, name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
232
233         for (i = 0; i < cnt; i++) {
234                 char *ptr = g_slist_nth_data(list, i);
235                 DBG_SECURE("%s", ptr);
236                 if (g_strcmp0(type, "text") == 0)
237                         data->file_path[i] = _bt_share_create_transfer_file(ptr);
238                 else
239                         data->file_path[i] = g_strdup(ptr);
240
241                 if (aul_get_mime_from_file(data->file_path[i], mime_type,
242                         BT_MIME_TYPE_MAX_LEN) == AUL_R_OK) {
243                         g_free(data->type[i]);
244                         data->type[i] = g_strdup(mime_type);
245                 }
246
247                 if (stat(data->file_path[i], &file_attr) == 0)
248                         file_size = file_attr.st_size;
249                 else
250                         file_size = 0;
251
252                 data->size[i] = file_size;
253                 data->content[i] = g_strdup(ptr);
254                 INFO(" type ( %s ), size (%d)", data->type[i], data->size[i]);
255         }
256
257         bt_transfer_list = g_slist_append(bt_transfer_list, data);
258
259         g_slist_free_full(list, g_free);
260         g_free(name);
261         g_free(type);
262
263         return data;
264 }
265 /* LCOV_EXCL_STOP */
266
267 static void __free_transfer_info(opc_transfer_info_t *node)
268 {
269         FN_START;
270         ret_if(node == NULL);
271
272         int i = 0;
273
274         for (i = 0; i < node->file_cnt; i++) {
275                 g_free(node->file_path[i]);
276                 g_free(node->content[i]);
277                 g_free(node->type[i]);
278         }
279
280         g_free(node->file_path);
281         g_free(node->content);
282         g_free(node->type);
283         g_free(node->size);
284         g_free(node);
285
286         FN_END;
287 }
288
289 void _remove_transfer_info(opc_transfer_info_t *node)
290 {
291         FN_START;
292         ret_if(node == NULL);
293
294         bt_transfer_list = g_slist_remove(bt_transfer_list, node);
295         __free_transfer_info(node);
296
297         FN_END;
298 }
299
300 /* LCOV_EXCL_START */
301 static int __request_file_send(opc_transfer_info_t *node)
302 {
303         FN_START;
304         int ret = 0;
305
306         retv_if(app_state == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
307         retv_if(node == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
308
309         ret = bluetooth_opc_push_files((bluetooth_device_address_t *)node->addr,
310                                      node->file_path);
311         retvm_if(ret != BLUETOOTH_ERROR_NONE, ret,
312                         "bluetooth_opc_push_files failed : %d", ret);
313
314         __bt_create_send_data(node);
315
316         FN_END;
317         return BLUETOOTH_ERROR_NONE;
318 }
319 /* LCOV_EXCL_STOP */
320
321 /* LCOV_EXCL_START */
322 static void __event_filter(GDBusConnection *connection,
323                 const gchar *sender_name,
324                 const gchar *object_path,
325                 const gchar *interface_name,
326                 const gchar *signal_name,
327                 GVariant *parameters,
328                 gpointer user_data)
329 {
330         int ret = 0;
331         bt_appdata_t *ad = app_state;
332 #ifdef CYNARA_ENABLE
333         const char *sender;
334         bt_share_cynara_creds sender_creds;
335 #endif
336
337 /*
338         DBG("Path = %s", object_path);
339 */
340         ret_if(object_path == NULL || strcmp(object_path, "/") == 0);
341         ret_if(interface_name == NULL || signal_name == NULL);
342
343         if (!g_strcmp0(interface_name, BT_SYSPOPUP_INTERFACE)) {
344                 DBG("syspopup signal_name = %s", signal_name);
345                 if (!g_strcmp0(signal_name, BT_SYSPOPUP_METHOD_RESPONSE)) {
346                         int res = 0;
347                         g_variant_get(parameters, "(i)", &res);
348                         __popup_res_cb(res);
349                 }
350         } else if (!g_strcmp0(interface_name, BT_UG_IPC_INTERFACE)) {
351                 DBG("ug signal_name = %s", signal_name);
352                 if (!g_strcmp0(signal_name, BT_UG_IPC_METHOD_SEND)) {
353                         opc_transfer_info_t *node = NULL;
354
355 #ifdef CYNARA_ENABLE
356                 sender = dbus_message_get_sender(msg);
357                 ret = _bt_share_cynara_get_creds(sys_conn, sender, &sender_creds);
358                 if (ret != 0) {
359                         ERR("acquiring cynara creds failed\n");
360                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
361                 }
362                 if (_bt_share_cynara_check(&sender_creds, BT_SHARE_PRIVILEGE) != BT_SHARE_FAIL) {
363                         ERR("Cynara denied file send\n");
364                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
365                 }
366 #endif
367                         node = __add_transfer_info(parameters);
368                         retm_if(node == NULL, "node is NULL");
369
370                         ret = __request_file_send(node);
371                         if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
372                                 INFO("Aleady OPC progressing. Once completed previous job, will be started");
373                         } else if (ret != BLUETOOTH_ERROR_NONE) {
374                                 _bt_create_warning_popup(BLUETOOTH_ERROR_INTERNAL, BT_STR_UNABLE_TO_SEND);
375                                 g_slist_free_full(bt_transfer_list, (GDestroyNotify)__free_transfer_info);
376                                 bt_transfer_list = NULL;
377                         }
378                 }
379         } else if (!g_strcmp0(interface_name, BT_SHARE_UI_INTERFACE)) {
380                 DBG("share ui signal_name = %s", signal_name);
381
382                 if (!g_strcmp0(signal_name, BT_SHARE_UI_SIGNAL_OPPABORT)) {
383                         const gchar *transfer_type = NULL;
384                         int noti_id = 0;
385
386                         g_variant_get(parameters, "(&si)", &transfer_type, &noti_id);
387
388                         ad->opp_transfer_abort = TRUE; /* Transfer aborted by user*/
389                         INFO("transfer_type = %s", transfer_type);
390                         if (!g_strcmp0(transfer_type, NOTI_TR_TYPE_OUT)) {
391                                 bluetooth_opc_cancel_push();
392                                 if (ad->opc_noti) {
393                                         ret = _bt_delete_notification(ad->opc_noti);
394                                         if (ret == NOTIFICATION_ERROR_NONE) {
395                                                 ad->opc_noti = NULL;
396                                                 ad->opc_noti_id = 0;
397                                         }
398                                 }
399                         } else {
400                                 bluetooth_obex_server_cancel_transfer(noti_id);
401                         }
402                 } else if (!g_strcmp0(signal_name, BT_SHARE_UI_SIGNAL_SEND_FILE)) {
403                         opc_transfer_info_t *node = NULL;
404
405 #ifdef CYNARA_ENABLE
406                         sender = dbus_message_get_sender(msg);
407                         ret = _bt_share_cynara_get_creds(sys_conn, sender, &sender_creds);
408                         if (ret != 0) {
409                                 ERR("acquiring cynara creds failed\n");
410                                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
411                         }
412
413                         if (_bt_share_cynara_check(&sender_creds, BT_SHARE_PRIVILEGE) != BT_SHARE_FAIL) {
414                                 ERR("Cynara denied file send\n");
415                                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
416                         }
417 #endif
418                         node = __add_transfer_info(parameters);
419                         retm_if(node == NULL, "node is NULL");
420
421                         ret = __request_file_send(node);
422                         if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
423                                 INFO("Aleady OPC progressing. Once completed previous job, will be started");
424                         } else if (ret != BLUETOOTH_ERROR_NONE) {
425                                 _bt_create_warning_popup(BLUETOOTH_ERROR_INTERNAL, BT_STR_UNABLE_TO_SEND);
426                                 g_slist_free_full(bt_transfer_list, (GDestroyNotify)__free_transfer_info);
427                                 bt_transfer_list = NULL;
428                         }
429                 } else if (!g_strcmp0(signal_name, BT_SHARE_UI_SIGNAL_INFO_UPDATE)) {
430                         int tr_uid = 0;
431                         int tr_type = 0;
432
433                         g_variant_get(parameters, "(ii)", &tr_uid, &tr_type);
434
435                         __bt_share_update_tr_info(tr_uid, tr_type);
436                 }
437         } else if (!g_strcmp0(interface_name, BT_SHARE_FRWK_INTERFACE)) {
438                 DBG("frwk signal_name = %s", signal_name);
439
440                 if (!g_strcmp0(signal_name, BT_SHARE_FRWK_SIGNAL_DEINIT)) {
441                         /* Deinitialize the obex server */
442                         if (bluetooth_obex_server_deinit() == BLUETOOTH_ERROR_NONE)
443                                 DBG("Obex Server deinit");
444                 }
445         } else if (!g_strcmp0(interface_name, BT_BLUEZ_INTERFACE)) {
446                 if (!g_strcmp0(signal_name, BT_BLUEZ_SIGNAL_NAME_OWNER_CHANGED)) {
447                         char *name = NULL;
448                         char *previous = NULL;
449                         char *current = NULL;
450
451                         g_variant_get(parameters, "(&s&s&s)", &name, &previous, &current);
452
453                         if (*current != '\0')
454                                 return;
455
456                         if (strcasecmp(name, "org.projectx.bt") == 0) {
457                                 INFO("bt-service is terminated");
458                                 _bt_terminate_bluetooth_share();
459                         }
460                 }
461         } else {
462                 DBG("Unhandled signal");
463         }
464 }
465 /* LCOV_EXCL_STOP */
466
467 static int __bt_share_add_filter_subscribe_signal(GDBusConnection *conn,
468                 gboolean subscribe)
469 {
470         struct {
471                 char *interface;
472                 char *member;
473         } match_string[SIGNAL_COUNT_TO_SUBSCRIBE] = {
474                         { BT_SYSPOPUP_INTERFACE, BT_SYSPOPUP_METHOD_RESPONSE },
475                         { BT_UG_IPC_INTERFACE, BT_UG_IPC_METHOD_SEND},
476                         { BT_SHARE_UI_INTERFACE, BT_SHARE_UI_SIGNAL_OPPABORT},
477                         { BT_SHARE_UI_INTERFACE, BT_SHARE_UI_SIGNAL_SEND_FILE},
478                         { BT_SHARE_UI_INTERFACE, BT_SHARE_UI_SIGNAL_INFO_UPDATE},
479                         { BT_SHARE_FRWK_INTERFACE, BT_SHARE_FRWK_SIGNAL_DEINIT},
480                         { BT_BLUEZ_INTERFACE, BT_BLUEZ_SIGNAL_NAME_OWNER_CHANGED}
481         };
482
483         static guint subs_add_filter_id[SIGNAL_COUNT_TO_SUBSCRIBE] = {0, };
484         int i;
485
486         retv_if(conn == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
487
488         if (subscribe == FALSE) {
489                 for (i = 0; i < SIGNAL_COUNT_TO_SUBSCRIBE; ++i) {
490                         if (subs_add_filter_id[i] != 0) {
491                                 g_dbus_connection_signal_unsubscribe(conn, subs_add_filter_id[i]);
492                                 subs_add_filter_id[i] = 0;
493                         }
494                 }
495                 return BLUETOOTH_ERROR_NONE;
496         }
497
498         for (i = 0; i < SIGNAL_COUNT_TO_SUBSCRIBE; ++i) {
499                 if (subs_add_filter_id[i] == 0) {
500                         subs_add_filter_id[i] = g_dbus_connection_signal_subscribe(
501                                         conn, NULL, match_string[i].interface,
502                                         match_string[i].member, NULL, NULL,
503                                         G_DBUS_SIGNAL_FLAGS_NONE, __event_filter, NULL, NULL);
504                 }
505         }
506         return BLUETOOTH_ERROR_NONE;
507 }
508
509 gboolean _bt_init_dbus_signal(void)
510 {
511         FN_START;
512         GError *error = NULL;
513         GDBusConnection *conn = NULL;
514
515         conn = __bt_get_system_gconn();
516         if (!conn) {
517                 /* LCOV_EXCL_START */
518                 ERR("GDBUS get failed");
519                 g_error_free(error);
520                 return FALSE;
521                 /* LCOV_EXCL_STOP */
522         }
523
524         gdbus_connection = conn;
525
526         /* Add the filter for network client functions */
527         __bt_share_add_filter_subscribe_signal(conn, TRUE);
528
529         FN_END;
530         return TRUE;
531 }
532
533 void _bt_deinit_dbus_signal(void)
534 {
535         FN_START;
536         __bt_share_add_filter_subscribe_signal(gdbus_connection, FALSE);
537         FN_END;
538 }
539
540 void _bt_send_disconnected_signal(char *signal, int result,
541                 char *address, int transfer_id)
542 {
543         FN_START;
544         retm_if(gdbus_connection == NULL, "gdbus_connection == NULL");
545
546         GDBusMessage *msg = NULL;
547         GVariant *param = NULL;
548
549         msg = g_dbus_message_new_signal(BT_SHARE_ENG_OBJECT,
550                                 BT_SHARE_ENG_INTERFACE,
551                                 signal);
552         retm_if(msg == NULL, "Unable to allocate memory");
553
554         param = g_variant_new("(isi)", result, address, transfer_id);
555         g_dbus_message_set_body(msg, param);
556
557         g_dbus_message_set_destination(msg, BT_SHARE_UI_INTERFACE);
558         g_dbus_connection_send_message(gdbus_connection, msg,
559                         G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
560
561         g_object_unref(msg);
562         FN_END;
563 }
564
565 void _bt_update_transfer_list_view(char *db)
566 {
567         ret_if(gdbus_connection == NULL);
568
569         GDBusMessage *msg = NULL;
570         GVariant *param = NULL;
571
572         msg = g_dbus_message_new_signal(BT_SHARE_ENG_OBJECT,
573                                 BT_SHARE_ENG_INTERFACE,
574                                 BT_SHARE_ENG_SIGNAL_UPDATE_VIEW);
575         retm_if(!msg, "Unable to allocate memory");
576
577         param = g_variant_new("(s)", db);
578         g_dbus_message_set_body(msg, param);
579
580         g_dbus_message_set_destination(msg, BT_SHARE_UI_INTERFACE);
581         g_dbus_connection_send_message(gdbus_connection, msg,
582                         G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
583
584         g_object_unref(msg);
585 }
586
587 void _bt_create_warning_popup(int error_type, char *msg)
588 {
589         /* If bluetooth-share-ui process is  not running */
590         /* Then create the process and terminate it after popup shown */
591         if (aul_app_is_running(UI_PACKAGE) == 0) {
592                 DBG("Creating new process for Warning Popup");
593                 char str[BT_TEXT_LEN_MAX] = { 0, };
594                 bundle *b = NULL;
595
596                 DBG("error_type: %d", error_type);
597                 switch (error_type) {
598                 case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
599                 case BLUETOOTH_ERROR_NOT_CONNECTED:
600                 case BLUETOOTH_ERROR_ACCESS_DENIED:
601                 case BLUETOOTH_ERROR_OUT_OF_MEMORY:
602                 case BLUETOOTH_ERROR_INTERNAL:
603                 case BLUETOOTH_ERROR_CANCEL:
604                         snprintf(str, BT_TEXT_LEN_MAX, "%s",
605                                  msg);
606                         break;
607                 default:
608                         return;
609                 }
610
611                 b = bundle_create();
612                 ret_if(b == NULL);
613
614                 bundle_add(b, "launch-type", "warning_popup");
615                 bundle_add(b, "message", str);
616
617                 aul_launch_app(UI_PACKAGE, b);
618
619                 bundle_free(b);
620         }
621         return;
622 }
623
624 static time_t __bt_get_current_timedata(void)
625 {
626         time_t t = 0;
627         time(&t);
628         return ((int)t);
629 }
630
631 /* LCOV_EXCL_START */
632 static char *__bt_conv_addr_type_to_addr_string(char *addr)
633 {
634         retv_if(addr == NULL, NULL);
635         char address[BT_ADDR_STR_LEN_MAX] = {0, };
636
637         snprintf(address, BT_ADDR_STR_LEN_MAX,
638                  "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
639                  addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
640
641         return g_strdup(address);
642 }
643 /* LCOV_EXCL_STOP */
644
645 /* LCOV_EXCL_START */
646 static void __bt_create_send_failed_data(char *filepath, char *dev_name,
647                                          char *addr, char *type, unsigned int size)
648 {
649         int session_id = -1;
650         sqlite3 *db = NULL;
651
652         db = bt_share_open_db();
653         ret_if(!db);
654
655         session_id = bt_share_get_last_session_id(db, BT_DB_OUTBOUND);
656         INFO("Last session id = %d", session_id);
657
658         bt_tr_data_t *tmp = NULL;
659         tmp = g_malloc0(sizeof(bt_tr_data_t));
660         ret_if(tmp == NULL);
661
662         tmp->tr_status = BT_TR_FAIL;
663         tmp->sid = session_id + 1;
664         tmp->file_path = g_strdup(filepath);
665         tmp->content = g_strdup(filepath);
666         tmp->dev_name = g_strdup(dev_name);
667         tmp->type = g_strdup(type);
668         tmp->timestamp = __bt_get_current_timedata();
669         tmp->addr = __bt_conv_addr_type_to_addr_string(addr);
670         tmp->size = size;
671         bt_share_add_tr_data(db, BT_DB_OUTBOUND, tmp);
672         __bt_tr_data_free(tmp);
673
674         bt_share_close_db(db);
675 }
676 /* LCOV_EXCL_STOP */
677
678 /* LCOV_EXCL_START */
679 static void __bt_create_send_data(opc_transfer_info_t *node)
680 {
681         FN_START;
682         ret_if(node == NULL);
683
684         int count = 0;
685         int session_id = -1;
686         char *snd_db_sid = NULL;
687         sqlite3 *db = NULL;
688
689         DBG_SECURE("Name [%s]", node->name);
690
691         db = bt_share_open_db();
692         ret_if(!db);
693
694         session_id = bt_share_get_last_session_id(db, BT_DB_OUTBOUND);
695         snd_db_sid = _bt_get_time_of_the_day();
696         INFO("Last session id = %d", session_id);
697
698         for (count = 0; count < node->file_cnt; count++) {
699                 bt_tr_data_t *tmp;
700                 tmp = g_malloc0(sizeof(bt_tr_data_t));
701                 if (tmp == NULL) {
702                         g_free(snd_db_sid);
703                         return;
704                 }
705
706                 tmp->tr_status = BT_TR_PENDING;
707                 tmp->sid = session_id + 1;
708                 tmp->file_path = g_strdup(node->file_path[count]);
709                 DBG("tmp->file_path : %s", tmp->file_path);
710
711                 tmp->content = g_strdup(node->content[count]);
712                 tmp->dev_name = g_strdup(node->name);
713                 tmp->type = g_strdup(node->type[count]);
714                 tmp->timestamp = __bt_get_current_timedata();
715                 tmp->addr = __bt_conv_addr_type_to_addr_string(node->addr);
716                 tmp->size = node->size[count];
717                 tmp->db_sid = g_strdup(snd_db_sid);
718                 bt_share_add_tr_data(db, BT_DB_OUTBOUND, tmp);
719                 __bt_tr_data_free(tmp);
720         }
721
722         bt_share_close_db(db);
723         g_free(snd_db_sid);
724
725         FN_END;
726 }
727 /* LCOV_EXCL_STOP */
728
729 gboolean _bt_update_sent_data_status(int uid,
730                                 bt_app_tr_status_e status, char *db_sid)
731 {
732         INFO("uid = %d", uid);
733         sqlite3 *db = NULL;
734         bt_tr_data_t *tmp = NULL;
735
736         db = bt_share_open_db();
737         retv_if(!db, FALSE);
738
739         tmp = g_malloc0(sizeof(bt_tr_data_t));
740         retv_if(!tmp, FALSE);
741
742         tmp->tr_status = status;
743         tmp->timestamp = __bt_get_current_timedata();
744         tmp->db_sid = g_strdup(db_sid);
745         bt_share_update_tr_data(db, BT_DB_OUTBOUND, uid, tmp);
746         bt_share_close_db(db);
747
748         __bt_tr_data_free(tmp);
749         return TRUE;
750 }
751
752 gboolean _bt_update_recv_data_status(int uid,
753                 bt_app_tr_status_e status, char *db_sid,
754                 unsigned int filesize, const char *mime_type)
755 {
756         INFO("uid = %d", uid);
757         sqlite3 *db = NULL;
758         bt_tr_data_t *tmp = NULL;
759
760         db = bt_share_open_db();
761         retv_if(!db, FALSE);
762
763         tmp = g_malloc0(sizeof(bt_tr_data_t));
764         retv_if(!tmp, FALSE);
765
766         tmp->tr_status = status;
767         tmp->timestamp = __bt_get_current_timedata();
768         tmp->db_sid = g_strdup(db_sid);
769         tmp->type = g_strdup(mime_type);
770         tmp->size = filesize;
771         bt_share_update_tr_data(db, BT_DB_INBOUND, uid, tmp);
772         bt_share_close_db(db);
773
774         __bt_tr_data_free(tmp);
775         return TRUE;
776 }
777
778 gboolean _bt_update_multiple_sent_data_status(int *uids, int uid_cnt,
779                                 bt_app_tr_status_e status, char *db_sid)
780 {
781         sqlite3 *db = NULL;
782         bt_tr_data_t *tmp = NULL;
783
784         db = bt_share_open_db();
785         retv_if(!db, FALSE);
786
787         tmp = g_malloc0(sizeof(bt_tr_data_t));
788         tmp->tr_status = status;
789         tmp->timestamp = __bt_get_current_timedata();
790         tmp->db_sid = g_strdup(db_sid);
791         bt_share_update_multiple_tr_data(db, BT_DB_OUTBOUND, uids, uid_cnt, tmp);
792         bt_share_close_db(db);
793
794         __bt_tr_data_free(tmp);
795         return TRUE;
796 }
797
798 gboolean _bt_add_recv_transfer_status_data(char *device_name,
799                         char *address, char *filepath, char *type,
800                         unsigned int size, int status, char *db_sid)
801 {
802         retv_if(device_name == NULL || filepath == NULL, FALSE);
803
804         sqlite3 *db = NULL;
805         bt_tr_data_t *tmp = NULL;
806
807         DBG_SECURE("Name [%s]", device_name);
808
809         db = bt_share_open_db();
810         retv_if(!db, FALSE);
811
812         tmp = g_malloc0(sizeof(bt_tr_data_t));
813
814         tmp->tr_status = status;
815         tmp->file_path = g_strdup(filepath);
816         tmp->dev_name = g_strdup(device_name);
817         tmp->addr = g_strdup(address);
818         tmp->db_sid = g_strdup(db_sid);
819         tmp->timestamp = __bt_get_current_timedata();
820         tmp->type = g_strdup(type);
821         tmp->size = size;
822         bt_share_add_tr_data(db, BT_DB_INBOUND, tmp);
823         bt_share_close_db(db);
824
825         __bt_tr_data_free(tmp);
826         return TRUE;
827 }
828
829 int _bt_share_get_unique_id(int db_table, const char *db_sid)
830 {
831         sqlite3 *db = NULL;
832         int index_id = -1;
833
834         DBG("db_table = %d", db_table);
835         db = bt_share_open_db();
836         retv_if(!db, 0);
837
838         index_id = bt_share_get_unique_id(db, db_table, db_sid);
839         bt_share_close_db(db);
840         DBG("index_id = %d", index_id);
841         return index_id;
842 }
843
844 /* LCOV_EXCL_START */
845 static void __bt_share_update_tr_info(int tr_uid, int tr_type)
846 {
847         DBG("tr_uid = %d", tr_uid);
848
849         int status = -1;
850         bt_appdata_t *ad = app_state;
851         bt_tr_data_t *info = NULL;
852         sqlite3 *db = NULL;
853
854         db = bt_share_open_db();
855         ret_if(!db);
856
857         if (tr_type == BT_TR_OUTBOUND) {
858                 if (tr_uid == -1) {
859                         /* Click the "clear list" button in bluetooth-share-ui */
860                         /* Delete all outbound db / notification info */
861                         _bt_delete_notification(ad->send_noti);
862                         ad->send_noti = NULL;
863
864                         ad->send_data.tr_success = 0;
865                         ad->send_data.tr_fail = 0;
866                         DBG("clear all");
867                         bt_share_remove_tr_data_non_pending(db, BT_DB_OUTBOUND);
868                         bt_share_close_db(db);
869                 } else {
870                         /* Delete selected outbound db / notification info */
871
872                         info = bt_share_get_tr_data(db, BT_DB_OUTBOUND, tr_uid);
873                         if (info != NULL) {
874                                 status = info->tr_status;
875                                 __bt_tr_data_free(info);
876                         }
877
878                         bt_share_remove_tr_data_by_id(db, BT_DB_OUTBOUND,
879                                                       tr_uid);
880                         bt_share_close_db(db);
881
882                         if (status == BT_TR_SUCCESS && ad->send_data.tr_success > 0)
883                                 ad->send_data.tr_success--;
884                         else if (status == BT_TR_FAIL && ad->send_data.tr_fail > 0)
885                                 ad->send_data.tr_fail--;
886                         else
887                                 return;
888
889                         if ((ad->send_data.tr_success + ad->send_data.tr_fail) != 0) {
890                                 _bt_update_notification(ad, ad->send_noti,
891                                                 NULL, NULL, NULL, NULL, NULL);
892                         } else {
893                                 _bt_delete_notification(ad->send_noti);
894                         }
895                 }
896         } else if (tr_type == BT_TR_INBOUND) {
897                 if (tr_uid == -1) {
898                         /* Click the "clear list" button in bluetooth-share-ui */
899                         /* Delete all inbound db / notification info */
900
901                         _bt_delete_notification(ad->receive_noti);
902                         ad->receive_noti = NULL;
903                         ad->recv_data.tr_success = 0;
904                         ad->recv_data.tr_fail = 0;
905                         bt_share_remove_all_tr_data(db, BT_DB_INBOUND);
906                         bt_share_close_db(db);
907                 } else {
908                         /* Delete selected outbound db / notification info */
909
910                         info = bt_share_get_tr_data(db, BT_DB_INBOUND, tr_uid);
911                         if (info != NULL) {
912                                 status = info->tr_status;
913                                 __bt_tr_data_free(info);
914                         }
915
916                         bt_share_remove_tr_data_by_id(db, BT_DB_INBOUND, tr_uid);
917                         bt_share_close_db(db);
918
919                         if (status == BT_TR_SUCCESS && ad->recv_data.tr_success > 0)
920                                 ad->recv_data.tr_success--;
921                         else if (status == BT_TR_FAIL && ad->recv_data.tr_fail > 0)
922                                 ad->recv_data.tr_fail--;
923                         else
924                                 return;
925
926                         if ((ad->recv_data.tr_success + ad->recv_data.tr_fail) != 0) {
927                                 _bt_update_notification(ad, ad->receive_noti,
928                                                 NULL, NULL, NULL, NULL, NULL);
929                         } else {
930                                 _bt_delete_notification(ad->receive_noti);
931                         }
932                 }
933         } else {
934                 ERR("Invalid transfer type");
935                 bt_share_close_db(db);
936         }
937 }
938 /* LCOV_EXCL_STOP */
939
940 static GDBusConnection *__bt_init_system_gconn(void)
941 {
942         if (gdbus_connection == NULL)
943                 gdbus_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
944
945         return gdbus_connection;
946 }
947
948 static GDBusConnection *__bt_get_system_gconn(void)
949 {
950         return (gdbus_connection) ? gdbus_connection : __bt_init_system_gconn();
951 }
952
953 GDBusProxy *__bt_init_core_proxy(void)
954 {
955         GDBusProxy *proxy;
956         GDBusConnection *conn;
957
958         conn = __bt_get_system_gconn();
959         if (!conn)
960                 return NULL;
961
962         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
963                                         NULL,
964                                         BT_CORE_NAME,
965                                         BT_CORE_PATH,
966                                         BT_CORE_INTERFACE,
967                                         NULL, NULL);
968
969         if (!proxy)
970                 return NULL;
971
972         core_proxy = proxy;
973
974         return proxy;
975 }
976
977 static GDBusProxy *__bt_get_core_proxy(void)
978 {
979         return (core_proxy) ? core_proxy : __bt_init_core_proxy();
980 }
981
982 int _bt_set_eventsystem_transfer_value(gboolean value)
983 {
984         GDBusProxy *proxy;
985         GError *error = NULL;
986         GVariant *reply;
987         int ret = BT_SHARE_ERROR_NONE;
988
989         DBG("+");
990
991         proxy = __bt_get_core_proxy();
992         if (!proxy)
993                 return BT_SHARE_FAIL;
994
995         reply = g_dbus_proxy_call_sync(proxy, "SetTransferValue",
996                         g_variant_new("(b)", value),
997                         G_DBUS_CALL_FLAGS_NONE, -1,
998                         NULL, &error);
999
1000         if (reply == NULL) {
1001                 ERR("Failed to Set tranfer value by bt core");
1002                 ret = BT_SHARE_FAIL;
1003                 if (error) {
1004                         ERR("Error %s", error->message);
1005                         g_error_free(error);
1006                 }
1007         }
1008         g_variant_unref(reply);
1009
1010         return ret;
1011 }