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