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[] = {
36 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 const char *china = "zh_CN";
59 const char *hongkong = "zh_HK";
63 bool pinyin_support = FALSE;
64 media_svc_check_pinyin_support(&pinyin_support);
66 /*Check Language Setting*/
67 lang = vconf_get_str(VCONFKEY_LANGSET);
68 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
70 if ((strncmp(china, lang, strlen(china)) == 0) ||
71 (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
81 static char *__get_order_str(media_content_order_e order_enum)
84 case MEDIA_CONTENT_ORDER_ASC:
86 case MEDIA_CONTENT_ORDER_DESC:
87 return (char *)"DESC";
93 static char *__get_collate_str(media_content_collation_e collate_type)
95 switch (collate_type) {
96 case MEDIA_CONTENT_COLLATE_NOCASE:
97 return (char *)"NOCASE";
98 case MEDIA_CONTENT_COLLATE_RTRIM:
99 return (char *)"RTRIM";
100 case MEDIA_CONTENT_COLLATE_LOCALIZED:
101 if (__is_pinyin_needed())
102 return (char *)"NOCASE";
104 return (char *)"localized";
105 default: return (char *)" ";
109 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
115 static char *__media_filter_replace_attr(attribute_h attr, char *name)
117 char *key_temp = NULL;
118 char *generated_value = NULL;
119 attribute_s *_attr = (attribute_s *)attr;
121 if (!g_hash_table_lookup_extended(_attr->attr_map, name, (gpointer)&key_temp, (gpointer)&generated_value)) {
122 //can't find the value
123 //media_content_error("NOT_FOUND_VALUE(%s)", name);
127 if (STRING_VALID(generated_value))
128 return strdup(generated_value);
130 media_content_error("__media_filter_replace_attr fail");
135 static int __tokenize_operator(token_t *token, const char *str, int op_type)
138 const char *tmp = str;
140 if (token != NULL && STRING_VALID(tmp)) {
141 token->type = op_type;
142 int token_size = strlen(media_token[op_type]);
143 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
145 token->str = (char*)calloc(token_size+1, sizeof(char));
146 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
148 strncpy(token->str, tmp, token_size);
149 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
158 static int __tokenize_string(token_t *token, const char *str, int size)
161 const char *tmp = str;
163 if (token != NULL && STRING_VALID(tmp) && size > 0) {
164 token->str = (char*)calloc(size+1, sizeof(char));
165 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
167 token->type = UNKNOWN_TYPE;
168 strncpy(token->str, tmp, size);
169 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
177 static int __tokenize_attribute(GList **token_list, const char *str)
182 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
184 const char *tmp = str;
185 const char *dst_ptr = str + strlen(str);
187 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
188 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
189 if (tmp[idx] == ' ') { //" "
190 if (idx == 0) { // ignore the space.
195 token_t *token = (token_t*)calloc(1, sizeof(token_t));
196 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
198 token->type = UNKNOWN_TYPE;
199 token->str = (char*)calloc(idx+1, sizeof(char));
200 if (token->str == NULL) {
202 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
205 strncpy(token->str, tmp, idx);
206 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
207 *token_list = g_list_append(*token_list, token);
208 tmp = tmp +idx + strlen(media_token[0]);
210 } else if (tmp[idx] == ',') { // " , "
212 token_t *token = (token_t*)calloc(1, sizeof(token_t));
213 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
215 ret = __tokenize_string(token, tmp, idx);
218 media_content_error("tokenize error occured");
221 *token_list = g_list_append(*token_list, token);
226 token_t *token = (token_t*)calloc(1, sizeof(token_t));
227 int size = __tokenize_operator(token, tmp, 3);
229 if (token != NULL && STRING_VALID(token->str)) {
230 *token_list = g_list_append(*token_list, token);
235 media_content_error("tokenize error occured");
241 if (*tmp) { //remained string
242 token_t *token = (token_t*)calloc(1, sizeof(token_t));
243 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
245 ret = __tokenize_string(token, tmp, idx);
248 media_content_error("tokenize error occured");
252 if (token != NULL && STRING_VALID(token->str)) {
253 *token_list = g_list_append(*token_list, token);
256 media_content_error("tokenize error occured");
261 return MEDIA_CONTENT_ERROR_NONE;
264 static int __tokenize(GList **token_list, const char *str)
269 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
271 const char *tmp = str;
272 const char *dst_ptr = str + strlen(str);
274 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
275 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
276 if (tmp[idx] == media_token[0][0]) { //" "
277 if (idx == 0) { // ignore the space.
283 token_t *token = (token_t*)calloc(1, sizeof(token_t));
284 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
286 token->type = UNKNOWN_TYPE;
287 token->str = (char *)calloc(idx+1, sizeof(char));
288 if (token->str == NULL) {
290 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
293 strncpy(token->str, tmp, idx);
294 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
295 *token_list = g_list_append(*token_list, token);
296 tmp = tmp +idx + strlen(media_token[0]);
298 } else if (tmp[idx] == media_token[1][0]) { // " \" "
301 for (j = idx+1; tmp[j]; j++) { //find next quotation
302 if (tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0]) {
306 if (tmp[j] == media_token[1][0]) {
307 token_t *token = (token_t*)calloc(1, sizeof(token_t));
308 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
310 token->str = (char*) calloc(j+1+1, sizeof(char));
311 if (token->str == NULL) {
313 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
316 token->type = STRING_TYPE;
317 strncpy(token->str, tmp, j+1);
318 //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
319 *token_list = g_list_append(*token_list, token);
320 tmp = tmp + strlen(token->str);
327 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
328 token_t *token = (token_t*)calloc(1, sizeof(token_t));
329 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
331 token->str = (char *) calloc(j+1, sizeof(char));
332 if (token->str == NULL) {
334 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
337 token->type = UNKNOWN_TYPE;
338 strncpy(token->str, tmp, j);
339 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
340 *token_list = g_list_append(*token_list, token);
341 tmp = tmp +strlen(token->str);
344 } else if (tmp[idx] == media_token[2][0]) { // " \' "
347 for (j = idx+1; tmp[j]; j++) {
348 if (tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0]) {
352 if (tmp[j] == media_token[2][0]) {
353 token_t *token = (token_t*)calloc(1, sizeof(token_t));
354 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
356 token->str = (char *) calloc(j+1+1, sizeof(char));
357 if (token->str == NULL) {
359 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
362 token->type = STRING_TYPE;
363 strncpy(token->str, tmp, j+1);
364 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
365 *token_list = g_list_append(*token_list, token);
366 tmp = tmp + strlen(token->str);
373 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
374 token_t *token = (token_t*)calloc(1, sizeof(token_t));
375 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
377 token->str = (char*) calloc(j+1, sizeof(char));
378 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);
390 } else if (tmp[idx] == media_token[3][0]) { //"("
392 token_t *token = (token_t*)calloc(1, sizeof(token_t));
393 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
395 ret = __tokenize_string(token, tmp, idx);
398 media_content_error("tokenize error occured");
401 *token_list = g_list_append(*token_list, token);
405 token_t *token = (token_t*)calloc(1, sizeof(token_t));
406 int size = __tokenize_operator(token, tmp, 3);
408 if (token != NULL && STRING_VALID(token->str)) {
409 *token_list = g_list_append(*token_list, token);
414 media_content_error("tokenize error occured");
418 } else if (tmp[idx] == media_token[4][0]) { //")"
420 token_t *token = (token_t*)calloc(1, sizeof(token_t));
421 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
423 ret = __tokenize_string(token, tmp, idx);
426 media_content_error("tokenize error occured");
429 *token_list = g_list_append(*token_list, token);
433 token_t *token = (token_t*)calloc(1, sizeof(token_t));
434 int size = __tokenize_operator(token, tmp, 4);
436 if (token != NULL && STRING_VALID(token->str)) {
437 *token_list = g_list_append(*token_list, token);
442 media_content_error("tokenize error occured");
446 } else if (tmp[idx] == media_token[5][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);
454 media_content_error("tokenize error occured");
457 *token_list = g_list_append(*token_list, token);
461 token_t *token = (token_t*)calloc(1, sizeof(token_t));
462 int size = __tokenize_operator(token, tmp, 5);
464 if (token != NULL && STRING_VALID(token->str)) {
465 *token_list = g_list_append(*token_list, token);
470 media_content_error("tokenize error occured");
474 } else if (tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) { //"<=",
476 token_t *token = (token_t*)calloc(1, sizeof(token_t));
477 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
479 ret = __tokenize_string(token, tmp, idx);
482 media_content_error("tokenize error occured");
485 *token_list = g_list_append(*token_list, token);
489 token_t *token = (token_t*)calloc(1, sizeof(token_t));
490 int size = __tokenize_operator(token, tmp, 6);
492 if (token != NULL && STRING_VALID(token->str)) {
493 *token_list = g_list_append(*token_list, token);
498 media_content_error("tokenize error occured");
502 } else if (tmp[idx] == media_token[7][0]) { //"<",
504 token_t *token = (token_t*)calloc(1, sizeof(token_t));
505 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
507 ret = __tokenize_string(token, tmp, idx);
510 media_content_error("tokenize error occured");
513 *token_list = g_list_append(*token_list, token);
517 token_t *token = (token_t*)calloc(1, sizeof(token_t));
518 int size = __tokenize_operator(token, tmp, 7);
520 if (token != NULL && STRING_VALID(token->str)) {
521 *token_list = g_list_append(*token_list, token);
526 media_content_error("tokenize error occured");
530 } else if (tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) { //">=",
532 token_t *token = (token_t*)calloc(1, sizeof(token_t));
533 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
535 ret = __tokenize_string(token, tmp, idx);
538 media_content_error("tokenize error occured");
541 *token_list = g_list_append(*token_list, token);
545 token_t *token = (token_t*)calloc(1, sizeof(token_t));
546 int size = __tokenize_operator(token, tmp, 8);
548 if (token != NULL && STRING_VALID(token->str)) {
549 *token_list = g_list_append(*token_list, token);
554 media_content_error("tokenize error occured");
558 } else if (tmp[idx] == media_token[9][0]) { //">",
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);
566 media_content_error("tokenize error occured");
569 *token_list = g_list_append(*token_list, token);
573 token_t *token = (token_t*)calloc(1, sizeof(token_t));
574 int size = __tokenize_operator(token, tmp, 9);
576 if (token != NULL && STRING_VALID(token->str)) {
577 *token_list = g_list_append(*token_list, token);
582 media_content_error("tokenize error occured");
585 } else if (tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) { //"!=",
587 token_t *token = (token_t*)calloc(1, sizeof(token_t));
588 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
590 ret = __tokenize_string(token, tmp, idx);
593 media_content_error("tokenize error occured");
596 *token_list = g_list_append(*token_list, token);
600 token_t *token = (token_t*)calloc(1, sizeof(token_t));
601 int size = __tokenize_operator(token, tmp, 10);
603 if (token != NULL && STRING_VALID(token->str)) {
604 *token_list = g_list_append(*token_list, token);
609 media_content_error("tokenize error occured");
615 if (*tmp) { //remained string
616 token_t *token = (token_t*)calloc(1, sizeof(token_t));
617 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
619 ret = __tokenize_string(token, tmp, idx);
622 media_content_error("tokenize error occured");
626 if (token != NULL && STRING_VALID(token->str)) {
627 *token_list = g_list_append(*token_list, token);
630 media_content_error("tokenize error occured");
635 return MEDIA_CONTENT_ERROR_NONE;
638 int _media_filter_attribute_create(attribute_h *attr)
640 int ret = MEDIA_CONTENT_ERROR_NONE;
642 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
644 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
645 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
647 _attr->attr_map = g_hash_table_new(g_str_hash, g_str_equal);
648 *attr = (attribute_h)_attr;
653 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
655 int ret = MEDIA_CONTENT_ERROR_NONE;
657 char *_platform = NULL;
658 attribute_s *_attr = (attribute_s*)attr;
660 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
662 if (STRING_VALID(user_attr) && STRING_VALID(platform_attr)) {
663 _user = strdup(user_attr);
664 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
666 _platform = strdup(platform_attr);
667 if (_platform == NULL) {
669 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
670 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
673 g_hash_table_insert(_attr->attr_map, _user, _platform);
675 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
676 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
682 int _media_filter_attribute_destory(attribute_h attr)
684 int ret = MEDIA_CONTENT_ERROR_NONE;
685 attribute_s *_attr = (attribute_s*)attr;
687 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
689 if (_attr->attr_map != NULL) {
690 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
691 g_hash_table_destroy(_attr->attr_map);
699 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
701 unsigned int idx = 0;
702 GList *token_list = NULL;
704 int ret = MEDIA_CONTENT_ERROR_NONE;
705 int total_str_size = 0;
708 media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
709 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
710 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
712 if (__tokenize(&token_list, condition) < 0) {
713 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
714 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
717 for (idx = 0; idx < g_list_length(token_list); idx++) {
718 token = (token_t*)g_list_nth_data(token_list, idx);
720 if (token->type == UNKNOWN_TYPE) {
721 char *replace_str = __media_filter_replace_attr(attr, token->str);
722 if (STRING_VALID(replace_str)) {
723 SAFE_FREE(token->str);
724 token->str = replace_str;
728 total_str_size += strlen(token->str)+1;
729 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
733 size = total_str_size + COLLATE_STR_SIZE + 1;
734 *generated_condition = (char*)calloc(size, sizeof(char));
736 for (idx = 0; idx < g_list_length(token_list); idx++) {
737 token = (token_t*)g_list_nth_data(token_list, idx);
739 if ((token != NULL) && STRING_VALID(token->str)) {
740 SAFE_STRLCAT(*generated_condition, token->str, size);
741 SAFE_STRLCAT(*generated_condition, SPACE, size);
743 SAFE_FREE(token->str);
748 if (collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM || collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
749 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
750 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
751 SAFE_STRLCAT(*generated_condition, SPACE, size);
754 //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
755 media_content_sec_debug("Condition : %s", *generated_condition);
757 //if(*generated_condition != NULL)
760 if (token_list != NULL)
761 g_list_free(token_list);
766 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
768 int ret = MEDIA_CONTENT_ERROR_NONE;
769 filter_s *_filter = NULL;
770 char option_query[DEFAULT_QUERY_SIZE] = {0, };
771 char condition[DEFAULT_QUERY_SIZE] = {0, };
773 //bool order_by = true;
775 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
776 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
778 _filter = (filter_s*)filter;
780 memset(option_query, 0x00, sizeof(option_query));
783 if (STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) || (_filter->order_type == MEDIA_CONTENT_ORDER_DESC))) {
784 unsigned int idx = 0;
785 int total_str_size = 0;
786 GList *token_list = NULL;
790 if (__tokenize_attribute(&token_list, _filter->order_keyword) < 0) {
791 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
792 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
795 for (idx = 0; idx < g_list_length(token_list); idx++) {
796 token = (token_t*)g_list_nth_data(token_list, idx);
798 if (token->type == UNKNOWN_TYPE) {
799 char *replace_str = __media_filter_replace_attr(attr, token->str);
800 if (STRING_VALID(replace_str)) {
801 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
802 if (attr_str == NULL) {
803 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
804 SAFE_FREE(replace_str);
808 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) {
809 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));
811 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
814 SAFE_FREE(token->str);
815 token->str = attr_str;
816 SAFE_FREE(replace_str);
818 media_content_error("There is no matched db field for %s", token->str);
822 total_str_size += strlen(token->str) + 1;
823 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
827 char *generated_condition = NULL;
828 size = total_str_size + COLLATE_STR_SIZE + 1;
829 generated_condition = (char*)calloc(size, sizeof(char));
831 for (idx = 0; idx < g_list_length(token_list); idx++) {
832 token = (token_t*)g_list_nth_data(token_list, idx);
834 if ((token != NULL) && STRING_VALID(token->str)) {
835 //media_content_debug("[%d] %s", idx, token->str);
836 SAFE_STRLCAT(generated_condition, token->str, size);
837 SAFE_STRLCAT(generated_condition, SPACE, size);
839 SAFE_FREE(token->str);
844 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
845 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
847 if (token_list != NULL)
848 g_list_free(token_list);
850 SAFE_FREE(generated_condition);
854 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
855 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
856 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
858 if (STRING_VALID(option_query))
859 *generated_option = strdup(option_query);
861 *generated_option = NULL;
866 int media_filter_create(filter_h *filter)
868 int ret = MEDIA_CONTENT_ERROR_NONE;
870 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
872 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
873 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
875 _filter->storage_id = NULL;
876 _filter->condition = NULL;
877 _filter->order_keyword = NULL;
878 _filter->order_type = -1;
879 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
880 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
881 _filter->offset = -1;
884 *filter = (filter_h)_filter;
889 int media_filter_destroy(filter_h filter)
891 int ret = MEDIA_CONTENT_ERROR_NONE;
892 filter_s *_filter = (filter_s*)filter;
895 SAFE_FREE(_filter->storage_id);
896 SAFE_FREE(_filter->condition);
897 SAFE_FREE(_filter->order_keyword);
900 ret = MEDIA_CONTENT_ERROR_NONE;
902 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
903 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
909 int media_filter_set_offset(filter_h filter, int offset, int count)
911 int ret = MEDIA_CONTENT_ERROR_NONE;
912 filter_s *_filter = (filter_s*)filter;
914 if (_filter != NULL) {
915 _filter->offset = offset;
916 _filter->count = count;
918 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
919 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
925 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
927 int ret = MEDIA_CONTENT_ERROR_NONE;
928 filter_s *_filter = (filter_s*)filter;
930 if ((_filter != NULL) && STRING_VALID(condition)
931 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
932 if (STRING_VALID(_filter->condition))
933 SAFE_FREE(_filter->condition);
935 _filter->condition = strdup(condition);
936 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
938 media_content_sec_debug("Condition string : %s", _filter->condition);
940 _filter->condition_collate_type = collate_type;
942 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
943 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
949 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
951 int ret = MEDIA_CONTENT_ERROR_NONE;
952 filter_s *_filter = (filter_s*)filter;
954 if ((_filter != NULL) && STRING_VALID(order_keyword)
955 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
956 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
957 SAFE_FREE(_filter->order_keyword);
959 if (STRING_VALID(order_keyword)) {
960 _filter->order_keyword = strdup(order_keyword);
961 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
963 _filter->order_type = order_type;
964 _filter->order_collate_type = collate_type;
966 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
967 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
970 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
971 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
977 int media_filter_set_storage(filter_h filter, const char *storage_id)
979 int ret = MEDIA_CONTENT_ERROR_NONE;
980 filter_s *_filter = (filter_s*)filter;
982 if ((_filter != NULL) && STRING_VALID(storage_id)) {
983 if (STRING_VALID(_filter->storage_id))
984 SAFE_FREE(_filter->storage_id);
986 _filter->storage_id = strdup(storage_id);
987 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
989 media_content_sec_debug("storage_id : %s", _filter->storage_id);
991 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
992 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
998 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1000 int ret = MEDIA_CONTENT_ERROR_NONE;
1001 filter_s *_filter = (filter_s*)filter;
1004 *offset = _filter->offset;
1005 *count = _filter->count;
1007 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1008 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1014 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1016 int ret = MEDIA_CONTENT_ERROR_NONE;
1017 filter_s *_filter = (filter_s*)filter;
1020 if (STRING_VALID(_filter->condition)) {
1021 *condition = strdup(_filter->condition);
1022 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1027 *collate_type = _filter->condition_collate_type;
1029 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1030 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1036 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1038 int ret = MEDIA_CONTENT_ERROR_NONE;
1039 filter_s *_filter = (filter_s*)filter;
1042 if (STRING_VALID(_filter->order_keyword)) {
1043 *order_keyword = strdup(_filter->order_keyword);
1044 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1046 *order_keyword = NULL;
1049 *order_type = _filter->order_type;
1050 *collate_type = _filter->order_collate_type;
1052 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1053 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1059 int media_filter_get_storage(filter_h filter, char **storage_id)
1061 int ret = MEDIA_CONTENT_ERROR_NONE;
1062 filter_s *_filter = (filter_s*)filter;
1065 if (STRING_VALID(_filter->storage_id)) {
1066 *storage_id = strdup(_filter->storage_id);
1067 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1072 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1073 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;