movie/music: update play info when there is no recent item
[profile/tv/apps/native/air_mediahub.git] / src / grid / grid_music.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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 <Elementary.h>
18 #include <media_content.h>
19 #include <app_debug.h>
20 #include <app_contents.h>
21 #include <app_media.h>
22 #include <gridmgr.h>
23 #include <inputmgr.h>
24 #include <viewmgr.h>
25
26 #include "define.h"
27 #include "view.h"
28 #include "data/datamgr.h"
29 #include "grid/grid.h"
30 #include "util/util.h"
31
32 #define STR_GENRE_NAME "Genre"
33
34 static char *_media_text_get(void *data, Evas_Object *obj, const char *part)
35 {
36         app_media *am;
37         app_media_info *info;
38
39         if (!data)
40                 return NULL;
41
42         am = data;
43
44         info = app_media_get_info(am);
45         if (!info) {
46                 _ERR("failed to get media info");
47                 return NULL;
48         }
49
50         if (!strcmp(part, PART_ELM_TEXT_TITLE))
51                 return strdup(info->title);
52         else if (!strcmp(part, PART_ELM_TEXT_ARTIST))
53                 return strdup(info->audio->artist);
54         else if (!strcmp(part, PART_ELM_TEXT_ALBUM))
55                 return strdup(info->audio->album);
56
57         return NULL;
58 }
59
60 static Evas_Object *_media_content_get(void *data,
61                         Evas_Object *obj, const char *part)
62 {
63         Evas_Object *eo;
64         app_media *am;
65         app_media_info *info;
66
67         if (!data)
68                 return NULL;
69
70         am = data;
71
72         info = app_media_get_info(am);
73         if (!info) {
74                 _ERR("failed to get media info");
75                 return NULL;
76         }
77
78         eo = NULL;
79         if (!strcmp(part, PART_ELM_SWALLOW_THUMBNAIL)) {
80                 eo = util_add_image(obj, info->thumbnail_path);
81                 if (!eo) {
82                         _ERR("failed to create image object");
83                         return NULL;
84                 }
85         } else if (!strcmp(part, PART_ELM_SWALLOW_FAVORITE)) {
86                 if (!info->favorite)
87                         return NULL;
88
89                 eo = util_add_image(obj, IMAGE_THUMBNAIL_FAVORITE);
90                 if (!eo) {
91                         _ERR("failed to create image object");
92                         return NULL;
93                 }
94         }
95
96         if (eo)
97                 evas_object_show(eo);
98
99         return eo;
100 }
101
102 static char *_album_text_get(void *data, Evas_Object *obj, const char *part)
103 {
104         struct album_info *ai;
105
106         if (!data)
107                 return NULL;
108
109         ai = data;
110
111         return strdup(ai->name);
112 }
113
114 static Evas_Object *_album_content_get(void *data, Evas_Object *obj,
115                         const char *part)
116 {
117         Evas_Object *eo;
118         struct album_info *ai;
119         struct color_data bg;
120
121         if (!data)
122                 return NULL;
123
124         ai = data;
125
126         eo = NULL;
127         if (!strcmp(part, PART_ELM_SWALLOW_THUMBNAIL)) {
128                 if (ai->album_art) {
129                         eo = util_add_image(obj, ai->album_art);
130                         if (!eo) {
131                                 _ERR("failed to create image object");
132                                 return NULL;
133                         }
134                 } else {
135                         eo = evas_object_rectangle_add(obj);
136                         if (!eo) {
137                                 _ERR("failed to create rectangle object");
138                                 return NULL;
139
140                         }
141
142                         app_contents_get_color(ai->name, &bg, NULL);
143                         evas_object_color_set(eo, bg.r, bg.g, bg.b, bg.a);
144                 }
145         } else if (!strcmp(part, PART_ELM_SWALLOW_ICON)) {
146                 if (ai->album_art)
147                         return NULL;
148
149                 eo = util_add_image(obj, IMAGE_THUMBNAIL_SONG_ALBUM);
150                 if (!eo) {
151                         _ERR("failed to create image object");
152                         return NULL;
153                 }
154         } else if (!strcmp(part, PART_ELM_SWALLOW_TEXTBG)) {
155                 eo = evas_object_rectangle_add(obj);
156                 if (!eo) {
157                         _ERR("failed to create rectangle object");
158                         return NULL;
159                 }
160
161                 app_contents_get_color(ai->name, NULL, &bg);
162                 evas_object_color_set(eo, bg.r, bg.g, bg.b, bg.a);
163         }
164
165         if (eo)
166                 evas_object_show(eo);
167
168         return eo;
169 }
170
171 static char *_artist_text_get(void *data, Evas_Object *obj, const char *part)
172 {
173         struct album_info *ai;
174
175         if (!data)
176                 return NULL;
177
178         ai = data;
179
180         return strdup(ai->artist);
181 }
182
183 static Evas_Object *_artist_content_get(void *data, Evas_Object *obj,
184                         const char *part)
185 {
186         Evas_Object *eo;
187         struct album_info *ai;
188         struct color_data bg;
189
190         if (!data)
191                 return NULL;
192
193         ai = data;
194
195         eo = NULL;
196         if (!strcmp(part, PART_ELM_SWALLOW_THUMBNAIL)) {
197                 if (ai->album_art) {
198                         eo = util_add_image(obj, ai->album_art);
199                         if (!eo) {
200                                 _ERR("failed to create image object");
201                                 return NULL;
202                         }
203                 } else {
204                         eo = evas_object_rectangle_add(obj);
205                         if (!eo) {
206                                 _ERR("failed to create rectangle object");
207                                 return NULL;
208
209                         }
210
211                         app_contents_get_color(ai->name, &bg, NULL);
212                         evas_object_color_set(eo, bg.r, bg.g, bg.b, bg.a);
213                 }
214         } else if (!strcmp(part, PART_ELM_SWALLOW_ICON)) {
215                 if (ai->album_art)
216                         return NULL;
217
218                 eo = util_add_image(obj, IMAGE_THUMBNAIL_ARTIST);
219                 if (!eo) {
220                         _ERR("failed to create image object");
221                         return NULL;
222                 }
223         } else if (!strcmp(part, PART_ELM_SWALLOW_TEXTBG)) {
224                 eo = evas_object_rectangle_add(obj);
225                 if (!eo) {
226                         _ERR("failed to create rectangle object");
227                         return NULL;
228                 }
229
230                 app_contents_get_color(ai->name, NULL, &bg);
231                 evas_object_color_set(eo, bg.r, bg.g, bg.b, bg.a);
232         }
233
234         if (eo)
235                 evas_object_show(eo);
236
237         return eo;
238 }
239
240 static char *_group_text_get(void *data, Evas_Object *obj, const char *part)
241 {
242         struct group_info *gi;
243         char buf[32];
244         int cnt;
245
246         if (!data)
247                 return NULL;
248
249         gi = data;
250
251         if (!strcmp(part, PART_ELM_TEXT_TITLE))
252                 return strdup(gi->name);
253         else if (!strcmp(part, PART_ELM_TEXT_COUNT)) {
254                 cnt = eina_list_count(gi->list);
255                 snprintf(buf, sizeof(buf), "%d", cnt);
256                 return strdup(buf);
257         }
258
259         return NULL;
260 }
261
262 static Evas_Object *_group_content_get(void *data, Evas_Object *obj,
263                         const char *part)
264 {
265         Evas_Object *eo;
266         struct group_info *gi;
267         struct color_data bg;
268
269         if (!data)
270                 return NULL;
271
272         gi = data;
273
274         eo = NULL;
275         if (!strcmp(part, PART_ELM_SWALLOW_BG)) {
276                 eo = evas_object_rectangle_add(obj);
277                 if (!eo) {
278                         _ERR("failed to create rectangle object");
279                         return NULL;
280                 }
281
282                 app_contents_get_color(gi->name, &bg, NULL);
283                 evas_object_color_set(eo, bg.r, bg.g, bg.b, bg.a);
284
285                 evas_object_show(eo);
286         }
287
288         return eo;
289 }
290
291 static Eina_List *_get_song_list(struct datamgr *dmgr)
292 {
293         if (!dmgr) {
294                 _ERR("invalid argument");
295                 return NULL;
296         }
297
298         return dmgr->ops->get_group(dmgr->handle, E_GROUP_MEDIA_NAME, NULL);
299 }
300
301 static Eina_List *_get_album_list(struct datamgr *dmgr)
302 {
303         if (!dmgr) {
304                 _ERR("invalid argument");
305                 return NULL;
306         }
307
308         return dmgr->ops->get_group(dmgr->handle, E_GROUP_ALBUM_NAME, NULL);
309 }
310
311 static Eina_List *_get_artist_list(struct datamgr *dmgr)
312 {
313         if (!dmgr) {
314                 _ERR("invalid argument");
315                 return NULL;
316         }
317
318         return dmgr->ops->get_group(dmgr->handle, E_GROUP_ALBUM_ARTIST, NULL);
319 }
320
321 static Eina_List *_get_genre_list(struct datamgr *dmgr)
322 {
323         Eina_List *list;
324         struct group_info *gi;
325
326         if (!dmgr) {
327                 _ERR("invalid argument");
328                 return NULL;
329         }
330
331         gi = calloc(1, sizeof(*gi));
332         if (!gi) {
333                 _ERR("failed to allocate memory");
334                 return NULL;
335         }
336
337         gi->name = STR_GENRE_NAME;
338         gi->list = dmgr->ops->get_group(dmgr->handle,
339                                 E_GROUP_MEDIA_GENRE, NULL);
340
341         list = NULL;
342         list = eina_list_append(list, gi);
343
344         return list;
345 }
346
347 static void _free_item_list(struct datamgr *dmgr, Eina_List *list)
348 {
349         if (!dmgr) {
350                 _ERR("invalid argument");
351                 return;
352         }
353
354         dmgr->ops->free_group(list);
355 }
356
357 static void _free_group_list(struct datamgr *dmgr, Eina_List *list)
358 {
359         struct group_info *gi;
360
361         if (!dmgr) {
362                 _ERR("invalid argument");
363                 return;
364         }
365
366         EINA_LIST_FREE(list, gi) {
367                 dmgr->ops->free_group(gi->list);
368                 free(gi);
369         }
370 }
371
372 static void _get_media_info(struct datamgr *dmgr, Eina_List *list, char *str,
373                         int size)
374 {
375         int cnt;
376
377         cnt = dmgr->ops->get_count(dmgr->handle, E_FILE_MUSIC);
378         if (cnt < 0) {
379                 _ERR("failed to get media count");
380                 cnt = 0;
381         }
382
383         snprintf(str, size, "%d Songs", cnt);
384 }
385
386 static void _get_album_info(struct datamgr *dmgr, Eina_List *list, char *str,
387                         int size)
388 {
389         Eina_List *l;
390         struct group_info *gi;
391         int cnt;
392
393         cnt = 0;
394         EINA_LIST_FOREACH(list, l, gi)
395                 cnt += eina_list_count(gi->list);
396
397         snprintf(str, size, "%d Albums", cnt);
398 }
399
400 static void _get_artist_info(struct datamgr *dmgr, Eina_List *list, char *str,
401                         int size)
402 {
403         Eina_List *l;
404         struct group_info *gi;
405         int cnt;
406
407         cnt = 0;
408         EINA_LIST_FOREACH(list, l, gi)
409                 cnt += eina_list_count(gi->list);
410
411         snprintf(str, size, "%d Artists", cnt);
412 }
413
414 static void _get_genre_info(struct datamgr *dmgr, Eina_List *list, char *str,
415                         int size)
416 {
417         struct group_info *gi;
418         int cnt;
419
420         gi = eina_list_nth(list, 0);
421         cnt = eina_list_count(gi->list);
422
423         snprintf(str, size, "%d Genre", cnt);
424 }
425
426 static void _key_down_cb(void *data, Elm_Object_Item *it,
427                         Evas_Event_Key_Down *ev)
428 {
429         struct view_update_data vdata;
430         struct datamgr *dmgr;
431         app_media *am;
432
433         if (!data || !ev) {
434                 _ERR("invalid argument");
435                 return;
436         }
437
438         dmgr = data;
439
440         if (!strcmp(ev->keyname, KEY_MENU) ||
441                 !strcmp(ev->keyname, KEY_CONTEXT_MENU)) {
442                 am = elm_object_item_data_get(it);
443                 if (!am) {
444                         _ERR("failed to get app media");
445                         return;
446                 }
447
448                 vdata.list = dmgr->ops->get_list(dmgr->handle,
449                                         E_LIST_MEDIA, NULL);
450                 vdata.index = util_get_media_index(vdata.list, am);
451
452                 viewmgr_update_view(VIEW_ACTION_MENU, UPDATE_CONTENT, &vdata);
453                 viewmgr_show_view(VIEW_ACTION_MENU);
454         }
455 }
456
457 static void _media_selected_cb(void *data, Elm_Object_Item *it)
458 {
459         struct view_update_data vdata;
460         struct datamgr *dmgr;
461         app_media *am;
462
463         if (!data || !it) {
464                 _ERR("invalid argument");
465                 return;
466         }
467
468         dmgr = data;
469
470         am = elm_object_item_data_get(it);
471         if (!am) {
472                 _ERR("failed to get app media");
473                 return;
474         }
475
476         vdata.list = dmgr->ops->get_list(dmgr->handle, E_LIST_MEDIA, NULL);
477         vdata.index = util_get_media_index(vdata.list, am);
478         vdata.id = VIEW_BASE;
479
480         viewmgr_update_view(VIEW_MPLAYER, UPDATE_CONTENT, &vdata);
481         viewmgr_push_view(VIEW_MPLAYER);
482 }
483
484 static void _album_selected_cb(void *data, Elm_Object_Item *it)
485 {
486         struct view_update_data vdata;
487         struct datamgr *dmgr;
488         struct album_info *ai;
489
490         if (!data || !it) {
491                 _ERR("invalid argument");
492                 return;
493         }
494
495         dmgr = data;
496
497         ai = elm_object_item_data_get(it);
498         if (!ai) {
499                 _ERR("failed to get album info");
500                 return;
501         }
502
503         vdata.list = dmgr->ops->get_list(dmgr->handle,
504                                 E_LIST_ALBUM_MEDIA, &ai->id);
505         vdata.index = E_DETAIL_MUSIC_ALBUM;
506         vdata.id = ai->name;
507
508         viewmgr_update_view(VIEW_DETAIL, UPDATE_CONTENT, &vdata);
509         viewmgr_push_view(VIEW_DETAIL);
510 }
511
512 static void _artist_selected_cb(void *data, Elm_Object_Item *it)
513 {
514         struct view_update_data vdata;
515         struct datamgr *dmgr;
516         struct album_info *ai;
517
518         if (!data || !it) {
519                 _ERR("invalid argument");
520                 return;
521         }
522
523         dmgr = data;
524
525         ai = elm_object_item_data_get(it);
526         if (!ai) {
527                 _ERR("failed to get album info");
528                 return;
529         }
530
531         vdata.list = dmgr->ops->get_list(dmgr->handle,
532                                 E_LIST_ARTIST_ALBUM, ai->artist);
533         vdata.index = E_DETAIL_MUSIC_ARTIST;
534         vdata.id = ai->artist;
535
536         viewmgr_update_view(VIEW_DETAIL, UPDATE_CONTENT, &vdata);
537         viewmgr_push_view(VIEW_DETAIL);
538 }
539
540 static void _genre_selected_cb(void *data, Elm_Object_Item *it)
541 {
542         struct view_update_data vdata;
543         struct group_info *gi;
544
545         if (!it) {
546                 _ERR("invalid argument");
547                 return;
548         }
549
550         gi = elm_object_item_data_get(it);
551         if (!gi) {
552                 _ERR("failed to get group info");
553                 return;
554         }
555
556         vdata.list = gi->list;
557         vdata.index = E_DETAIL_MUSIC_GENRE;
558         vdata.id = gi->name;
559
560         viewmgr_update_view(VIEW_DETAIL, UPDATE_CONTENT, &vdata);
561         viewmgr_push_view(VIEW_DETAIL);
562 }
563
564 static struct grid_class _gclass[] = {
565         [E_MUSIC_SONG] = {
566                 .item_style = STYLE_MUSIC_SONG,
567                 .text_get = _media_text_get,
568                 .content_get = _media_content_get
569         },
570         [E_MUSIC_ALBUM] = {
571                 .item_style = STYLE_MUSIC_ALBUM,
572                 .text_get = _album_text_get,
573                 .content_get = _album_content_get
574         },
575         [E_MUSIC_ARTIST] = {
576                 .item_style = STYLE_MUSIC_ARTIST,
577                 .text_get = _artist_text_get,
578                 .content_get = _artist_content_get
579         },
580         [E_MUSIC_GENRE] = {
581                 .item_style = STYLE_MUSIC_GENRE,
582                 .text_get = _group_text_get,
583                 .content_get = _group_content_get
584         },
585         /* Other view mode class will be added later */
586 };
587
588 static struct grid_data _gdata[] = {
589         [E_MUSIC_SONG] = {
590                 .item_x = MUSIC_SONG_ITEM_X,
591                 .item_y = MUSIC_SONG_ITEM_Y,
592                 .item_num = MUSIC_SONG_ITEM_NUM,
593                 .grid_padding = MUSIC_SONG_GRID_PADDING,
594                 .gclass = &_gclass[E_MUSIC_SONG],
595                 .get_item_list = _get_song_list,
596                 .free_item_list = _free_item_list,
597                 .get_item_info = _get_media_info,
598                 .key_down_cb = _key_down_cb,
599                 .selected_cb = _media_selected_cb,
600         },
601         [E_MUSIC_ALBUM] = {
602                 .item_x = MUSIC_ALBUM_ITEM_X,
603                 .item_y = MUSIC_ALBUM_ITEM_Y,
604                 .item_num = MUSIC_ALBUM_ITEM_NUM,
605                 .grid_padding = MUSIC_ALBUM_GRID_PADDING,
606                 .gclass = &_gclass[E_MUSIC_ALBUM],
607                 .get_item_list = _get_album_list,
608                 .free_item_list = _free_item_list,
609                 .get_item_info = _get_album_info,
610                 .selected_cb = _album_selected_cb,
611         },
612         [E_MUSIC_ARTIST] = {
613                 .item_x = MUSIC_ARTIST_ITEM_X,
614                 .item_y = MUSIC_ARTIST_ITEM_Y,
615                 .item_num = MUSIC_ARTIST_ITEM_NUM,
616                 .grid_padding = MUSIC_ARTIST_GRID_PADDING,
617                 .gclass = &_gclass[E_MUSIC_ARTIST],
618                 .get_item_list = _get_artist_list,
619                 .free_item_list = _free_item_list,
620                 .get_item_info = _get_artist_info,
621                 .selected_cb = _artist_selected_cb,
622         },
623         [E_MUSIC_GENRE] = {
624                 .item_x = MUSIC_GENRE_ITEM_X,
625                 .item_y = MUSIC_GENRE_ITEM_Y,
626                 .item_num = MUSIC_GENRE_ITEM_NUM,
627                 .grid_padding = MUSIC_GENRE_GRID_PADDING,
628                 .gclass = &_gclass[E_MUSIC_GENRE],
629                 .get_item_list = _get_genre_list,
630                 .free_item_list = _free_group_list,
631                 .get_item_info = _get_genre_info,
632                 .selected_cb = _genre_selected_cb,
633         },
634         /* Other view mode data will be added later */
635 };
636
637 struct grid_data *get_music_grid_data(int type)
638 {
639         if (type < 0 || type >= E_MUSIC_MAX) {
640                 _ERR("invalid argument");
641                 return NULL;
642         }
643
644         return &_gdata[type];
645 }