2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <media_info_private.h>
19 #include <media_util_private.h>
22 static bool __is_pinyin_needed(void)
25 const char *china = "zh_CN";
26 const char *hongkong = "zh_HK";
30 bool pinyin_support = FALSE;
31 media_svc_check_pinyin_support(&pinyin_support);
33 /*Check Language Setting*/
34 lang = vconf_get_str(VCONFKEY_LANGSET);
35 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
37 if ((strncmp(china, lang, strlen(china)) == 0) ||
38 (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
48 static const char *__get_order_str(media_content_order_e order_enum)
51 case MEDIA_CONTENT_ORDER_ASC:
53 case MEDIA_CONTENT_ORDER_DESC:
60 static const char *__get_collate_str(media_content_collation_e collate_type)
62 switch (collate_type) {
63 case MEDIA_CONTENT_COLLATE_NOCASE:
64 return " COLLATE NOCASE ";
65 case MEDIA_CONTENT_COLLATE_RTRIM:
66 return " COLLATE RTRIM ";
67 case MEDIA_CONTENT_COLLATE_LOCALIZED:
68 if (__is_pinyin_needed())
69 return " COLLATE NOCASE ";
71 return " COLLATE localized ";
77 static bool __check_collate_type(media_content_collation_e collate_type)
79 switch (collate_type) {
80 case MEDIA_CONTENT_COLLATE_DEFAULT:
81 case MEDIA_CONTENT_COLLATE_NOCASE:
82 case MEDIA_CONTENT_COLLATE_RTRIM:
83 case MEDIA_CONTENT_COLLATE_LOCALIZED:
90 int _media_filter_build_condition(bool is_full, const char *condition, media_content_collation_e collate_type, char **result)
92 media_content_retvm_if(result == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid out param");
93 media_content_retvm_if(!STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
94 media_content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition collate");
97 *result = g_strdup_printf("(%s)", condition);
99 *result = g_strdup_printf("(%s%s)", condition, __get_collate_str(collate_type));
101 media_content_sec_debug("Condition : %s", *result);
103 return MEDIA_CONTENT_ERROR_NONE;
106 int _media_filter_build_option(filter_h filter, char **result)
108 filter_s *_filter = (filter_s *)filter;
110 media_content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
111 media_content_retvm_if(!result, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid out param");
113 if (!STRING_VALID(_filter->order_keyword)) {
114 *result = g_strdup_printf(" LIMIT %d, %d", _filter->offset, _filter->count);
115 return MEDIA_CONTENT_ERROR_NONE;
118 if (_filter->is_full_order) {
119 *result = g_strdup_printf("ORDER BY %s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
120 return MEDIA_CONTENT_ERROR_NONE;
123 if (_filter->order_type == MEDIA_CONTENT_ORDER_ASC || _filter->order_type == MEDIA_CONTENT_ORDER_DESC) {
124 *result = g_strdup_printf("ORDER BY %s%s%s LIMIT %d, %d",
125 _filter->order_keyword,
126 __get_collate_str(_filter->order_collate_type),
127 __get_order_str(_filter->order_type),
131 *result = g_strdup_printf("%s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
134 return MEDIA_CONTENT_ERROR_NONE;
137 int media_filter_create(filter_h *filter)
139 int ret = MEDIA_CONTENT_ERROR_NONE;
141 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
143 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
144 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
146 _filter->storage_id = NULL;
147 _filter->condition = NULL;
148 _filter->order_keyword = NULL;
149 _filter->order_type = -1;
150 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
151 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
152 _filter->offset = -1;
154 _filter->is_full_condition = false;
155 _filter->is_full_order = false;
157 *filter = (filter_h)_filter;
162 int media_filter_destroy(filter_h filter)
164 int ret = MEDIA_CONTENT_ERROR_NONE;
165 filter_s *_filter = (filter_s*)filter;
168 SAFE_FREE(_filter->storage_id);
169 SAFE_FREE(_filter->condition);
170 SAFE_FREE(_filter->order_keyword);
173 ret = MEDIA_CONTENT_ERROR_NONE;
175 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
176 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
182 int media_filter_set_offset(filter_h filter, int offset, int count)
184 int ret = MEDIA_CONTENT_ERROR_NONE;
185 filter_s *_filter = (filter_s*)filter;
187 if (_filter != NULL) {
188 _filter->offset = offset;
189 _filter->count = count;
191 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
192 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
198 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
200 int ret = MEDIA_CONTENT_ERROR_NONE;
201 filter_s *_filter = (filter_s*)filter;
203 if ((_filter != NULL) && STRING_VALID(condition)
204 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
206 _filter->is_full_condition = false;
208 if (STRING_VALID(_filter->condition))
209 SAFE_FREE(_filter->condition);
211 char new_condition[MAX_QUERY_SIZE] = {0, };
212 memset(new_condition, 0, sizeof(new_condition));
213 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
214 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
216 _filter->condition = strdup(new_condition);
217 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
219 media_content_sec_debug("Condition string : %s", _filter->condition);
221 _filter->condition_collate_type = collate_type;
223 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
224 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
230 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
232 int ret = MEDIA_CONTENT_ERROR_NONE;
233 filter_s *_filter = (filter_s*)filter;
235 if ((_filter != NULL) && STRING_VALID(order_keyword)
236 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
237 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
239 _filter->is_full_order = false;
241 SAFE_FREE(_filter->order_keyword);
243 _filter->order_keyword = strdup(order_keyword);
244 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
246 _filter->order_type = order_type;
247 _filter->order_collate_type = collate_type;
249 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
250 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
256 int media_filter_set_storage(filter_h filter, const char *storage_id)
258 int ret = MEDIA_CONTENT_ERROR_NONE;
259 media_content_warn("DEPRECATION WARNING: media_filter_set_storage() is deprecated and will be removed from next release. Use media_filter_set_condition() with MEDIA_PATH keyword instead.");
260 filter_s *_filter = (filter_s*)filter;
262 if ((_filter != NULL) && STRING_VALID(storage_id)) {
263 if (STRING_VALID(_filter->storage_id))
264 SAFE_FREE(_filter->storage_id);
266 _filter->storage_id = strdup(storage_id);
267 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
269 media_content_sec_debug("storage_id : %s", _filter->storage_id);
271 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
272 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
278 int media_filter_get_offset(filter_h filter, int *offset, int *count)
280 int ret = MEDIA_CONTENT_ERROR_NONE;
281 filter_s *_filter = (filter_s*)filter;
284 *offset = _filter->offset;
285 *count = _filter->count;
287 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
288 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
294 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
296 int ret = MEDIA_CONTENT_ERROR_NONE;
297 filter_s *_filter = (filter_s*)filter;
300 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == false) {
301 char new_condition[MAX_QUERY_SIZE] = {0, };
302 memset(new_condition, 0, sizeof(new_condition));
303 ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
304 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
306 *condition = strdup(new_condition);
307 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
312 *collate_type = _filter->condition_collate_type;
314 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
315 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
321 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
323 int ret = MEDIA_CONTENT_ERROR_NONE;
324 filter_s *_filter = (filter_s*)filter;
327 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == false) {
328 *order_keyword = strdup(_filter->order_keyword);
329 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
331 *order_keyword = NULL;
334 *order_type = _filter->order_type;
335 *collate_type = _filter->order_collate_type;
337 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
338 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
344 int media_filter_get_storage(filter_h filter, char **storage_id)
346 int ret = MEDIA_CONTENT_ERROR_NONE;
347 media_content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
348 filter_s *_filter = (filter_s*)filter;
351 if (STRING_VALID(_filter->storage_id)) {
352 *storage_id = strdup(_filter->storage_id);
353 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
358 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
359 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
365 int media_filter_set_condition_v2(filter_h filter, const char *condition)
367 int ret = MEDIA_CONTENT_ERROR_NONE;
368 filter_s *_filter = (filter_s*)filter;
370 if ((_filter != NULL) && STRING_VALID(condition)) {
371 _filter->is_full_condition = true;
373 if (STRING_VALID(_filter->condition))
374 SAFE_FREE(_filter->condition);
376 char new_condition[MAX_QUERY_SIZE] = {0, };
377 memset(new_condition, 0, sizeof(new_condition));
378 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
379 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
381 _filter->condition = strdup(new_condition);
382 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
384 media_content_sec_debug("Condition string : %s", _filter->condition);
386 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
387 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
393 int media_filter_get_condition_v2(filter_h filter, char **condition)
395 int ret = MEDIA_CONTENT_ERROR_NONE;
396 filter_s *_filter = (filter_s*)filter;
399 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == true) {
400 char new_condition[MAX_QUERY_SIZE] = {0, };
401 memset(new_condition, 0, sizeof(new_condition));
402 ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
403 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
405 *condition = strdup(new_condition);
406 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
411 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
412 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
418 int media_filter_set_order_v2(filter_h filter, const char *order)
420 int ret = MEDIA_CONTENT_ERROR_NONE;
421 filter_s *_filter = (filter_s*)filter;
423 if ((_filter != NULL) && STRING_VALID(order)) {
424 _filter->is_full_order = true;
426 SAFE_FREE(_filter->order_keyword);
428 _filter->order_keyword = strdup(order);
429 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
431 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
432 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
438 int media_filter_get_order_v2(filter_h filter, char **order)
440 int ret = MEDIA_CONTENT_ERROR_NONE;
441 filter_s *_filter = (filter_s*)filter;
444 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == true) {
445 *order = strdup(_filter->order_keyword);
446 media_content_retvm_if(*order == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
451 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
452 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;