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)
58 char *china = "zh_CN";
59 char *hongkong = "zh_HK";
63 bool pinyin_support = FALSE;
64 media_svc_check_pinyin_support(&pinyin_support);
67 /*Check Language Setting*/
68 lang = vconf_get_str(VCONFKEY_LANGSET);
71 media_content_error("Fail to get string of language set");
74 if((strncmp(china, lang, strlen(china)) == 0) ||
75 (strncmp(hongkong, lang, strlen(hongkong)) == 0))
86 static char *__get_order_str(media_content_order_e order_enum)
89 case MEDIA_CONTENT_ORDER_ASC:
91 case MEDIA_CONTENT_ORDER_DESC:
98 static char *__get_collate_str(media_content_collation_e collate_type)
100 switch(collate_type) {
101 case MEDIA_CONTENT_COLLATE_NOCASE:
103 case MEDIA_CONTENT_COLLATE_RTRIM:
105 case MEDIA_CONTENT_COLLATE_LOCALIZED:
106 if(__is_pinyin_needed())
114 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
120 static char *__media_filter_replace_attr(attribute_h attr, char *name)
122 char *key_temp = NULL;
123 char *generated_value = NULL;
124 attribute_s *_attr = (attribute_s *)attr;
126 if(!g_hash_table_lookup_extended(_attr->attr_map,
128 (gpointer)&key_temp, (gpointer)&generated_value))
130 //can't find the value
131 //media_content_error("NOT_FOUND_VALUE(%s)", name);
135 if(STRING_VALID(generated_value))
137 return strdup(generated_value);
140 media_content_error("__media_filter_replace_attr fail");
145 static int __tokenize_operator(token_t *token, const char *str, int op_type)
148 const char *tmp = str;
150 if(token != NULL && STRING_VALID(tmp))
152 token->type = op_type;
153 int token_size = strlen(media_token[op_type]);
156 media_content_error("Invalid token_size. op_type[%d]", op_type);
160 token->str = (char*)calloc(token_size+1, sizeof(char));
161 if(token->str == NULL)
163 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
167 strncpy(token->str, tmp, token_size);
168 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
179 static int __tokenize_string(token_t *token, const char *str, int size)
182 const char *tmp = str;
184 if(token != NULL && STRING_VALID(tmp) && size > 0)
186 token->str = (char*)calloc(size+1, sizeof(char));
187 if(token->str == NULL)
189 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
192 token->type = UNKNOWN_TYPE;
193 strncpy(token->str, tmp, size);
194 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
204 static int __tokenize_attribute(GList **token_list, const char *str)
209 if(!STRING_VALID(str)) {
210 media_content_error("Parameter string in invalid");
211 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
214 const char *tmp = str;
215 const char *dst_ptr = str + strlen(str);
217 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
219 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
220 if(tmp[idx] == ' ') //" "
222 if(idx == 0) // ignore the space.
228 token_t *token = (token_t*)calloc(1, sizeof(token_t));
231 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
234 token->type = UNKNOWN_TYPE;
235 token->str = (char*)calloc(idx+1, sizeof(char));
236 if(token->str == NULL)
239 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
242 strncpy(token->str, tmp, idx);
243 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
244 *token_list = g_list_append(*token_list, token);
245 tmp = tmp +idx + strlen(media_token[0]);
248 else if(tmp[idx] == ',') // " , "
252 token_t *token = (token_t*)calloc(1, sizeof(token_t));
255 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
258 ret = __tokenize_string(token, tmp, idx);
262 media_content_error("tokenize error occured");
267 *token_list = g_list_append(*token_list, token);
272 token_t *token = (token_t*)calloc(1, sizeof(token_t));
273 int size = __tokenize_operator(token, tmp,3);
275 if(token != NULL && STRING_VALID(token->str))
277 *token_list = g_list_append(*token_list, token);
284 media_content_error("tokenize error occured");
290 if(*tmp) //remained string
292 token_t *token = (token_t*)calloc(1, sizeof(token_t));
295 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
299 ret = __tokenize_string(token, tmp, idx);
303 media_content_error("tokenize error occured");
307 if(token != NULL && STRING_VALID(token->str))
309 *token_list = g_list_append(*token_list, token);
314 media_content_error("tokenize error occured");
319 return MEDIA_CONTENT_ERROR_NONE;
322 static int __tokenize(GList **token_list, const char *str)
327 if(!STRING_VALID(str)) {
328 media_content_error("Parameter string in invalid");
329 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
332 const char *tmp = str;
333 const char *dst_ptr = str + strlen(str);
335 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
337 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
338 if(tmp[idx] == media_token[0][0]) //" "
340 if(idx == 0) // ignore the space.
347 token_t *token = (token_t*)calloc(1, sizeof(token_t));
350 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
353 token->type = UNKNOWN_TYPE;
354 token->str = (char*)calloc(idx+1, sizeof(char));
355 if(token->str == NULL)
358 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
361 strncpy(token->str, tmp, idx);
362 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
363 *token_list = g_list_append(*token_list, token);
364 tmp = tmp +idx + strlen(media_token[0]);
367 else if(tmp[idx] == media_token[1][0]) // " \" "
371 for(j = idx+1; tmp[j]; j++) //find next quotation
373 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
378 if(tmp[j] == media_token[1][0])
380 token_t *token = (token_t*)calloc(1, sizeof(token_t));
383 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
386 token->str = (char*) calloc(j+1+1, sizeof(char));
387 if(token->str == NULL)
390 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
393 token->type = STRING_TYPE;
394 strncpy(token->str, tmp, j+1);
395 //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
396 *token_list = g_list_append(*token_list, token);
397 tmp = tmp + strlen(token->str);
404 if(!flag && *tmp != '\0' && tmp[j]=='\0')
406 token_t *token = (token_t*)calloc(1, sizeof(token_t));
409 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
412 token->str = (char*) calloc(j+1,sizeof(char));
413 if(token->str == NULL)
416 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
419 token->type = UNKNOWN_TYPE;
420 strncpy(token->str, tmp,j);
421 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
422 *token_list = g_list_append(*token_list, token);
423 tmp = tmp +strlen(token->str);
427 else if(tmp[idx] == media_token[2][0]) // " \' "
431 for(j = idx+1; tmp[j]; j++)
433 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
438 if(tmp[j] == media_token[2][0])
440 token_t *token = (token_t*)calloc(1, sizeof(token_t));
443 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
446 token->str = (char*) calloc(j+1+1, sizeof(char));
447 if(token->str == NULL)
450 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
453 token->type = STRING_TYPE;
454 strncpy(token->str, tmp, j+1);
455 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
456 *token_list = g_list_append(*token_list, token);
457 tmp = tmp + strlen(token->str);
464 if(!flag && *tmp != '\0' && tmp[j]=='\0')
466 token_t *token = (token_t*)calloc(1, sizeof(token_t));
469 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
472 token->str = (char*) calloc(j+1,sizeof(char));
473 if(token->str == NULL)
476 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
479 token->type = UNKNOWN_TYPE;
480 strncpy(token->str, tmp,j);
481 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
482 *token_list = g_list_append(*token_list, token);
483 tmp = tmp + strlen(token->str);
487 else if(tmp[idx] == media_token[3][0]) //"("
491 token_t *token = (token_t*)calloc(1, sizeof(token_t));
494 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
497 ret = __tokenize_string(token, tmp, idx);
501 media_content_error("tokenize error occured");
506 *token_list = g_list_append(*token_list, token);
510 token_t *token = (token_t*)calloc(1, sizeof(token_t));
511 int size = __tokenize_operator(token, tmp,3);
513 if(token != NULL && STRING_VALID(token->str))
515 *token_list = g_list_append(*token_list, token);
522 media_content_error("tokenize error occured");
527 else if(tmp[idx] == media_token[4][0]) //")"
531 token_t *token = (token_t*)calloc(1, sizeof(token_t));
534 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
538 ret = __tokenize_string(token, tmp, idx);
542 media_content_error("tokenize error occured");
547 *token_list = g_list_append(*token_list, token);
551 token_t *token = (token_t*)calloc(1, sizeof(token_t));
552 int size = __tokenize_operator(token, tmp,4);
554 if(token != NULL && STRING_VALID(token->str))
556 *token_list = g_list_append(*token_list, token);
563 media_content_error("tokenize error occured");
568 else if(tmp[idx] == media_token[5][0]) //"="
572 token_t *token = (token_t*)calloc(1, sizeof(token_t));
575 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
579 ret = __tokenize_string(token, tmp, idx);
583 media_content_error("tokenize error occured");
588 *token_list = g_list_append(*token_list, token);
592 token_t *token = (token_t*)calloc(1, sizeof(token_t));
593 int size = __tokenize_operator(token, tmp,5);
595 if(token != NULL && STRING_VALID(token->str))
597 *token_list = g_list_append(*token_list, token);
604 media_content_error("tokenize error occured");
609 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) //"<=",
613 token_t *token = (token_t*)calloc(1, sizeof(token_t));
616 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
620 ret = __tokenize_string(token, tmp, idx);
624 media_content_error("tokenize error occured");
629 *token_list = g_list_append(*token_list, token);
633 token_t *token = (token_t*)calloc(1, sizeof(token_t));
634 int size = __tokenize_operator(token, tmp,6);
636 if(token != NULL && STRING_VALID(token->str))
638 *token_list = g_list_append(*token_list, token);
645 media_content_error("tokenize error occured");
650 else if(tmp[idx] == media_token[7][0]) //"<",
654 token_t *token = (token_t*)calloc(1, sizeof(token_t));
657 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
661 ret = __tokenize_string(token, tmp, idx);
665 media_content_error("tokenize error occured");
670 *token_list = g_list_append(*token_list, token);
674 token_t *token = (token_t*)calloc(1, sizeof(token_t));
675 int size = __tokenize_operator(token, tmp,7);
677 if(token != NULL && STRING_VALID(token->str))
679 *token_list = g_list_append(*token_list, token);
686 media_content_error("tokenize error occured");
691 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) //">=",
695 token_t *token = (token_t*)calloc(1, sizeof(token_t));
698 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
702 ret = __tokenize_string(token, tmp, idx);
706 media_content_error("tokenize error occured");
711 *token_list = g_list_append(*token_list, token);
715 token_t *token = (token_t*)calloc(1, sizeof(token_t));
716 int size = __tokenize_operator(token, tmp,8);
718 if(token != NULL && STRING_VALID(token->str))
720 *token_list = g_list_append(*token_list, token);
727 media_content_error("tokenize error occured");
732 else if(tmp[idx] == media_token[9][0]) //">",
736 token_t *token = (token_t*)calloc(1, sizeof(token_t));
739 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
743 ret = __tokenize_string(token, tmp, idx);
747 media_content_error("tokenize error occured");
752 *token_list = g_list_append(*token_list, token);
756 token_t *token = (token_t*)calloc(1, sizeof(token_t));
757 int size = __tokenize_operator(token, tmp,9);
759 if(token != NULL && STRING_VALID(token->str))
761 *token_list = g_list_append(*token_list, token);
768 media_content_error("tokenize error occured");
775 if(*tmp) //remained string
777 token_t *token = (token_t*)calloc(1, sizeof(token_t));
780 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
784 ret = __tokenize_string(token, tmp, idx);
788 media_content_error("tokenize error occured");
792 if(token != NULL && STRING_VALID(token->str))
794 *token_list = g_list_append(*token_list, token);
799 media_content_error("tokenize error occured");
804 return MEDIA_CONTENT_ERROR_NONE;
807 int _media_filter_attribute_create(attribute_h *attr)
809 int ret = MEDIA_CONTENT_ERROR_NONE;
813 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
814 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
817 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
821 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
822 ret = MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
826 _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
827 *attr = (attribute_h)_attr;
833 int _media_filter_attribute_add(attribute_h attr, char *user_attr, char *platform_attr)
835 int ret = MEDIA_CONTENT_ERROR_NONE;
837 char *_platform = NULL;
838 attribute_s *_attr = (attribute_s*)attr;
842 if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
844 _user = strdup(user_attr);
847 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
848 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
851 _platform = strdup(platform_attr);
852 if(_platform == NULL)
855 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
856 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
859 g_hash_table_insert (_attr->attr_map, _user, _platform);
863 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
864 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
869 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
870 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
876 int _media_filter_attribute_destory(attribute_h attr)
878 int ret = MEDIA_CONTENT_ERROR_NONE;
879 attribute_s *_attr = (attribute_s*)attr;
883 if(_attr->attr_map != NULL)
885 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
886 g_hash_table_destroy(_attr->attr_map);
893 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
894 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
900 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
903 GList *token_list = NULL;
905 int ret = MEDIA_CONTENT_ERROR_NONE;
906 int total_str_size = 0;
909 if((condition == NULL) || (generated_condition == NULL))
911 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
912 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
917 if(__tokenize(&token_list, condition) < 0)
919 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
920 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
923 for(idx = 0; idx < g_list_length(token_list); idx++)
925 token = (token_t*)g_list_nth_data(token_list, idx);
927 if(token->type == UNKNOWN_TYPE)
929 char *replace_str = __media_filter_replace_attr(attr, token->str);
930 if(STRING_VALID(replace_str))
932 SAFE_FREE(token->str);
933 token->str = replace_str;
937 total_str_size += strlen(token->str)+1;
938 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
942 size = total_str_size + COLLATE_STR_SIZE + 1;
943 * generated_condition = (char*)calloc(size, sizeof(char));
945 for(idx = 0; idx < g_list_length(token_list); idx++)
947 token = (token_t*)g_list_nth_data(token_list, idx);
949 if((token != NULL) && STRING_VALID(token->str))
951 SAFE_STRLCAT(*generated_condition, token->str, size);
952 SAFE_STRLCAT(*generated_condition, SPACE, size);
954 SAFE_FREE(token->str);
959 if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
960 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
961 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
962 SAFE_STRLCAT(*generated_condition, SPACE, size);
965 //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
966 media_content_sec_debug("Condition : %s", *generated_condition);
968 //if(*generated_condition != NULL)
971 if(token_list != NULL)
972 g_list_free(token_list);
976 media_content_error("DB field mapping table doesn't exist. Check db connection", MEDIA_CONTENT_ERROR_DB_FAILED);
977 return MEDIA_CONTENT_ERROR_DB_FAILED;
983 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
985 int ret = MEDIA_CONTENT_ERROR_NONE;
986 filter_s *_filter = NULL;
987 char option_query[DEFAULT_QUERY_SIZE] = {0, };
988 char condition[DEFAULT_QUERY_SIZE] = {0, };
990 //bool order_by = true;
994 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
995 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1000 media_content_error("DB field mapping table doesn't exist. Check db connection", MEDIA_CONTENT_ERROR_DB_FAILED);
1001 return MEDIA_CONTENT_ERROR_DB_FAILED;
1004 _filter = (filter_s*)filter;
1006 memset(option_query, 0x00, sizeof(option_query));
1009 if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
1012 int total_str_size = 0;
1013 GList *token_list = NULL;
1017 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
1019 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1020 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1023 for(idx = 0; idx < g_list_length(token_list); idx++)
1025 token = (token_t*)g_list_nth_data(token_list, idx);
1027 if(token->type == UNKNOWN_TYPE)
1029 char *replace_str = __media_filter_replace_attr(attr, token->str);
1030 if(STRING_VALID(replace_str))
1032 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
1033 if(attr_str == NULL)
1035 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1036 SAFE_FREE(replace_str);
1040 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) {
1041 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));
1043 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
1046 SAFE_FREE(token->str);
1047 token->str = attr_str;
1048 SAFE_FREE(replace_str);
1052 media_content_error("There is no matched db field for %s", token->str);
1056 total_str_size += strlen(token->str) + 1;
1057 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
1061 char *generated_condition = NULL;
1062 size = total_str_size + COLLATE_STR_SIZE + 1;
1063 generated_condition = (char*)calloc(size, sizeof(char));
1065 for(idx = 0; idx < g_list_length(token_list); idx++)
1067 token = (token_t*)g_list_nth_data(token_list, idx);
1069 if((token != NULL) && STRING_VALID(token->str))
1071 //media_content_debug("[%d] %s", idx, token->str);
1072 SAFE_STRLCAT(generated_condition, token->str, size);
1073 SAFE_STRLCAT(generated_condition, SPACE, size);
1075 SAFE_FREE(token->str);
1080 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
1081 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1083 if(token_list != NULL)
1084 g_list_free(token_list);
1086 SAFE_FREE(generated_condition);
1090 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
1091 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
1092 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1094 if(STRING_VALID(option_query))
1096 *generated_option = strdup(option_query);
1100 *generated_option = NULL;
1106 int media_filter_create(filter_h *filter)
1108 int ret = MEDIA_CONTENT_ERROR_NONE;
1112 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1113 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1116 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
1120 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1121 ret = MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1125 _filter->storage_id = NULL;
1126 _filter->condition = NULL;
1127 _filter->order_keyword = NULL;
1128 _filter->order_type = -1;
1129 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1130 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1131 _filter->offset = -1;
1132 _filter->count = -1;
1134 *filter = (filter_h)_filter;
1140 int media_filter_destroy(filter_h filter)
1142 int ret = MEDIA_CONTENT_ERROR_NONE;
1143 filter_s *_filter = (filter_s*)filter;
1147 SAFE_FREE(_filter->storage_id);
1148 SAFE_FREE(_filter->condition);
1149 SAFE_FREE(_filter->order_keyword);
1152 ret = MEDIA_CONTENT_ERROR_NONE;
1156 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1157 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1163 int media_filter_set_offset(filter_h filter, int offset, int count)
1165 int ret = MEDIA_CONTENT_ERROR_NONE;
1166 filter_s *_filter = (filter_s*)filter;
1170 _filter->offset = offset;
1171 _filter->count = count;
1175 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1176 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1182 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1184 int ret = MEDIA_CONTENT_ERROR_NONE;
1185 filter_s *_filter = (filter_s*)filter;
1187 if((_filter != NULL) && STRING_VALID(condition)
1188 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1190 if(STRING_VALID(_filter->condition))
1192 SAFE_FREE(_filter->condition);
1195 _filter->condition = strdup(condition);
1196 if(_filter->condition == NULL)
1198 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1199 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1202 media_content_sec_debug("Condition string : %s", _filter->condition);
1204 _filter->condition_collate_type = collate_type;
1208 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1209 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1215 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1217 int ret = MEDIA_CONTENT_ERROR_NONE;
1218 filter_s *_filter = (filter_s*)filter;
1220 if((_filter != NULL) && STRING_VALID(order_keyword)
1221 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1222 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1224 SAFE_FREE(_filter->order_keyword);
1226 if(STRING_VALID(order_keyword))
1228 _filter->order_keyword = strdup(order_keyword);
1230 if(_filter->order_keyword == NULL)
1232 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1233 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1235 _filter->order_type = order_type;
1236 _filter->order_collate_type = collate_type;
1240 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1241 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1246 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1247 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1253 int media_filter_set_storage(filter_h filter, const char *storage_id)
1255 int ret = MEDIA_CONTENT_ERROR_NONE;
1256 filter_s *_filter = (filter_s*)filter;
1258 if((_filter != NULL) && STRING_VALID(storage_id))
1260 if(STRING_VALID(_filter->storage_id))
1262 SAFE_FREE(_filter->storage_id);
1265 _filter->storage_id = strdup(storage_id);
1266 if(_filter->storage_id == NULL)
1268 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1269 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1272 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1276 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1277 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1283 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1285 int ret = MEDIA_CONTENT_ERROR_NONE;
1286 filter_s *_filter = (filter_s*)filter;
1290 *offset = _filter->offset;
1291 *count = _filter->count;
1295 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1296 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1302 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1304 int ret = MEDIA_CONTENT_ERROR_NONE;
1305 filter_s *_filter = (filter_s*)filter;
1309 if(STRING_VALID(_filter->condition))
1311 *condition = strdup(_filter->condition);
1312 if(*condition == NULL)
1314 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1315 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1323 *collate_type = _filter->condition_collate_type;
1327 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1328 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1334 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1336 int ret = MEDIA_CONTENT_ERROR_NONE;
1337 filter_s *_filter = (filter_s*)filter;
1341 if(STRING_VALID(_filter->order_keyword))
1343 *order_keyword = strdup(_filter->order_keyword);
1344 if(*order_keyword == NULL)
1346 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1347 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1352 *order_keyword = NULL;
1355 *order_type = _filter->order_type;
1356 *collate_type = _filter->order_collate_type;
1360 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1361 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1367 int media_filter_get_storage(filter_h filter, char **storage_id)
1369 int ret = MEDIA_CONTENT_ERROR_NONE;
1370 filter_s *_filter = (filter_s*)filter;
1374 if(STRING_VALID(_filter->storage_id))
1376 *storage_id = strdup(_filter->storage_id);
1377 if(*storage_id == NULL)
1379 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1380 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1390 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1391 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;