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 const char *media_token[] =
36 typedef struct _token_t
43 #define MAX_LEFT_VALUE 512
46 #define UNKNOWN_TYPE 1000
47 #define STRING_TYPE 100
49 static char *__get_order_str(media_content_order_e order_enum);
50 static char *__get_collate_str(media_content_collation_e collate_type);
51 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
52 static char *__media_filter_replace_attr(attribute_h attr, char *name);
53 static int __tokenize_operator(token_t *token, const char *str, int op_type);
54 static int __tokenize(GList **token_list, const char *str);
56 static bool __is_pinyin_needed(void)
59 const char *china = "zh_CN";
60 const char *hongkong = "zh_HK";
64 bool pinyin_support = FALSE;
65 media_svc_check_pinyin_support(&pinyin_support);
68 /*Check Language Setting*/
69 lang = vconf_get_str(VCONFKEY_LANGSET);
70 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
72 if((strncmp(china, lang, strlen(china)) == 0) ||
73 (strncmp(hongkong, lang, strlen(hongkong)) == 0))
84 static char *__get_order_str(media_content_order_e order_enum)
87 case MEDIA_CONTENT_ORDER_ASC:
89 case MEDIA_CONTENT_ORDER_DESC:
90 return (char *)"DESC";
96 static char *__get_collate_str(media_content_collation_e collate_type)
98 switch(collate_type) {
99 case MEDIA_CONTENT_COLLATE_NOCASE:
100 return (char *)"NOCASE";
101 case MEDIA_CONTENT_COLLATE_RTRIM:
102 return (char *)"RTRIM";
103 case MEDIA_CONTENT_COLLATE_LOCALIZED:
104 if(__is_pinyin_needed())
105 return (char *)"NOCASE";
107 return (char *)"localized";
108 default: return (char *)" ";
112 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
118 static char *__media_filter_replace_attr(attribute_h attr, char *name)
120 char *key_temp = NULL;
121 char *generated_value = NULL;
122 attribute_s *_attr = (attribute_s *)attr;
124 if(!g_hash_table_lookup_extended(_attr->attr_map,
126 (gpointer)&key_temp, (gpointer)&generated_value))
128 //can't find the value
129 //media_content_error("NOT_FOUND_VALUE(%s)", name);
133 if(STRING_VALID(generated_value))
135 return strdup(generated_value);
138 media_content_error("__media_filter_replace_attr fail");
143 static int __tokenize_operator(token_t *token, const char *str, int op_type)
146 const char *tmp = str;
148 if(token != NULL && STRING_VALID(tmp))
150 token->type = op_type;
151 int token_size = strlen(media_token[op_type]);
152 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
154 token->str = (char*)calloc(token_size+1, sizeof(char));
155 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
157 strncpy(token->str, tmp, token_size);
158 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
169 static int __tokenize_string(token_t *token, const char *str, int size)
172 const char *tmp = str;
174 if(token != NULL && STRING_VALID(tmp) && size > 0)
176 token->str = (char*)calloc(size+1, sizeof(char));
177 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
179 token->type = UNKNOWN_TYPE;
180 strncpy(token->str, tmp, size);
181 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
191 static int __tokenize_attribute(GList **token_list, const char *str)
196 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
198 const char *tmp = str;
199 const char *dst_ptr = str + strlen(str);
201 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
203 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
204 if(tmp[idx] == ' ') //" "
206 if(idx == 0) // ignore the space.
212 token_t *token = (token_t*)calloc(1, sizeof(token_t));
213 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
215 token->type = UNKNOWN_TYPE;
216 token->str = (char*)calloc(idx+1, sizeof(char));
217 if(token->str == NULL)
220 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
223 strncpy(token->str, tmp, idx);
224 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
225 *token_list = g_list_append(*token_list, token);
226 tmp = tmp +idx + strlen(media_token[0]);
229 else if(tmp[idx] == ',') // " , "
233 token_t *token = (token_t*)calloc(1, sizeof(token_t));
234 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
236 ret = __tokenize_string(token, tmp, idx);
240 media_content_error("tokenize error occured");
245 *token_list = g_list_append(*token_list, token);
250 token_t *token = (token_t*)calloc(1, sizeof(token_t));
251 int size = __tokenize_operator(token, tmp,3);
253 if(token != NULL && STRING_VALID(token->str))
255 *token_list = g_list_append(*token_list, token);
262 media_content_error("tokenize error occured");
268 if(*tmp) //remained string
270 token_t *token = (token_t*)calloc(1, sizeof(token_t));
271 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
273 ret = __tokenize_string(token, tmp, idx);
277 media_content_error("tokenize error occured");
281 if(token != NULL && STRING_VALID(token->str))
283 *token_list = g_list_append(*token_list, token);
288 media_content_error("tokenize error occured");
293 return MEDIA_CONTENT_ERROR_NONE;
296 static int __tokenize(GList **token_list, const char *str)
301 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
303 const char *tmp = str;
304 const char *dst_ptr = str + strlen(str);
306 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
308 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
309 if(tmp[idx] == media_token[0][0]) //" "
311 if(idx == 0) // ignore the space.
318 token_t *token = (token_t*)calloc(1, sizeof(token_t));
319 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
321 token->type = UNKNOWN_TYPE;
322 token->str = (char*)calloc(idx+1, sizeof(char));
323 if(token->str == NULL)
326 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
329 strncpy(token->str, tmp, idx);
330 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
331 *token_list = g_list_append(*token_list, token);
332 tmp = tmp +idx + strlen(media_token[0]);
335 else if(tmp[idx] == media_token[1][0]) // " \" "
339 for(j = idx+1; tmp[j]; j++) //find next quotation
341 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
346 if(tmp[j] == media_token[1][0])
348 token_t *token = (token_t*)calloc(1, sizeof(token_t));
349 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
351 token->str = (char*) calloc(j+1+1, sizeof(char));
352 if(token->str == NULL)
355 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
358 token->type = STRING_TYPE;
359 strncpy(token->str, tmp, j+1);
360 //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
361 *token_list = g_list_append(*token_list, token);
362 tmp = tmp + strlen(token->str);
369 if(!flag && *tmp != '\0' && tmp[j]=='\0')
371 token_t *token = (token_t*)calloc(1, sizeof(token_t));
372 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
374 token->str = (char*) calloc(j+1,sizeof(char));
375 if(token->str == NULL)
378 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
381 token->type = UNKNOWN_TYPE;
382 strncpy(token->str, tmp,j);
383 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
384 *token_list = g_list_append(*token_list, token);
385 tmp = tmp +strlen(token->str);
389 else if(tmp[idx] == media_token[2][0]) // " \' "
393 for(j = idx+1; tmp[j]; j++)
395 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
400 if(tmp[j] == media_token[2][0])
402 token_t *token = (token_t*)calloc(1, sizeof(token_t));
403 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
405 token->str = (char*) calloc(j+1+1, sizeof(char));
406 if(token->str == NULL)
409 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
412 token->type = STRING_TYPE;
413 strncpy(token->str, tmp, j+1);
414 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
415 *token_list = g_list_append(*token_list, token);
416 tmp = tmp + strlen(token->str);
423 if(!flag && *tmp != '\0' && tmp[j]=='\0')
425 token_t *token = (token_t*)calloc(1, sizeof(token_t));
426 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
428 token->str = (char*) calloc(j+1,sizeof(char));
429 if(token->str == NULL)
432 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
435 token->type = UNKNOWN_TYPE;
436 strncpy(token->str, tmp,j);
437 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
438 *token_list = g_list_append(*token_list, token);
439 tmp = tmp + strlen(token->str);
443 else if(tmp[idx] == media_token[3][0]) //"("
447 token_t *token = (token_t*)calloc(1, sizeof(token_t));
448 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
450 ret = __tokenize_string(token, tmp, idx);
454 media_content_error("tokenize error occured");
459 *token_list = g_list_append(*token_list, token);
463 token_t *token = (token_t*)calloc(1, sizeof(token_t));
464 int size = __tokenize_operator(token, tmp,3);
466 if(token != NULL && STRING_VALID(token->str))
468 *token_list = g_list_append(*token_list, token);
475 media_content_error("tokenize error occured");
480 else if(tmp[idx] == media_token[4][0]) //")"
484 token_t *token = (token_t*)calloc(1, sizeof(token_t));
485 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
487 ret = __tokenize_string(token, tmp, idx);
491 media_content_error("tokenize error occured");
496 *token_list = g_list_append(*token_list, token);
500 token_t *token = (token_t*)calloc(1, sizeof(token_t));
501 int size = __tokenize_operator(token, tmp,4);
503 if(token != NULL && STRING_VALID(token->str))
505 *token_list = g_list_append(*token_list, token);
512 media_content_error("tokenize error occured");
517 else if(tmp[idx] == media_token[5][0]) //"="
521 token_t *token = (token_t*)calloc(1, sizeof(token_t));
522 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
524 ret = __tokenize_string(token, tmp, idx);
528 media_content_error("tokenize error occured");
533 *token_list = g_list_append(*token_list, token);
537 token_t *token = (token_t*)calloc(1, sizeof(token_t));
538 int size = __tokenize_operator(token, tmp,5);
540 if(token != NULL && STRING_VALID(token->str))
542 *token_list = g_list_append(*token_list, token);
549 media_content_error("tokenize error occured");
554 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) //"<=",
558 token_t *token = (token_t*)calloc(1, sizeof(token_t));
559 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
561 ret = __tokenize_string(token, tmp, idx);
565 media_content_error("tokenize error occured");
570 *token_list = g_list_append(*token_list, token);
574 token_t *token = (token_t*)calloc(1, sizeof(token_t));
575 int size = __tokenize_operator(token, tmp,6);
577 if(token != NULL && STRING_VALID(token->str))
579 *token_list = g_list_append(*token_list, token);
586 media_content_error("tokenize error occured");
591 else if(tmp[idx] == media_token[7][0]) //"<",
595 token_t *token = (token_t*)calloc(1, sizeof(token_t));
596 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
598 ret = __tokenize_string(token, tmp, idx);
602 media_content_error("tokenize error occured");
607 *token_list = g_list_append(*token_list, token);
611 token_t *token = (token_t*)calloc(1, sizeof(token_t));
612 int size = __tokenize_operator(token, tmp,7);
614 if(token != NULL && STRING_VALID(token->str))
616 *token_list = g_list_append(*token_list, token);
623 media_content_error("tokenize error occured");
628 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) //">=",
632 token_t *token = (token_t*)calloc(1, sizeof(token_t));
633 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
635 ret = __tokenize_string(token, tmp, idx);
639 media_content_error("tokenize error occured");
644 *token_list = g_list_append(*token_list, token);
648 token_t *token = (token_t*)calloc(1, sizeof(token_t));
649 int size = __tokenize_operator(token, tmp,8);
651 if(token != NULL && STRING_VALID(token->str))
653 *token_list = g_list_append(*token_list, token);
660 media_content_error("tokenize error occured");
665 else if(tmp[idx] == media_token[9][0]) //">",
669 token_t *token = (token_t*)calloc(1, sizeof(token_t));
670 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
672 ret = __tokenize_string(token, tmp, idx);
676 media_content_error("tokenize error occured");
681 *token_list = g_list_append(*token_list, token);
685 token_t *token = (token_t*)calloc(1, sizeof(token_t));
686 int size = __tokenize_operator(token, tmp,9);
688 if(token != NULL && STRING_VALID(token->str))
690 *token_list = g_list_append(*token_list, token);
697 media_content_error("tokenize error occured");
701 else if(tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) //"!=",
705 token_t *token = (token_t*)calloc(1, sizeof(token_t));
706 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
708 ret = __tokenize_string(token, tmp, idx);
712 media_content_error("tokenize error occured");
717 *token_list = g_list_append(*token_list, token);
721 token_t *token = (token_t*)calloc(1, sizeof(token_t));
722 int size = __tokenize_operator(token, tmp, 10);
724 if(token != NULL && STRING_VALID(token->str))
726 *token_list = g_list_append(*token_list, token);
733 media_content_error("tokenize error occured");
739 if(*tmp) //remained string
741 token_t *token = (token_t*)calloc(1, sizeof(token_t));
742 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
744 ret = __tokenize_string(token, tmp, idx);
748 media_content_error("tokenize error occured");
752 if(token != NULL && STRING_VALID(token->str))
754 *token_list = g_list_append(*token_list, token);
759 media_content_error("tokenize error occured");
764 return MEDIA_CONTENT_ERROR_NONE;
767 int _media_filter_attribute_create(attribute_h *attr)
769 int ret = MEDIA_CONTENT_ERROR_NONE;
771 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
773 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
774 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
776 _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
777 *attr = (attribute_h)_attr;
782 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
784 int ret = MEDIA_CONTENT_ERROR_NONE;
786 char *_platform = NULL;
787 attribute_s *_attr = (attribute_s*)attr;
789 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
791 if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
793 _user = strdup(user_attr);
794 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
796 _platform = strdup(platform_attr);
797 if(_platform == NULL)
800 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
801 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
804 g_hash_table_insert (_attr->attr_map, _user, _platform);
808 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
809 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
815 int _media_filter_attribute_destory(attribute_h attr)
817 int ret = MEDIA_CONTENT_ERROR_NONE;
818 attribute_s *_attr = (attribute_s*)attr;
820 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
822 if(_attr->attr_map != NULL)
824 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
825 g_hash_table_destroy(_attr->attr_map);
833 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
835 unsigned int idx = 0;
836 GList *token_list = NULL;
838 int ret = MEDIA_CONTENT_ERROR_NONE;
839 int total_str_size = 0;
842 media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
843 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
844 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
846 if(__tokenize(&token_list, condition) < 0)
848 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
849 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
852 for(idx = 0; idx < g_list_length(token_list); idx++)
854 token = (token_t*)g_list_nth_data(token_list, idx);
856 if(token->type == UNKNOWN_TYPE)
858 char *replace_str = __media_filter_replace_attr(attr, token->str);
859 if(STRING_VALID(replace_str))
861 SAFE_FREE(token->str);
862 token->str = replace_str;
866 total_str_size += strlen(token->str)+1;
867 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
871 size = total_str_size + COLLATE_STR_SIZE + 1;
872 * generated_condition = (char*)calloc(size, sizeof(char));
874 for(idx = 0; idx < g_list_length(token_list); idx++)
876 token = (token_t*)g_list_nth_data(token_list, idx);
878 if((token != NULL) && STRING_VALID(token->str))
880 SAFE_STRLCAT(*generated_condition, token->str, size);
881 SAFE_STRLCAT(*generated_condition, SPACE, size);
883 SAFE_FREE(token->str);
888 if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
889 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
890 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
891 SAFE_STRLCAT(*generated_condition, SPACE, size);
894 //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
895 media_content_sec_debug("Condition : %s", *generated_condition);
897 //if(*generated_condition != NULL)
900 if(token_list != NULL)
901 g_list_free(token_list);
906 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
908 int ret = MEDIA_CONTENT_ERROR_NONE;
909 filter_s *_filter = NULL;
910 char option_query[DEFAULT_QUERY_SIZE] = {0, };
911 char condition[DEFAULT_QUERY_SIZE] = {0, };
913 //bool order_by = true;
915 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
916 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
918 _filter = (filter_s*)filter;
920 memset(option_query, 0x00, sizeof(option_query));
923 if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
925 unsigned int idx = 0;
926 int total_str_size = 0;
927 GList *token_list = NULL;
931 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
933 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
934 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
937 for(idx = 0; idx < g_list_length(token_list); idx++)
939 token = (token_t*)g_list_nth_data(token_list, idx);
941 if(token->type == UNKNOWN_TYPE)
943 char *replace_str = __media_filter_replace_attr(attr, token->str);
944 if(STRING_VALID(replace_str))
946 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
949 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
950 SAFE_FREE(replace_str);
954 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) {
955 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));
957 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
960 SAFE_FREE(token->str);
961 token->str = attr_str;
962 SAFE_FREE(replace_str);
966 media_content_error("There is no matched db field for %s", token->str);
970 total_str_size += strlen(token->str) + 1;
971 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
975 char *generated_condition = NULL;
976 size = total_str_size + COLLATE_STR_SIZE + 1;
977 generated_condition = (char*)calloc(size, sizeof(char));
979 for(idx = 0; idx < g_list_length(token_list); idx++)
981 token = (token_t*)g_list_nth_data(token_list, idx);
983 if((token != NULL) && STRING_VALID(token->str))
985 //media_content_debug("[%d] %s", idx, token->str);
986 SAFE_STRLCAT(generated_condition, token->str, size);
987 SAFE_STRLCAT(generated_condition, SPACE, size);
989 SAFE_FREE(token->str);
994 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
995 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
997 if(token_list != NULL)
998 g_list_free(token_list);
1000 SAFE_FREE(generated_condition);
1004 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
1005 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
1006 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1008 if(STRING_VALID(option_query))
1010 *generated_option = strdup(option_query);
1014 *generated_option = NULL;
1020 int media_filter_create(filter_h *filter)
1022 int ret = MEDIA_CONTENT_ERROR_NONE;
1024 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
1026 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
1027 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1029 _filter->storage_id = NULL;
1030 _filter->condition = NULL;
1031 _filter->order_keyword = NULL;
1032 _filter->order_type = -1;
1033 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1034 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1035 _filter->offset = -1;
1036 _filter->count = -1;
1038 *filter = (filter_h)_filter;
1043 int media_filter_destroy(filter_h filter)
1045 int ret = MEDIA_CONTENT_ERROR_NONE;
1046 filter_s *_filter = (filter_s*)filter;
1050 SAFE_FREE(_filter->storage_id);
1051 SAFE_FREE(_filter->condition);
1052 SAFE_FREE(_filter->order_keyword);
1055 ret = MEDIA_CONTENT_ERROR_NONE;
1059 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1060 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1066 int media_filter_set_offset(filter_h filter, int offset, int count)
1068 int ret = MEDIA_CONTENT_ERROR_NONE;
1069 filter_s *_filter = (filter_s*)filter;
1073 _filter->offset = offset;
1074 _filter->count = count;
1078 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1079 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1085 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1087 int ret = MEDIA_CONTENT_ERROR_NONE;
1088 filter_s *_filter = (filter_s*)filter;
1090 if((_filter != NULL) && STRING_VALID(condition)
1091 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1093 if(STRING_VALID(_filter->condition))
1095 SAFE_FREE(_filter->condition);
1098 _filter->condition = strdup(condition);
1099 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1101 media_content_sec_debug("Condition string : %s", _filter->condition);
1103 _filter->condition_collate_type = collate_type;
1107 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1108 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1114 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1116 int ret = MEDIA_CONTENT_ERROR_NONE;
1117 filter_s *_filter = (filter_s*)filter;
1119 if((_filter != NULL) && STRING_VALID(order_keyword)
1120 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1121 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1123 SAFE_FREE(_filter->order_keyword);
1125 if(STRING_VALID(order_keyword))
1127 _filter->order_keyword = strdup(order_keyword);
1128 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1130 _filter->order_type = order_type;
1131 _filter->order_collate_type = collate_type;
1135 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1136 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1141 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1142 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1148 int media_filter_set_storage(filter_h filter, const char *storage_id)
1150 int ret = MEDIA_CONTENT_ERROR_NONE;
1151 filter_s *_filter = (filter_s*)filter;
1153 if((_filter != NULL) && STRING_VALID(storage_id))
1155 if(STRING_VALID(_filter->storage_id))
1157 SAFE_FREE(_filter->storage_id);
1160 _filter->storage_id = strdup(storage_id);
1161 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1163 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1167 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1168 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1174 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1176 int ret = MEDIA_CONTENT_ERROR_NONE;
1177 filter_s *_filter = (filter_s*)filter;
1181 *offset = _filter->offset;
1182 *count = _filter->count;
1186 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1187 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1193 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1195 int ret = MEDIA_CONTENT_ERROR_NONE;
1196 filter_s *_filter = (filter_s*)filter;
1200 if(STRING_VALID(_filter->condition))
1202 *condition = strdup(_filter->condition);
1203 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1210 *collate_type = _filter->condition_collate_type;
1214 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1215 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1221 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1223 int ret = MEDIA_CONTENT_ERROR_NONE;
1224 filter_s *_filter = (filter_s*)filter;
1228 if(STRING_VALID(_filter->order_keyword))
1230 *order_keyword = strdup(_filter->order_keyword);
1231 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1235 *order_keyword = NULL;
1238 *order_type = _filter->order_type;
1239 *collate_type = _filter->order_collate_type;
1243 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1244 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1250 int media_filter_get_storage(filter_h filter, char **storage_id)
1252 int ret = MEDIA_CONTENT_ERROR_NONE;
1253 filter_s *_filter = (filter_s*)filter;
1257 if(STRING_VALID(_filter->storage_id))
1259 *storage_id = strdup(_filter->storage_id);
1260 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1269 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1270 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;