summaryrefslogtreecommitdiff
path: root/src/layout/gallery.c
blob: 7c6aad99da361c1344c31c2a8fc0e6c89413726e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <Elementary.h>
#include <media_content.h>
#include <app_debug.h>
#include <app_media.h>
#include <gridmgr.h>
#include <inputmgr.h>
#include <layoutmgr.h>
#include <viewmgr.h>

#include "define.h"
#include "layout.h"
#include "view.h"
#include "data/datamgr.h"
#include "grid/grid.h"
#include "util/listmgr.h"
#include "util/util.h"
#include "util/locmgr.h"

#define TEXT_NOCONTENT "No Photo & Video"

#define GRID_PADDING 6
#define GRID_ITEM_X (200 + GRID_PADDING)
#define GRID_ITEM_Y (200 + GRID_PADDING)
#define GRID_NUM_ITEM 3

#define BOX_PADDING (62 - GRID_PADDING)

#define JOB_INTERVAL 1.0

struct _priv {
	Evas_Object *base;
	Evas_Object *layout;
	Evas_Object *menu_btn;
	Evas_Object *view_btn;

	layoutmgr *lmgr;

	struct listmgr *listmgr;
	struct listmgr_data *ldata;

	struct datamgr *dmgr[E_DATA_MAX];

	Eina_List *media_list;

	int view_mode;
	int source_type;

	struct grid_data *gdata;

	struct locmgr *locmgr;
};

static void _recent_item_selected(struct _priv *priv, app_media *am)
{
	app_media_info *info;
	struct view_update_data vdata;
	struct datamgr *dmgr;

	info = app_media_get_info(am);
	if (!info) {
		_ERR("failed to get app media info");
		return;
	}

	dmgr = priv->dmgr[E_DATA_MEDIA];
	if (!dmgr)
		return;

	vdata.list = dmgr->ops->get_list(dmgr->handle, E_LIST_MEDIA, NULL);
	vdata.index = util_get_media_index_from_id(vdata.list, info->media_id);
	vdata.id = VIEW_BASE;

	viewmgr_update_view(VIEW_VIEWER, UPDATE_CONTENT, &vdata);
	viewmgr_push_view(VIEW_VIEWER);
}

static struct listmgr_data *_create_listmgr_data(struct _priv *priv)
{
	struct listmgr_data *data;

	data = calloc(1, sizeof(*data));
	if (!data) {
		_ERR("failed to allocate memory");
		return NULL;
	}

	data->menu_btn = priv->menu_btn;
	data->view_btn = priv->view_btn;

	return data;
}

static void _update_bottom_area(struct _priv *priv)
{
	struct datamgr *dmgr;
	char buf[128];

	/* update view mode button state */
	if (priv->media_list)
		elm_object_disabled_set(priv->view_btn, EINA_FALSE);
	else
		elm_object_disabled_set(priv->view_btn, EINA_TRUE);

	/* update content info */
	dmgr = priv->dmgr[E_DATA_MEDIA];
	if (!dmgr)
		return;

	priv->gdata->get_item_info(dmgr, priv->media_list, buf, sizeof(buf));

	elm_object_part_text_set(priv->base, PART_BASE_CONTENT_INFO, buf);
}

static void _clear_content_list(struct _priv *priv)
{
	if (priv->media_list) {
		priv->gdata->free_item_list(priv->dmgr[E_DATA_MEDIA],
				priv->media_list);
		priv->media_list = NULL;
	}

	elm_object_part_text_set(priv->layout, PART_NOCONTENT, "");

	listmgr_clear_content_list(priv->listmgr);
}

static void _update_content_list(struct _priv *priv)
{
	if (priv->media_list) {
		_update_bottom_area(priv);
		return;
	}

	priv->gdata = get_gallery_grid_data(priv->view_mode);
	if (!priv->gdata) {
		elm_object_part_text_set(priv->layout,
				PART_NOCONTENT, TEXT_NOCONTENT);
		return;
	}

	priv->media_list = priv->gdata->get_item_list(priv->dmgr[E_DATA_MEDIA]);

	_update_bottom_area(priv);

	if (!priv->media_list) {
		elm_object_part_text_set(priv->layout,
				PART_NOCONTENT, TEXT_NOCONTENT);
		return;
	}

	priv->gdata->data = priv->dmgr[E_DATA_MEDIA];

	if (!listmgr_update_content_list(priv->listmgr,
				priv->media_list, priv->gdata))
		_ERR("failed to update list area");
}

static void _destroy_datamgr(struct _priv *priv)
{
	int i;

	for (i = 0; i < E_DATA_MAX; i++)
		datamgr_destroy(priv->dmgr[i]);
}

static void _destroy_utils(struct _priv *priv)
{
	locmgr_destroy(priv->locmgr);
	_destroy_datamgr(priv);

	listmgr_destroy(priv->listmgr);
	free(priv->ldata);
}

static Eina_Bool _create_location(void *data)
{
	struct _priv *priv;
	struct locmgr *locmgr;
	struct datamgr *dmgr;

	if (!data) {
		_ERR("invalid argument");
		return ECORE_CALLBACK_CANCEL;
	}

	priv = data;

	dmgr = priv->dmgr[E_DATA_MEDIA];
	if (!dmgr) {
		_ERR("failed to get datamgr");
		return ECORE_CALLBACK_CANCEL;
	}

	locmgr = locmgr_create();
	if (!locmgr) {
		_ERR("failed to create locmgr");
		return ECORE_CALLBACK_CANCEL;
	}

	priv->locmgr = locmgr;

	locmgr_set_locations(locmgr,
			dmgr->ops->get_list(dmgr->handle, E_LIST_MEDIA, NULL));

	return ECORE_CALLBACK_CANCEL;
}

static bool _create_utils(struct _priv *priv)
{
	struct listmgr *listmgr;
	struct listmgr_data *ldata;
	struct datamgr *dmgr;
	int i;

	for (i = 0; i < E_DATA_MAX; i++) {
		dmgr = datamgr_create(i, GALLERY_MEDIA_COND, E_SOURCE_ALL);
		if (!dmgr) {
			_ERR("failed to create datamgr");
			_destroy_datamgr(priv);
			return false;
		}

		priv->dmgr[i] = dmgr;
	}

	ldata = _create_listmgr_data(priv);
	if (!ldata) {
		_ERR("failed to create listmgr data");
		_destroy_datamgr(priv);
		return false;
	}

	listmgr = listmgr_create(priv->layout, (void *)ldata);
	if (!listmgr) {
		_ERR("failed to create listmgr");
		_destroy_datamgr(priv);
		free(ldata);
		return false;
	}

	priv->ldata = ldata;
	priv->listmgr = listmgr;

	return true;
}

static bool _create(layoutmgr *lmgr, void *data)
{
	struct layout_data *ld;
	struct _priv *priv;
	Evas_Object *base, *layout;

	if (!lmgr) {
		_ERR("failed to get layoutmgr");
		return false;
	}

	if (!data) {
		_ERR("invalid argument");
		return false;
	}

	ld = data;

	priv = calloc(1, sizeof(*priv));
	if (!priv) {
		_ERR("failed to allocate priv");
		return false;
	}

	base = layoutmgr_get_base(lmgr);
	if (!base) {
		_ERR("failed to get base object");
		goto err;
	}

	layout = elm_layout_add(base);
	if (!layout) {
		_ERR("failed to create layout");
		goto err;
	}

	if (!elm_layout_file_set(layout, EDJEFILE, GRP_GALLERY_LAYOUT)) {
		_ERR("failed to set layout file");
		goto err2;
	}

	priv->base = base;
	priv->layout = layout;
	priv->menu_btn = ld->top;
	priv->view_btn = ld->bottom;
	priv->lmgr = lmgr;
	priv->gdata = get_gallery_grid_data(E_GALLERY_EVENT);

	if (!_create_utils(priv)) {
		_ERR("failed to create utils");
		goto err2;
	}

	layoutmgr_set_layout_data(lmgr, LAYOUT_GALLERY, priv);

	if (!listmgr_draw_list_area(priv->listmgr)) {
		_ERR("failed to draw list area");
		goto err3;
	}

	/* add job for set location tag */
	ecore_timer_add(JOB_INTERVAL, _create_location, priv);

	return true;

err3:
	_destroy_utils(priv);
err2:
	evas_object_del(layout);
err:
	free(priv);
	return false;
}

static void _destroy(void *layout_data)
{
	struct _priv *priv;

	if (!layout_data) {
		_ERR("failed to get layout data");
		return;
	}

	priv = layout_data;

	priv->gdata->free_item_list(priv->dmgr[E_DATA_MEDIA], priv->media_list);

	_destroy_utils(priv);

	evas_object_del(priv->layout);

	free(priv);
}

static void _show(void *layout_data)
{
	struct _priv *priv;

	if (!layout_data) {
		_ERR("failed to layout data");
		return;
	}

	priv = layout_data;

	evas_object_show(priv->layout);
	elm_object_part_content_set(priv->base,
			PART_BASE_THUMBNAIL_AREA, priv->layout);
}

static void _hide(void *layout_data)
{
	struct _priv *priv;

	if (!layout_data) {
		_ERR("failed to get layout data");
		return;
	}

	priv = layout_data;

	evas_object_hide(priv->layout);
	elm_object_part_content_unset(priv->base, PART_BASE_THUMBNAIL_AREA);
}

static void _update(void *layout_data, int update_type, void *data)
{
	struct view_update_data *vdata;
	struct _priv *priv;
	struct datamgr *dmgr;
	int mode;

	if (!layout_data) {
		_ERR("failed to get layout data");
		return;
	}

	priv = layout_data;

	switch (update_type) {
	case UPDATE_CONTENT:
		_update_content_list(priv);
		break;
	case UPDATE_CONTENT_ITEM:
		listmgr_update_content_item(priv->listmgr);
		break;
	case UPDATE_FOCUS:
		if (!data) {
			_ERR("invalid argument");
			return;
		}

		vdata = data;

		listmgr_focus_content_list(priv->listmgr, vdata->id);
		break;
	case UPDATE_BACK:
		listmgr_focus_content_list(priv->listmgr, NULL);
		break;
	case UPDATE_RECENT:
		if (!data) {
			_ERR("invalid argument");
			return;
		}

		_update_content_list(priv);
		_recent_item_selected(priv, data);
		break;
	case UPDATE_FOCUS_STATE:
		listmgr_set_focus_state(priv->listmgr, EINA_FALSE);
		break;
	case UPDATE_VIEW_MODE:
		if (!data) {
			_ERR("invalid argument");
			return;
		}

		vdata = data;
		mode = vdata->index;

		/* Folder view mode will be implemented later. */
		if (mode == E_GALLERY_FOLDER)
			return;

		if (mode == priv->view_mode)
			return;

		_clear_content_list(priv);

		priv->view_mode = mode;
		_update_content_list(priv);
		break;
	case UPDATE_SOURCE_TYPE:
		if (!data) {
			_ERR("invalid argument");
			return;
		}

		vdata = data;
		mode = vdata->index;

		if (mode == priv->source_type)
			return;

		priv->source_type = mode;

		dmgr = priv->dmgr[E_DATA_MEDIA];
		dmgr->ops->set_source(dmgr->handle, mode);

		_clear_content_list(priv);

		_update_content_list(priv);
		break;
	default:
		break;
	}
}

static layout_class _lclass = {
	.layout_id = LAYOUT_GALLERY,
	.create = _create,
	.show = _show,
	.hide = _hide,
	.destroy = _destroy,
	.update = _update,
};

layout_class *layout_gallery_get_lclass(void)
{
	return &_lclass;
}