2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <Elementary.h>
20 #include "AppCommon.h"
21 #include "InputHandler.h"
27 #include "song_info.h"
28 #include "album_info.h"
29 #include "music-controller.h"
30 #include "LayoutMgr.h"
32 #include "common-ui.h"
34 #include "ExtBaseLayout.h"
36 #include "song-layout.h"
37 #include "base-view.h"
46 struct SSongItemInfo {
48 Elm_Object_Item *item;
54 Evas_Object *songlist;
55 Elm_Object_Item *focused_item;
57 Eina_List *it_infolist;
58 Elm_Genlist_Item_Class *item_class;
59 CMusicController *mhandle;
62 SContentInfo *ctxtinfo;
66 memset(this, 0, sizeof(SSongLayout));
74 char *CSongLayout::sm_CbGetListItemText(void *data, Evas_Object *obj, const char *part)
76 SSongItemInfo *itinfo = (SSongItemInfo *)data;
77 CSongInfo *sinfo = NULL;
86 sinfo = itinfo->sinfo;
88 snprintf(buf, sizeof(buf), "%s", MUSIC_STR_EMPTY);
90 if (!strcmp(part, "elm.text")) {
91 index = elm_genlist_item_index_get(itinfo->item);
92 if ((index & 1) == 1) {
93 elm_object_item_signal_emit(itinfo->item,
94 MUSIC_SIGNAL_EVEN_ROW, MUSIC_BASE_VIEW);
97 elm_object_item_signal_emit(itinfo->item,
98 MUSIC_SIGNAL_ODD_ROW, MUSIC_BASE_VIEW);
100 snprintf(buf, sizeof(buf), " %s", sinfo->Name());
102 else if (!strcmp(part, "elm.text1"))
103 snprintf(buf, sizeof(buf), "%s", sinfo->Artist());
104 else if (!strcmp(part, "elm.text2"))
105 snprintf(buf, sizeof(buf), "%s", sinfo->Album());
106 else if (!strcmp(part, "elm.text3")) {
107 duration = sinfo->Duration();
108 timestr = _get_timestr_from_milseconds(duration);
110 snprintf(buf, sizeof(buf), "%s", timestr);
115 if (strcmp(buf, MUSIC_STR_EMPTY))
122 Evas_Object *CSongLayout::sm_CbGetListItemContent(void *data, Evas_Object *obj, const char *part)
124 SSongItemInfo *itinfo = (SSongItemInfo *)data;
125 CSongInfo *sinfo = NULL;
126 Evas_Object *img = NULL;
128 char buf[MAX_LENGTH];
133 sinfo = itinfo->sinfo;
135 img = elm_image_add(obj);
138 elm_image_aspect_fixed_set(img, EINA_FALSE);
140 if (!strcmp(part, "elm.swallow.icon")) {
141 path = sinfo->ThumbnailPath();
143 elm_image_file_set(img, path, "NULL");
145 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
146 MUSIC_IMAGE_DEFAULT_THUMB);
147 elm_image_file_set(img, buf, "NULL");
155 void CSongLayout::sm_CbRemoveListItem(void *data, Evas_Object *obj)
161 void CSongLayout::sm_CbCtxtUpdate(void *dt, enum EActionType type, int lid)
163 CSongLayout *root = (CSongLayout *)dt;
166 root->m_OnCtxtUpdate(type, lid);
170 void CSongLayout::m_OnCtxtUpdate(EActionType type, int lid)
172 Eina_List *list = NULL;
175 if (!m->ctxtinfo || !m->ctxtinfo->context)
178 if (type == ACTION_TYPE_ADDNEXT)
179 mode = ADD_TYPE_NEXT;
183 list = eina_list_append(list, m->ctxtinfo->context);
184 if (type == ACTION_TYPE_ADDTO) {
185 if (!m->mhandle->MediaAddsongsPlaylist(lid, list))
186 _ERR(" Adding songs to playlist failed ");
188 _create_message_box(m->base, MUSIC_TEXT_ADDTO_MSG);
193 m->mhandle->UpdatePlaylist(list, mode);
194 eina_list_free(list);
196 if (type == ACTION_TYPE_PLAY) {
198 m->mhandle->SetCurrentSong(((CSongInfo *)m->ctxtinfo->context)->Id());
201 _update_playback_view((EAddType)type, Layout(), m->focused_item);
205 void CSongLayout::sm_CbCtxtClose(void *dt)
207 CSongLayout *root = (CSongLayout *)dt;
210 root->m_OnCtxtClose();
214 void CSongLayout::m_OnCtxtClose(void)
217 m->vmgr->PushView(MUSIC_BASE_VIEW, NULL);
218 elm_object_item_focus_set(m->focused_item, EINA_TRUE);
222 void CSongLayout::sm_CbItemSelect(void *data, Evas_Object *obj, void *event_info)
224 CSongLayout *root = (CSongLayout*)data;
227 root->m_OnItemSelect(obj, event_info);
230 void CSongLayout::m_OnItemSelect(Evas_Object *obj, void *event_info)
232 SSongItemInfo *itinfo = NULL;
233 char *mediaId = NULL;
235 itinfo = m_FindItemInfoFromItem(m->it_infolist, (Elm_Object_Item *)event_info);
237 _ERR(" no item info found ");
241 mediaId = itinfo->sinfo->Id();
244 m->mhandle->UpdatePlaylist(m->slist, ADD_TYPE_FRESH);
245 m->mhandle->SetCurrentSong(mediaId);
251 int CSongLayout::sm_CbSortArtistAZ(const void *d1, const void *d2)
253 CSongInfo *sinfo1 = (CSongInfo *)d1;
254 CSongInfo *sinfo2 = (CSongInfo *)d2;
258 txt1 = sinfo1->Artist();
262 txt2 = sinfo2->Artist();
266 return strcasecmp(txt1, txt2);
270 int CSongLayout::sm_CbSortArtistZA(const void *d1, const void *d2)
272 CSongInfo *sinfo1 = (CSongInfo *)d1;
273 CSongInfo *sinfo2 = (CSongInfo *)d2;
277 txt1 = sinfo1->Artist();
281 txt2 = sinfo2->Artist();
285 return strcasecmp(txt2, txt1);
289 int CSongLayout::sm_CbSortAlbumAZ(const void *d1, const void *d2)
291 CSongInfo *sinfo1 = (CSongInfo *)d1;
292 CSongInfo *sinfo2 = (CSongInfo *)d2;
296 txt1 = sinfo1->Album();
300 txt2 = sinfo2->Album();
304 return strcasecmp(txt1, txt2);
308 int CSongLayout::sm_CbSortAlbumZA(const void *d1, const void *d2)
310 CSongInfo *sinfo1 = (CSongInfo *)d1;
311 CSongInfo *sinfo2 = (CSongInfo *)d2;
315 txt1 = sinfo1->Album();
319 txt2 = sinfo2->Album();
323 return strcasecmp(txt2, txt1);
327 SSongItemInfo *CSongLayout::m_FindItemInfoFromItem(Eina_List *list, Elm_Object_Item *item)
330 SSongItemInfo *itinfo = NULL;
333 EINA_LIST_FOREACH(list, l, obj) {
334 itinfo = (SSongItemInfo *)obj;
335 if (itinfo->item == item)
343 void CSongLayout::m_UpdateSongList(bool sort_flag)
349 struct SSongItemInfo *itinfo;
350 Elm_Object_Item *item;
352 /* Remove existing songlist and prepare afresh */
353 m_EmptySongList(sort_flag);
356 r = m->mhandle->MediaGetList(LIST_TYPE_SONG, NULL, &(m->slist));
357 if (r == false || eina_list_count(m->slist) == 0) {
358 _ERR(" Fetching song list from media failed ");
360 SetEmptyStatus(true);
367 EINA_LIST_FOREACH(m->slist, l, obj) {
368 sinfo = (CSongInfo *)obj;
369 itinfo = (SSongItemInfo *)calloc(1, sizeof(*itinfo));
373 itinfo->sinfo = sinfo;
374 item = elm_genlist_item_append(m->songlist, m->item_class,
375 itinfo, NULL, ELM_GENLIST_ITEM_NONE,
376 sm_CbItemSelect, this);
378 m->it_infolist = eina_list_append(m->it_infolist, itinfo);
382 void CSongLayout::m_SortSongList(void)
386 const char *sortFuncId[] = {
395 sortType = CInfo::SortType();
397 m->slist = CSort::Sort(m->slist, sortFuncId[sortType]);
400 void CSongLayout::m_EmptySongList(bool sort_flag)
403 elm_genlist_clear(m->songlist);
407 eina_list_free(m->slist);
411 eina_list_free(m->it_infolist);
412 m->it_infolist = NULL;
416 void CSongLayout::m_GotoPlayback(void)
418 if (!m->vmgr->PushView(MUSIC_PLAYBACK_VIEW, NULL))
419 _ERR(" viewmgr push view MUSIC_PLAYBACK_VIEW failed ");
423 void CSongLayout::m_GotoPlaybackUri(void)
425 Eina_List *list = NULL;
426 CSongInfo *sinfo = m->mhandle->MediaSongByUri(m->uri);
428 _ERR(" Fetching song list from uri failed ");
432 list = eina_list_append(list, sinfo);
436 m->mhandle->UpdatePlaylist(list, ADD_TYPE_FRESH);
438 eina_list_free(list);
444 void CSongLayout::m_CreateSongList(void)
446 Evas_Object *genlist = NULL;
447 Elm_Genlist_Item_Class *list_item = NULL;
449 genlist = elm_genlist_add(Layout());
453 elm_genlist_homogeneous_set(genlist, EINA_TRUE);
454 evas_object_size_hint_weight_set(genlist,
455 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
456 elm_object_part_content_set(Layout(), MUSIC_PART_SONGLIST, genlist);
457 Connect(genlist, SONG_LAYOUT_GENLIST, TYPE_MOUSE_MOVE | TYPE_KEY_DOWN | TYPE_ACTIVATED);
458 m->songlist = genlist;
460 list_item = elm_genlist_item_class_new();
462 evas_object_del(genlist);
463 _ERR(" elm_genlist_item_class_new failed ");
466 list_item->item_style = MUSIC_STYLE_SONGLIST;
467 list_item->func.text_get = sm_CbGetListItemText;
468 list_item->func.content_get = sm_CbGetListItemContent;
469 list_item->func.state_get = NULL;
470 list_item->func.del = sm_CbRemoveListItem;
471 m->item_class = list_item;
473 m_UpdateSongList(false);
477 bool CSongLayout::Create(CLayoutMgr *mgr, const char *uri)
481 Evas_Object *base = NULL;
482 Evas_Object *win = NULL;
483 Evas_Object *layout = NULL;
484 CMusicController *mhandle = NULL;
485 CViewMgr *vmgr = NULL;
492 _ERR(" calloc failed ");
496 vmgr = CViewMgr::GetInstance();
498 _ERR(" get view mgr failed ");
506 _ERR(" get base failed ");
512 win = vmgr->Window();
514 _ERR(" vimewmgr get win failed ");
520 mhandle = CMusicController::GetInstance();
522 _ERR(" music controller get music instance failed ");
528 layout = _add_base(base, MUSIC_SONG_LAYOUT);
530 _ERR(" get layout failed ");
536 bool r = CExtBaseLayout::Create(layout);
538 _ERR(" CBaseLayout creation failed ");
539 evas_object_del(layout);
545 CSortMgr::GetInstance()->RegisterSortFunction(SORT_BY_ARTIST_AZ, sm_CbSortArtistAZ);
546 CSortMgr::GetInstance()->RegisterSortFunction(SORT_BY_ARTIST_ZA, sm_CbSortArtistZA);
547 CSortMgr::GetInstance()->RegisterSortFunction(SORT_BY_ALBUM_AZ, sm_CbSortAlbumAZ);
548 CSortMgr::GetInstance()->RegisterSortFunction(SORT_BY_ALBUM_ZA, sm_CbSortAlbumZA);
551 m->uri = new char[strlen(uri) + 1];
561 m->mhandle = mhandle;
566 Connect(layout, SONG_LAYOUT, TYPE_KEY_DOWN);
569 parcel.ctxtInfo = NULL;
570 parcel.updateType = E_FOCUS_UPDATE;
571 parcel.layoutId = MUSIC_SONG_LAYOUT;
572 parcel.keyEvent = NULL;
573 m->vmgr->UpdateView((const char *)MUSIC_BASE_VIEW, &parcel);
578 void CSongLayout::Destroy(void)
582 CExtBaseLayout::Destroy();
583 evas_object_del(Layout());
592 void CSongLayout::t_OnShow(void)
596 evas_object_show(Layout());
607 void CSongLayout::Update(bool focusFlag)
612 m_UpdateSongList(true);
616 elm_object_focus_set(m->songlist, EINA_TRUE);
620 void CSongLayout::OnKeyDown(int id, Evas *e, Evas_Object *obj, Evas_Event_Key_Down *ev)
625 if (!strcmp(ev->keyname, KEY_BACK) ||
626 !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
628 parcel.ctxtInfo = NULL;
629 parcel.updateType = E_FOCUS_UPDATE;
630 parcel.layoutId = NULL;
631 parcel.keyEvent = NULL;
632 m->vmgr->UpdateView((const char *)MUSIC_BASE_VIEW, &parcel);
637 case SONG_LAYOUT_GENLIST:
639 Elm_Object_Item *it = NULL;
640 SContentInfo *ctxtinfo = NULL;
641 SSongItemInfo *itinfo = NULL;
643 if (strcmp(ev->keyname, KEY_MENU) &&
644 strcmp(ev->keyname, KEY_MENU_REMOTE))
647 it = elm_object_focused_item_get(obj);
649 _ERR(" unable to get focused item ");
652 m->focused_item = it;
659 ctxtinfo = (SContentInfo *)calloc(1, sizeof(*ctxtinfo));
663 itinfo = m_FindItemInfoFromItem(m->it_infolist, it);
669 ctxtinfo->type = CONTEXT_TYPE_SONG;
670 ctxtinfo->context = itinfo->sinfo;
671 ctxtinfo->cbdata = this;
672 ctxtinfo->update = sm_CbCtxtUpdate;
673 ctxtinfo->close = sm_CbCtxtClose;
675 m->ctxtinfo = ctxtinfo;
678 parcel.ctxtInfo = ctxtinfo;
679 if (!m->vmgr->PushView(MUSIC_CONTEXT_VIEW, &parcel))
680 _ERR("viewmgr push view MUSIC_CONTEXT_VIEW failed");
690 void CSongLayout::OnMouseMove(int id, Evas *e, Evas_Object *obj, Evas_Event_Mouse_Move *ev)
693 case SONG_LAYOUT_GENLIST:
695 Elm_Object_Item *item;
697 item = elm_genlist_at_xy_item_get(obj, ev->cur.canvas.x,
698 ev->cur.canvas.y, NULL);
700 if (!elm_object_item_focus_get(item))
701 elm_object_item_focus_set(item, EINA_TRUE);
711 void CSongLayout::OnActivated(int id, Evas_Object *obj, Elm_Object_Item *item)
714 case SONG_LAYOUT_GENLIST:
715 m_OnItemSelect(obj, item);