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 sqlite3 *db_handle = NULL;
22 static int ref_count = 0;
23 static GMutex db_mutex;
24 static uid_t content_g_uid = 0;
26 sqlite3 * _content_get_db_handle(void)
31 uid_t _content_get_uid(void)
33 if (content_g_uid == 0)
34 return tzplatform_getuid(TZ_USER_NAME);
39 int _content_query_prepare(char *select_query, char *condition_query, char *option_query, sqlite3_stmt **stmt)
41 int ret = MEDIA_CONTENT_ERROR_NONE;
44 media_content_retvm_if(!STRING_VALID(select_query), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid select_query");
46 if (!STRING_VALID(condition_query))
47 condition_query = (char *)" ";
49 if (!STRING_VALID(option_query))
50 option_query = (char *)" ";
52 query = sqlite3_mprintf("%s %s %s", select_query, condition_query, option_query);
53 ret = _content_get_result(query, stmt);
54 SQLITE3_SAFE_FREE(query);
59 int _content_get_result(char *query, sqlite3_stmt **stmt)
61 int err = MEDIA_CONTENT_ERROR_NONE;
63 media_content_retvm_if(_content_get_db_handle() == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "database is not connected");
64 media_content_retvm_if(!STRING_VALID(query), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid query");
66 media_content_sec_debug("Query[%s]", query);
68 err = sqlite3_prepare_v2(_content_get_db_handle(), query, strlen(query), stmt, NULL);
69 if (err != SQLITE_OK) {
70 media_content_error("Failed to sqlite3_prepare_v2[%s]", sqlite3_errmsg(_content_get_db_handle()));
71 if (err == SQLITE_BUSY)
72 return MEDIA_CONTENT_ERROR_DB_BUSY;
73 else if (err == SQLITE_PERM)
74 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
76 return MEDIA_CONTENT_ERROR_DB_FAILED;
79 return MEDIA_CONTENT_ERROR_NONE;
82 #ifdef _USE_SENIOR_MODE
83 int _content_query_prepare_by_union_select(sqlite3_stmt **stmt, char *select_query1, char *condition_query1, char *option_query1, char *select_query2, char *condition_query2, char *option_query2)
86 int err = MEDIA_CONTENT_ERROR_NONE;
87 char query[MAX_QUERY_SIZE] = {0, };
88 memset(query, '\0', sizeof(query));
90 media_content_retvm_if(_content_get_db_handle() == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "database is not connected");
91 media_content_retvm_if(!STRING_VALID(select_query1), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid select_query1");
92 media_content_retvm_if(!STRING_VALID(select_query2), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid select_query2");
94 if (!STRING_VALID(condition_query1))
95 condition_query1 = (char *)" ";
97 if (!STRING_VALID(option_query1))
98 option_query1 = (char *)" ";
100 if (!STRING_VALID(condition_query2))
101 condition_query2 = (char *)" ";
103 if (!STRING_VALID(option_query2))
104 option_query2 = (char *)" ";
106 len = snprintf(query, sizeof(query), "SELECT * FROM (%s %s %s) as table1 UNION ALL SELECT * FROM (%s %s %s) as table2",
107 select_query1, condition_query1, option_query1, select_query2, condition_query2, option_query2);
108 if (len > 0 && len < sizeof(query)) {
110 } else if (len >= sizeof(query)) {
111 query[MAX_QUERY_SIZE -1] = '\0';
113 media_content_error("snprintf failed");
114 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
117 media_content_sec_debug("Query : [%s]", query);
119 err = sqlite3_prepare_v2(_content_get_db_handle(), query, strlen(query), stmt, NULL);
120 if (err != SQLITE_OK) {
121 media_content_error("DB_FAILED(0x%08x) fail to sqlite3_prepare(), %s", MEDIA_CONTENT_ERROR_DB_FAILED, sqlite3_errmsg(_content_get_db_handle()));
123 if (err == SQLITE_BUSY) {
124 media_content_error(" BUSY ERROR");
125 return MEDIA_CONTENT_ERROR_DB_BUSY;
126 } else if (err == SQLITE_PERM) {
127 media_content_error("PERMISSION EROR");
128 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
130 media_content_error("OTHER ERROR");
131 return MEDIA_CONTENT_ERROR_DB_FAILED;
135 return MEDIA_CONTENT_ERROR_NONE;
139 int _content_error_capi(int content_error)
141 if (content_error != MEDIA_CONTENT_ERROR_NONE)
142 media_content_error("MS Error[%d]", content_error);
145 if (content_error == MS_MEDIA_ERR_NONE)
146 return MEDIA_CONTENT_ERROR_NONE;
148 /* Internal operation error*/
149 else if (content_error == MS_MEDIA_ERR_INVALID_PARAMETER)
150 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
152 else if (content_error == MS_MEDIA_ERR_OUT_OF_MEMORY)
153 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
155 /* DB operation error*/
156 else if (content_error == MS_MEDIA_ERR_DB_BUSY_FAIL)
157 return MEDIA_CONTENT_ERROR_DB_BUSY;
159 else if ((content_error <= MS_MEDIA_ERR_DB_CONNECT_FAIL) && (content_error >= MS_MEDIA_ERR_DB_INTERNAL))
160 return MEDIA_CONTENT_ERROR_DB_FAILED;
162 /* IPC operation error*/
163 else if (content_error == MS_MEDIA_ERR_IPC)
164 return MEDIA_CONTENT_ERROR_NETWORK;
166 /* MEDIA SERVER error*/
167 else if (content_error == MS_MEDIA_ERR_PERMISSION_DENIED)
168 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
171 else if ((content_error == MS_MEDIA_ERR_THUMB_TOO_BIG) || (content_error == MS_MEDIA_ERR_THUMB_UNSUPPORTED))
172 return MEDIA_CONTENT_ERROR_UNSUPPORTED_CONTENT;
175 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
178 int _content_query_sql(char *query_str)
180 int ret = MEDIA_CONTENT_ERROR_NONE;
182 /*DB will be updated by Media Server.*/
183 ret = media_db_request_update_db(query_str, _content_get_uid());
185 return _content_error_capi(ret);
188 int media_content_connect(void)
190 int ret = MEDIA_CONTENT_ERROR_NONE;
192 g_mutex_lock(&db_mutex);
193 media_content_info("ref count : %d", ref_count);
195 if (ref_count == 0) {
196 if (db_handle == NULL) {
197 ret = media_db_connect(&db_handle, _content_get_uid(), false);
198 ret = _content_error_capi(ret);
199 if (ret == MEDIA_CONTENT_ERROR_NONE)
202 media_content_error("Wrong DB Connection status");
203 ret = MEDIA_CONTENT_ERROR_DB_FAILED;
206 if (db_handle != NULL) {
209 media_content_error("Wrong DB Handle status");
210 ret = MEDIA_CONTENT_ERROR_DB_FAILED;
214 media_content_info("ref count changed to: %d", ref_count);
215 g_mutex_unlock(&db_mutex);
220 int media_content_connect_with_uid(uid_t uid)
222 media_content_sec_debug("media_content_connect_with_uid [%d]", uid);
225 return media_content_connect();
228 int media_content_disconnect(void)
230 g_mutex_lock(&db_mutex);
231 media_content_debug("ref count : %d", ref_count);
233 if (db_handle && ref_count > 0) {
234 if (--ref_count == 0) {
235 media_db_disconnect(db_handle);
239 media_content_error("Database is not connected");
240 g_mutex_unlock(&db_mutex);
241 return MEDIA_CONTENT_ERROR_DB_FAILED;
244 g_mutex_unlock(&db_mutex);
246 media_content_info("ref count changed to: %d", ref_count);
248 return MEDIA_CONTENT_ERROR_NONE;
251 int media_content_scan_file(const char *path)
253 int ret = MEDIA_CONTENT_ERROR_NONE;
254 bool ignore_file = FALSE;
255 bool ignore_dir = FALSE;
256 char *folder_path = NULL;
257 int check_file = MEDIA_CONTENT_ERROR_NONE;
258 char storage_id[MEDIA_CONTENT_UUID_SIZE+1] = {0,};
259 char repl_path[MAX_PATH_LEN] = {0,};
261 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
263 media_content_sec_debug("Path : %s", path);
265 memset(repl_path, 0, sizeof(repl_path));
266 ret = _media_content_replace_path(path, repl_path);
267 media_content_retvm_if(!STRING_VALID(repl_path), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
269 ret = _media_util_check_ignore_file(repl_path, &ignore_file);
270 media_content_retvm_if(ignore_file == TRUE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path");
272 memset(storage_id, 0x00, sizeof(storage_id));
273 ret = media_svc_get_storage_id(_content_get_db_handle(), repl_path, storage_id, _content_get_uid());
274 if (ret != MS_MEDIA_ERR_NONE) {
275 media_content_error("media_svc_get_storage_id failed : %d", ret);
276 return _content_error_capi(ret);
279 check_file = _media_util_check_file_exist(repl_path);
280 if (check_file == MEDIA_CONTENT_ERROR_NONE) {
281 /* This means this path has to be inserted or refreshed */
282 folder_path = g_path_get_dirname(repl_path);
283 ret = _media_util_check_ignore_dir(folder_path, &ignore_dir);
284 SAFE_FREE(folder_path);
286 media_content_retvm_if(ignore_dir == TRUE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid folder path");
288 media_content_retvm_if(!_media_util_check_support_media_type(repl_path), MEDIA_CONTENT_ERROR_NOT_SUPPORTED, "Unsupported media type");
290 ms_user_storage_type_e storage_type;
292 ret = ms_user_get_storage_type(_content_get_uid(), repl_path, &storage_type);
293 if (ret != MS_MEDIA_ERR_NONE) {
294 media_content_sec_error("ms_user_get_storage_type failed : %d (%s)", ret, repl_path);
295 return _content_error_capi(ret);
297 ret = media_svc_check_item_exist_by_path(_content_get_db_handle(), storage_id, repl_path);
298 if (ret == MS_MEDIA_ERR_NONE) {
300 ret = media_svc_refresh_item(_content_get_db_handle(), false, storage_id, storage_type, repl_path, _content_get_uid());
301 if (ret != MS_MEDIA_ERR_NONE) {
302 media_content_error("media_svc_refresh_item failed : %d", ret);
303 return _content_error_capi(ret);
306 } else if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
308 ret = media_svc_insert_item_immediately(_content_get_db_handle(), storage_id, storage_type, repl_path, _content_get_uid());
309 if (ret != MS_MEDIA_ERR_NONE) {
310 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
311 media_content_sec_error("This item is already inserted. This may be normal operation because other process already did this (%s)", repl_path);
312 ret = MEDIA_CONTENT_ERROR_NONE;
314 media_content_sec_error("media_svc_insert_item_immediately failed : %d (%s)", ret, repl_path);
317 return _content_error_capi(ret);
320 media_content_error("media_svc_check_item_exist_by_path failed : %d", ret);
321 return _content_error_capi(ret);
323 } else if (check_file == MEDIA_CONTENT_ERROR_PERMISSION_DENIED) {
324 media_content_error("You have no permission for this file %d", ret);
325 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
327 /* This means this path has to be deleted */
328 media_content_debug("This path doesn't exists in file system... So now start to delete it from DB");
329 ret = media_svc_delete_item_by_path(_content_get_db_handle(), storage_id, repl_path, _content_get_uid());
330 if (ret != MS_MEDIA_ERR_NONE) {
331 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
332 media_content_error("Does not exist in media DB also... So, this is an invalid parameter");
333 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
336 media_content_error("media_svc_delete_item_by_path failed : %d", ret);
337 return _content_error_capi(ret);
341 return _content_error_capi(ret);
344 void _media_content_scan_cb(media_request_result_s* result, void *user_data)
347 media_content_scan_cb_data *cb_data = user_data;
349 err = _content_error_capi(result->result);
350 #ifdef _USE_TVPD_MODE
351 if (result->request_type != MEDIA_REQUEST_SCAN_COMPLETE &&
352 result->request_type != MEDIA_REQUEST_SCAN_PARTIAL) {
353 if (cb_data && cb_data->callback) {
354 media_content_debug("begin:User callback is being called now, result=%d", err);
355 cb_data->callback(err, cb_data->user_data);
356 media_content_debug("end:User callback is being called now, result=%d", err);
362 if (cb_data && cb_data->callback) {
363 media_content_debug("User callback is being called now");
364 cb_data->callback(err, cb_data->user_data);
373 #ifdef _USE_TVPD_MODE
374 void _media_content_scan_cb_v2(media_request_result_s* result, void *user_data)
377 media_content_scan_cb_data_v2 *cb_data = user_data;
378 media_content_complete_phase_e complete_phase = -1;
380 media_content_debug("cb_data is NULL");
381 err = _content_error_capi(result->result);
382 media_content_debug("result is %d", err);
384 if (result->request_type == MEDIA_REQUEST_SCAN_PARTIAL)
385 complete_phase = MEDIA_CONTENT_SCAN_PARTIAL_COMPLETE;
386 else if (result->request_type == MEDIA_REQUEST_SCAN_COMPLETE)
387 complete_phase = MEDIA_CONTENT_SCAN_COMPLETE;
388 else if (result->request_type == MEDIA_REQUEST_EXTRACT_COMPLETE)
389 complete_phase = MEDIA_CONTENT_EXTRACT_COMPLETE;
391 if (cb_data && cb_data->callback)
392 cb_data->callback(err, complete_phase, cb_data->user_data);
394 media_content_debug("run error");
396 if ((result->request_type != MEDIA_REQUEST_SCAN_COMPLETE) &&
397 (result->request_type != MEDIA_REQUEST_SCAN_PARTIAL))
404 int media_content_scan_folder(const char *path, bool is_recursive, media_scan_completed_cb callback, void *user_data)
406 int ret = MEDIA_CONTENT_ERROR_NONE;
407 bool ignore_dir = FALSE;
408 char storage_id[MEDIA_CONTENT_UUID_SIZE+1] = {0, };
409 char repl_path[MAX_PATH_LEN] = {0, };
410 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
412 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path");
413 memset(repl_path, 0, sizeof(repl_path));
414 ret = _media_content_replace_path(path, repl_path);
415 media_content_retvm_if(!STRING_VALID(repl_path), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
417 memset(storage_id, 0x00, sizeof(storage_id));
419 ret = _media_content_check_dir(repl_path);
420 media_content_retvm_if(ret == MEDIA_CONTENT_ERROR_PERMISSION_DENIED, ret, "Permission Denied");
422 if (ret == MEDIA_CONTENT_ERROR_NONE) {
423 /* If directory exist check that's ignore directory or not*/
424 ret = _media_util_check_ignore_dir(repl_path, &ignore_dir);
425 media_content_retvm_if((ignore_dir == TRUE || ret != MEDIA_CONTENT_ERROR_NONE), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid folder path");
427 /* This means this folder has to be deleted */
428 /* Or, it is real invalid path.. check storage type */
429 ret = ms_user_get_storage_type(_content_get_uid(), repl_path, &storage_type);
430 if (ret != MS_MEDIA_ERR_NONE) {
431 media_content_sec_error("ms_user_get_storage_type failed : %d (%s)", ret, repl_path);
432 return _content_error_capi(ret);
435 media_content_debug("This path doesn't exists in file system... So will be deleted it from DB");
438 ret = media_svc_get_storage_id(_content_get_db_handle(), repl_path, storage_id, _content_get_uid());
439 if (ret != MS_MEDIA_ERR_NONE) {
440 media_content_error("media_svc_get_storage_id failed : %d", ret);
441 return _content_error_capi(ret);
444 media_content_scan_cb_data *cb_data = NULL;
445 cb_data = (media_content_scan_cb_data *)malloc(sizeof(media_content_scan_cb_data));
446 media_content_retvm_if(cb_data == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
448 cb_data->callback = callback;
449 cb_data->user_data = user_data;
451 ret = media_directory_scanning_async(repl_path, storage_id, is_recursive, _media_content_scan_cb, cb_data, _content_get_uid());
452 if (ret != MS_MEDIA_ERR_NONE) {
453 media_content_error("media_directory_scanning_async failed : %d", ret);
457 return _content_error_capi(ret);
460 #ifdef _USE_TVPD_MODE
461 int media_content_scan_folder_v2(const char *path, bool is_recursive, media_scan_completed_cb_v2 callback, void *user_data)
463 int ret = MEDIA_CONTENT_ERROR_NONE;
464 bool ignore_dir = FALSE;
465 char storage_id[MEDIA_CONTENT_UUID_SIZE+1] = {0, };
467 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path");
468 memset(storage_id, 0x00, sizeof(storage_id));
470 ret = _media_util_check_ignore_dir(path, &ignore_dir);
471 media_content_retvm_if(ignore_dir, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid folder path");
473 ret = _media_content_check_dir(path);
474 media_content_retvm_if(ret == MEDIA_CONTENT_ERROR_PERMISSION_DENIED, ret, "Permission Denied");
475 media_content_retvm_if(ret == MEDIA_CONTENT_ERROR_INVALID_PARAMETER, ret, "invalid path[%s]", path);
477 media_content_scan_cb_data_v2* cb_data = NULL;
478 cb_data = (media_content_scan_cb_data_v2*)malloc(sizeof(media_content_scan_cb_data_v2));
479 media_content_retvm_if(cb_data == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
481 cb_data->callback = callback;
482 cb_data->user_data = user_data;
484 ret = media_svc_get_storage_id(_content_get_db_handle(), path, storage_id, _content_get_uid());
485 /*FIX ME. need to check ret value?*/
487 ret = media_directory_scanning_async(path, storage_id, is_recursive, _media_content_scan_cb_v2, cb_data, _content_get_uid());
488 if (ret != MS_MEDIA_ERR_NONE)
489 media_content_error("media_directory_scanning_async failed : %d", ret);
491 return _content_error_capi(ret);
495 int media_content_cancel_scan_folder(const char *path)
497 int ret = MEDIA_CONTENT_ERROR_NONE;
498 char repl_path[MAX_PATH_LEN] = {0, };
500 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path");
502 memset(repl_path, 0, sizeof(repl_path));
503 ret = _media_content_replace_path(path, repl_path);
504 media_content_retvm_if(!STRING_VALID(repl_path), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
506 ret = media_directory_scanning_cancel(repl_path, _content_get_uid());
507 if (ret != MS_MEDIA_ERR_NONE)
508 media_content_error("media_directory_scanning_async failed : %d", ret);
510 return _content_error_capi(ret);
513 void _media_content_db_update_noti_cb(
515 media_item_type_e item,
516 media_item_update_type_e update_type,
519 media_type_e content_type,
523 media_noti_cb_s *_noti_info = (media_noti_cb_s *)user_data;
525 if (_noti_info != NULL && _noti_info->update_noti_cb)
526 _noti_info->update_noti_cb(
527 MEDIA_CONTENT_ERROR_NONE,
535 _noti_info->user_data);
538 int media_content_add_db_updated_cb(media_content_db_update_cb callback, void *user_data, media_content_noti_h *noti_handle)
540 int ret = MEDIA_CONTENT_ERROR_NONE;
541 media_noti_cb_s *noti_info = NULL;
543 media_content_retvm_if(noti_handle == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "handle is NULL");
544 media_content_retvm_if(callback == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "callback is NULL");
546 noti_info = (media_noti_cb_s *)calloc(1, sizeof(media_noti_cb_s));
547 media_content_retvm_if(noti_info == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Failed to create noti info");
549 noti_info->update_noti_cb = callback;
550 noti_info->user_data = user_data;
552 ret = media_db_update_subscribe_internal((MediaNotiHandle*)noti_handle, _media_content_db_update_noti_cb, (void *)noti_info);
554 return _content_error_capi(ret);
557 #ifdef TIZEN_FEATURE_COMPATIBILITY
558 int media_content_set_db_updated_cb(media_content_db_update_cb callback, void *user_data)
560 media_content_warn("DEPRECATION WARNING: media_content_set_db_updated_cb() is removed from 5.0.");
562 return MEDIA_CONTENT_ERROR_NONE;
566 void __media_content_clear_user_data(void *user_data)
568 media_noti_cb_s *noti_info = user_data;
570 SAFE_FREE(noti_info);
575 int media_content_remove_db_updated_cb(media_content_noti_h noti_handle)
577 int ret = MEDIA_CONTENT_ERROR_NONE;
579 ret = media_db_update_unsubscribe_internal((MediaNotiHandle)noti_handle, __media_content_clear_user_data);
581 return _content_error_capi(ret);
583 #ifdef _USE_TVPD_MODE
584 GMutex* _content_get_db_mutex(void)