summaryrefslogtreecommitdiff
path: root/src/doc-data-util.c
blob: ac8e7be9ef334736c784275d7d381a238bb9c5b6 (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
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.org/license/
 *
 * 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 "doc-data-util.h"

/*
*  create a doc_item
*/
doc_item *doc_data_util_calloc_item(void)
{
	doc_item *ditem = (doc_item *) calloc(1, sizeof(doc_item));
	DOC_CHECK_NULL(ditem);
	return ditem;
}

/*
*   destroy a doc_item
*/
static int _doc_data_util_free_item(doc_item * ditem)
{
	if (ditem) {
		if (ditem->item) {
			doc_data_type_free_item((void **) &(ditem->item));
			ditem->item = NULL;
		}
		DOC_FREE(ditem);
	}
	return 0;
}

int doc_data_util_free_mtype_items(Eina_List ** elist)
{
	void *current = NULL;

	if (elist && *elist) {
		doc_dbg("Clear Mitems list.");
		EINA_LIST_FREE(*elist, current) {
			if (current) {
				doc_data_type_free_item((void **) &current);
				current = NULL;
			}
		}

		*elist = NULL;
	}

	return 0;
}

int doc_data_util_free_item(doc_item * item)
{
	DOC_CHECK_VAL(item, -1);

	_doc_data_util_free_item(item);
	return 0;
}

/*
*  Create a doc_item for selected item
*/
doc_sel_item_s *doc_data_util_new_sel_item(doc_item * ditem)
{
	DOC_CHECK_NULL(ditem);
	DOC_CHECK_NULL(ditem->item);
	doc_sel_item_s *item =
		(doc_sel_item_s *) calloc(1, sizeof(doc_sel_item_s));
	DOC_CHECK_NULL(item);
	item->uuid = strdup(ditem->item->uuid);
	item->file_url = strdup(ditem->item->file_url);
	item->store_type = ditem->store_type;
	item->sequence = ditem->sequence;
	return item;
}

/*
*  Free a doc_sel_item_s item
*/
int doc_data_util_free_sel_item(doc_sel_item_s * item)
{
	DOC_CHECK_VAL(item, -1);
	DOC_FREEIF(item->uuid);
	DOC_FREEIF(item->file_url);
	DOC_FREE(item);
	return 0;
}

int doc_data_util_create_filter(doc_filter_s * condition,
								filter_h * filter)
{
	DOC_CHECK_VAL(filter, -1);
	DOC_CHECK_VAL(condition, -1);
	int ret = -1;
	filter_h tmp_filter = NULL;

	ret = media_filter_create(&tmp_filter);
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Fail to create filter!");
		return -1;
	}

	if (strlen(condition->cond) > 0) {
		ret = media_filter_set_condition(tmp_filter, condition->cond,
										 condition->collate_type);
		if (ret != MEDIA_CONTENT_ERROR_NONE) {
			doc_dbgE("Fail to set condition!");
			goto DOC_DATA_UTIL_FAILED;
		}
	}

	if (strlen(condition->sort_keyword) > 0) {
		ret = media_filter_set_order(tmp_filter, condition->sort_type,
									 condition->sort_keyword,
									 condition->collate_type);
		if (ret != MEDIA_CONTENT_ERROR_NONE) {
			doc_dbgE("Fail to set order!");
			goto DOC_DATA_UTIL_FAILED;
		}
	}

	doc_dbg("offset: %d, count: %d", condition->offset, condition->count);
	if (condition->offset != -1) {
		ret = media_filter_set_offset(tmp_filter, condition->offset,
									  condition->count);
		if (ret != MEDIA_CONTENT_ERROR_NONE) {
			doc_dbgE("Fail to set offset!");
			goto DOC_DATA_UTIL_FAILED;
		}
	}

	*filter = tmp_filter;
	return 0;

DOC_DATA_UTIL_FAILED:

	if (tmp_filter) {
		media_filter_destroy(tmp_filter);
		tmp_filter = NULL;
		*filter = NULL;
	}
	return -1;
}

int doc_data_util_destroy_filter(filter_h filter)
{
	DOC_CHECK_VAL(filter, -1);

	if (media_filter_destroy(filter) != MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Failed to destroy filter!");
		return -1;
	}

	return 0;
}

bool doc_data_util_clone_media(media_info_h media, doc_media_s ** pitem,
							   bool b_meta)
{
	DOC_CHECK_FALSE(pitem);
	doc_media_s *item = NULL;

	*pitem = NULL;

	item = (doc_media_s *) calloc(1, sizeof(doc_media_s));
	DOC_CHECK_FALSE(item);
	item->gtype = DOC_TYPE_MEDIA;

	if (media_info_clone(&(item->media_h), media) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Clone media handle error");
		goto DOC_DATA_UTIL_FAILED;
	}
	if (media_info_get_media_id(media, &(item->uuid)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get media id error");
		goto DOC_DATA_UTIL_FAILED;
	}
	if (media_info_get_display_name(media, &(item->display_name)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get media display name error");
		goto DOC_DATA_UTIL_FAILED;
	}
	if (media_info_get_file_path(media, &(item->file_url)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get media file path error");
		goto DOC_DATA_UTIL_FAILED;
	}
	if (media_info_get_media_type
		(media,
		 (media_content_type_e *) & (item->type)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get media type error");
		goto DOC_DATA_UTIL_FAILED;
	}
	if (media_info_get_thumbnail_path(media, &(item->thumb_url)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get media thumbnail path error");
		goto DOC_DATA_UTIL_FAILED;
	}
	doc_sdbg("thumb_url: %s", item->thumb_url);

	if (media_info_get_timeline(media, &(item->mtime)) !=
		MEDIA_CONTENT_ERROR_NONE) {
		doc_dbgE("Get media modified time error");
		goto DOC_DATA_UTIL_FAILED;
	}
	/*  if (media_info_get_mode(media, (media_content_mode_e *)&(item->mode)) != MEDIA_CONTENT_ERROR_NONE) {
	   doc_dbgE("Get media mode failed!");
	   goto DOC_DATA_UTIL_FAILED;
	   } */

	char* root_directory_path;
	if (storage_get_root_directory(0, &root_directory_path) == STORAGE_ERROR_NONE) // '0' is root path of media storage.
	{
		if (strncmp(item->file_url, root_directory_path, strlen(root_directory_path)) == 0) /* The device's internal storage */
		{
			item->storage_type = DOC_PHONE;
		}
		else /* The device's external storage */
		{
			item->storage_type = DOC_MMC;
		}
	}
	else
	{
		doc_dbgE("Get storage type failed!");
		goto DOC_DATA_UTIL_FAILED;
	}
	DOC_FREEIF(root_directory_path);

	*pitem = item;
	return true;

DOC_DATA_UTIL_FAILED:

	doc_data_type_free_item((void **) (&item));
	return false;
}