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>
20 static char *media_token[] =
35 typedef struct _token_t
42 #define MAX_LEFT_VALUE 512
45 #define UNKNOWN_TYPE 1000
46 #define STRING_TYPE 100
48 static char *__get_order_str(media_content_order_e order_enum);
49 static char *__get_collate_str(media_content_collation_e collate_type);
50 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
51 static char *__media_filter_replace_attr(attribute_h attr, char *name);
52 static int __tokenize_operator(token_t *token, const char *str, int op_type);
53 static int __tokenize(GList **token_list, const char *str);
55 static bool __is_pinyin_needed(void)
59 char *china = "zh_CN";
60 char *hongkong = "zh_HK";
65 bool pinyin_support = FALSE;
66 media_svc_check_pinyin_support(&pinyin_support);
69 /*Check Language Setting*/
70 lang = vconf_get_str(VCONFKEY_LANGSET);
73 media_content_error("Fail to get string of language set");
76 if((strncmp(china, lang, strlen(china)) == 0) ||
77 (strncmp(hongkong, lang, strlen(hongkong)) == 0))
88 static char *__get_order_str(media_content_order_e order_enum)
91 case MEDIA_CONTENT_ORDER_ASC:
93 case MEDIA_CONTENT_ORDER_DESC:
100 static char *__get_collate_str(media_content_collation_e collate_type)
102 switch(collate_type) {
103 case MEDIA_CONTENT_COLLATE_NOCASE:
105 case MEDIA_CONTENT_COLLATE_RTRIM:
107 case MEDIA_CONTENT_COLLATE_LOCALIZED:
108 if(__is_pinyin_needed())
116 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
122 static char *__media_filter_replace_attr(attribute_h attr, char *name)
124 char *key_temp = NULL;
125 char *generated_value = NULL;
126 attribute_s *_attr = (attribute_s *)attr;
128 if(!g_hash_table_lookup_extended(_attr->attr_map,
130 (gpointer)&key_temp, (gpointer)&generated_value))
132 //can't find the value
133 //media_content_error("NOT_FOUND_VALUE(%s)", name);
137 if(STRING_VALID(generated_value))
139 return strdup(generated_value);
142 media_content_error("__media_filter_replace_attr fail");
147 static int __tokenize_operator(token_t *token, const char *str, int op_type)
150 const char *tmp = str;
152 if(token != NULL && STRING_VALID(tmp))
154 token->type = op_type;
155 int token_size = strlen(media_token[op_type]);
158 media_content_error("Invalid token_size. op_type[%d]", op_type);
162 token->str = (char*)calloc(token_size+1, sizeof(char));
163 if(token->str == NULL)
165 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
169 strncpy(token->str, tmp, token_size);
170 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
181 static int __tokenize_string(token_t *token, const char *str, int size)
184 const char *tmp = str;
186 if(token != NULL && STRING_VALID(tmp) && size > 0)
188 token->str = (char*)calloc(size+1, sizeof(char));
189 if(token->str == NULL)
191 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
194 token->type = UNKNOWN_TYPE;
195 strncpy(token->str, tmp, size);
196 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
206 static int __tokenize_attribute(GList **token_list, const char *str)
211 if(!STRING_VALID(str)) {
212 media_content_error("Parameter string in invalid");
213 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
216 const char *tmp = str;
217 const char *dst_ptr = str + strlen(str);
219 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
221 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
222 if(tmp[idx] == ' ') //" "
224 if(idx == 0) // ignore the space.
230 token_t *token = (token_t*)calloc(1, sizeof(token_t));
233 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
236 token->type = UNKNOWN_TYPE;
237 token->str = (char*)calloc(idx+1, sizeof(char));
238 if(token->str == NULL)
241 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
244 strncpy(token->str, tmp, idx);
245 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
246 *token_list = g_list_append(*token_list, token);
247 tmp = tmp +idx + strlen(media_token[0]);
250 else if(tmp[idx] == ',') // " , "
254 token_t *token = (token_t*)calloc(1, sizeof(token_t));
257 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
260 ret = __tokenize_string(token, tmp, idx);
264 media_content_error("tokenize error occured");
269 *token_list = g_list_append(*token_list, token);
274 token_t *token = (token_t*)calloc(1, sizeof(token_t));
275 int size = __tokenize_operator(token, tmp,3);
277 if(token != NULL && STRING_VALID(token->str))
279 *token_list = g_list_append(*token_list, token);
286 media_content_error("tokenize error occured");
292 if(*tmp) //remained string
294 token_t *token = (token_t*)calloc(1, sizeof(token_t));
297 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
301 ret = __tokenize_string(token, tmp, idx);
305 media_content_error("tokenize error occured");
309 if(token != NULL && STRING_VALID(token->str))
311 *token_list = g_list_append(*token_list, token);
316 media_content_error("tokenize error occured");
321 return MEDIA_CONTENT_ERROR_NONE;
324 static int __tokenize(GList **token_list, const char *str)
329 if(!STRING_VALID(str)) {
330 media_content_error("Parameter string in invalid");
331 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
334 const char *tmp = str;
335 const char *dst_ptr = str + strlen(str);
337 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
339 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
340 if(tmp[idx] == media_token[0][0]) //" "
342 if(idx == 0) // ignore the space.
349 token_t *token = (token_t*)calloc(1, sizeof(token_t));
352 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
355 token->type = UNKNOWN_TYPE;
356 token->str = (char*)calloc(idx+1, sizeof(char));
357 if(token->str == NULL)
360 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
363 strncpy(token->str, tmp, idx);
364 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
365 *token_list = g_list_append(*token_list, token);
366 tmp = tmp +idx + strlen(media_token[0]);
369 else if(tmp[idx] == media_token[1][0]) // " \" "
373 for(j = idx+1; tmp[j]; j++) //find next quotation
375 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
380 if(tmp[j] == media_token[1][0])
382 token_t *token = (token_t*)calloc(1, sizeof(token_t));
385 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
388 token->str = (char*) calloc(j+1+1, sizeof(char));
389 if(token->str == NULL)
392 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
395 token->type = STRING_TYPE;
396 strncpy(token->str, tmp, j+1);
397 //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
398 *token_list = g_list_append(*token_list, token);
399 tmp = tmp + strlen(token->str);
406 if(!flag && *tmp != '\0' && tmp[j]=='\0')
408 token_t *token = (token_t*)calloc(1, sizeof(token_t));
411 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
414 token->str = (char*) calloc(j+1,sizeof(char));
415 if(token->str == NULL)
418 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
421 token->type = UNKNOWN_TYPE;
422 strncpy(token->str, tmp,j);
423 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
424 *token_list = g_list_append(*token_list, token);
425 tmp = tmp +strlen(token->str);
429 else if(tmp[idx] == media_token[2][0]) // " \' "
433 for(j = idx+1; tmp[j]; j++)
435 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
440 if(tmp[j] == media_token[2][0])
442 token_t *token = (token_t*)calloc(1, sizeof(token_t));
445 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
448 token->str = (char*) calloc(j+1+1, sizeof(char));
449 if(token->str == NULL)
452 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
455 token->type = STRING_TYPE;
456 strncpy(token->str, tmp, j+1);
457 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
458 *token_list = g_list_append(*token_list, token);
459 tmp = tmp + strlen(token->str);
466 if(!flag && *tmp != '\0' && tmp[j]=='\0')
468 token_t *token = (token_t*)calloc(1, sizeof(token_t));
471 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
474 token->str = (char*) calloc(j+1,sizeof(char));
475 if(token->str == NULL)
478 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
481 token->type = UNKNOWN_TYPE;
482 strncpy(token->str, tmp,j);
483 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
484 *token_list = g_list_append(*token_list, token);
485 tmp = tmp + strlen(token->str);
489 else if(tmp[idx] == media_token[3][0]) //"("
493 token_t *token = (token_t*)calloc(1, sizeof(token_t));
496 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
499 ret = __tokenize_string(token, tmp, idx);
503 media_content_error("tokenize error occured");
508 *token_list = g_list_append(*token_list, token);
512 token_t *token = (token_t*)calloc(1, sizeof(token_t));
513 int size = __tokenize_operator(token, tmp,3);
515 if(token != NULL && STRING_VALID(token->str))
517 *token_list = g_list_append(*token_list, token);
524 media_content_error("tokenize error occured");
529 else if(tmp[idx] == media_token[4][0]) //")"
533 token_t *token = (token_t*)calloc(1, sizeof(token_t));
536 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
540 ret = __tokenize_string(token, tmp, idx);
544 media_content_error("tokenize error occured");
549 *token_list = g_list_append(*token_list, token);
553 token_t *token = (token_t*)calloc(1, sizeof(token_t));
554 int size = __tokenize_operator(token, tmp,4);
556 if(token != NULL && STRING_VALID(token->str))
558 *token_list = g_list_append(*token_list, token);
565 media_content_error("tokenize error occured");
570 else if(tmp[idx] == media_token[5][0]) //"="
574 token_t *token = (token_t*)calloc(1, sizeof(token_t));
577 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
581 ret = __tokenize_string(token, tmp, idx);
585 media_content_error("tokenize error occured");
590 *token_list = g_list_append(*token_list, token);
594 token_t *token = (token_t*)calloc(1, sizeof(token_t));
595 int size = __tokenize_operator(token, tmp,5);
597 if(token != NULL && STRING_VALID(token->str))
599 *token_list = g_list_append(*token_list, token);
606 media_content_error("tokenize error occured");
611 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) //"<=",
615 token_t *token = (token_t*)calloc(1, sizeof(token_t));
618 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
622 ret = __tokenize_string(token, tmp, idx);
626 media_content_error("tokenize error occured");
631 *token_list = g_list_append(*token_list, token);
635 token_t *token = (token_t*)calloc(1, sizeof(token_t));
636 int size = __tokenize_operator(token, tmp,6);
638 if(token != NULL && STRING_VALID(token->str))
640 *token_list = g_list_append(*token_list, token);
647 media_content_error("tokenize error occured");
652 else if(tmp[idx] == media_token[7][0]) //"<",
656 token_t *token = (token_t*)calloc(1, sizeof(token_t));
659 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
663 ret = __tokenize_string(token, tmp, idx);
667 media_content_error("tokenize error occured");
672 *token_list = g_list_append(*token_list, token);
676 token_t *token = (token_t*)calloc(1, sizeof(token_t));
677 int size = __tokenize_operator(token, tmp,7);
679 if(token != NULL && STRING_VALID(token->str))
681 *token_list = g_list_append(*token_list, token);
688 media_content_error("tokenize error occured");
693 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) //">=",
697 token_t *token = (token_t*)calloc(1, sizeof(token_t));
700 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
704 ret = __tokenize_string(token, tmp, idx);
708 media_content_error("tokenize error occured");
713 *token_list = g_list_append(*token_list, token);
717 token_t *token = (token_t*)calloc(1, sizeof(token_t));
718 int size = __tokenize_operator(token, tmp,8);
720 if(token != NULL && STRING_VALID(token->str))
722 *token_list = g_list_append(*token_list, token);
729 media_content_error("tokenize error occured");
734 else if(tmp[idx] == media_token[9][0]) //">",
738 token_t *token = (token_t*)calloc(1, sizeof(token_t));
741 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
745 ret = __tokenize_string(token, tmp, idx);
749 media_content_error("tokenize error occured");
754 *token_list = g_list_append(*token_list, token);
758 token_t *token = (token_t*)calloc(1, sizeof(token_t));
759 int size = __tokenize_operator(token, tmp,9);
761 if(token != NULL && STRING_VALID(token->str))
763 *token_list = g_list_append(*token_list, token);
770 media_content_error("tokenize error occured");
777 if(*tmp) //remained string
779 token_t *token = (token_t*)calloc(1, sizeof(token_t));
782 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
786 ret = __tokenize_string(token, tmp, idx);
790 media_content_error("tokenize error occured");
794 if(token != NULL && STRING_VALID(token->str))
796 *token_list = g_list_append(*token_list, token);
801 media_content_error("tokenize error occured");
806 return MEDIA_CONTENT_ERROR_NONE;
809 int _media_filter_attribute_create(attribute_h *attr)
811 int ret = MEDIA_CONTENT_ERROR_NONE;
815 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
816 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
819 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
823 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
824 ret = MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
828 _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
829 *attr = (attribute_h)_attr;
835 int _media_filter_attribute_add(attribute_h attr, char *user_attr, char *platform_attr)
837 int ret = MEDIA_CONTENT_ERROR_NONE;
839 char *_platform = NULL;
840 attribute_s *_attr = (attribute_s*)attr;
844 if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
846 _user = strdup(user_attr);
849 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
850 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
853 _platform = strdup(platform_attr);
854 if(_platform == NULL)
857 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
858 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
861 g_hash_table_insert (_attr->attr_map, _user, _platform);
865 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
866 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
871 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
872 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
878 int _media_filter_attribute_destory(attribute_h attr)
880 int ret = MEDIA_CONTENT_ERROR_NONE;
881 attribute_s *_attr = (attribute_s*)attr;
885 if(_attr->attr_map != NULL)
887 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
888 g_hash_table_destroy(_attr->attr_map);
895 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
896 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
902 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
905 GList *token_list = NULL;
907 int ret = MEDIA_CONTENT_ERROR_NONE;
908 int total_str_size = 0;
911 if((condition == NULL) || (generated_condition == NULL))
913 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
914 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
919 if(__tokenize(&token_list, condition) < 0)
921 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
922 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
925 for(idx = 0; idx < g_list_length(token_list); idx++)
927 token = (token_t*)g_list_nth_data(token_list, idx);
929 if(token->type == UNKNOWN_TYPE)
931 char *replace_str = __media_filter_replace_attr(attr, token->str);
932 if(STRING_VALID(replace_str))
934 SAFE_FREE(token->str);
935 token->str = replace_str;
939 total_str_size += strlen(token->str)+1;
940 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
944 size = total_str_size + COLLATE_STR_SIZE + 1;
945 * generated_condition = (char*)calloc(size, sizeof(char));
947 for(idx = 0; idx < g_list_length(token_list); idx++)
949 token = (token_t*)g_list_nth_data(token_list, idx);
951 if((token != NULL) && STRING_VALID(token->str))
953 SAFE_STRLCAT(*generated_condition, token->str, size);
954 SAFE_STRLCAT(*generated_condition, SPACE, size);
956 SAFE_FREE(token->str);
961 if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
962 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
963 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
964 SAFE_STRLCAT(*generated_condition, SPACE, size);
967 //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
968 media_content_sec_debug("Condition : %s", *generated_condition);
970 //if(*generated_condition != NULL)
973 if(token_list != NULL)
974 g_list_free(token_list);
978 media_content_error("DB field mapping table doesn't exist. Check db connection", MEDIA_CONTENT_ERROR_DB_FAILED);
979 return MEDIA_CONTENT_ERROR_DB_FAILED;
985 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
987 int ret = MEDIA_CONTENT_ERROR_NONE;
988 filter_s *_filter = NULL;
989 char option_query[DEFAULT_QUERY_SIZE] = {0, };
990 char condition[DEFAULT_QUERY_SIZE] = {0, };
992 //bool order_by = true;
996 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
997 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1002 media_content_error("DB field mapping table doesn't exist. Check db connection", MEDIA_CONTENT_ERROR_DB_FAILED);
1003 return MEDIA_CONTENT_ERROR_DB_FAILED;
1006 _filter = (filter_s*)filter;
1008 memset(option_query, 0x00, sizeof(option_query));
1011 if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
1014 int total_str_size = 0;
1015 GList *token_list = NULL;
1019 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
1021 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1022 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1025 for(idx = 0; idx < g_list_length(token_list); idx++)
1027 token = (token_t*)g_list_nth_data(token_list, idx);
1029 if(token->type == UNKNOWN_TYPE)
1031 char *replace_str = __media_filter_replace_attr(attr, token->str);
1032 if(STRING_VALID(replace_str))
1034 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
1035 if(attr_str == NULL)
1037 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1038 SAFE_FREE(replace_str);
1042 if(_filter->order_collate_type == MEDIA_CONTENT_COLLATE_NOCASE || _filter->order_collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||_filter->order_collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
1043 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s COLLATE %s %s", replace_str, __get_collate_str(_filter->order_collate_type), __get_order_str(_filter->order_type));
1045 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
1048 SAFE_FREE(token->str);
1049 token->str = attr_str;
1050 SAFE_FREE(replace_str);
1054 media_content_error("There is no matched db field for %s", token->str);
1058 total_str_size += strlen(token->str) + 1;
1059 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
1063 char *generated_condition = NULL;
1064 size = total_str_size + COLLATE_STR_SIZE + 1;
1065 generated_condition = (char*)calloc(size, sizeof(char));
1067 for(idx = 0; idx < g_list_length(token_list); idx++)
1069 token = (token_t*)g_list_nth_data(token_list, idx);
1071 if((token != NULL) && STRING_VALID(token->str))
1073 //media_content_debug("[%d] %s", idx, token->str);
1074 SAFE_STRLCAT(generated_condition, token->str, size);
1075 SAFE_STRLCAT(generated_condition, SPACE, size);
1077 SAFE_FREE(token->str);
1082 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
1083 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1085 if(token_list != NULL)
1086 g_list_free(token_list);
1088 SAFE_FREE(generated_condition);
1092 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
1093 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
1094 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1096 if(STRING_VALID(option_query))
1098 *generated_option = strdup(option_query);
1102 *generated_option = NULL;
1108 int media_filter_create(filter_h *filter)
1110 int ret = MEDIA_CONTENT_ERROR_NONE;
1114 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1115 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1118 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
1122 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1123 ret = MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1127 _filter->storage_id = NULL;
1128 _filter->condition = NULL;
1129 _filter->order_keyword = NULL;
1130 _filter->order_type = -1;
1131 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1132 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1133 _filter->offset = -1;
1134 _filter->count = -1;
1136 *filter = (filter_h)_filter;
1142 int media_filter_destroy(filter_h filter)
1144 int ret = MEDIA_CONTENT_ERROR_NONE;
1145 filter_s *_filter = (filter_s*)filter;
1149 SAFE_FREE(_filter->storage_id);
1150 SAFE_FREE(_filter->condition);
1151 SAFE_FREE(_filter->order_keyword);
1154 ret = MEDIA_CONTENT_ERROR_NONE;
1158 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1159 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1165 int media_filter_set_offset(filter_h filter, int offset, int count)
1167 int ret = MEDIA_CONTENT_ERROR_NONE;
1168 filter_s *_filter = (filter_s*)filter;
1172 _filter->offset = offset;
1173 _filter->count = count;
1177 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1178 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1184 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1186 int ret = MEDIA_CONTENT_ERROR_NONE;
1187 filter_s *_filter = (filter_s*)filter;
1189 if((_filter != NULL) && STRING_VALID(condition)
1190 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1192 if(STRING_VALID(_filter->condition))
1194 SAFE_FREE(_filter->condition);
1197 _filter->condition = strdup(condition);
1198 if(_filter->condition == NULL)
1200 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1201 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1204 media_content_sec_debug("Condition string : %s", _filter->condition);
1206 _filter->condition_collate_type = collate_type;
1210 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1211 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1217 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1219 int ret = MEDIA_CONTENT_ERROR_NONE;
1220 filter_s *_filter = (filter_s*)filter;
1222 if((_filter != NULL) && STRING_VALID(order_keyword)
1223 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1224 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1226 SAFE_FREE(_filter->order_keyword);
1228 if(STRING_VALID(order_keyword))
1230 _filter->order_keyword = strdup(order_keyword);
1232 if(_filter->order_keyword == NULL)
1234 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1235 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1237 _filter->order_type = order_type;
1238 _filter->order_collate_type = collate_type;
1242 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1243 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1248 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1249 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1255 int media_filter_set_storage(filter_h filter, const char *storage_id)
1257 int ret = MEDIA_CONTENT_ERROR_NONE;
1258 filter_s *_filter = (filter_s*)filter;
1260 if((_filter != NULL) && STRING_VALID(storage_id))
1262 if(STRING_VALID(_filter->storage_id))
1264 SAFE_FREE(_filter->storage_id);
1267 _filter->storage_id = strdup(storage_id);
1268 if(_filter->storage_id == NULL)
1270 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1271 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1274 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1278 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1279 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1285 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1287 int ret = MEDIA_CONTENT_ERROR_NONE;
1288 filter_s *_filter = (filter_s*)filter;
1292 *offset = _filter->offset;
1293 *count = _filter->count;
1297 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1298 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1304 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1306 int ret = MEDIA_CONTENT_ERROR_NONE;
1307 filter_s *_filter = (filter_s*)filter;
1311 if(STRING_VALID(_filter->condition))
1313 *condition = strdup(_filter->condition);
1314 if(*condition == NULL)
1316 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1317 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1325 *collate_type = _filter->condition_collate_type;
1329 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1330 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1336 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1338 int ret = MEDIA_CONTENT_ERROR_NONE;
1339 filter_s *_filter = (filter_s*)filter;
1343 if(STRING_VALID(_filter->order_keyword))
1345 *order_keyword = strdup(_filter->order_keyword);
1346 if(*order_keyword == NULL)
1348 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1349 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1354 *order_keyword = NULL;
1357 *order_type = _filter->order_type;
1358 *collate_type = _filter->order_collate_type;
1362 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1363 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1369 int media_filter_get_storage(filter_h filter, char **storage_id)
1371 int ret = MEDIA_CONTENT_ERROR_NONE;
1372 filter_s *_filter = (filter_s*)filter;
1376 if(STRING_VALID(_filter->storage_id))
1378 *storage_id = strdup(_filter->storage_id);
1379 if(*storage_id == NULL)
1381 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1382 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1392 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1393 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;