9bb00e7e4fd0604a04652afffada87f6580929ca
[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                 ret = ms_insert_item_batch(handle, storage_id, path, uid);
282         } else if (ret == MS_MEDIA_ERR_NONE) {
283                 if (!modified) {
284                         ret = ((SET_ITEM_VALIDITY)func_array[eSET_VALIDITY])(storage_id, path, true, true, uid);
285                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ITEM_VALIDITY failed [%d]", ret);
286                 } else {
287                         ret = ((REFRESH_ITEM)func_array[eREFRESH_ITEM])(handle, storage_id, path, uid);
288                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "REFRESH_ITEM failed [%d]", ret);
289                 }
290         } else {
291                 MS_DBG_ERR("check_item_exist failed [%d]", ret);
292         }
293
294         return ret;
295 }
296
297 #ifdef _USE_TVPD_MODE
298 int ms_scan_validate_item(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
299 {
300         int ret = MS_MEDIA_ERR_NONE;
301         bool modified = false;
302
303         ret = __ms_check_item_exist(handle, storage_id, path, &modified);
304         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
305                 ret = ms_scan_item_batch(handle, storage_id, path, uid);
306         } else if (ret == MS_MEDIA_ERR_NONE) {
307                 if (!modified) {
308                         ret = ((SET_ITEM_VALIDITY)func_array[eSET_VALIDITY])(storage_id, path, true, true, uid);
309                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ITEM_VALIDITY failed [%d]", ret);
310                 } else {
311                         ret = ((REFRESH_ITEM)func_array[eREFRESH_ITEM])(handle, storage_id, path, uid);
312                         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "REFRESH_ITEM failed [%d]", ret);
313                 }
314         } else {
315                 MS_DBG_ERR("check_item_exist failed [%d]", ret);
316         }
317
318         return ret;
319 }
320 #endif
321
322 int ms_validity_change_all_items(sqlite3 *handle, const char *storage_id, bool validity , uid_t uid)
323 {
324         int ret = MS_MEDIA_ERR_NONE;
325
326 #ifdef _USE_TVPD_MODE
327         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
328
329         char *sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d;", storage_id, validity);
330 #else
331         char *sql = sqlite3_mprintf("UPDATE media SET validity=%d WHERE storage_uuid=%Q;", validity, storage_id);
332 #endif
333
334         ret = media_db_update_db_direct(sql, uid);
335         MS_SQLITE3_SAFE_FREE(sql);
336
337         return ret;
338 }
339
340 int ms_insert_item_batch(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
341 {
342         int ret = MS_MEDIA_ERR_NONE;
343         ms_user_storage_type_e storage_type = -1;
344
345         MS_DBG_FENTER();
346
347         ret = ms_user_get_storage_type(uid, path, &storage_type);
348         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
349
350         ret = ((INSERT_ITEM)func_array[eINSERT_BATCH])(handle, storage_id, path, storage_type, uid);
351         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_ITEM failed [%d]", ret);
352
353         return ret;
354 }
355
356 int ms_scan_item_batch(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
357 {
358         int ret = MS_MEDIA_ERR_NONE;
359         ms_user_storage_type_e storage_type;
360
361         MS_DBG_FENTER();
362
363         ret = ms_user_get_storage_type(uid, path, &storage_type);
364         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
365
366         ret = ((INSERT_ITEM_SCAN)func_array[eINSERT_SCAN])(handle, storage_id, path, storage_type, uid);
367         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_ITEM_SCAN failed [%d]", ret);
368
369         return ret;
370 }
371
372 int ms_get_and_extract_media(sqlite3 *handle, const char *storage_id, int scan_type, const char *path)
373 {
374         int ret = MS_MEDIA_ERR_NONE;
375
376         ret = ((GET_AND_EXTRACT_MEDIA)func_array[eGET_AND_EXTRACT_MEDIA])(handle, storage_id, scan_type, path);
377         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
378                 MS_DBG_ERR("GET_AND_EXTRACT_MEDIA failed [%d]", ret);
379
380         return ret;
381 }
382
383 int ms_delete_invalid_items(sqlite3 *handle, const char *storage_id, uid_t uid)
384 {
385         int ret = MS_MEDIA_ERR_NONE;
386         sqlite3_stmt *sql_stmt = NULL;
387         GPtrArray *thumb_list = NULL;
388         char *path = NULL;
389         int i = 0;
390
391         thumb_list = g_ptr_array_new_with_free_func(g_free);
392 #ifdef _USE_TVPD_MODE
393         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
394
395         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);
396 #else
397         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);
398 #endif
399         MS_DBG_SLOG("[SQL query] : %s", sql);
400
401         ret = media_db_get_result(handle, sql, &sql_stmt);
402         if (ret != MS_MEDIA_ERR_NONE) {
403                 MS_DBG_ERR("Query failed. err[%d]", ret);
404                 g_ptr_array_free(thumb_list, TRUE);
405                 return ret;
406         }
407
408         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
409                 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
410                 g_ptr_array_add(thumb_list, path);
411         }
412
413         MS_SQLITE3_FINALIZE(sql_stmt);
414
415 #ifdef _USE_TVPD_MODE
416         sql = sqlite3_mprintf("DELETE FROM '%q' WHERE validity IN (%d, %d)", storage_id, MS_INVALID, MS_SCANNING);
417 #else
418         sql = sqlite3_mprintf("DELETE FROM media WHERE validity IN (%d, %d)", MS_INVALID, MS_SCANNING);
419 #endif
420
421         ret = media_db_update_db_direct(sql, uid);
422         MS_SQLITE3_SAFE_FREE(sql);
423         if (ret != MS_MEDIA_ERR_NONE) {
424                 g_ptr_array_free(thumb_list, TRUE);
425                 return ret;
426         }
427
428         for (i = 0; i < thumb_list->len; i++) {
429                 path = g_ptr_array_index(thumb_list, i);
430
431                 if (remove(path) != 0)
432                         MS_DBG_ERR("fail to remove thumbnail file.");
433         }
434
435         g_ptr_array_free(thumb_list, TRUE);
436
437         return MS_MEDIA_ERR_NONE;
438 }
439
440 int ms_set_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, int recursive, uid_t uid)
441 {
442         int ret = MS_MEDIA_ERR_NONE;
443         char *sql = NULL;
444
445         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
446         MS_DBG_RETVM_IF(!MS_STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
447
448 #ifdef _USE_TVPD_MODE
449         if (recursive)
450                 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE media_path LIKE '%q/%%';", storage_id, validity, path);
451         else
452                 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);
453 #else
454         if (recursive)
455                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE media_path LIKE '%q/%%';", INTERNAL_STORAGE_ID, validity, path);
456         else
457                 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);
458 #endif
459
460         ret = media_db_update_db_direct(sql, uid);
461         MS_SQLITE3_SAFE_FREE(sql);
462
463         return ret;
464 }
465
466 int ms_delete_invalid_items_in_folder(sqlite3 *handle, const char *storage_id, const char *path, bool is_recursive, uid_t uid)
467 {
468         int ret = MS_MEDIA_ERR_NONE;
469
470         ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[eDELETE_FOLDER])(handle, storage_id, path, is_recursive, uid);
471         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "DELETE_ALL_INVALID_ITEMS_IN_FOLDER failed [%d]", ret);
472
473         return ret;
474 }
475
476 int ms_send_dir_update_noti(const char*path, const char*folder_id, ms_noti_type_e noti_type, int pid)
477 {
478         int ret = MS_MEDIA_ERR_NONE;
479
480         ret = ((SEND_DIR_UPDATE_NOTI)func_array[eSEND_DIR_UPDATE_NOTI])(path, folder_id, (int)noti_type, pid);
481         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SEND_DIR_UPDATE_NOTI failed [%d]", ret);
482
483         return ret;
484 }
485
486 int ms_update_folder_time(sqlite3 *handle, const char *storage_id, char *folder_path, uid_t uid)
487 {
488         int ret = MS_MEDIA_ERR_NONE;
489
490         ret = ((UPDATE_FOLDER_TIME)func_array[eUPDATE_FOLDER_TIME])(handle, storage_id, folder_path, uid);
491         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_FOLDER_TIME failed [%d]", ret);
492
493         return ret;
494 }
495
496 int ms_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
497 {
498         int ret = MS_MEDIA_ERR_NONE;
499
500         MS_DBG_FENTER();
501
502         ret = ((GET_STORAGE_ID)func_array[eGET_STORAGE_ID])(handle, path, storage_id, uid);
503         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_STORAGE_ID failed [%d]", ret);
504
505         MS_DBG("storage_id [%s]", storage_id);
506
507         return ret;
508 }
509
510 int ms_set_storage_scan_status(sqlite3 *handle, char *storage_id, media_scan_status_e scan_status, uid_t uid)
511 {
512         int ret = MS_MEDIA_ERR_NONE;
513         int status = scan_status;
514
515         MS_DBG_FENTER();
516
517         ret = ((SET_STORAGE_SCAN_STATUS)func_array[eSET_STORAGE_SCAN_STATUS])(handle, storage_id, status, uid);
518         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_STORAGE_SCAN_STATUS failed [%d]", ret);
519
520         MS_DBG("storage_id [%s], scan_status [%d]", storage_id, scan_status);
521
522         return ret;
523 }
524
525 int ms_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
526 {
527         int ret = MS_MEDIA_ERR_NONE;
528         ms_user_storage_type_e storage_type;
529
530         ret = ms_user_get_storage_type(uid, path, &storage_type);
531         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
532
533         ret = ((INSERT_FOLDER)func_array[eINSERT_FOLDER])(handle, storage_id, path, storage_type, uid);
534         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_FOLDER failed [%d]", ret);
535
536         return ret;
537 }
538
539 int ms_delete_invalid_folder(const char *storage_id, uid_t uid)
540 {
541         int ret = MS_MEDIA_ERR_NONE;
542         char *sql = NULL;
543
544 #ifdef _USE_TVPD_MODE
545         MS_DBG_RETVM_IF(!MS_STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
546
547         sql = sqlite3_mprintf("DELETE FROM folder WHERE storage_uuid='%q' AND validity IN (%d, %d)", storage_id, MS_INVALID, MS_SCANNING);
548 #else
549         sql = sqlite3_mprintf("DELETE FROM folder WHERE validity IN (%d, %d)", MS_INVALID, MS_SCANNING);
550 #endif
551         ret = media_db_update_db_direct(sql, uid);
552
553         MS_SQLITE3_SAFE_FREE(sql);
554
555         return ret;
556 }
557
558 int ms_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, bool is_recursive, uid_t uid)
559 {
560         int ret = MS_MEDIA_ERR_NONE;
561
562         ret = ((SET_FOLDER_VALIDITY)func_array[eSET_FOLDER_VALIDITY])(handle, storage_id, path, validity, is_recursive, uid);
563         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_FOLDER_VALIDITY failed [%d]", ret);
564
565         return ret;
566 }
567
568 int ms_check_db_upgrade(sqlite3 *handle, uid_t uid)
569 {
570         int ret = MS_MEDIA_ERR_NONE;
571
572         MS_DBG_FENTER();
573
574         ret = ((CHECK_DB)func_array[eCHECK_DB])(handle, uid);
575         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_DB failed [%d]", ret);
576
577         MS_DBG_FLEAVE();
578
579         return ret;
580 }
581
582 int ms_genarate_uuid(char **uuid)
583 {
584         int ret = MS_MEDIA_ERR_NONE;
585
586         ret = ((GET_UUID)func_array[eGET_UUID])(uuid);
587         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_UUID failed [%d]", ret);
588
589         return ret;
590 }
591
592 int ms_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
593 {
594         int ret = MS_MEDIA_ERR_NONE;
595
596         MS_DBG_FENTER();
597
598         ret = ((CHECK_STORAGE)func_array[eCHECK_STORAGE])(handle, storage_id, storage_path, validity, uid);
599         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_STORAGE failed [%d]", ret);
600
601         MS_DBG_FLEAVE();
602
603         return ret;
604 }
605
606 int ms_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
607 {
608         int ret = MS_MEDIA_ERR_NONE;
609         ms_user_storage_type_e storage_type;
610
611         MS_DBG_FENTER();
612
613         ret = ms_user_get_storage_type(uid, storage_path, &storage_type);
614         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
615
616         ret = ((INSERT_STORAGE)func_array[eINSERT_STORAGE])(handle, storage_id, storage_type, storage_path, uid);
617         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "INSERT_STORAGE failed [%d]", ret);
618
619         MS_DBG_FLEAVE();
620
621         return ret;
622 }
623
624 int ms_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
625 {
626         int ret = MS_MEDIA_ERR_NONE;
627
628         MS_DBG_FENTER();
629
630         ret = ((UPDATE_STORAGE)func_array[eUPDATE_STORAGE])(handle, storage_id, storage_path, uid);
631         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_STORAGE failed [%d]", ret);
632
633         MS_DBG_FLEAVE();
634
635         return ret;
636 }
637
638 int ms_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
639 {
640         int ret = MS_MEDIA_ERR_NONE;
641
642         ret = ((SET_STORAGE_VALIDITY)func_array[eSET_STORAGE_VALIDITY])(handle, storage_id, validity, uid);
643         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_STORAGE_VALIDITY failed [%d]", ret);
644
645         return ret;
646 }
647
648 int ms_set_all_storage_validity(sqlite3 *handle, int validity, uid_t uid)
649 {
650         int ret = MS_MEDIA_ERR_NONE;
651
652         ret = ((SET_ALL_STORAGE_VALIDITY)func_array[eSET_ALL_STORAGE_VALIDITY])(handle, validity, uid);
653         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_ALL_STORAGE_VALIDITY failed [%d]", ret);
654
655         return ret;
656 }
657
658 int ms_update_meta_batch(sqlite3 *handle, uid_t uid)
659 {
660         int ret = MS_MEDIA_ERR_NONE;
661
662         MS_DBG_FENTER();
663
664         ret = ((UPDATE_ITEM_META)func_array[eUPDATE_ITEM_META])(handle, uid);
665         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_ITEM_META failed [%d]", ret);
666
667         return ret;
668 }
669
670 int ms_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid)
671 {
672         int ret = MS_MEDIA_ERR_NONE;
673
674         ret = ((DELETE_INVALID_FOLDER_BY_PATH)func_array[eDELETE_INVALID_FOLDER_BY_PATH])(handle, storage_id, folder_path, uid);
675         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "DELETE_INVALID_FOLDER_BY_PATH failed [%d]", ret);
676
677         return ret;
678 }
679
680 int ms_check_folder_exist(sqlite3 *handle, const char *storage_id, const char *folder_path)
681 {
682         int ret = MS_MEDIA_ERR_NONE;
683
684         ret = ((CHECK_FOLDER_EXIST)func_array[eCHECK_FOLDER_EXIST])(handle, storage_id, folder_path);
685         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_FOLDER_EXIST failed [%d]", ret);
686
687         return ret;
688 }
689
690 int ms_get_folder_id(sqlite3 *handle, const char *storage_id, const char *path, char **folder_id)
691 {
692         int ret = MS_MEDIA_ERR_NONE;
693         sqlite3_stmt *sql_stmt = NULL;
694         char *sql = NULL;
695
696         MS_DBG_RETVM_IF(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
697 #ifdef _USE_TVPD_MODE
698         MS_DBG_RETVM_IF(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
699
700         sql = sqlite3_mprintf("SELECT folder_id FROM folder WHERE (storage_uuid='%q' AND folder_path='%q')", storage_id, path);
701 #else
702         sql = sqlite3_mprintf("SELECT folder_id FROM folder WHERE folder_path=%Q", path);
703 #endif
704         ret = media_db_get_result(handle, sql, &sql_stmt);
705         MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
706
707         if (sqlite3_step(sql_stmt) == SQLITE_ROW) {
708                 *folder_id = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
709                 MS_DBG("folder_id [%s]", *folder_id);
710         } else {
711                 *folder_id = NULL;
712                 ret = MS_MEDIA_ERR_DB_NO_RECORD;
713         }
714
715         MS_SQLITE3_FINALIZE(sql_stmt);
716
717         return ret;
718 }
719
720 void ms_register_start(bool noti_status, int pid)
721 {
722         int ret = MS_MEDIA_ERR_NONE;
723
724         ret = ((INSERT_ITEM_BEGIN)func_array[eINSERT_BEGIN])(noti_status, pid);
725         if (ret != MS_MEDIA_ERR_NONE)
726                 MS_DBG_ERR("INSERT_ITEM_BEGIN failed [%d]", ret);
727 }
728
729 void ms_register_end(uid_t uid)
730 {
731         int ret = MS_MEDIA_ERR_NONE;
732
733         ret = ((INSERT_ITEM_END)func_array[eINSERT_END])(uid);
734         if (ret != MS_MEDIA_ERR_NONE)
735                 MS_DBG_ERR("INSERT_ITEM_END failed [%d]", ret);
736 }
737
738 int ms_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
739 {
740         int ret = MS_MEDIA_ERR_NONE;
741         int status = 0;
742
743         ret = ((GET_FOLDER_SCAN_STATUS)func_array[eGET_FOLDER_SCAN_STATUS])(handle, storage_id, path, &status);
744         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_FOLDER_SCAN_STATUS failed [%d]", ret);
745         *scan_status = status;
746
747         MS_DBG_SLOG("OK path = [%s], scan_status = [%d]", path, *scan_status);
748
749         return ret;
750 }
751
752 int ms_set_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int scan_status, uid_t uid)
753 {
754         int ret = MS_MEDIA_ERR_NONE;
755         int status = scan_status;
756
757         ret = ((SET_FOLDER_SCAN_STATUS)func_array[eSET_FOLDER_SCAN_STATUS])(handle, storage_id, path, status, uid);
758         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "SET_FOLDER_SCAN_STATUS failed [%d]", ret);
759
760         MS_DBG_SLOG("OK path = [%s], scan_status = [%d]", path, scan_status);
761
762         return ret;
763 }
764
765 int ms_check_folder_modified(sqlite3 *handle, const char *path, const char *storage_id, bool *modified)
766 {
767         int ret = MS_MEDIA_ERR_NONE;
768         MS_DBG("path = [%s], storage_id = [%s]", path, storage_id);
769
770         ret = ((CHECK_FOLDER_MODIFIED)func_array[eCHECK_FOLDER_MODIFIED])(handle, path, storage_id, modified);
771         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHECK_FOLDER_MODIFIED failed [%d]", ret);
772
773         return ret;
774 }
775
776 int ms_get_null_scan_folder_list(sqlite3 *handle, const char *stroage_id, const char *path, GPtrArray **dir_array)
777 {
778         int ret = MS_MEDIA_ERR_NONE;
779
780         ret = ((GET_NULL_SCAN_FOLDER_LIST)func_array[eGET_NULL_SCAN_FOLDER_LIST])(handle, stroage_id, path, dir_array);
781         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_NULL_SCAN_FOLDER_LIST failed [%d]", ret);
782
783         return ret;
784 }
785
786 int ms_change_validity_item_batch(sqlite3 *handle, const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
787 {
788         int ret = MS_MEDIA_ERR_NONE;
789
790         ret = ((CHANGE_VALIDITY_ITEM_BATCH)func_array[eCHANGE_VALIDITY_ITEM_BATCH])(handle, storage_id, path, des_validity, src_validity, uid);
791         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "CHANGE_VALIDITY_ITEM_BATCH failed [%d]", ret);
792
793         return ret;
794 }
795
796 int ms_get_media_type(const char *path, int *media_type)
797 {
798         int ret = MS_MEDIA_ERR_NONE;
799
800         ret = ((GET_MEDIA_TYPE)func_array[eGET_MEDIA_TYPE])(path, media_type);
801         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "GET_MEDIA_TYPE failed [%d]", ret);
802
803         return ret;
804 }
805
806 bool ms_check_support_media_type(const char *path)
807 {
808         int ret = SYSTEM_INFO_ERROR_NONE;
809         int media_type = -1;
810         bool is_supported = false;
811
812         MS_DBG_RETVM_IF(!MS_STRING_VALID(path), false, "path is empty");
813
814         if (scan_other_type == -1) {
815                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
816                 if (ret != SYSTEM_INFO_ERROR_NONE) {
817                         MS_DBG_ERR("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
818                         return false;
819                 }
820
821                 scan_other_type = is_supported;
822         }
823
824         if (!scan_other_type) {
825                 ret = ms_get_media_type(path, &media_type);
826                 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
827
828                 if (media_type == MS_MEDIA_TYPE_OTHER)
829                         return false;
830         }
831
832         return true;
833 }