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>
19 #include <media_util_private.h>
22 static const char *media_token[] = {
37 typedef struct _token_t {
42 #define UNKNOWN_TYPE 1000
43 #define STRING_TYPE 100
45 static char *__get_order_str(media_content_order_e order_enum);
46 static char *__get_collate_str(media_content_collation_e collate_type);
47 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
48 static char *__media_filter_replace_attr(attribute_h attr, char *name);
49 static int __tokenize_operator(token_t *token, const char *str, int op_type);
50 static int __tokenize(GList **token_list, const char *str);
52 static bool __is_pinyin_needed(void)
55 const char *china = "zh_CN";
56 const char *hongkong = "zh_HK";
60 bool pinyin_support = FALSE;
61 media_svc_check_pinyin_support(&pinyin_support);
63 /*Check Language Setting*/
64 lang = vconf_get_str(VCONFKEY_LANGSET);
65 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
67 if ((strncmp(china, lang, strlen(china)) == 0) ||
68 (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
78 static char *__get_order_str(media_content_order_e order_enum)
81 case MEDIA_CONTENT_ORDER_ASC:
83 case MEDIA_CONTENT_ORDER_DESC:
84 return (char *)"DESC";
90 static char *__get_collate_str(media_content_collation_e collate_type)
92 switch (collate_type) {
93 case MEDIA_CONTENT_COLLATE_NOCASE:
94 return (char *)"NOCASE";
95 case MEDIA_CONTENT_COLLATE_RTRIM:
96 return (char *)"RTRIM";
97 case MEDIA_CONTENT_COLLATE_LOCALIZED:
98 if (__is_pinyin_needed())
99 return (char *)"NOCASE";
101 return (char *)"localized";
102 default: return (char *)" ";
106 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
112 static char *__media_filter_replace_attr(attribute_h attr, char *name)
114 char *key_temp = NULL;
115 char *generated_value = NULL;
116 attribute_s *_attr = (attribute_s *)attr;
118 if (!g_hash_table_lookup_extended(_attr->attr_map, name, (gpointer)&key_temp, (gpointer)&generated_value)) {
119 /* can't find the value */
120 /* media_content_error("NOT_FOUND_VALUE(%s)", name); */
124 if (STRING_VALID(generated_value))
125 return strdup(generated_value);
127 media_content_error("__media_filter_replace_attr fail");
132 static int __tokenize_operator(token_t *token, const char *str, int op_type)
135 const char *tmp = str;
137 if (token != NULL && STRING_VALID(tmp)) {
138 token->type = op_type;
139 int token_size = strlen(media_token[op_type]);
140 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
142 token->str = (char*)calloc(token_size+1, sizeof(char));
143 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
145 strncpy(token->str, tmp, token_size);
146 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
155 static int __tokenize_string(token_t *token, const char *str, int size)
158 const char *tmp = str;
160 if (token != NULL && STRING_VALID(tmp) && size > 0) {
161 token->str = (char*)calloc(size+1, sizeof(char));
162 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
164 token->type = UNKNOWN_TYPE;
165 strncpy(token->str, tmp, size);
166 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
174 static int __tokenize_attribute(GList **token_list, const char *str)
179 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
181 const char *tmp = str;
182 const char *dst_ptr = str + strlen(str);
184 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
185 /*media_content_debug("[%d] '%c'", idx, tmp[idx]);*/
186 if (tmp[idx] == ' ') { /* " " */
187 if (idx == 0) { /* ignore the space. */
192 token_t *token = (token_t*)calloc(1, sizeof(token_t));
193 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
195 token->type = UNKNOWN_TYPE;
196 token->str = (char*)calloc(idx+1, sizeof(char));
197 if (token->str == NULL) {
199 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
202 strncpy(token->str, tmp, idx);
203 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
204 *token_list = g_list_append(*token_list, token);
205 tmp = tmp +idx + strlen(media_token[0]);
207 } else if (tmp[idx] == ',') { /* " , " */
209 token_t *token = (token_t*)calloc(1, sizeof(token_t));
210 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
212 ret = __tokenize_string(token, tmp, idx);
215 media_content_error("tokenize error occured");
218 *token_list = g_list_append(*token_list, token);
223 token_t *token = (token_t*)calloc(1, sizeof(token_t));
224 int size = __tokenize_operator(token, tmp, 3);
226 if (token != NULL && STRING_VALID(token->str)) {
227 *token_list = g_list_append(*token_list, token);
232 media_content_error("tokenize error occured");
238 if (*tmp) { /* remained string */
239 token_t *token = (token_t*)calloc(1, sizeof(token_t));
240 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
242 ret = __tokenize_string(token, tmp, idx);
245 media_content_error("tokenize error occured");
249 if (token != NULL && STRING_VALID(token->str)) {
250 *token_list = g_list_append(*token_list, token);
253 media_content_error("tokenize error occured");
258 return MEDIA_CONTENT_ERROR_NONE;
261 static int __tokenize(GList **token_list, const char *str)
266 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
268 const char *tmp = str;
269 const char *dst_ptr = str + strlen(str);
271 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
272 /* media_content_debug("[%d] '%c'", idx, tmp[idx]); */
273 if (tmp[idx] == media_token[0][0]) { /* " " */
274 if (idx == 0) { /* ignore the space. */
280 token_t *token = (token_t*)calloc(1, sizeof(token_t));
281 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
283 token->type = UNKNOWN_TYPE;
284 token->str = (char *)calloc(idx+1, sizeof(char));
285 if (token->str == NULL) {
287 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
290 strncpy(token->str, tmp, idx);
291 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
292 *token_list = g_list_append(*token_list, token);
293 tmp = tmp +idx + strlen(media_token[0]);
295 } else if (tmp[idx] == media_token[1][0]) { /* " \" " */
298 for (j = idx+1; tmp[j]; j++) { /* find next quotation */
299 if (tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0]) {
303 if (tmp[j] == media_token[1][0]) {
304 token_t *token = (token_t*)calloc(1, sizeof(token_t));
305 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
307 token->str = (char*) calloc(j+1+1, sizeof(char));
308 if (token->str == NULL) {
310 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
313 token->type = STRING_TYPE;
314 strncpy(token->str, tmp, j+1);
315 /* media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j); */
316 *token_list = g_list_append(*token_list, token);
317 tmp = tmp + strlen(token->str);
324 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
325 token_t *token = (token_t*)calloc(1, sizeof(token_t));
326 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
328 token->str = (char *) calloc(j+1, sizeof(char));
329 if (token->str == NULL) {
331 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
334 token->type = UNKNOWN_TYPE;
335 strncpy(token->str, tmp, j);
336 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
337 *token_list = g_list_append(*token_list, token);
338 tmp = tmp +strlen(token->str);
341 } else if (tmp[idx] == media_token[2][0]) { /* " \' "*/
344 for (j = idx+1; tmp[j]; j++) {
345 if (tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0]) {
349 if (tmp[j] == media_token[2][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) {
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]", token->type, token->str); */
362 *token_list = g_list_append(*token_list, token);
363 tmp = tmp + strlen(token->str);
370 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) {
377 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
380 token->type = UNKNOWN_TYPE;
381 strncpy(token->str, tmp, j);
382 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
383 *token_list = g_list_append(*token_list, token);
384 tmp = tmp + strlen(token->str);
387 } else if (tmp[idx] == media_token[3][0]) { /* "(" */
389 token_t *token = (token_t*)calloc(1, sizeof(token_t));
390 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
392 ret = __tokenize_string(token, tmp, idx);
395 media_content_error("tokenize error occured");
398 *token_list = g_list_append(*token_list, token);
402 token_t *token = (token_t*)calloc(1, sizeof(token_t));
403 int size = __tokenize_operator(token, tmp, 3);
405 if (token != NULL && STRING_VALID(token->str)) {
406 *token_list = g_list_append(*token_list, token);
411 media_content_error("tokenize error occured");
415 } else if (tmp[idx] == media_token[4][0]) { /* ")" */
417 token_t *token = (token_t*)calloc(1, sizeof(token_t));
418 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
420 ret = __tokenize_string(token, tmp, idx);
423 media_content_error("tokenize error occured");
426 *token_list = g_list_append(*token_list, token);
430 token_t *token = (token_t*)calloc(1, sizeof(token_t));
431 int size = __tokenize_operator(token, tmp, 4);
433 if (token != NULL && STRING_VALID(token->str)) {
434 *token_list = g_list_append(*token_list, token);
439 media_content_error("tokenize error occured");
443 } else if (tmp[idx] == media_token[5][0]) { /* "=" */
445 token_t *token = (token_t*)calloc(1, sizeof(token_t));
446 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
448 ret = __tokenize_string(token, tmp, idx);
451 media_content_error("tokenize error occured");
454 *token_list = g_list_append(*token_list, token);
458 token_t *token = (token_t*)calloc(1, sizeof(token_t));
459 int size = __tokenize_operator(token, tmp, 5);
461 if (token != NULL && STRING_VALID(token->str)) {
462 *token_list = g_list_append(*token_list, token);
467 media_content_error("tokenize error occured");
471 } else if (tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) { /* "<=", */
473 token_t *token = (token_t*)calloc(1, sizeof(token_t));
474 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
476 ret = __tokenize_string(token, tmp, idx);
479 media_content_error("tokenize error occured");
482 *token_list = g_list_append(*token_list, token);
486 token_t *token = (token_t*)calloc(1, sizeof(token_t));
487 int size = __tokenize_operator(token, tmp, 6);
489 if (token != NULL && STRING_VALID(token->str)) {
490 *token_list = g_list_append(*token_list, token);
495 media_content_error("tokenize error occured");
499 } else if (tmp[idx] == media_token[7][0]) { /* "<", */
501 token_t *token = (token_t*)calloc(1, sizeof(token_t));
502 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
504 ret = __tokenize_string(token, tmp, idx);
507 media_content_error("tokenize error occured");
510 *token_list = g_list_append(*token_list, token);
514 token_t *token = (token_t*)calloc(1, sizeof(token_t));
515 int size = __tokenize_operator(token, tmp, 7);
517 if (token != NULL && STRING_VALID(token->str)) {
518 *token_list = g_list_append(*token_list, token);
523 media_content_error("tokenize error occured");
527 } else if (tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) { /* ">=", */
529 token_t *token = (token_t*)calloc(1, sizeof(token_t));
530 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
532 ret = __tokenize_string(token, tmp, idx);
535 media_content_error("tokenize error occured");
538 *token_list = g_list_append(*token_list, token);
542 token_t *token = (token_t*)calloc(1, sizeof(token_t));
543 int size = __tokenize_operator(token, tmp, 8);
545 if (token != NULL && STRING_VALID(token->str)) {
546 *token_list = g_list_append(*token_list, token);
551 media_content_error("tokenize error occured");
555 } else if (tmp[idx] == media_token[9][0]) { /* ">", */
557 token_t *token = (token_t*)calloc(1, sizeof(token_t));
558 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
560 ret = __tokenize_string(token, tmp, idx);
563 media_content_error("tokenize error occured");
566 *token_list = g_list_append(*token_list, token);
570 token_t *token = (token_t*)calloc(1, sizeof(token_t));
571 int size = __tokenize_operator(token, tmp, 9);
573 if (token != NULL && STRING_VALID(token->str)) {
574 *token_list = g_list_append(*token_list, token);
579 media_content_error("tokenize error occured");
582 } else if (tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) { /* "!=", */
584 token_t *token = (token_t*)calloc(1, sizeof(token_t));
585 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
587 ret = __tokenize_string(token, tmp, idx);
590 media_content_error("tokenize error occured");
593 *token_list = g_list_append(*token_list, token);
597 token_t *token = (token_t*)calloc(1, sizeof(token_t));
598 int size = __tokenize_operator(token, tmp, 10);
600 if (token != NULL && STRING_VALID(token->str)) {
601 *token_list = g_list_append(*token_list, token);
606 media_content_error("tokenize error occured");
612 if (*tmp) { /* remained string */
613 token_t *token = (token_t*)calloc(1, sizeof(token_t));
614 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
616 ret = __tokenize_string(token, tmp, idx);
619 media_content_error("tokenize error occured");
623 if (token != NULL && STRING_VALID(token->str)) {
624 *token_list = g_list_append(*token_list, token);
627 media_content_error("tokenize error occured");
632 return MEDIA_CONTENT_ERROR_NONE;
635 int _media_filter_attribute_create(attribute_h *attr)
637 int ret = MEDIA_CONTENT_ERROR_NONE;
639 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
641 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
642 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
644 _attr->attr_map = g_hash_table_new(g_str_hash, g_str_equal);
645 *attr = (attribute_h)_attr;
650 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
652 int ret = MEDIA_CONTENT_ERROR_NONE;
654 char *_platform = NULL;
655 attribute_s *_attr = (attribute_s*)attr;
657 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
659 if (STRING_VALID(user_attr) && STRING_VALID(platform_attr)) {
660 _user = g_strdup(user_attr);
661 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
663 _platform = g_strdup(platform_attr);
664 if (_platform == NULL) {
666 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
667 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
670 g_hash_table_insert(_attr->attr_map, _user, _platform);
672 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
673 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
679 int _media_filter_attribute_destory(attribute_h attr)
681 int ret = MEDIA_CONTENT_ERROR_NONE;
682 attribute_s *_attr = (attribute_s*)attr;
684 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
686 if (_attr->attr_map != NULL) {
687 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
688 g_hash_table_destroy(_attr->attr_map);
696 int _media_filter_attribute_generate(attribute_h attr, filter_h filter, char **generated_condition)
698 unsigned int idx = 0;
699 GList *token_list = NULL;
700 int ret = MEDIA_CONTENT_ERROR_NONE;
702 filter_s *_filter = NULL;
703 char tmp_condition[MAX_QUERY_SIZE] = {0, };
705 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
706 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
707 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
709 _filter = (filter_s*)filter;
711 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
712 media_content_retvm_if(_filter->condition_collate_type < MEDIA_CONTENT_COLLATE_DEFAULT ||
713 _filter->condition_collate_type > MEDIA_CONTENT_COLLATE_LOCALIZED, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition collate");
715 if (__tokenize(&token_list, _filter->condition) < 0) {
716 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
717 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
720 for (idx = 0; idx < g_list_length(token_list); idx++) {
721 token = (token_t*)g_list_nth_data(token_list, idx);
723 if ((token != NULL) && (token->type == UNKNOWN_TYPE)) {
724 char *replace_str = __media_filter_replace_attr(attr, token->str);
725 if (STRING_VALID(replace_str)) {
726 SAFE_FREE(token->str);
727 token->str = replace_str;
729 SAFE_FREE(replace_str);
734 /* make the statment */
735 memset(tmp_condition, 0, sizeof(tmp_condition));
736 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_OPEN_BRACKET, sizeof(tmp_condition));
738 for (idx = 0; idx < g_list_length(token_list); idx++) {
739 token = (token_t*)g_list_nth_data(token_list, idx);
741 if ((token != NULL) && STRING_VALID(token->str)) {
742 SAFE_STRLCAT(tmp_condition, token->str, sizeof(tmp_condition));
743 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_SPACE, sizeof(tmp_condition));
745 SAFE_FREE(token->str);
750 /* Process for filter v1 */
751 if (_filter->is_full_condition == false && _filter->condition_collate_type != MEDIA_CONTENT_COLLATE_DEFAULT) {
752 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_COLLATE, sizeof(tmp_condition));
753 SAFE_STRLCAT(tmp_condition, __get_collate_str(_filter->condition_collate_type), sizeof(tmp_condition));
754 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_SPACE, sizeof(tmp_condition));
757 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_BRACKET, sizeof(tmp_condition));
759 if (STRING_VALID(tmp_condition))
760 *generated_condition = g_strdup(tmp_condition);
762 *generated_condition = NULL;
764 media_content_sec_debug("Condition : %s", *generated_condition);
766 if (token_list != NULL) {
767 g_list_free(token_list);
774 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
776 int ret = MEDIA_CONTENT_ERROR_NONE;
777 filter_s *_filter = NULL;
778 char query[DEFAULT_QUERY_SIZE] = {0, };
779 char option[DEFAULT_QUERY_SIZE] = {0, };
781 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
782 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
784 _filter = (filter_s*)filter;
786 memset(query, 0x00, sizeof(query));
789 if (STRING_VALID(_filter->order_keyword)) {
790 if ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) || (_filter->order_type == MEDIA_CONTENT_ORDER_DESC)) {
791 unsigned int idx = 0;
792 GList *token_list = NULL;
796 if (__tokenize_attribute(&token_list, _filter->order_keyword) < 0) {
797 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
798 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
801 for (idx = 0; idx < g_list_length(token_list); idx++) {
802 token = (token_t*)g_list_nth_data(token_list, idx);
804 if ((token != NULL) && (token->type == UNKNOWN_TYPE)) {
805 char *replace_str = __media_filter_replace_attr(attr, token->str);
806 if (STRING_VALID(replace_str)) {
807 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
808 if (attr_str == NULL) {
809 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
810 SAFE_FREE(replace_str);
814 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)
815 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));
817 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
819 SAFE_FREE(token->str);
820 token->str = attr_str;
821 SAFE_FREE(replace_str);
823 media_content_error("There is no matched db field for %s", token->str);
824 SAFE_FREE(replace_str);
829 /* make the statment */
830 SAFE_STRLCAT(query, QUERY_KEYWORD_ORDER_BY, sizeof(query));
832 for (idx = 0; idx < g_list_length(token_list); idx++) {
833 token = (token_t*)g_list_nth_data(token_list, idx);
835 if ((token != NULL) && STRING_VALID(token->str)) {
836 SAFE_STRLCAT(query, token->str, sizeof(query));
837 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
839 SAFE_FREE(token->str);
844 if (token_list != NULL) {
845 g_list_free(token_list);
849 SAFE_STRLCAT(query, _filter->order_keyword, sizeof(query));
854 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
856 memset(option, 0, sizeof(option));
857 snprintf(option, sizeof(option), "%s %d, %d", QUERY_KEYWORD_LIMIT, _filter->offset, _filter->count);
859 SAFE_STRLCAT(query, option, sizeof(query));
861 if (STRING_VALID(query)) {
862 *generated_option = g_strdup(query);
863 media_content_sec_debug("Option : %s", *generated_option);
865 *generated_option = NULL;
871 int _media_filter_attribute_option_generate_with_full_query(attribute_h attr, filter_h filter, char **generated_option)
873 unsigned int idx = 0;
874 GList *token_list = NULL;
875 int ret = MEDIA_CONTENT_ERROR_NONE;
876 token_t *token = NULL;
877 filter_s * _filter = NULL;
878 char query[DEFAULT_QUERY_SIZE] = {0, };
879 char option[DEFAULT_QUERY_SIZE] = {0, };
881 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
882 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
883 media_content_retvm_if(generated_option == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_option");
885 _filter = (filter_s*)filter;
887 memset(query, 0, sizeof(query));
890 if (STRING_VALID(_filter->order_keyword)) {
891 if (__tokenize_attribute(&token_list, _filter->order_keyword) < 0) {
892 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the order", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
893 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
896 for (idx = 0; idx < g_list_length(token_list); idx++) {
897 token = (token_t*)g_list_nth_data(token_list, idx);
899 if ((token != NULL) && (token->type == UNKNOWN_TYPE)) {
900 char *replace_str = __media_filter_replace_attr(attr, token->str);
901 if (STRING_VALID(replace_str)) {
902 SAFE_FREE(token->str);
903 token->str = replace_str;
905 SAFE_FREE(replace_str);
910 /* make the statment */
911 SAFE_STRLCAT(query, QUERY_KEYWORD_ORDER_BY, sizeof(query));
913 for (idx = 0; idx < g_list_length(token_list); idx++) {
914 token = (token_t*)g_list_nth_data(token_list, idx);
916 if ((token != NULL) && STRING_VALID(token->str)) {
917 SAFE_STRLCAT(query, token->str, sizeof(query));
918 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
920 SAFE_FREE(token->str);
925 if (token_list != NULL) {
926 g_list_free(token_list);
932 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
934 memset(option, 0, sizeof(option));
935 snprintf(option, sizeof(option), "%s %d, %d", QUERY_KEYWORD_LIMIT, _filter->offset, _filter->count);
936 SAFE_STRLCAT(query, option, sizeof(query));
938 if (STRING_VALID(query)) {
939 *generated_option = g_strdup(query);
940 media_content_sec_debug("Option : %s", *generated_option);
942 *generated_option = NULL;
949 int media_filter_create(filter_h *filter)
951 int ret = MEDIA_CONTENT_ERROR_NONE;
953 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
955 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
956 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
958 _filter->storage_id = NULL;
959 _filter->condition = NULL;
960 _filter->order_keyword = NULL;
961 _filter->order_type = -1;
962 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
963 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
964 _filter->offset = -1;
966 _filter->is_full_condition = false;
967 _filter->is_full_order = false;
969 *filter = (filter_h)_filter;
974 int media_filter_destroy(filter_h filter)
976 int ret = MEDIA_CONTENT_ERROR_NONE;
977 filter_s *_filter = (filter_s*)filter;
980 SAFE_FREE(_filter->storage_id);
981 SAFE_FREE(_filter->condition);
982 SAFE_FREE(_filter->order_keyword);
985 ret = MEDIA_CONTENT_ERROR_NONE;
987 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
988 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
994 int media_filter_set_offset(filter_h filter, int offset, int count)
996 int ret = MEDIA_CONTENT_ERROR_NONE;
997 filter_s *_filter = (filter_s*)filter;
999 if (_filter != NULL) {
1000 _filter->offset = offset;
1001 _filter->count = count;
1003 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1004 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1010 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1012 int ret = MEDIA_CONTENT_ERROR_NONE;
1013 filter_s *_filter = (filter_s*)filter;
1015 if ((_filter != NULL) && STRING_VALID(condition)
1016 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
1018 _filter->is_full_condition = false;
1020 if (STRING_VALID(_filter->condition))
1021 SAFE_FREE(_filter->condition);
1023 char new_condition[MAX_QUERY_SIZE] = {0, };
1024 memset(new_condition, 0, sizeof(new_condition));
1025 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
1026 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1028 _filter->condition = strdup(new_condition);
1029 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1031 media_content_sec_debug("Condition string : %s", _filter->condition);
1033 _filter->condition_collate_type = collate_type;
1035 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1036 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1042 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1044 int ret = MEDIA_CONTENT_ERROR_NONE;
1045 filter_s *_filter = (filter_s*)filter;
1047 if ((_filter != NULL) && STRING_VALID(order_keyword)
1048 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
1049 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
1051 _filter->is_full_order = false;
1053 SAFE_FREE(_filter->order_keyword);
1055 _filter->order_keyword = strdup(order_keyword);
1056 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1058 _filter->order_type = order_type;
1059 _filter->order_collate_type = collate_type;
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_storage(filter_h filter, const char *storage_id)
1070 int ret = MEDIA_CONTENT_ERROR_NONE;
1071 media_content_warn("DEPRECATION WARNING: media_filter_set_storage() is deprecated and will be removed from next release. Use media_filter_set_condition() with MEDIA_PATH keyword instead.");
1072 filter_s *_filter = (filter_s*)filter;
1074 if ((_filter != NULL) && STRING_VALID(storage_id)) {
1075 if (STRING_VALID(_filter->storage_id))
1076 SAFE_FREE(_filter->storage_id);
1078 _filter->storage_id = strdup(storage_id);
1079 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1081 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1083 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1084 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1090 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1092 int ret = MEDIA_CONTENT_ERROR_NONE;
1093 filter_s *_filter = (filter_s*)filter;
1096 *offset = _filter->offset;
1097 *count = _filter->count;
1099 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1100 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1106 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1108 int ret = MEDIA_CONTENT_ERROR_NONE;
1109 filter_s *_filter = (filter_s*)filter;
1112 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == false) {
1113 char new_condition[MAX_QUERY_SIZE] = {0, };
1114 memset(new_condition, 0, sizeof(new_condition));
1115 ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
1116 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1118 *condition = strdup(new_condition);
1119 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1124 *collate_type = _filter->condition_collate_type;
1126 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1127 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1133 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1135 int ret = MEDIA_CONTENT_ERROR_NONE;
1136 filter_s *_filter = (filter_s*)filter;
1139 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == false) {
1140 *order_keyword = strdup(_filter->order_keyword);
1141 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1143 *order_keyword = NULL;
1146 *order_type = _filter->order_type;
1147 *collate_type = _filter->order_collate_type;
1149 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1150 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1156 int media_filter_get_storage(filter_h filter, char **storage_id)
1158 int ret = MEDIA_CONTENT_ERROR_NONE;
1159 media_content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
1160 filter_s *_filter = (filter_s*)filter;
1163 if (STRING_VALID(_filter->storage_id)) {
1164 *storage_id = strdup(_filter->storage_id);
1165 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1170 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1171 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1177 int media_filter_set_condition_v2(filter_h filter, const char *condition)
1179 int ret = MEDIA_CONTENT_ERROR_NONE;
1180 filter_s *_filter = (filter_s*)filter;
1182 if ((_filter != NULL) && STRING_VALID(condition)) {
1183 _filter->is_full_condition = true;
1185 if (STRING_VALID(_filter->condition))
1186 SAFE_FREE(_filter->condition);
1188 char new_condition[MAX_QUERY_SIZE] = {0, };
1189 memset(new_condition, 0, sizeof(new_condition));
1190 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
1191 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1193 _filter->condition = strdup(new_condition);
1194 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1196 media_content_sec_debug("Condition string : %s", _filter->condition);
1198 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1199 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1205 int media_filter_get_condition_v2(filter_h filter, char **condition)
1207 int ret = MEDIA_CONTENT_ERROR_NONE;
1208 filter_s *_filter = (filter_s*)filter;
1211 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == true) {
1212 char new_condition[MAX_QUERY_SIZE] = {0, };
1213 memset(new_condition, 0, sizeof(new_condition));
1214 ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
1215 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1217 *condition = strdup(new_condition);
1218 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1223 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1224 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1230 int media_filter_set_order_v2(filter_h filter, const char *order)
1232 int ret = MEDIA_CONTENT_ERROR_NONE;
1233 filter_s *_filter = (filter_s*)filter;
1235 if ((_filter != NULL) && STRING_VALID(order)) {
1236 _filter->is_full_order = true;
1238 SAFE_FREE(_filter->order_keyword);
1240 _filter->order_keyword = strdup(order);
1241 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
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_order_v2(filter_h filter, char **order)
1252 int ret = MEDIA_CONTENT_ERROR_NONE;
1253 filter_s *_filter = (filter_s*)filter;
1256 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == true) {
1257 *order = strdup(_filter->order_keyword);
1258 media_content_retvm_if(*order == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1263 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1264 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;