be55b3273ff4fd32a34026e3ddd8ad57b5e37c1b
[apps/core/preloaded/music-player.git] / src / mp-main.c
1 /*
2  * Copyright 2012         Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "mp-ta.h"
18 #include "music.h"
19 #include "mp-widget.h"
20 #include "mp-util.h"
21 #include "mp-setting-ctrl.h"
22 #include "mp-item.h"
23 #include "mp-player-control.h"
24 #include "mp-play-view.h"
25 #include "mp-http-mgr.h"
26 #include "mp-playlist-mgr.h"
27 #include "mp-ug-launch.h"
28 #include "mp-popup.h"
29
30 #include <signal.h>
31 #include <glib.h>
32 #include <glib-object.h>
33 #include "mp-player-mgr.h"
34 #include "mp-player-debug.h"
35 #include <syspopup_caller.h>
36 #include <power.h>
37 #include "mp-minicontroller.h"
38 #include "mp-app.h"
39 #include "mp-play.h"
40 #include "mp-volume.h"
41 #include "mp-common-defs.h"
42
43
44 #ifdef MP_SOUND_PLAYER
45 #include "sp-view-manager.h"
46 #else
47 #include "mp-library.h"
48 #include "mp-common.h"
49 #include "mp-view-manager.h"
50 #endif
51
52
53
54
55 #ifdef MP_FEATURE_AVRCP_13
56 #include "mp-avrcp.h"
57 #endif
58
59
60
61
62 #ifdef MP_FEATURE_EXIT_ON_BACK
63 #define MP_EXIT_ON_BACK "ExitOnBack"
64 #endif
65
66
67 static bool _mp_main_init(struct appdata *ad);
68 static Eina_Bool _mp_main_win_visibility_change_cb(void *data, int type, void *event);
69 static Eina_Bool _mp_main_win_focus_in_cb(void *data, int type, void *event);
70 static Eina_Bool _mp_main_win_focus_out_cb(void *data, int type, void *event);
71 static Eina_Bool _mp_main_app_init_idler_cb(void *data);
72
73 static void
74 _mp_main_exit_cb(void *data, Evas_Object * obj, void *event_info)
75 {
76         mp_evas_object_del(obj);
77
78         mp_app_exit(data);
79 }
80
81 static void
82 _mp_evas_flush_post(void *data, Evas * e, void *event_info)
83 {
84         struct appdata *ad = (struct appdata *)data;
85         MP_CHECK(ad);
86
87         evas_event_callback_del(e, EVAS_CALLBACK_RENDER_FLUSH_POST, _mp_evas_flush_post);
88
89 }
90
91 static bool
92 _mp_main_init(struct appdata *ad)
93 {
94         EDBusHandle = NULL;
95         ad->request_play_id = MP_SYS_PLST_NONE;
96         ad->music_setting_change_flag = false;
97
98         mp_media_info_connect();
99
100         ad->app_init_idler = ecore_idler_add(_mp_main_app_init_idler_cb, ad);
101
102         return TRUE;
103 }
104
105 static bool
106 _mp_main_set_current_playing_item(struct appdata *ad, const char *current_player_path)
107 {
108         startfunc;
109         ad->loadtype = LOAD_TRACK;
110
111         mp_playlist_mgr_clear(ad->playlist_mgr);
112         mp_plst_item *item = mp_playlist_mgr_item_append(ad->playlist_mgr, current_player_path, NULL, MP_TRACK_URI);
113         mp_playlist_mgr_set_current(ad->playlist_mgr, item);
114
115         ad->launch_type = MP_LAUNCH_BY_PATH;
116
117         return true;
118
119 }
120
121 static bool
122 _mp_main_is_launching_available(struct appdata *ad)
123 {
124         TA_S(2, "mp_check_battery_available");
125         if (mp_check_battery_available())
126         {
127                 Evas_Object *popup = mp_popup_create(ad->win_main, MP_POPUP_NORMAL, NULL, ad, _mp_main_exit_cb, ad);
128                 elm_object_text_set(popup, GET_SYS_STR("IDS_COM_BODY_LOW_BATTERY"));
129                 mp_popup_button_set(popup, MP_POPUP_BTN_1, GET_SYS_STR("IDS_COM_SK_OK"), MP_POPUP_YES);
130                 mp_popup_timeout_set(popup, MP_POPUP_TIMEOUT);
131                 evas_object_show(ad->win_main);
132                 evas_object_show(popup);
133                 return false;
134         }
135         TA_E(2,"mp_check_battery_available");
136
137         TA_S(2, "mp_check_mass_storage_mode");
138         if (mp_check_mass_storage_mode())
139         {
140                 Evas_Object *popup = mp_popup_create(ad->win_main, MP_POPUP_NORMAL, NULL, ad, _mp_main_exit_cb, ad);
141                 elm_object_text_set(popup, GET_SYS_STR("IDS_COM_POP_UNABLE_TO_USE_DURING_MASS_STORAGE_MODE"));
142                 mp_popup_button_set(popup, MP_POPUP_BTN_1, GET_SYS_STR("IDS_COM_SK_OK"), MP_POPUP_YES);
143                 mp_popup_timeout_set(popup, MP_POPUP_TIMEOUT);
144                 evas_object_show(ad->win_main);
145                 evas_object_show(popup);
146                 return false;
147         }
148         TA_E(2,"mp_check_mass_storage_mode");
149         return true;
150 }
151
152 #ifndef MP_SOUND_PLAYER
153 void
154 _mp_main_create_view_by_item(struct appdata *ad, const char *argv1)
155 {
156         mp_retm_if(argv1 == NULL, "argv1 is NULL");
157         mp_retm_if(ad == NULL, "ad is NULL");
158
159         if (argv1)
160         {
161                 if (strstr(argv1, MP_SHORTCUT_PLAYLIST) == argv1)
162                 {
163                         int playlist_id = -1;
164                         char *id;
165                         id = strtok((char *)argv1, MP_FUNC_ADD_TO_HOME_SEPARATION);
166                         id = strtok(NULL, MP_FUNC_ADD_TO_HOME_SEPARATION);
167                         if(id)
168                         {
169                                 playlist_id = atoi(id);
170                                 DEBUG_TRACE("Playlist id is %d", playlist_id);
171                                 ad->launch_type = MP_LAUNCH_ADD_TO_HOME;
172                                 ad->loadtype = LOAD_PLAYLIST;
173                                 ad->request_play_id = playlist_id;
174
175                                 mp_library_load(ad);
176                         }
177                 }
178                 else if (strstr(argv1, MP_SHORTCUT_GROUP) == argv1)
179                 {
180                         char *id;
181                         id = strtok((char *)argv1, MP_FUNC_ADD_TO_HOME_SEPARATION);
182                         id = strtok(NULL, MP_FUNC_ADD_TO_HOME_SEPARATION);
183                         if (id)
184                         {
185                                 ad->request_group_type = (atoi(id));
186                                 mp_debug("view_type is %d", ad->request_group_type);
187
188                                 id = strtok(NULL, MP_FUNC_ADD_TO_HOME_SEPARATION);
189
190                                 if (id)
191                                 {
192                                         IF_FREE(ad->request_group_name);
193                                         ad->request_group_name = g_strdup(id);
194
195                                         DEBUG_TRACE("Reqest Album name is %s", ad->request_group_name);
196                                         ad->launch_type = MP_LAUNCH_ADD_TO_HOME;
197                                         ad->loadtype = LOAD_GROUP;
198                                         mp_library_load(ad);
199                                 }
200                         }
201                 }
202                 else if (strstr(argv1, MP_SHORTCUT_SONG) == argv1)
203                 {
204                         char current_player_path[255] = { 0, };
205                         char temp[255] = { 0, };
206                         strncpy(temp, argv1, 255);
207
208                         int i = 0;
209                         for (i = 0; i < strlen(temp); i++)
210                         {
211                                 if (temp[i] == '/')
212                                 {
213                                         int j = i + 1;
214                                         while (temp[j])
215                                         {
216                                                 current_player_path[j - (i + 1)] = temp[j];
217                                                 j++;
218                                         }
219                                         DEBUG_TRACE("current_player_path is %s", current_player_path);
220                                         break;
221                                 }
222                         }
223
224                         ad->launch_type = MP_LAUNCH_ADD_TO_HOME;
225                         ad->loadtype = LOAD_TRACK;
226
227                         IF_FREE(ad->request_playing_path);
228                         ad->request_playing_path = g_strdup(current_player_path);
229
230                 }
231         }
232 }
233 #endif
234
235 static Eina_Bool _mp_main_win_visibility_change_cb(void *data, int type, void *event)
236 {
237         struct appdata *ad = (struct appdata *)data;
238         mp_retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "ad is null");
239
240         Ecore_X_Event_Window_Visibility_Change* ev = (Ecore_X_Event_Window_Visibility_Change *)event;
241         mp_debug("## Type=[%d], win=[%d], fully_obscured=[%d] ##", type, ev->win, ev->fully_obscured);
242
243         if (ev->win == ad->xwin) {
244                 /* main window */
245                 if (ev->fully_obscured == 1) {
246                         mp_debug("hide main window");
247                         mp_player_mgr_vol_type_unset();
248
249                         ad->app_is_foreground = false;
250                 } else {
251                         mp_debug("show main window");
252                         mp_player_mgr_vol_type_set();
253                         ad->app_is_foreground = true;
254                 }
255         }
256
257         return ECORE_CALLBACK_PASS_ON;
258 }
259
260 static Eina_Bool _mp_main_win_focus_in_cb(void *data, int type, void *event)
261 {
262         startfunc;
263         struct appdata *ad = (struct appdata *)data;
264         mp_retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "ad is null");
265
266
267         Ecore_X_Event_Window_Focus_In *ev = (Ecore_X_Event_Window_Focus_In *)event;
268         if (ev->win == ad->xwin) {
269                 ad->is_focus_out = false;
270                 if (ad->win_minicon && ad->b_minicontroller_show)
271                         mp_minicontroller_destroy(ad);
272         }
273 #ifndef MP_SOUND_PLAYER
274         int db_state = VCONFKEY_FILEMANAGER_DB_UPDATED;
275         vconf_get_int(VCONFKEY_FILEMANAGER_DB_STATUS, &db_state);
276         if(db_state == VCONFKEY_FILEMANAGER_DB_UPDATING)
277         {
278                 DEBUG_TRACE("update list");
279                 mp_view_manager_update_list_contents(mp_util_get_view_data(ad), true);
280         }
281 #endif
282
283
284         if (ad->playing_view && ad->playing_view->layout && !ad->ug) {
285                 mp_volume_key_grab_start(ad->xwin);
286         }
287
288         return ECORE_CALLBACK_PASS_ON;
289 }
290
291 static void _show_minicontroller(struct appdata *ad)
292 {
293         ad->is_focus_out = true;
294         if (ad->player_state == PLAY_STATE_PAUSED || ad->player_state == PLAY_STATE_PLAYING)
295         {
296                 if (!ad->win_minicon)
297                         mp_minicontroller_create(ad);
298                 else
299                         mp_minicontroller_show(ad);
300         }
301 }
302
303 static Eina_Bool _mp_main_win_focus_out_cb(void *data, int type, void *event)
304 {
305         startfunc;
306         struct appdata *ad = (struct appdata *)data;
307         mp_retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "ad is null");
308
309
310         Ecore_X_Event_Window_Focus_Out *ev = (Ecore_X_Event_Window_Focus_Out *)event;
311         if (ev->win == ad->xwin) {
312                 /* Testing Code. If a track is getting played or paused,
313                    the MiniController should be displayed as soon as the main window goes to back ground.
314                    When again the Music ICon in Main menu is pressed, the mini controller will be hidden and
315                    The Main Screen of the Music Application will be displayed. */
316                 _show_minicontroller(ad);
317         }
318
319
320         mp_volume_key_grab_end();
321
322         return ECORE_CALLBACK_PASS_ON;
323 }
324
325 static Eina_Bool _mp_main_client_message_cb(void *data, int type, void *event)
326 {
327         struct appdata *ad = data;
328         MP_CHECK_FALSE(ad);
329
330         Ecore_X_Event_Client_Message *ev =
331             (Ecore_X_Event_Client_Message *) event;
332         int new_angle = 0;
333
334         if (ev->message_type == ECORE_X_ATOM_E_ILLUME_ROTATE_ROOT_ANGLE)
335         {
336                 new_angle = ev->data.l[0];
337                 DEBUG_TRACE("ROTATION: %d", new_angle);
338                 mp_minicontroller_rotate(ad, new_angle);
339         }
340         else if (ev->message_type == ECORE_X_ATOM_E_ILLUME_QUICKPANEL_STATE)
341         {
342                 bool visible = false;
343                 if (ev->data.l[0] == ECORE_X_ATOM_E_ILLUME_QUICKPANEL_ON)
344                 {
345                         mp_debug("quickpanel show");
346                         visible = true;
347                 }
348                 else
349                 {
350                         mp_debug("quickpanel hide");
351                         visible = false;
352                 }
353                 mp_minicontroller_visible_set(ad, visible);
354         }
355
356         return ECORE_CALLBACK_PASS_ON;
357 }
358
359 static void __mp_main_lcd_state_changed_cb(power_state_e state, void *user_data)
360 {
361         DEBUG_TRACE("power_state: %d", state);
362
363         struct appdata *ad = user_data;
364         MP_CHECK(ad);
365
366         if(state == POWER_STATE_SCREEN_OFF){
367                 ad->is_lcd_off = true;
368 #ifdef MP_SOUND_PLAYER
369                 mp_play_view_progress_timer_freeze(ad);
370 #else
371                 mp_view_manager_freeze_progress_timer(ad);
372 #endif
373         }else if(state == POWER_STATE_NORMAL){
374                 ad->is_lcd_off = false;
375                 /* for refresh progressbar */
376                 ad->music_pos = mp_player_mgr_get_position() / 1000.0;
377                 ad->music_length = mp_player_mgr_get_duration() / 1000.0;
378 #ifdef MP_SOUND_PLAYER
379                 mp_play_view_progress_timer_thaw(ad);
380 #else
381                 mp_view_manager_thaw_progress_timer(ad);
382 #endif
383         }
384 }
385
386
387 Eina_Bool __mp_main_app_prop_change(void *data, int ev_type, void *ev)
388 {
389         startfunc;
390         Ecore_X_Event_Window_Property *event = ev;
391
392         if (event->win != ecore_x_window_root_first_get())
393                 return ECORE_CALLBACK_PASS_ON;
394
395         if (event->atom != ecore_x_atom_get("FONT_TYPE_change"))
396                 return ECORE_CALLBACK_PASS_ON;
397
398         DEBUG_TRACE("Font is changed!(FONT_TYPE_change)\n");
399
400         //We have a config(font) changed property. Here you can apply to new fonts
401
402         return ECORE_CALLBACK_PASS_ON;
403 }
404
405 static Eina_Bool
406 _mp_main_app_init_idler_cb(void *data)
407 {
408         startfunc;
409         struct appdata *ad = data;
410         MP_CHECK_FALSE(ad);
411
412         if (!mp_app_noti_init(ad))
413         {
414                 ERROR_TRACE("Error when noti init");
415         }
416
417         ad->key_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, mp_app_key_down_cb, ad);
418         ad->key_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, mp_app_key_up_cb, ad);
419         ad->mouse_button_down = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, mp_app_mouse_event_cb, ad);
420         ad->mouse_button_up = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_UP, mp_app_mouse_event_cb, ad);
421         ad->mouse_move = ecore_event_handler_add(ECORE_EVENT_MOUSE_MOVE, mp_app_mouse_event_cb, ad);
422         /* window visibility change event */
423         ad->visibility_change = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE, _mp_main_win_visibility_change_cb, ad);
424         /* window focus in/out event */
425         ad->focus_in = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_FOCUS_IN, _mp_main_win_focus_in_cb, ad);
426         ad->focus_out = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_FOCUS_OUT, _mp_main_win_focus_out_cb, ad);
427         ad->client_msg = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, _mp_main_client_message_cb, ad);
428         ad->property = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, __mp_main_app_prop_change, ad);
429
430 #ifdef MP_ENABLE_INOTIFY
431         mp_app_inotify_init(ad);
432 #endif
433
434         ad->app_init_idler = NULL;
435
436
437 #ifdef MP_FEATURE_AUTO_OFF
438         mp_setting_auto_off_set_callback(mp_app_auto_off_changed_cb, ad);
439 #endif
440
441 #ifdef MP_FEATURE_PLAY_SPEED
442         mp_setting_reset_play_speed();
443         mp_setting_set_play_speed_change_callback(mp_app_play_speed_changed_cb, ad);
444 #endif
445
446         power_set_changed_cb(__mp_main_lcd_state_changed_cb, ad);
447
448
449
450
451         return ECORE_CALLBACK_CANCEL;
452 }
453
454 static bool __mp_main_service_extra_data_cb(service_h service, const char *key, void *user_data)
455 {
456         MP_CHECK_FALSE(service);
457         char * value = NULL;
458         service_get_extra_data(service, key, &value);
459         DEBUG_TRACE("key: %s, value: %s", key, value);
460         IF_FREE(value);
461
462         return true;
463 }
464
465
466 #ifdef MP_FEATURE_EXIT_ON_BACK
467 static Eina_Bool
468 _mp_main_caller_win_destroy_cb(void *data, int type, void *event)
469 {
470         startfunc;
471         Ecore_X_Event_Window_Hide *ev;
472         struct appdata *ad = data;
473         MP_CHECK_VAL(ad, ECORE_CALLBACK_RENEW);
474
475         ev = event;
476         if (ev == NULL) {
477                 DEBUG_TRACE("ev is NULL");
478                 return ECORE_CALLBACK_RENEW;
479         }
480         //DEBUG_TRACE("win: %d, caller_win: %d", ev->win ,ad->caller_win_id);
481         if(ev->win == ad->caller_win_id) {
482                 elm_exit();
483         }
484
485         return ECORE_CALLBACK_RENEW;
486 }
487 #endif
488
489
490 static int
491 _mp_main_parse_service(struct appdata *ad, service_h service, char **request_title, char **path, int *lunching_by_menu_icon, bool *activate_window)
492 {
493         int ret = 0;
494         MP_CHECK_VAL(service, -1);
495
496         service_foreach_extra_data(service, __mp_main_service_extra_data_cb, NULL);
497         char *value = NULL;
498 #ifdef MP_SOUND_PLAYER
499         if (!service_get_extra_data(service, MP_REQ_TYPE, &value))
500         {
501                 DEBUG_TRACE("launch by S Voice app. req tyep: %s", value);
502         }
503         else if(mp_util_get_uri_from_app_svc(service, ad, path))
504         {
505                 DEBUG_TRACE("uri: %s", *path);
506         }
507         else
508                 ERROR_TRACE("No uri...");
509
510 #ifdef MP_FEATURE_EXIT_ON_BACK
511         if(ad->caller_win_id)
512         {
513                 DEBUG_TRACE("unset transient for win: 0x%x", ad->caller_win_id);
514                 ecore_x_icccm_transient_for_unset(elm_win_xwindow_get(ad->win_main));
515                 ecore_event_handler_del(ad->callerWinEventHandler);
516                 ad->caller_win_id = 0;
517                 ad->callerWinEventHandler = NULL;
518         }
519
520         unsigned int id = 0;
521         service_get_window(service, &id);
522         service_get_extra_data(service, MP_EXIT_ON_BACK, &value);
523
524         DEBUG_TRACE("Caller window id: 0x%x, ExitOnBack: %s", id, value);
525         if(id && value && !strcasecmp(value, "true"))
526         {
527                 ecore_x_icccm_transient_for_set(elm_win_xwindow_get(ad->win_main), id);
528                 ecore_x_window_client_manage(id);
529
530                 ad->callerWinEventHandler =
531                         ecore_event_handler_add(ECORE_X_EVENT_WINDOW_DESTROY,
532                                                 _mp_main_caller_win_destroy_cb, ad);
533                 ad->caller_win_id = id;
534         }
535 #endif
536
537 #else
538         char *uri = NULL;
539
540         *activate_window = true;
541
542         if(!service_get_extra_data(service, "shortcut", &uri))
543         {
544                 if (uri)
545                 {
546                         mp_debug("uri = %s", uri);
547                         *lunching_by_menu_icon = 1;
548                         *path = uri;
549                 }
550         }
551         else if (!service_get_extra_data(service, MP_REQ_TYPE, &value))
552         {
553                 IF_FREE(value);
554         }
555
556         else
557         {
558                 if(!service_get_extra_data(service, MP_MM_KEY, &value))
559                 {
560                         DEBUG_TRACE("mm key event, ad->player_state : %d", ad->player_state);
561                         *activate_window = false;
562                         if(ad->player_state == PLAY_STATE_PAUSED)
563                         {
564                                 ad->launch_type = MP_LAUNCH_DEFAULT;
565                                 ad->loadtype = LOAD_DEFAULT;
566                                 mp_play_control_play_pause(ad, true);
567                         }
568                         else
569                         {
570                                 ad->launch_type = MP_LAUNCH_PLAY_RECENT;
571                                 ad->loadtype = LOAD_MM_KEY;
572
573                                 int count;
574                                 mp_media_list_h media = NULL;
575
576                                 *activate_window = false;
577                                 mp_media_info_list_count(MP_TRACK_BY_PLAYED_TIME, NULL, NULL, NULL, 0, &count);
578                                 if(!count)
579                                 {
580                                         mp_media_info_list_count(MP_TRACK_ALL, NULL, NULL, NULL, 0, &count);
581                                         mp_media_info_list_create(&media, MP_TRACK_ALL, NULL, NULL, NULL, 0, 0, count);
582                                 }
583                                 else
584                                         mp_media_info_list_create(&media, MP_TRACK_BY_PLAYED_TIME, NULL, NULL, NULL, 0, 0, count);
585
586                                 mp_util_append_media_list_item_to_playlist(ad->playlist_mgr, media, count, 0, NULL);
587                                 mp_media_info_list_destroy(media);
588                         }
589                         IF_FREE(value);
590                 }
591         }
592
593 #endif
594         return ret;
595 }
596
597
598 static void
599 _mp_atexit_cb(void)
600 {
601         ERROR_TRACE("%%%%%%%%%%%%%%%%%%%%%");
602         ERROR_TRACE("#exit() invoked. music-player is exiting");
603         ERROR_TRACE("%%%%%%%%%%%%%%%%%%%%%");
604 }
605
606 static void
607 _mp_playlist_item_change_callback(mp_plst_item *item, void *userdata)
608 {
609         struct appdata *ad = userdata;
610         MP_CHECK(ad);
611
612         if(ad->current_track_info)
613         {
614                 mp_util_free_track_info(ad->current_track_info);
615                 ad->current_track_info = NULL;
616         }
617
618         if(item)
619         {
620                 mp_util_load_track_info(ad, item, &ad->current_track_info);
621 #ifdef MP_FEATURE_AVRCP_13
622                 mp_avrcp_noti_track(ad->current_track_info->title,
623                         ad->current_track_info->artist, ad->current_track_info->album,
624                         ad->current_track_info->genre, ad->current_track_info->duration);
625 #endif
626         }
627
628 }
629
630 /**< Called before main loop */
631 static bool
632 mp_create(void *data)
633 {
634         struct appdata *ad = data;
635
636         atexit(_mp_atexit_cb);
637
638         MP_CHECK_VAL(ad, EINA_FALSE);
639
640         TA_S(0, "mp_create");
641
642         TA_S(1, "elm_theme_extension_add");
643         /* do extension add before add elm object.*/
644         elm_theme_extension_add(NULL, THEME_NAME);
645         TA_E(1,"elm_theme_extension_add");
646
647         TA_S(1, "bindtextdomain");
648         bindtextdomain(DOMAIN_NAME, LOCALE_DIR);
649         DEBUG_TRACE("DOMAIN_NAME: %s, LOCALE_DIR: %s", DOMAIN_NAME, LOCALE_DIR);
650         TA_E(1,"bindtextdomain");
651
652         TA_S(1, "mp_create_win");
653         ad->win_main = mp_create_win(GET_SYS_STR("IDS_COM_BODY_MUSIC"));
654         mp_retv_if(ad->win_main == NULL, EINA_FALSE);
655         elm_win_indicator_mode_set(ad->win_main, ELM_WIN_INDICATOR_SHOW);
656         evas_event_callback_add(evas_object_evas_get(ad->win_main), EVAS_CALLBACK_RENDER_FLUSH_POST,
657                                 _mp_evas_flush_post, ad);
658         ad->xwin = elm_win_xwindow_get(ad->win_main);
659         TA_E(1,"mp_create_win");
660
661         TA_S(1, "mp_widget_create_bgimg");
662         ad->bgimage = mp_widget_create_bgimg(ad->win_main);
663         ad->evas = evas_object_evas_get(ad->win_main);
664         TA_E(1,"mp_widget_create_bgimg");
665
666         TA_S(1, "mp_player_mgr_session_init");
667         /*initialize session type */
668         if (!mp_player_mgr_session_init())
669         {
670                 ERROR_TRACE("Error when set session");
671                 return EINA_FALSE;
672         }
673         TA_E(1,"mp_player_mgr_session_init");
674
675         TA_S(1, "mp_setting_init");
676         mp_setting_init(ad);
677         TA_E(1,"mp_setting_init");
678
679         TA_S(1, "_mp_main_init");
680         if (!_mp_main_init(ad))
681         {
682                 ERROR_TRACE("Fail when init music");
683                 return EINA_FALSE;
684         }
685         TA_E(1,"_mp_main_init");
686
687
688 #ifdef ENABLE_RICHINFO
689         xmlInitParser();
690 #endif
691
692         TA_S(1, "mp_http_mgr_create");
693         mp_http_mgr_create(ad);
694         TA_E(1,"mp_http_mgr_create");
695
696
697         TA_S(1, "mp_playlist_mgr_create");
698         ad->playlist_mgr = mp_playlist_mgr_create();
699         mp_playlist_mgr_set_item_change_callback(ad->playlist_mgr, _mp_playlist_item_change_callback, ad);
700         int val = 0;
701         mp_setting_get_shuffle_state(&val);
702         mp_playlist_mgr_set_shuffle(ad->playlist_mgr, val);
703 #ifdef MP_SOUND_PLAYER
704         val = MP_SETTING_REP_NON;
705 #else
706         mp_setting_get_repeat_state(&val);
707 #endif
708         mp_playlist_mgr_set_repeat(ad->playlist_mgr, val);
709         TA_E(1,"mp_playlist_mgr_create");
710
711         TA_S(1, "mp_language_mgr_create");
712         mp_language_mgr_create();
713         TA_E(1,"mp_language_mgr_create");
714
715         TA_S(1, "mp_conformant_add");
716         Evas_Object *conformant = elm_conformant_add(ad->win_main);
717         MP_CHECK_FALSE(conformant);
718         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
719         evas_object_show(conformant);
720         elm_win_resize_object_add(ad->win_main, conformant);
721         ad->conformant = conformant;
722         TA_E(1,"mp_conformant_add");
723
724         TA_E(0, "mp_create");
725         return EINA_TRUE;
726 }
727
728 /**< Called after main loop */
729 static void
730 mp_terminate(void *data)
731 {
732         struct appdata *ad = data;
733         DEBUG_TRACE_FUNC();
734         MP_CHECK(ad);
735
736         mp_language_mgr_destroy();
737         mp_ecore_idler_del(ad->mss_startup_idler);
738         mp_ecore_idler_del(ad->bt_pause_idler);
739         mp_ecore_idler_del(ad->app_init_idler);
740         mp_ecore_idler_del(ad->popup_del_idler);
741         mp_ecore_timer_del(ad->longpress_timer);
742 #ifdef MP_FEATURE_AUTO_OFF
743         mp_ecore_timer_del(ad->auto_off_timer);
744         mp_ecore_timer_del(ad->pause_off_timer);
745 #endif
746
747         mp_app_ungrab_mm_keys(ad);
748
749         if (ad->key_down)
750                 ecore_event_handler_del(ad->key_down);
751         if (ad->key_up)
752                 ecore_event_handler_del(ad->key_up);
753         if (ad->mouse_button_down)
754                 ecore_event_handler_del(ad->mouse_button_down);
755         if (ad->visibility_change) {
756                 ecore_event_handler_del(ad->visibility_change);
757                 ad->visibility_change = NULL;
758         }
759         if (ad->focus_in) {
760                 ecore_event_handler_del(ad->focus_in);
761                 ad->focus_in = NULL;
762         }
763         if (ad->focus_out) {
764                 ecore_event_handler_del(ad->focus_out);
765                 ad->focus_out = NULL;
766         }
767
768         if (ad->player_state != PLAY_STATE_NONE)
769         {
770                 mp_player_mgr_stop(ad);
771                 mp_player_mgr_destroy(ad);
772         }
773
774         if (!mp_util_is_other_player_playing())
775         {
776                 vconf_set_int(VCONFKEY_MUSIC_STATE, VCONFKEY_MUSIC_OFF);
777         }
778         mp_minicontroller_destroy(ad);
779         mp_player_mgr_vol_type_unset();
780         if (!mp_player_mgr_session_finish())
781                 ERROR_TRACE("Error when set session");
782 #ifdef MP_ENABLE_INOTIFY
783         mp_app_inotify_finalize(ad);
784 #endif
785         mp_http_mgr_destory(ad);
786
787         mp_media_info_disconnect();
788
789 #ifdef ENABLE_RICHINFO
790         xmlCleanupParser();
791 #endif
792         mp_setting_deinit(ad);
793         if (!mp_app_noti_ignore())
794                 ERROR_TRACE("Error when ignore noti");
795
796
797 #ifdef MP_SOUND_PLAYER
798         sp_view_mgr_destroy(ad->view_mgr);
799         ad->view_mgr = NULL;
800 #endif
801
802
803         MP_TA_ACUM_ITEM_SHOW_RESULT_TO(MP_TA_SHOW_FILE);
804         MP_TA_RELEASE();
805
806         mp_lyric_mgr_destory(ad);
807
808
809
810
811
812         return;
813 }
814
815 /**< Called when every window goes back */
816 static void
817 mp_pause(void *data)
818 {
819         DEBUG_TRACE_FUNC();
820
821         return;
822 }
823
824 /**< Called when any window comes on top */
825 static void
826 mp_resume(void *data)
827 {
828         DEBUG_TRACE_FUNC();
829
830         return;
831 }
832
833 /**< Called at the first idler and relaunched by AUL*/
834 static void
835 mp_service(service_h service, void *data)
836 {
837         startfunc;
838
839         struct appdata *ad = data;
840         mp_ret_if(ad == NULL);
841
842         if(ad->app_is_foreground)
843         {
844                 DEBUG_TRACE("relaunch is not allowed");
845                 elm_win_activate(ad->win_main);
846                 return;
847         }
848         else
849                 ad->app_is_foreground = true;
850
851         TA_S(0, "mp_service");
852
853         TA_S(1, "_mp_main_is_launching_available");
854         if (!_mp_main_is_launching_available(ad))
855                 return;
856         TA_E(1,"_mp_main_is_launching_available");
857
858         char *request_title = NULL;
859         int lunching_by_menu_icon = 0;
860         char *path = NULL;
861         bool activate_window = true;
862
863         ad->launch_type = MP_LAUNCH_DEFAULT;
864         ad->loadtype = LOAD_DEFAULT;
865
866         TA_S(1, "_mp_main_parse_service");
867         if(_mp_main_parse_service(ad, service, &request_title, &path, &lunching_by_menu_icon, &activate_window))
868         {
869                 ERROR_TRACE("Error: _mp_main_parse_service");
870                 elm_exit();
871                 return;
872         }
873         TA_E(1,"_mp_main_parse_service");
874
875 #ifdef MP_SOUND_PLAYER
876         if (path) {
877                 _mp_main_set_current_playing_item(ad, path);
878                 free(path);
879         }
880         else {
881                 mp_error("no path");
882                 mp_app_exit(ad);
883                 return;
884         }
885
886         IF_FREE(ad->latest_playing_key_id);
887         if (!ad->base_layout_main) {
888
889                 ad->base_layout_main = mp_widget_create_layout_main(ad->conformant);
890                 elm_object_content_set(ad->conformant, ad->base_layout_main);
891
892                 ad->loadtype = LOAD_TRACK;
893                 /* create base navi bar */
894                 Evas_Object *navibar = mp_widget_navigation_new(ad->base_layout_main, ad);
895                 elm_object_part_content_set(ad->base_layout_main, "elm.swallow.content", navibar);
896
897                 ad->view_mgr = sp_view_mgr_create(navibar);
898                 ad->naviframe = navibar;
899         }
900
901         mp_play_destory(ad);
902         mp_play_view_load(ad);
903         ad->paused_by_user = FALSE;
904
905         mp_play_new_file(data, TRUE);
906 #else
907
908         if (path)
909         {
910                 if (lunching_by_menu_icon)
911                         _mp_main_create_view_by_item(ad, path);
912                 else
913                 {
914                         _mp_main_set_current_playing_item(ad, path);
915                         IF_FREE(ad->request_playing_path);
916                         ad->request_playing_path = g_strdup(path);
917                 }
918                 free(path);
919         }
920
921         DEBUG_TRACE("ad->launch_type:%d, ad->loadtype:%d", ad->launch_type, ad->loadtype);
922
923         if (ad->launch_type != MP_LAUNCH_DEFAULT)
924         {
925                 //destory popup
926                 mp_popup_destroy(ad);
927
928                 //destroy info view..
929                 mp_view_manager_pop_info_view(ad);
930
931                 //destroy ug.
932                 mp_ug_destory_all(ad);
933
934         }
935
936         TA_S(1, "mp_library_create");
937         if (ad->base_layout_main == NULL)
938                 mp_library_create(ad);
939         TA_E(1, "mp_library_create");
940
941         if (ad->launch_type != MP_LAUNCH_DEFAULT &&
942                 (ad->loadtype == LOAD_TRACK))
943         {
944                 IF_FREE(ad->latest_playing_key_id);
945                 ad->load_play_view = true;
946
947                 ad->b_add_tracks = 0;
948                 elm_toolbar_item_selected_set(ad->library->ctltab_songs, EINA_TRUE);
949                 Evas_Object *layout = mp_view_manager_get_last_view_layout(ad);
950                 mp_view_layout_set_edit_mode(mp_util_get_layout_data(layout), false);
951
952                 if(ad->loadtype == LOAD_TRACK)
953                 {
954                         mp_play_view_load_by_path(ad, ad->request_playing_path);
955                 }
956                 else
957                 {
958                         mp_play_view_load_and_play(ad, NULL, FALSE);
959                 }
960
961                 IF_FREE(ad->request_playing_path);
962
963                 ad->paused_by_user = FALSE;
964
965         }
966         else if(ad->loadtype == LOAD_MM_KEY)
967         {
968                 ad->launch_type = MP_LAUNCH_DEFAULT;
969                 ad->loadtype= LOAD_DEFAULT;
970                 mp_play_new_file(ad, TRUE);
971                 _show_minicontroller(ad);
972         }
973 #endif
974
975
976         TA_S(1, "evas_object_show");
977         evas_object_show(ad->win_main);
978         TA_E(1,"evas_object_show");
979
980         if(activate_window)
981         {
982                 DEBUG_TRACE("activate window");
983                 TA_S(1, "elm_win_activate");
984                 elm_win_activate(ad->win_main);
985                 TA_E(1,"elm_win_activate");
986         }
987         else
988         {
989                 evas_object_hide(ad->win_main);
990                 ad->app_is_foreground = false;
991                 elm_win_iconified_set(ad->win_main, EINA_TRUE);
992         }
993
994         TA_S(1, "mp_player_mgr_vol_type_set");
995         mp_player_mgr_vol_type_set();
996         TA_E(1,"mp_player_mgr_vol_type_set");
997
998         TA_E(0,"MP-LAUNCH_TIME");
999         TA_E(0,"mp_service");
1000
1001         endfunc;
1002
1003         return;
1004 }
1005
1006 static void
1007 mp_low_battery(void *data)
1008 {
1009         struct appdata *ad = (struct appdata *)data;
1010         MP_CHECK(ad);
1011
1012         if (mp_check_battery_available())
1013         {
1014                 mp_app_exit(ad);
1015         }
1016         return;
1017 }
1018
1019 void
1020 mp_device_orientation_cb(app_device_orientation_e orientation, void *user_data)
1021 {
1022         struct appdata *ad = user_data;
1023         MP_CHECK(ad);
1024
1025 }
1026
1027 static void
1028 __mp_language_changed_cb(void *user_data)
1029 {
1030         DEBUG_TRACE("");
1031         struct appdata *ad = user_data;
1032         mp_popup_destroy(ad);
1033 #ifndef MP_SOUND_PLAYER
1034         mp_view_manager_pop_info_view(ad);
1035 #endif
1036         mp_ug_destory_all(ad);
1037         mp_language_mgr_update();
1038 }
1039
1040 int
1041 main(int argc, char *argv[])
1042 {
1043         struct appdata ad;
1044         app_event_callback_s event_callbacks;
1045
1046         event_callbacks.create = mp_create;
1047         event_callbacks.terminate = mp_terminate;
1048         event_callbacks.pause = mp_pause;
1049         event_callbacks.resume = mp_resume;
1050         event_callbacks.service = mp_service;
1051         event_callbacks.low_memory = NULL;
1052         event_callbacks.low_battery = mp_low_battery;
1053         event_callbacks.device_orientation = mp_device_orientation_cb;
1054         event_callbacks.language_changed = __mp_language_changed_cb;
1055         event_callbacks.region_format_changed = NULL;
1056
1057         DEBUG_TRACE(" starting music main");
1058
1059         MP_TA_INIT();
1060         TA_S(0, "MP-LAUNCH_TIME");
1061         memset(&ad, 0x0, sizeof(struct appdata));
1062         return app_efl_main(&argc, &argv, &event_callbacks, &ad);
1063 }