2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <media_info_private.h>
19 #include <media_util_private.h>
22 static bool __is_pinyin_needed(void)
25 const char *china = "zh_CN";
26 const char *hongkong = "zh_HK";
30 bool pinyin_support = FALSE;
31 media_svc_check_pinyin_support(&pinyin_support);
33 /*Check Language Setting*/
34 lang = vconf_get_str(VCONFKEY_LANGSET);
35 content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
37 if ((strncmp(china, lang, strlen(china)) == 0) ||
38 (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
48 static const char *__get_order_str(media_content_order_e order_enum)
51 case MEDIA_CONTENT_ORDER_ASC:
53 case MEDIA_CONTENT_ORDER_DESC:
60 static const char *__get_collate_str(media_content_collation_e collate_type)
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 ";
71 return " COLLATE localized ";
77 static bool __check_collate_type(media_content_collation_e collate_type)
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:
90 static bool __check_order_type(media_content_order_e order)
93 case MEDIA_CONTENT_ORDER_ASC:
94 case MEDIA_CONTENT_ORDER_DESC:
95 case MEDIA_CONTENT_ORDER_OTHER:
102 int _media_filter_build_condition(bool is_full, const char *condition, media_content_collation_e collate_type, char **result)
104 content_retvm_if(!result, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid result");
105 content_retvm_if(!STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid condition");
106 content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid collate_type");
109 *result = g_strdup_printf("(%s)", condition);
111 *result = g_strdup_printf("(%s%s)", condition, __get_collate_str(collate_type));
113 content_sec_debug("Condition : %s", *result);
115 return MEDIA_CONTENT_ERROR_NONE;
118 int _media_filter_build_option(filter_h filter, char **result)
120 filter_s *_filter = (filter_s *)filter;
122 content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
123 content_retvm_if(!result, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid result");
125 if (!STRING_VALID(_filter->order_keyword)) {
126 *result = g_strdup_printf(" LIMIT %d, %d", _filter->offset, _filter->count);
127 return MEDIA_CONTENT_ERROR_NONE;
130 if (_filter->is_full_order) {
131 *result = g_strdup_printf("ORDER BY %s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
132 return MEDIA_CONTENT_ERROR_NONE;
135 if (_filter->order_type == MEDIA_CONTENT_ORDER_ASC || _filter->order_type == MEDIA_CONTENT_ORDER_DESC) {
136 *result = g_strdup_printf("ORDER BY %s%s%s LIMIT %d, %d",
137 _filter->order_keyword,
138 __get_collate_str(_filter->order_collate_type),
139 __get_order_str(_filter->order_type),
143 *result = g_strdup_printf("%s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
146 return MEDIA_CONTENT_ERROR_NONE;
149 int media_filter_create(filter_h *filter)
151 int ret = MEDIA_CONTENT_ERROR_NONE;
153 content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
155 filter_s *_filter = (filter_s *)calloc(1, sizeof(filter_s));
156 content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
158 _filter->storage_id = NULL;
159 _filter->condition = NULL;
160 _filter->order_keyword = NULL;
161 _filter->order_type = -1;
162 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
163 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
164 _filter->offset = -1;
166 _filter->is_full_condition = false;
167 _filter->is_full_order = false;
169 *filter = (filter_h)_filter;
174 int media_filter_destroy(filter_h filter)
176 filter_s *_filter = (filter_s *)filter;
177 content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid filter");
179 SAFE_FREE(_filter->storage_id);
180 SAFE_FREE(_filter->condition);
181 SAFE_FREE(_filter->order_keyword);
184 return MEDIA_CONTENT_ERROR_NONE;
187 int media_filter_set_offset(filter_h filter, int offset, int count)
189 filter_s *_filter = (filter_s *)filter;
190 content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid filter");
192 _filter->offset = offset;
193 _filter->count = count;
195 return MEDIA_CONTENT_ERROR_NONE;
198 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
200 int ret = MEDIA_CONTENT_ERROR_NONE;
201 filter_s *_filter = (filter_s *)filter;
202 content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid filter");
203 content_retvm_if(!STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid condition");
204 content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid collate_type");
206 _filter->is_full_condition = false;
207 SAFE_FREE(_filter->condition);
209 _filter->condition = _media_content_replace_path_in_condition(condition);
212 If an error is occured in _media_content_replace_path_in_condition(),
213 A suitable return value is 'MEDIA_CONTENT_ERROR_INVALID_OPERATION'.
214 However, it is not stated in the description of media_filter_set_condition().
215 Therefore, use 'MEDIA_CONTENT_ERROR_OUT_OF_MEMORY' temporarily.
216 It will be modified after removing _media_content_replace_path_in_condition() function.
218 content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to set condition");
220 content_sec_debug("Condition string : %s", _filter->condition);
222 _filter->condition_collate_type = collate_type;
227 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
229 filter_s *_filter = (filter_s *)filter;
230 content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid filter");
231 content_retvm_if(!STRING_VALID(order_keyword), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid order_keyword");
232 content_retvm_if(!__check_order_type(order_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid order_type");
233 content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid collate_type");
235 _filter->is_full_order = false;
236 SAFE_FREE(_filter->order_keyword);
238 _filter->order_keyword = g_strdup(order_keyword);
239 _filter->order_type = order_type;
240 _filter->order_collate_type = collate_type;
242 return MEDIA_CONTENT_ERROR_NONE;
245 int media_filter_set_storage(filter_h filter, const char *storage_id)
247 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.");
248 filter_s *_filter = (filter_s *)filter;
249 content_retvm_if(!_filter || !STRING_VALID(storage_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
251 SAFE_FREE(_filter->storage_id);
253 _filter->storage_id = g_strdup(storage_id);
254 content_sec_debug("storage_id : %s", _filter->storage_id);
256 return MEDIA_CONTENT_ERROR_NONE;
259 int media_filter_get_offset(filter_h filter, int *offset, int *count)
261 filter_s *_filter = (filter_s *)filter;
262 content_retvm_if(!_filter || !offset || !count, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
264 *offset = _filter->offset;
265 *count = _filter->count;
267 return MEDIA_CONTENT_ERROR_NONE;
270 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
272 filter_s *_filter = (filter_s *)filter;
273 content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid _filter");
274 content_retvm_if(!condition, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid condition");
275 content_retvm_if(!collate_type, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid collate_type");
277 if (!_filter->is_full_condition)
278 *condition = g_strdup(_filter->condition);
280 *collate_type = _filter->condition_collate_type;
282 return MEDIA_CONTENT_ERROR_NONE;
285 int media_filter_get_order(filter_h filter, media_content_order_e *order_type, char **order_keyword, media_content_collation_e *collate_type)
287 filter_s *_filter = (filter_s *)filter;
288 content_retvm_if(!_filter || !order_type, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
289 content_retvm_if(!order_keyword || !collate_type, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
291 if (!_filter->is_full_order)
292 *order_keyword = g_strdup(_filter->order_keyword);
294 *order_type = _filter->order_type;
295 *collate_type = _filter->order_collate_type;
297 return MEDIA_CONTENT_ERROR_NONE;
300 int media_filter_get_storage(filter_h filter, char **storage_id)
302 content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
303 filter_s *_filter = (filter_s *)filter;
304 content_retvm_if(!_filter || !storage_id, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
306 *storage_id = g_strdup(_filter->storage_id);
308 return MEDIA_CONTENT_ERROR_NONE;
311 int media_filter_set_condition_v2(filter_h filter, const char *condition)
313 filter_s *_filter = (filter_s *)filter;
314 content_retvm_if(!_filter || !STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
316 _filter->is_full_condition = true;
317 SAFE_FREE(_filter->condition);
320 If an error is occured in _media_content_replace_path_in_condition(),
321 A suitable return value is 'MEDIA_CONTENT_ERROR_INVALID_OPERATION'.
322 However, it is not stated in the description of media_filter_set_condition().
323 Therefore, use 'MEDIA_CONTENT_ERROR_OUT_OF_MEMORY' temporarily.
324 It will be modified after removing _media_content_replace_path_in_condition() function.
326 _filter->condition = _media_content_replace_path_in_condition(condition);
327 content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to set condition");
329 content_sec_debug("Condition string : %s", _filter->condition);
331 return MEDIA_CONTENT_ERROR_NONE;
334 int media_filter_get_condition_v2(filter_h filter, char **condition)
336 filter_s *_filter = (filter_s *)filter;
337 content_retvm_if(!_filter || !condition, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
339 if (_filter->is_full_condition)
340 *condition = g_strdup(_filter->condition);
342 return MEDIA_CONTENT_ERROR_NONE;
345 int media_filter_set_order_v2(filter_h filter, const char *order)
347 filter_s *_filter = (filter_s *)filter;
348 content_retvm_if(!_filter || !STRING_VALID(order), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
350 _filter->is_full_order = true;
351 SAFE_FREE(_filter->order_keyword);
353 _filter->order_keyword = g_strdup(order);
355 return MEDIA_CONTENT_ERROR_NONE;
358 int media_filter_get_order_v2(filter_h filter, char **order)
360 filter_s *_filter = (filter_s *)filter;
361 content_retvm_if(!_filter || !order, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
363 if (_filter->is_full_order)
364 *order = g_strdup(_filter->order_keyword);
366 return MEDIA_CONTENT_ERROR_NONE;