4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * GirishAshok Joshi <girish.joshi@samsung.com>
8 * DoHyun Pyun <dh79.pyun@samsung.com>
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
14 * http://www.tizenopensource.org/license
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.
26 #include <app_control.h>
27 #include <app_control_internal.h>
33 #include <vconf-keys.h>
36 #include <X11/Xatom.h>
37 #include <X11/Xutil.h>
40 #include <efl_extension.h>
42 #include "bt-share-ui-main.h"
43 #include "bt-share-ui-view.h"
44 #include "bt-share-ui-popup.h"
45 #include "bt-share-ui-ipc.h"
46 #include "bt-share-ui-resource.h"
47 #include "bt-share-ui-widget.h"
48 #include <bluetooth-share-api.h>
49 #include <bluetooth.h>
50 #include <bluetooth_internal.h>
51 #include <bundle_internal.h>
52 #include <system_info.h>
54 bt_share_appdata_t *app_state = NULL;
55 bt_share_appdata_t app_data = {0,};
57 tizen_profile_t _get_tizen_profile()
59 static tizen_profile_t profile = _PROFILE_UNKNOWN;
60 char *profileName = NULL;
62 if (__builtin_expect(profile != _PROFILE_UNKNOWN, 1))
65 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
67 return _PROFILE_UNKNOWN;
69 switch (*profileName) {
72 profile = _PROFILE_MOBILE;
76 profile = _PROFILE_WEARABLE;
80 profile = _PROFILE_TV;
84 if (!strncasecmp(profileName, "ivi", 3))
85 profile = _PROFILE_IVI;
86 else if (!strncasecmp(profileName, "iot", 3))
87 profile = _PROFILE_IOT;
89 profile = _PROFILE_COMMON;
91 default: // common or unknown ==> ALL ARE COMMON.
92 profile = _PROFILE_COMMON;
99 static void __bt_lang_changed_cb(app_event_info_h event_info, void *data)
102 ret_if(data == NULL);
104 bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
107 elm_genlist_realized_items_update(ad->tr_genlist);
109 if (ad->toolbar_button) {
110 DBG("Changing language for toolbar_button: ad->launch_mode: %d",
113 if ((ad->tr_type == BT_TR_INBOUND) || (ad->launch_mode == BT_LAUNCH_ONGOING))
114 elm_object_text_set(ad->toolbar_button, BT_STR_STOP);
116 elm_object_text_set(ad->toolbar_button, BT_STR_RESEND_FAILED_FILES);
121 static bt_share_launch_mode_t __bt_parse_launch_mode(bt_share_appdata_t *ad, bundle *b)
123 const char *launch_type = NULL;
124 bt_share_launch_mode_t launch_mode = BT_LAUNCH_NONE;
125 retvm_if(ad == NULL, -1, "Invalid param");
126 retvm_if(b == NULL, -1, "Invalid param");
128 launch_type = bundle_get_val(b, "launch-type");
129 retv_if(!launch_type, -1);
131 if (!strcasecmp(launch_type, "ongoing"))
132 launch_mode = BT_LAUNCH_ONGOING;
133 else if (!strcasecmp(launch_type, "transfer_list"))
134 launch_mode = BT_LAUNCH_TRANSFER_LIST;
136 ERR("Invalid bundle value");
141 static void __bt_share_free_tr_data(bt_share_transfer_data_t *tr_data)
144 ret_if(tr_data == NULL);
146 g_free((char *) tr_data->device_name);
147 g_free((char *) tr_data->device_address);
148 g_free((char *) tr_data->transfer_type);
149 g_free((char *) tr_data->db_sid);
150 g_free((char *) tr_data->filename);
155 static int __bt_share_launch_handler(bt_share_appdata_t *ad, bundle *b,
156 bt_share_launch_mode_t launch_mode)
159 const char *transfer_type = NULL;
160 const char *temp = NULL;
162 unsigned char tranferred;
166 retvm_if(ad == NULL, -1, "Invalid param");
167 retvm_if(b == NULL, -1, "Invalid param");
169 if (launch_mode == BT_LAUNCH_TRANSFER_LIST ||
170 launch_mode == BT_LAUNCH_ONGOING) {
171 INFO("%s", launch_mode == BT_LAUNCH_TRANSFER_LIST ?
172 "BT_LAUNCH_TRANSFER_LIST" : "BT_LAUNCH_ONGOING");
173 bt_share_transfer_data_t *transfer_data = NULL;
175 elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
176 elm_win_indicator_opacity_set(ad->win, ELM_WIN_INDICATOR_OPAQUE);
178 transfer_type = bundle_get_val(b, "transfer_type");
179 retvm_if(!transfer_type, -1, "Invalid transfer type!");
181 if (!strcmp(transfer_type, "outbound"))
182 tr_type = BT_TR_OUTBOUND;
184 tr_type = BT_TR_INBOUND;
187 db = bt_share_open_db();
188 retvm_if(!db, -1, "fail to open db!");
190 /* Create Transfer Data */
191 transfer_data = g_new0(bt_share_transfer_data_t, 1);
192 transfer_data->device_name = g_strdup(bundle_get_val(b, "device_name"));
193 transfer_data->device_address = g_strdup(bundle_get_val(b, "device_addr"));
194 transfer_data->transfer_type = g_strdup(bundle_get_val(b, "transfer_type"));
195 transfer_data->db_sid = g_strdup(bundle_get_val(b, "db_sid"));
197 INFO("Create Transfer data - Name: %s, Address :%s, type: %s, SID: %s",
198 transfer_data->device_name, transfer_data->device_address,
199 transfer_data->transfer_type, transfer_data->db_sid);
201 if (launch_mode == BT_LAUNCH_TRANSFER_LIST) {
202 temp = bundle_get_val(b, "transfer_id");
204 transfer_data->transfer_id = atoi(temp);
205 INFO_SECURE("Transfer ID: %d", transfer_data->transfer_id);
207 if (tr_type == BT_TR_INBOUND) {
209 DBG("INBOUND TRANSFER");
211 /* Check if noti_id is updated or not. If noti_id is not updated it means
212 * it's an ongoing transfer. If its an ongoing transfer then redirect it to
213 * progress view, otherwise just launch the transfer list.
215 noti_id = bt_share_get_noti_id(db, BT_DB_INBOUND, transfer_data->db_sid);
217 DBG("noti_id: [%d]", noti_id);
218 /* Get transfer progress only if its an ongoing transfer. */
219 if (!noti_id && bt_opp_get_transfer_progress(BT_TRANSFER_INBOUND,
220 transfer_data->transfer_id, &tranferred) == 0) {
221 DBG("INBOUND ONGOING TRANSFER");
222 launch_mode = BT_LAUNCH_ONGOING;
223 transfer_data->percentage = tranferred;
225 transfer_data->filename = g_strdup(bundle_get_val(b, "filename"));
226 INFO_SECURE("File Name: %s", transfer_data->filename);
230 _bt_set_opc_launched_session(FALSE);
232 /* Create Progress Data */
233 transfer_data->filename = g_strdup(bundle_get_val(b, "filename"));
234 INFO_SECURE("Create Progress Data : File Name: %s", transfer_data->filename);
235 temp = bundle_get_val(b, "size");
237 transfer_data->size = atol(temp);
238 temp = bundle_get_val(b, "transfer_id");
240 transfer_data->transfer_id = atoi(temp);
241 INFO_SECURE("Transfer ID: %d", transfer_data->transfer_id);
244 temp = bundle_get_val(b, "progress_cnt");
247 INFO("PROGRESS TEXT: %s", temp);
248 current = strtol(temp + 1, &stop, 10);
249 total = strtol(stop + 1, &stop, 10);
250 transfer_data->current_file = current;
251 transfer_data->total_files = total;
253 INFO("File Progress: [%d/%d]", transfer_data->current_file,
254 transfer_data->total_files);
256 if (tr_type == BT_TR_OUTBOUND) {
257 bt_opp_get_transfer_progress(BT_TRANSFER_OUTBOUND,
259 _bt_set_opc_launched_session(TRUE);
261 bt_opp_get_transfer_progress(BT_TRANSFER_INBOUND,
262 transfer_data->transfer_id, &tranferred);
264 transfer_data->percentage = tranferred;
267 if (ad->tr_view && ad->tr_type == tr_type && ad->launch_mode == launch_mode &&
268 g_strcmp0(ad->db_sid, transfer_data->db_sid) == 0) {
269 ERR("Same view. no need to create transfer view!");
271 /* Show the TOP of List */
272 elm_genlist_item_show(ad->device_item, ELM_GENLIST_ITEM_SCROLLTO_TOP);
273 __bt_share_free_tr_data(transfer_data);
274 bt_share_close_db(db);
278 __bt_share_free_tr_data(ad->transfer_info);
279 ad->transfer_info = transfer_data;
280 transfer_data->success = 0;
281 transfer_data->failed = 0;
282 ad->launch_mode = launch_mode;
284 ad->tr_type = tr_type;
286 /* Get data from share DB */
291 ad->db_sid = g_strdup(transfer_data->db_sid);
292 INFO_SECURE("Device Address: %s", transfer_data->device_address);
293 ad->tr_data_list = bt_share_get_all_tr_data_by_sid(db,
294 ad->tr_type, transfer_data->device_address,
295 transfer_data->db_sid);
297 bt_share_close_db(db);
299 _bt_create_transfer_view(ad);
302 ERR("Invalid bundle value");
310 static bool __app_create(void *data)
313 bt_share_appdata_t *ad = data;
314 Evas_Object *win = NULL;
316 bt_adapter_state_e status = BT_ADAPTER_DISABLED;
318 elm_app_base_scale_set(1.8);
321 win = _bt_create_win(PACKAGE);
322 retv_if(win == NULL, false);
325 bindtextdomain(BT_COMMON_PKG, BT_COMMON_RES);
327 if (bt_initialize() != BT_ERROR_NONE)
328 ERR("bt_initialize() failed");
330 if (bt_adapter_get_state(&status) != BT_ERROR_NONE)
331 ERR("bt_adapter_get_state() failed!");
332 DBG("bt_status : %d", status);
334 ad->bt_status = status;
336 /* Set event callbacks */
338 bt_adapter_set_state_changed_cb(_bt_cb_state_changed, (void *)ad);
339 if (ret != BT_ERROR_NONE)
340 ERR("bt_adapter_set_state_changed_cb failed");
345 static void __app_service(app_control_h app_control, void *user_data)
347 INFO("__app_service");
348 bt_share_appdata_t *ad = user_data;
351 bt_share_launch_mode_t launch_mode;
354 ret = app_control_export_as_bundle(app_control, &b);
356 if (ad->dbus_conn == NULL)
359 launch_mode = __bt_parse_launch_mode(ad, b);
360 if (launch_mode == BT_LAUNCH_NONE) {
367 ret = __bt_share_launch_handler(ad, b, launch_mode);
371 evas_object_show(ad->win);
372 elm_win_activate(ad->win);
376 static void __app_terminate(void *data)
379 bt_share_appdata_t *ad = data;
382 _bt_destroy_info_popup(ad);
383 _bt_signal_deinit(ad);
385 if (ad->tr_device_itc) {
386 elm_genlist_item_class_free(ad->tr_device_itc);
387 ad->tr_device_itc = NULL;
389 if (ad->tr_status_itc) {
390 elm_genlist_item_class_free(ad->tr_status_itc);
391 ad->tr_status_itc = NULL;
393 if (ad->tr_progress_itc) {
394 elm_genlist_item_class_free(ad->tr_progress_itc);
395 ad->tr_progress_itc = NULL;
397 if (ad->tr_file_title_itc) {
398 elm_genlist_item_class_free(ad->tr_file_title_itc);
399 ad->tr_file_title_itc = NULL;
401 if (ad->tr_data_itc) {
402 elm_genlist_item_class_free(ad->tr_data_itc);
403 ad->tr_data_itc = NULL;
405 if (ad->transfer_info) {
406 __bt_share_free_tr_data(ad->transfer_info);
407 ad->transfer_info = NULL;
411 ecore_idler_del(ad->idler);
418 err = bt_adapter_unset_state_changed_cb();
419 if (err != BT_ERROR_NONE)
420 ERR("unset of state change cb failed: %d", err);
422 err = bt_deinitialize();
423 if (err != BT_ERROR_NONE)
424 ERR("bt_deinitialize failed: %d", err);
428 static void __app_pause(void *data)
430 INFO("__app_pause ");
433 static void __app_resume(void *data)
435 INFO("__app_resume");
438 EXPORT int main(int argc, char *argv[])
440 DBG("Start bluetooth-share-ui main()");
442 ui_app_lifecycle_callback_s callback = {0,};
443 app_event_handler_h lang_changed_handler;
444 app_state = &app_data;
446 callback.create = __app_create;
447 callback.terminate = __app_terminate;
448 callback.pause = __app_pause;
449 callback.resume = __app_resume;
450 callback.app_control = __app_service;
452 ui_app_add_event_handler(&lang_changed_handler, APP_EVENT_LANGUAGE_CHANGED, __bt_lang_changed_cb, &app_data);
454 DBG("ui_app_main() is called.");
455 int ret = ui_app_main(argc, argv, &callback, &app_data);
456 if (ret != APP_ERROR_NONE)
457 ERR("ui_app_main() is failed. err = %d", ret);
459 DBG("End bluetooth-share-ui main()");