movie/music: update play info when there is no recent item
[profile/tv/apps/native/air_mediahub.git] / src / grid / grid_gallery.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_PLACE_NAME "City"
33
34 static void _create_thumbnail_completed_cb(media_content_error_e err,
35                         const char *path, void *user_data)
36 {
37         Elm_Object_Item *it;
38         app_media *am;
39
40         if (!user_data)
41                 return;
42
43         it = user_data;
44
45         media_content_connect();
46
47         am = elm_object_item_data_get(it);
48
49         app_media_update(am);
50
51         elm_gengrid_item_fields_update(it, PART_ELM_SWALLOW_THUMBNAIL,
52                                 ELM_GENGRID_ITEM_FIELD_CONTENT);
53
54         media_content_disconnect();
55 }
56
57 static char *_media_text_get(void *data, Evas_Object *obj, const char *part)
58 {
59         app_media *am;
60         app_media_info *info;
61         char buf[32];
62
63         if (!data)
64                 return NULL;
65
66         am = data;
67
68         info = app_media_get_info(am);
69         if (!info) {
70                 _ERR("failed to get media info");
71                 return NULL;
72         }
73
74         if (info->media_type != MEDIA_CONTENT_TYPE_VIDEO)
75                 return NULL;
76
77         if (!strcmp(part, PART_ELM_TEXT_PLAYTIME)) {
78                 util_time_string(buf, sizeof(buf),
79                                         info->video->duration, false);
80
81                 return strdup(buf);
82         }
83
84         return NULL;
85 }
86
87 static Evas_Object *_media_content_get(void *data,
88                         Evas_Object *obj, const char *part)
89 {
90         Evas_Object *eo;
91         app_media *am;
92         app_media_info *info;
93
94         if (!data)
95                 return NULL;
96
97         am = data;
98
99         info = app_media_get_info(am);
100         if (!info) {
101                 _ERR("failed to get media info");
102                 return NULL;
103         }
104
105         eo = NULL;
106         if (!strcmp(part, PART_ELM_SWALLOW_THUMBNAIL)) {
107                 if (!info->thumbnail_path) {
108                         util_create_thumbnail(obj, am,
109                                 _create_thumbnail_completed_cb);
110                         return NULL;
111                 }
112
113                 eo = util_add_image(obj, info->thumbnail_path);
114                 if (!eo) {
115                         _ERR("failed to create image object");
116                         return NULL;
117                 }
118         } else if (!strcmp(part, PART_ELM_SWALLOW_PICTURE)) {
119                 if (info->media_type != MEDIA_CONTENT_TYPE_IMAGE)
120                         return NULL;
121
122                 if (info->thumbnail_path)
123                         return NULL;
124
125                 eo = util_add_image(obj, IMAGE_THUMBNAIL_PICTURE);
126                 if (!eo) {
127                         _ERR("failed to create image object");
128                         return NULL;
129                 }
130         } else if (!strcmp(part, PART_ELM_SWALLOW_VIDEO)) {
131                 if (info->media_type != MEDIA_CONTENT_TYPE_VIDEO)
132                         return NULL;
133
134                 eo = util_add_image(obj, IMAGE_THUMBNAIL_PLAY);
135                 if (!eo) {
136                         _ERR("failed to create image object");
137                         return NULL;
138                 }
139         } else if (!strcmp(part, PART_ELM_SWALLOW_FAVORITE)) {
140                 if (!info->favorite)
141                         return NULL;
142
143                 eo = util_add_image(obj, IMAGE_THUMBNAIL_FAVORITE);
144                 if (!eo) {
145                         _ERR("failed to create image object");
146                         return NULL;
147                 }
148         }
149
150         if (eo)
151                 evas_object_show(eo);
152
153         return eo;
154 }
155
156 static char *_place_text_get(void *data, Evas_Object *obj, const char *part)
157 {
158         struct group_info *gi;
159         int cnt;
160         char buf[64];
161         char *city, *country, *tmp;
162
163         if (!data)
164                 return NULL;
165
166         gi = data;
167
168         snprintf(buf, sizeof(buf), "%s", gi->name);
169         city = strtok_r(buf, "/", &tmp);
170         country = strtok_r(tmp, "", &tmp);
171
172         if (!strcmp(part, PART_ELM_TEXT_COUNT)) {
173                 cnt = eina_list_count(gi->list);
174                 snprintf(buf, sizeof(buf), "%d", cnt);
175                 return strdup(buf);
176         } else if (!strcmp(part, PART_ELM_TEXT_CITY)) {
177                 if (!city)
178                         return NULL;
179
180                 return strdup(city);
181         } else if (!strcmp(part, PART_ELM_TEXT_COUNTRY)) {
182                 if (!country)
183                         return NULL;
184
185                 return strdup(country);
186         }
187
188         return NULL;
189 }
190
191 static Evas_Object *_place_content_get(void *data, Evas_Object *obj,
192                         const char *part)
193 {
194         Evas_Object *eo;
195         struct group_info *gi;
196         struct color_data bg;
197
198         if (!data)
199                 return NULL;
200
201         gi = data;
202
203         eo = NULL;
204         if (!strcmp(part, PART_ELM_SWALLOW_BG)) {
205                 eo = evas_object_rectangle_add(obj);
206                 if (!eo) {
207                         _ERR("failed to create rectangle object");
208                         return NULL;
209                 }
210
211                 app_contents_get_color(gi->name, &bg, NULL);
212                 evas_object_color_set(eo, bg.r, bg.g, bg.b, bg.a);
213
214                 evas_object_show(eo);
215         }
216
217         return eo;
218 }
219
220 static Eina_List *_get_event_list(struct datamgr *dmgr)
221 {
222         if (!dmgr) {
223                 _ERR("invalid argument");
224                 return NULL;
225         }
226
227         return dmgr->ops->get_group(dmgr->handle, E_GROUP_MEDIA_DATE, NULL);
228 }
229
230 static Eina_List *_get_place_list(struct datamgr *dmgr)
231 {
232         Eina_List *list;
233         struct group_info *gi;
234
235         if (!dmgr) {
236                 _ERR("invalid argument");
237                 return NULL;
238         }
239
240         gi = calloc(1, sizeof(*gi));
241         if (!gi) {
242                 _ERR("failed to allocate memory");
243                 return NULL;
244         }
245
246         gi->name = STR_PLACE_NAME;
247         gi->list = dmgr->ops->get_group(dmgr->handle,
248                                 E_GROUP_MEDIA_PLACE, NULL);
249
250         list = NULL;
251         list = eina_list_append(list, gi);
252
253         return list;
254 }
255
256 static Eina_List *_get_type_list(struct datamgr *dmgr)
257 {
258         if (!dmgr) {
259                 _ERR("invalid argument");
260                 return NULL;
261         }
262
263         return dmgr->ops->get_group(dmgr->handle, E_GROUP_MEDIA_TYPE, NULL);
264 }
265
266 static void _free_media_list(struct datamgr *dmgr, Eina_List *list)
267 {
268         if (!dmgr) {
269                 _ERR("invalid argument");
270                 return;
271         }
272
273         dmgr->ops->free_group(list);
274 }
275
276 static void _free_place_list(struct datamgr *dmgr, Eina_List *list)
277 {
278         struct group_info *gi;
279
280         if (!dmgr) {
281                 _ERR("invalid argument");
282                 return;
283         }
284
285         EINA_LIST_FREE(list, gi) {
286                 dmgr->ops->free_group(gi->list);
287                 free(gi);
288         }
289 }
290
291 static void _get_media_info(struct datamgr *dmgr, Eina_List *list, char *str,
292                         int size)
293 {
294         int photo, video;
295
296         photo = dmgr->ops->get_count(dmgr->handle, E_FILE_PHOTO);
297         video = dmgr->ops->get_count(dmgr->handle, E_FILE_VIDEO);
298
299         if (photo < 0) {
300                 _ERR("failed to get media count");
301                 photo = 0;
302         }
303
304         if (video < 0) {
305                 _ERR("failed to get media count");
306                 video = 0;
307         }
308
309         snprintf(str, size, "%d Photos, %d Videos", photo, video);
310 }
311
312 static void _get_place_info(struct datamgr *dmgr, Eina_List *list, char *str,
313                         int size)
314 {
315         Eina_List *l;
316         struct group_info *gi;
317         int cnt;
318
319         cnt = 0;
320         EINA_LIST_FOREACH(list, l, gi)
321                 cnt += eina_list_count(gi->list);
322
323         snprintf(str, size, "%d Cities", cnt);
324 }
325
326 static void _key_down_cb(void *data, Elm_Object_Item *it,
327                         Evas_Event_Key_Down *ev)
328 {
329         struct view_update_data vdata;
330         struct datamgr *dmgr;
331         app_media *am;
332
333         if (!data || !ev) {
334                 _ERR("invalid argument");
335                 return;
336         }
337
338         dmgr = data;
339
340         if (!strcmp(ev->keyname, KEY_MENU) ||
341                 !strcmp(ev->keyname, KEY_CONTEXT_MENU)) {
342                 am = elm_object_item_data_get(it);
343                 if (!am) {
344                         _ERR("failed to get app media");
345                         return;
346                 }
347
348                 vdata.list = dmgr->ops->get_list(dmgr->handle,
349                                         E_LIST_MEDIA, NULL);
350                 vdata.index = util_get_media_index(vdata.list, am);
351
352                 viewmgr_update_view(VIEW_ACTION_MENU, UPDATE_CONTENT, &vdata);
353                 viewmgr_show_view(VIEW_ACTION_MENU);
354         }
355 }
356
357 static void _media_selected_cb(void *data, Elm_Object_Item *it)
358 {
359         struct view_update_data vdata;
360         struct datamgr *dmgr;
361         app_media *am;
362
363         if (!data || !it) {
364                 _ERR("invalid argument");
365                 return;
366         }
367
368         dmgr = data;
369
370         am = elm_object_item_data_get(it);
371         if (!am) {
372                 _ERR("failed to get app media");
373                 return;
374         }
375
376         vdata.list = dmgr->ops->get_list(dmgr->handle, E_LIST_MEDIA, NULL);
377         vdata.index = util_get_media_index(vdata.list, am);
378         vdata.id = VIEW_BASE;
379
380         viewmgr_update_view(VIEW_VIEWER, UPDATE_CONTENT, &vdata);
381         viewmgr_push_view(VIEW_VIEWER);
382 }
383
384 static void _place_selected_cb(void *data, Elm_Object_Item *it)
385 {
386         struct view_update_data vdata;
387         struct group_info *gi;
388
389         if (!it) {
390                 _ERR("invalid argument");
391                 return;
392         }
393
394         gi = elm_object_item_data_get(it);
395         if (!gi) {
396                 _ERR("failed to get group info");
397                 return;
398         }
399
400         vdata.list = gi->list;
401         vdata.index = E_DETAIL_GALLERY_PLACE;
402         vdata.id = gi->name;
403
404         viewmgr_update_view(VIEW_DETAIL, UPDATE_CONTENT, &vdata);
405         viewmgr_push_view(VIEW_DETAIL);
406 }
407
408 static struct grid_class _gclass[] = {
409         [E_GALLERY_EVENT] = {
410                 .item_style = STYLE_GALLERY_EVENT,
411                 .text_get = _media_text_get,
412                 .content_get = _media_content_get
413         },
414         [E_GALLERY_PLACE] = {
415                 .item_style = STYLE_GALLERY_PLACE,
416                 .text_get = _place_text_get,
417                 .content_get = _place_content_get
418         },
419         /* Other view mode class will be added later */
420 };
421
422 static struct grid_data _gdata[] = {
423         [E_GALLERY_EVENT] = {
424                 .item_x = GALLERY_EVENT_ITEM_X,
425                 .item_y = GALLERY_EVENT_ITEM_Y,
426                 .item_num = GALLERY_EVENT_ITEM_NUM,
427                 .grid_padding = GALLERY_EVENT_GRID_PADDING,
428                 .gclass = &_gclass[E_GALLERY_EVENT],
429                 .get_item_list = _get_event_list,
430                 .free_item_list = _free_media_list,
431                 .get_item_info = _get_media_info,
432                 .key_down_cb = _key_down_cb,
433                 .selected_cb = _media_selected_cb
434         },
435         [E_GALLERY_PLACE] = {
436                 .item_x = GALLERY_PLACE_ITEM_X,
437                 .item_y = GALLERY_PLACE_ITEM_Y,
438                 .item_num = GALLERY_PLACE_ITEM_NUM,
439                 .grid_padding = GALLERY_PLACE_GRID_PADDING,
440                 .gclass = &_gclass[E_GALLERY_PLACE],
441                 .get_item_list = _get_place_list,
442                 .free_item_list = _free_place_list,
443                 .get_item_info = _get_place_info,
444                 .selected_cb = _place_selected_cb
445         },
446         [E_GALLERY_VIDEO] = {
447                 .item_x = GALLERY_EVENT_ITEM_X,
448                 .item_y = GALLERY_EVENT_ITEM_Y,
449                 .item_num = GALLERY_EVENT_ITEM_NUM,
450                 .grid_padding = GALLERY_EVENT_GRID_PADDING,
451                 .gclass = &_gclass[E_GALLERY_EVENT],
452                 .get_item_list = _get_type_list,
453                 .free_item_list = _free_media_list,
454                 .get_item_info = _get_media_info,
455                 .key_down_cb = _key_down_cb,
456                 .selected_cb = _media_selected_cb
457         },
458         /* Other view mode data will be added later */
459 };
460
461 struct grid_data *get_gallery_grid_data(int type)
462 {
463         if (type < 0 || type >= E_GALLERY_MAX) {
464                 _ERR("invalid argument");
465                 return NULL;
466         }
467
468         return &_gdata[type];
469 }