Merge valid_item functions
[platform/core/multimedia/media-server.git] / src / common / media-common-db-svc.c
1 /*
2  * Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <dlfcn.h>
22 #include <sys/stat.h>
23
24 #include "media-util.h"
25
26 #include "media-common-utils.h"
27 #include "media-common-db-svc.h"
28 #include "media-common-dbg.h"
29 #include <tzplatform_config.h>
30
31 #define CONFIG_PATH PATH_LIBDIR"/libmedia-content-plugin.so"
32
33 static GMutex db_mutex;
34 static void **func_array;
35 static void *func_handle = NULL;
36 static int scan_other_type = -1;
37
38 typedef int (*CLEANUP_DB)(sqlite3*, uid_t);
39 typedef int (*INSERT_ITEM_BEGIN)(bool, int);
40 typedef int (*INSERT_ITEM_END)(uid_t);
41
42 typedef int (*SEND_DIR_UPDATE_NOTI)(const char *, const char *, int, int);
43 typedef int (*INSERT_ITEM)(sqlite3 *, const char *, const char *, int, uid_t);
44 typedef int (*SET_ITEM_VALIDITY)(const char *, const char *, int, int, uid_t);
45 typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(sqlite3 *, const char *, const char *, bool, uid_t);
46 typedef int (*UPDATE_FOLDER_TIME)(sqlite3 *, const char *, const char *, uid_t);
47 typedef int (*GET_STORAGE_ID)(sqlite3 *, const char *, char *, uid_t uid);
48 typedef int (*SET_STORAGE_SCAN_STATUS)(sqlite3 *, const char *, int, uid_t);
49 typedef int (*INSERT_FOLDER)(sqlite3 *, const char *, const char *, int, uid_t);
50 typedef int (*SET_FOLDER_VALIDITY)(sqlite3 *, const char *, const char *, int, bool, uid_t);
51 typedef int (*INSERT_ITEM_SCAN)(sqlite3 *, const char *, const char *, int, uid_t);
52 typedef int (*GET_FOLDER_SCAN_STATUS)(sqlite3 *, const char *, const char *, int *);
53 typedef int (*SET_FOLDER_SCAN_STATUS)(sqlite3 *, const char *, const char *, int, uid_t);
54 typedef int (*CHECK_FOLDER_MODIFIED)(sqlite3 *, const char *, const char *, bool *);
55 typedef int (*GET_NULL_SCAN_FOLDER_LIST)(sqlite3 *, const char *, const char *, GPtrArray **);
56 typedef int (*CHANGE_VALIDITY_ITEM_BATCH)(sqlite3 *, const char *, const char *, int, int, uid_t);
57
58 typedef int (*CHECK_DB)(sqlite3*, uid_t);
59 typedef int (*GET_UUID)(char **);
60 typedef int (*CHECK_STORAGE)(sqlite3 *, const char *, char **, int *, uid_t);
61 typedef int (*INSERT_STORAGE)(sqlite3 *, const char *, int, const char *, uid_t);
62 typedef int (*UPDATE_STORAGE)(sqlite3 *, const char *, const char *, uid_t);
63 typedef int (*SET_STORAGE_VALIDITY)(sqlite3 *, const char *, int, uid_t uid);
64 typedef int (*SET_ALL_STORAGE_VALIDITY)(sqlite3 *, int, uid_t);
65
66 typedef int (*UPDATE_ITEM_META)(sqlite3 *, uid_t);
67
68 typedef int (*DELETE_INVALID_FOLDER_BY_PATH)(sqlite3 *, const char *, const char *, uid_t);
69 typedef int (*CHECK_FOLDER_EXIST)(sqlite3*, const char*, const char*);
70 typedef int (*GET_AND_EXTRACT_MEDIA)(sqlite3*, const char*, int, const char *);
71 typedef int (*GET_MEDIA_TYPE)(const char *, int *);
72 typedef int (*REFRESH_ITEM)(sqlite3 *, const char *, const char *, uid_t);
73
74 enum func_list {
75         eCLEANUP_DB,
76         eINSERT_BEGIN,
77         eINSERT_END,
78         eINSERT_BATCH,
79         eINSERT_SCAN,
80         eSET_VALIDITY,
81         eDELETE_FOLDER,
82         eSEND_DIR_UPDATE_NOTI,
83         eUPDATE_FOLDER_TIME,
84         eGET_STORAGE_ID,
85         eSET_STORAGE_SCAN_STATUS,
86         eINSERT_FOLDER,
87         eSET_FOLDER_VALIDITY,
88         eGET_FOLDER_SCAN_STATUS,
89         eSET_FOLDER_SCAN_STATUS,
90         eCHECK_FOLDER_MODIFIED,
91         eGET_NULL_SCAN_FOLDER_LIST,
92         eCHANGE_VALIDITY_ITEM_BATCH,
93         eCHECK_DB,
94         eGET_UUID,
95         eCHECK_STORAGE,
96         eINSERT_STORAGE,
97         eUPDATE_STORAGE,
98         eSET_STORAGE_VALIDITY,
99         eSET_ALL_STORAGE_VALIDITY,
100         eUPDATE_ITEM_META,
101         eDELETE_INVALID_FOLDER_BY_PATH,
102         eCHECK_FOLDER_EXIST,
103         eGET_MEDIA_TYPE,
104         eGET_AND_EXTRACT_MEDIA,
105         eREFRESH_ITEM,
106         eFUNC_MAX
107 };
108
109 int ms_load_functions(void)
110 {
111         int func_index;
112         char func_list[eFUNC_MAX][40] = {
113                 "cleanup_db",
114                 "insert_item_begin",
115                 "insert_item_end",
116                 "insert_item",
117                 "insert_item_scan",
118                 "set_item_validity",
119                 "delete_all_invalid_items_in_folder",
120                 "send_dir_update_noti",
121                 "update_folder_time",
122                 "get_storage_id",
123                 "set_storage_scan_status",
124                 "insert_folder",
125                 "set_folder_validity",
126                 "get_folder_scan_status",
127                 "set_folder_scan_status",
128                 "check_folder_modified",
129                 "get_null_scan_folder_list",
130                 "change_validity_item_batch",
131                 "check_db",
132                 "get_uuid",
133                 "check_storage",
134                 "insert_storage",
135                 "update_storage",
136                 "set_storage_validity",
137                 "set_all_storage_validity",
138                 "update_item_meta",
139                 "delete_invalid_folder_by_path",
140                 "check_folder_exist",
141                 "get_media_type",
142                 "get_and_extract_media",
143                 "refresh_item",
144                 };
145
146         func_handle = dlopen(CONFIG_PATH, RTLD_LAZY);
147         MS_DBG_RETVM_IF(!func_handle, MS_MEDIA_ERR_INTERNAL, "%s", dlerror());
148
149         dlerror();
150
151         func_array = g_new0(gpointer, eFUNC_MAX);
152
153         for (func_index = 0; func_index < eFUNC_MAX ; func_index++) {
154                 func_array[func_index] = dlsym(func_handle, func_list[func_index]);
155                 if (func_array[func_index] == NULL) {
156                         MS_DBG_ERR("dlsym failed[%s]", func_list[func_index]);
157                         g_free(func_array);
158                         dlclose(func_handle);
159
160                         return MS_MEDIA_ERR_INTERNAL;
161                 }
162         }
163
164         return MS_MEDIA_ERR_NONE;
165 }
166
167 void ms_unload_functions(void)
168 {
169         dlclose(func_handle);
170         g_free(func_array);
171 }
172
173 int ms_connect_db(sqlite3 **handle, uid_t uid)
174 {
175         int ret = MS_MEDIA_ERR_NONE;
176
177         g_mutex_lock(&db_mutex);
178
179         ret = media_db_connect(handle, uid, false);
180
181         g_mutex_unlock(&db_mutex);
182
183         return ret;
184 }
185
186 void ms_disconnect_db(sqlite3 *handle)
187 {
188         media_db_disconnect(handle);
189 }
190
191 int ms_cleanup_db(sqlite3 *handle, uid_t uid)
192 {
193         int ret = MS_MEDIA_ERR_NONE;
194
195         ret = ((CLEANUP_DB)func_array[eCLEANUP_DB])(handle, uid);
196         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CLEANUP_DB failed [%d]", ret);
197
198         MS_DBG_INFO("Cleanup Media DB");
199
200         return ret;
201 }
202
203 static int __ms_check_item_exist(sqlite3 *handle, const char *storage_id, const char *path, bool *modified)
204 {
205         int ret = MS_MEDIA_ERR_NONE;
206
207         time_t modified_time = 0;
208         unsigned long long file_size = 0;
209         struct stat st;
210         sqlite3_stmt *sql_stmt = NULL;
211         char *sql = NULL;
212
213         *modified = true;
214         MS_DBG_RETVM_IF(!MS_STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
215
216 #ifdef _USE_TVPD_MODE
217         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
218
219         sql = sqlite3_mprintf("SELECT media_modified_time, media_size, media_type, media_timeline,folder_id FROM '%q' WHERE media_path='%q'", storage_id, path);
220 #else
221         sql = sqlite3_mprintf("SELECT media_modified_time, media_size FROM media WHERE media_path='%q'", path);
222 #endif
223
224         ret = media_db_get_result(handle, sql, &sql_stmt);
225         if (ret != MS_MEDIA_ERR_NONE) {
226                 MS_DBG_ERR("Error when media_db_get_result. err = [%d]", ret);
227                 return ret;
228         }
229
230         if (sqlite3_step(sql_stmt) != SQLITE_ROW) {
231                 ret = MS_MEDIA_ERR_DB_NO_RECORD;
232         } else {
233                 modified_time = (int)sqlite3_column_int(sql_stmt, 0);
234                 file_size = (unsigned long long)sqlite3_column_int64(sql_stmt, 1);
235 #ifdef _USE_TVPD_MODE
236                 int media_type = (int)sqlite3_column_int(sql_stmt, 2);
237                 if (media_type == MS_MEDIA_TYPE_PVR)
238                         modified_time = (int)sqlite3_column_int(sql_stmt, 3);
239 #endif
240                 memset(&st, 0, sizeof(struct stat));
241                 if (stat(path, &st) == 0) {
242                         if ((st.st_mtime != modified_time) || (st.st_size != file_size)) {
243                                 *modified = true;
244                         } else {
245                                 *modified = false;
246 #ifdef _USE_TVPD_MODE
247                                 char *folder_id_media = NULL;
248                                 char *folder_id_folder = NULL;
249
250                                 folder_id_media = (char *)sqlite3_column_text(sql_stmt, 4);
251                                 char *folder_path = NULL;
252                                 folder_path = g_path_get_dirname(path);
253                                 ret = ms_get_folder_id(handle, storage_id, folder_path, &folder_id_folder);
254                                 g_free(folder_path);
255                                 if(ret == MS_MEDIA_ERR_NONE) {
256                                         if (g_strcmp0(folder_id_media, folder_id_folder) == 0)
257                                                 *modified = false;
258                                         else
259                                                 *modified = true;
260                                 } else
261                                         *modified = true;
262
263                                 g_free(folder_id_folder);
264 #endif
265                         }
266                 }
267         }
268
269         MS_SQLITE3_FINALIZE(sql_stmt);
270
271         return ret;
272 }
273
274 int ms_validate_item(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
275 {
276         int ret = MS_MEDIA_ERR_NONE;
277         bool modified = false;
278
279         ret = __ms_check_item_exist(handle, storage_id, path, &modified);
280         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
281 #ifdef _USE_TVPD_MODE
282                 ret = ms_scan_item_batch(handle, storage_id, path, uid);
283 #else
284                 ret = ms_insert_item_batch(handle, storage_id, path, uid);
285 #endif
286         } else if (ret == MS_MEDIA_ERR_NONE) {
287                 if (!modified) {
288                         ret = ((SET_ITEM_VALIDITY)func_array[eSET_VALIDITY])(storage_id, path, true, true, uid);
289                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ITEM_VALIDITY failed [%d]", ret);
290                 } else {
291                         ret = ((REFRESH_ITEM)func_array[eREFRESH_ITEM])(handle, storage_id, path, uid);
292                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "REFRESH_ITEM failed [%d]", ret);
293                 }
294         } else {
295                 MS_DBG_ERR("check_item_exist failed [%d]", ret);
296         }
297
298         return ret;
299 }
300
301 int ms_validity_change_all_items(sqlite3 *handle, const char *storage_id, bool validity , uid_t uid)
302 {
303         int ret = MS_MEDIA_ERR_NONE;
304
305 #ifdef _USE_TVPD_MODE
306         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
307
308         char *sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d;", storage_id, validity);
309 #else
310         char *sql = sqlite3_mprintf("UPDATE media SET validity=%d WHERE storage_uuid=%Q;", validity, storage_id);
311 #endif
312
313         ret = media_db_update_db_direct(sql, uid);
314         MS_SQLITE3_SAFE_FREE(sql);
315
316         return ret;
317 }
318
319 int ms_insert_item_batch(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
320 {
321         int ret = MS_MEDIA_ERR_NONE;
322         ms_user_storage_type_e storage_type = -1;
323
324         MS_DBG_FENTER();
325
326         ret = ms_user_get_storage_type(uid, path, &storage_type);
327         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
328
329         ret = ((INSERT_ITEM)func_array[eINSERT_BATCH])(handle, storage_id, path, storage_type, uid);
330         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_ITEM failed [%d]", ret);
331
332         return ret;
333 }
334
335 int ms_scan_item_batch(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
336 {
337         int ret = MS_MEDIA_ERR_NONE;
338         ms_user_storage_type_e storage_type;
339
340         MS_DBG_FENTER();
341
342         ret = ms_user_get_storage_type(uid, path, &storage_type);
343         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
344
345         ret = ((INSERT_ITEM_SCAN)func_array[eINSERT_SCAN])(handle, storage_id, path, storage_type, uid);
346         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_ITEM_SCAN failed [%d]", ret);
347
348         return ret;
349 }
350
351 int ms_get_and_extract_media(sqlite3 *handle, const char *storage_id, int scan_type, const char *path)
352 {
353         int ret = MS_MEDIA_ERR_NONE;
354
355         ret = ((GET_AND_EXTRACT_MEDIA)func_array[eGET_AND_EXTRACT_MEDIA])(handle, storage_id, scan_type, path);
356         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
357                 MS_DBG_ERR("GET_AND_EXTRACT_MEDIA failed [%d]", ret);
358
359         return ret;
360 }
361
362 int ms_delete_invalid_items(sqlite3 *handle, const char *storage_id, uid_t uid)
363 {
364         int ret = MS_MEDIA_ERR_NONE;
365         sqlite3_stmt *sql_stmt = NULL;
366         GPtrArray *thumb_list = NULL;
367         char *path = NULL;
368         int i = 0;
369
370         thumb_list = g_ptr_array_new_with_free_func(g_free);
371 #ifdef _USE_TVPD_MODE
372         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
373
374         char *sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM '%q' WHERE validity IN (%d, %d) AND media_thumbnail_path IS NOT NULL", storage_id, MS_INVALID, MS_SCANNING);
375 #else
376         char *sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM media WHERE validity IN (%d, %d) AND media_thumbnail_path IS NOT NULL", MS_INVALID, MS_SCANNING);
377 #endif
378         MS_DBG_SLOG("[SQL query] : %s", sql);
379
380         ret = media_db_get_result(handle, sql, &sql_stmt);
381         if (ret != MS_MEDIA_ERR_NONE) {
382                 MS_DBG_ERR("Query failed. err[%d]", ret);
383                 g_ptr_array_free(thumb_list, TRUE);
384                 return ret;
385         }
386
387         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
388                 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
389                 g_ptr_array_add(thumb_list, path);
390         }
391
392         MS_SQLITE3_FINALIZE(sql_stmt);
393
394 #ifdef _USE_TVPD_MODE
395         sql = sqlite3_mprintf("DELETE FROM '%q' WHERE validity IN (%d, %d)", storage_id, MS_INVALID, MS_SCANNING);
396 #else
397         sql = sqlite3_mprintf("DELETE FROM media WHERE validity IN (%d, %d)", MS_INVALID, MS_SCANNING);
398 #endif
399
400         ret = media_db_update_db_direct(sql, uid);
401         MS_SQLITE3_SAFE_FREE(sql);
402         if (ret != MS_MEDIA_ERR_NONE) {
403                 g_ptr_array_free(thumb_list, TRUE);
404                 return ret;
405         }
406
407         for (i = 0; i < thumb_list->len; i++) {
408                 path = g_ptr_array_index(thumb_list, i);
409
410                 if (remove(path) != 0)
411                         MS_DBG_ERR("fail to remove thumbnail file.");
412         }
413
414         g_ptr_array_free(thumb_list, TRUE);
415
416         return MS_MEDIA_ERR_NONE;
417 }
418
419 int ms_set_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, int recursive, uid_t uid)
420 {
421         int ret = MS_MEDIA_ERR_NONE;
422         char *sql = NULL;
423
424         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
425         MS_DBG_RETVM_IF(!MS_STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
426
427 #ifdef _USE_TVPD_MODE
428         if (recursive)
429                 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE media_path LIKE '%q/%%';", storage_id, validity, path);
430         else
431                 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE folder_id IN (SELECT folder_id FROM folder WHERE folder_path='%q' AND storage_uuid='%q');", storage_id, validity, path, storage_id);
432 #else
433         if (recursive)
434                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE media_path LIKE '%q/%%';", INTERNAL_STORAGE_ID, validity, path);
435         else
436                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_id=(SELECT folder_id FROM folder WHERE folder_path=%Q);", INTERNAL_STORAGE_ID, validity, path, storage_id);
437 #endif
438
439         ret = media_db_update_db_direct(sql, uid);
440         MS_SQLITE3_SAFE_FREE(sql);
441
442         return ret;
443 }
444
445 int ms_delete_invalid_items_in_folder(sqlite3 *handle, const char *storage_id, const char *path, bool is_recursive, uid_t uid)
446 {
447         int ret = MS_MEDIA_ERR_NONE;
448
449         ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[eDELETE_FOLDER])(handle, storage_id, path, is_recursive, uid);
450         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "DELETE_ALL_INVALID_ITEMS_IN_FOLDER failed [%d]", ret);
451
452         return ret;
453 }
454
455 int ms_send_dir_update_noti(const char*path, const char*folder_id, ms_noti_type_e noti_type, int pid)
456 {
457         int ret = MS_MEDIA_ERR_NONE;
458
459         ret = ((SEND_DIR_UPDATE_NOTI)func_array[eSEND_DIR_UPDATE_NOTI])(path, folder_id, (int)noti_type, pid);
460         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SEND_DIR_UPDATE_NOTI failed [%d]", ret);
461
462         return ret;
463 }
464
465 int ms_update_folder_time(sqlite3 *handle, const char *storage_id, char *folder_path, uid_t uid)
466 {
467         int ret = MS_MEDIA_ERR_NONE;
468
469         ret = ((UPDATE_FOLDER_TIME)func_array[eUPDATE_FOLDER_TIME])(handle, storage_id, folder_path, uid);
470         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_FOLDER_TIME failed [%d]", ret);
471
472         return ret;
473 }
474
475 int ms_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
476 {
477         int ret = MS_MEDIA_ERR_NONE;
478
479         MS_DBG_FENTER();
480
481         ret = ((GET_STORAGE_ID)func_array[eGET_STORAGE_ID])(handle, path, storage_id, uid);
482         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_STORAGE_ID failed [%d]", ret);
483
484         MS_DBG("storage_id [%s]", storage_id);
485
486         return ret;
487 }
488
489 int ms_set_storage_scan_status(sqlite3 *handle, char *storage_id, media_scan_status_e scan_status, uid_t uid)
490 {
491         int ret = MS_MEDIA_ERR_NONE;
492         int status = scan_status;
493
494         MS_DBG_FENTER();
495
496         ret = ((SET_STORAGE_SCAN_STATUS)func_array[eSET_STORAGE_SCAN_STATUS])(handle, storage_id, status, uid);
497         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_STORAGE_SCAN_STATUS failed [%d]", ret);
498
499         MS_DBG("storage_id [%s], scan_status [%d]", storage_id, scan_status);
500
501         return ret;
502 }
503
504 int ms_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
505 {
506         int ret = MS_MEDIA_ERR_NONE;
507         ms_user_storage_type_e storage_type;
508
509         ret = ms_user_get_storage_type(uid, path, &storage_type);
510         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
511
512         ret = ((INSERT_FOLDER)func_array[eINSERT_FOLDER])(handle, storage_id, path, storage_type, uid);
513         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_FOLDER failed [%d]", ret);
514
515         return ret;
516 }
517
518 int ms_delete_invalid_folder(const char *storage_id, uid_t uid)
519 {
520         int ret = MS_MEDIA_ERR_NONE;
521         char *sql = NULL;
522
523 #ifdef _USE_TVPD_MODE
524         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
525
526         sql = sqlite3_mprintf("DELETE FROM folder WHERE storage_uuid='%q' AND validity IN (%d, %d)", storage_id, MS_INVALID, MS_SCANNING);
527 #else
528         sql = sqlite3_mprintf("DELETE FROM folder WHERE validity IN (%d, %d)", MS_INVALID, MS_SCANNING);
529 #endif
530         ret = media_db_update_db_direct(sql, uid);
531
532         MS_SQLITE3_SAFE_FREE(sql);
533
534         return ret;
535 }
536
537 int ms_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, bool is_recursive, uid_t uid)
538 {
539         int ret = MS_MEDIA_ERR_NONE;
540
541         ret = ((SET_FOLDER_VALIDITY)func_array[eSET_FOLDER_VALIDITY])(handle, storage_id, path, validity, is_recursive, uid);
542         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_FOLDER_VALIDITY failed [%d]", ret);
543
544         return ret;
545 }
546
547 int ms_check_db_upgrade(sqlite3 *handle, uid_t uid)
548 {
549         int ret = MS_MEDIA_ERR_NONE;
550
551         MS_DBG_FENTER();
552
553         ret = ((CHECK_DB)func_array[eCHECK_DB])(handle, uid);
554         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_DB failed [%d]", ret);
555
556         MS_DBG_FLEAVE();
557
558         return ret;
559 }
560
561 int ms_genarate_uuid(char **uuid)
562 {
563         int ret = MS_MEDIA_ERR_NONE;
564
565         ret = ((GET_UUID)func_array[eGET_UUID])(uuid);
566         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_UUID failed [%d]", ret);
567
568         return ret;
569 }
570
571 int ms_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
572 {
573         int ret = MS_MEDIA_ERR_NONE;
574
575         MS_DBG_FENTER();
576
577         ret = ((CHECK_STORAGE)func_array[eCHECK_STORAGE])(handle, storage_id, storage_path, validity, uid);
578         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_STORAGE failed [%d]", ret);
579
580         MS_DBG_FLEAVE();
581
582         return ret;
583 }
584
585 int ms_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
586 {
587         int ret = MS_MEDIA_ERR_NONE;
588         ms_user_storage_type_e storage_type;
589
590         MS_DBG_FENTER();
591
592         ret = ms_user_get_storage_type(uid, storage_path, &storage_type);
593         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
594
595         ret = ((INSERT_STORAGE)func_array[eINSERT_STORAGE])(handle, storage_id, storage_type, storage_path, uid);
596         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_STORAGE failed [%d]", ret);
597
598         MS_DBG_FLEAVE();
599
600         return ret;
601 }
602
603 int ms_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
604 {
605         int ret = MS_MEDIA_ERR_NONE;
606
607         MS_DBG_FENTER();
608
609         ret = ((UPDATE_STORAGE)func_array[eUPDATE_STORAGE])(handle, storage_id, storage_path, uid);
610         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_STORAGE failed [%d]", ret);
611
612         MS_DBG_FLEAVE();
613
614         return ret;
615 }
616
617 int ms_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
618 {
619         int ret = MS_MEDIA_ERR_NONE;
620
621         ret = ((SET_STORAGE_VALIDITY)func_array[eSET_STORAGE_VALIDITY])(handle, storage_id, validity, uid);
622         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_STORAGE_VALIDITY failed [%d]", ret);
623
624         return ret;
625 }
626
627 int ms_set_all_storage_validity(sqlite3 *handle, int validity, uid_t uid)
628 {
629         int ret = MS_MEDIA_ERR_NONE;
630
631         ret = ((SET_ALL_STORAGE_VALIDITY)func_array[eSET_ALL_STORAGE_VALIDITY])(handle, validity, uid);
632         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ALL_STORAGE_VALIDITY failed [%d]", ret);
633
634         return ret;
635 }
636
637 int ms_update_meta_batch(sqlite3 *handle, uid_t uid)
638 {
639         int ret = MS_MEDIA_ERR_NONE;
640
641         MS_DBG_FENTER();
642
643         ret = ((UPDATE_ITEM_META)func_array[eUPDATE_ITEM_META])(handle, uid);
644         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_ITEM_META failed [%d]", ret);
645
646         return ret;
647 }
648
649 int ms_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid)
650 {
651         int ret = MS_MEDIA_ERR_NONE;
652
653         ret = ((DELETE_INVALID_FOLDER_BY_PATH)func_array[eDELETE_INVALID_FOLDER_BY_PATH])(handle, storage_id, folder_path, uid);
654         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "DELETE_INVALID_FOLDER_BY_PATH failed [%d]", ret);
655
656         return ret;
657 }
658
659 int ms_check_folder_exist(sqlite3 *handle, const char *storage_id, const char *folder_path)
660 {
661         int ret = MS_MEDIA_ERR_NONE;
662
663         ret = ((CHECK_FOLDER_EXIST)func_array[eCHECK_FOLDER_EXIST])(handle, storage_id, folder_path);
664         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_FOLDER_EXIST failed [%d]", ret);
665
666         return ret;
667 }
668
669 int ms_get_folder_id(sqlite3 *handle, const char *storage_id, const char *path, char **folder_id)
670 {
671         int ret = MS_MEDIA_ERR_NONE;
672         sqlite3_stmt *sql_stmt = NULL;
673         char *sql = NULL;
674
675         MS_DBG_RETVM_IF(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
676 #ifdef _USE_TVPD_MODE
677         MS_DBG_RETVM_IF(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
678
679         sql = sqlite3_mprintf("SELECT folder_id FROM folder WHERE (storage_uuid='%q' AND folder_path='%q')", storage_id, path);
680 #else
681         sql = sqlite3_mprintf("SELECT folder_id FROM folder WHERE folder_path=%Q", path);
682 #endif
683         ret = media_db_get_result(handle, sql, &sql_stmt);
684         MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
685
686         if (sqlite3_step(sql_stmt) == SQLITE_ROW) {
687                 *folder_id = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
688                 MS_DBG("folder_id [%s]", *folder_id);
689         } else {
690                 *folder_id = NULL;
691                 ret = MS_MEDIA_ERR_DB_NO_RECORD;
692         }
693
694         MS_SQLITE3_FINALIZE(sql_stmt);
695
696         return ret;
697 }
698
699 void ms_register_start(bool noti_status, int pid)
700 {
701         int ret = MS_MEDIA_ERR_NONE;
702
703         ret = ((INSERT_ITEM_BEGIN)func_array[eINSERT_BEGIN])(noti_status, pid);
704         if (ret != MS_MEDIA_ERR_NONE)
705                 MS_DBG_ERR("INSERT_ITEM_BEGIN failed [%d]", ret);
706 }
707
708 void ms_register_end(uid_t uid)
709 {
710         int ret = MS_MEDIA_ERR_NONE;
711
712         ret = ((INSERT_ITEM_END)func_array[eINSERT_END])(uid);
713         if (ret != MS_MEDIA_ERR_NONE)
714                 MS_DBG_ERR("INSERT_ITEM_END failed [%d]", ret);
715 }
716
717 int ms_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
718 {
719         int ret = MS_MEDIA_ERR_NONE;
720         int status = 0;
721
722         ret = ((GET_FOLDER_SCAN_STATUS)func_array[eGET_FOLDER_SCAN_STATUS])(handle, storage_id, path, &status);
723         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_FOLDER_SCAN_STATUS failed [%d]", ret);
724         *scan_status = status;
725
726         MS_DBG_SLOG("OK path = [%s], scan_status = [%d]", path, *scan_status);
727
728         return ret;
729 }
730
731 int ms_set_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int scan_status, uid_t uid)
732 {
733         int ret = MS_MEDIA_ERR_NONE;
734         int status = scan_status;
735
736         ret = ((SET_FOLDER_SCAN_STATUS)func_array[eSET_FOLDER_SCAN_STATUS])(handle, storage_id, path, status, uid);
737         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_FOLDER_SCAN_STATUS failed [%d]", ret);
738
739         MS_DBG_SLOG("OK path = [%s], scan_status = [%d]", path, scan_status);
740
741         return ret;
742 }
743
744 int ms_check_folder_modified(sqlite3 *handle, const char *path, const char *storage_id, bool *modified)
745 {
746         int ret = MS_MEDIA_ERR_NONE;
747         MS_DBG("path = [%s], storage_id = [%s]", path, storage_id);
748
749         ret = ((CHECK_FOLDER_MODIFIED)func_array[eCHECK_FOLDER_MODIFIED])(handle, path, storage_id, modified);
750         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_FOLDER_MODIFIED failed [%d]", ret);
751
752         return ret;
753 }
754
755 int ms_get_null_scan_folder_list(sqlite3 *handle, const char *stroage_id, const char *path, GPtrArray **dir_array)
756 {
757         int ret = MS_MEDIA_ERR_NONE;
758
759         ret = ((GET_NULL_SCAN_FOLDER_LIST)func_array[eGET_NULL_SCAN_FOLDER_LIST])(handle, stroage_id, path, dir_array);
760         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_NULL_SCAN_FOLDER_LIST failed [%d]", ret);
761
762         return ret;
763 }
764
765 int ms_change_validity_item_batch(sqlite3 *handle, const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
766 {
767         int ret = MS_MEDIA_ERR_NONE;
768
769         ret = ((CHANGE_VALIDITY_ITEM_BATCH)func_array[eCHANGE_VALIDITY_ITEM_BATCH])(handle, storage_id, path, des_validity, src_validity, uid);
770         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHANGE_VALIDITY_ITEM_BATCH failed [%d]", ret);
771
772         return ret;
773 }
774
775 int ms_get_media_type(const char *path, int *media_type)
776 {
777         int ret = MS_MEDIA_ERR_NONE;
778
779         ret = ((GET_MEDIA_TYPE)func_array[eGET_MEDIA_TYPE])(path, media_type);
780         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_MEDIA_TYPE failed [%d]", ret);
781
782         return ret;
783 }
784
785 bool ms_check_support_media_type(const char *path)
786 {
787         int ret = SYSTEM_INFO_ERROR_NONE;
788         int media_type = -1;
789         bool is_supported = false;
790
791         MS_DBG_RETVM_IF(!MS_STRING_VALID(path), false, "path is empty");
792
793         if (scan_other_type == -1) {
794                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
795                 if (ret != SYSTEM_INFO_ERROR_NONE) {
796                         MS_DBG_ERR("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
797                         return false;
798                 }
799
800                 scan_other_type = is_supported;
801         }
802
803         if (!scan_other_type) {
804                 ret = ms_get_media_type(path, &media_type);
805                 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
806
807                 if (media_type == MS_MEDIA_TYPE_OTHER)
808                         return false;
809         }
810
811         return true;
812 }