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>
21 static const char *media_token[] =
37 typedef struct _token_t
44 #define MAX_LEFT_VALUE 512
47 #define UNKNOWN_TYPE 1000
48 #define STRING_TYPE 100
50 static char *__get_order_str(media_content_order_e order_enum);
51 static char *__get_collate_str(media_content_collation_e collate_type);
52 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
53 static char *__media_filter_replace_attr(attribute_h attr, char *name);
54 static int __tokenize_operator(token_t *token, const char *str, int op_type);
55 static int __tokenize(GList **token_list, const char *str);
57 static bool __is_pinyin_needed(void)
60 const char *china = "zh_CN";
61 const 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);
71 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
73 if((strncmp(china, lang, strlen(china)) == 0) ||
74 (strncmp(hongkong, lang, strlen(hongkong)) == 0))
85 static char *__get_order_str(media_content_order_e order_enum)
88 case MEDIA_CONTENT_ORDER_ASC:
90 case MEDIA_CONTENT_ORDER_DESC:
91 return (char *)"DESC";
97 static char *__get_collate_str(media_content_collation_e collate_type)
99 switch(collate_type) {
100 case MEDIA_CONTENT_COLLATE_NOCASE:
101 return (char *)"NOCASE";
102 case MEDIA_CONTENT_COLLATE_RTRIM:
103 return (char *)"RTRIM";
104 case MEDIA_CONTENT_COLLATE_LOCALIZED:
105 if(__is_pinyin_needed())
106 return (char *)"NOCASE";
108 return (char *)"localized";
109 default: return (char *)" ";
113 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
119 static char *__media_filter_replace_attr(attribute_h attr, char *name)
121 char *key_temp = NULL;
122 char *generated_value = NULL;
123 attribute_s *_attr = (attribute_s *)attr;
125 if(!g_hash_table_lookup_extended(_attr->attr_map,
127 (gpointer)&key_temp, (gpointer)&generated_value))
129 //can't find the value
130 //media_content_error("NOT_FOUND_VALUE(%s)", name);
134 if(STRING_VALID(generated_value))
136 return strdup(generated_value);
139 media_content_error("__media_filter_replace_attr fail");
144 static int __tokenize_operator(token_t *token, const char *str, int op_type)
147 const char *tmp = str;
149 if(token != NULL && STRING_VALID(tmp))
151 token->type = op_type;
152 int token_size = strlen(media_token[op_type]);
153 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
155 token->str = (char*)calloc(token_size+1, sizeof(char));
156 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
158 strncpy(token->str, tmp, token_size);
159 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
170 static int __tokenize_string(token_t *token, const char *str, int size)
173 const char *tmp = str;
175 if(token != NULL && STRING_VALID(tmp) && size > 0)
177 token->str = (char*)calloc(size+1, sizeof(char));
178 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
180 token->type = UNKNOWN_TYPE;
181 strncpy(token->str, tmp, size);
182 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
192 static int __tokenize_attribute(GList **token_list, const char *str)
197 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
199 const char *tmp = str;
200 const char *dst_ptr = str + strlen(str);
202 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
204 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
205 if(tmp[idx] == ' ') //" "
207 if(idx == 0) // ignore the space.
213 token_t *token = (token_t*)calloc(1, sizeof(token_t));
214 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
216 token->type = UNKNOWN_TYPE;
217 token->str = (char*)calloc(idx+1, sizeof(char));
218 if(token->str == NULL)
221 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
224 strncpy(token->str, tmp, idx);
225 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
226 *token_list = g_list_append(*token_list, token);
227 tmp = tmp +idx + strlen(media_token[0]);
230 else if(tmp[idx] == ',') // " , "
234 token_t *token = (token_t*)calloc(1, sizeof(token_t));
235 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
237 ret = __tokenize_string(token, tmp, idx);
241 media_content_error("tokenize error occured");
246 *token_list = g_list_append(*token_list, token);
251 token_t *token = (token_t*)calloc(1, sizeof(token_t));
252 int size = __tokenize_operator(token, tmp,3);
254 if(token != NULL && STRING_VALID(token->str))
256 *token_list = g_list_append(*token_list, token);
263 media_content_error("tokenize error occured");
269 if(*tmp) //remained string
271 token_t *token = (token_t*)calloc(1, sizeof(token_t));
272 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
274 ret = __tokenize_string(token, tmp, idx);
278 media_content_error("tokenize error occured");
282 if(token != NULL && STRING_VALID(token->str))
284 *token_list = g_list_append(*token_list, token);
289 media_content_error("tokenize error occured");
294 return MEDIA_CONTENT_ERROR_NONE;
297 static int __tokenize(GList **token_list, const char *str)
302 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
304 const char *tmp = str;
305 const char *dst_ptr = str + strlen(str);
307 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
309 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
310 if(tmp[idx] == media_token[0][0]) //" "
312 if(idx == 0) // ignore the space.
319 token_t *token = (token_t*)calloc(1, sizeof(token_t));
320 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
322 token->type = UNKNOWN_TYPE;
323 token->str = (char*)calloc(idx+1, sizeof(char));
324 if(token->str == NULL)
327 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
330 strncpy(token->str, tmp, idx);
331 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
332 *token_list = g_list_append(*token_list, token);
333 tmp = tmp +idx + strlen(media_token[0]);
336 else if(tmp[idx] == media_token[1][0]) // " \" "
340 for(j = idx+1; tmp[j]; j++) //find next quotation
342 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
347 if(tmp[j] == media_token[1][0])
349 token_t *token = (token_t*)calloc(1, sizeof(token_t));
350 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
352 token->str = (char*) calloc(j+1+1, sizeof(char));
353 if(token->str == NULL)
356 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
359 token->type = STRING_TYPE;
360 strncpy(token->str, tmp, j+1);
361 //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
362 *token_list = g_list_append(*token_list, token);
363 tmp = tmp + strlen(token->str);
370 if(!flag && *tmp != '\0' && tmp[j]=='\0')
372 token_t *token = (token_t*)calloc(1, sizeof(token_t));
373 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
375 token->str = (char*) calloc(j+1,sizeof(char));
376 if(token->str == NULL)
379 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
382 token->type = UNKNOWN_TYPE;
383 strncpy(token->str, tmp,j);
384 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
385 *token_list = g_list_append(*token_list, token);
386 tmp = tmp +strlen(token->str);
390 else if(tmp[idx] == media_token[2][0]) // " \' "
394 for(j = idx+1; tmp[j]; j++)
396 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
401 if(tmp[j] == media_token[2][0])
403 token_t *token = (token_t*)calloc(1, sizeof(token_t));
404 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
406 token->str = (char*) calloc(j+1+1, sizeof(char));
407 if(token->str == NULL)
410 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
413 token->type = STRING_TYPE;
414 strncpy(token->str, tmp, j+1);
415 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
416 *token_list = g_list_append(*token_list, token);
417 tmp = tmp + strlen(token->str);
424 if(!flag && *tmp != '\0' && tmp[j]=='\0')
426 token_t *token = (token_t*)calloc(1, sizeof(token_t));
427 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
429 token->str = (char*) calloc(j+1,sizeof(char));
430 if(token->str == NULL)
433 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
436 token->type = UNKNOWN_TYPE;
437 strncpy(token->str, tmp,j);
438 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
439 *token_list = g_list_append(*token_list, token);
440 tmp = tmp + strlen(token->str);
444 else if(tmp[idx] == media_token[3][0]) //"("
448 token_t *token = (token_t*)calloc(1, sizeof(token_t));
449 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
451 ret = __tokenize_string(token, tmp, idx);
455 media_content_error("tokenize error occured");
460 *token_list = g_list_append(*token_list, token);
464 token_t *token = (token_t*)calloc(1, sizeof(token_t));
465 int size = __tokenize_operator(token, tmp,3);
467 if(token != NULL && STRING_VALID(token->str))
469 *token_list = g_list_append(*token_list, token);
476 media_content_error("tokenize error occured");
481 else if(tmp[idx] == media_token[4][0]) //")"
485 token_t *token = (token_t*)calloc(1, sizeof(token_t));
486 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
488 ret = __tokenize_string(token, tmp, idx);
492 media_content_error("tokenize error occured");
497 *token_list = g_list_append(*token_list, token);
501 token_t *token = (token_t*)calloc(1, sizeof(token_t));
502 int size = __tokenize_operator(token, tmp,4);
504 if(token != NULL && STRING_VALID(token->str))
506 *token_list = g_list_append(*token_list, token);
513 media_content_error("tokenize error occured");
518 else if(tmp[idx] == media_token[5][0]) //"="
522 token_t *token = (token_t*)calloc(1, sizeof(token_t));
523 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
525 ret = __tokenize_string(token, tmp, idx);
529 media_content_error("tokenize error occured");
534 *token_list = g_list_append(*token_list, token);
538 token_t *token = (token_t*)calloc(1, sizeof(token_t));
539 int size = __tokenize_operator(token, tmp,5);
541 if(token != NULL && STRING_VALID(token->str))
543 *token_list = g_list_append(*token_list, token);
550 media_content_error("tokenize error occured");
555 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) //"<=",
559 token_t *token = (token_t*)calloc(1, sizeof(token_t));
560 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
562 ret = __tokenize_string(token, tmp, idx);
566 media_content_error("tokenize error occured");
571 *token_list = g_list_append(*token_list, token);
575 token_t *token = (token_t*)calloc(1, sizeof(token_t));
576 int size = __tokenize_operator(token, tmp,6);
578 if(token != NULL && STRING_VALID(token->str))
580 *token_list = g_list_append(*token_list, token);
587 media_content_error("tokenize error occured");
592 else if(tmp[idx] == media_token[7][0]) //"<",
596 token_t *token = (token_t*)calloc(1, sizeof(token_t));
597 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
599 ret = __tokenize_string(token, tmp, idx);
603 media_content_error("tokenize error occured");
608 *token_list = g_list_append(*token_list, token);
612 token_t *token = (token_t*)calloc(1, sizeof(token_t));
613 int size = __tokenize_operator(token, tmp,7);
615 if(token != NULL && STRING_VALID(token->str))
617 *token_list = g_list_append(*token_list, token);
624 media_content_error("tokenize error occured");
629 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) //">=",
633 token_t *token = (token_t*)calloc(1, sizeof(token_t));
634 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
636 ret = __tokenize_string(token, tmp, idx);
640 media_content_error("tokenize error occured");
645 *token_list = g_list_append(*token_list, token);
649 token_t *token = (token_t*)calloc(1, sizeof(token_t));
650 int size = __tokenize_operator(token, tmp,8);
652 if(token != NULL && STRING_VALID(token->str))
654 *token_list = g_list_append(*token_list, token);
661 media_content_error("tokenize error occured");
666 else if(tmp[idx] == media_token[9][0]) //">",
670 token_t *token = (token_t*)calloc(1, sizeof(token_t));
671 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
673 ret = __tokenize_string(token, tmp, idx);
677 media_content_error("tokenize error occured");
682 *token_list = g_list_append(*token_list, token);
686 token_t *token = (token_t*)calloc(1, sizeof(token_t));
687 int size = __tokenize_operator(token, tmp,9);
689 if(token != NULL && STRING_VALID(token->str))
691 *token_list = g_list_append(*token_list, token);
698 media_content_error("tokenize error occured");
702 else if(tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) //"!=",
706 token_t *token = (token_t*)calloc(1, sizeof(token_t));
707 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
709 ret = __tokenize_string(token, tmp, idx);
713 media_content_error("tokenize error occured");
718 *token_list = g_list_append(*token_list, token);
722 token_t *token = (token_t*)calloc(1, sizeof(token_t));
723 int size = __tokenize_operator(token, tmp, 10);
725 if(token != NULL && STRING_VALID(token->str))
727 *token_list = g_list_append(*token_list, token);
734 media_content_error("tokenize error occured");
740 if(*tmp) //remained string
742 token_t *token = (token_t*)calloc(1, sizeof(token_t));
743 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
745 ret = __tokenize_string(token, tmp, idx);
749 media_content_error("tokenize error occured");
753 if(token != NULL && STRING_VALID(token->str))
755 *token_list = g_list_append(*token_list, token);
760 media_content_error("tokenize error occured");
765 return MEDIA_CONTENT_ERROR_NONE;
768 int _media_filter_attribute_create(attribute_h *attr)
770 int ret = MEDIA_CONTENT_ERROR_NONE;
772 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
774 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
775 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
777 _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
778 *attr = (attribute_h)_attr;
783 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
785 int ret = MEDIA_CONTENT_ERROR_NONE;
787 char *_platform = NULL;
788 attribute_s *_attr = (attribute_s*)attr;
790 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
792 if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
794 _user = strdup(user_attr);
795 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
797 _platform = strdup(platform_attr);
798 if(_platform == NULL)
801 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
802 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
805 g_hash_table_insert (_attr->attr_map, _user, _platform);
809 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
810 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
816 int _media_filter_attribute_destory(attribute_h attr)
818 int ret = MEDIA_CONTENT_ERROR_NONE;
819 attribute_s *_attr = (attribute_s*)attr;
821 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
823 if(_attr->attr_map != NULL)
825 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
826 g_hash_table_destroy(_attr->attr_map);
834 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
836 unsigned int idx = 0;
837 GList *token_list = NULL;
839 int ret = MEDIA_CONTENT_ERROR_NONE;
840 int total_str_size = 0;
843 media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
844 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
845 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
847 if(__tokenize(&token_list, condition) < 0)
849 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
850 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
853 for(idx = 0; idx < g_list_length(token_list); idx++)
855 token = (token_t*)g_list_nth_data(token_list, idx);
857 if(token->type == UNKNOWN_TYPE)
859 char *replace_str = __media_filter_replace_attr(attr, token->str);
860 if(STRING_VALID(replace_str))
862 SAFE_FREE(token->str);
863 token->str = replace_str;
867 total_str_size += strlen(token->str)+1;
868 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
872 size = total_str_size + COLLATE_STR_SIZE + 1;
873 * generated_condition = (char*)calloc(size, sizeof(char));
875 for(idx = 0; idx < g_list_length(token_list); idx++)
877 token = (token_t*)g_list_nth_data(token_list, idx);
879 if((token != NULL) && STRING_VALID(token->str))
881 SAFE_STRLCAT(*generated_condition, token->str, size);
882 SAFE_STRLCAT(*generated_condition, SPACE, size);
884 SAFE_FREE(token->str);
889 if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
890 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
891 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
892 SAFE_STRLCAT(*generated_condition, SPACE, size);
895 //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
896 media_content_sec_debug("Condition : %s", *generated_condition);
898 //if(*generated_condition != NULL)
901 if(token_list != NULL)
902 g_list_free(token_list);
907 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
909 int ret = MEDIA_CONTENT_ERROR_NONE;
910 filter_s *_filter = NULL;
911 char option_query[DEFAULT_QUERY_SIZE] = {0, };
912 char condition[DEFAULT_QUERY_SIZE] = {0, };
914 //bool order_by = true;
916 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
917 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
919 _filter = (filter_s*)filter;
921 memset(option_query, 0x00, sizeof(option_query));
924 if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
926 unsigned int idx = 0;
927 int total_str_size = 0;
928 GList *token_list = NULL;
932 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
934 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
935 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
938 for(idx = 0; idx < g_list_length(token_list); idx++)
940 token = (token_t*)g_list_nth_data(token_list, idx);
942 if(token->type == UNKNOWN_TYPE)
944 char *replace_str = __media_filter_replace_attr(attr, token->str);
945 if(STRING_VALID(replace_str))
947 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
950 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
951 SAFE_FREE(replace_str);
955 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) {
956 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));
958 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
961 SAFE_FREE(token->str);
962 token->str = attr_str;
963 SAFE_FREE(replace_str);
967 media_content_error("There is no matched db field for %s", token->str);
971 total_str_size += strlen(token->str) + 1;
972 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
976 char *generated_condition = NULL;
977 size = total_str_size + COLLATE_STR_SIZE + 1;
978 generated_condition = (char*)calloc(size, sizeof(char));
980 for(idx = 0; idx < g_list_length(token_list); idx++)
982 token = (token_t*)g_list_nth_data(token_list, idx);
984 if((token != NULL) && STRING_VALID(token->str))
986 //media_content_debug("[%d] %s", idx, token->str);
987 SAFE_STRLCAT(generated_condition, token->str, size);
988 SAFE_STRLCAT(generated_condition, SPACE, size);
990 SAFE_FREE(token->str);
995 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
996 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
998 if(token_list != NULL)
999 g_list_free(token_list);
1001 SAFE_FREE(generated_condition);
1005 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
1006 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
1007 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1009 if(STRING_VALID(option_query))
1011 *generated_option = strdup(option_query);
1015 *generated_option = NULL;
1021 int media_filter_create(filter_h *filter)
1023 int ret = MEDIA_CONTENT_ERROR_NONE;
1025 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
1027 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
1028 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1030 _filter->storage_id = NULL;
1031 _filter->condition = NULL;
1032 _filter->order_keyword = NULL;
1033 _filter->order_type = -1;
1034 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1035 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1036 _filter->offset = -1;
1037 _filter->count = -1;
1039 *filter = (filter_h)_filter;
1044 int media_filter_destroy(filter_h filter)
1046 int ret = MEDIA_CONTENT_ERROR_NONE;
1047 filter_s *_filter = (filter_s*)filter;
1051 SAFE_FREE(_filter->storage_id);
1052 SAFE_FREE(_filter->condition);
1053 SAFE_FREE(_filter->order_keyword);
1056 ret = MEDIA_CONTENT_ERROR_NONE;
1060 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1061 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1067 int media_filter_set_offset(filter_h filter, int offset, int count)
1069 int ret = MEDIA_CONTENT_ERROR_NONE;
1070 filter_s *_filter = (filter_s*)filter;
1074 _filter->offset = offset;
1075 _filter->count = count;
1079 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1080 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1086 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1088 int ret = MEDIA_CONTENT_ERROR_NONE;
1089 filter_s *_filter = (filter_s*)filter;
1091 if((_filter != NULL) && STRING_VALID(condition)
1092 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1094 if(STRING_VALID(_filter->condition))
1096 SAFE_FREE(_filter->condition);
1099 _filter->condition = strdup(condition);
1100 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1102 media_content_sec_debug("Condition string : %s", _filter->condition);
1104 _filter->condition_collate_type = collate_type;
1108 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1109 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1115 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1117 int ret = MEDIA_CONTENT_ERROR_NONE;
1118 filter_s *_filter = (filter_s*)filter;
1120 if((_filter != NULL) && STRING_VALID(order_keyword)
1121 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1122 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1124 SAFE_FREE(_filter->order_keyword);
1126 if(STRING_VALID(order_keyword))
1128 _filter->order_keyword = strdup(order_keyword);
1129 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1131 _filter->order_type = order_type;
1132 _filter->order_collate_type = collate_type;
1136 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1137 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1142 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1143 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1149 int media_filter_set_storage(filter_h filter, const char *storage_id)
1151 int ret = MEDIA_CONTENT_ERROR_NONE;
1152 filter_s *_filter = (filter_s*)filter;
1154 if((_filter != NULL) && STRING_VALID(storage_id))
1156 if(STRING_VALID(_filter->storage_id))
1158 SAFE_FREE(_filter->storage_id);
1161 _filter->storage_id = strdup(storage_id);
1162 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1164 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1168 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1169 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1175 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1177 int ret = MEDIA_CONTENT_ERROR_NONE;
1178 filter_s *_filter = (filter_s*)filter;
1182 *offset = _filter->offset;
1183 *count = _filter->count;
1187 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1188 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1194 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1196 int ret = MEDIA_CONTENT_ERROR_NONE;
1197 filter_s *_filter = (filter_s*)filter;
1201 if(STRING_VALID(_filter->condition))
1203 *condition = strdup(_filter->condition);
1204 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1211 *collate_type = _filter->condition_collate_type;
1215 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1216 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1222 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1224 int ret = MEDIA_CONTENT_ERROR_NONE;
1225 filter_s *_filter = (filter_s*)filter;
1229 if(STRING_VALID(_filter->order_keyword))
1231 *order_keyword = strdup(_filter->order_keyword);
1232 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1236 *order_keyword = NULL;
1239 *order_type = _filter->order_type;
1240 *collate_type = _filter->order_collate_type;
1244 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1245 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1251 int media_filter_get_storage(filter_h filter, char **storage_id)
1253 int ret = MEDIA_CONTENT_ERROR_NONE;
1254 filter_s *_filter = (filter_s*)filter;
1258 if(STRING_VALID(_filter->storage_id))
1260 *storage_id = strdup(_filter->storage_id);
1261 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1270 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1271 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;