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