listmgr: add update parameter for listmgr_update_focus_item
[profile/tv/apps/native/air_mediahub.git] / src / layout / 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_media.h>
21 #include <gridmgr.h>
22 #include <layoutmgr.h>
23 #include <viewmgr.h>
24
25 #include "define.h"
26 #include "view.h"
27 #include "data/mediadata.h"
28 #include "util/listmgr.h"
29 #include "util/util.h"
30
31 #define LIST_MEDIA_COND "media_type=0 OR \
32                         (media_type=1 AND copyright LIKE \"Unknown\")"
33
34 #define TEXT_NOCONTENT "No Photo & Video"
35
36 #define GRID_ITEM_X 206
37 #define GRID_ITEM_Y 206
38 #define GRID_NUM_ITEM 3
39
40 #define BOX_PADDING 62
41
42 struct _priv {
43         Evas_Object *base;
44         Evas_Object *layout;
45         Evas_Object *menu_btn;
46
47         layoutmgr *lmgr;
48
49         struct listmgr *listmgr;
50         struct listmgr_data *ldata;
51
52         struct mediadata *md;
53         int cur_index;
54
55         Eina_List *media_list;
56 };
57
58 static char *_grid_text_get(void *data, Evas_Object *obj, const char *part)
59 {
60         app_media *am;
61         app_media_info *info;
62         char buf[32];
63
64         if (!data)
65                 return NULL;
66
67         am = data;
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 *_grid_content_get(void *data,
88                         Evas_Object *obj, const char *part)
89 {
90         Evas_Object *image;
91         app_media *am;
92         app_media_info *info;
93
94         if (!data)
95                 return NULL;
96
97         am = data;
98         info = app_media_get_info(am);
99         if (!info) {
100                 _ERR("failed to get media info");
101                 return NULL;
102         }
103
104         image = NULL;
105         if (!strcmp(part, PART_ELM_SWALLOW_THUMBNAIL)) {
106                 image = util_add_image(obj, info->thumbnail_path);
107                 if (!image) {
108                         _ERR("failed to create image object");
109                         return NULL;
110                 }
111
112                 evas_object_show(image);
113         } else if (!strcmp(part, PART_ELM_SWALLOW_VIDEO)) {
114                 if (info->media_type == MEDIA_CONTENT_TYPE_VIDEO) {
115                         image = util_add_image(obj, IMAGE_THUMBNAIL_PLAY);
116                         if (!image) {
117                                 _ERR("failed to create image object");
118                                 return NULL;
119                         }
120
121                         evas_object_show(image);
122                 }
123         }
124
125         return image;
126 }
127
128 static struct grid_class _gclass = {
129         .item_style = STYLE_GRID_GALLERY_ITEM,
130         .text_get = _grid_text_get,
131         .content_get = _grid_content_get
132 };
133
134 static void _grid_selected_cb(void *data, Elm_Object_Item *it)
135 {
136         app_media *am;
137         struct view_update_data vdata;
138         struct _priv *priv;
139
140         if (!data || !it) {
141                 _ERR("invalid argument");
142                 return;
143         }
144
145         priv = data;
146
147         am = elm_object_item_data_get(it);
148         if (!am) {
149                 _ERR("failed to get app media");
150                 return;
151         }
152
153         vdata.list = mediadata_get_medialist(priv->md);
154         vdata.index = util_get_media_index(vdata.list, am);
155         priv->cur_index = vdata.index;
156
157         viewmgr_update_view(VIEW_VIEWER, UPDATE_CONTENT, &vdata);
158         viewmgr_push_view(VIEW_VIEWER);
159 }
160
161 static struct listmgr_data *_create_listmgr_data(struct _priv *priv)
162 {
163         struct listmgr_data *data;
164         struct grid_ops *gops;
165
166         data = calloc(1, sizeof(*data));
167         if (!data)
168                 goto err;
169
170         data->menu_btn = priv->menu_btn;
171
172         data->grid_item_x = GRID_ITEM_X;
173         data->grid_item_y = GRID_ITEM_Y;
174         data->grid_num_item = GRID_NUM_ITEM;
175         data->box_padding = BOX_PADDING;
176
177         gops = calloc(1, sizeof(*gops));
178         if (!gops)
179                 goto err;
180
181         gops->gclass = &_gclass;
182         gops->selected_cb = _grid_selected_cb;
183         gops->ops_data = priv;
184
185         data->gops = gops;
186
187         return data;
188
189 err:
190         _ERR("failed to allocate memory");
191         return NULL;
192 }
193
194 static void _update_list_area(struct _priv *priv)
195 {
196         Eina_List *list;
197
198         if (priv->media_list)
199                 return;
200
201         list = mediadata_get_list(priv->md, E_LIST_DATE);
202         if (!list) {
203                 elm_object_part_text_set(priv->layout,
204                                 PART_NOCONTENT, TEXT_NOCONTENT);
205                 return;
206         }
207
208         if (!listmgr_update_content_list(priv->listmgr, list))
209                 _ERR("failed to update list area");
210
211         priv->media_list = list;
212 }
213
214 static bool _create(layoutmgr *lmgr, void *data)
215 {
216         struct listmgr *listmgr;
217         struct listmgr_data *ldata;
218         struct mediadata *md;
219         struct _priv *priv;
220         Evas_Object *base, *layout;
221
222         if (!lmgr) {
223                 _ERR("failed to get layoutmgr");
224                 return false;
225         }
226
227         if (!data) {
228                 _ERR("invalid argument");
229                 return false;
230         }
231
232         priv = calloc(1, sizeof(*priv));
233         if (!priv) {
234                 _ERR("failed to allocate priv");
235                 return false;
236         }
237
238         priv->menu_btn = (Evas_Object *)data;
239
240         base = layoutmgr_get_base(lmgr);
241         if (!base) {
242                 _ERR("failed to get base object");
243                 goto err;
244         }
245
246         layout = elm_layout_add(base);
247         if (!layout) {
248                 _ERR("failed to create layout");
249                 goto err;
250         }
251
252         if (!elm_layout_file_set(layout, EDJEFILE, GRP_GALLERY_LAYOUT)) {
253                 _ERR("failed to set layout file");
254                 goto err2;
255         }
256
257         ldata = _create_listmgr_data(priv);
258         if (!ldata) {
259                 _ERR("failed to create listmgr data");
260                 goto err2;
261         }
262
263         listmgr = listmgr_create(layout, (void *)ldata);
264         if (!listmgr) {
265                 _ERR("failed to create listmgr");
266                 goto err3;
267         }
268
269         md = mediadata_create(LIST_MEDIA_COND, E_SOURCE_ALL, E_SORT_DATE);
270         if (!md) {
271                 _ERR("failed to create mediadata");
272                 listmgr_destroy(listmgr);
273                 goto err3;
274         }
275
276         priv->base = base;
277         priv->layout = layout;
278         priv->lmgr = lmgr;
279         priv->listmgr = listmgr;
280         priv->ldata = ldata;
281         priv->md = md;
282
283         layoutmgr_set_layout_data(lmgr, LAYOUT_GALLERY, priv);
284
285         if (!listmgr_draw_list_area(listmgr)) {
286                 _ERR("failed to draw list area");
287                 mediadata_destroy(md);
288                 listmgr_destroy(listmgr);
289                 goto err3;
290         }
291
292         return true;
293
294 err3:
295         free(ldata);
296 err2:
297         evas_object_del(layout);
298 err:
299         free(priv);
300         return false;
301 }
302
303 static void _destroy(void *layout_data)
304 {
305         struct _priv *priv;
306
307         if (!layout_data) {
308                 _ERR("failed to get layout data");
309                 return;
310         }
311
312         priv = layout_data;
313
314         mediadata_free_list(priv->media_list);
315         mediadata_destroy(priv->md);
316
317         listmgr_destroy(priv->listmgr);
318         free(priv->ldata->gops);
319         free(priv->ldata);
320
321         evas_object_del(priv->layout);
322
323         free(priv);
324 }
325
326 static void _show(void *layout_data)
327 {
328         struct _priv *priv;
329
330         if (!layout_data) {
331                 _ERR("failed to layout data");
332                 return;
333         }
334
335         priv = layout_data;
336
337         evas_object_show(priv->layout);
338         elm_object_part_content_set(priv->base,
339                         PART_THUMBNAIL_AREA, priv->layout);
340 }
341
342 static void _hide(void *layout_data)
343 {
344         struct _priv *priv;
345
346         if (!layout_data) {
347                 _ERR("failed to get layout data");
348                 return;
349         }
350
351         priv = layout_data;
352
353         evas_object_hide(priv->layout);
354         elm_object_part_content_unset(priv->base, PART_THUMBNAIL_AREA);
355 }
356
357 static void _update(void *layout_data, int update_type, void *data)
358 {
359         struct _priv *priv;
360         int index;
361         bool update;
362
363         if (!layout_data) {
364                 _ERR("failed to get layout data");
365                 return;
366         }
367
368         priv = layout_data;
369
370         switch (update_type) {
371         case UPDATE_CONTENT:
372                 _update_list_area(priv);
373                 break;
374         case UPDATE_FOCUS:
375                 if (!data) {
376                         _ERR("invalid argument");
377                         return;
378                 }
379
380                 index = *(int *)data;
381
382                 if (priv->cur_index != index)
383                         update = true;
384                 else
385                         update = false;
386
387                 listmgr_update_focus_item(priv->listmgr, index, update);
388
389                 break;
390         default:
391                 break;
392         }
393 }
394
395 static layout_class _lclass = {
396         .layout_id = LAYOUT_GALLERY,
397         .create = _create,
398         .show = _show,
399         .hide = _hide,
400         .destroy = _destroy,
401         .update = _update,
402 };
403
404 layout_class *layout_gallery_get_lclass(void)
405 {
406         return &_lclass;
407 }