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 static char *g_src_path = NULL;
23 int media_folder_get_folder_count_from_db(filter_h filter, int *folder_count)
25 int ret = MEDIA_CONTENT_ERROR_NONE;
28 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
29 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
32 ret = _media_db_get_group_count(filter, MEDIA_GROUP_FOLDER, folder_count);
37 int media_folder_foreach_folder_from_db(filter_h filter, media_folder_cb callback, void *user_data)
39 int ret = MEDIA_CONTENT_ERROR_NONE;
41 if (callback == NULL) {
42 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
43 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
46 #ifdef _USE_TV_PROFILE
47 g_mutex_lock(_content_get_db_mutex());
50 ret = _media_db_get_folder(filter, callback, user_data);
52 #ifdef _USE_TV_PROFILE
53 g_mutex_unlock(_content_get_db_mutex());
59 int media_folder_get_media_count_from_db(const char *folder_id, filter_h filter, int *media_count)
61 int ret = MEDIA_CONTENT_ERROR_NONE;
63 if (STRING_VALID(folder_id) && media_count) {
64 ret = _media_db_get_group_item_count(folder_id, filter, MEDIA_GROUP_FOLDER, media_count);
66 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
67 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
73 int media_folder_foreach_media_from_db(const char *folder_id, filter_h filter, media_info_cb callback, void *user_data)
75 int ret = MEDIA_CONTENT_ERROR_NONE;
77 if ((callback != NULL) && STRING_VALID(folder_id)) {
78 ret = _media_db_get_group_item(folder_id, filter, callback, user_data, MEDIA_GROUP_FOLDER);
80 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
81 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
87 #ifdef _USE_SENIOR_MODE
88 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)
90 int ret = MEDIA_CONTENT_ERROR_NONE;
92 if ((callback != NULL) && STRING_VALID(folder_id)) {
93 ret = _media_db_get_group_item_by_union_select(folder_id, filter1, filter2, callback, user_data);
95 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
96 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
103 int media_folder_destroy(media_folder_h folder)
105 int ret = MEDIA_CONTENT_ERROR_NONE;
106 media_folder_s *_folder = (media_folder_s*)folder;
108 SAFE_FREE(_folder->path);
109 SAFE_FREE(_folder->name);
110 SAFE_FREE(_folder->folder_id);
111 SAFE_FREE(_folder->parent_folder_id);
112 SAFE_FREE(_folder->storage_uuid);
114 ret = MEDIA_CONTENT_ERROR_NONE;
116 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
117 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
122 int media_folder_clone(media_folder_h *dst, media_folder_h src)
124 int ret = MEDIA_CONTENT_ERROR_NONE;
125 media_folder_s *_src = (media_folder_s*)src;
128 media_folder_s *_dst = (media_folder_s*)calloc(1, sizeof(media_folder_s));
129 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
131 if (STRING_VALID(_src->folder_id)) {
132 _dst->folder_id = strdup(_src->folder_id);
133 if (_dst->folder_id == NULL) {
135 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
136 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
140 if (STRING_VALID(_src->parent_folder_id)) {
141 _dst->parent_folder_id = strdup(_src->parent_folder_id);
142 if (_dst->parent_folder_id == NULL) {
143 media_folder_destroy((media_folder_h)_dst);
144 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
145 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
149 if (STRING_VALID(_src->name)) {
150 _dst->name = strdup(_src->name);
151 if (_dst->name == NULL) {
152 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
153 media_folder_destroy((media_folder_h)_dst);
154 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
158 if (STRING_VALID(_src->path)) {
159 _dst->path = strdup(_src->path);
160 if (_dst->path == NULL) {
161 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
162 media_folder_destroy((media_folder_h)_dst);
163 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
167 if (STRING_VALID(_src->storage_uuid)) {
168 _dst->storage_uuid = strdup(_src->storage_uuid);
169 if (_dst->storage_uuid == NULL) {
170 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
171 media_folder_destroy((media_folder_h)_dst);
172 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
176 _dst->storage_type = _src->storage_type;
177 _dst->modified_time = _src->modified_time;
178 _dst->folder_order = _src->folder_order;
180 *dst = (media_folder_h)_dst;
182 ret = MEDIA_CONTENT_ERROR_NONE;
184 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
185 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
191 int media_folder_get_folder_id(media_folder_h folder, char **folder_id)
193 int ret = MEDIA_CONTENT_ERROR_NONE;
194 media_folder_s *_folder = (media_folder_s*)folder;
197 if (STRING_VALID(_folder->folder_id)) {
198 *folder_id = strdup(_folder->folder_id);
199 media_content_retvm_if(*folder_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
203 ret = MEDIA_CONTENT_ERROR_NONE;
205 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
206 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
212 int media_folder_get_parent_folder_id(media_folder_h folder, char **parent_folder_id)
214 int ret = MEDIA_CONTENT_ERROR_NONE;
215 media_content_warn("DEPRECATION WARNING: media_folder_get_parent_folder_id() is deprecated and will be removed from next release.");
216 media_folder_s *_folder = (media_folder_s*)folder;
219 if (STRING_VALID(_folder->parent_folder_id)) {
220 *parent_folder_id = strdup(_folder->parent_folder_id);
221 media_content_retvm_if(*parent_folder_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
223 *parent_folder_id = NULL;
225 ret = MEDIA_CONTENT_ERROR_NONE;
227 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
228 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
234 int media_folder_get_path(media_folder_h folder, char **path)
236 int ret = MEDIA_CONTENT_ERROR_NONE;
237 media_folder_s *_folder = (media_folder_s*)folder;
240 if (STRING_VALID(_folder->path)) {
241 *path = strdup(_folder->path);
242 media_content_retvm_if(*path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
246 ret = MEDIA_CONTENT_ERROR_NONE;
249 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
250 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
257 int media_folder_get_name(media_folder_h folder, char **name)
259 int ret = MEDIA_CONTENT_ERROR_NONE;
260 media_folder_s *_folder = (media_folder_s*)folder;
263 if (STRING_VALID(_folder->name)) {
264 *name = strdup(_folder->name);
265 media_content_retvm_if(*name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
269 ret = MEDIA_CONTENT_ERROR_NONE;
272 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
273 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
279 int media_folder_get_modified_time(media_folder_h folder, time_t* time)
281 int ret = MEDIA_CONTENT_ERROR_NONE;
282 media_folder_s *_folder = (media_folder_s*)folder;
285 *time = _folder->modified_time;
286 ret = MEDIA_CONTENT_ERROR_NONE;
288 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
289 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
295 int media_folder_get_storage_type(media_folder_h folder, media_content_storage_e* storage_type)
297 int ret = MEDIA_CONTENT_ERROR_NONE;
298 media_folder_s *_folder = (media_folder_s*)folder;
301 *storage_type = _folder->storage_type;
302 ret = MEDIA_CONTENT_ERROR_NONE;
304 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
305 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
311 int media_folder_get_storage_id(media_folder_h folder, char **storage_id)
313 int ret = MEDIA_CONTENT_ERROR_NONE;
314 media_folder_s *_folder = (media_folder_s*)folder;
317 if (STRING_VALID(_folder->storage_uuid)) {
318 *storage_id = strdup(_folder->storage_uuid);
319 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
323 ret = MEDIA_CONTENT_ERROR_NONE;
326 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
327 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
333 int media_folder_get_order(media_folder_h folder, int *order)
335 int ret = MEDIA_CONTENT_ERROR_NONE;
336 media_content_warn("DEPRECATION WARNING: media_folder_get_order() is deprecated and will be removed from next release.");
337 media_folder_s *_folder = (media_folder_s*)folder;
340 *order = _folder->folder_order;
341 ret = MEDIA_CONTENT_ERROR_NONE;
343 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
344 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
351 int media_folder_get_folder_from_db(const char *folder_id, media_folder_h *folder)
353 int ret = MEDIA_CONTENT_ERROR_NONE;
354 sqlite3_stmt *stmt = NULL;
355 char select_query[DEFAULT_QUERY_SIZE];
356 char *tmp_path = NULL;
357 char *parent_path = NULL;
358 char repl_path[MAX_PATH_LEN] = {0, };
359 media_folder_s *_folder = NULL;
361 if (!STRING_VALID(folder_id)) {
362 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
363 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
366 memset(select_query, 0x00, sizeof(select_query));
368 snprintf(select_query, sizeof(select_query), SELECT_FOLDER_FROM_FOLDER, folder_id);
370 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
371 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
373 if (sqlite3_step(stmt) == SQLITE_ROW) {
374 _folder = (media_folder_s*)calloc(1, sizeof(media_folder_s));
375 if (_folder == NULL) {
376 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
377 SQLITE3_FINALIZE(stmt);
378 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
381 _folder->folder_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
383 tmp_path = g_strdup((const char *)sqlite3_column_text(stmt, 1));
384 _media_content_rollback_path(tmp_path, repl_path);
385 _folder->path = g_strdup(repl_path);
386 _folder->name = g_strdup((const char *)sqlite3_column_text(stmt, 2));
387 _folder->modified_time = (int)sqlite3_column_int(stmt, 3);
388 _folder->storage_type = (int)sqlite3_column_int(stmt, 5);
389 _folder->storage_uuid = g_strdup((const char *)sqlite3_column_text(stmt, 6));
390 _folder->folder_order = (int)sqlite3_column_int(stmt, 7);
392 *folder = (media_folder_h)_folder;
395 SQLITE3_FINALIZE(stmt);
398 if (STRING_VALID(tmp_path)) {
399 parent_path = g_path_get_dirname(tmp_path);
400 memset(select_query, 0x00, sizeof(select_query));
401 snprintf(select_query, sizeof(select_query), SELECT_FOLDER_ID_BY_PATH, parent_path);
404 SAFE_FREE(parent_path);
406 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
407 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
409 if (sqlite3_step(stmt) == SQLITE_ROW)
410 _folder->parent_folder_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
412 SQLITE3_FINALIZE(stmt);
418 int media_folder_update_to_db(media_folder_h folder)
420 int ret = MEDIA_CONTENT_ERROR_NONE;
421 media_content_warn("DEPRECATION WARNING: media_folder_update_to_db() is deprecated and will be removed from next release.");
422 media_folder_s *_folder = (media_folder_s*)folder;
424 char repl_path[MAX_PATH_LEN] = {0, };
426 if ((_folder == NULL) || (_folder->folder_id == NULL)) {
427 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
428 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
431 /* Update folder order */
432 sql = sqlite3_mprintf(UPDATE_FOLDER_ORDER, _folder->folder_order, _folder->folder_id);
434 ret = _content_query_sql(sql);
435 SQLITE3_SAFE_FREE(sql);
437 if (STRING_VALID(_folder->folder_id) && g_src_path) {
438 char *name_pinyin = NULL;
439 bool pinyin_support = FALSE;
441 ret = _media_content_replace_path(_folder->path, repl_path);
443 /* Set modified time */
446 _folder->modified_time = date;
448 /*Update Pinyin If Support Pinyin*/
449 media_svc_check_pinyin_support(&pinyin_support);
451 media_svc_get_pinyin(_folder->name, &name_pinyin);
453 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q', modified_time=%d, name_pinyin='%q' WHERE folder_uuid=%Q",
454 DB_TABLE_FOLDER, repl_path, _folder->name, _folder->modified_time, name_pinyin, _folder->folder_id);
456 ret = _content_query_sql(sql);
457 SQLITE3_SAFE_FREE(sql);
458 if (ret != MEDIA_CONTENT_ERROR_NONE) {
459 SAFE_FREE(g_src_path);
463 /* Update all folder record's path, which are matched by old parent path */
464 char *update_folder_path_sql = NULL;
465 char src_path_slash[MAX_PATH_LEN + 1] = {0, };
466 char dst_path_slash[MAX_PATH_LEN + 1] = {0, };
468 snprintf(src_path_slash, sizeof(src_path_slash), "%s/", g_src_path);
469 snprintf(dst_path_slash, sizeof(dst_path_slash), "%s/", repl_path);
471 update_folder_path_sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q', '%q');", src_path_slash, dst_path_slash);
473 ret = _content_query_sql(update_folder_path_sql);
474 SQLITE3_SAFE_FREE(update_folder_path_sql);
475 if (ret != MEDIA_CONTENT_ERROR_NONE) {
476 SAFE_FREE(g_src_path);
480 ret = media_svc_rename_folder(_content_get_db_handle(), _folder->storage_uuid, g_src_path, repl_path, tzplatform_getuid(TZ_USER_NAME));
481 SAFE_FREE(g_src_path);
483 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
489 int media_folder_set_name(media_folder_h folder, const char *name)
491 int ret = MEDIA_CONTENT_ERROR_NONE;
492 media_content_warn("DEPRECATION WARNING: media_folder_set_name() is deprecated and will be removed from next release.");
493 media_folder_s *_folder = (media_folder_s*)folder;
495 if (_folder != NULL && STRING_VALID(name)) {
496 if (STRING_VALID(_folder->path) && STRING_VALID(_folder->name)) {
497 char *folder_path = NULL;
498 char new_folder_path[MAX_PATH_LEN] = {0,};
499 bool ignore_dir = FALSE;
500 char repl_path[MAX_PATH_LEN] = {0, };
502 memset(repl_path, 0, sizeof(repl_path));
503 ret = _media_content_replace_path(_folder->path, repl_path);
504 media_content_retvm_if(!STRING_VALID(repl_path), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
506 folder_path = g_path_get_dirname(repl_path);
507 media_content_sec_debug("Existed Folder Path : %s", repl_path);
508 snprintf(new_folder_path, sizeof(new_folder_path), "%s/%s", folder_path, name);
509 media_content_sec_debug("New Path : %s", new_folder_path);
511 ret = _media_util_check_ignore_dir(new_folder_path, &ignore_dir);
512 if (ignore_dir == TRUE) {
513 media_content_error("Invalid folder path");
514 SAFE_FREE(folder_path);
518 SAFE_FREE(g_src_path);
519 g_src_path = strdup(repl_path);
521 SAFE_FREE(_folder->path);
522 SAFE_FREE(_folder->name);
523 SAFE_FREE(folder_path);
524 memset(repl_path, 0, sizeof(repl_path));
525 ret = _media_content_rollback_path(new_folder_path, repl_path);
526 media_content_retvm_if(!STRING_VALID(repl_path), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
528 _folder->path = strdup(repl_path);
529 media_content_retvm_if(_folder->path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
532 _folder->name = strdup(name);
533 media_content_retvm_if(_folder->name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
535 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
536 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
542 int media_folder_set_order(media_folder_h folder, int order)
544 int ret = MEDIA_CONTENT_ERROR_NONE;
545 media_content_warn("DEPRECATION WARNING: media_folder_set_order() is deprecated and will be removed from next release.");
546 media_folder_s *_folder = (media_folder_s*)folder;
548 if (_folder != NULL && order >= 0) {
549 _folder->folder_order = order;
551 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
552 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
557 #ifdef _USE_TV_PROFILE
558 int media_folder_get_scan_status(const char *storage_uuid, char* path, media_folder_scan_status_e *scan_status)
560 int ret = MEDIA_CONTENT_ERROR_NONE;
561 int status = MEDIA_DIR_SCAN_NONE;
563 if (STRING_VALID(storage_uuid) && STRING_VALID(path)) {
564 ret = media_svc_get_folder_scan_status(_content_get_db_handle(), storage_uuid, path, &status);
565 if (ret != MS_MEDIA_ERR_NONE) {
566 media_content_error("media_svc_get_folder_scan_status failed");
567 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
569 *scan_status = status;
572 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
573 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;