Add vconf header
[platform/core/api/media-content.git] / src / media_filter.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17
18 #include <media_info_private.h>
19 #include <vconf.h>
20
21 static const char *media_token[] =
22 {
23         " ",
24         "\"",
25         "'",
26         "(",
27         ")",
28         "=",
29         "<=",
30         "<",
31         ">=",
32         ">",
33         "!=",
34 };
35
36
37 typedef struct _token_t
38 {
39         int type;
40         char *str;
41 }token_t;
42
43
44 #define MAX_LEFT_VALUE 512
45 #define SPACE_LEN 1
46 #define SPACE " "
47 #define UNKNOWN_TYPE 1000
48 #define STRING_TYPE 100
49
50 static char *__get_order_str(media_content_order_e order_enum);
51 static char *__get_collate_str(media_content_collation_e collate_type);
52 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
53 static char *__media_filter_replace_attr(attribute_h attr, char *name);
54 static int __tokenize_operator(token_t *token, const char *str, int op_type);
55 static int __tokenize(GList **token_list, const char *str);
56
57 static bool __is_pinyin_needed(void)
58 {
59         char *lang = NULL;
60         const char *china = "zh_CN";
61         const char *hongkong = "zh_HK";
62         int ret = FALSE;
63
64         /*Check CSC first*/
65         bool pinyin_support = FALSE;
66         media_svc_check_pinyin_support(&pinyin_support);
67         if(pinyin_support)
68         {
69                 /*Check Language Setting*/
70                 lang = vconf_get_str(VCONFKEY_LANGSET);
71                 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
72
73                 if((strncmp(china, lang, strlen(china)) == 0) ||
74                         (strncmp(hongkong, lang, strlen(hongkong)) == 0))
75                 {
76                         ret = TRUE;
77                 }
78
79                 SAFE_FREE(lang);
80         }
81
82         return ret;
83 }
84
85 static char *__get_order_str(media_content_order_e order_enum)
86 {
87         switch(order_enum) {
88                 case MEDIA_CONTENT_ORDER_ASC:
89                         return (char *)"ASC";
90                 case MEDIA_CONTENT_ORDER_DESC:
91                         return (char *)"DESC";
92                 default:
93                         return (char *)" ";
94         }
95 }
96
97 static char *__get_collate_str(media_content_collation_e collate_type)
98 {
99         switch(collate_type) {
100                 case MEDIA_CONTENT_COLLATE_NOCASE:
101                         return (char *)"NOCASE";
102                 case MEDIA_CONTENT_COLLATE_RTRIM:
103                         return (char *)"RTRIM";
104                 case MEDIA_CONTENT_COLLATE_LOCALIZED:
105                         if(__is_pinyin_needed())
106                                 return (char *)"NOCASE";
107                         else
108                                 return (char *)"localized";
109                 default: return (char *)" ";
110         }
111 }
112
113 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
114 {
115         SAFE_FREE(key);
116         SAFE_FREE(value);
117 }
118
119 static char *__media_filter_replace_attr(attribute_h attr, char *name)
120 {
121         char *key_temp = NULL;
122         char *generated_value = NULL;
123         attribute_s *_attr = (attribute_s *)attr;
124
125         if(!g_hash_table_lookup_extended(_attr->attr_map,
126                                                                                 name,
127                                                                                 (gpointer)&key_temp, (gpointer)&generated_value))
128         {
129                 //can't find the value
130                 //media_content_error("NOT_FOUND_VALUE(%s)", name);
131                 return NULL;
132         }
133
134         if(STRING_VALID(generated_value))
135         {
136                 return strdup(generated_value);
137         }
138
139         media_content_error("__media_filter_replace_attr fail");
140
141         return NULL;
142 }
143
144 static int __tokenize_operator(token_t *token, const char *str, int op_type)
145 {
146         int ret = 0;
147         const char *tmp = str;
148
149         if(token != NULL && STRING_VALID(tmp))
150         {
151                 token->type = op_type;
152                 int token_size = strlen(media_token[op_type]);
153                 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
154
155                 token->str = (char*)calloc(token_size+1, sizeof(char));
156                 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
157
158                 strncpy(token->str, tmp, token_size);
159                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
160                 ret = token_size;
161         }
162         else
163         {
164                 ret = -1;
165         }
166
167         return ret;
168 }
169
170 static int __tokenize_string(token_t *token, const char *str, int size)
171 {
172         int ret = size;
173         const char *tmp = str;
174
175         if(token != NULL && STRING_VALID(tmp) && size > 0)
176         {
177                 token->str = (char*)calloc(size+1, sizeof(char));
178                 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
179
180                 token->type = UNKNOWN_TYPE;
181                 strncpy(token->str, tmp, size);
182                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
183         }
184         else
185         {
186                 ret = -1;
187         }
188
189         return ret;
190 }
191
192 static int __tokenize_attribute(GList **token_list, const char *str)
193 {
194         int ret = 0;
195         int idx = 0;
196
197         media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
198
199         const char *tmp = str;
200         const char *dst_ptr = str + strlen(str);
201
202         for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
203         {
204                 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
205                 if(tmp[idx] == ' ')             //" "
206                 {
207                         if(idx == 0)            // ignore the space.
208                         {
209                                 tmp++;
210                                 idx = -1;
211                                 continue;
212                         }
213                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
214                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
215
216                         token->type = UNKNOWN_TYPE;
217                         token->str = (char*)calloc(idx+1, sizeof(char));
218                         if(token->str == NULL)
219                         {
220                                 SAFE_FREE(token);
221                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
222                                 return -1;
223                         }
224                         strncpy(token->str, tmp, idx);
225                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
226                         *token_list = g_list_append(*token_list, token);
227                         tmp = tmp +idx + strlen(media_token[0]);
228                         idx = -1;
229                 }
230                 else if(tmp[idx] == ',')        // " , "
231                 {
232                         if(idx != 0)
233                         {
234                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
235                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
236
237                                 ret = __tokenize_string(token, tmp, idx);
238                                 if (ret < 0)
239                                 {
240                                         SAFE_FREE(token);
241                                         media_content_error("tokenize error occured");
242                                         return -1;
243                                 }
244                                 else
245                                 {
246                                         *token_list = g_list_append(*token_list, token);
247                                         tmp = tmp + idx;
248                                 }
249                         }
250
251                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
252                         int size = __tokenize_operator(token, tmp,3);
253
254                         if(token != NULL && STRING_VALID(token->str))
255                         {
256                                 *token_list = g_list_append(*token_list, token);
257                                 tmp += size;
258                                 idx = -1;
259                         }
260                         else
261                         {
262                                 SAFE_FREE(token);
263                                 media_content_error("tokenize error occured");
264                                 return -1;
265                         }
266                 }
267         }
268
269         if(*tmp)                        //remained string
270         {
271                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
272                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
273
274                 ret = __tokenize_string(token, tmp, idx);
275                 if (ret < 0)
276                 {
277                         SAFE_FREE(token);
278                         media_content_error("tokenize error occured");
279                         return -1;
280                 }
281
282                 if(token != NULL && STRING_VALID(token->str))
283                 {
284                         *token_list = g_list_append(*token_list, token);
285                 }
286                 else
287                 {
288                         SAFE_FREE(token);
289                         media_content_error("tokenize error occured");
290                         return -1;
291                 }
292         }
293
294         return MEDIA_CONTENT_ERROR_NONE;
295 }
296
297 static int __tokenize(GList **token_list, const char *str)
298 {
299         int ret = 0;
300         int idx = 0;
301
302         media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
303
304         const char *tmp = str;
305         const char *dst_ptr = str + strlen(str);
306
307         for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
308         {
309                 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
310                 if(tmp[idx] == media_token[0][0])               //" "
311                 {
312                         if(idx == 0)            // ignore the space.
313                         {
314                                 tmp++;
315                                 idx = -1;
316                                 continue;
317                         }
318
319                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
320                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
321
322                         token->type = UNKNOWN_TYPE;
323                         token->str = (char*)calloc(idx+1, sizeof(char));
324                         if(token->str == NULL)
325                         {
326                                 SAFE_FREE(token);
327                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
328                                 return -1;
329                         }
330                         strncpy(token->str, tmp, idx);
331                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
332                         *token_list = g_list_append(*token_list, token);
333                         tmp = tmp +idx + strlen(media_token[0]);
334                         idx = -1;
335                 }
336                 else if(tmp[idx] == media_token[1][0])  // " \" "
337                 {
338                         int j;
339                         bool flag = false;
340                         for(j = idx+1; tmp[j]; j++)     //find next quotation
341                         {
342                                 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
343                                 {
344                                         j += 1;
345                                         continue;
346                                 }
347                                 if(tmp[j] == media_token[1][0])
348                                 {
349                                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
350                                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
351
352                                         token->str = (char*) calloc(j+1+1, sizeof(char));
353                                         if(token->str == NULL)
354                                         {
355                                                 SAFE_FREE(token);
356                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
357                                                 return -1;
358                                         }
359                                         token->type = STRING_TYPE;
360                                         strncpy(token->str, tmp, j+1);
361                                         //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
362                                         *token_list = g_list_append(*token_list, token);
363                                         tmp = tmp + strlen(token->str);
364                                         idx = -1;
365                                         flag = true;
366                                         break;
367                                 }
368                         }
369
370                         if(!flag && *tmp != '\0' && tmp[j]=='\0')
371                         {
372                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
373                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
374
375                                 token->str = (char*) calloc(j+1,sizeof(char));
376                                 if(token->str == NULL)
377                                 {
378                                         SAFE_FREE(token);
379                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
380                                         return -1;
381                                 }
382                                 token->type = UNKNOWN_TYPE;
383                                 strncpy(token->str, tmp,j);
384                                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
385                                 *token_list = g_list_append(*token_list, token);
386                                 tmp = tmp +strlen(token->str);
387                                 idx = -1;
388                         }
389                 }
390                 else if(tmp[idx] == media_token[2][0])  // " \' "
391                 {
392                         int j;
393                         bool flag = false;
394                         for(j = idx+1; tmp[j]; j++)
395                         {
396                                 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
397                                 {
398                                         j += 1;
399                                         continue;
400                                 }
401                                 if(tmp[j] == media_token[2][0])
402                                 {
403                                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
404                                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
405
406                                         token->str = (char*) calloc(j+1+1, sizeof(char));
407                                         if(token->str == NULL)
408                                         {
409                                                 SAFE_FREE(token);
410                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
411                                                 return -1;
412                                         }
413                                         token->type = STRING_TYPE;
414                                         strncpy(token->str, tmp, j+1);
415                                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
416                                         *token_list = g_list_append(*token_list, token);
417                                         tmp = tmp + strlen(token->str);
418                                         idx = -1;
419                                         flag = true;
420                                         break;
421                                 }
422                         }
423
424                         if(!flag && *tmp != '\0' && tmp[j]=='\0')
425                         {
426                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
427                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
428
429                                 token->str = (char*) calloc(j+1,sizeof(char));
430                                 if(token->str == NULL)
431                                 {
432                                         SAFE_FREE(token);
433                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
434                                         return -1;
435                                 }
436                                 token->type = UNKNOWN_TYPE;
437                                 strncpy(token->str, tmp,j);
438                                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
439                                 *token_list = g_list_append(*token_list, token);
440                                 tmp = tmp + strlen(token->str);
441                                 idx = -1;
442                         }
443                 }
444                 else if(tmp[idx] == media_token[3][0])  //"("
445                 {
446                         if(idx != 0)
447                         {
448                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
449                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
450
451                                 ret = __tokenize_string(token, tmp, idx);
452                                 if (ret < 0)
453                                 {
454                                         SAFE_FREE(token);
455                                         media_content_error("tokenize error occured");
456                                         return -1;
457                                 }
458                                 else
459                                 {
460                                         *token_list = g_list_append(*token_list, token);
461                                         tmp = tmp + idx;
462                                 }
463                         }
464                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
465                         int size = __tokenize_operator(token, tmp,3);
466
467                         if(token != NULL && STRING_VALID(token->str))
468                         {
469                                 *token_list = g_list_append(*token_list, token);
470                                 tmp += size;
471                                 idx = -1;
472                         }
473                         else
474                         {
475                                 SAFE_FREE(token);
476                                 media_content_error("tokenize error occured");
477                                 return -1;
478                         }
479
480                 }
481                 else if(tmp[idx] == media_token[4][0])  //")"
482                 {
483                         if(idx != 0)
484                         {
485                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
486                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
487
488                                 ret = __tokenize_string(token, tmp, idx);
489                                 if (ret < 0)
490                                 {
491                                         SAFE_FREE(token);
492                                         media_content_error("tokenize error occured");
493                                         return -1;
494                                 }
495                                 else
496                                 {
497                                         *token_list = g_list_append(*token_list, token);
498                                         tmp = tmp + idx;
499                                 }
500                         }
501                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
502                         int size = __tokenize_operator(token, tmp,4);
503
504                         if(token != NULL && STRING_VALID(token->str))
505                         {
506                                 *token_list = g_list_append(*token_list, token);
507                                 tmp += size;
508                                 idx = -1;
509                         }
510                         else
511                         {
512                                 SAFE_FREE(token);
513                                 media_content_error("tokenize error occured");
514                                 return -1;
515                         }
516
517                 }
518                 else if(tmp[idx] == media_token[5][0])  //"="
519                 {
520                         if(idx != 0)
521                         {
522                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
523                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
524
525                                 ret = __tokenize_string(token, tmp, idx);
526                                 if (ret < 0)
527                                 {
528                                         SAFE_FREE(token);
529                                         media_content_error("tokenize error occured");
530                                         return -1;
531                                 }
532                                 else
533                                 {
534                                         *token_list = g_list_append(*token_list, token);
535                                         tmp = tmp + idx;
536                                 }
537                         }
538                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
539                         int size = __tokenize_operator(token, tmp,5);
540
541                         if(token != NULL && STRING_VALID(token->str))
542                         {
543                                 *token_list = g_list_append(*token_list, token);
544                                 tmp += size;
545                                 idx = -1;
546                         }
547                         else
548                         {
549                                 SAFE_FREE(token);
550                                 media_content_error("tokenize error occured");
551                                 return -1;
552                         }
553
554                 }
555                 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1])       //"<=",
556                 {
557                         if(idx != 0)
558                         {
559                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
560                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
561
562                                 ret = __tokenize_string(token, tmp, idx);
563                                 if (ret < 0)
564                                 {
565                                         SAFE_FREE(token);
566                                         media_content_error("tokenize error occured");
567                                         return -1;
568                                 }
569                                 else
570                                 {
571                                         *token_list = g_list_append(*token_list, token);
572                                         tmp = tmp + idx;
573                                 }
574                         }
575                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
576                         int size = __tokenize_operator(token, tmp,6);
577
578                         if(token != NULL && STRING_VALID(token->str))
579                         {
580                                 *token_list = g_list_append(*token_list, token);
581                                 tmp += size;
582                                 idx = -1;
583                         }
584                         else
585                         {
586                                 SAFE_FREE(token);
587                                 media_content_error("tokenize error occured");
588                                 return -1;
589                         }
590
591                 }
592                 else if(tmp[idx] == media_token[7][0])  //"<",
593                 {
594                         if(idx != 0)
595                         {
596                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
597                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
598
599                                 ret = __tokenize_string(token, tmp, idx);
600                                 if (ret < 0)
601                                 {
602                                         SAFE_FREE(token);
603                                         media_content_error("tokenize error occured");
604                                         return -1;
605                                 }
606                                 else
607                                 {
608                                         *token_list = g_list_append(*token_list, token);
609                                         tmp = tmp + idx;
610                                 }
611                         }
612                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
613                         int size = __tokenize_operator(token, tmp,7);
614
615                         if(token != NULL && STRING_VALID(token->str))
616                         {
617                                 *token_list = g_list_append(*token_list, token);
618                                 tmp += size;
619                                 idx = -1;
620                         }
621                         else
622                         {
623                                 SAFE_FREE(token);
624                                 media_content_error("tokenize error occured");
625                                 return -1;
626                         }
627
628                 }
629                 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1])       //">=",
630                 {
631                         if(idx != 0)
632                         {
633                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
634                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
635
636                                 ret = __tokenize_string(token, tmp, idx);
637                                 if (ret < 0)
638                                 {
639                                         SAFE_FREE(token);
640                                         media_content_error("tokenize error occured");
641                                         return -1;
642                                 }
643                                 else
644                                 {
645                                         *token_list = g_list_append(*token_list, token);
646                                         tmp = tmp + idx;
647                                 }
648                         }
649                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
650                         int size = __tokenize_operator(token, tmp,8);
651
652                         if(token != NULL && STRING_VALID(token->str))
653                         {
654                                 *token_list = g_list_append(*token_list, token);
655                                 tmp += size;
656                                 idx = -1;
657                         }
658                         else
659                         {
660                                 SAFE_FREE(token);
661                                 media_content_error("tokenize error occured");
662                                 return -1;
663                         }
664
665                 }
666                 else if(tmp[idx] == media_token[9][0])  //">",
667                 {
668                         if(idx != 0)
669                         {
670                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
671                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
672
673                                 ret = __tokenize_string(token, tmp, idx);
674                                 if (ret < 0)
675                                 {
676                                         SAFE_FREE(token);
677                                         media_content_error("tokenize error occured");
678                                         return -1;
679                                 }
680                                 else
681                                 {
682                                         *token_list = g_list_append(*token_list, token);
683                                         tmp = tmp + idx;
684                                 }
685                         }
686                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
687                         int size = __tokenize_operator(token, tmp,9);
688
689                         if(token != NULL && STRING_VALID(token->str))
690                         {
691                                 *token_list = g_list_append(*token_list, token);
692                                 tmp += size;
693                                 idx = -1;
694                         }
695                         else
696                         {
697                                 SAFE_FREE(token);
698                                 media_content_error("tokenize error occured");
699                                 return -1;
700                         }
701                 }
702                 else if(tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1])     //"!=",
703                 {
704                         if(idx != 0)
705                         {
706                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
707                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
708
709                                 ret = __tokenize_string(token, tmp, idx);
710                                 if (ret < 0)
711                                 {
712                                         SAFE_FREE(token);
713                                         media_content_error("tokenize error occured");
714                                         return -1;
715                                 }
716                                 else
717                                 {
718                                         *token_list = g_list_append(*token_list, token);
719                                         tmp = tmp + idx;
720                                 }
721                         }
722                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
723                         int size = __tokenize_operator(token, tmp, 10);
724
725                         if(token != NULL && STRING_VALID(token->str))
726                         {
727                                 *token_list = g_list_append(*token_list, token);
728                                 tmp += size;
729                                 idx = -1;
730                         }
731                         else
732                         {
733                                 SAFE_FREE(token);
734                                 media_content_error("tokenize error occured");
735                                 return -1;
736                         }
737                 }
738         }
739
740         if(*tmp)                        //remained string
741         {
742                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
743                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
744
745                 ret = __tokenize_string(token, tmp, idx);
746                 if (ret < 0)
747                 {
748                         SAFE_FREE(token);
749                         media_content_error("tokenize error occured");
750                         return -1;
751                 }
752
753                 if(token != NULL && STRING_VALID(token->str))
754                 {
755                         *token_list = g_list_append(*token_list, token);
756                 }
757                 else
758                 {
759                         SAFE_FREE(token);
760                         media_content_error("tokenize error occured");
761                         return -1;
762                 }
763         }
764
765         return MEDIA_CONTENT_ERROR_NONE;
766 }
767
768 int _media_filter_attribute_create(attribute_h *attr)
769 {
770         int ret = MEDIA_CONTENT_ERROR_NONE;
771
772         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
773
774         attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
775         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
776
777         _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
778         *attr = (attribute_h)_attr;
779
780         return ret;
781 }
782
783 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
784 {
785         int ret = MEDIA_CONTENT_ERROR_NONE;
786         char *_user = NULL;
787         char *_platform = NULL;
788         attribute_s *_attr = (attribute_s*)attr;
789
790         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
791
792         if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
793         {
794                 _user = strdup(user_attr);
795                 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
796
797                 _platform = strdup(platform_attr);
798                 if(_platform == NULL)
799                 {
800                         SAFE_FREE(_user);
801                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
802                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
803                 }
804
805                 g_hash_table_insert (_attr->attr_map, _user, _platform);
806         }
807         else
808         {
809                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
810                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
811         }
812
813         return ret;
814 }
815
816 int _media_filter_attribute_destory(attribute_h attr)
817 {
818         int ret = MEDIA_CONTENT_ERROR_NONE;
819         attribute_s *_attr = (attribute_s*)attr;
820
821         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
822
823         if(_attr->attr_map != NULL)
824         {
825                 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
826                 g_hash_table_destroy(_attr->attr_map);
827         }
828
829         SAFE_FREE(_attr);
830
831         return ret;
832 }
833
834 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
835 {
836         unsigned int idx = 0;
837         GList *token_list = NULL;
838         int size = 0;
839         int ret = MEDIA_CONTENT_ERROR_NONE;
840         int total_str_size = 0;
841         token_t *token;
842
843         media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
844         media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
845         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
846
847         if(__tokenize(&token_list, condition) < 0)
848         {
849                 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
850                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
851         }
852
853         for(idx = 0; idx < g_list_length(token_list); idx++)
854         {
855                 token = (token_t*)g_list_nth_data(token_list, idx);
856
857                 if(token->type == UNKNOWN_TYPE)
858                 {
859                         char *replace_str = __media_filter_replace_attr(attr, token->str);
860                         if(STRING_VALID(replace_str))
861                         {
862                                 SAFE_FREE(token->str);
863                                 token->str = replace_str;
864                         }
865                 }
866
867                 total_str_size += strlen(token->str)+1;
868                 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
869         }
870
871         //make the statment
872         size = total_str_size + COLLATE_STR_SIZE + 1;
873         * generated_condition = (char*)calloc(size, sizeof(char));
874
875         for(idx = 0; idx < g_list_length(token_list); idx++)
876         {
877                 token = (token_t*)g_list_nth_data(token_list, idx);
878
879                 if((token != NULL) && STRING_VALID(token->str))
880                 {
881                         SAFE_STRLCAT(*generated_condition, token->str, size);
882                         SAFE_STRLCAT(*generated_condition, SPACE, size);
883
884                         SAFE_FREE(token->str);
885                         SAFE_FREE(token);
886                 }
887         }
888
889         if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
890                 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
891                 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
892                 SAFE_STRLCAT(*generated_condition, SPACE, size);
893         }
894
895         //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
896         media_content_sec_debug("Condition : %s", *generated_condition);
897
898         //if(*generated_condition != NULL)
899         //      res = 1;
900
901         if(token_list != NULL)
902                 g_list_free(token_list);
903
904         return ret;
905 }
906
907 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
908 {
909         int ret = MEDIA_CONTENT_ERROR_NONE;
910         filter_s *_filter = NULL;
911         char option_query[DEFAULT_QUERY_SIZE] = {0, };
912         char condition[DEFAULT_QUERY_SIZE] = {0, };
913         int size = 0;
914         //bool order_by = true;
915
916         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
917         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
918
919         _filter = (filter_s*)filter;
920
921         memset(option_query, 0x00, sizeof(option_query));
922
923         /* Order by*/
924         if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
925         {
926                 unsigned int idx = 0;
927                 int total_str_size = 0;
928                 GList *token_list = NULL;
929                 token_t *token;
930                 char *attr_str;
931
932                 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
933                 {
934                         media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
935                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
936                 }
937
938                 for(idx = 0; idx < g_list_length(token_list); idx++)
939                 {
940                         token = (token_t*)g_list_nth_data(token_list, idx);
941
942                         if(token->type == UNKNOWN_TYPE)
943                         {
944                                 char *replace_str = __media_filter_replace_attr(attr, token->str);
945                                 if(STRING_VALID(replace_str))
946                                 {
947                                         attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
948                                         if(attr_str == NULL)
949                                         {
950                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
951                                                 SAFE_FREE(replace_str);
952                                                 continue;
953                                         }
954
955                                         if(_filter->order_collate_type == MEDIA_CONTENT_COLLATE_NOCASE || _filter->order_collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||_filter->order_collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
956                                                 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s COLLATE %s %s", replace_str, __get_collate_str(_filter->order_collate_type), __get_order_str(_filter->order_type));
957                                         } else {
958                                                 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
959                                         }
960
961                                         SAFE_FREE(token->str);
962                                         token->str = attr_str;
963                                         SAFE_FREE(replace_str);
964                                 }
965                                 else
966                                 {
967                                         media_content_error("There is no matched db field for %s", token->str);
968                                 }
969                         }
970
971                         total_str_size += strlen(token->str) + 1;
972                         //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
973                 }
974
975                 //make the statment
976                 char *generated_condition = NULL;
977                 size = total_str_size + COLLATE_STR_SIZE + 1;
978                 generated_condition = (char*)calloc(size, sizeof(char));
979
980                 for(idx = 0; idx < g_list_length(token_list); idx++)
981                 {
982                         token = (token_t*)g_list_nth_data(token_list, idx);
983
984                         if((token != NULL) && STRING_VALID(token->str))
985                         {
986                                 //media_content_debug("[%d] %s", idx, token->str);
987                                 SAFE_STRLCAT(generated_condition, token->str, size);
988                                 SAFE_STRLCAT(generated_condition, SPACE, size);
989
990                                 SAFE_FREE(token->str);
991                                 SAFE_FREE(token);
992                         }
993                 }
994
995                 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
996                 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
997
998                 if(token_list != NULL)
999                         g_list_free(token_list);
1000
1001                 SAFE_FREE(generated_condition);
1002         }
1003
1004         /* offset */
1005         SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
1006         snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
1007         SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1008
1009         if(STRING_VALID(option_query))
1010         {
1011                 *generated_option = strdup(option_query);
1012         }
1013         else
1014         {
1015                 *generated_option = NULL;
1016         }
1017
1018         return ret;
1019 }
1020
1021 int media_filter_create(filter_h *filter)
1022 {
1023         int ret = MEDIA_CONTENT_ERROR_NONE;
1024
1025         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
1026
1027         filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
1028         media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1029
1030         _filter->storage_id = NULL;
1031         _filter->condition = NULL;
1032         _filter->order_keyword = NULL;
1033         _filter->order_type = -1;
1034         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1035         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1036         _filter->offset = -1;
1037         _filter->count = -1;
1038
1039         *filter = (filter_h)_filter;
1040
1041         return ret;
1042 }
1043
1044 int media_filter_destroy(filter_h filter)
1045 {
1046         int ret = MEDIA_CONTENT_ERROR_NONE;
1047         filter_s *_filter = (filter_s*)filter;
1048
1049         if(_filter)
1050         {
1051                 SAFE_FREE(_filter->storage_id);
1052                 SAFE_FREE(_filter->condition);
1053                 SAFE_FREE(_filter->order_keyword);
1054                 SAFE_FREE(_filter);
1055
1056                 ret = MEDIA_CONTENT_ERROR_NONE;
1057         }
1058         else
1059         {
1060                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1061                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1062         }
1063
1064         return ret;
1065 }
1066
1067 int media_filter_set_offset(filter_h filter, int offset, int count)
1068 {
1069         int ret = MEDIA_CONTENT_ERROR_NONE;
1070         filter_s *_filter = (filter_s*)filter;
1071
1072         if(_filter != NULL)
1073         {
1074                 _filter->offset = offset;
1075                 _filter->count = count;
1076         }
1077         else
1078         {
1079                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1080                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1081         }
1082
1083         return ret;
1084 }
1085
1086 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1087 {
1088         int ret = MEDIA_CONTENT_ERROR_NONE;
1089         filter_s *_filter = (filter_s*)filter;
1090
1091         if((_filter != NULL) && STRING_VALID(condition)
1092                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1093         {
1094                 if(STRING_VALID(_filter->condition))
1095                 {
1096                         SAFE_FREE(_filter->condition);
1097                 }
1098
1099                 _filter->condition = strdup(condition);
1100                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1101
1102                 media_content_sec_debug("Condition string : %s", _filter->condition);
1103
1104                 _filter->condition_collate_type = collate_type;
1105         }
1106         else
1107         {
1108                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1109                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1110         }
1111
1112         return ret;
1113 }
1114
1115 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1116 {
1117         int ret = MEDIA_CONTENT_ERROR_NONE;
1118         filter_s *_filter = (filter_s*)filter;
1119
1120         if((_filter != NULL) && STRING_VALID(order_keyword)
1121                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1122                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1123         {
1124                 SAFE_FREE(_filter->order_keyword);
1125
1126                 if(STRING_VALID(order_keyword))
1127                 {
1128                         _filter->order_keyword = strdup(order_keyword);
1129                         media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1130
1131                         _filter->order_type = order_type;
1132                         _filter->order_collate_type = collate_type;
1133                 }
1134                 else
1135                 {
1136                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1137                         ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1138                 }
1139         }
1140         else
1141         {
1142                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1143                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1144         }
1145
1146         return ret;
1147 }
1148
1149 int media_filter_set_storage(filter_h filter, const char *storage_id)
1150 {
1151         int ret = MEDIA_CONTENT_ERROR_NONE;
1152         filter_s *_filter = (filter_s*)filter;
1153
1154         if((_filter != NULL) && STRING_VALID(storage_id))
1155         {
1156                 if(STRING_VALID(_filter->storage_id))
1157                 {
1158                         SAFE_FREE(_filter->storage_id);
1159                 }
1160
1161                 _filter->storage_id = strdup(storage_id);
1162                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1163
1164                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1165         }
1166         else
1167         {
1168                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1169                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1170         }
1171
1172         return ret;
1173 }
1174
1175 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1176 {
1177         int ret = MEDIA_CONTENT_ERROR_NONE;
1178         filter_s *_filter = (filter_s*)filter;
1179
1180         if(_filter)
1181         {
1182                 *offset = _filter->offset;
1183                 *count = _filter->count;
1184         }
1185         else
1186         {
1187                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1188                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1189         }
1190
1191         return ret;
1192 }
1193
1194 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1195 {
1196         int ret = MEDIA_CONTENT_ERROR_NONE;
1197         filter_s *_filter = (filter_s*)filter;
1198
1199         if(_filter)
1200         {
1201                 if(STRING_VALID(_filter->condition))
1202                 {
1203                         *condition = strdup(_filter->condition);
1204                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1205                 }
1206                 else
1207                 {
1208                         *condition = NULL;
1209                 }
1210
1211                 *collate_type = _filter->condition_collate_type;
1212         }
1213         else
1214         {
1215                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1216                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1217         }
1218
1219         return ret;
1220 }
1221
1222 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1223 {
1224         int ret = MEDIA_CONTENT_ERROR_NONE;
1225         filter_s *_filter = (filter_s*)filter;
1226
1227         if(_filter)
1228         {
1229                 if(STRING_VALID(_filter->order_keyword))
1230                 {
1231                         *order_keyword = strdup(_filter->order_keyword);
1232                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1233                 }
1234                 else
1235                 {
1236                         *order_keyword = NULL;
1237                 }
1238
1239                 *order_type = _filter->order_type;
1240                 *collate_type = _filter->order_collate_type;
1241         }
1242         else
1243         {
1244                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1245                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1246         }
1247
1248         return ret;
1249 }
1250
1251 int media_filter_get_storage(filter_h filter, char **storage_id)
1252 {
1253         int ret = MEDIA_CONTENT_ERROR_NONE;
1254         filter_s *_filter = (filter_s*)filter;
1255
1256         if(_filter)
1257         {
1258                 if(STRING_VALID(_filter->storage_id))
1259                 {
1260                         *storage_id = strdup(_filter->storage_id);
1261                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1262                 }
1263                 else
1264                 {
1265                         *storage_id = NULL;
1266                 }
1267         }
1268         else
1269         {
1270                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1271                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1272         }
1273
1274         return ret;
1275 }