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)
60 const char *china = "zh_CN";
61 const char *hongkong = "zh_HK";
66 bool pinyin_support = FALSE;
67 media_svc_check_pinyin_support(&pinyin_support);
70 /*Check Language Setting*/
71 lang = vconf_get_str(VCONFKEY_LANGSET);
72 media_content_retvm_if(lang == NULL, ret, "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:
92 return (char *)"DESC";
98 static char *__get_collate_str(media_content_collation_e collate_type)
100 switch(collate_type) {
101 case MEDIA_CONTENT_COLLATE_NOCASE:
102 return (char *)"NOCASE";
103 case MEDIA_CONTENT_COLLATE_RTRIM:
104 return (char *)"RTRIM";
105 case MEDIA_CONTENT_COLLATE_LOCALIZED:
106 if(__is_pinyin_needed())
107 return (char *)"NOCASE";
109 return (char *)"localized";
110 default: return (char *)" ";
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]);
154 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
156 token->str = (char*)calloc(token_size+1, sizeof(char));
157 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
159 strncpy(token->str, tmp, token_size);
160 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
171 static int __tokenize_string(token_t *token, const char *str, int size)
174 const char *tmp = str;
176 if(token != NULL && STRING_VALID(tmp) && size > 0)
178 token->str = (char*)calloc(size+1, sizeof(char));
179 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
181 token->type = UNKNOWN_TYPE;
182 strncpy(token->str, tmp, size);
183 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
193 static int __tokenize_attribute(GList **token_list, const char *str)
198 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
200 const char *tmp = str;
201 const char *dst_ptr = str + strlen(str);
203 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
205 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
206 if(tmp[idx] == ' ') //" "
208 if(idx == 0) // ignore the space.
214 token_t *token = (token_t*)calloc(1, sizeof(token_t));
215 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
217 token->type = UNKNOWN_TYPE;
218 token->str = (char*)calloc(idx+1, sizeof(char));
219 if(token->str == NULL)
222 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
225 strncpy(token->str, tmp, idx);
226 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
227 *token_list = g_list_append(*token_list, token);
228 tmp = tmp +idx + strlen(media_token[0]);
231 else if(tmp[idx] == ',') // " , "
235 token_t *token = (token_t*)calloc(1, sizeof(token_t));
236 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
238 ret = __tokenize_string(token, tmp, idx);
242 media_content_error("tokenize error occured");
247 *token_list = g_list_append(*token_list, token);
252 token_t *token = (token_t*)calloc(1, sizeof(token_t));
253 int size = __tokenize_operator(token, tmp,3);
255 if(token != NULL && STRING_VALID(token->str))
257 *token_list = g_list_append(*token_list, token);
264 media_content_error("tokenize error occured");
270 if(*tmp) //remained string
272 token_t *token = (token_t*)calloc(1, sizeof(token_t));
273 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
275 ret = __tokenize_string(token, tmp, idx);
279 media_content_error("tokenize error occured");
283 if(token != NULL && STRING_VALID(token->str))
285 *token_list = g_list_append(*token_list, token);
290 media_content_error("tokenize error occured");
295 return MEDIA_CONTENT_ERROR_NONE;
298 static int __tokenize(GList **token_list, const char *str)
303 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
305 const char *tmp = str;
306 const char *dst_ptr = str + strlen(str);
308 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
310 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
311 if(tmp[idx] == media_token[0][0]) //" "
313 if(idx == 0) // ignore the space.
320 token_t *token = (token_t*)calloc(1, sizeof(token_t));
321 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
323 token->type = UNKNOWN_TYPE;
324 token->str = (char*)calloc(idx+1, sizeof(char));
325 if(token->str == NULL)
328 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
331 strncpy(token->str, tmp, idx);
332 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
333 *token_list = g_list_append(*token_list, token);
334 tmp = tmp +idx + strlen(media_token[0]);
337 else if(tmp[idx] == media_token[1][0]) // " \" "
341 for(j = idx+1; tmp[j]; j++) //find next quotation
343 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
348 if(tmp[j] == media_token[1][0])
350 token_t *token = (token_t*)calloc(1, sizeof(token_t));
351 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
353 token->str = (char*) calloc(j+1+1, sizeof(char));
354 if(token->str == NULL)
357 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
360 token->type = STRING_TYPE;
361 strncpy(token->str, tmp, j+1);
362 //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
363 *token_list = g_list_append(*token_list, token);
364 tmp = tmp + strlen(token->str);
371 if(!flag && *tmp != '\0' && tmp[j]=='\0')
373 token_t *token = (token_t*)calloc(1, sizeof(token_t));
374 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
376 token->str = (char*) calloc(j+1,sizeof(char));
377 if(token->str == NULL)
380 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
383 token->type = UNKNOWN_TYPE;
384 strncpy(token->str, tmp,j);
385 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
386 *token_list = g_list_append(*token_list, token);
387 tmp = tmp +strlen(token->str);
391 else if(tmp[idx] == media_token[2][0]) // " \' "
395 for(j = idx+1; tmp[j]; j++)
397 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
402 if(tmp[j] == media_token[2][0])
404 token_t *token = (token_t*)calloc(1, sizeof(token_t));
405 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
407 token->str = (char*) calloc(j+1+1, sizeof(char));
408 if(token->str == NULL)
411 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
414 token->type = STRING_TYPE;
415 strncpy(token->str, tmp, j+1);
416 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
417 *token_list = g_list_append(*token_list, token);
418 tmp = tmp + strlen(token->str);
425 if(!flag && *tmp != '\0' && tmp[j]=='\0')
427 token_t *token = (token_t*)calloc(1, sizeof(token_t));
428 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
430 token->str = (char*) calloc(j+1,sizeof(char));
431 if(token->str == NULL)
434 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
437 token->type = UNKNOWN_TYPE;
438 strncpy(token->str, tmp,j);
439 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
440 *token_list = g_list_append(*token_list, token);
441 tmp = tmp + strlen(token->str);
445 else if(tmp[idx] == media_token[3][0]) //"("
449 token_t *token = (token_t*)calloc(1, sizeof(token_t));
450 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
452 ret = __tokenize_string(token, tmp, idx);
456 media_content_error("tokenize error occured");
461 *token_list = g_list_append(*token_list, token);
465 token_t *token = (token_t*)calloc(1, sizeof(token_t));
466 int size = __tokenize_operator(token, tmp,3);
468 if(token != NULL && STRING_VALID(token->str))
470 *token_list = g_list_append(*token_list, token);
477 media_content_error("tokenize error occured");
482 else if(tmp[idx] == media_token[4][0]) //")"
486 token_t *token = (token_t*)calloc(1, sizeof(token_t));
487 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
489 ret = __tokenize_string(token, tmp, idx);
493 media_content_error("tokenize error occured");
498 *token_list = g_list_append(*token_list, token);
502 token_t *token = (token_t*)calloc(1, sizeof(token_t));
503 int size = __tokenize_operator(token, tmp,4);
505 if(token != NULL && STRING_VALID(token->str))
507 *token_list = g_list_append(*token_list, token);
514 media_content_error("tokenize error occured");
519 else if(tmp[idx] == media_token[5][0]) //"="
523 token_t *token = (token_t*)calloc(1, sizeof(token_t));
524 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
526 ret = __tokenize_string(token, tmp, idx);
530 media_content_error("tokenize error occured");
535 *token_list = g_list_append(*token_list, token);
539 token_t *token = (token_t*)calloc(1, sizeof(token_t));
540 int size = __tokenize_operator(token, tmp,5);
542 if(token != NULL && STRING_VALID(token->str))
544 *token_list = g_list_append(*token_list, token);
551 media_content_error("tokenize error occured");
556 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) //"<=",
560 token_t *token = (token_t*)calloc(1, sizeof(token_t));
561 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
563 ret = __tokenize_string(token, tmp, idx);
567 media_content_error("tokenize error occured");
572 *token_list = g_list_append(*token_list, token);
576 token_t *token = (token_t*)calloc(1, sizeof(token_t));
577 int size = __tokenize_operator(token, tmp,6);
579 if(token != NULL && STRING_VALID(token->str))
581 *token_list = g_list_append(*token_list, token);
588 media_content_error("tokenize error occured");
593 else if(tmp[idx] == media_token[7][0]) //"<",
597 token_t *token = (token_t*)calloc(1, sizeof(token_t));
598 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
600 ret = __tokenize_string(token, tmp, idx);
604 media_content_error("tokenize error occured");
609 *token_list = g_list_append(*token_list, token);
613 token_t *token = (token_t*)calloc(1, sizeof(token_t));
614 int size = __tokenize_operator(token, tmp,7);
616 if(token != NULL && STRING_VALID(token->str))
618 *token_list = g_list_append(*token_list, token);
625 media_content_error("tokenize error occured");
630 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) //">=",
634 token_t *token = (token_t*)calloc(1, sizeof(token_t));
635 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
637 ret = __tokenize_string(token, tmp, idx);
641 media_content_error("tokenize error occured");
646 *token_list = g_list_append(*token_list, token);
650 token_t *token = (token_t*)calloc(1, sizeof(token_t));
651 int size = __tokenize_operator(token, tmp,8);
653 if(token != NULL && STRING_VALID(token->str))
655 *token_list = g_list_append(*token_list, token);
662 media_content_error("tokenize error occured");
667 else if(tmp[idx] == media_token[9][0]) //">",
671 token_t *token = (token_t*)calloc(1, sizeof(token_t));
672 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
674 ret = __tokenize_string(token, tmp, idx);
678 media_content_error("tokenize error occured");
683 *token_list = g_list_append(*token_list, token);
687 token_t *token = (token_t*)calloc(1, sizeof(token_t));
688 int size = __tokenize_operator(token, tmp,9);
690 if(token != NULL && STRING_VALID(token->str))
692 *token_list = g_list_append(*token_list, token);
699 media_content_error("tokenize error occured");
703 else if(tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) //"!=",
707 token_t *token = (token_t*)calloc(1, sizeof(token_t));
708 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
710 ret = __tokenize_string(token, tmp, idx);
714 media_content_error("tokenize error occured");
719 *token_list = g_list_append(*token_list, token);
723 token_t *token = (token_t*)calloc(1, sizeof(token_t));
724 int size = __tokenize_operator(token, tmp, 10);
726 if(token != NULL && STRING_VALID(token->str))
728 *token_list = g_list_append(*token_list, token);
735 media_content_error("tokenize error occured");
741 if(*tmp) //remained string
743 token_t *token = (token_t*)calloc(1, sizeof(token_t));
744 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
746 ret = __tokenize_string(token, tmp, idx);
750 media_content_error("tokenize error occured");
754 if(token != NULL && STRING_VALID(token->str))
756 *token_list = g_list_append(*token_list, token);
761 media_content_error("tokenize error occured");
766 return MEDIA_CONTENT_ERROR_NONE;
769 int _media_filter_attribute_create(attribute_h *attr)
771 int ret = MEDIA_CONTENT_ERROR_NONE;
773 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
775 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
776 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
778 _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
779 *attr = (attribute_h)_attr;
784 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
786 int ret = MEDIA_CONTENT_ERROR_NONE;
788 char *_platform = NULL;
789 attribute_s *_attr = (attribute_s*)attr;
791 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
793 if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
795 _user = strdup(user_attr);
796 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
798 _platform = strdup(platform_attr);
799 if(_platform == NULL)
802 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
803 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
806 g_hash_table_insert (_attr->attr_map, _user, _platform);
810 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
811 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
817 int _media_filter_attribute_destory(attribute_h attr)
819 int ret = MEDIA_CONTENT_ERROR_NONE;
820 attribute_s *_attr = (attribute_s*)attr;
822 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
824 if(_attr->attr_map != NULL)
826 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
827 g_hash_table_destroy(_attr->attr_map);
835 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
837 unsigned int idx = 0;
838 GList *token_list = NULL;
840 int ret = MEDIA_CONTENT_ERROR_NONE;
841 int total_str_size = 0;
844 media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
845 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
846 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
848 if(__tokenize(&token_list, condition) < 0)
850 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
851 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
854 for(idx = 0; idx < g_list_length(token_list); idx++)
856 token = (token_t*)g_list_nth_data(token_list, idx);
858 if(token->type == UNKNOWN_TYPE)
860 char *replace_str = __media_filter_replace_attr(attr, token->str);
861 if(STRING_VALID(replace_str))
863 SAFE_FREE(token->str);
864 token->str = replace_str;
868 total_str_size += strlen(token->str)+1;
869 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
873 size = total_str_size + COLLATE_STR_SIZE + 1;
874 * generated_condition = (char*)calloc(size, sizeof(char));
876 for(idx = 0; idx < g_list_length(token_list); idx++)
878 token = (token_t*)g_list_nth_data(token_list, idx);
880 if((token != NULL) && STRING_VALID(token->str))
882 SAFE_STRLCAT(*generated_condition, token->str, size);
883 SAFE_STRLCAT(*generated_condition, SPACE, size);
885 SAFE_FREE(token->str);
890 if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
891 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
892 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
893 SAFE_STRLCAT(*generated_condition, SPACE, size);
896 //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
897 media_content_sec_debug("Condition : %s", *generated_condition);
899 //if(*generated_condition != NULL)
902 if(token_list != NULL)
903 g_list_free(token_list);
908 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
910 int ret = MEDIA_CONTENT_ERROR_NONE;
911 filter_s *_filter = NULL;
912 char option_query[DEFAULT_QUERY_SIZE] = {0, };
913 char condition[DEFAULT_QUERY_SIZE] = {0, };
915 //bool order_by = true;
917 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
918 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
920 _filter = (filter_s*)filter;
922 memset(option_query, 0x00, sizeof(option_query));
925 if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
927 unsigned int idx = 0;
928 int total_str_size = 0;
929 GList *token_list = NULL;
933 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
935 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
936 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
939 for(idx = 0; idx < g_list_length(token_list); idx++)
941 token = (token_t*)g_list_nth_data(token_list, idx);
943 if(token->type == UNKNOWN_TYPE)
945 char *replace_str = __media_filter_replace_attr(attr, token->str);
946 if(STRING_VALID(replace_str))
948 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
951 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
952 SAFE_FREE(replace_str);
956 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) {
957 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));
959 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
962 SAFE_FREE(token->str);
963 token->str = attr_str;
964 SAFE_FREE(replace_str);
968 media_content_error("There is no matched db field for %s", token->str);
972 total_str_size += strlen(token->str) + 1;
973 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
977 char *generated_condition = NULL;
978 size = total_str_size + COLLATE_STR_SIZE + 1;
979 generated_condition = (char*)calloc(size, sizeof(char));
981 for(idx = 0; idx < g_list_length(token_list); idx++)
983 token = (token_t*)g_list_nth_data(token_list, idx);
985 if((token != NULL) && STRING_VALID(token->str))
987 //media_content_debug("[%d] %s", idx, token->str);
988 SAFE_STRLCAT(generated_condition, token->str, size);
989 SAFE_STRLCAT(generated_condition, SPACE, size);
991 SAFE_FREE(token->str);
996 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
997 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
999 if(token_list != NULL)
1000 g_list_free(token_list);
1002 SAFE_FREE(generated_condition);
1006 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
1007 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
1008 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1010 if(STRING_VALID(option_query))
1012 *generated_option = strdup(option_query);
1016 *generated_option = NULL;
1022 int media_filter_create(filter_h *filter)
1024 int ret = MEDIA_CONTENT_ERROR_NONE;
1026 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
1028 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
1029 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1031 _filter->storage_id = NULL;
1032 _filter->condition = NULL;
1033 _filter->order_keyword = NULL;
1034 _filter->order_type = -1;
1035 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1036 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1037 _filter->offset = -1;
1038 _filter->count = -1;
1040 *filter = (filter_h)_filter;
1045 int media_filter_destroy(filter_h filter)
1047 int ret = MEDIA_CONTENT_ERROR_NONE;
1048 filter_s *_filter = (filter_s*)filter;
1052 SAFE_FREE(_filter->storage_id);
1053 SAFE_FREE(_filter->condition);
1054 SAFE_FREE(_filter->order_keyword);
1057 ret = MEDIA_CONTENT_ERROR_NONE;
1061 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1062 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1068 int media_filter_set_offset(filter_h filter, int offset, int count)
1070 int ret = MEDIA_CONTENT_ERROR_NONE;
1071 filter_s *_filter = (filter_s*)filter;
1075 _filter->offset = offset;
1076 _filter->count = count;
1080 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1081 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1087 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1089 int ret = MEDIA_CONTENT_ERROR_NONE;
1090 filter_s *_filter = (filter_s*)filter;
1092 if((_filter != NULL) && STRING_VALID(condition)
1093 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1095 if(STRING_VALID(_filter->condition))
1097 SAFE_FREE(_filter->condition);
1100 _filter->condition = strdup(condition);
1101 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1103 media_content_sec_debug("Condition string : %s", _filter->condition);
1105 _filter->condition_collate_type = collate_type;
1109 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1110 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1116 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1118 int ret = MEDIA_CONTENT_ERROR_NONE;
1119 filter_s *_filter = (filter_s*)filter;
1121 if((_filter != NULL) && STRING_VALID(order_keyword)
1122 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1123 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1125 SAFE_FREE(_filter->order_keyword);
1127 if(STRING_VALID(order_keyword))
1129 _filter->order_keyword = strdup(order_keyword);
1130 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1132 _filter->order_type = order_type;
1133 _filter->order_collate_type = collate_type;
1137 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1138 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1143 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1144 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1150 int media_filter_set_storage(filter_h filter, const char *storage_id)
1152 int ret = MEDIA_CONTENT_ERROR_NONE;
1153 filter_s *_filter = (filter_s*)filter;
1155 if((_filter != NULL) && STRING_VALID(storage_id))
1157 if(STRING_VALID(_filter->storage_id))
1159 SAFE_FREE(_filter->storage_id);
1162 _filter->storage_id = strdup(storage_id);
1163 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1165 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1169 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1170 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1176 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1178 int ret = MEDIA_CONTENT_ERROR_NONE;
1179 filter_s *_filter = (filter_s*)filter;
1183 *offset = _filter->offset;
1184 *count = _filter->count;
1188 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1189 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1195 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1197 int ret = MEDIA_CONTENT_ERROR_NONE;
1198 filter_s *_filter = (filter_s*)filter;
1202 if(STRING_VALID(_filter->condition))
1204 *condition = strdup(_filter->condition);
1205 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1212 *collate_type = _filter->condition_collate_type;
1216 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1217 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1223 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1225 int ret = MEDIA_CONTENT_ERROR_NONE;
1226 filter_s *_filter = (filter_s*)filter;
1230 if(STRING_VALID(_filter->order_keyword))
1232 *order_keyword = strdup(_filter->order_keyword);
1233 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1237 *order_keyword = NULL;
1240 *order_type = _filter->order_type;
1241 *collate_type = _filter->order_collate_type;
1245 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1246 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1252 int media_filter_get_storage(filter_h filter, char **storage_id)
1254 int ret = MEDIA_CONTENT_ERROR_NONE;
1255 filter_s *_filter = (filter_s*)filter;
1259 if(STRING_VALID(_filter->storage_id))
1261 *storage_id = strdup(_filter->storage_id);
1262 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1271 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1272 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;