Remove dbus-glib-1 dependency
[platform/core/connectivity/bluetooth-share.git] / bt-share / src / 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 <dbus/dbus.h>
21 #include <glib.h>
22 #include <time.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 "bluetooth-api.h"
32 #include "bt-share-main.h"
33 #include "bt-share-ipc.h"
34 #include "bt-share-syspopup.h"
35 #include "bt-share-notification.h"
36 #include "bt-share-resource.h"
37 #include "obex-event-handler.h"
38 #include "bluetooth-share-api.h"
39 #include "bt-share-common.h"
40 #include "bt-share-cynara.h"
41
42 #define FILEPATH_LEN_MAX 4096
43
44 GSList *bt_transfer_list = NULL;
45 DBusConnection *dbus_connection = NULL;
46
47 extern struct bt_appdata *app_state;
48
49 static void __bt_create_send_data(opc_transfer_info_t *node);
50
51 static void __bt_share_update_tr_info(int tr_uid, int tr_type);
52
53 static void __bt_tr_data_free(bt_tr_data_t *data)
54 {
55         retm_if(data == NULL, "Invalid param");
56
57         g_free(data->file_path);
58         g_free(data->dev_name);
59         g_free(data->addr);
60         g_free(data->type);
61         g_free(data->content);
62         g_free(data);
63 }
64
65 static void __popup_res_cb(int res)
66 {
67         DBG("+\n");
68         struct bt_appdata *ad = app_state;
69         if (ad->popups.syspopup_request == FALSE) {
70                         DBG("This event is not mine\n");
71                         return;
72         }
73         DBG(" res : %d\n", res);
74         /* Inorder to support calling popup from callback, we have to make
75          * syspopup_request false here and also should not assign
76          * ad->popups.popup_cb = NULL */
77
78         ad->popups.syspopup_request = FALSE;
79
80         if (NULL != ad->popups.popup_cb) {
81                 if (res == 0)
82                         ad->popups.popup_cb(ad->popups.popup_cb_data,
83                                 NULL,
84                                 (void *)POPUP_RESPONSE_OK);
85                 else if (res == 1)
86                         ad->popups.popup_cb(ad->popups.popup_cb_data,
87                                 NULL,
88                                 (void *)POPUP_RESPONSE_CANCEL);
89                 else if (res == 2)
90                         ad->popups.popup_cb(ad->popups.popup_cb_data,
91                                 NULL,
92                                 (void *)POPUP_RESPONSE_TIMEOUT);
93         }
94         DBG("-\n");
95 }
96
97
98 static opc_transfer_info_t *__add_transfer_info(DBusMessage *msg)
99 {
100         int reserved = 0;
101         int cnt = 0;
102         char *addr = NULL;
103         int len;
104         char *filepath = NULL;
105         char *mode = NULL;
106         char *name = NULL;
107         char *type = NULL;
108         opc_transfer_info_t *data;
109         int i = 0;
110         char *token = NULL;
111         char *ptr = NULL;
112
113         retv_if(msg == NULL, NULL);
114         dbus_message_get_args(msg, NULL,
115                                 DBUS_TYPE_INT32, &reserved,
116                                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
117                                 &addr, &len,
118                                 DBUS_TYPE_INT32, &cnt,
119                                 DBUS_TYPE_STRING, &filepath,
120                                 DBUS_TYPE_STRING, &mode,
121                                 DBUS_TYPE_STRING, &name,
122                                 DBUS_TYPE_STRING, &type,
123                                 DBUS_TYPE_INVALID);
124
125         retv_if(cnt <= 0, NULL);
126         retv_if(addr == NULL, NULL);
127         retv_if(filepath == NULL, NULL);
128         retv_if(name == NULL, NULL);
129
130         DBG("reserved ( %d )\n", reserved);
131         DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", addr[0],
132              addr[1], addr[2], addr[3], addr[4], addr[5]);
133         DBG(" cnt( %d )\n", cnt);
134         DBG(" filepath( %s )\n", filepath);
135         DBG(" mode ( %s )\n", mode);
136         DBG(" name ( %s )\n", name);
137         DBG(" type ( %s )\n", type);
138
139         data = g_new0(opc_transfer_info_t, 1);
140
141         data->content = g_new0(char *, cnt + 1);
142         data->file_path = g_new0(char *, cnt + 1);
143         data->file_cnt = cnt;
144         memcpy(data->addr, addr, BLUETOOTH_ADDRESS_LENGTH);
145         memcpy(data->name, name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
146         data->type = g_strdup(type);
147
148         token = strtok_r(filepath, FILE_PATH_DELIM, &ptr);
149         while ((token != NULL) && (i < cnt)) {
150                 if (g_strcmp0(type, "text") == 0) {
151                         data->file_path[i] = _bt_share_create_transfer_file(token);
152                 } else {
153                         data->file_path[i] = g_strdup(token);
154                 }
155
156                 DBG(" file path ( %s )\n", data->file_path[i]);
157
158                 data->content[i] = g_strdup(token);
159                 DBG("Content [%d] [%s]\n", i, data->content[i]);
160                 i++;
161
162                 token = strtok_r(NULL, FILE_PATH_DELIM, &ptr);
163         }
164
165         bt_transfer_list = g_slist_append(bt_transfer_list, data);
166
167         return data;
168 }
169
170
171 void _free_transfer_info(opc_transfer_info_t *node)
172 {
173         int i;
174
175         DBG("+\n");
176         ret_if(node == NULL);
177
178         for (i = 0; i < node->file_cnt; i++) {
179                 _bt_remove_tmp_file(node->file_path[i]);
180                 g_free(node->file_path[i]);
181                 g_free(node->content[i]);
182         }
183         g_free(node->file_path);
184         g_free(node->content);
185         g_free(node->type);
186         g_free(node);
187
188         DBG("-\n");
189 }
190
191 void _remove_transfer_info(opc_transfer_info_t *node)
192 {
193         DBG("+\n");
194         ret_if(node == NULL);
195
196         bt_transfer_list = g_slist_remove(bt_transfer_list, node);
197         _free_transfer_info(node);
198
199         DBG("-\n");
200 }
201
202 int _request_file_send(opc_transfer_info_t *node)
203 {
204         struct bt_appdata *ad = app_state;
205         int ret;
206         DBG("+\n");
207         retv_if(ad == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
208         retv_if(node == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
209
210         ret = bluetooth_opc_push_files((bluetooth_device_address_t *)node->addr,
211                                                 node->file_path);
212         if (ret != BLUETOOTH_ERROR_NONE) {
213                 DBG("bluetooth_opc_push_files failed : %d", ret);
214                 return ret;
215         }
216
217         __bt_create_send_data(node);
218
219         DBG("-\n");
220         return BLUETOOTH_ERROR_NONE;
221 }
222
223 static int __bt_get_owner_info(DBusMessage *msg, char **name,
224                 char **previous, char **current)
225 {
226         DBusMessageIter item_iter;
227
228         dbus_message_iter_init(msg, &item_iter);
229
230         if (dbus_message_iter_get_arg_type(&item_iter)
231                         != DBUS_TYPE_STRING) {
232                 ERR("This is bad format dbus\n");
233                 return BLUETOOTH_ERROR_INTERNAL;
234         }
235
236         dbus_message_iter_get_basic(&item_iter, name);
237
238         retv_if(*name == NULL, BLUETOOTH_ERROR_INTERNAL);
239
240         dbus_message_iter_next(&item_iter);
241
242         if (dbus_message_iter_get_arg_type(&item_iter)
243                         != DBUS_TYPE_STRING) {
244                 ERR("This is bad format dbus\n");
245                 return BLUETOOTH_ERROR_INTERNAL;
246         }
247
248         dbus_message_iter_get_basic(&item_iter, previous);
249
250         retv_if(*previous == NULL, BLUETOOTH_ERROR_INTERNAL);
251
252         dbus_message_iter_next(&item_iter);
253
254         if (dbus_message_iter_get_arg_type(&item_iter)
255                         != DBUS_TYPE_STRING) {
256                 ERR("This is bad format dbus\n");
257                 return BLUETOOTH_ERROR_INTERNAL;
258         }
259
260         dbus_message_iter_get_basic(&item_iter, current);
261
262         retv_if(*current == NULL, BLUETOOTH_ERROR_INTERNAL);
263
264         return BLUETOOTH_ERROR_NONE;
265 }
266
267 static DBusHandlerResult __event_filter(DBusConnection *sys_conn,
268                                                         DBusMessage *msg, void *data)
269 {
270         int ret;
271         char *member;
272         struct bt_appdata *ad = app_state;
273         const char *path = dbus_message_get_path(msg);
274
275 #ifdef CYNARA_ENABLE
276         const char *sender;
277         bt_share_cynara_creds sender_creds;
278 #endif
279
280         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
281                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
282
283         if (path == NULL || strcmp(path, "/") == 0)
284                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
285
286         member = (char *)dbus_message_get_member(msg);
287         DBG("member (%s)\n", member);
288
289         if (dbus_message_is_signal(msg, BT_SYSPOPUP_INTERFACE, BT_SYSPOPUP_METHOD_RESPONSE)) {
290                 int res = 0;
291                 dbus_message_get_args(msg, NULL,
292                                         DBUS_TYPE_INT32, &res,
293                                         DBUS_TYPE_INVALID);
294                 __popup_res_cb(res);
295         } else if (dbus_message_is_signal(msg, BT_UG_IPC_INTERFACE, BT_UG_IPC_METHOD_SEND)) {
296                 opc_transfer_info_t *node;
297
298 #ifdef CYNARA_ENABLE
299                 sender = dbus_message_get_sender(msg);
300                 ret = _bt_share_cynara_get_creds(sys_conn, sender, &sender_creds);
301                 if (ret != 0) {
302                         ERR("acquiring cynara creds failed\n");
303                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
304                 }
305
306                 if (_bt_share_cynara_check(&sender_creds, BT_SHARE_PRIVILEGE) != BT_SHARE_FAIL) {
307                         ERR("Cynara denied file send\n");
308                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
309                 }
310 #endif
311                 node = __add_transfer_info(msg);
312                 if (node == NULL)
313                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
314
315                 ret = _request_file_send(node);
316                 if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
317                         DBG("Aleady OPC progressing. Once completed previous job, will be started\n");
318                 } else if ( ret != BLUETOOTH_ERROR_NONE) {
319                         _bt_create_warning_popup(BLUETOOTH_ERROR_INTERNAL, BT_STR_UNABLE_TO_SEND);
320                         g_slist_free_full(bt_transfer_list,
321                                                 (GDestroyNotify)_free_transfer_info);
322                         bt_transfer_list = NULL;
323                 }
324         } else if (dbus_message_is_signal(msg, BT_SHARE_UI_INTERFACE, BT_SHARE_UI_SIGNAL_OPPABORT)) {
325                 const char *transfer_type = NULL;
326                 int noti_id = 0;
327
328                 if (!dbus_message_get_args(msg, NULL,
329                                         DBUS_TYPE_STRING, &transfer_type,
330                                         DBUS_TYPE_INT32, &noti_id,
331                                         DBUS_TYPE_INVALID)) {
332                         ERR("OPP abort handling failed");
333                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
334                 }
335                 ad->opp_transfer_abort = TRUE; /* Transfer aborted by user*/
336                 INFO("transfer_type = %s", transfer_type);
337                 if (!g_strcmp0(transfer_type, NOTI_TR_TYPE_OUT)) {
338                         bluetooth_opc_cancel_push();
339                         if (ad->opc_noti) {
340                                 ret = _bt_delete_notification(ad->opc_noti);
341                                 if (ret == NOTIFICATION_ERROR_NONE) {
342                                         ad->opc_noti = NULL;
343                                         ad->opc_noti_id = 0;
344                                 }
345                         }
346                 } else {
347                         bluetooth_obex_server_cancel_transfer(noti_id);
348                 }
349         } else if (dbus_message_is_signal(msg, BT_SHARE_UI_INTERFACE,
350                                 BT_SHARE_UI_SIGNAL_SEND_FILE)) {
351                 opc_transfer_info_t *node;
352
353 #ifdef CYNARA_ENABLE
354                 sender = dbus_message_get_sender(msg);
355                 ret = _bt_share_cynara_get_creds(sys_conn, sender, &sender_creds);
356                 if (ret != 0) {
357                         ERR("acquiring cynara creds failed\n");
358                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
359                 }
360
361                 if (_bt_share_cynara_check(&sender_creds, BT_SHARE_PRIVILEGE) != BT_SHARE_FAIL) {
362                         ERR("Cynara denied file send\n");
363                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
364                 }
365 #endif
366                 node = __add_transfer_info(msg);
367                 if (node == NULL)
368                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
369
370                 ret = _request_file_send(node);
371                 if (ret == BLUETOOTH_ERROR_IN_PROGRESS) {
372                         DBG("Aleady OPC progressing. Once completed previous job, will be started\n");
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,
376                                                         (GDestroyNotify)_free_transfer_info);
377                         bt_transfer_list = NULL;
378                 }
379         } else if (dbus_message_is_signal(msg, BT_SHARE_UI_INTERFACE,
380                                 BT_SHARE_UI_SIGNAL_INFO_UPDATE)) {
381                 int tr_uid = 0;
382                 int tr_type = 0;
383                 dbus_message_get_args(msg, NULL,
384                                         DBUS_TYPE_INT32, &tr_uid,
385                                         DBUS_TYPE_INT32, &tr_type,
386                                         DBUS_TYPE_INVALID);
387
388                 DBG("tr_uid = %d, tr_type = %d \n", tr_uid, tr_type);
389                 __bt_share_update_tr_info(tr_uid, tr_type);
390         } else if (dbus_message_is_signal(msg, BT_SHARE_FRWK_INTERFACE,
391                                 BT_SHARE_FRWK_SIGNAL_DEINIT)) {
392                 /* Deinitialize the obex server */
393                 if (bluetooth_obex_server_deinit() == BLUETOOTH_ERROR_NONE) {
394                         DBG("Obex Server deinit");
395                 }
396         } else if (strcasecmp(member, "NameOwnerChanged") == 0) {
397                 char *name = NULL;
398                 char *previous = NULL;
399                 char *current = NULL;
400
401                 if (__bt_get_owner_info(msg, &name, &previous, &current)) {
402                         ERR("Fail to get the owner info");
403                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
404                 }
405
406                 if (*current != '\0')
407                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
408
409                 if (strcasecmp(name, "org.bluez") == 0) {
410                         INFO("Bluetoothd is terminated");
411                         _bt_terminate_bluetooth_share();
412                 }
413         } else {
414                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
415         }
416         return DBUS_HANDLER_RESULT_HANDLED;
417 }
418
419 gboolean _bt_init_dbus_signal(void)
420 {
421         DBG("+");
422         DBusError dbus_error;
423
424         dbus_error_init(&dbus_error);
425
426         dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
427
428         if (!dbus_connection) {
429                 ERR(" DBUS get failed");
430                 if (dbus_error_is_set(&dbus_error)) {
431                         ERR("D-Bus Error: %s\n", dbus_error.message);
432                         dbus_error_free(&dbus_error);
433                 }
434                 return FALSE;
435         }
436
437         /* Add the filter for network client functions */
438         dbus_connection_add_filter(dbus_connection, __event_filter, NULL, NULL);
439         dbus_bus_add_match(dbus_connection,
440                         "type=signal,interface=" BT_BLUEZ_INTERFACE
441                         ",member=NameOwnerChanged", &dbus_error);
442         dbus_bus_add_match(dbus_connection,
443                         "type=signal,interface=" BT_SYSPOPUP_INTERFACE
444                         ",member=Response", &dbus_error);
445         dbus_bus_add_match(dbus_connection,
446                         "type=signal,interface=" BT_UG_IPC_INTERFACE
447                         ",member=Send", &dbus_error);
448         dbus_bus_add_match(dbus_connection,
449                         "type=signal,interface=" BT_SHARE_UI_INTERFACE
450                         ",member=opp_abort", &dbus_error);
451         dbus_bus_add_match(dbus_connection,
452                         "type=signal,interface=" BT_SHARE_UI_INTERFACE
453                         ",member=send_file", &dbus_error);
454         dbus_bus_add_match(dbus_connection,
455                         "type=signal,interface=" BT_SHARE_UI_INTERFACE
456                         ",member=info_update", &dbus_error);
457         dbus_bus_add_match(dbus_connection,
458                         "type=signal,interface=" BT_SHARE_FRWK_INTERFACE
459                         ",member=deinit", &dbus_error);
460         if (dbus_error_is_set(&dbus_error)) {
461                 ERR("Fail to add dbus filter signal");
462                 dbus_error_free(&dbus_error);
463         }
464
465         DBG("-");
466         return TRUE;
467 }
468
469 void _bt_send_message_to_ui(int transfer_id, char *name, int percentage, gboolean completed, int error_type)
470 {
471         DBusMessage *msg = NULL;
472         DBG("+\n");
473         ret_if(dbus_connection == NULL);
474         ret_if(name == NULL);
475
476         msg = dbus_message_new_signal(BT_SHARE_ENG_OBJECT,
477                                       BT_SHARE_ENG_INTERFACE,
478                                       BT_SHARE_ENG_SIGNAL_PROGRESS);
479         if (!msg) {
480                 ERR("Unable to allocate memory\n");
481                 return;
482         }
483         if (!dbus_message_append_args(msg,
484                         DBUS_TYPE_INT32, &transfer_id,
485                         DBUS_TYPE_STRING, &name,
486                         DBUS_TYPE_INT32, &percentage,
487                         DBUS_TYPE_BOOLEAN, &completed,
488                         DBUS_TYPE_INT32, &error_type,
489                         DBUS_TYPE_INVALID)) {
490                 ERR("Event sending failed\n");
491                 dbus_message_unref(msg);
492                 return;
493         }
494         DBG("Dbus send message append request is done.\n");
495
496         dbus_message_set_destination(msg, BT_SHARE_UI_INTERFACE);
497         dbus_connection_send(dbus_connection, msg, NULL);
498         dbus_message_unref(msg);
499         DBG("-\n");
500         return;
501 }
502
503
504 void _bt_create_warning_popup(int error_type, char *msg)
505 {
506         /* If bluetooth-share-ui process is  not running */
507         /* Then create the process and terminate it after popup shown */
508         if (aul_app_is_running(UI_PACKAGE) == 0) {
509                 DBG("Creating new process for Warning Popup");
510                 char str[BT_TEXT_LEN_MAX] = { 0, };
511                 bundle *b;
512
513                 DBG("error_type: %d",error_type);
514                 switch (error_type) {
515                         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
516                         case BLUETOOTH_ERROR_NOT_CONNECTED:
517                         case BLUETOOTH_ERROR_ACCESS_DENIED:
518                         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
519                         case BLUETOOTH_ERROR_INTERNAL:
520                         case BLUETOOTH_ERROR_CANCEL:
521                                 snprintf(str, BT_TEXT_LEN_MAX, "%s",
522                                                 msg);
523                                 break;
524                         default:
525                                 return;
526                 }
527
528                 b = bundle_create();
529                 ret_if(b == NULL);
530
531                 bundle_add(b, "launch-type", "warning_popup");
532                 bundle_add(b, "message", str);
533
534                 aul_launch_app(UI_PACKAGE, b);
535
536                 bundle_free(b);
537         }
538         return;
539 }
540
541 void _bt_update_transfer_list_view(const char *table)
542 {
543         DBusMessage *msg = NULL;
544         ret_if(dbus_connection == NULL);
545
546         msg = dbus_message_new_signal(BT_SHARE_ENG_OBJECT,
547                                       BT_SHARE_ENG_INTERFACE,
548                                       BT_SHARE_ENG_SIGNAL_UPDATE_VIEW);
549         if (!msg) {
550                 ERR("Unable to allocate memory\n");
551                 return;
552         }
553
554         if (!dbus_message_append_args(msg,
555                         DBUS_TYPE_STRING, &table,
556                         DBUS_TYPE_INVALID)) {
557                 DBG("Event sending failed\n");
558                 dbus_message_unref(msg);
559                 return;
560         }
561
562         dbus_message_set_destination(msg, BT_SHARE_UI_INTERFACE);
563         dbus_connection_send(dbus_connection, msg, NULL);
564         dbus_message_unref(msg);
565 }
566
567 static time_t __bt_get_current_timedata(void)
568 {
569         time_t t;
570         time(&t);
571         return ((int) t);
572 }
573
574 static char *__bt_conv_addr_type_to_addr_string(char *addr)
575 {
576         char address[BT_ADDR_STR_LEN_MAX] = {0,};
577         retv_if(addr == NULL, NULL);
578
579         snprintf(address, BT_ADDR_STR_LEN_MAX,
580                 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
581                 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
582
583         return g_strdup(address);
584 }
585
586 static void __bt_create_send_data(opc_transfer_info_t *node)
587 {
588         DBG("__bt_create_send_data  \n");
589         ret_if(node == NULL);
590         struct bt_appdata *ad = app_state;
591
592         int count = 0;
593         int session_id;
594
595         sqlite3 *db = NULL;
596
597         DBG("Name [%s]\n", node->name);
598
599         db = bt_share_open_db();
600         if (!db)
601                 return;
602
603         session_id = bt_share_get_last_session_id(db, BT_DB_OUTBOUND);
604         DBG("Last session id = %d\n", session_id);
605         for (count = 0; count < node->file_cnt; count++) {
606                 bt_tr_data_t *tmp;
607                 tmp = g_malloc0(sizeof(bt_tr_data_t));
608                 if (tmp == NULL)
609                         return;
610
611                 tmp->tr_status = BT_TR_ONGOING;
612                 tmp->sid = session_id + 1;
613                 tmp->file_path = g_strdup(node->file_path[count]);
614                 DBG("tmp->file_path : %s", tmp->file_path);
615
616                 tmp->content = g_strdup(node->content[count]);
617                 tmp->dev_name =  g_strdup(node->name);
618                 tmp->type =  g_strdup(node->type);
619                 tmp->timestamp = __bt_get_current_timedata();
620                 tmp->addr = __bt_conv_addr_type_to_addr_string(node->addr);
621                 tmp->size = node->size[count];
622                 bt_share_add_tr_data(db, BT_DB_OUTBOUND, tmp);
623                 __bt_tr_data_free(tmp);
624
625         }
626
627         if (ad->tr_send_list) {
628                 bt_share_release_tr_data_list(ad->tr_send_list);
629                 ad->tr_send_list = NULL;
630         }
631
632         ad->tr_send_list = bt_share_get_tr_data_list_by_status(db,
633                                                 BT_DB_OUTBOUND, BT_TR_ONGOING);
634         bt_share_close_db(db);
635
636         if (ad->tr_send_list == NULL)
637                 return;
638
639         ad->tr_next_data = ad->tr_send_list;
640
641         return;
642 }
643
644 gboolean _bt_update_sent_data_status(int uid, bt_app_tr_status_t status)
645 {
646         DBG("_bt_update_sent_data_status  \n");
647
648         DBG("uid = %d  \n", uid);
649         sqlite3 *db = NULL;
650         bt_tr_data_t *tmp;
651
652         db = bt_share_open_db();
653         if (!db)
654                 return FALSE;
655
656         tmp = g_malloc0(sizeof(bt_tr_data_t));
657         if (!tmp)
658                 return FALSE;
659
660         tmp->tr_status = status;
661         tmp->timestamp = __bt_get_current_timedata();
662         bt_share_update_tr_data(db, BT_DB_OUTBOUND, uid, tmp);
663         bt_share_close_db(db);
664         __bt_tr_data_free(tmp);
665
666         return TRUE;
667 }
668
669 gboolean _bt_add_recv_transfer_status_data(char *device_name,
670                                         char *filepath, char *type,
671                                                                 unsigned int size, int status)
672 {
673         if (device_name == NULL || filepath == NULL)
674                 return FALSE;
675
676         sqlite3 *db = NULL;
677         bt_tr_data_t *tmp;
678
679         DBG("Name [%s]\n", device_name);
680
681         db = bt_share_open_db();
682         if (!db)
683                 return FALSE;
684
685         tmp = g_malloc0(sizeof(bt_tr_data_t));
686         if(tmp == NULL)
687                 return FALSE;
688
689         tmp->tr_status = status;
690         tmp->file_path = g_strdup(filepath);
691         tmp->dev_name =  g_strdup(device_name);
692         tmp->timestamp = __bt_get_current_timedata();
693         tmp->type = g_strdup(type);
694         tmp->size = size;
695         bt_share_add_tr_data(db, BT_DB_INBOUND, tmp);
696         bt_share_close_db(db);
697         __bt_tr_data_free(tmp);
698
699         return TRUE;
700 }
701
702 static void __bt_share_update_tr_info(int tr_uid, int tr_type)
703 {
704         DBG("__bt_share_update_tr_info tr_uid = %d \n", tr_uid);
705         int status = -1;
706         struct bt_appdata *ad = app_state;
707         char str[NOTIFICATION_TEXT_LEN_MAX] = { 0 };
708         bt_tr_data_t *info = NULL;
709         sqlite3 *db = NULL;
710
711         db = bt_share_open_db();
712         if (!db)
713                 return;
714
715         if (tr_type == BT_TR_OUTBOUND) {
716                 if (tr_uid == -1) {
717                         /* Click the "clear list" button in bluetooth-share-ui */
718                         /* Delete all outbound db / notification info */
719                         _bt_delete_notification(ad->send_noti);
720                         ad->send_noti = NULL;
721                         ad->send_data.tr_success = 0;
722                         ad->send_data.tr_fail = 0;
723                         bt_share_remove_tr_data_by_notification(db, BT_DB_OUTBOUND);
724                         bt_share_close_db(db);
725                 } else {
726                         /* Delete selected outbound db / notification info */
727
728                         info = bt_share_get_tr_data(db, BT_DB_OUTBOUND, tr_uid);
729                         if (info != NULL) {
730                                 status = info->tr_status;
731                                 g_free(info);
732                         }
733
734                         bt_share_remove_tr_data_by_id(db, BT_DB_OUTBOUND, tr_uid);
735                         bt_share_close_db(db);
736
737                         if (status == BT_TR_SUCCESS && ad->send_data.tr_success > 0)
738                                 ad->send_data.tr_success--;
739                         else if (status == BT_TR_FAIL && ad->send_data.tr_fail > 0)
740                                 ad->send_data.tr_fail--;
741                         else
742                                 return;
743
744                         if ((ad->send_data.tr_success + ad->send_data.tr_fail) != 0) {
745                                 snprintf(str, sizeof(str), "%s %d %d", BT_TR_STATUS,
746                                         ad->send_data.tr_success, ad->send_data.tr_fail);
747
748                                 _bt_update_notification(ad, ad->send_noti,
749                                                 NULL, NULL, NULL);
750                         } else {
751                                 _bt_delete_notification(ad->send_noti);
752                         }
753                 }
754         } else if (tr_type == BT_TR_INBOUND) {
755                 if (tr_uid == -1) {
756                         /* Click the "clear list" button in bluetooth-share-ui */
757                         /* Delete all inbound db / notification info */
758
759                         _bt_delete_notification(ad->receive_noti);
760                         ad->receive_noti = NULL;
761                         ad->recv_data.tr_success = 0;
762                         ad->recv_data.tr_fail = 0;
763                         bt_share_remove_all_tr_data(db, BT_DB_INBOUND);
764                         bt_share_close_db(db);
765                 } else {
766                         /* Delete selected outbound db / notification info */
767
768                         info = bt_share_get_tr_data(db, BT_DB_INBOUND, tr_uid);
769                         if (info != NULL) {
770                                 status = info->tr_status;
771                                 g_free(info);
772                         }
773
774                         bt_share_remove_tr_data_by_id(db, BT_DB_INBOUND, tr_uid);
775                         bt_share_close_db(db);
776
777                         if (status == BT_TR_SUCCESS && ad->recv_data.tr_success > 0)
778                                 ad->recv_data.tr_success--;
779                         else if (status == BT_TR_FAIL && ad->recv_data.tr_fail > 0)
780                                 ad->recv_data.tr_fail--;
781                         else
782                                 return;
783
784                         if ((ad->recv_data.tr_success + ad->recv_data.tr_fail) != 0) {
785                                 snprintf(str, sizeof(str), "%s %d %d", BT_TR_STATUS,
786                                         ad->recv_data.tr_success, ad->recv_data.tr_fail);
787
788                                 _bt_update_notification(ad, ad->receive_noti,
789                                                 NULL, NULL, NULL);
790                         } else {
791                                 _bt_delete_notification(ad->receive_noti);
792                         }
793                 }
794         } else {
795                 ERR("Invalid transfer type  \n");
796         }
797         return;
798 }
799