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 _filter->condition = _media_content_replace_path_in_condition(condition);
214 If an error is occured in _media_content_replace_path_in_condition(),
215 A suitable return value is 'MEDIA_CONTENT_ERROR_INVALID_OPERATION'.
216 However, it is not stated in the description of media_filter_set_condition().
217 Therefore, use 'MEDIA_CONTENT_ERROR_OUT_OF_MEMORY' temporarily.
218 It will be modified after removing _media_content_replace_path_in_condition() function.
220 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to _media_content_replace_path_in_condition");
222 media_content_sec_debug("Condition string : %s", _filter->condition);
224 _filter->condition_collate_type = collate_type;
226 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
227 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
233 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
235 int ret = MEDIA_CONTENT_ERROR_NONE;
236 filter_s *_filter = (filter_s *)filter;
238 if ((_filter != NULL) && STRING_VALID(order_keyword)
239 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
240 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
242 _filter->is_full_order = false;
244 SAFE_FREE(_filter->order_keyword);
246 _filter->order_keyword = strdup(order_keyword);
247 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
249 _filter->order_type = order_type;
250 _filter->order_collate_type = collate_type;
252 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
253 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
259 int media_filter_set_storage(filter_h filter, const char *storage_id)
261 int ret = MEDIA_CONTENT_ERROR_NONE;
262 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.");
263 filter_s *_filter = (filter_s *)filter;
265 if ((_filter != NULL) && STRING_VALID(storage_id)) {
266 if (STRING_VALID(_filter->storage_id))
267 SAFE_FREE(_filter->storage_id);
269 _filter->storage_id = strdup(storage_id);
270 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
272 media_content_sec_debug("storage_id : %s", _filter->storage_id);
274 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
275 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
281 int media_filter_get_offset(filter_h filter, int *offset, int *count)
283 int ret = MEDIA_CONTENT_ERROR_NONE;
284 filter_s *_filter = (filter_s *)filter;
287 *offset = _filter->offset;
288 *count = _filter->count;
290 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
291 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
297 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
299 int ret = MEDIA_CONTENT_ERROR_NONE;
300 filter_s *_filter = (filter_s *)filter;
303 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == false) {
304 *condition = strdup(_filter->condition);
305 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
310 *collate_type = _filter->condition_collate_type;
312 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
313 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
319 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
321 int ret = MEDIA_CONTENT_ERROR_NONE;
322 filter_s *_filter = (filter_s *)filter;
325 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == false) {
326 *order_keyword = strdup(_filter->order_keyword);
327 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
329 *order_keyword = NULL;
332 *order_type = _filter->order_type;
333 *collate_type = _filter->order_collate_type;
335 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
336 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
342 int media_filter_get_storage(filter_h filter, char **storage_id)
344 int ret = MEDIA_CONTENT_ERROR_NONE;
345 media_content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
346 filter_s *_filter = (filter_s *)filter;
349 if (STRING_VALID(_filter->storage_id)) {
350 *storage_id = strdup(_filter->storage_id);
351 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
356 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
357 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
363 int media_filter_set_condition_v2(filter_h filter, const char *condition)
365 int ret = MEDIA_CONTENT_ERROR_NONE;
366 filter_s *_filter = (filter_s *)filter;
368 if ((_filter != NULL) && STRING_VALID(condition)) {
369 _filter->is_full_condition = true;
371 if (STRING_VALID(_filter->condition))
372 SAFE_FREE(_filter->condition);
375 If an error is occured in _media_content_replace_path_in_condition(),
376 A suitable return value is 'MEDIA_CONTENT_ERROR_INVALID_OPERATION'.
377 However, it is not stated in the description of media_filter_set_condition().
378 Therefore, use 'MEDIA_CONTENT_ERROR_OUT_OF_MEMORY' temporarily.
379 It will be modified after removing _media_content_replace_path_in_condition() function.
381 _filter->condition = _media_content_replace_path_in_condition(condition);
382 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to _media_content_replace_path_in_condition");
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 *condition = strdup(_filter->condition);
401 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
406 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
407 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
413 int media_filter_set_order_v2(filter_h filter, const char *order)
415 int ret = MEDIA_CONTENT_ERROR_NONE;
416 filter_s *_filter = (filter_s *)filter;
418 if ((_filter != NULL) && STRING_VALID(order)) {
419 _filter->is_full_order = true;
421 SAFE_FREE(_filter->order_keyword);
423 _filter->order_keyword = strdup(order);
424 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
426 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
427 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
433 int media_filter_get_order_v2(filter_h filter, char **order)
435 int ret = MEDIA_CONTENT_ERROR_NONE;
436 filter_s *_filter = (filter_s *)filter;
439 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == true) {
440 *order = strdup(_filter->order_keyword);
441 media_content_retvm_if(*order == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
446 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
447 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;