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>
21 #include <media_content_type_product.h>
25 static char *g_src_path = NULL;
28 int media_folder_get_folder_count_from_db(filter_h filter, int *folder_count)
30 int ret = MEDIA_CONTENT_ERROR_NONE;
33 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
34 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
37 g_mutex_lock(_content_get_db_mutex());
40 ret = _media_db_get_group_count(filter, MEDIA_GROUP_FOLDER, folder_count);
43 g_mutex_unlock(_content_get_db_mutex());
50 int media_folder_foreach_folder_from_db(filter_h filter, media_folder_cb callback, void *user_data)
52 int ret = MEDIA_CONTENT_ERROR_NONE;
54 if (callback == NULL) {
55 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
56 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
60 g_mutex_lock(_content_get_db_mutex());
63 ret = _media_db_get_folder(filter, callback, user_data);
66 g_mutex_unlock(_content_get_db_mutex());
72 int media_folder_get_media_count_from_db(const char *folder_id, filter_h filter, int *media_count)
74 int ret = MEDIA_CONTENT_ERROR_NONE;
76 if (STRING_VALID(folder_id) && media_count) {
78 g_mutex_lock(_content_get_db_mutex());
81 ret = _media_db_get_group_item_count(folder_id, filter, MEDIA_GROUP_FOLDER, media_count);
84 g_mutex_unlock(_content_get_db_mutex());
88 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
89 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
95 int media_folder_foreach_media_from_db(const char *folder_id, filter_h filter, media_info_cb callback, void *user_data)
97 int ret = MEDIA_CONTENT_ERROR_NONE;
99 if ((callback != NULL) && STRING_VALID(folder_id)) {
100 #ifdef _USE_TVPD_MODE
101 g_mutex_lock(_content_get_db_mutex());
104 ret = _media_db_get_group_item(folder_id, filter, callback, user_data, MEDIA_GROUP_FOLDER);
106 #ifdef _USE_TVPD_MODE
107 g_mutex_unlock(_content_get_db_mutex());
111 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
112 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
118 #ifdef _USE_SENIOR_MODE
119 int media_folder_foreach_media_from_db_by_union_select(const char* folder_id, filter_h filter1, filter_h filter2, media_info_cb callback, void* user_data)
121 int ret = MEDIA_CONTENT_ERROR_NONE;
123 if ((callback != NULL) && STRING_VALID(folder_id)) {
124 ret = _media_db_get_group_item_by_union_select(folder_id, filter1, filter2, callback, user_data);
126 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
127 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
134 int media_folder_destroy(media_folder_h folder)
136 int ret = MEDIA_CONTENT_ERROR_NONE;
137 media_folder_s *_folder = (media_folder_s*)folder;
139 SAFE_FREE(_folder->path);
140 SAFE_FREE(_folder->name);
141 SAFE_FREE(_folder->folder_id);
142 SAFE_FREE(_folder->storage_uuid);
144 ret = MEDIA_CONTENT_ERROR_NONE;
146 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
147 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
152 int media_folder_clone(media_folder_h *dst, media_folder_h src)
154 int ret = MEDIA_CONTENT_ERROR_NONE;
155 media_folder_s *_src = (media_folder_s*)src;
158 media_folder_s *_dst = (media_folder_s*)calloc(1, sizeof(media_folder_s));
159 content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
161 if (STRING_VALID(_src->folder_id)) {
162 _dst->folder_id = strdup(_src->folder_id);
163 if (_dst->folder_id == NULL) {
165 content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
166 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
170 if (STRING_VALID(_src->name)) {
171 _dst->name = strdup(_src->name);
172 if (_dst->name == NULL) {
173 content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
174 media_folder_destroy((media_folder_h)_dst);
175 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
179 if (STRING_VALID(_src->path)) {
180 _dst->path = strdup(_src->path);
181 if (_dst->path == NULL) {
182 content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
183 media_folder_destroy((media_folder_h)_dst);
184 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
188 if (STRING_VALID(_src->storage_uuid)) {
189 _dst->storage_uuid = strdup(_src->storage_uuid);
190 if (_dst->storage_uuid == NULL) {
191 content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
192 media_folder_destroy((media_folder_h)_dst);
193 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
197 _dst->storage_type = _src->storage_type;
199 *dst = (media_folder_h)_dst;
201 ret = MEDIA_CONTENT_ERROR_NONE;
203 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
204 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
210 int media_folder_get_folder_id(media_folder_h folder, char **folder_id)
212 int ret = MEDIA_CONTENT_ERROR_NONE;
213 media_folder_s *_folder = (media_folder_s*)folder;
216 if (STRING_VALID(_folder->folder_id)) {
217 *folder_id = strdup(_folder->folder_id);
218 content_retvm_if(*folder_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
222 ret = MEDIA_CONTENT_ERROR_NONE;
224 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
225 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
231 int media_folder_get_path(media_folder_h folder, char **path)
233 int ret = MEDIA_CONTENT_ERROR_NONE;
234 media_folder_s *_folder = (media_folder_s*)folder;
237 if (STRING_VALID(_folder->path)) {
238 *path = strdup(_folder->path);
239 content_retvm_if(*path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
243 ret = MEDIA_CONTENT_ERROR_NONE;
246 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
247 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
254 int media_folder_get_name(media_folder_h folder, char **name)
256 int ret = MEDIA_CONTENT_ERROR_NONE;
257 media_folder_s *_folder = (media_folder_s*)folder;
260 if (STRING_VALID(_folder->name)) {
261 *name = strdup(_folder->name);
262 content_retvm_if(*name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
266 ret = MEDIA_CONTENT_ERROR_NONE;
269 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
270 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
276 int media_folder_get_storage_type(media_folder_h folder, media_content_storage_e* storage_type)
278 int ret = MEDIA_CONTENT_ERROR_NONE;
279 content_warn("DEPRECATION WARNING: media_folder_get_storage_type() is deprecated and will be removed from next release. Use storage_get_type_dev() instead.");
280 media_folder_s *_folder = (media_folder_s*)folder;
283 *storage_type = _folder->storage_type;
284 ret = MEDIA_CONTENT_ERROR_NONE;
286 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
287 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
293 int media_folder_get_storage_id(media_folder_h folder, char **storage_id)
295 int ret = MEDIA_CONTENT_ERROR_NONE;
296 content_warn("DEPRECATION WARNING: media_folder_get_storage_id() is deprecated and will be removed from next release.");
297 media_folder_s *_folder = (media_folder_s*)folder;
300 if (STRING_VALID(_folder->storage_uuid)) {
301 *storage_id = strdup(_folder->storage_uuid);
302 content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
306 ret = MEDIA_CONTENT_ERROR_NONE;
309 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
310 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
316 int media_folder_get_folder_from_db(const char *folder_id, media_folder_h *folder)
318 int ret = MEDIA_CONTENT_ERROR_NONE;
319 sqlite3_stmt *stmt = NULL;
320 char select_query[DEFAULT_QUERY_SIZE] = {0, };
321 media_folder_s *_folder = NULL;
323 if (!STRING_VALID(folder_id)) {
324 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
325 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
328 snprintf(select_query, sizeof(select_query), SELECT_FOLDER_FROM_FOLDER, folder_id);
330 ret = _content_get_result(select_query, &stmt);
331 content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
333 if (sqlite3_step(stmt) == SQLITE_ROW) {
334 _folder = (media_folder_s*)calloc(1, sizeof(media_folder_s));
335 if (_folder == NULL) {
336 content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
337 SQLITE3_FINALIZE(stmt);
338 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
341 _folder->folder_id = g_strdup(folder_id);
342 _folder->path = g_strdup((const char *)sqlite3_column_text(stmt, 0));
343 _folder->name = g_strdup((const char *)sqlite3_column_text(stmt, 1));
344 _folder->storage_type = (int)sqlite3_column_int(stmt, 2);
345 _folder->storage_uuid = g_strdup((const char *)sqlite3_column_text(stmt, 3));
347 *folder = (media_folder_h)_folder;
350 SQLITE3_FINALIZE(stmt);
354 #ifdef _USE_TVPD_MODE
355 int media_folder_update_to_db(media_folder_h folder)
357 int ret = MEDIA_CONTENT_ERROR_NONE;
358 content_warn("DEPRECATION WARNING: media_folder_update_to_db() is deprecated and will be removed from next release.");
359 media_folder_s *_folder = (media_folder_s*)folder;
360 char repl_path[MAX_PATH_LEN] = {0, };
362 if ((_folder == NULL) || (_folder->folder_id == NULL)) {
363 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
364 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
367 if (STRING_VALID(_folder->folder_id) && g_src_path) {
368 ret = _media_content_replace_path(_folder->path, repl_path);
369 if (ret != MEDIA_CONTENT_ERROR_NONE) {
370 SAFE_FREE(g_src_path);
373 ret = media_svc_rename_folder(_content_get_db_handle(), _folder->storage_uuid, g_src_path, repl_path, _content_get_uid());
374 SAFE_FREE(g_src_path);
376 return _content_error_capi(ret);
382 int media_folder_set_name(media_folder_h folder, const char *name)
384 int ret = MEDIA_CONTENT_ERROR_NONE;
385 content_warn("DEPRECATION WARNING: media_folder_set_name() is deprecated and will be removed from next release.");
386 media_folder_s *_folder = (media_folder_s*)folder;
388 if (_folder != NULL && STRING_VALID(name) && name[0] != '.') {
389 if (STRING_VALID(_folder->path) && STRING_VALID(_folder->name)) {
390 char *folder_path = NULL;
391 char new_folder_path[MAX_PATH_LEN] = {0,};
392 char repl_path[MAX_PATH_LEN] = {0, };
394 ret = _media_content_replace_path(_folder->path, repl_path);
395 content_retvm_if(!STRING_VALID(repl_path), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
397 folder_path = g_path_get_dirname(repl_path);
398 content_sec_debug("Existed Folder Path : %s", repl_path);
399 snprintf(new_folder_path, sizeof(new_folder_path), "%s/%s", folder_path, name);
400 content_sec_debug("New Path : %s", new_folder_path);
402 SAFE_FREE(g_src_path);
403 g_src_path = strdup(repl_path);
405 SAFE_FREE(_folder->path);
406 SAFE_FREE(_folder->name);
407 SAFE_FREE(folder_path);
409 _folder->path = strdup(new_folder_path);
410 content_retvm_if(_folder->path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
411 _folder->name = strdup(name);
412 content_retvm_if(_folder->name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
414 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
415 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
418 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
419 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
425 int media_folder_insert_to_db(const char *path)
427 int ret = MEDIA_CONTENT_ERROR_NONE;
428 char repl_path[MAX_PATH_LEN] = {0, };
429 char storage_id[MEDIA_CONTENT_UUID_SIZE+1] = {0, };
431 content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
433 ret = _media_content_replace_path(path, repl_path);
434 content_retvm_if(!STRING_VALID(repl_path), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
436 content_retvm_if(_media_util_is_ignorable_dir(repl_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid folder path");
438 ret = _media_content_check_dir(repl_path);
439 content_retvm_if(ret == MEDIA_CONTENT_ERROR_PERMISSION_DENIED, ret, "Permission Denied");
440 content_retvm_if(ret == MEDIA_CONTENT_ERROR_INVALID_PARAMETER, ret, "invalid path[%s]", path);
442 ret = media_svc_get_storage_id(_content_get_db_handle(), repl_path, storage_id, _content_get_uid());
443 if (ret != MS_MEDIA_ERR_NONE) {
444 content_error("media_svc_get_storage_id failed : %d", ret);
445 return _content_error_capi(ret);
448 ret = media_svc_check_folder_exist_by_path(_content_get_db_handle(), storage_id, repl_path);
449 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
450 content_sec_debug("media_svc_check_folder_exist_by_path : no record : %s", repl_path);
451 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
453 ret = ms_user_get_storage_type(_content_get_uid(), repl_path, &storage_type);
454 if (ret != MS_MEDIA_ERR_NONE) {
455 content_sec_error("ms_user_get_storage_type failed : %d", ret);
456 return _content_error_capi(ret);
459 ret = media_svc_insert_folder(_content_get_db_handle(), storage_id, storage_type, repl_path, _content_get_uid());
460 if (ret != MS_MEDIA_ERR_NONE) {
461 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
462 content_sec_error("This item is already inserted. This may be normal operation because other process already did this (%s)", repl_path);
463 ret = MEDIA_CONTENT_ERROR_NONE;
465 content_sec_error("media_svc_insert_folder failed : %d (%s)", ret, repl_path);
468 return _content_error_capi(ret);
471 ret = media_svc_set_folder_scan_status(storage_id, repl_path, MEDIA_DIR_SCAN_DONE, _content_get_uid());
472 if (ret != MS_MEDIA_ERR_NONE) {
473 content_sec_error("ms_set_folder_scan_status failed : %d", ret);
474 return _content_error_capi(ret);
476 } else if (ret != MS_MEDIA_ERR_NONE) {
477 content_sec_error("media_svc_check_folder_exist_by_path failed : %d (%s)", ret, repl_path);
478 return _content_error_capi(ret);
484 int media_folder_get_scan_status(const char *storage_uuid, char* path, media_folder_scan_status_e *scan_status)
486 int ret = MEDIA_CONTENT_ERROR_NONE;
487 int status = MEDIA_DIR_SCAN_NONE;
489 if (STRING_VALID(storage_uuid) && STRING_VALID(path)) {
490 ret = media_svc_get_folder_scan_status(_content_get_db_handle(), storage_uuid, path, &status);
491 if (ret != MS_MEDIA_ERR_NONE) {
492 content_error("media_svc_get_folder_scan_status failed");
493 ret = _content_error_capi(ret);
495 *scan_status = status;
498 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
499 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
505 int media_folder_reset_scan_status(const char *storage_uuid, const char* path)
507 int ret = MEDIA_CONTENT_ERROR_NONE;
508 if (STRING_VALID(path)) {
509 ret = media_svc_set_folder_validity(FALSE, storage_uuid, path, 0, TRUE, _content_get_uid());
510 if (ret != MS_MEDIA_ERR_NONE) {
511 content_error("set folder validity failed");
512 ret = _content_error_capi(ret);
516 ret = media_svc_set_folder_scan_status(storage_uuid, path, MEDIA_DIR_SCAN_NONE, _content_get_uid());
517 if (ret != MS_MEDIA_ERR_NONE) {
518 content_error("media_folder_reset_scan_status failed");
519 ret = _content_error_capi(ret);
522 content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
523 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;