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)
51 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
52 SAFE_FREE(storage_uuid);
56 _storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
57 media_content_retvm_if(_storage == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
59 _storage->storage_id = strdup(storage_uuid);
60 _storage->storage_path = strdup(storage_path);
61 _storage->storage_name = strdup(storage_name);
62 _storage->storage_type = storage_type;
64 *storage = (media_storage_h)_storage;
66 SAFE_FREE(storage_uuid);
71 int media_storage_delete_from_db(const char *storage_id)
73 int ret = MEDIA_CONTENT_ERROR_NONE;
75 if(!STRING_VALID(storage_id))
77 media_content_error("Invalid Storage ID");
78 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
81 ret = media_svc_delete_storage(_content_get_db_handle(), storage_id, NULL, tzplatform_getuid(TZ_USER_NAME));
83 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
86 int media_storage_get_storage_info_from_db(const char *storage_id, media_storage_h *storage)
88 int ret = MEDIA_CONTENT_ERROR_NONE;
89 char select_query[DEFAULT_QUERY_SIZE];
90 sqlite3_stmt *stmt = NULL;
92 if(!STRING_VALID(storage_id) || (storage == NULL))
94 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
95 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
98 memset(select_query, 0x00, sizeof(select_query));
99 snprintf(select_query, sizeof(select_query), SELECT_STORAGE_INFO_FROM_STORAGE, storage_id);
101 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
102 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
104 while(sqlite3_step(stmt) == SQLITE_ROW)
106 media_storage_s *_storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
110 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
111 SQLITE3_FINALIZE(stmt);
112 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
115 __media_storage_get_detail(stmt, (media_storage_h)_storage);
117 *storage = (media_storage_h)_storage;
120 SQLITE3_FINALIZE(stmt);
125 int media_storage_get_storage_count_from_db(filter_h filter, int *storage_count)
127 int ret = MEDIA_CONTENT_ERROR_NONE;
131 ret = _media_db_get_group_count(filter, MEDIA_GROUP_STORAGE, storage_count);
135 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
136 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
142 int media_storage_foreach_storage_from_db(filter_h filter, media_storage_cb callback, void *user_data)
144 int ret = MEDIA_CONTENT_ERROR_NONE;
148 ret = _media_db_get_storage(filter, callback, user_data);
152 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
153 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
159 int media_storage_get_media_count_from_db(const char *storage_id, filter_h filter, int *media_count)
161 int ret = MEDIA_CONTENT_ERROR_NONE;
163 if(STRING_VALID(storage_id) && media_count)
165 ret = _media_db_get_group_item_count(storage_id, filter, MEDIA_GROUP_STORAGE, media_count);
169 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
170 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
176 int media_storage_foreach_media_from_db(const char *storage_id, filter_h filter, media_info_cb callback, void *user_data)
178 int ret = MEDIA_CONTENT_ERROR_NONE;
180 if((callback != NULL) && STRING_VALID(storage_id))
182 ret = _media_db_get_group_item(storage_id, filter, callback, user_data, MEDIA_GROUP_STORAGE);
186 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
187 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
193 int media_storage_destroy(media_storage_h storage)
195 int ret = MEDIA_CONTENT_ERROR_NONE;
196 media_storage_s *_storage = (media_storage_s*)storage;
199 SAFE_FREE(_storage->storage_id);
200 SAFE_FREE(_storage->storage_path);
201 SAFE_FREE(_storage->storage_name);
202 SAFE_FREE(_storage->storage_account);
205 ret = MEDIA_CONTENT_ERROR_NONE;
209 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
210 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
216 int media_storage_clone(media_storage_h *dst, media_storage_h src)
218 int ret = MEDIA_CONTENT_ERROR_NONE;
219 media_storage_s *_src = (media_storage_s*)src;
223 media_storage_s *_dst = (media_storage_s*)calloc(1, sizeof(media_storage_s));
224 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
226 if(STRING_VALID(_src->storage_id))
228 _dst->storage_id = strdup(_src->storage_id);
229 if(_dst->storage_id == NULL)
231 media_storage_destroy((media_storage_h)_dst);
232 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
233 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
237 if(STRING_VALID(_src->storage_name))
239 _dst->storage_name = strdup(_src->storage_name);
240 if(_dst->storage_name == NULL)
242 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
243 media_storage_destroy((media_storage_h)_dst);
244 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
248 if(STRING_VALID(_src->storage_path))
250 _dst->storage_path = strdup(_src->storage_path);
251 if(_dst->storage_path == NULL)
253 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
254 media_storage_destroy((media_storage_h)_dst);
255 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
259 if(STRING_VALID(_src->storage_account))
261 _dst->storage_account = strdup(_src->storage_account);
262 if(_dst->storage_account == NULL)
264 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
265 media_storage_destroy((media_storage_h)_dst);
266 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
270 _dst->storage_type = _src->storage_type;
272 *dst = (media_storage_h)_dst;
274 ret = MEDIA_CONTENT_ERROR_NONE;
278 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
279 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
285 int media_storage_get_id(media_storage_h storage, char **storage_id)
287 int ret = MEDIA_CONTENT_ERROR_NONE;
288 media_storage_s *_storage = (media_storage_s*)storage;
290 if(_storage && storage_id)
292 if(STRING_VALID(_storage->storage_id))
294 *storage_id = strdup(_storage->storage_id);
295 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
302 ret = MEDIA_CONTENT_ERROR_NONE;
306 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
307 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
313 int media_storage_get_name(media_storage_h storage, char **storage_name)
315 int ret = MEDIA_CONTENT_ERROR_NONE;
316 media_storage_s *_storage = (media_storage_s*)storage;
318 if(_storage && storage_name)
320 if(STRING_VALID(_storage->storage_name))
322 *storage_name = strdup(_storage->storage_name);
323 media_content_retvm_if(*storage_name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
327 *storage_name = NULL;
330 ret = MEDIA_CONTENT_ERROR_NONE;
334 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
335 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
341 int media_storage_get_path(media_storage_h storage, char **storage_path)
343 int ret = MEDIA_CONTENT_ERROR_NONE;
344 media_storage_s *_storage = (media_storage_s*)storage;
346 if(_storage && storage_path)
348 if(STRING_VALID(_storage->storage_path))
350 *storage_path = strdup(_storage->storage_path);
351 media_content_retvm_if(*storage_path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
355 *storage_path = NULL;
358 ret = MEDIA_CONTENT_ERROR_NONE;
362 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
363 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
369 int media_storage_get_storage_account(media_storage_h storage, char **storage_account)
371 int ret = MEDIA_CONTENT_ERROR_NONE;
372 media_storage_s *_storage = (media_storage_s*)storage;
374 if(_storage && storage_account)
376 if(STRING_VALID(_storage->storage_account))
378 *storage_account = strdup(_storage->storage_account);
379 media_content_retvm_if(*storage_account == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
383 *storage_account = NULL;
386 ret = MEDIA_CONTENT_ERROR_NONE;
390 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
391 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
397 int media_storage_get_type(media_storage_h storage, media_content_storage_e *storage_type)
399 int ret = MEDIA_CONTENT_ERROR_NONE;
400 media_storage_s *_storage = (media_storage_s*)storage;
402 if(_storage && storage_type)
404 *storage_type = _storage->storage_type;
405 ret = MEDIA_CONTENT_ERROR_NONE;
409 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
410 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
416 int media_storage_get_scan_status(const char *storage_uuid, media_storage_scan_status_e *scan_status)
418 int ret = MEDIA_CONTENT_ERROR_NONE;
419 media_svc_scan_status_type_e status = MEDIA_STORAGE_SCAN_NONE;
421 if(STRING_VALID(storage_uuid))
423 ret = media_svc_get_storage_scan_status(_content_get_db_handle(), storage_uuid, &status);
424 if (ret != MS_MEDIA_ERR_NONE) {
425 media_content_error("media_svc_get_storage_scan_status failed");
426 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
428 *scan_status = status;
433 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
434 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;