summaryrefslogtreecommitdiff
path: root/src/data/data_photo.c
blob: 1e577d98cf4634042469b2535289a29fff55538a (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
/*
 * 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 <app_debug.h>
#include <Eina.h>
#include <media_content.h>
#include <app_contents.h>

#include "data_photo.h"
#include "datamgr.h"
#include "defs.h"

#define MEDIA_CONDITION_IMAGE "MEDIA_TYPE=0"

static bool _get_media(media_info_h m, void *data)
{
	int r;
	char *thumb, *mediaid;
	Eina_List **list;
	struct datamgr_item *di;
	bool favorite;

	if (!m) {
		_ERR("Invalid argument");
		return false;
	}

	list = data;

	r = media_info_get_media_id(m, &mediaid);
	if (r != MEDIA_CONTENT_ERROR_NONE || !mediaid) {
		_ERR("failed to get media id");
		return false;
	}

	r = media_info_get_thumbnail_path(m, &thumb);
	if (r != MEDIA_CONTENT_ERROR_NONE || !thumb) {
		_ERR("failed to get thumb");
		free(mediaid);
		return true;
	}

	favorite = false;
	r = app_contents_favorite_check(CONTENTS_PHOTO, mediaid, &favorite);
	if (r != APP_CONTENTS_ERROR_NONE)
		_ERR("failed to get favorite");

	di = calloc(1, sizeof(*di));
	if (!di) {
		_ERR("failed to calloc datamgr item");
		free(mediaid);
		free(thumb);
		return false;
	}
	di->icon = strdup(thumb);
	if (favorite)
		di->focus_icon = strdup(IMAGE_PHOTO_FAVORITE);

	*list = eina_list_append(*list, di);

	free(mediaid);
	free(thumb);

	return true;
}

static void _unload_photo(struct datamgr *dm)
{
	struct datamgr_item *di;

	EINA_LIST_FREE(dm->list, di) {
		free(di->icon);
		free(di->focus_icon);

		free(di);
	}

	dm->list = NULL;
}

static Eina_List *_get_items(struct datamgr *dm)
{
	filter_h f;
	int r;

	if (!dm) {
		_ERR("Invalid argument");
		return NULL;
	}

	_unload_photo(dm);

	r = media_filter_create(&f);
	if (r != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to created filter");
		return NULL;
	}

	media_filter_set_condition(f, MEDIA_CONDITION_IMAGE,
			MEDIA_CONTENT_COLLATE_DEFAULT);

	r = media_info_foreach_media_from_db(f,
			_get_media, &dm->list);
	if (r != MEDIA_CONTENT_ERROR_NONE)
		_ERR("folder foreach failed");

	media_filter_destroy(f);


	return dm->list;
}

static bool _init(struct datamgr *dm)
{
	return true;
}

static void _fini(struct datamgr *dm)
{
	if (!dm) {
		_ERR("Invalid argument");
		return;
	}

	_unload_photo(dm);
}

static struct data_class dclass = {
	.init = _init,
	.fini = _fini,
	.get_items = _get_items
};

struct data_class *datamgr_photo_get_dclass(void)
{
	return &dclass;
}