Modify the genlist item style for IOT profile
[apps/native/bluetooth-share-ui.git] / src / bt-share-ui-main.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 <stdio.h>
25 #include <app.h>
26 #include <app_control.h>
27 #include <app_control_internal.h>
28 #if 0
29 #include <Ecore_X.h>
30 #include <utilX.h>
31 #endif
32 #include <vconf.h>
33 #include <vconf-keys.h>
34 #include <E_DBus.h>
35 #if 0
36 #include <X11/Xatom.h>
37 #include <X11/Xutil.h>
38 #endif
39 #include <aul.h>
40 #include <efl_extension.h>
41 #include "applog.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>
53
54 bt_share_appdata_t *app_state = NULL;
55 bt_share_appdata_t app_data = {0,};
56
57 tizen_profile_t _get_tizen_profile()
58 {
59         static tizen_profile_t profile = _PROFILE_UNKNOWN;
60         char *profileName = NULL;
61
62         if (__builtin_expect(profile != _PROFILE_UNKNOWN, 1))
63                 return profile;
64
65         system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
66         if (!profileName)
67                 return _PROFILE_UNKNOWN;
68
69         switch (*profileName) {
70                 case 'm':
71                 case 'M':
72                         profile = _PROFILE_MOBILE;
73                         break;
74                 case 'w':
75                 case 'W':
76                         profile = _PROFILE_WEARABLE;
77                         break;
78                 case 't':
79                 case 'T':
80                         profile = _PROFILE_TV;
81                         break;
82                 case 'i':
83                 case 'I':
84                         if (!strncasecmp(profileName, "ivi", 3))
85                                 profile = _PROFILE_IVI;
86                         else if (!strncasecmp(profileName, "iot", 3))
87                                 profile = _PROFILE_IOT;
88                         else
89                                 profile = _PROFILE_COMMON;
90                         break;
91                 default: // common or unknown ==> ALL ARE COMMON.
92                         profile = _PROFILE_COMMON;
93         }
94         free(profileName);
95
96         return profile;
97 }
98
99 static void __bt_lang_changed_cb(app_event_info_h event_info, void *data)
100 {
101         DBG("+");
102         ret_if(data == NULL);
103
104         bt_share_appdata_t *ad = (bt_share_appdata_t *)data;
105
106         if (ad->tr_genlist)
107                 elm_genlist_realized_items_update(ad->tr_genlist);
108
109         if (ad->toolbar_button) {
110                 DBG("Changing language for toolbar_button: ad->launch_mode: %d",
111                                 ad->launch_mode);
112
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);
115                 else
116                         elm_object_text_set(ad->toolbar_button, BT_STR_RESEND_FAILED_FILES);
117         }
118
119 }
120
121 static bt_share_launch_mode_t __bt_parse_launch_mode(bt_share_appdata_t *ad, bundle *b)
122 {
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");
127
128         launch_type = bundle_get_val(b, "launch-type");
129         retv_if(!launch_type, -1);
130
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;
135         else
136                 ERR("Invalid bundle value");
137
138         return launch_mode;
139 }
140
141 static void __bt_share_free_tr_data(bt_share_transfer_data_t *tr_data)
142 {
143         FN_START;
144         ret_if(tr_data == NULL);
145
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);
151         g_free(tr_data);
152         FN_END;
153 }
154
155 static int __bt_share_launch_handler(bt_share_appdata_t *ad, bundle *b,
156                                                 bt_share_launch_mode_t launch_mode)
157 {
158         FN_START;
159         const char *transfer_type = NULL;
160         const char *temp = NULL;
161         char *stop = NULL;
162         unsigned char tranferred;
163         int tr_type = 0;
164         sqlite3 *db = NULL;
165
166         retvm_if(ad == NULL, -1, "Invalid param");
167         retvm_if(b == NULL, -1, "Invalid param");
168
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;
174
175                 elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
176                 elm_win_indicator_opacity_set(ad->win, ELM_WIN_INDICATOR_OPAQUE);
177
178                 transfer_type = bundle_get_val(b, "transfer_type");
179                 retvm_if(!transfer_type, -1, "Invalid transfer type!");
180
181                 if (!strcmp(transfer_type, "outbound"))
182                         tr_type = BT_TR_OUTBOUND;
183                 else
184                         tr_type = BT_TR_INBOUND;
185
186                 /* Open the DB */
187                 db = bt_share_open_db();
188                 retvm_if(!db, -1, "fail to open db!");
189
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"));
196
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);
200
201                 if (launch_mode == BT_LAUNCH_TRANSFER_LIST) {
202                         temp = bundle_get_val(b, "transfer_id");
203                         if (temp != NULL) {
204                                 transfer_data->transfer_id = atoi(temp);
205                                 INFO_SECURE("Transfer ID: %d", transfer_data->transfer_id);
206
207                                 if (tr_type == BT_TR_INBOUND) {
208                                         int noti_id;
209                                         DBG("INBOUND TRANSFER");
210
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.
214                                          */
215                                         noti_id = bt_share_get_noti_id(db, BT_DB_INBOUND, transfer_data->db_sid);
216
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;
224
225                                                 transfer_data->filename = g_strdup(bundle_get_val(b, "filename"));
226                                                 INFO_SECURE("File Name: %s", transfer_data->filename);
227                                         }
228                                 }
229                         }
230                         _bt_set_opc_launched_session(FALSE);
231                 } else {
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");
236                         if (temp != NULL)
237                                 transfer_data->size = atol(temp);
238                         temp = bundle_get_val(b, "transfer_id");
239                         if (temp != NULL) {
240                                 transfer_data->transfer_id = atoi(temp);
241                                 INFO_SECURE("Transfer ID: %d", transfer_data->transfer_id);
242                         }
243
244                         temp = bundle_get_val(b, "progress_cnt");
245                         if (temp != NULL) {
246                                 int current, total;
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;
252                         }
253                         INFO("File Progress: [%d/%d]", transfer_data->current_file,
254                                         transfer_data->total_files);
255
256                         if (tr_type == BT_TR_OUTBOUND) {
257                                 bt_opp_get_transfer_progress(BT_TRANSFER_OUTBOUND,
258                                                 -1, &tranferred);
259                                 _bt_set_opc_launched_session(TRUE);
260                         } else {
261                                 bt_opp_get_transfer_progress(BT_TRANSFER_INBOUND,
262                                                 transfer_data->transfer_id, &tranferred);
263                         }
264                         transfer_data->percentage = tranferred;
265                 }
266
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!");
270
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);
275                         return 0;
276                 }
277
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;
283
284                 ad->tr_type = tr_type;
285
286                 /* Get data from share DB */
287                 if (ad->db_sid) {
288                         g_free(ad->db_sid);
289                         ad->db_sid = NULL;
290                 }
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);
296
297                 bt_share_close_db(db);
298
299                 _bt_create_transfer_view(ad);
300
301         } else {
302                 ERR("Invalid bundle value");
303                 return -1;
304         }
305
306         FN_END;
307         return 0;
308 }
309
310 static bool __app_create(void *data)
311 {
312         FN_START;
313         bt_share_appdata_t *ad = data;
314         Evas_Object *win = NULL;
315         int ret;
316         bt_adapter_state_e status = BT_ADAPTER_DISABLED;
317
318         elm_app_base_scale_set(1.8);
319
320         /* create window */
321         win = _bt_create_win(PACKAGE);
322         retv_if(win == NULL, false);
323         ad->win = win;
324
325         bindtextdomain(BT_COMMON_PKG, BT_COMMON_RES);
326
327         if (bt_initialize() != BT_ERROR_NONE)
328                 ERR("bt_initialize() failed");
329
330         if (bt_adapter_get_state(&status) != BT_ERROR_NONE)
331                 ERR("bt_adapter_get_state() failed!");
332         DBG("bt_status : %d", status);
333
334         ad->bt_status = status;
335
336         /* Set event callbacks */
337         ret =
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");
341         FN_END;
342         return true;
343 }
344
345 static void __app_service(app_control_h app_control, void *user_data)
346 {
347         INFO("__app_service");
348         bt_share_appdata_t *ad = user_data;
349         int ret;
350         bundle *b = NULL;
351         bt_share_launch_mode_t launch_mode;
352         ret_if(ad == NULL);
353
354         ret = app_control_export_as_bundle(app_control, &b);
355
356         if (ad->dbus_conn == NULL)
357                 _bt_signal_init(ad);
358
359         launch_mode = __bt_parse_launch_mode(ad, b);
360         if (launch_mode == BT_LAUNCH_NONE) {
361                 if (b)
362                         bundle_free(b);
363                 _bt_terminate_app();
364                 return;
365         }
366
367         ret = __bt_share_launch_handler(ad, b, launch_mode);
368         if (ret < 0)
369                 _bt_terminate_app();
370
371         evas_object_show(ad->win);
372         elm_win_activate(ad->win);
373         bundle_free(b);
374 }
375
376 static void __app_terminate(void *data)
377 {
378         FN_START;
379         bt_share_appdata_t *ad = data;
380         int err;
381
382         _bt_destroy_info_popup(ad);
383         _bt_signal_deinit(ad);
384
385         if (ad->tr_device_itc) {
386                 elm_genlist_item_class_free(ad->tr_device_itc);
387                 ad->tr_device_itc = NULL;
388         }
389         if (ad->tr_status_itc) {
390                 elm_genlist_item_class_free(ad->tr_status_itc);
391                 ad->tr_status_itc = NULL;
392         }
393         if (ad->tr_progress_itc) {
394                 elm_genlist_item_class_free(ad->tr_progress_itc);
395                 ad->tr_progress_itc = NULL;
396         }
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;
400         }
401         if (ad->tr_data_itc) {
402                 elm_genlist_item_class_free(ad->tr_data_itc);
403                 ad->tr_data_itc = NULL;
404         }
405         if (ad->transfer_info) {
406                 __bt_share_free_tr_data(ad->transfer_info);
407                 ad->transfer_info = NULL;
408         }
409
410         if (ad->idler) {
411                 ecore_idler_del(ad->idler);
412                 ad->idler = NULL;
413         }
414         if (ad->db_sid) {
415                 g_free(ad->db_sid);
416                 ad->db_sid = NULL;
417         }
418         err = bt_adapter_unset_state_changed_cb();
419         if (err != BT_ERROR_NONE)
420                 ERR("unset of state change cb  failed: %d", err);
421
422         err = bt_deinitialize();
423         if (err != BT_ERROR_NONE)
424                 ERR("bt_deinitialize failed: %d", err);
425         FN_END;
426 }
427
428 static void __app_pause(void *data)
429 {
430         INFO("__app_pause ");
431 }
432
433 static void __app_resume(void *data)
434 {
435         INFO("__app_resume");
436 }
437
438 EXPORT int main(int argc, char *argv[])
439 {
440         DBG("Start bluetooth-share-ui main()");
441
442         ui_app_lifecycle_callback_s callback = {0,};
443         app_event_handler_h lang_changed_handler;
444         app_state = &app_data;
445
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;
451
452         ui_app_add_event_handler(&lang_changed_handler, APP_EVENT_LANGUAGE_CHANGED, __bt_lang_changed_cb, &app_data);
453
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);
458
459         DBG("End bluetooth-share-ui main()");
460         return ret;
461 }
462