Add other type for media filter
[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)) {
783                 if ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) || (_filter->order_type == MEDIA_CONTENT_ORDER_DESC)) {
784                         unsigned int idx = 0;
785                         int total_str_size = 0;
786                         GList *token_list = NULL;
787                         token_t *token;
788                         char *attr_str;
789
790                         if (__tokenize_attribute(&token_list, _filter->order_keyword) < 0) {
791                                 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
792                                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
793                         }
794
795                         for (idx = 0; idx < g_list_length(token_list); idx++) {
796                                 token = (token_t*)g_list_nth_data(token_list, idx);
797
798                                 if (token->type == UNKNOWN_TYPE) {
799                                         char *replace_str = __media_filter_replace_attr(attr, token->str);
800                                         if (STRING_VALID(replace_str)) {
801                                                 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
802                                                 if (attr_str == NULL) {
803                                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
804                                                         SAFE_FREE(replace_str);
805                                                         continue;
806                                                 }
807
808                                                 if (_filter->order_collate_type == MEDIA_CONTENT_COLLATE_NOCASE || _filter->order_collate_type == MEDIA_CONTENT_COLLATE_RTRIM || _filter->order_collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED)
809                                                         snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s COLLATE %s %s", replace_str, __get_collate_str(_filter->order_collate_type), __get_order_str(_filter->order_type));
810                                                 else
811                                                         snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
812
813                                                 SAFE_FREE(token->str);
814                                                 token->str = attr_str;
815                                                 SAFE_FREE(replace_str);
816                                         } else {
817                                                 media_content_error("There is no matched db field for %s", token->str);
818                                         }
819                                 }
820
821                                 total_str_size += strlen(token->str) + 1;
822                                 /* media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str); */
823                         }
824
825                         /* make the statment */
826                         char *generated_condition = NULL;
827                         size = total_str_size + COLLATE_STR_SIZE + 1;
828                         generated_condition = (char*)calloc(size, sizeof(char));
829
830                         for (idx = 0; idx < g_list_length(token_list); idx++) {
831                                 token = (token_t*)g_list_nth_data(token_list, idx);
832
833                                 if ((token != NULL) && STRING_VALID(token->str)) {
834                                         /* media_content_debug("[%d] %s", idx, token->str); */
835                                         SAFE_STRLCAT(generated_condition, token->str, size);
836                                         SAFE_STRLCAT(generated_condition, SPACE, size);
837
838                                         SAFE_FREE(token->str);
839                                         SAFE_FREE(token);
840                                 }
841                         }
842
843                         snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
844                         SAFE_STRLCAT(option_query, condition, sizeof(option_query));
845
846                         if (token_list != NULL)
847                                 g_list_free(token_list);
848
849                         SAFE_FREE(generated_condition);
850                 } else {
851                         SAFE_STRLCAT(option_query, _filter->order_keyword, sizeof(option_query));
852                         media_content_error("option_query [%s]", option_query);
853                 }
854         }
855
856         /* offset */
857         SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
858         snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
859         SAFE_STRLCAT(option_query, condition, sizeof(option_query));
860
861         if (STRING_VALID(option_query))
862                 *generated_option = strdup(option_query);
863         else
864                 *generated_option = NULL;
865
866         return ret;
867 }
868
869 int media_filter_create(filter_h *filter)
870 {
871         int ret = MEDIA_CONTENT_ERROR_NONE;
872
873         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
874
875         filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
876         media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
877
878         _filter->storage_id = NULL;
879         _filter->condition = NULL;
880         _filter->order_keyword = NULL;
881         _filter->order_type = -1;
882         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
883         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
884         _filter->offset = -1;
885         _filter->count = -1;
886
887         *filter = (filter_h)_filter;
888
889         return ret;
890 }
891
892 int media_filter_destroy(filter_h filter)
893 {
894         int ret = MEDIA_CONTENT_ERROR_NONE;
895         filter_s *_filter = (filter_s*)filter;
896
897         if (_filter) {
898                 SAFE_FREE(_filter->storage_id);
899                 SAFE_FREE(_filter->condition);
900                 SAFE_FREE(_filter->order_keyword);
901                 SAFE_FREE(_filter);
902
903                 ret = MEDIA_CONTENT_ERROR_NONE;
904         } else {
905                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
906                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
907         }
908
909         return ret;
910 }
911
912 int media_filter_set_offset(filter_h filter, int offset, int count)
913 {
914         int ret = MEDIA_CONTENT_ERROR_NONE;
915         filter_s *_filter = (filter_s*)filter;
916
917         if (_filter != NULL) {
918                 _filter->offset = offset;
919                 _filter->count = count;
920         } else {
921                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
922                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
923         }
924
925         return ret;
926 }
927
928 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
929 {
930         int ret = MEDIA_CONTENT_ERROR_NONE;
931         filter_s *_filter = (filter_s*)filter;
932
933         if ((_filter != NULL) && STRING_VALID(condition)
934                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
935                 if (STRING_VALID(_filter->condition))
936                         SAFE_FREE(_filter->condition);
937
938                 _filter->condition = strdup(condition);
939                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
940
941                 media_content_sec_debug("Condition string : %s", _filter->condition);
942
943                 _filter->condition_collate_type = collate_type;
944         } else {
945                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
946                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
947         }
948
949         return ret;
950 }
951
952 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
953 {
954         int ret = MEDIA_CONTENT_ERROR_NONE;
955         filter_s *_filter = (filter_s*)filter;
956
957         if ((_filter != NULL) && STRING_VALID(order_keyword)
958                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
959                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
960                 SAFE_FREE(_filter->order_keyword);
961
962                 if (STRING_VALID(order_keyword)) {
963                         _filter->order_keyword = strdup(order_keyword);
964                         media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
965
966                         _filter->order_type = order_type;
967                         _filter->order_collate_type = collate_type;
968                 } else {
969                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
970                         ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
971                 }
972         } else {
973                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
974                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
975         }
976
977         return ret;
978 }
979
980 int media_filter_set_storage(filter_h filter, const char *storage_id)
981 {
982         int ret = MEDIA_CONTENT_ERROR_NONE;
983         filter_s *_filter = (filter_s*)filter;
984
985         if ((_filter != NULL) && STRING_VALID(storage_id)) {
986                 if (STRING_VALID(_filter->storage_id))
987                         SAFE_FREE(_filter->storage_id);
988
989                 _filter->storage_id = strdup(storage_id);
990                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
991
992                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
993         } else {
994                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
995                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
996         }
997
998         return ret;
999 }
1000
1001 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1002 {
1003         int ret = MEDIA_CONTENT_ERROR_NONE;
1004         filter_s *_filter = (filter_s*)filter;
1005
1006         if (_filter) {
1007                 *offset = _filter->offset;
1008                 *count = _filter->count;
1009         } else {
1010                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1011                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1012         }
1013
1014         return ret;
1015 }
1016
1017 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1018 {
1019         int ret = MEDIA_CONTENT_ERROR_NONE;
1020         filter_s *_filter = (filter_s*)filter;
1021
1022         if (_filter) {
1023                 if (STRING_VALID(_filter->condition)) {
1024                         *condition = strdup(_filter->condition);
1025                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1026                 } else {
1027                         *condition = NULL;
1028                 }
1029
1030                 *collate_type = _filter->condition_collate_type;
1031         } else {
1032                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1033                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1034         }
1035
1036         return ret;
1037 }
1038
1039 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1040 {
1041         int ret = MEDIA_CONTENT_ERROR_NONE;
1042         filter_s *_filter = (filter_s*)filter;
1043
1044         if (_filter) {
1045                 if (STRING_VALID(_filter->order_keyword)) {
1046                         *order_keyword = strdup(_filter->order_keyword);
1047                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1048                 } else {
1049                         *order_keyword = NULL;
1050                 }
1051
1052                 *order_type = _filter->order_type;
1053                 *collate_type = _filter->order_collate_type;
1054         } else {
1055                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1056                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1057         }
1058
1059         return ret;
1060 }
1061
1062 int media_filter_get_storage(filter_h filter, char **storage_id)
1063 {
1064         int ret = MEDIA_CONTENT_ERROR_NONE;
1065         filter_s *_filter = (filter_s*)filter;
1066
1067         if (_filter) {
1068                 if (STRING_VALID(_filter->storage_id)) {
1069                         *storage_id = strdup(_filter->storage_id);
1070                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1071                 } else {
1072                         *storage_id = NULL;
1073                 }
1074         } else {
1075                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1076                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1077         }
1078
1079         return ret;
1080 }