73a83e7e39a4b814279f957253665a11f0e9ca4a
[apps/native/bluetooth-share-ui.git] / src / bt-share-ui-view.c
1 /*
2 * bluetooth-share-ui
3 *
4 * Copyright 2012 Samsung Electronics Co., Ltd
5 *
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 *           GirishAshok Joshi <girish.joshi@samsung.com>
8 *           DoHyun Pyun <dh79.pyun@samsung.com>
9 *
10 * Licensed under the Flora License, Version 1.1 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.tizenopensource.org/license
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 */
23
24 #include <glib.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #if 0
29 #include <Ecore_X.h>
30 #include <utilX.h>
31 #endif
32 #include <Elementary.h>
33 #include <efl_extension.h>
34 #include <aul.h>
35 #include <app.h>
36 #include <vconf.h>
37 #include <vconf-keys.h>
38 #include <bluetooth-share-api.h>
39 #include <bluetooth_internal.h>
40 #include <notification.h>
41 #include <notification_internal.h>
42 #include <notification_list.h>
43
44 #include "applog.h"
45 #include "bt-share-ui-view.h"
46 #include "bt-share-ui-popup.h"
47 #include "bt-share-ui-ipc.h"
48 #include "bt-share-ui-resource.h"
49 #include "bt-share-ui-widget.h"
50 #include "bt-share-ui-main.h"
51
52 #define NOTI_OPS_APP_ID "bluetooth-share-opp-server"
53 #define NOTI_OPC_APP_ID "bluetooth-share-opp-client"
54
55 #define BT_DEFAULT_MEM_PHONE 0
56 #define BT_DEFAULT_MEM_MMC 1
57
58 #define BT_DOWNLOAD_PHONE_FOLDER "/opt/usr/media/Downloads"
59 #define BT_DOWNLOAD_MMC_FOLDER "/opt/media/SDCardA1"
60 #define BT_CONTACT_SHARE_TMP_DIR BT_DOWNLOAD_PHONE_FOLDER "/.bluetooth"
61
62 extern bt_share_appdata_t *app_state;
63
64 static Evas_Object *__bt_add_tr_data_genlist(Evas_Object *parent,
65                                                   bt_share_appdata_t *ad);
66
67 void _bt_delete_selected_notification(bt_share_tr_type_e tr_type,
68                                         int noti_id, const char *opp_role)
69 {
70         notification_list_h list_head;
71         notification_list_h list_traverse;
72         notification_h noti;
73         int priv_id;
74
75         notification_get_list(NOTIFICATION_TYPE_NOTI, -1, &list_head);
76         list_traverse = list_head;
77
78         while (list_traverse != NULL) {
79                 noti = notification_list_get_data(list_traverse);
80                 notification_get_id(noti, NULL, &priv_id);
81                 if (priv_id == noti_id) {
82                         DBG_SECURE("Deleting Notification ID: %d", noti_id);
83                         if (notification_delete_by_priv_id(opp_role,
84                                         NOTIFICATION_TYPE_NOTI, priv_id)
85                                         != NOTIFICATION_ERROR_NONE)
86                                 ERR("Could Not Delete Notification");
87                         break;
88                 }
89                 list_traverse = notification_list_get_next(list_traverse);
90         }
91 }
92
93 static void __bt_get_noti_id_opp_role_and_table(bt_share_appdata_t *ad, int *noti_id,
94                 char **opp_role, bt_tr_db_table_e *table)
95 {
96         sqlite3 *db = bt_share_open_db();
97
98         if (ad->tr_type == BT_TR_OUTBOUND) {
99                 *noti_id = bt_share_get_noti_id(db, BT_DB_OUTBOUND, ad->transfer_info->db_sid);
100                 *opp_role = NOTI_OPC_APP_ID;
101                 *table = BT_DB_OUTBOUND;
102         } else {
103                 *noti_id = bt_share_get_noti_id(db, BT_DB_INBOUND, ad->transfer_info->db_sid);
104                 *opp_role = NOTI_OPS_APP_ID;
105                 *table = BT_DB_INBOUND;
106         }
107
108         bt_share_close_db(db);
109 }
110
111 void _bt_share_ui_handle_transfer_disconnected(bt_share_appdata_t *ad,
112                 bt_share_tr_type_e type)
113 {
114         FN_START;
115         ret_if(ad == NULL);
116
117         if (ad->progress_item) {
118                 DBG("Updating Status");
119                 Elm_Object_Item *git = elm_genlist_item_insert_after(ad->tr_genlist,
120                                 ad->tr_status_itc, ad, NULL,
121                                 ad->device_item, ELM_GENLIST_ITEM_NONE,
122                                 NULL, NULL);
123                 if (git == NULL) {
124                         ERR("elm_genlist_item_insert_after is failed!");
125                 } else {
126                         elm_genlist_item_select_mode_set(git,
127                                                  ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
128                         ad->status_item = git;
129                 }
130
131                 elm_object_item_del(ad->progress_item);
132                 ad->progress_item = NULL;
133                 ad->progressbar = NULL;
134         }
135
136         FN_END;
137 }
138
139 void _bt_share_ui_handle_transfer_complete(bt_share_appdata_t *ad,
140                 char *address, bt_share_tr_type_e type)
141 {
142         FN_START;
143         ret_if(ad == NULL || ad->transfer_info == NULL || ad->transfer_info->device_address == NULL);
144         DBG_SECURE("Received Address: %s", address);
145         DBG_SECURE("Device Address: %s", ad->transfer_info->device_address);
146
147         ret_if(g_strcmp0(ad->transfer_info->device_address, address));
148
149         FN_END;
150 }
151
152 void _bt_share_ui_handle_transfer_started(bt_share_appdata_t *ad,
153                 char *address, char *file_name, unsigned long size, int transfer_id,
154                 bt_share_tr_type_e type)
155 {
156         FN_START;
157         ret_if(ad == NULL || ad->transfer_info == NULL || ad->transfer_info->device_address == NULL);
158         DBG_SECURE("Received Address: %s", address);
159         DBG_SECURE("Device Address: %s", ad->transfer_info->device_address);
160         ret_if(g_strcmp0(ad->transfer_info->device_address, address));
161
162         ad->transfer_info->filename = g_strdup(file_name);
163         DBG_SECURE("Transfer ID: %d", transfer_id);
164         ad->transfer_info->size = size;
165         ad->transfer_info->percentage = 0;
166         ad->transfer_info->current_file++;
167
168         if (ad->status_item) {
169                 elm_object_item_del(ad->status_item);
170                 ad->status_item = NULL;
171         }
172
173         if (ad->progress_item) {
174                 _bt_share_genlist_item_text_update(ad->progress_item, BT_SHARE_ITEM_PART_PROGRESSBAR_FILE_NAME);
175                 _bt_share_genlist_item_content_update(ad->progress_item, BT_SHARE_ITEM_PART_PROGRESSBAR_ICON);
176         } else {
177                 /* Create Progress bar if not present */
178                 Elm_Object_Item *git = elm_genlist_item_insert_after(ad->tr_genlist,
179                                 ad->tr_progress_itc, ad, NULL, ad->device_item,
180                                 ELM_GENLIST_ITEM_NONE, NULL, NULL);
181                 if (git == NULL) {
182                         ERR("elm_genlist_item_append is failed!");
183                 } else {
184                         elm_genlist_item_select_mode_set(git,
185                                                  ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
186                         ad->progress_item = git;
187                 }
188         }
189
190         if (!ad->toolbar_button) {
191                 /* Create Stop Button if not present */
192                 ad->toolbar_button = _bt_share_create_toolbar_button(ad,
193                                 BT_STR_STOP);
194         }
195
196         FN_END;
197 }
198
199 Evas_Object *_bt_create_win(const char *name)
200 {
201         Evas_Object *eo = NULL;
202
203         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
204         retv_if(!eo, NULL);
205
206         elm_win_title_set(eo, name);
207         elm_win_borderless_set(eo, EINA_TRUE);
208         return eo;
209 }
210
211 void _bt_terminate_app(void)
212 {
213         DBG("Terminate BT SHARE UI");
214         elm_exit();
215 }
216
217 static void __bt_clear_view(void *data)
218 {
219         DBG("+");
220
221         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
222         ret_if(!ad);
223
224         if (ad->tr_genlist) {
225                 elm_genlist_clear(ad->tr_genlist);
226                 ad->tr_genlist = NULL;
227                 ad->device_item = NULL;
228                 ad->progress_item = NULL;
229                 ad->status_item = NULL;
230                 ad->file_title_item = NULL;
231                 ad->tr_data_itc = NULL;
232                 ad->progressbar = NULL;
233         }
234
235         if (ad->navi_fr) {
236                 evas_object_del(ad->navi_fr);
237                 ad->navi_fr = NULL;
238         }
239
240         if (ad->tr_view) {
241                 evas_object_del(ad->tr_view);
242                 ad->tr_view = NULL;
243         }
244         DBG("-");
245 }
246
247 static Eina_Bool __bt_back_button_cb(void *data, Elm_Object_Item *it)
248 {
249         DBG("pop current view ");
250         retvm_if(!data, EINA_FALSE, "invalid parameter!");
251
252         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
253         int ret;
254
255         __bt_clear_view(data);
256
257         if (ad->tr_data_list) {
258                 ret = bt_share_release_tr_data_list(ad->tr_data_list);
259
260                 if (ret != BT_SHARE_ERR_NONE)
261                         ERR("Transfer data release failed ");
262                 ad->tr_data_list = NULL;
263         }
264
265         _bt_terminate_app();
266
267         return EINA_FALSE;
268 }
269
270 static Evas_Object *__bt_tr_progress_icon_get(void *data, Evas_Object *obj,
271                                             const char *part)
272 {
273         FN_START;
274
275         Evas_Object *progress_layout = NULL;
276         Evas_Object *progressbar = NULL;
277         bt_share_appdata_t *ad = NULL;
278
279         retv_if(data == NULL, NULL);
280         ad = (bt_share_appdata_t *)data;
281
282         bt_share_transfer_data_t *transfer_info = ad->transfer_info;
283
284         if (!strcmp(part, BT_SHARE_ITEM_PART_PROGRESSBAR_ICON)) {
285                 char buff[BT_STR_PROGRESS_MAX_LEN] = { 0, };
286                 char *markup_text = NULL;
287
288                 DBG("Creating new progress layout!!!");
289                 progress_layout = elm_layout_add(obj);
290                 elm_layout_file_set(progress_layout, EDJFILE, "popup_text_progressbar_view_layout");
291                 evas_object_size_hint_align_set(progress_layout, EVAS_HINT_FILL,
292                                 EVAS_HINT_FILL);
293                 evas_object_size_hint_weight_set(progress_layout, EVAS_HINT_EXPAND,
294                                 EVAS_HINT_EXPAND);
295                 evas_object_show(progress_layout);
296
297                 DBG("Creating new progressbar!!!");
298                 progressbar = elm_progressbar_add(progress_layout);
299
300                 elm_progressbar_unit_format_set(progressbar, NULL);
301                 elm_progressbar_horizontal_set(progressbar, EINA_TRUE);
302                 evas_object_size_hint_align_set(progressbar, EVAS_HINT_FILL,
303                                 EVAS_HINT_FILL);
304                 evas_object_size_hint_weight_set(progressbar, EVAS_HINT_EXPAND,
305                                 EVAS_HINT_EXPAND);
306                 elm_progressbar_pulse(progressbar, EINA_TRUE);
307
308                 evas_object_show(progressbar);
309                 ad->progressbar = progressbar;
310
311                 markup_text = elm_entry_utf8_to_markup(transfer_info->filename);
312                 DBG_SECURE("Filename: %s", markup_text);
313
314                 DBG("Filename: %s", markup_text);
315
316                 elm_object_part_text_set(progress_layout, "elm.text.description", markup_text);
317                 g_free(markup_text);
318
319                 elm_object_part_content_set(progress_layout, "progressbar", progressbar);
320
321                 elm_progressbar_value_set(progressbar,
322                                 (double) transfer_info->percentage / 100);
323
324                 elm_object_signal_emit(progressbar, "elm,bottom,text,show", "elm");
325
326                 snprintf(buff, BT_STR_PROGRESS_MAX_LEN - 1 , "%d%%", transfer_info->percentage);
327                 elm_object_part_text_set(progressbar, "elm.text.bottom.left", buff);
328
329                 if (ad->launch_mode == BT_LAUNCH_ONGOING &&
330                                 ad->tr_type == BT_TR_OUTBOUND) {
331                         memset(buff, 0, BT_STR_PROGRESS_MAX_LEN);
332                         snprintf(buff, BT_STR_PROGRESS_MAX_LEN - 1 , "[%d/%d]",
333                                         transfer_info->current_file, transfer_info->total_files);
334                         elm_object_part_text_set(progressbar, "elm.text.bottom.right", buff);
335                 }
336         }
337
338         return progress_layout;
339 }
340
341 static Evas_Object *__bt_tr_icon_get(void *data, Evas_Object *obj,
342                                             const char *part)
343 {
344         Evas_Object *ly = NULL;
345         Evas_Object *icon = NULL;
346         bt_tr_data_t *info = NULL;
347         bt_share_appdata_t *ad = app_state;
348         char *img = NULL;
349         bt_gl_data_t *gl_data = NULL;
350
351         retv_if(!data, NULL);
352         gl_data = (bt_gl_data_t *)data;
353
354         info = gl_data->tr_data;
355         retv_if(!info, NULL);
356
357         if (!strcmp(part, BT_SHARE_ITEM_PART_FILE_TRANSFER_STATUS_ICON)) {
358                 ly = elm_layout_add(obj);
359                 if (info->tr_status == BT_TRANSFER_SUCCESS ||
360                                 info->tr_status == BT_TRANSFER_FAIL) {
361                         icon = elm_image_add(obj);
362                         if (ad->tr_type == BT_TR_OUTBOUND) {
363                                 img = (info->tr_status == BT_TRANSFER_SUCCESS) ?
364                                                 BT_ICON_SEND_PASS : BT_ICON_SEND_FAIL;
365                         } else {
366                                 img = (info->tr_status == BT_TRANSFER_SUCCESS) ?
367                                                 BT_ICON_RECV_PASS : BT_ICON_RECV_FAIL;
368                         }
369
370                         elm_image_file_set(icon, EDJ_IMAGES, img);
371                         evas_object_size_hint_align_set(icon, EVAS_HINT_FILL,
372                                         EVAS_HINT_FILL);
373                         evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND,
374                                         EVAS_HINT_EXPAND);
375
376                         evas_object_show(icon);
377                         evas_object_size_hint_min_set(icon, ELM_SCALE_SIZE(40), ELM_SCALE_SIZE(40));
378                         return icon;
379                 } else if (info->tr_status == BT_TRANSFER_ONGOING) {
380 #if 0
381                         int ret;
382                         ret = elm_layout_file_set(ly, EDJ_BT_ICON_ANIMATION,
383                                 (ad->tr_type == BT_TR_OUTBOUND ? BT_ANI_UPLOAD : BT_ANI_DOWNLOAD));
384                         if (ret == EINA_FALSE)
385                                 ERR("Error in setting layout file");
386 #endif
387                         icon = elm_progressbar_add(obj);
388                         elm_object_style_set(icon, "process_medium");
389                         evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, 0.5);
390                         evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
391                         elm_progressbar_pulse(icon, TRUE);
392                         return icon;
393                 } else if (info->tr_status == BT_TRANSFER_PENDING) {
394                         icon = elm_progressbar_add(obj);
395                         elm_object_style_set(icon, "process_medium");
396                         evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, 0.5);
397                         evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
398                         elm_progressbar_pulse(icon, TRUE);
399                         return icon;
400                 }
401         }
402         return ly;
403 }
404
405 #if 0
406 void _bt_util_get_number(char *source, char *dest)
407 {
408         int len = 7;
409         int slen = 0;
410         char buf[9];
411
412         slen = strlen(source);
413         slen--;
414         buf[8] = '\0';
415         while (len > -1) {
416                 if (slen > -1) {
417                         if (isdigit(source[slen])) {
418                                 buf[len] = source[slen];
419                                 --len;
420                         }
421                         --slen;
422                 } else {
423                         break;
424                 }
425         }
426         if (len < 0)
427                 len = 0;
428         strcpy(dest, &buf[len]);
429 }
430
431 void _bt_util_get_contact_name(int lcontact_id, char **contact_name)
432 {
433         int ret = 0;
434         int count = 0;
435         contacts_filter_h filter = NULL;
436         contacts_query_h query = NULL;
437         contacts_list_h list = NULL;
438         contacts_record_h record = NULL;
439         char *name = NULL;
440
441         DBG("+");
442         ret = contacts_filter_create(_contacts_contact._uri, &filter);
443         if (ret != CONTACTS_ERROR_NONE) {
444                 ERR("Fail to create filter for contacts");
445                 goto fail;
446         }
447
448         ret = contacts_filter_add_int(filter, _contacts_contact.id,
449                         CONTACTS_MATCH_EXACTLY, lcontact_id);
450         if (ret != CONTACTS_ERROR_NONE) {
451                 ERR("Fail to add str to filter for contacts");
452                 goto fail;
453         }
454
455         ret = contacts_query_create(_contacts_contact._uri, &query);
456         if (ret != CONTACTS_ERROR_NONE) {
457                 ERR("Fail to create qurey for contacts");
458                 goto fail;
459         }
460
461         ret = contacts_query_set_filter(query, filter);
462         if (ret != CONTACTS_ERROR_NONE) {
463                 ERR("Fail to set filter for contacts");
464                 goto fail;
465         }
466
467         ret = contacts_db_get_count_with_query(query, &count);
468         if (ret != CONTACTS_ERROR_NONE) {
469                 ERR("Fail to get count from db");
470                 goto fail;
471         }
472
473         if (count < 1) {
474                 DBG("No match");
475                 goto fail;
476         }
477         DBG("count = %d", count);
478         ret = contacts_db_get_records_with_query(query, 0, 0, &list);
479         if (ret != CONTACTS_ERROR_NONE) {
480                 ERR("Fail to get records from db");
481                 goto fail;
482         }
483         contacts_list_first(list);
484
485         while (ret == CONTACTS_ERROR_NONE) {
486                 contacts_list_get_current_record_p(list, &record);
487                 contacts_record_get_str_p(record,
488                                 _contacts_contact.display_name,
489                                 &name);
490                 if (name != NULL) {
491                         *contact_name = g_strdup(name);
492                         break;
493                 }
494
495                 ret = contacts_list_next(list);
496         }
497
498 fail:
499         if (filter)
500                 contacts_filter_destroy(filter);
501
502         if (query)
503                 contacts_query_destroy(query);
504
505         if (list)
506                 contacts_list_destroy(list, true);
507         DBG("-");
508         return;
509 }
510
511 unsigned int bt_crc32(const void *src, unsigned long src_sz)
512 {
513         return (crc32(0, src, src_sz) & 0xFFFF);
514 }
515
516 void _bt_util_get_contact_info(unsigned char *auth_info, int *contact_id, char **contact_name)
517 {
518         unsigned int shash = 0;
519         unsigned int chash = 0;
520         char pnumber[20] = {0,};
521         char str_hash[7] = {0,};
522         int ret = CONTACTS_ERROR_NONE;
523         char *number = NULL;
524         int lcontact_id = 0;
525         int count = 0;
526
527         contacts_filter_h filter = NULL;
528         contacts_query_h query = NULL;
529         contacts_list_h list = NULL;
530         contacts_record_h record = NULL;
531
532         retm_if(!auth_info, "auth_info is NULL");
533         retm_if(!contact_id, "contact_id is NULL");
534         retm_if(!contact_name, "contact_name is NULL");
535         DBG("+");
536         if (contacts_connect() != CONTACTS_ERROR_NONE) {
537                 ERR("contacts_connect failed");
538                 return;
539         }
540
541         memcpy(&shash, auth_info, 3);
542         shash = ntohl(shash);
543         shash >>= 8;
544
545         memcpy(&chash, &auth_info[3], 2);
546         chash = ntohl(chash);
547         chash >>= 16;
548
549         g_snprintf(str_hash, 7, "%X", shash);
550
551         ret = contacts_filter_create(_contacts_quick_connect_info._uri, &filter);
552         if (ret != CONTACTS_ERROR_NONE) {
553                 ERR("Fail to create filter for contacts");
554                 goto fail;
555         }
556
557         ret = contacts_filter_add_str(filter, _contacts_quick_connect_info.hash,
558                         CONTACTS_MATCH_EXACTLY, str_hash);
559         if (ret != CONTACTS_ERROR_NONE) {
560                 ERR("Fail to add str to filter for contacts");
561                 goto fail;
562         }
563
564         ret = contacts_query_create(_contacts_quick_connect_info._uri, &query);
565         if (ret != CONTACTS_ERROR_NONE) {
566                 ERR("Fail to create qurey for contacts");
567                 goto fail;
568         }
569
570         ret = contacts_query_set_filter(query, filter);
571         if (ret != CONTACTS_ERROR_NONE) {
572                 ERR("Fail to set filter for contacts");
573                 goto fail;
574         }
575
576         ret = contacts_db_get_count_with_query(query, &count);
577         if (ret != CONTACTS_ERROR_NONE) {
578                 ERR("Fail to get count from db");
579                 goto fail;
580         }
581
582         if (count < 1) {
583                 DBG("No match");
584                 goto fail;
585         }
586
587         ret = contacts_db_get_records_with_query(query, 0, 0, &list);
588         if (ret != CONTACTS_ERROR_NONE) {
589                 ERR("Fail to get records from db");
590                 goto fail;
591         }
592
593         contacts_list_first(list);
594
595         while (ret == CONTACTS_ERROR_NONE) {
596                 contacts_list_get_current_record_p(list, &record);
597                 contacts_record_get_str_p(record,
598                                 _contacts_quick_connect_info.number,
599                                 &number);
600                 DBG_SECURE("number: [%s]", number);
601                 if (number != NULL) {
602                         _bt_util_get_number(number, pnumber);
603                         DBG_SECURE("pnumber: [%s]", pnumber);
604
605                         DBG_SECURE("CRC [%X], [%X]", chash, bt_crc32(pnumber, strlen(pnumber)));
606
607                         if (bt_crc32(pnumber , strlen(pnumber)) == chash) {
608                                 contacts_record_get_int(record,
609                                                 _contacts_quick_connect_info.contact_id,
610                                                 &lcontact_id);
611                                 *contact_id = lcontact_id;
612                                 _bt_util_get_contact_name(lcontact_id, contact_name);
613                                 DBG_SECURE("contact id : %d", lcontact_id);
614                                 break;
615                         }
616                 }
617                 ret = contacts_list_next(list);
618         }
619
620 fail:
621         if (filter)
622                 contacts_filter_destroy(filter);
623
624         if (query)
625                 contacts_query_destroy(query);
626
627         if (list)
628                 contacts_list_destroy(list, true);
629
630         contacts_disconnect();
631
632         DBG("-");
633 }
634 #endif
635
636 static char *__bt_tr_device_label_get(void *data, Evas_Object *obj,
637                                       const char *part)
638 {
639         FN_START;
640         retv_if(data == NULL, NULL);
641         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
642         char *markup_text = NULL;
643
644         retv_if(ad->transfer_info == NULL, NULL);
645
646         if (!strcmp(part, BT_SHARE_ITEM_PART_DEVICE_NAME_TITLE)) {
647                 return g_strdup(BT_STR_DEVICENAME);
648         } else if (!strcmp(part, BT_SHARE_ITEM_PART_DEVICE_NAME)) {
649                 bt_share_transfer_data_t *transfer_info = ad->transfer_info;
650                 DBG_SECURE("Device : %s", transfer_info->device_name);
651                 if (ad->bt_status == BT_ADAPTER_ENABLED) {
652                         bt_error_e ret = BT_ERROR_NONE;
653                         bt_device_info_s *dev_info = NULL;
654
655                         /* Get the contact name from manufacturer data */
656                         DBG_SECURE("Address : %s", transfer_info->device_address);
657                         ret =  bt_adapter_get_bonded_device_info(transfer_info->device_address, &dev_info);
658                         if (ret == BT_ERROR_NONE) {
659                                 DBG_SECURE("Using remote name [%s] only.", dev_info->remote_name);
660                                 markup_text = elm_entry_utf8_to_markup(dev_info->remote_name);
661 #if 0
662                                 unsigned char auth_info[5] = {0, };
663                                 int contact_id = -1;
664                                 char *contact_name = NULL;
665                                 gboolean is_alias_set = FALSE;
666                                 ret = bt_adapter_get_bonded_device_is_alias_set(dev_info->remote_address, &is_alias_set);
667                                 if (ret != BT_ERROR_NONE)
668                                         ERR("bt_adapter_get_bonded_device_is_alias_set() is failed!!! error: 0x%04X", ret);
669
670                                 if (is_alias_set == TRUE) {
671                                         DBG_SECURE("Device alias is set. Using remote name [%s] only.", dev_info->remote_name);
672                                         markup_text = elm_entry_utf8_to_markup(dev_info->remote_name);
673                                 } else if (dev_info->manufacturer_data_len >= 30 &&
674                                                 dev_info->manufacturer_data[0] == 0x00 &&
675                                                 dev_info->manufacturer_data[1] == 0x75) {
676                                         unsigned char auth_info_null[5];
677                                         memset(auth_info_null, 0X0, 5);
678                                         memcpy(auth_info, &(dev_info->manufacturer_data[10]), 5);
679
680                                         /* Check for validity of auth_info */
681                                         if (memcmp(auth_info, auth_info_null, 5)) {
682                                                 _bt_util_get_contact_info(auth_info, &contact_id, &contact_name);
683                                                 DBG_SECURE("contact id : %d | contact name : %s", contact_id, contact_name);
684                                                 if (contact_name) {
685                                                         markup_text = elm_entry_utf8_to_markup(contact_name);
686                                                         g_free(contact_name);
687                                                 }
688                                         }
689                                 }
690 #endif
691                                 bt_adapter_free_device_info(dev_info);
692                         } else {
693                                 ERR("bt_adapter_get_bonded_device_info() is failed!!! error: 0x%04X", ret);
694                         }
695                 }
696
697                 if (!markup_text)
698                         markup_text = elm_entry_utf8_to_markup(transfer_info->device_name);
699
700                 return markup_text;
701         }
702         FN_END;
703         return NULL;
704 }
705
706 static char *__bt_tr_status_label_get(void *data, Evas_Object *obj,
707                                       const char *part)
708 {
709         FN_START;
710         retv_if(data == NULL, NULL);
711         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
712         bt_share_transfer_data_t *transfer_info = ad->transfer_info;
713         char *tmp_success = NULL;
714         char *tmp_failed = NULL;
715         char *ret_str = NULL;
716
717         retv_if(transfer_info == NULL, NULL);
718
719         if (!strcmp(part, BT_SHARE_ITEM_PART_TRANSFER_TYPE_TITLE)) {
720                 if (ad->tr_type == BT_TR_INBOUND)
721                         return (transfer_info->success > 0) ?
722                                 g_strdup(BT_STR_FILE_RECEIVED) :
723                                 g_strdup(BT_STR_RECEIVING_FAILED);
724                 else
725                         return (transfer_info->success > 0) ?
726                                 g_strdup(BT_STR_FILE_SENT) :
727                                 g_strdup(BT_STR_SENDING_FAILED);
728         } else if (!strcmp(part, BT_SHARE_ITEM_PART_TRANSFER_STATUS)) {
729                 char *stms_str = NULL;
730
731                 DBG_SECURE("Success %d, Failed %d", transfer_info->success, transfer_info->failed);
732                 if (transfer_info->success == 0 &&
733                                 transfer_info->failed == 0)
734                         return NULL;
735
736                 stms_str = (ad->tr_type == BT_TR_INBOUND) ? BT_STR_PD_RECEIVED : BT_STR_PD_SENT;
737                 tmp_success = g_strdup_printf(stms_str, transfer_info->success);
738
739                 stms_str = BT_STR_PD_FAILED;
740                 tmp_failed = g_strdup_printf(stms_str, transfer_info->failed);
741
742                 ret_str = g_strdup_printf("%s, %s", tmp_success, tmp_failed);
743
744                 g_free(tmp_success);
745                 g_free(tmp_failed);
746                 return ret_str;
747         }
748         FN_END;
749         return NULL;
750 }
751
752 #if 0
753 static char *__bt_tr_progress_label_get(void *data, Evas_Object *obj,
754                                       const char *part)
755 {
756         FN_START;
757         retv_if(data == NULL, NULL);
758         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
759         char *markup_text = NULL;
760
761         retv_if(ad->transfer_info == NULL, NULL);
762
763         if (!strcmp(part, BT_SHARE_ITEM_PART_PROGRESSBAR_FILE_NAME)) {
764                 bt_share_transfer_data_t *transfer_info = ad->transfer_info;
765                 markup_text = elm_entry_utf8_to_markup(transfer_info->filename);
766                 DBG_SECURE("Filename: %s", markup_text);
767                 return markup_text;
768         }
769
770         FN_END;
771         return NULL;
772 }
773 #endif
774
775 static char *__bt_tr_file_title_label_get(void *data, Evas_Object *obj,
776                                       const char *part)
777 {
778         FN_START;
779         if (!strcmp(part, BT_SHARE_ITEM_PART_FILE_LIST_TITLE)) {
780                 DBG("File List");
781                 return g_strdup(BT_STR_FILE_LIST);
782         }
783         FN_END;
784         return NULL;
785 }
786
787 static char *__bt_tr_label_get(void *data, Evas_Object *obj,
788                                       const char *part)
789 {
790         FN_START;
791         bt_tr_data_t *info = NULL;
792         char *name = NULL;
793         char buf[BT_GLOBALIZATION_STR_LENGTH] = { 0 };
794         bt_gl_data_t *gl_data = NULL;
795         char *markup_text = NULL;
796         retv_if(data == NULL, NULL);
797         gl_data = (bt_gl_data_t *)data;
798         retvm_if(gl_data == NULL, NULL, "gl_data is NULL!");
799
800         info = gl_data->tr_data;
801         retv_if(info == NULL, NULL);
802
803         if (!strcmp(part, BT_SHARE_ITEM_PART_FILE_NAME)) {
804                 name = strrchr(info->file_path, '/');
805                 if (name != NULL)
806                         name++;
807                 else
808                         name = info->file_path;
809                 markup_text = elm_entry_utf8_to_markup(name);
810
811                 g_strlcpy(buf, markup_text, BT_GLOBALIZATION_STR_LENGTH);
812         } else if (!strcmp(part, BT_SHARE_ITEM_PART_FILE_TRANSFER_STATUS_TEXT)) {
813                 DBG("info->tr_status : %d", info->tr_status);
814                 if (info->tr_status == BT_TRANSFER_SUCCESS) {
815                         if (gl_data->tr_inbound)
816                                 g_strlcpy(buf, BT_STR_RECEIVED, BT_GLOBALIZATION_STR_LENGTH);
817                         else
818                                 g_strlcpy(buf, BT_STR_SENT, BT_GLOBALIZATION_STR_LENGTH);
819                 } else if (info->tr_status == BT_TRANSFER_FAIL) {
820                         g_strlcpy(buf, BT_STR_FAILED, BT_GLOBALIZATION_STR_LENGTH);
821                 } else if (info->tr_status == BT_TRANSFER_ONGOING) {
822                         if (gl_data->tr_inbound)
823                                 g_strlcpy(buf, BT_STR_RECEIVING, BT_GLOBALIZATION_STR_LENGTH);
824                         else
825                                 g_strlcpy(buf, BT_STR_SENDING, BT_GLOBALIZATION_STR_LENGTH);
826                 } else if (info->tr_status == BT_TRANSFER_PENDING) {
827                         g_strlcpy(buf, BT_STR_WAITING, BT_GLOBALIZATION_STR_LENGTH);
828                 }
829         } else if (!strcmp(part, BT_SHARE_ITEM_PART_FILE_SIZE)) {
830                 char *size;
831
832                 if ((info->tr_status == BT_TRANSFER_ONGOING ||
833                                 info->tr_status == BT_TRANSFER_PENDING) && gl_data->tr_inbound)
834                         return NULL;
835
836                 if (info->size > 1024 * 1024 * 1024) { //GB
837                         size = g_strdup_printf("%.2f GB", (float)info->size / (1024 * 1024 * 1024));
838                 } else if (info->size > 1024 * 1024) { //MB
839                         size = g_strdup_printf("%.1f MB", (float)info->size / (1024 * 1024));
840                 } else if (info->size > 1024) { //KB
841                         size = g_strdup_printf("%.1f KB", (float)info->size / (1024));
842                 } else {
843                         size = g_strdup_printf("%d B", info->size);
844                 }
845
846                 g_strlcpy(buf, size, BT_GLOBALIZATION_STR_LENGTH);
847                 g_free(size);
848         } else {
849                 DBG("empty text for label.");
850                 return NULL;
851         }
852         if (markup_text)
853                 free(markup_text);
854
855         FN_END;
856         return strdup(buf);
857 }
858
859 static void  __bt_tr_del(void *data, Evas_Object *obj)
860 {
861         bt_gl_data_t *gl_data;
862         gl_data = (bt_gl_data_t *)data;
863
864         g_free(gl_data);
865 }
866
867 #if 0
868 static void __bt_genlist_realized_cb(void *data,
869                         Evas_Object *obj, void *event_info)
870 {
871         retm_if(event_info == NULL, "Invalid param\n");
872
873         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
874 #ifdef KIRAN_ACCESSIBILITY
875         Evas_Object *ao;
876         char str[BT_STR_ACCES_INFO_MAX_LEN] = {0, };
877 #endif
878         char *name;
879         char *date;
880         bt_tr_data_t *info;
881         bt_gl_data_t *gl_data;
882
883         gl_data = (bt_gl_data_t *)elm_object_item_data_get(item);
884         ret_if(gl_data == NULL);
885
886         info = gl_data->tr_data;
887
888         ret_if(info == NULL);
889         ret_if(info->dev_name == NULL);
890         ret_if(info->file_path == NULL);
891
892         name = strrchr(info->file_path, '/');
893         if (name != NULL)
894                 name++;
895         else
896                 name = info->file_path;
897
898         date = __bt_get_tr_timedate((time_t)(info->timestamp));
899
900 #ifdef KIRAN_ACCESSIBILITY
901         snprintf(str, sizeof(str), "%s, %s, %s, %s",
902                         BT_STR_ACC_ICON, name, info->dev_name, date);
903         ao = elm_object_item_access_object_get(item);
904         elm_access_info_set(ao, ELM_ACCESS_INFO, str);
905 #endif
906         g_free(date);
907 }
908 #endif
909
910 static void __bt_popup_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
911 {
912         FN_START;
913         retm_if(data == NULL, "data is NULL");
914         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
915
916         if (ad->info_popup) {
917                 evas_object_del(ad->info_popup);
918                 ad->info_popup = NULL;
919         }
920         FN_END;
921 }
922
923 void __bt_popup_del_by_timeout(void *data, Evas_Object *obj, void *event_info)
924 {
925         FN_START;
926         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
927         ret_if(!ad);
928         if (ad->info_popup) {
929                 evas_object_del(ad->info_popup);
930                 ad->info_popup = NULL;
931         }
932         FN_END;
933 }
934
935 Evas_Object *__bt_create_error_popup(bt_share_appdata_t *ad)
936 {
937         FN_START;
938         Evas_Object *popup = NULL;
939         retvm_if(ad == NULL, NULL, "ad is NULL");
940
941         popup = elm_popup_add(ad->win);
942         retvm_if(popup == NULL, NULL, "popup is NULL");
943
944         elm_object_focus_set(popup, EINA_FALSE);
945         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
946
947         elm_object_text_set(popup, BT_STR_UNABLE_TO_FIND_APPLICATION);
948
949         elm_popup_timeout_set(popup, 3);
950         evas_object_smart_callback_add(popup, "timeout", (Evas_Smart_Cb)__bt_popup_del_by_timeout, ad);
951         evas_object_show(popup);
952         evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, __bt_popup_del_cb, ad);
953
954         ad->info_popup = popup;
955
956         FN_END;
957         return popup;
958 }
959
960 static bt_file_type_e __get_file_type(const char *name)
961 {
962         FN_START;
963
964         char *extn = NULL;
965
966         extn = strrchr(name, '.');
967         if (extn != NULL)
968                 extn++;
969
970         if (extn != NULL) {
971                 DBG("extn : %s", extn);
972                 if (!strcasecmp(extn, "png") || !strcasecmp(extn, "bmp") || !strcasecmp(extn, "gif") ||
973                          !strcasecmp(extn, "jpg") || !strcasecmp(extn, "jpeg") || !strcasecmp(extn, "jpe") ||
974                           !strcasecmp(extn, "jp2") || !strcasecmp(extn, "pjpeg") || !strcasecmp(extn, "tif") ||
975                            !strcasecmp(extn, "wbmp") || !strcasecmp(extn, "wmf"))
976                         return BT_FILE_IMAGE;
977                 else if (!strcasecmp(extn, "vcf"))
978                         return BT_FILE_VCARD;
979                 else if (!strcasecmp(extn, "vcs"))
980                         return BT_FILE_VCAL;
981                 else if (!strcasecmp(extn, "vbm"))
982                         return BT_FILE_VBOOKMARK;
983         }
984         FN_END;
985         return BT_FILE_OTHER;
986 }
987
988 static gboolean __bt_open_file(const char *path)
989 {
990         FN_START;
991         app_control_h handle;
992         int ret;
993         bt_file_type_e file_type;
994         bt_share_appdata_t *ad = app_state;
995
996         app_control_create(&handle);
997         app_control_set_operation(handle, APP_CONTROL_OPERATION_VIEW);
998         app_control_set_uri(handle, path);
999
1000         file_type = __get_file_type(path);
1001
1002         if (file_type == BT_FILE_IMAGE) {
1003                 app_control_set_mime(handle, "image/*");
1004                 app_control_add_extra_data(handle, "Path", path);
1005                 app_control_set_launch_mode(handle, APP_CONTROL_LAUNCH_MODE_GROUP);
1006         }
1007
1008         ret = app_control_send_launch_request(handle, NULL, NULL);
1009
1010         if (ret == APP_CONTROL_ERROR_APP_NOT_FOUND)
1011                 __bt_create_error_popup(ad);
1012
1013         app_control_destroy(handle);
1014         FN_END;
1015         return TRUE;
1016 }
1017
1018 static Elm_Object_Item * __bt_add_file_title_item(bt_share_appdata_t *ad)
1019 {
1020         retvm_if(!ad, NULL, "ad is NULL!");
1021         retvm_if(!ad->tr_genlist, NULL, "tr_genlist is NULL!");
1022         retvm_if(ad->file_title_item, NULL, "file_title_item is exist");
1023
1024         Elm_Object_Item *git = NULL;
1025         git = elm_genlist_item_append(ad->tr_genlist,
1026                         ad->tr_file_title_itc, NULL,
1027                         NULL, ELM_GENLIST_ITEM_NONE,
1028                         NULL, NULL);
1029
1030         if (git == NULL) {
1031                 ERR("elm_genlist_item_append is failed!");
1032         } else {
1033                 elm_genlist_item_select_mode_set(git,
1034                                          ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
1035                 ad->file_title_item = git;
1036         }
1037
1038         return git;
1039 }
1040
1041 static void __bt_tr_data_item_sel(void *data, Evas_Object *obj, void *event_info)
1042 {
1043         bt_share_appdata_t *ad = app_state;
1044         bt_gl_data_t *gl_data = (bt_gl_data_t *) data;
1045         bt_tr_data_t *info = NULL;
1046         char *path = NULL;
1047         char *ext = NULL;
1048         int default_memory = 0;
1049
1050         ret_if(data == NULL);
1051         ret_if(event_info == NULL);
1052
1053         FN_START;
1054
1055         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
1056
1057         info = gl_data->tr_data;
1058         retm_if(info->file_path == NULL, "File Path is NULL");
1059
1060         ext = strrchr(info->file_path, '.');
1061         if (ext++) {
1062                 if (0 == g_strcmp0(ext, "vcf")) {
1063                         if (gl_data->tr_inbound == true) {
1064                                 if (info->tr_status == BT_TRANSFER_SUCCESS)
1065                                         _bt_create_info_popup(ad, BT_STR_RECEIVED_VCF_FILE_ALREADY_IMPORTED_TO_CONTACTS);
1066                         } else {
1067                                 _bt_create_info_popup(ad, BT_STR_VCF_FILES_ARE_TEMPORARY_AND_CANT_BE_OPENED);
1068                         }
1069                         return;
1070                 }
1071         }
1072
1073         if (gl_data->tr_inbound == true) {
1074                 if (info->tr_status == BT_TRANSFER_SUCCESS) {
1075                         if (vconf_get_int(VCONFKEY_SETAPPL_DEFAULT_MEM_BLUETOOTH_INT, &default_memory) != 0)
1076                                 ERR("vconf get failed");
1077
1078                         if (default_memory == BT_DEFAULT_MEM_PHONE)
1079                                 path = g_strdup_printf("%s/%s", BT_DOWNLOAD_PHONE_FOLDER, info->file_path);
1080                         else if (default_memory == BT_DEFAULT_MEM_MMC)
1081                                 path = g_strdup_printf("%s/%s", BT_DOWNLOAD_MMC_FOLDER, info->file_path);
1082
1083                         if (path)
1084                                 INFO_SECURE("path : %s", path);
1085                         else
1086                                 INFO_SECURE("path is empty");
1087
1088                         if (path && access(path, F_OK) == 0)
1089                                 __bt_open_file(path);
1090                         else
1091                                 _bt_create_info_popup(ad, BT_STR_FILE_NOT_EXIST);
1092
1093                         g_free(path);
1094                 }
1095         } else {
1096                 path = info->file_path;
1097                 INFO_SECURE("path : %s", path);
1098
1099                 if (path && access(path, F_OK) == 0) {
1100                         if (g_str_has_prefix(path, BT_CONTACT_SHARE_TMP_DIR) == TRUE)
1101                                 /* TODO: change to proper string when UX is updated */
1102                                 _bt_create_info_popup(ad, BT_STR_FILE_NOT_EXIST);
1103                         else
1104                                 __bt_open_file(path);
1105                 } else {
1106                         _bt_create_info_popup(ad, BT_STR_FILE_NOT_EXIST);
1107                 }
1108         }
1109
1110         FN_END;
1111 }
1112
1113 void _bt_genlist_prepend_tr_data_item(bt_share_appdata_t *ad,
1114                                 Evas_Object *genlist, bt_tr_data_t *info, int tr_type)
1115 {
1116         FN_START;
1117         retm_if(ad == NULL || info == NULL, "Invalid parameters!");
1118         retm_if(ad->tr_data_itc == NULL, "ad->tr_data_itc is NULL!");
1119
1120         bt_gl_data_t *gl_data = NULL;
1121
1122         gl_data = g_new0(bt_gl_data_t, 1);
1123         gl_data->tr_data = info;
1124         DBG("info->tr_status : %d", info->tr_status);
1125         if (tr_type == BT_TR_OUTBOUND || tr_type == BT_TR_INBOUND) {
1126                 if (info->tr_status == BT_TRANSFER_SUCCESS)
1127                         ad->transfer_info->success++;
1128                 else if (info->tr_status == BT_TRANSFER_FAIL)
1129                         ad->transfer_info->failed++;
1130         }
1131
1132         if (tr_type == BT_TR_OUTBOUND) {
1133                 gl_data->tr_inbound = false;
1134
1135                 if (ad->outbound_latest_id < info->id)
1136                         ad->outbound_latest_id = info->id;
1137         } else if (tr_type == BT_TR_INBOUND) {
1138                 gl_data->tr_inbound = true;
1139
1140                 if (ad->inbound_latest_id < info->id)
1141                         ad->inbound_latest_id = info->id;
1142         }
1143
1144         elm_genlist_item_append(genlist, ad->tr_data_itc, gl_data, NULL,
1145                         ELM_GENLIST_ITEM_NONE, __bt_tr_data_item_sel, gl_data);
1146
1147         evas_object_show(genlist);
1148
1149         FN_END;
1150 }
1151
1152 void __bt_update_transfer_count(bt_share_appdata_t *ad, bt_tr_data_t *info)
1153 {
1154         if (info->tr_status == BT_TRANSFER_SUCCESS)
1155                 ad->transfer_info->success++;
1156         else
1157                 ad->transfer_info->failed++;
1158
1159         if (ad->status_item)
1160                 elm_genlist_item_fields_update(ad->status_item, "*",
1161                                 ELM_GENLIST_ITEM_FIELD_TEXT);
1162 }
1163
1164 void _bt_genlist_append_tr_data_item(bt_share_appdata_t *ad,
1165                                         bt_tr_data_t *info, int tr_type)
1166 {
1167         FN_START;
1168
1169         ret_if(ad == NULL || info == NULL);
1170         ret_if(ad->tr_data_itc == NULL);
1171         ret_if(ad->tr_genlist == NULL);
1172
1173         Elm_Object_Item *git = NULL;
1174
1175         if (elm_genlist_items_count(ad->tr_genlist) == 0) {
1176                 Evas_Object *genlist = NULL;
1177                 Elm_Object_Item *navi_it = NULL;
1178
1179                 genlist = __bt_add_tr_data_genlist(ad->tr_view, ad);
1180                 retm_if(genlist == NULL, "genlist is NULL!");
1181
1182                 if (ad->tr_genlist) {
1183                         DBG("Clear the previous genlist");
1184                         elm_genlist_clear(ad->tr_genlist);
1185                         ad->tr_genlist = NULL;
1186                 }
1187
1188                 ad->tr_genlist = genlist;
1189
1190                 if (ad->navi_it) {
1191                         elm_object_item_part_content_set(ad->navi_it,
1192                                         "elm.swallow.content", genlist);
1193                 } else {
1194                         navi_it = elm_naviframe_item_push(ad->navi_fr, NULL,
1195                                         NULL, NULL, genlist, NULL);
1196                         if (navi_it)
1197                                 elm_object_item_domain_translatable_text_set(navi_it,
1198                                                 BT_COMMON_PKG,
1199                                                 (ad->tr_type == BT_TR_INBOUND) ?
1200                                                                 "IDS_ST_HEADER_RECEIVE" : "IDS_ST_HEADER_SEND");
1201                         elm_naviframe_item_pop_cb_set(navi_it, __bt_back_button_cb, ad);
1202                         ad->navi_it = navi_it;
1203                 }
1204                 return;
1205         }
1206
1207         if (!ad->file_title_item) {
1208                 retm_if(NULL == __bt_add_file_title_item(ad),
1209                                 "__bt_add_file_title_item is failed");
1210         }
1211
1212         bt_gl_data_t *gl_data = NULL;
1213         gl_data = g_new0(bt_gl_data_t, 1);
1214         gl_data->tr_data = info;
1215
1216         if (tr_type == BT_TR_OUTBOUND) {
1217                 gl_data->tr_inbound = false;
1218
1219                 if (ad->outbound_latest_id < info->id)
1220                         ad->outbound_latest_id = info->id;
1221         } else if (tr_type == BT_TR_INBOUND) {
1222                 gl_data->tr_inbound = true;
1223
1224                 if (ad->inbound_latest_id < info->id)
1225                         ad->inbound_latest_id = info->id;
1226         }
1227
1228         git = elm_genlist_item_append(ad->tr_genlist, ad->tr_data_itc, gl_data,
1229                         NULL, ELM_GENLIST_ITEM_NONE, __bt_tr_data_item_sel, gl_data);
1230
1231         __bt_update_transfer_count(ad, info);
1232
1233         if (info->tr_status == BT_TRANSFER_ONGOING)
1234                 ad->current_item = git;
1235
1236         evas_object_show(ad->tr_genlist);
1237
1238         FN_END;
1239 }
1240
1241 static void __bt_share_gl_highlighted(void *data, Evas_Object *obj,
1242                                                         void *event_info)
1243 {
1244         FN_START;
1245         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
1246         bt_gl_data_t *gl_data;
1247
1248         ret_if(item == NULL);
1249
1250         gl_data = (bt_gl_data_t *)elm_object_item_data_get(item);
1251         ret_if(gl_data == NULL);
1252
1253         gl_data->highlighted = TRUE;
1254
1255         elm_genlist_item_fields_update(item, "*",
1256                                 ELM_GENLIST_ITEM_FIELD_CONTENT);
1257
1258         FN_END;
1259 }
1260
1261 static void __bt_share_gl_unhighlighted(void *data, Evas_Object *obj,
1262                                                         void *event_info)
1263 {
1264         FN_START;
1265         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
1266         bt_gl_data_t *gl_data;
1267
1268         ret_if(item == NULL);
1269
1270         gl_data = (bt_gl_data_t *)elm_object_item_data_get(item);
1271         ret_if(gl_data == NULL);
1272         gl_data->highlighted = FALSE;
1273
1274         elm_genlist_item_fields_update(item, "*",
1275                                 ELM_GENLIST_ITEM_FIELD_CONTENT);
1276
1277         FN_END;
1278 }
1279
1280 static Evas_Object *__bt_add_tr_data_genlist(Evas_Object *parent,
1281                                                   bt_share_appdata_t *ad)
1282 {
1283         FN_START;
1284         retvm_if(ad == NULL, NULL, "Inavalid parameter!");
1285
1286         Evas_Object *genlist = elm_genlist_add(parent);
1287
1288         evas_object_smart_callback_add(genlist, "highlighted",
1289                                 __bt_share_gl_highlighted, ad);
1290
1291         evas_object_smart_callback_add(genlist, "unhighlighted",
1292                                 __bt_share_gl_unhighlighted, ad);
1293
1294         elm_genlist_homogeneous_set(genlist, EINA_TRUE);
1295         ad->tr_device_itc = elm_genlist_item_class_new();
1296         if (ad->tr_device_itc) {
1297                 ad->tr_device_itc->item_style = "type1";
1298                 ad->tr_device_itc->func.text_get = __bt_tr_device_label_get;
1299                 ad->tr_device_itc->func.content_get = NULL;
1300                 ad->tr_device_itc->func.state_get = NULL;
1301                 ad->tr_device_itc->func.del = NULL;
1302         }
1303
1304         ad->tr_status_itc = elm_genlist_item_class_new();
1305         if (ad->tr_status_itc) {
1306                 ad->tr_status_itc->item_style = "type1";
1307                 ad->tr_status_itc->func.text_get = __bt_tr_status_label_get;
1308                 ad->tr_status_itc->func.content_get = NULL;
1309                 ad->tr_status_itc->func.state_get = NULL;
1310                 ad->tr_status_itc->func.del = NULL;
1311         }
1312
1313         ad->tr_progress_itc = elm_genlist_item_class_new();
1314         if (ad->tr_progress_itc) {
1315                 ad->tr_progress_itc->item_style = "full";
1316                 ad->tr_progress_itc->func.text_get = NULL;
1317                 ad->tr_progress_itc->func.content_get = __bt_tr_progress_icon_get;
1318                 ad->tr_progress_itc->func.state_get = NULL;
1319                 ad->tr_progress_itc->func.del = NULL;
1320         }
1321
1322         ad->tr_file_title_itc = elm_genlist_item_class_new();
1323         if (ad->tr_file_title_itc) {
1324                 ad->tr_file_title_itc->item_style = "groupindex";
1325                 ad->tr_file_title_itc->func.text_get = __bt_tr_file_title_label_get;
1326                 ad->tr_file_title_itc->func.content_get = NULL;
1327                 ad->tr_file_title_itc->func.state_get = NULL;
1328                 ad->tr_file_title_itc->func.del = NULL;
1329         }
1330
1331         ad->tr_data_itc = elm_genlist_item_class_new();
1332         if (ad->tr_data_itc) {
1333                 ad->tr_data_itc->item_style = "type1";
1334                 ad->tr_data_itc->func.text_get = __bt_tr_label_get;
1335                 ad->tr_data_itc->func.content_get = __bt_tr_icon_get;
1336                 ad->tr_data_itc->func.state_get = NULL;
1337                 ad->tr_data_itc->func.del = __bt_tr_del;
1338         }
1339
1340         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
1341         FN_END;
1342         return genlist;
1343 }
1344
1345 static Evas_Object * __bt_create_naviframe(bt_share_appdata_t *ad)
1346 {
1347         FN_START;
1348         retv_if(ad == NULL, NULL);
1349         Evas_Object *navi_fr = NULL;
1350         /* Naviframe */
1351         navi_fr = elm_naviframe_add(ad->tr_view);
1352         eext_object_event_callback_add(navi_fr, EEXT_CALLBACK_BACK,
1353                                                 eext_naviframe_back_cb, NULL);
1354         elm_object_part_content_set(ad->tr_view, "elm.swallow.content", navi_fr);
1355         evas_object_show(navi_fr);
1356         FN_END;
1357         return navi_fr;
1358 }
1359
1360 void _bt_cb_state_changed(int result,
1361                         bt_adapter_state_e adapter_state,
1362                         void *user_data)
1363 {
1364         FN_START;
1365         DBG("bluetooth %s", adapter_state == BT_ADAPTER_ENABLED ?
1366                                 "enabled" : "disabled");
1367
1368         ret_if(!user_data);
1369         ret_if(result != BT_ERROR_NONE);
1370
1371         bt_share_appdata_t *ad = (bt_share_appdata_t *)user_data;
1372
1373         ad->bt_status = adapter_state;
1374
1375         if (adapter_state == BT_ADAPTER_ENABLED && ad->send_after_turning_on) {
1376                 DBG("Adapter enabled, resend pending items");
1377                 /* close turning on popup */
1378                 if (ad->turning_on_popup) {
1379                         evas_object_del(ad->turning_on_popup);
1380                         ad->turning_on_popup = NULL;
1381                 }
1382
1383                 _bt_share_ui_retry_failed(ad);
1384                 if (ad->launch_mode == BT_LAUNCH_TRANSFER_LIST) {
1385                         int noti_id;
1386                         char *opp_role;
1387                         bt_tr_db_table_e table;
1388
1389                         __bt_get_noti_id_opp_role_and_table(ad, &noti_id, &opp_role, &table);
1390
1391                         DBG_SECURE("Notification ID: %d", noti_id);
1392                         if (noti_id < 0) {
1393                                 ERR("Invalid Notification ID");
1394                         } else {
1395                                 sqlite3 *db = bt_share_open_db();
1396                                 bt_share_remove_tr_data_by_notification(db, table, noti_id);
1397                                 bt_share_close_db(db);
1398                                 _bt_delete_selected_notification(ad->tr_type, noti_id, opp_role);
1399                         }
1400                 }
1401
1402                 ad->send_after_turning_on = FALSE;
1403                 _bt_terminate_app();
1404         }
1405         FN_END;
1406 }
1407
1408 gboolean __bt_share_is_battery_low(void)
1409 {
1410         FN_START;
1411
1412         int value = 0;
1413         int charging = 0;
1414
1415         if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, (void *)&charging))
1416                 ERR("Get the battery charging status fail");
1417
1418         if (charging == 1)
1419                 return FALSE;
1420
1421         DBG("charging: %d", charging);
1422
1423         if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, (void *)&value)) {
1424                 ERR("Get the battery low status fail");
1425                 return FALSE;
1426         }
1427
1428         if (value <= VCONFKEY_SYSMAN_BAT_POWER_OFF)
1429                 return TRUE;
1430
1431         FN_END;
1432         return FALSE;
1433 }
1434
1435 int _bt_share_enable_bt(bt_share_appdata_t *ad)
1436 {
1437         FN_START;
1438         int ret;
1439         retv_if(ad == NULL, -1);
1440 /*
1441         if (__bt_share_is_battery_low() == TRUE) {
1442                 // Battery is critical low
1443                 _bt_main_create_information_popup(ad, BT_STR_LOW_BATTERY);
1444                 return -1;
1445         }
1446 */
1447
1448         ret = bt_adapter_enable();
1449         if (ret == BT_ERROR_ALREADY_DONE) {
1450                 _bt_cb_state_changed(BT_ERROR_NONE, BT_ADAPTER_ENABLED, ad);
1451         } else if (ret == BT_ERROR_NOW_IN_PROGRESS) {
1452                 ERR("Enabling in progress [%d]", ret);
1453         } else if (ret != BT_ERROR_NONE) {
1454                 ERR("Failed to enable bluetooth [%d]", ret);
1455         } else {
1456                 ad->turning_on_popup = _bt_share_add_turning_on_popup(ad);
1457                 ad->send_after_turning_on = TRUE;
1458         }
1459         FN_END;
1460         return 0;
1461 }
1462
1463 void _bt_share_toolbar_button_cb(void *data, Evas_Object *obj,
1464                 void *event_info)
1465 {
1466         FN_START;
1467         ret_if(!data);
1468         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
1469         bt_share_abort_data_t *abort_data = g_new0(bt_share_abort_data_t, 1);
1470
1471         const char *text = elm_object_text_get(obj);
1472
1473         if (g_strcmp0(text, BT_STR_STOP) == 0) {
1474                 if (ad->transfer_info) {
1475                         abort_data->transfer_id = ad->transfer_info->transfer_id;
1476                         abort_data->transfer_type = ad->transfer_info->transfer_type;
1477                         _bt_abort_signal_send(ad, abort_data);
1478                 }
1479         } else if (g_strcmp0(text, BT_STR_RESEND_FAILED_FILES) == 0) {
1480                 /* for BT off case */
1481                 if (ad->bt_status == BT_ADAPTER_DISABLED) {
1482                         _bt_share_enable_bt(ad);
1483                 } else {
1484                         _bt_share_ui_retry_failed(ad);
1485                         if (ad->launch_mode == BT_LAUNCH_TRANSFER_LIST) {
1486                                 int noti_id;
1487                                 char *opp_role;
1488                                 bt_tr_db_table_e table;
1489
1490                                 __bt_get_noti_id_opp_role_and_table(ad, &noti_id, &opp_role, &table);
1491
1492                                 DBG_SECURE("Notification ID: %d", noti_id);
1493                                 if (noti_id < 0) {
1494                                         ERR("Invalid Notification ID");
1495                                 } else {
1496                                         sqlite3 *db = bt_share_open_db();
1497                                         bt_share_remove_tr_data_by_notification(db, table, noti_id);
1498                                         bt_share_close_db(db);
1499                                         _bt_delete_selected_notification(ad->tr_type, noti_id, opp_role);
1500                                 }
1501                         }
1502                         _bt_terminate_app();
1503                 }
1504         }
1505
1506         g_free(abort_data);
1507         FN_END;
1508 }
1509
1510 Evas_Object * _bt_share_create_toolbar_button(bt_share_appdata_t *ad, char *text)
1511 {
1512         Evas_Object *layout = NULL;
1513         Evas_Object *toolbar_button = NULL;
1514
1515         layout = elm_layout_add(ad->navi_fr);
1516         elm_layout_file_set(layout, EDJFILE, "toolbar_button_ly");
1517
1518         toolbar_button = elm_button_add(layout);
1519
1520         /* Use "bottom" style button */
1521         elm_object_style_set(toolbar_button, "bottom");
1522         evas_object_size_hint_weight_set(toolbar_button, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1523         evas_object_size_hint_align_set(toolbar_button, EVAS_HINT_FILL, 0.5);
1524
1525         elm_object_text_set(toolbar_button, text);
1526
1527         evas_object_smart_callback_add(toolbar_button, "clicked",
1528                         _bt_share_toolbar_button_cb, ad);
1529
1530         /* Set button into "toolbar" swallow part */
1531         elm_object_part_content_set(layout, "button", toolbar_button);
1532         elm_object_item_part_content_set(ad->navi_it, "toolbar", layout);
1533         ad->toolbar_ly = layout;
1534         return toolbar_button;
1535 }
1536
1537 void _bt_share_delete_toolbar_button(bt_share_appdata_t *ad)
1538 {
1539         FN_START;
1540         ret_if(!ad);
1541
1542         if (ad->toolbar_button) {
1543                 evas_object_del(ad->toolbar_button);
1544                 ad->toolbar_button = NULL;
1545         }
1546         if (ad->toolbar_ly) {
1547                 evas_object_del(ad->toolbar_ly);
1548                 ad->toolbar_ly = NULL;
1549         }
1550         FN_END;
1551 }
1552
1553 static Eina_Bool __bt_list_item_add(bt_share_appdata_t *ad)
1554 {
1555         FN_START;
1556         int i;
1557
1558         retv_if(!ad, EINA_FALSE);
1559         GSList *tr_data_list = ad->tr_data_list;
1560         retv_if(ad->launch_mode == BT_LAUNCH_TRANSFER_LIST && !tr_data_list, EINA_FALSE);
1561
1562         /* Add first 5 genlist items */
1563         for (i = 0; NULL != tr_data_list && i < 5; i++) {
1564                 _bt_genlist_prepend_tr_data_item(ad,  ad->tr_genlist, tr_data_list->data,
1565                                                 ad->tr_type);
1566                 tr_data_list = g_slist_next(tr_data_list);
1567         }
1568
1569         if (ad->launch_mode == BT_LAUNCH_TRANSFER_LIST &&
1570                         ad->tr_type == BT_TR_OUTBOUND && ad->transfer_info->failed) {
1571                 ad->toolbar_button = _bt_share_create_toolbar_button(ad,
1572                                 BT_STR_RESEND_FAILED_FILES);
1573         }
1574
1575         FN_END;
1576
1577         return EINA_TRUE;
1578 }
1579
1580 static Eina_Bool __bt_list_item_idler(void *data)
1581 {
1582         FN_START;
1583
1584         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
1585
1586         retv_if(!ad, EINA_FALSE);
1587         GSList *tr_data_list = ad->tr_data_list;
1588         retv_if(ad->launch_mode == BT_LAUNCH_TRANSFER_LIST && !tr_data_list, EINA_FALSE);
1589
1590         DBG("Total Items in List : %d", g_slist_length(tr_data_list));
1591         /* Add rest of the genlist items */
1592         if (g_slist_length(ad->tr_data_list) >= 5) {
1593                 tr_data_list = g_slist_nth(ad->tr_data_list, 5);
1594
1595                 while (NULL != tr_data_list) {
1596                         _bt_genlist_prepend_tr_data_item(ad,  ad->tr_genlist, tr_data_list->data,
1597                                                         ad->tr_type);
1598                         tr_data_list = g_slist_next(tr_data_list);
1599                 }
1600         }
1601
1602         if (ad->launch_mode == BT_LAUNCH_TRANSFER_LIST &&
1603                         ad->tr_type == BT_TR_OUTBOUND &&
1604                         ad->transfer_info->failed &&
1605                         ad->toolbar_button == NULL) {
1606                 ad->toolbar_button = _bt_share_create_toolbar_button(ad,
1607                                 BT_STR_RESEND_FAILED_FILES);
1608         }
1609
1610         /* Delete the notification */
1611         /* TODO: Delete Notification only if
1612          * transfer(sent) is completed with no failed items or received screen related to this session */
1613         if (ad->launch_mode == BT_LAUNCH_TRANSFER_LIST &&
1614                         ((ad->tr_type == BT_TR_OUTBOUND &&
1615                                         ad->transfer_info->failed == 0) || ad->tr_type == BT_TR_INBOUND)) {
1616                 int noti_id;
1617                 char *opp_role;
1618                 bt_tr_db_table_e table;
1619
1620                 __bt_get_noti_id_opp_role_and_table(ad, &noti_id, &opp_role, &table);
1621
1622                 DBG_SECURE("Notification ID: %d", noti_id);
1623                 if (noti_id < 0) {
1624                         ERR("Invalid Notification ID");
1625                 } else {
1626                         if (ad->bt_status == BT_ADAPTER_DISABLED) {
1627                                 sqlite3 *db = bt_share_open_db();
1628                                 bt_share_remove_tr_data_by_notification(db, table, noti_id);
1629                                 bt_share_close_db(db);
1630                         }
1631                         _bt_delete_selected_notification(ad->tr_type, noti_id, opp_role);
1632                 }
1633         }
1634
1635         if (ad->status_item)
1636                 elm_genlist_item_fields_update(ad->status_item, "*",
1637                                 ELM_GENLIST_ITEM_FIELD_TEXT);
1638
1639         FN_END;
1640         return EINA_FALSE;
1641 }
1642
1643 int _bt_create_transfer_view(bt_share_appdata_t *ad)
1644 {
1645         FN_START;
1646         retv_if(ad == NULL, -1);
1647
1648         Elm_Object_Item *navi_it = NULL;
1649         Elm_Object_Item *git = NULL;
1650         Evas_Object *conform = NULL;
1651         Evas_Object *navi_fr = NULL;
1652         Evas_Object *bg = NULL;
1653         Evas_Object *layout = NULL;
1654         Evas_Object *genlist = NULL;
1655
1656         __bt_clear_view(ad);
1657         bg = _bt_create_bg(ad->win, NULL);
1658         retv_if(bg == NULL, -1);
1659         ad->bg = bg;
1660
1661         conform = _bt_create_conformant(ad->win, NULL);
1662         retvm_if(conform == NULL, -1, "conform is NULL!");
1663         ad->conform = conform;
1664
1665         bg = elm_bg_add(conform);
1666         elm_object_style_set(bg, "indicator/headerbg");
1667         elm_object_part_content_set(conform, "elm.swallow.indicator_bg", bg);
1668         evas_object_show(bg);
1669
1670         layout = _bt_create_layout(conform, EDJFILE, "share_view");
1671         ad->tr_view = layout;
1672
1673         elm_object_content_set(conform, layout);
1674         elm_win_conformant_set(ad->win, EINA_TRUE);
1675
1676         navi_fr = __bt_create_naviframe(ad);
1677         retvm_if(navi_fr == NULL, -1, "navi_fr is NULL!");
1678         ad->navi_fr = navi_fr;
1679
1680         /* Genlist */
1681         genlist = __bt_add_tr_data_genlist(layout, ad);
1682         retvm_if(genlist == NULL, -1, "genlist is NULL!");
1683         ad->tr_genlist = genlist;
1684
1685         git = elm_genlist_item_append(genlist,
1686                         ad->tr_device_itc, ad,
1687                         NULL, ELM_GENLIST_ITEM_NONE,
1688                         NULL, NULL);
1689         if (git == NULL) {
1690                 ERR("elm_genlist_item_append is failed!");
1691         } else {
1692                 elm_genlist_item_select_mode_set(git,
1693                                          ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
1694                 ad->device_item = git;
1695         }
1696
1697         if (ad->launch_mode == BT_LAUNCH_TRANSFER_LIST) {
1698                 git = elm_genlist_item_append(genlist,
1699                                 ad->tr_status_itc, ad,
1700                                 NULL, ELM_GENLIST_ITEM_NONE,
1701                                 NULL, NULL);
1702                 if (git == NULL) {
1703                         ERR("elm_genlist_item_append is failed!");
1704                 } else {
1705                         elm_genlist_item_select_mode_set(git,
1706                                                  ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
1707                         ad->status_item = git;
1708                 }
1709         } else if (ad->launch_mode == BT_LAUNCH_ONGOING) {
1710                 if (ad->progress_item) {
1711                         elm_object_item_del(ad->progress_item);
1712                         ad->progress_item = NULL;
1713                         ad->progressbar = NULL;
1714                 }
1715
1716                 git = elm_genlist_item_append(genlist,
1717                                 ad->tr_progress_itc, ad,
1718                                 NULL, ELM_GENLIST_ITEM_NONE,
1719                                 NULL, NULL);
1720                 if (git == NULL) {
1721                         ERR("elm_genlist_item_append is failed!");
1722                 } else {
1723                         elm_genlist_item_select_mode_set(git,
1724                                                  ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
1725                         ad->progress_item = git;
1726                 }
1727         }
1728
1729         __bt_add_file_title_item(ad);
1730
1731         navi_it = elm_naviframe_item_push(navi_fr, NULL,
1732                                         NULL, NULL, genlist, NULL);
1733         if (navi_it == NULL) {
1734                 ERR("elm_naviframe_item_push is failed!");
1735         } else {
1736                 elm_object_item_domain_translatable_text_set(navi_it, BT_COMMON_PKG,
1737                 (ad->tr_type == BT_TR_INBOUND) ? "IDS_ST_HEADER_RECEIVE" : "IDS_ST_HEADER_SEND");
1738                 elm_naviframe_item_pop_cb_set(navi_it, __bt_back_button_cb, ad);
1739                 ad->navi_it = navi_it;
1740         }
1741
1742         if (ad->launch_mode == BT_LAUNCH_ONGOING) {
1743                 ad->toolbar_button = _bt_share_create_toolbar_button(ad,
1744                                 BT_STR_STOP);
1745         }
1746
1747         __bt_list_item_add(ad);
1748
1749         ad->idler = ecore_idler_add(__bt_list_item_idler, ad);
1750         if (!ad->idler)
1751                 ERR("idler can not be added");
1752
1753         FN_END;
1754         return 0;
1755 }