Add storage API.
[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
20 static const char *media_token[] =
21 {
22         " ",
23         "\"",
24         "'",
25         "(",
26         ")",
27         "=",
28         "<=",
29         "<",
30         ">=",
31         ">",
32 };
33
34
35 typedef struct _token_t
36 {
37         int type;
38         char *str;
39 }token_t;
40
41
42 #define MAX_LEFT_VALUE 512
43 #define SPACE_LEN 1
44 #define SPACE " "
45 #define UNKNOWN_TYPE 1000
46 #define STRING_TYPE 100
47
48 static char *__get_order_str(media_content_order_e order_enum);
49 static char *__get_collate_str(media_content_collation_e collate_type);
50 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
51 static char *__media_filter_replace_attr(attribute_h attr, char *name);
52 static int __tokenize_operator(token_t *token, const char *str, int op_type);
53 static int __tokenize(GList **token_list, const char *str);
54
55 static bool __is_pinyin_needed(void)
56 {
57 #if 0
58         char *lang = NULL;
59         const char *china = "zh_CN";
60         const char *hongkong = "zh_HK";
61 #endif
62         int ret = FALSE;
63 #if 0
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 #endif
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                 }
703         }
704
705         if(*tmp)                        //remained string
706         {
707                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
708                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
709
710                 ret = __tokenize_string(token, tmp, idx);
711                 if (ret < 0)
712                 {
713                         SAFE_FREE(token);
714                         media_content_error("tokenize error occured");
715                         return -1;
716                 }
717
718                 if(token != NULL && STRING_VALID(token->str))
719                 {
720                         *token_list = g_list_append(*token_list, token);
721                 }
722                 else
723                 {
724                         SAFE_FREE(token);
725                         media_content_error("tokenize error occured");
726                         return -1;
727                 }
728         }
729
730         return MEDIA_CONTENT_ERROR_NONE;
731 }
732
733 int _media_filter_attribute_create(attribute_h *attr)
734 {
735         int ret = MEDIA_CONTENT_ERROR_NONE;
736
737         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
738
739         attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
740         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
741
742         _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
743         *attr = (attribute_h)_attr;
744
745         return ret;
746 }
747
748 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
749 {
750         int ret = MEDIA_CONTENT_ERROR_NONE;
751         char *_user = NULL;
752         char *_platform = NULL;
753         attribute_s *_attr = (attribute_s*)attr;
754
755         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
756
757         if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
758         {
759                 _user = strdup(user_attr);
760                 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
761
762                 _platform = strdup(platform_attr);
763                 if(_platform == NULL)
764                 {
765                         SAFE_FREE(_user);
766                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
767                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
768                 }
769
770                 g_hash_table_insert (_attr->attr_map, _user, _platform);
771         }
772         else
773         {
774                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
775                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
776         }
777
778         return ret;
779 }
780
781 int _media_filter_attribute_destory(attribute_h attr)
782 {
783         int ret = MEDIA_CONTENT_ERROR_NONE;
784         attribute_s *_attr = (attribute_s*)attr;
785
786         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
787
788         if(_attr->attr_map != NULL)
789         {
790                 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
791                 g_hash_table_destroy(_attr->attr_map);
792         }
793
794         SAFE_FREE(_attr);
795
796         return ret;
797 }
798
799 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
800 {
801         unsigned int idx = 0;
802         GList *token_list = NULL;
803         int size = 0;
804         int ret = MEDIA_CONTENT_ERROR_NONE;
805         int total_str_size = 0;
806         token_t *token;
807
808         media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
809         media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
810         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
811
812         if(__tokenize(&token_list, condition) < 0)
813         {
814                 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
815                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
816         }
817
818         for(idx = 0; idx < g_list_length(token_list); idx++)
819         {
820                 token = (token_t*)g_list_nth_data(token_list, idx);
821
822                 if(token->type == UNKNOWN_TYPE)
823                 {
824                         char *replace_str = __media_filter_replace_attr(attr, token->str);
825                         if(STRING_VALID(replace_str))
826                         {
827                                 SAFE_FREE(token->str);
828                                 token->str = replace_str;
829                         }
830                 }
831
832                 total_str_size += strlen(token->str)+1;
833                 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
834         }
835
836         //make the statment
837         size = total_str_size + COLLATE_STR_SIZE + 1;
838         * generated_condition = (char*)calloc(size, sizeof(char));
839
840         for(idx = 0; idx < g_list_length(token_list); idx++)
841         {
842                 token = (token_t*)g_list_nth_data(token_list, idx);
843
844                 if((token != NULL) && STRING_VALID(token->str))
845                 {
846                         SAFE_STRLCAT(*generated_condition, token->str, size);
847                         SAFE_STRLCAT(*generated_condition, SPACE, size);
848
849                         SAFE_FREE(token->str);
850                         SAFE_FREE(token);
851                 }
852         }
853
854         if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
855                 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
856                 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
857                 SAFE_STRLCAT(*generated_condition, SPACE, size);
858         }
859
860         //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
861         media_content_sec_debug("Condition : %s", *generated_condition);
862
863         //if(*generated_condition != NULL)
864         //      res = 1;
865
866         if(token_list != NULL)
867                 g_list_free(token_list);
868
869         return ret;
870 }
871
872 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
873 {
874         int ret = MEDIA_CONTENT_ERROR_NONE;
875         filter_s *_filter = NULL;
876         char option_query[DEFAULT_QUERY_SIZE] = {0, };
877         char condition[DEFAULT_QUERY_SIZE] = {0, };
878         int size = 0;
879         //bool order_by = true;
880
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
884         _filter = (filter_s*)filter;
885
886         memset(option_query, 0x00, sizeof(option_query));
887
888         /* Order by*/
889         if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
890         {
891                 unsigned int idx = 0;
892                 int total_str_size = 0;
893                 GList *token_list = NULL;
894                 token_t *token;
895                 char *attr_str;
896
897                 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
898                 {
899                         media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
900                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
901                 }
902
903                 for(idx = 0; idx < g_list_length(token_list); idx++)
904                 {
905                         token = (token_t*)g_list_nth_data(token_list, idx);
906
907                         if(token->type == UNKNOWN_TYPE)
908                         {
909                                 char *replace_str = __media_filter_replace_attr(attr, token->str);
910                                 if(STRING_VALID(replace_str))
911                                 {
912                                         attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
913                                         if(attr_str == NULL)
914                                         {
915                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
916                                                 SAFE_FREE(replace_str);
917                                                 continue;
918                                         }
919
920                                         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) {
921                                                 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));
922                                         } else {
923                                                 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
924                                         }
925
926                                         SAFE_FREE(token->str);
927                                         token->str = attr_str;
928                                         SAFE_FREE(replace_str);
929                                 }
930                                 else
931                                 {
932                                         media_content_error("There is no matched db field for %s", token->str);
933                                 }
934                         }
935
936                         total_str_size += strlen(token->str) + 1;
937                         //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
938                 }
939
940                 //make the statment
941                 char *generated_condition = NULL;
942                 size = total_str_size + COLLATE_STR_SIZE + 1;
943                 generated_condition = (char*)calloc(size, sizeof(char));
944
945                 for(idx = 0; idx < g_list_length(token_list); idx++)
946                 {
947                         token = (token_t*)g_list_nth_data(token_list, idx);
948
949                         if((token != NULL) && STRING_VALID(token->str))
950                         {
951                                 //media_content_debug("[%d] %s", idx, token->str);
952                                 SAFE_STRLCAT(generated_condition, token->str, size);
953                                 SAFE_STRLCAT(generated_condition, SPACE, size);
954
955                                 SAFE_FREE(token->str);
956                                 SAFE_FREE(token);
957                         }
958                 }
959
960                 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
961                 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
962
963                 if(token_list != NULL)
964                         g_list_free(token_list);
965
966                 SAFE_FREE(generated_condition);
967         }
968
969         /* offset */
970         SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
971         snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
972         SAFE_STRLCAT(option_query, condition, sizeof(option_query));
973
974         if(STRING_VALID(option_query))
975         {
976                 *generated_option = strdup(option_query);
977         }
978         else
979         {
980                 *generated_option = NULL;
981         }
982
983         return ret;
984 }
985
986 int media_filter_create(filter_h *filter)
987 {
988         int ret = MEDIA_CONTENT_ERROR_NONE;
989
990         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
991
992         filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
993         media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
994
995         _filter->storage_id = NULL;
996         _filter->condition = NULL;
997         _filter->order_keyword = NULL;
998         _filter->order_type = -1;
999         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1000         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1001         _filter->offset = -1;
1002         _filter->count = -1;
1003
1004         *filter = (filter_h)_filter;
1005
1006         return ret;
1007 }
1008
1009 int media_filter_destroy(filter_h filter)
1010 {
1011         int ret = MEDIA_CONTENT_ERROR_NONE;
1012         filter_s *_filter = (filter_s*)filter;
1013
1014         if(_filter)
1015         {
1016                 SAFE_FREE(_filter->storage_id);
1017                 SAFE_FREE(_filter->condition);
1018                 SAFE_FREE(_filter->order_keyword);
1019                 SAFE_FREE(_filter);
1020
1021                 ret = MEDIA_CONTENT_ERROR_NONE;
1022         }
1023         else
1024         {
1025                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1026                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1027         }
1028
1029         return ret;
1030 }
1031
1032 int media_filter_set_offset(filter_h filter, int offset, int count)
1033 {
1034         int ret = MEDIA_CONTENT_ERROR_NONE;
1035         filter_s *_filter = (filter_s*)filter;
1036
1037         if(_filter != NULL)
1038         {
1039                 _filter->offset = offset;
1040                 _filter->count = count;
1041         }
1042         else
1043         {
1044                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1045                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1046         }
1047
1048         return ret;
1049 }
1050
1051 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1052 {
1053         int ret = MEDIA_CONTENT_ERROR_NONE;
1054         filter_s *_filter = (filter_s*)filter;
1055
1056         if((_filter != NULL) && STRING_VALID(condition)
1057                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1058         {
1059                 if(STRING_VALID(_filter->condition))
1060                 {
1061                         SAFE_FREE(_filter->condition);
1062                 }
1063
1064                 _filter->condition = strdup(condition);
1065                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1066
1067                 media_content_sec_debug("Condition string : %s", _filter->condition);
1068
1069                 _filter->condition_collate_type = collate_type;
1070         }
1071         else
1072         {
1073                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1074                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1075         }
1076
1077         return ret;
1078 }
1079
1080 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1081 {
1082         int ret = MEDIA_CONTENT_ERROR_NONE;
1083         filter_s *_filter = (filter_s*)filter;
1084
1085         if((_filter != NULL) && STRING_VALID(order_keyword)
1086                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1087                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1088         {
1089                 SAFE_FREE(_filter->order_keyword);
1090
1091                 if(STRING_VALID(order_keyword))
1092                 {
1093                         _filter->order_keyword = strdup(order_keyword);
1094                         media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1095
1096                         _filter->order_type = order_type;
1097                         _filter->order_collate_type = collate_type;
1098                 }
1099                 else
1100                 {
1101                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1102                         ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1103                 }
1104         }
1105         else
1106         {
1107                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1108                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1109         }
1110
1111         return ret;
1112 }
1113
1114 int media_filter_set_storage(filter_h filter, const char *storage_id)
1115 {
1116         int ret = MEDIA_CONTENT_ERROR_NONE;
1117         filter_s *_filter = (filter_s*)filter;
1118
1119         if((_filter != NULL) && STRING_VALID(storage_id))
1120         {
1121                 if(STRING_VALID(_filter->storage_id))
1122                 {
1123                         SAFE_FREE(_filter->storage_id);
1124                 }
1125
1126                 _filter->storage_id = strdup(storage_id);
1127                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1128
1129                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1130         }
1131         else
1132         {
1133                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1134                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1135         }
1136
1137         return ret;
1138 }
1139
1140 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1141 {
1142         int ret = MEDIA_CONTENT_ERROR_NONE;
1143         filter_s *_filter = (filter_s*)filter;
1144
1145         if(_filter)
1146         {
1147                 *offset = _filter->offset;
1148                 *count = _filter->count;
1149         }
1150         else
1151         {
1152                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1153                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1154         }
1155
1156         return ret;
1157 }
1158
1159 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1160 {
1161         int ret = MEDIA_CONTENT_ERROR_NONE;
1162         filter_s *_filter = (filter_s*)filter;
1163
1164         if(_filter)
1165         {
1166                 if(STRING_VALID(_filter->condition))
1167                 {
1168                         *condition = strdup(_filter->condition);
1169                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1170                 }
1171                 else
1172                 {
1173                         *condition = NULL;
1174                 }
1175
1176                 *collate_type = _filter->condition_collate_type;
1177         }
1178         else
1179         {
1180                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1181                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1182         }
1183
1184         return ret;
1185 }
1186
1187 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1188 {
1189         int ret = MEDIA_CONTENT_ERROR_NONE;
1190         filter_s *_filter = (filter_s*)filter;
1191
1192         if(_filter)
1193         {
1194                 if(STRING_VALID(_filter->order_keyword))
1195                 {
1196                         *order_keyword = strdup(_filter->order_keyword);
1197                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1198                 }
1199                 else
1200                 {
1201                         *order_keyword = NULL;
1202                 }
1203
1204                 *order_type = _filter->order_type;
1205                 *collate_type = _filter->order_collate_type;
1206         }
1207         else
1208         {
1209                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1210                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1211         }
1212
1213         return ret;
1214 }
1215
1216 int media_filter_get_storage(filter_h filter, char **storage_id)
1217 {
1218         int ret = MEDIA_CONTENT_ERROR_NONE;
1219         filter_s *_filter = (filter_s*)filter;
1220
1221         if(_filter)
1222         {
1223                 if(STRING_VALID(_filter->storage_id))
1224                 {
1225                         *storage_id = strdup(_filter->storage_id);
1226                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1227                 }
1228                 else
1229                 {
1230                         *storage_id = NULL;
1231                 }
1232         }
1233         else
1234         {
1235                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1236                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1237         }
1238
1239         return ret;
1240 }