listmgr: add callback functions for grid 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 media_type=1"
32
33 #define TEXT_NOCONTENT "No Photo & Video"
34
35 #define GRID_ITEM_X 206
36 #define GRID_ITEM_Y 206
37 #define GRID_NUM_ITEM 3
38
39 #define BOX_PADDING 62
40
41 struct _priv {
42         Evas_Object *base;
43         Evas_Object *layout;
44
45         layoutmgr *lmgr;
46
47         struct listmgr *listmgr;
48
49         struct mediadata *md;
50
51         Eina_List *media_list;
52 };
53
54 static Evas_Object *_grid_content_get(void *data,
55                         Evas_Object *obj, const char *part)
56 {
57         Evas_Object *image;
58         app_media *am;
59         app_media_info *info;
60
61         if (!data)
62                 return NULL;
63
64         am = data;
65         info = app_media_get_info(am);
66         if (!info) {
67                 _ERR("failed to get media info");
68                 return NULL;
69         }
70
71         image = NULL;
72         if (!strcmp(part, PART_ELM_SWALLOW_THUMBNAIL)) {
73                 image = util_add_image(obj, info->thumbnail_path);
74                 if (!image) {
75                         _ERR("failed to create image object");
76                         return NULL;
77                 }
78
79                 evas_object_show(image);
80         } else if (!strcmp(part, PART_ELM_SWALLOW_VIDEO)) {
81                 if (info->media_type == MEDIA_CONTENT_TYPE_VIDEO) {
82                         image = util_add_image(obj, IMAGE_THUMBNAIL_PLAY);
83                         if (!image) {
84                                 _ERR("failed to create image object");
85                                 return NULL;
86                         }
87
88                         evas_object_show(image);
89                 }
90         }
91
92         return image;
93 }
94
95 static struct grid_class _gclass = {
96         .item_style = STYLE_GRID_GALLERY_ITEM,
97         .content_get = _grid_content_get
98 };
99
100 static void _grid_selected_cb(void *data, Elm_Object_Item *it)
101 {
102         app_media *am;
103         struct view_data *vdata;
104         struct _priv *priv;
105
106         if (!data || !it) {
107                 _ERR("invalid argument");
108                 return;
109         }
110
111         priv = data;
112
113         am = elm_object_item_data_get(it);
114         if (!am) {
115                 _ERR("failed to get app media");
116                 return;
117         }
118
119         vdata = calloc(1, sizeof(*vdata));
120         if (!vdata) {
121                 _ERR("failed to allocate view data");
122                 return;
123         }
124
125         vdata->list = mediadata_get_medialist(priv->md);
126         vdata->index = util_get_media_index(vdata->list, am);
127
128         viewmgr_update_view(VIEW_VIEWER, UPDATE_CONTENT, vdata);
129         viewmgr_push_view(VIEW_VIEWER);
130 }
131
132 static struct listmgr_data *_create_listmgr_data(struct _priv *priv)
133 {
134         struct listmgr_data *data;
135
136         data = calloc(1, sizeof(*data));
137         if (!data) {
138                 _ERR("failed to allocate listmgr data");
139                 return NULL;
140         }
141
142         data->grid_item_x = GRID_ITEM_X;
143         data->grid_item_y = GRID_ITEM_Y;
144         data->grid_num_item = GRID_NUM_ITEM;
145         data->box_padding = BOX_PADDING;
146
147         data->gclass = &_gclass;
148
149         data->grid_selected_cb = _grid_selected_cb;
150         data->cb_data = priv;
151
152         return data;
153 }
154
155 static void _update_list_area(struct _priv *priv)
156 {
157         Eina_List *list;
158
159         if (priv->media_list)
160                 return;
161
162         list = mediadata_get_list(priv->md, E_LIST_DATE);
163         if (!list) {
164                 elm_object_part_text_set(priv->layout,
165                                 PART_NOCONTENT, TEXT_NOCONTENT);
166                 return;
167         }
168
169         if (!listmgr_update_list_area(priv->listmgr, list))
170                 _ERR("failed to update list area");
171
172         priv->media_list = list;
173 }
174
175 static bool _create(layoutmgr *lmgr, void *data)
176 {
177         struct listmgr *listmgr;
178         struct listmgr_data *ldata;
179         struct mediadata *md;
180         struct _priv *priv;
181         Evas_Object *base, *layout;
182
183         if (!lmgr) {
184                 _ERR("failed to get layoutmgr");
185                 return false;
186         }
187
188         priv = calloc(1, sizeof(*priv));
189         if (!priv) {
190                 _ERR("failed to allocate priv");
191                 return false;
192         }
193
194         base = layoutmgr_get_base(lmgr);
195         if (!base) {
196                 _ERR("failed to get base object");
197                 goto err;
198         }
199
200         layout = elm_layout_add(base);
201         if (!layout) {
202                 _ERR("failed to create layout");
203                 goto err;
204         }
205
206         if (!elm_layout_file_set(layout, EDJEFILE, GRP_GALLERY_LAYOUT)) {
207                 _ERR("failed to set layout file");
208                 goto err2;
209         }
210
211         ldata = _create_listmgr_data(priv);
212         if (!ldata) {
213                 _ERR("failed to create listmgr data");
214                 goto err2;
215         }
216
217         listmgr = listmgr_create(layout, (void *)ldata);
218         if (!listmgr) {
219                 _ERR("failed to create listmgr");
220                 goto err3;
221         }
222
223         md = mediadata_create(LIST_MEDIA_COND, E_SOURCE_ALL, E_SORT_DATE);
224         if (!md) {
225                 _ERR("failed to create mediadata");
226                 listmgr_destroy(listmgr);
227                 goto err3;
228         }
229
230         priv->base = base;
231         priv->layout = layout;
232         priv->lmgr = lmgr;
233         priv->listmgr = listmgr;
234         priv->md = md;
235
236         layoutmgr_set_layout_data(lmgr, LAYOUT_GALLERY, priv);
237
238         if (!listmgr_draw_list_area(listmgr)) {
239                 _ERR("failed to draw list area");
240                 mediadata_destroy(md);
241                 listmgr_destroy(listmgr);
242                 goto err3;
243         }
244
245         return true;
246
247 err3:
248         free(ldata);
249 err2:
250         evas_object_del(layout);
251 err:
252         free(priv);
253         return false;
254 }
255
256 static void _destroy(void *layout_data)
257 {
258         struct _priv *priv;
259
260         if (!layout_data) {
261                 _ERR("failed to get layout data");
262                 return;
263         }
264
265         priv = layout_data;
266
267         mediadata_free_list(priv->media_list);
268         mediadata_destroy(priv->md);
269
270         listmgr_destroy(priv->listmgr);
271
272         evas_object_del(priv->layout);
273         free(priv);
274 }
275
276 static void _show(void *layout_data)
277 {
278         struct _priv *priv;
279
280         if (!layout_data) {
281                 _ERR("failed to layout data");
282                 return;
283         }
284
285         priv = layout_data;
286
287         evas_object_show(priv->layout);
288         elm_object_part_content_set(priv->base,
289                         PART_THUMBNAIL_AREA, priv->layout);
290 }
291
292 static void _hide(void *layout_data)
293 {
294         struct _priv *priv;
295
296         if (!layout_data) {
297                 _ERR("failed to get layout data");
298                 return;
299         }
300
301         priv = layout_data;
302
303         evas_object_hide(priv->layout);
304         elm_object_part_content_unset(priv->base, PART_THUMBNAIL_AREA);
305 }
306
307 static void _update(void *layout_data, int update_type, void *data)
308 {
309         struct _priv *priv;
310
311         if (!layout_data) {
312                 _ERR("failed to get layout data");
313                 return;
314         }
315
316         priv = layout_data;
317
318         _update_list_area(priv);
319 }
320
321 static layout_class _lclass = {
322         .layout_id = LAYOUT_GALLERY,
323         .create = _create,
324         .show = _show,
325         .hide = _hide,
326         .destroy = _destroy,
327         .update = _update,
328 };
329
330 layout_class *layout_gallery_get_lclass(void)
331 {
332         return &_lclass;
333 }