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