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 _storage->storage_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
27 _storage->storage_name = g_strdup((const char *)sqlite3_column_text(stmt, 1));
28 _storage->storage_path = g_strdup((const char *)sqlite3_column_text(stmt, 2));
29 _storage->storage_account = g_strdup((const char *)sqlite3_column_text(stmt, 3));
30 _storage->storage_type = (int)sqlite3_column_int(stmt, 4);
35 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)
37 int ret = MEDIA_CONTENT_ERROR_NONE;
38 char *storage_uuid = NULL;
39 media_storage_s *_storage = NULL;
41 media_content_retvm_if(!STRING_VALID(storage_name), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid storage_name");
42 media_content_retvm_if(!STRING_VALID(storage_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid storage_path");
44 ret = media_svc_generate_uuid(&storage_uuid);
45 media_content_retvm_if(ret != MS_MEDIA_ERR_NONE, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Fail to get storage_id");
46 media_content_retvm_if(storage_uuid == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Invalid storage_id");
48 ret = media_svc_insert_storage(_content_get_db_handle(), storage_uuid, storage_name, storage_path, storage_account, storage_type, tzplatform_getuid(TZ_USER_NAME));
49 if (ret != MS_MEDIA_ERR_NONE) {
50 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
51 SAFE_FREE(storage_uuid);
55 _storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
56 media_content_retvm_if(_storage == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
58 _storage->storage_id = strdup(storage_uuid);
59 _storage->storage_path = strdup(storage_path);
60 _storage->storage_name = strdup(storage_name);
61 _storage->storage_type = storage_type;
63 *storage = (media_storage_h)_storage;
65 SAFE_FREE(storage_uuid);
70 int media_storage_delete_from_db(const char *storage_id)
72 int ret = MEDIA_CONTENT_ERROR_NONE;
74 if (!STRING_VALID(storage_id)) {
75 media_content_error("Invalid Storage ID");
76 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
79 ret = media_svc_delete_storage(_content_get_db_handle(), storage_id, NULL, tzplatform_getuid(TZ_USER_NAME));
81 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
84 int media_storage_get_storage_info_from_db(const char *storage_id, media_storage_h *storage)
86 int ret = MEDIA_CONTENT_ERROR_NONE;
87 char select_query[DEFAULT_QUERY_SIZE];
88 sqlite3_stmt *stmt = NULL;
90 if (!STRING_VALID(storage_id) || (storage == NULL)) {
91 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
92 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
95 memset(select_query, 0x00, sizeof(select_query));
96 snprintf(select_query, sizeof(select_query), SELECT_STORAGE_INFO_FROM_STORAGE, storage_id);
98 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
99 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
101 while (sqlite3_step(stmt) == SQLITE_ROW) {
102 media_storage_s *_storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
104 if (_storage == NULL) {
105 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
106 SQLITE3_FINALIZE(stmt);
107 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
110 __media_storage_get_detail(stmt, (media_storage_h)_storage);
112 *storage = (media_storage_h)_storage;
115 SQLITE3_FINALIZE(stmt);
120 int media_storage_get_storage_count_from_db(filter_h filter, int *storage_count)
122 int ret = MEDIA_CONTENT_ERROR_NONE;
125 ret = _media_db_get_group_count(filter, MEDIA_GROUP_STORAGE, storage_count);
127 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
128 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
134 int media_storage_foreach_storage_from_db(filter_h filter, media_storage_cb callback, void *user_data)
136 int ret = MEDIA_CONTENT_ERROR_NONE;
138 if (callback != NULL) {
139 ret = _media_db_get_storage(filter, callback, user_data);
141 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
142 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
148 int media_storage_get_media_count_from_db(const char *storage_id, filter_h filter, int *media_count)
150 int ret = MEDIA_CONTENT_ERROR_NONE;
152 if (STRING_VALID(storage_id) && media_count) {
153 ret = _media_db_get_group_item_count(storage_id, filter, MEDIA_GROUP_STORAGE, media_count);
155 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
156 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
162 int media_storage_foreach_media_from_db(const char *storage_id, filter_h filter, media_info_cb callback, void *user_data)
164 int ret = MEDIA_CONTENT_ERROR_NONE;
166 if ((callback != NULL) && STRING_VALID(storage_id)) {
167 ret = _media_db_get_group_item(storage_id, filter, callback, user_data, MEDIA_GROUP_STORAGE);
169 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
170 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
176 int media_storage_destroy(media_storage_h storage)
178 int ret = MEDIA_CONTENT_ERROR_NONE;
179 media_storage_s *_storage = (media_storage_s*)storage;
181 SAFE_FREE(_storage->storage_id);
182 SAFE_FREE(_storage->storage_path);
183 SAFE_FREE(_storage->storage_name);
184 SAFE_FREE(_storage->storage_account);
187 ret = MEDIA_CONTENT_ERROR_NONE;
189 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
190 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
196 int media_storage_clone(media_storage_h *dst, media_storage_h src)
198 int ret = MEDIA_CONTENT_ERROR_NONE;
199 media_storage_s *_src = (media_storage_s*)src;
202 media_storage_s *_dst = (media_storage_s*)calloc(1, sizeof(media_storage_s));
203 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
205 if (STRING_VALID(_src->storage_id)) {
206 _dst->storage_id = strdup(_src->storage_id);
207 if (_dst->storage_id == NULL) {
208 media_storage_destroy((media_storage_h)_dst);
209 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
210 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
214 if (STRING_VALID(_src->storage_name)) {
215 _dst->storage_name = strdup(_src->storage_name);
216 if (_dst->storage_name == NULL) {
217 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
218 media_storage_destroy((media_storage_h)_dst);
219 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
223 if (STRING_VALID(_src->storage_path)) {
224 _dst->storage_path = strdup(_src->storage_path);
225 if (_dst->storage_path == NULL) {
226 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
227 media_storage_destroy((media_storage_h)_dst);
228 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
232 if (STRING_VALID(_src->storage_account)) {
233 _dst->storage_account = strdup(_src->storage_account);
234 if (_dst->storage_account == NULL) {
235 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
236 media_storage_destroy((media_storage_h)_dst);
237 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
241 _dst->storage_type = _src->storage_type;
243 *dst = (media_storage_h)_dst;
245 ret = MEDIA_CONTENT_ERROR_NONE;
247 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
248 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
254 int media_storage_get_id(media_storage_h storage, char **storage_id)
256 int ret = MEDIA_CONTENT_ERROR_NONE;
257 media_storage_s *_storage = (media_storage_s*)storage;
259 if (_storage && storage_id) {
260 if (STRING_VALID(_storage->storage_id)) {
261 *storage_id = strdup(_storage->storage_id);
262 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
267 ret = MEDIA_CONTENT_ERROR_NONE;
269 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
270 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
276 int media_storage_get_name(media_storage_h storage, char **storage_name)
278 int ret = MEDIA_CONTENT_ERROR_NONE;
279 media_storage_s *_storage = (media_storage_s*)storage;
281 if (_storage && storage_name) {
282 if (STRING_VALID(_storage->storage_name)) {
283 *storage_name = strdup(_storage->storage_name);
284 media_content_retvm_if(*storage_name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
286 *storage_name = NULL;
289 ret = MEDIA_CONTENT_ERROR_NONE;
291 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
292 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
298 int media_storage_get_path(media_storage_h storage, char **storage_path)
300 int ret = MEDIA_CONTENT_ERROR_NONE;
301 media_storage_s *_storage = (media_storage_s*)storage;
303 if (_storage && storage_path) {
304 if (STRING_VALID(_storage->storage_path)) {
305 *storage_path = strdup(_storage->storage_path);
306 media_content_retvm_if(*storage_path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
308 *storage_path = NULL;
311 ret = MEDIA_CONTENT_ERROR_NONE;
313 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
314 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
320 int media_storage_get_storage_account(media_storage_h storage, char **storage_account)
322 int ret = MEDIA_CONTENT_ERROR_NONE;
323 media_storage_s *_storage = (media_storage_s*)storage;
325 if (_storage && storage_account) {
326 if (STRING_VALID(_storage->storage_account)) {
327 *storage_account = strdup(_storage->storage_account);
328 media_content_retvm_if(*storage_account == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
330 *storage_account = NULL;
333 ret = MEDIA_CONTENT_ERROR_NONE;
335 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
336 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
342 int media_storage_get_type(media_storage_h storage, media_content_storage_e *storage_type)
344 int ret = MEDIA_CONTENT_ERROR_NONE;
345 media_storage_s *_storage = (media_storage_s*)storage;
347 if (_storage && storage_type) {
348 *storage_type = _storage->storage_type;
349 ret = MEDIA_CONTENT_ERROR_NONE;
351 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
352 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
358 int media_storage_get_scan_status(const char *storage_uuid, media_storage_scan_status_e *scan_status)
360 int ret = MEDIA_CONTENT_ERROR_NONE;
361 media_svc_scan_status_type_e status = MEDIA_STORAGE_SCAN_NONE;
363 if (STRING_VALID(storage_uuid)) {
364 ret = media_svc_get_storage_scan_status(_content_get_db_handle(), storage_uuid, &status);
365 if (ret != MS_MEDIA_ERR_NONE) {
366 media_content_error("media_svc_get_storage_scan_status failed");
367 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
369 *scan_status = status;
372 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
373 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;