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>
21 static void __media_storage_get_detail(sqlite3_stmt* stmt, media_storage_h storage)
23 media_storage_s *_storage = (media_storage_s*)storage;
25 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 0)))
26 _storage->storage_id = strdup((const char *)sqlite3_column_text(stmt, 0));
28 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 1)))
29 _storage->storage_name = strdup((const char *)sqlite3_column_text(stmt, 1));
31 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 2)))
32 _storage->storage_path = strdup((const char *)sqlite3_column_text(stmt, 2));
34 if(STRING_VALID((const char *)sqlite3_column_text(stmt, 3)))
35 _storage->storage_account = strdup((const char *)sqlite3_column_text(stmt, 3));
37 _storage->storage_type = (int)sqlite3_column_int(stmt, 4);
42 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)
44 int ret = MEDIA_CONTENT_ERROR_NONE;
45 char *storage_uuid = NULL;
46 media_storage_s *_storage = NULL;
48 if(!STRING_VALID(storage_name))
50 media_content_error("Invalid Storage Name");
51 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
54 if(!STRING_VALID(storage_path))
56 media_content_error("Invalid path");
57 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
60 storage_uuid = media_info_generate_uuid();
61 if(storage_uuid == NULL)
63 media_content_error("Fail to get storage_id");
64 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
67 ret = media_svc_insert_storage(_content_get_db_handle(), storage_uuid, storage_name, storage_path, storage_account, storage_type, tzplatform_getuid(TZ_USER_NAME));
68 if (ret != MS_MEDIA_ERR_NONE)
70 ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
74 _storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
75 media_content_retvm_if(_storage == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
77 _storage->storage_id = strdup(storage_uuid);
78 _storage->storage_path = strdup(storage_path);
79 _storage->storage_name = strdup(storage_name);
80 _storage->storage_type = storage_type;
82 *storage = (media_storage_h)_storage;
87 int media_storage_delete_from_db(const char *storage_id)
89 int ret = MEDIA_CONTENT_ERROR_NONE;
91 if(!STRING_VALID(storage_id))
93 media_content_error("Invalid Storage ID");
94 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
97 ret = media_svc_delete_storage(_content_get_db_handle(), storage_id, tzplatform_getuid(TZ_USER_NAME));
99 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
102 int media_storage_get_storage_info_from_db(const char *storage_id, media_storage_h *storage)
104 int ret = MEDIA_CONTENT_ERROR_NONE;
105 char select_query[DEFAULT_QUERY_SIZE];
106 sqlite3_stmt *stmt = NULL;
108 if(!STRING_VALID(storage_id) || (storage == NULL))
110 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
111 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
114 memset(select_query, 0x00, sizeof(select_query));
115 snprintf(select_query, sizeof(select_query), SELECT_STORAGE_INFO_FROM_STORAGE, storage_id);
117 ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
118 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
120 while(sqlite3_step(stmt) == SQLITE_ROW)
122 media_storage_s *_storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
126 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
127 SQLITE3_FINALIZE(stmt);
128 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
131 __media_storage_get_detail(stmt, (media_storage_h)_storage);
133 *storage = (media_storage_h)_storage;
136 SQLITE3_FINALIZE(stmt);
141 int media_storage_get_storage_count_from_db(filter_h filter, int *storage_count)
143 int ret = MEDIA_CONTENT_ERROR_NONE;
147 ret = _media_db_get_group_count(filter, MEDIA_GROUP_STORAGE, storage_count);
151 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
152 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
158 int media_storage_foreach_storage_from_db(filter_h filter, media_storage_cb callback, void *user_data)
160 int ret = MEDIA_CONTENT_ERROR_NONE;
164 ret = _media_db_get_storage(filter, callback, user_data);
168 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
169 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
175 int media_storage_get_media_count_from_db(const char *storage_id, filter_h filter, int *media_count)
177 int ret = MEDIA_CONTENT_ERROR_NONE;
179 if(STRING_VALID(storage_id) && media_count)
181 ret = _media_db_get_group_item_count(storage_id, filter, MEDIA_GROUP_STORAGE, media_count);
185 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
186 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
192 int media_storage_foreach_media_from_db(const char *storage_id, filter_h filter, media_info_cb callback, void *user_data)
194 int ret = MEDIA_CONTENT_ERROR_NONE;
196 if((callback != NULL) && STRING_VALID(storage_id))
198 ret = _media_db_get_group_item(storage_id, filter, callback, user_data, MEDIA_GROUP_STORAGE);
202 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
203 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
209 int media_storage_destroy(media_storage_h storage)
211 int ret = MEDIA_CONTENT_ERROR_NONE;
212 media_storage_s *_storage = (media_storage_s*)storage;
215 SAFE_FREE(_storage->storage_id);
216 SAFE_FREE(_storage->storage_path);
217 SAFE_FREE(_storage->storage_name);
218 SAFE_FREE(_storage->storage_account);
221 ret = MEDIA_CONTENT_ERROR_NONE;
225 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
226 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
232 int media_storage_clone(media_storage_h *dst, media_storage_h src)
234 int ret = MEDIA_CONTENT_ERROR_NONE;
235 media_storage_s *_src = (media_storage_s*)src;
239 media_storage_s *_dst = (media_storage_s*)calloc(1, sizeof(media_storage_s));
240 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
242 if(STRING_VALID(_src->storage_id))
244 _dst->storage_id = strdup(_src->storage_id);
245 if(_dst->storage_id == NULL)
247 media_storage_destroy((media_storage_h)_dst);
248 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
249 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
253 if(STRING_VALID(_src->storage_name))
255 _dst->storage_name = strdup(_src->storage_name);
256 if(_dst->storage_name == NULL)
258 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
259 media_storage_destroy((media_storage_h)_dst);
260 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
264 if(STRING_VALID(_src->storage_path))
266 _dst->storage_path = strdup(_src->storage_path);
267 if(_dst->storage_path == NULL)
269 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
270 media_storage_destroy((media_storage_h)_dst);
271 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
275 if(STRING_VALID(_src->storage_account))
277 _dst->storage_account = strdup(_src->storage_account);
278 if(_dst->storage_account == NULL)
280 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
281 media_storage_destroy((media_storage_h)_dst);
282 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
286 _dst->storage_type = _src->storage_type;
288 *dst = (media_storage_h)_dst;
290 ret = MEDIA_CONTENT_ERROR_NONE;
294 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
295 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
301 int media_storage_get_id(media_storage_h storage, char **storage_id)
303 int ret = MEDIA_CONTENT_ERROR_NONE;
304 media_storage_s *_storage = (media_storage_s*)storage;
306 if(_storage && storage_id)
308 if(STRING_VALID(_storage->storage_id))
310 *storage_id = strdup(_storage->storage_id);
311 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
318 ret = MEDIA_CONTENT_ERROR_NONE;
322 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
323 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
329 int media_storage_get_name(media_storage_h storage, char **storage_name)
331 int ret = MEDIA_CONTENT_ERROR_NONE;
332 media_storage_s *_storage = (media_storage_s*)storage;
334 if(_storage && storage_name)
336 if(STRING_VALID(_storage->storage_name))
338 *storage_name = strdup(_storage->storage_name);
339 media_content_retvm_if(*storage_name == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
343 *storage_name = NULL;
346 ret = MEDIA_CONTENT_ERROR_NONE;
350 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
351 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
357 int media_storage_get_path(media_storage_h storage, char **storage_path)
359 int ret = MEDIA_CONTENT_ERROR_NONE;
360 media_storage_s *_storage = (media_storage_s*)storage;
362 if(_storage && storage_path)
364 if(STRING_VALID(_storage->storage_path))
366 *storage_path = strdup(_storage->storage_path);
367 media_content_retvm_if(*storage_path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
371 *storage_path = NULL;
374 ret = MEDIA_CONTENT_ERROR_NONE;
378 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
379 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
385 int media_storage_get_storage_account(media_storage_h storage, char **storage_account)
387 int ret = MEDIA_CONTENT_ERROR_NONE;
388 media_storage_s *_storage = (media_storage_s*)storage;
390 if(_storage && storage_account)
392 if(STRING_VALID(_storage->storage_account))
394 *storage_account = strdup(_storage->storage_account);
395 media_content_retvm_if(*storage_account == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
399 *storage_account = NULL;
402 ret = MEDIA_CONTENT_ERROR_NONE;
406 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
407 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
413 int media_storage_get_type(media_storage_h storage, media_content_storage_e *storage_type)
415 int ret = MEDIA_CONTENT_ERROR_NONE;
416 media_storage_s *_storage = (media_storage_s*)storage;
418 if(_storage && storage_type)
420 *storage_type = _storage->storage_type;
421 ret = MEDIA_CONTENT_ERROR_NONE;
425 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
426 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;