Use sqlite3_snprintf instead of g_strlcpy
[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 <media_util_private.h>
20 #include <vconf.h>
21
22 static bool __is_pinyin_needed(void)
23 {
24         char *lang = NULL;
25         const char *china = "zh_CN";
26         const char *hongkong = "zh_HK";
27         int ret = FALSE;
28
29         /*Check CSC first*/
30         bool pinyin_support = FALSE;
31         media_svc_check_pinyin_support(&pinyin_support);
32         if (pinyin_support) {
33                 /*Check Language Setting*/
34                 lang = vconf_get_str(VCONFKEY_LANGSET);
35                 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
36
37                 if ((strncmp(china, lang, strlen(china)) == 0) ||
38                         (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
39                         ret = TRUE;
40                 }
41
42                 SAFE_FREE(lang);
43         }
44
45         return ret;
46 }
47
48 static const char *__get_order_str(media_content_order_e order_enum)
49 {
50         switch (order_enum) {
51         case MEDIA_CONTENT_ORDER_ASC:
52                 return "ASC ";
53         case MEDIA_CONTENT_ORDER_DESC:
54                 return "DESC ";
55         default:
56                 return " ";
57         }
58 }
59
60 static const char *__get_collate_str(media_content_collation_e collate_type)
61 {
62         switch (collate_type) {
63         case MEDIA_CONTENT_COLLATE_NOCASE:
64                 return " COLLATE NOCASE ";
65         case MEDIA_CONTENT_COLLATE_RTRIM:
66                 return " COLLATE RTRIM ";
67         case MEDIA_CONTENT_COLLATE_LOCALIZED:
68                 if (__is_pinyin_needed())
69                         return " COLLATE NOCASE ";
70                 else
71                         return " COLLATE localized ";
72         default:
73                 return " ";
74         }
75 }
76
77 static bool __check_collate_type(media_content_collation_e collate_type)
78 {
79         switch (collate_type) {
80         case MEDIA_CONTENT_COLLATE_DEFAULT:
81         case MEDIA_CONTENT_COLLATE_NOCASE:
82         case MEDIA_CONTENT_COLLATE_RTRIM:
83         case MEDIA_CONTENT_COLLATE_LOCALIZED:
84                 return true;
85         default:
86                 return false;
87         }
88 }
89
90 int _media_filter_build_condition(bool is_full, const char *condition, media_content_collation_e collate_type, char **result)
91 {
92         media_content_retvm_if(result == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid out param");
93         media_content_retvm_if(!STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
94         media_content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition collate");
95
96         if (is_full)
97                 *result = g_strdup_printf("(%s)", condition);
98         else
99                 *result = g_strdup_printf("(%s%s)", condition, __get_collate_str(collate_type));
100
101         media_content_sec_debug("Condition : %s", *result);
102
103         return MEDIA_CONTENT_ERROR_NONE;
104 }
105
106 int _media_filter_build_option(filter_h filter, char **result)
107 {
108         filter_s *_filter = (filter_s *)filter;
109
110         media_content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
111         media_content_retvm_if(!result, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid out param");
112
113         if (!STRING_VALID(_filter->order_keyword)) {
114                 *result = g_strdup_printf(" LIMIT %d, %d", _filter->offset, _filter->count);
115                 return MEDIA_CONTENT_ERROR_NONE;
116         }
117
118         if (_filter->is_full_order) {
119                 *result = g_strdup_printf("ORDER BY %s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
120                 return MEDIA_CONTENT_ERROR_NONE;
121         }
122
123         if (_filter->order_type == MEDIA_CONTENT_ORDER_ASC || _filter->order_type == MEDIA_CONTENT_ORDER_DESC) {
124                 *result = g_strdup_printf("ORDER BY %s%s%s LIMIT %d, %d",
125                         _filter->order_keyword,
126                         __get_collate_str(_filter->order_collate_type),
127                         __get_order_str(_filter->order_type),
128                         _filter->offset,
129                         _filter->count);
130         } else {
131                 *result = g_strdup_printf("%s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
132         }
133
134         return MEDIA_CONTENT_ERROR_NONE;
135 }
136
137 int media_filter_create(filter_h *filter)
138 {
139         int ret = MEDIA_CONTENT_ERROR_NONE;
140
141         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
142
143         filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
144         media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
145
146         _filter->storage_id = NULL;
147         _filter->condition = NULL;
148         _filter->order_keyword = NULL;
149         _filter->order_type = -1;
150         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
151         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
152         _filter->offset = -1;
153         _filter->count = -1;
154         _filter->is_full_condition = false;
155         _filter->is_full_order = false;
156
157         *filter = (filter_h)_filter;
158
159         return ret;
160 }
161
162 int media_filter_destroy(filter_h filter)
163 {
164         int ret = MEDIA_CONTENT_ERROR_NONE;
165         filter_s *_filter = (filter_s*)filter;
166
167         if (_filter) {
168                 SAFE_FREE(_filter->storage_id);
169                 SAFE_FREE(_filter->condition);
170                 SAFE_FREE(_filter->order_keyword);
171                 SAFE_FREE(_filter);
172
173                 ret = MEDIA_CONTENT_ERROR_NONE;
174         } else {
175                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
176                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
177         }
178
179         return ret;
180 }
181
182 int media_filter_set_offset(filter_h filter, int offset, int count)
183 {
184         int ret = MEDIA_CONTENT_ERROR_NONE;
185         filter_s *_filter = (filter_s*)filter;
186
187         if (_filter != NULL) {
188                 _filter->offset = offset;
189                 _filter->count = count;
190         } else {
191                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
192                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
193         }
194
195         return ret;
196 }
197
198 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
199 {
200         int ret = MEDIA_CONTENT_ERROR_NONE;
201         filter_s *_filter = (filter_s*)filter;
202
203         if ((_filter != NULL) && STRING_VALID(condition)
204                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
205
206                 _filter->is_full_condition = false;
207
208                 if (STRING_VALID(_filter->condition))
209                         SAFE_FREE(_filter->condition);
210
211                 char new_condition[MAX_QUERY_SIZE] = {0, };
212                 memset(new_condition, 0, sizeof(new_condition));
213                 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
214                 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
215
216                 _filter->condition = strdup(new_condition);
217                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
218
219                 media_content_sec_debug("Condition string : %s", _filter->condition);
220
221                 _filter->condition_collate_type = collate_type;
222         } else {
223                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
224                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
225         }
226
227         return ret;
228 }
229
230 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
231 {
232         int ret = MEDIA_CONTENT_ERROR_NONE;
233         filter_s *_filter = (filter_s*)filter;
234
235         if ((_filter != NULL) && STRING_VALID(order_keyword)
236                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
237                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
238
239                 _filter->is_full_order = false;
240
241                 SAFE_FREE(_filter->order_keyword);
242
243                 _filter->order_keyword = strdup(order_keyword);
244                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
245
246                 _filter->order_type = order_type;
247                 _filter->order_collate_type = collate_type;
248         } else {
249                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
250                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
251         }
252
253         return ret;
254 }
255
256 int media_filter_set_storage(filter_h filter, const char *storage_id)
257 {
258         int ret = MEDIA_CONTENT_ERROR_NONE;
259         media_content_warn("DEPRECATION WARNING: media_filter_set_storage() is deprecated and will be removed from next release. Use media_filter_set_condition() with MEDIA_PATH keyword instead.");
260         filter_s *_filter = (filter_s*)filter;
261
262         if ((_filter != NULL) && STRING_VALID(storage_id)) {
263                 if (STRING_VALID(_filter->storage_id))
264                         SAFE_FREE(_filter->storage_id);
265
266                 _filter->storage_id = strdup(storage_id);
267                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
268
269                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
270         } else {
271                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
272                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
273         }
274
275         return ret;
276 }
277
278 int media_filter_get_offset(filter_h filter, int *offset, int *count)
279 {
280         int ret = MEDIA_CONTENT_ERROR_NONE;
281         filter_s *_filter = (filter_s*)filter;
282
283         if (_filter) {
284                 *offset = _filter->offset;
285                 *count = _filter->count;
286         } else {
287                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
288                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
289         }
290
291         return ret;
292 }
293
294 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
295 {
296         int ret = MEDIA_CONTENT_ERROR_NONE;
297         filter_s *_filter = (filter_s*)filter;
298
299         if (_filter) {
300                 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == false) {
301                         char new_condition[MAX_QUERY_SIZE] = {0, };
302                         memset(new_condition, 0, sizeof(new_condition));
303                         ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
304                         media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
305
306                         *condition = strdup(new_condition);
307                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
308                 } else {
309                         *condition = NULL;
310                 }
311
312                 *collate_type = _filter->condition_collate_type;
313         } else {
314                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
315                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
316         }
317
318         return ret;
319 }
320
321 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
322 {
323         int ret = MEDIA_CONTENT_ERROR_NONE;
324         filter_s *_filter = (filter_s*)filter;
325
326         if (_filter) {
327                 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == false) {
328                         *order_keyword = strdup(_filter->order_keyword);
329                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
330                 } else {
331                         *order_keyword = NULL;
332                 }
333
334                 *order_type = _filter->order_type;
335                 *collate_type = _filter->order_collate_type;
336         } else {
337                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
338                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
339         }
340
341         return ret;
342 }
343
344 int media_filter_get_storage(filter_h filter, char **storage_id)
345 {
346         int ret = MEDIA_CONTENT_ERROR_NONE;
347         media_content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
348         filter_s *_filter = (filter_s*)filter;
349
350         if (_filter) {
351                 if (STRING_VALID(_filter->storage_id)) {
352                         *storage_id = strdup(_filter->storage_id);
353                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
354                 } else {
355                         *storage_id = NULL;
356                 }
357         } else {
358                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
359                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
360         }
361
362         return ret;
363 }
364
365 int media_filter_set_condition_v2(filter_h filter, const char *condition)
366 {
367         int ret = MEDIA_CONTENT_ERROR_NONE;
368         filter_s *_filter = (filter_s*)filter;
369
370         if ((_filter != NULL) && STRING_VALID(condition)) {
371                 _filter->is_full_condition = true;
372
373                 if (STRING_VALID(_filter->condition))
374                         SAFE_FREE(_filter->condition);
375
376                 char new_condition[MAX_QUERY_SIZE] = {0, };
377                 memset(new_condition, 0, sizeof(new_condition));
378                 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
379                 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
380
381                 _filter->condition = strdup(new_condition);
382                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
383
384                 media_content_sec_debug("Condition string : %s", _filter->condition);
385         } else {
386                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
387                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
388         }
389
390         return ret;
391 }
392
393 int media_filter_get_condition_v2(filter_h filter, char **condition)
394 {
395         int ret = MEDIA_CONTENT_ERROR_NONE;
396         filter_s *_filter = (filter_s*)filter;
397
398         if (_filter) {
399                 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == true) {
400                         char new_condition[MAX_QUERY_SIZE] = {0, };
401                         memset(new_condition, 0, sizeof(new_condition));
402                         ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
403                         media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
404
405                         *condition = strdup(new_condition);
406                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
407                 } else {
408                         *condition = NULL;
409                 }
410         } else {
411                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
412                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
413         }
414
415         return ret;
416 }
417
418 int media_filter_set_order_v2(filter_h filter, const char *order)
419 {
420         int ret = MEDIA_CONTENT_ERROR_NONE;
421         filter_s *_filter = (filter_s*)filter;
422
423         if ((_filter != NULL) && STRING_VALID(order)) {
424                 _filter->is_full_order = true;
425
426                 SAFE_FREE(_filter->order_keyword);
427
428                 _filter->order_keyword = strdup(order);
429                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
430         } else {
431                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
432                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
433         }
434
435         return ret;
436 }
437
438 int media_filter_get_order_v2(filter_h filter, char **order)
439 {
440         int ret = MEDIA_CONTENT_ERROR_NONE;
441         filter_s *_filter = (filter_s*)filter;
442
443         if (_filter) {
444                 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == true) {
445                         *order = strdup(_filter->order_keyword);
446                         media_content_retvm_if(*order == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
447                 } else {
448                         *order = NULL;
449                 }
450         } else {
451                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
452                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
453         }
454
455         return ret;
456 }
457