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_content.h>
19 #include <media_info_private.h>
20 #include <media_content_internal.h>
22 static void __media_storage_get_detail(sqlite3_stmt* stmt, media_storage_h storage)
24 media_storage_s *_storage = (media_storage_s*)storage;
26 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 0)))
27 _storage->storage_id = strdup((const char *)sqlite3_column_text(stmt, 0));
29 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 1)))
30 _storage->storage_name = strdup((const char *)sqlite3_column_text(stmt, 1));
32 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 2)))
33 _storage->storage_path = strdup((const char *)sqlite3_column_text(stmt, 2));
35 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 3)))
36 _storage->storage_account = strdup((const char *)sqlite3_column_text(stmt, 3));
38 _storage->storage_type = (int)sqlite3_column_int(stmt, 4);
43 int media_storage_insert_to_db(const char *storage_name, const char *storage_path, const char *storage_account, media_content_storage_e storage_type, media_storage_h *storage)
45 int ret = MEDIA_CONTENT_ERROR_NONE;
46 char *storage_uuid = NULL;
47 media_storage_s *_storage = NULL;
49 media_content_retvm_if(!STRING_VALID(storage_name), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid storage_name");
50 media_content_retvm_if(!STRING_VALID(storage_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid storage_path");
52 ret = media_svc_generate_uuid(&storage_uuid);
53 media_content_retvm_if(ret != MS_MEDIA_ERR_NONE, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Fail to get storage_id");
54 media_content_retvm_if(storage_uuid == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Invalid storage_id");
56 ret = media_svc_insert_storage(_content_get_db_handle(), storage_uuid, storage_name, storage_path, storage_account, storage_type, tzplatform_getuid(TZ_USER_NAME));
57 if (ret != MS_MEDIA_ERR_NONE)
59 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
60 SAFE_FREE(storage_uuid);
64 _storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
65 media_content_retvm_if(_storage == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
67 _storage->storage_id = strdup(storage_uuid);
68 _storage->storage_path = strdup(storage_path);
69 _storage->storage_name = strdup(storage_name);
70 _storage->storage_type = storage_type;
72 *storage = (media_storage_h)_storage;
74 SAFE_FREE(storage_uuid);
79 int media_storage_delete_from_db(const char *storage_id)
81 int ret = MEDIA_CONTENT_ERROR_NONE;
83 if(!STRING_VALID(storage_id))
85 media_content_error("Invalid Storage ID");
86 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
89 ret = media_svc_delete_storage(_content_get_db_handle(), storage_id, NULL, tzplatform_getuid(TZ_USER_NAME));
91 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
94 int media_storage_get_storage_info_from_db(const char *storage_id, media_storage_h *storage)
96 int ret = MEDIA_CONTENT_ERROR_NONE;
97 char select_query[DEFAULT_QUERY_SIZE];
98 sqlite3_stmt *stmt = NULL;
100 if(!STRING_VALID(storage_id) || (storage == NULL))
102 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
103 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
106 memset(select_query, 0x00, sizeof(select_query));
107 snprintf(select_query, sizeof(select_query), SELECT_STORAGE_INFO_FROM_STORAGE, storage_id);
109 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
110 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
112 while(sqlite3_step(stmt) == SQLITE_ROW)
114 media_storage_s *_storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
118 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
119 SQLITE3_FINALIZE(stmt);
120 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
123 __media_storage_get_detail(stmt, (media_storage_h)_storage);
125 *storage = (media_storage_h)_storage;
128 SQLITE3_FINALIZE(stmt);
133 int media_storage_get_storage_count_from_db(filter_h filter, int *storage_count)
135 int ret = MEDIA_CONTENT_ERROR_NONE;
139 ret = _media_db_get_group_count(filter, MEDIA_GROUP_STORAGE, storage_count);
143 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
144 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
150 int media_storage_foreach_storage_from_db(filter_h filter, media_storage_cb callback, void *user_data)
152 int ret = MEDIA_CONTENT_ERROR_NONE;
156 ret = _media_db_get_storage(filter, callback, user_data);
160 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
161 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
167 int media_storage_get_media_count_from_db(const char *storage_id, filter_h filter, int *media_count)
169 int ret = MEDIA_CONTENT_ERROR_NONE;
171 if(STRING_VALID(storage_id) && media_count)
173 ret = _media_db_get_group_item_count(storage_id, filter, MEDIA_GROUP_STORAGE, media_count);
177 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
178 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
184 int media_storage_foreach_media_from_db(const char *storage_id, filter_h filter, media_info_cb callback, void *user_data)
186 int ret = MEDIA_CONTENT_ERROR_NONE;
188 if((callback != NULL) && STRING_VALID(storage_id))
190 ret = _media_db_get_group_item(storage_id, filter, callback, user_data, MEDIA_GROUP_STORAGE);
194 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
195 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
201 int media_storage_destroy(media_storage_h storage)
203 int ret = MEDIA_CONTENT_ERROR_NONE;
204 media_storage_s *_storage = (media_storage_s*)storage;
207 SAFE_FREE(_storage->storage_id);
208 SAFE_FREE(_storage->storage_path);
209 SAFE_FREE(_storage->storage_name);
210 SAFE_FREE(_storage->storage_account);
213 ret = MEDIA_CONTENT_ERROR_NONE;
217 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
218 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
224 int media_storage_clone(media_storage_h *dst, media_storage_h src)
226 int ret = MEDIA_CONTENT_ERROR_NONE;
227 media_storage_s *_src = (media_storage_s*)src;
231 media_storage_s *_dst = (media_storage_s*)calloc(1, sizeof(media_storage_s));
232 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
234 if(STRING_VALID(_src->storage_id))
236 _dst->storage_id = strdup(_src->storage_id);
237 if(_dst->storage_id == NULL)
239 media_storage_destroy((media_storage_h)_dst);
240 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
241 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
245 if(STRING_VALID(_src->storage_name))
247 _dst->storage_name = strdup(_src->storage_name);
248 if(_dst->storage_name == NULL)
250 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
251 media_storage_destroy((media_storage_h)_dst);
252 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
256 if(STRING_VALID(_src->storage_path))
258 _dst->storage_path = strdup(_src->storage_path);
259 if(_dst->storage_path == NULL)
261 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
262 media_storage_destroy((media_storage_h)_dst);
263 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
267 if(STRING_VALID(_src->storage_account))
269 _dst->storage_account = strdup(_src->storage_account);
270 if(_dst->storage_account == NULL)
272 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
273 media_storage_destroy((media_storage_h)_dst);
274 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
278 _dst->storage_type = _src->storage_type;
280 *dst = (media_storage_h)_dst;
282 ret = MEDIA_CONTENT_ERROR_NONE;
286 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
287 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
293 int media_storage_get_id(media_storage_h storage, char **storage_id)
295 int ret = MEDIA_CONTENT_ERROR_NONE;
296 media_storage_s *_storage = (media_storage_s*)storage;
298 if(_storage && storage_id)
300 if(STRING_VALID(_storage->storage_id))
302 *storage_id = strdup(_storage->storage_id);
303 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
310 ret = MEDIA_CONTENT_ERROR_NONE;
314 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
315 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
321 int media_storage_get_name(media_storage_h storage, char **storage_name)
323 int ret = MEDIA_CONTENT_ERROR_NONE;
324 media_storage_s *_storage = (media_storage_s*)storage;
326 if(_storage && storage_name)
328 if(STRING_VALID(_storage->storage_name))
330 *storage_name = strdup(_storage->storage_name);
331 media_content_retvm_if(*storage_name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
335 *storage_name = NULL;
338 ret = MEDIA_CONTENT_ERROR_NONE;
342 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
343 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
349 int media_storage_get_path(media_storage_h storage, char **storage_path)
351 int ret = MEDIA_CONTENT_ERROR_NONE;
352 media_storage_s *_storage = (media_storage_s*)storage;
354 if(_storage && storage_path)
356 if(STRING_VALID(_storage->storage_path))
358 *storage_path = strdup(_storage->storage_path);
359 media_content_retvm_if(*storage_path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
363 *storage_path = NULL;
366 ret = MEDIA_CONTENT_ERROR_NONE;
370 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
371 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
377 int media_storage_get_storage_account(media_storage_h storage, char **storage_account)
379 int ret = MEDIA_CONTENT_ERROR_NONE;
380 media_storage_s *_storage = (media_storage_s*)storage;
382 if(_storage && storage_account)
384 if(STRING_VALID(_storage->storage_account))
386 *storage_account = strdup(_storage->storage_account);
387 media_content_retvm_if(*storage_account == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
391 *storage_account = NULL;
394 ret = MEDIA_CONTENT_ERROR_NONE;
398 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
399 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
405 int media_storage_get_type(media_storage_h storage, media_content_storage_e *storage_type)
407 int ret = MEDIA_CONTENT_ERROR_NONE;
408 media_storage_s *_storage = (media_storage_s*)storage;
410 if(_storage && storage_type)
412 *storage_type = _storage->storage_type;
413 ret = MEDIA_CONTENT_ERROR_NONE;
417 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
418 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
424 int media_storage_get_scan_status(const char *storage_uuid, media_storage_scan_status_e *scan_status)
426 int ret = MEDIA_CONTENT_ERROR_NONE;
427 media_svc_scan_status_type_e status = MEDIA_STORAGE_SCAN_NONE;
429 if(STRING_VALID(storage_uuid))
431 ret = media_svc_get_storage_scan_status(_content_get_db_handle(), storage_uuid, &status);
432 if (ret != MS_MEDIA_ERR_NONE) {
433 media_content_error("media_svc_get_storage_scan_status failed");
434 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
436 *scan_status = status;
441 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
442 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;