Apply tizen coding rule
[platform/core/multimedia/libmedia-service.git] / plugin / media-content-plugin.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.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
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <mm_file.h>
25 #include <media-thumbnail.h>
26 #include "media-svc.h"
27 #include "media-svc-util.h"
28
29 #define MEDIA_SVC_PLUGIN_ERROR_NONE             0
30 #define MEDIA_SVC_PLUGIN_ERROR                  -1
31
32 #define STRING_VALID(str)       \
33         ((str != NULL && strlen(str) > 0) ? TRUE : FALSE)
34 #define STORAGE_VALID(storage)\
35         (((storage == MEDIA_SVC_STORAGE_INTERNAL) || (storage == MEDIA_SVC_STORAGE_EXTERNAL) || (storage == MEDIA_SVC_STORAGE_EXTERNAL_USB)) ? TRUE : FALSE)
36
37
38 typedef enum {
39         ERR_HANDLE = 1,
40         ERR_FILE_PATH,
41         ERR_FOLDER_PATH,
42         ERR_MIME_TYPE,
43         ERR_NOT_MEDIAFILE,
44         ERR_STORAGE_TYPE,
45         ERR_CHECK_ITEM,
46         ERR_MAX,
47 } media_svc_error_type_e;
48
49 static void __set_error_message(int err_type, char **err_msg);
50
51 static void __set_error_message(int err_type, char **err_msg)
52 {
53         if (err_msg)
54                 *err_msg = NULL;
55         else
56                 return;
57
58         if (err_type == ERR_HANDLE)
59                 *err_msg = strdup("invalid handle");
60         else if (err_type == ERR_FILE_PATH)
61                 *err_msg = strdup("invalid file path");
62         else if (err_type == ERR_FOLDER_PATH)
63                 *err_msg = strdup("invalid folder path");
64         else if (err_type == ERR_MIME_TYPE)
65                 *err_msg = strdup("invalid mime type");
66         else if (err_type == ERR_NOT_MEDIAFILE)
67                 *err_msg = strdup("not media content");
68         else if (err_type == ERR_STORAGE_TYPE)
69                 *err_msg = strdup("invalid storage type");
70         else if (err_type == ERR_CHECK_ITEM)
71                 *err_msg = strdup("item does not exist");
72         else if (err_type == MS_MEDIA_ERR_DB_CONNECT_FAIL)
73                 *err_msg = strdup("DB connect error");
74         else if (err_type == MS_MEDIA_ERR_DB_DISCONNECT_FAIL)
75                 *err_msg = strdup("DB disconnect error");
76         else if (err_type == MS_MEDIA_ERR_INVALID_PARAMETER)
77                 *err_msg = strdup("invalid parameter");
78         else if (err_type == MS_MEDIA_ERR_DB_INTERNAL)
79                 *err_msg = strdup("DB internal error");
80         else if (err_type == MS_MEDIA_ERR_DB_NO_RECORD)
81                 *err_msg = strdup("not found in DB");
82         else if (err_type == MS_MEDIA_ERR_INTERNAL)
83                 *err_msg = strdup("media service internal error");
84         else if (err_type == MS_MEDIA_ERR_DB_CORRUPT)
85                 *err_msg = strdup("DB corrupt error");
86         else
87                 *err_msg = strdup("error unknown");
88
89         return;
90 }
91
92 int connect_db(void **handle, uid_t uid, char **err_msg)
93 {
94         int ret = media_svc_connect(handle, uid, true);
95
96         if (ret < 0) {
97                 __set_error_message(ret, err_msg);
98                 return MEDIA_SVC_PLUGIN_ERROR;
99         }
100
101         return MEDIA_SVC_PLUGIN_ERROR_NONE;
102 }
103
104 int disconnect_db(void *handle, char **err_msg)
105 {
106         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
107
108         if (handle == NULL) {
109                 __set_error_message(ERR_HANDLE, err_msg);
110                 return MEDIA_SVC_PLUGIN_ERROR;
111         }
112
113         ret = media_svc_disconnect(handle);
114         if (ret < 0) {
115                 __set_error_message(ret, err_msg);
116                 return MEDIA_SVC_PLUGIN_ERROR;
117         }
118
119         return MEDIA_SVC_PLUGIN_ERROR_NONE;
120 }
121
122 int check_item_exist(void *handle, const char *storage_id, const char *file_path, bool *modified, char **err_msg)
123 {
124         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
125         *modified = TRUE;
126
127         if (handle == NULL) {
128                 __set_error_message(ERR_HANDLE, err_msg);
129                 return MEDIA_SVC_PLUGIN_ERROR;
130         }
131
132         if (!STRING_VALID(file_path)) {
133                 __set_error_message(ERR_FILE_PATH, err_msg);
134                 return MEDIA_SVC_PLUGIN_ERROR;
135         }
136
137         time_t modified_time = 0;
138         unsigned long long file_size = 0;
139         struct stat st;
140
141         ret = media_svc_get_file_info(handle, storage_id, file_path, &modified_time, &file_size);
142         if (ret == MS_MEDIA_ERR_NONE) {
143                 memset(&st, 0, sizeof(struct stat));
144                 if (stat(file_path, &st) == 0) {
145                         if ((st.st_mtime != modified_time) || (st.st_size != file_size))
146                                 *modified = TRUE;
147                         else
148                                 *modified = FALSE;
149                 }
150
151                 return MEDIA_SVC_PLUGIN_ERROR_NONE;     /*exist */
152         }
153
154         __set_error_message(ERR_CHECK_ITEM, err_msg);
155
156         return MEDIA_SVC_PLUGIN_ERROR;          /*not exist */
157 }
158
159 int insert_item_begin(void *handle, int item_cnt, int with_noti, int from_pid, char **err_msg)
160 {
161         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
162
163         if (handle == NULL) {
164                 __set_error_message(ERR_HANDLE, err_msg);
165                 return MEDIA_SVC_PLUGIN_ERROR;
166         }
167
168         ret = media_svc_insert_item_begin(handle, item_cnt, with_noti, from_pid);
169         if (ret < 0) {
170                 __set_error_message(ret, err_msg);
171                 return MEDIA_SVC_PLUGIN_ERROR;
172         }
173
174         return MEDIA_SVC_PLUGIN_ERROR_NONE;
175 }
176
177 int insert_item_end(void *handle, uid_t uid, char **err_msg)
178 {
179         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
180
181         if (handle == NULL) {
182                 __set_error_message(ERR_HANDLE, err_msg);
183                 return MEDIA_SVC_PLUGIN_ERROR;
184         }
185
186         ret = media_svc_insert_item_end(handle, uid);
187         if (ret < 0) {
188                 __set_error_message(ret, err_msg);
189                 return MEDIA_SVC_PLUGIN_ERROR;
190         }
191
192         return MEDIA_SVC_PLUGIN_ERROR_NONE;
193 }
194
195 int insert_item(void *handle, const char *storage_id, const char *file_path, int storage_type, uid_t uid, char **err_msg)
196 {
197         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
198
199         if (handle == NULL) {
200                 __set_error_message(ERR_HANDLE, err_msg);
201                 return MEDIA_SVC_PLUGIN_ERROR;
202         }
203
204         if (!STRING_VALID(file_path)) {
205                 __set_error_message(ERR_FILE_PATH, err_msg);
206                 return MEDIA_SVC_PLUGIN_ERROR;
207         }
208
209         if (!STORAGE_VALID(storage_type)) {
210                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
211                 return MEDIA_SVC_PLUGIN_ERROR;
212         }
213
214         ret = media_svc_insert_item_bulk(handle, storage_id, storage_type, file_path, FALSE, uid);
215         if (ret < 0) {
216                 __set_error_message(ret, err_msg);
217                 return MEDIA_SVC_PLUGIN_ERROR;
218         }
219
220         return MEDIA_SVC_PLUGIN_ERROR_NONE;
221 }
222
223 int insert_item_immediately(void *handle, const char *storage_id, const char *file_path, int storage_type, uid_t uid, char **err_msg)
224 {
225         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
226
227         if (handle == NULL) {
228                 __set_error_message(ERR_HANDLE, err_msg);
229                 return MEDIA_SVC_PLUGIN_ERROR;
230         }
231
232         if (!STRING_VALID(file_path)) {
233                 __set_error_message(ERR_FILE_PATH, err_msg);
234                 return MEDIA_SVC_PLUGIN_ERROR;
235         }
236
237         if (!STORAGE_VALID(storage_type)) {
238                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
239                 return MEDIA_SVC_PLUGIN_ERROR;
240         }
241
242         ret = media_svc_insert_item_immediately(handle, storage_id, storage_type, file_path, uid);
243         if (ret < 0) {
244                 __set_error_message(ret, err_msg);
245                 return MEDIA_SVC_PLUGIN_ERROR;
246         }
247
248         return MEDIA_SVC_PLUGIN_ERROR_NONE;
249 }
250
251 int insert_burst_item(void *handle, const char *storage_id, const char *file_path, int storage_type, uid_t uid, char **err_msg)
252 {
253         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
254
255         if (handle == NULL) {
256                 __set_error_message(ERR_HANDLE, err_msg);
257                 return MEDIA_SVC_PLUGIN_ERROR;
258         }
259
260         if (!STRING_VALID(file_path)) {
261                 __set_error_message(ERR_FILE_PATH, err_msg);
262                 return MEDIA_SVC_PLUGIN_ERROR;
263         }
264
265         if (!STORAGE_VALID(storage_type)) {
266                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
267                 return MEDIA_SVC_PLUGIN_ERROR;
268         }
269
270         ret = media_svc_insert_item_bulk(handle, storage_id, storage_type, file_path, TRUE, uid);
271         if (ret < 0) {
272                 __set_error_message(ret, err_msg);
273                 return MEDIA_SVC_PLUGIN_ERROR;
274         }
275
276         return MEDIA_SVC_PLUGIN_ERROR_NONE;
277 }
278
279 int set_all_storage_items_validity(void *handle, const char *storage_id, int storage_type, int validity, uid_t uid, char **err_msg)
280 {
281         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
282
283         if (handle == NULL) {
284                 __set_error_message(ERR_HANDLE, err_msg);
285                 return MEDIA_SVC_PLUGIN_ERROR;
286         }
287
288         if (!STORAGE_VALID(storage_type)) {
289                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
290                 return MEDIA_SVC_PLUGIN_ERROR;
291         }
292
293         ret = media_svc_set_all_storage_items_validity(handle, storage_id, storage_type, validity, uid);
294         if (ret < 0) {
295                 __set_error_message(ret, err_msg);
296                 return MEDIA_SVC_PLUGIN_ERROR;
297         }
298
299         return MEDIA_SVC_PLUGIN_ERROR_NONE;
300 }
301
302 int set_folder_item_validity(void *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid, char **err_msg)
303 {
304         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
305
306         if (handle == NULL) {
307                 __set_error_message(ERR_HANDLE, err_msg);
308                 return MEDIA_SVC_PLUGIN_ERROR;
309         }
310
311         if (!STRING_VALID(folder_path)) {
312                 __set_error_message(ERR_FOLDER_PATH, err_msg);
313                 return MEDIA_SVC_PLUGIN_ERROR;
314         }
315
316         ret = media_svc_set_folder_items_validity(handle, storage_id, folder_path, validity, recursive, uid);
317         if (ret < 0) {
318                 __set_error_message(ret, err_msg);
319                 return MEDIA_SVC_PLUGIN_ERROR;
320         }
321
322         return MEDIA_SVC_PLUGIN_ERROR_NONE;
323 }
324
325 int set_item_validity_begin(void *handle, int item_cnt, char **err_msg)
326 {
327         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
328
329         if (handle == NULL) {
330                 __set_error_message(ERR_HANDLE, err_msg);
331                 return MEDIA_SVC_PLUGIN_ERROR;
332         }
333
334         ret = media_svc_set_item_validity_begin(handle, item_cnt);
335         if (ret < 0) {
336                 __set_error_message(ret, err_msg);
337                 return MEDIA_SVC_PLUGIN_ERROR;
338         }
339
340         return MEDIA_SVC_PLUGIN_ERROR_NONE;
341 }
342
343 int set_item_validity_end(void *handle, uid_t uid, char **err_msg)
344 {
345         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
346
347         if (handle == NULL) {
348                 __set_error_message(ERR_HANDLE, err_msg);
349                 return MEDIA_SVC_PLUGIN_ERROR;
350         }
351
352         ret = media_svc_set_item_validity_end(handle, uid);
353         if (ret < 0) {
354                 __set_error_message(ret, err_msg);
355                 return MEDIA_SVC_PLUGIN_ERROR;
356         }
357
358         return MEDIA_SVC_PLUGIN_ERROR_NONE;
359 }
360
361 int set_item_validity(void *handle, const char *storage_id, const char *file_path, int storage_type, int validity, uid_t uid, char **err_msg)
362 {
363         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
364
365         if (handle == NULL) {
366                 __set_error_message(ERR_HANDLE, err_msg);
367                 return MEDIA_SVC_PLUGIN_ERROR;
368         }
369
370         if (!STRING_VALID(file_path)) {
371                 __set_error_message(ERR_FILE_PATH, err_msg);
372                 return MEDIA_SVC_PLUGIN_ERROR;
373         }
374
375         ret = media_svc_set_item_validity(handle, storage_id, file_path, validity, uid);
376
377         if (ret < 0) {
378                 __set_error_message(ret, err_msg);
379                 return MEDIA_SVC_PLUGIN_ERROR;
380         }
381
382         return MEDIA_SVC_PLUGIN_ERROR_NONE;
383 }
384
385 int delete_item(void *handle, const char *storage_id, const char *file_path, uid_t uid, char **err_msg)
386 {
387         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
388
389         if (handle == NULL) {
390                 __set_error_message(ERR_HANDLE, err_msg);
391                 return MEDIA_SVC_PLUGIN_ERROR;
392         }
393
394         if (!STRING_VALID(file_path)) {
395                 __set_error_message(ERR_FILE_PATH, err_msg);
396                 return MEDIA_SVC_PLUGIN_ERROR;
397         }
398
399         ret = media_svc_check_item_exist_by_path(handle, storage_id, file_path);
400         if (ret == 0) {
401                 ret = media_svc_delete_item_by_path(handle, storage_id, file_path, uid);
402
403                 if (ret < 0) {
404                         __set_error_message(ret, err_msg);
405                         return MEDIA_SVC_PLUGIN_ERROR;
406                 } else
407                         return MEDIA_SVC_PLUGIN_ERROR_NONE;
408         }
409
410         __set_error_message(ERR_CHECK_ITEM, err_msg);   /*not exist in DB so can't delete item. */
411         return MEDIA_SVC_PLUGIN_ERROR;
412 }
413
414 int delete_all_items_in_storage(void *handle, const char *storage_id, int storage_type, uid_t uid, char **err_msg)
415 {
416         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
417
418         if (handle == NULL) {
419                 __set_error_message(ERR_HANDLE, err_msg);
420                 return MEDIA_SVC_PLUGIN_ERROR;
421         }
422
423         if (!STORAGE_VALID(storage_type)) {
424                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
425                 return MEDIA_SVC_PLUGIN_ERROR;
426         }
427
428         ret = media_svc_delete_all_items_in_storage(handle, storage_id, storage_type, uid);
429         if (ret < 0) {
430                 __set_error_message(ret, err_msg);
431                 return MEDIA_SVC_PLUGIN_ERROR;
432         }
433
434         return MEDIA_SVC_PLUGIN_ERROR_NONE;
435 }
436
437 int delete_all_invalid_items_in_storage(void *handle, const char *storage_id, int storage_type, uid_t uid, char **err_msg)
438 {
439         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
440
441         if (handle == NULL) {
442                 __set_error_message(ERR_HANDLE, err_msg);
443                 return MEDIA_SVC_PLUGIN_ERROR;
444         }
445
446         if (!STORAGE_VALID(storage_type)) {
447                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
448                 return MEDIA_SVC_PLUGIN_ERROR;
449         }
450
451         ret = media_svc_delete_invalid_items_in_storage(handle, storage_id, storage_type, uid);
452         if (ret < 0) {
453                 __set_error_message(ret, err_msg);
454                 return MEDIA_SVC_PLUGIN_ERROR;
455         }
456
457         return MEDIA_SVC_PLUGIN_ERROR_NONE;
458 }
459
460 int delete_all_invalid_items_in_folder(void *handle, const char *storage_id, const char *folder_path, bool is_recursve, uid_t uid, char **err_msg)
461 {
462         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
463
464         if (handle == NULL) {
465                 __set_error_message(ERR_HANDLE, err_msg);
466                 return MEDIA_SVC_PLUGIN_ERROR;
467         }
468
469         if (!STRING_VALID(folder_path)) {
470                 __set_error_message(ERR_FOLDER_PATH, err_msg);
471                 return MEDIA_SVC_PLUGIN_ERROR;
472         }
473
474         ret = media_svc_delete_invalid_items_in_folder(handle, storage_id, folder_path, is_recursve, uid);
475         if (ret < 0) {
476                 __set_error_message(ret, err_msg);
477                 return MEDIA_SVC_PLUGIN_ERROR;
478         }
479
480         return MEDIA_SVC_PLUGIN_ERROR_NONE;
481 }
482
483
484 int update_begin(void)
485 {
486         return MEDIA_SVC_PLUGIN_ERROR_NONE;
487 }
488
489 int update_end(const char *start_path, uid_t uid)
490 {
491         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
492
493         ret = thumbnail_request_extract_all_thumbs(uid);
494         if (ret < 0) {
495                 return MEDIA_SVC_PLUGIN_ERROR;
496         }
497
498         return MEDIA_SVC_PLUGIN_ERROR_NONE;
499 }
500
501 int send_dir_update_noti(void *handle, const char *storage_id, const char *dir_path, const char *folder_id, int update_type, int pid, char **err_msg)
502 {
503         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
504
505         if (!STRING_VALID(dir_path)) {
506                 __set_error_message(ERR_FOLDER_PATH, err_msg);
507                 return MEDIA_SVC_PLUGIN_ERROR;
508         }
509
510         ret = media_svc_send_dir_update_noti(handle, storage_id, dir_path, folder_id, (media_item_update_type_e)update_type, pid);
511         if (ret < 0) {
512                 __set_error_message(ret, err_msg);
513                 return MEDIA_SVC_PLUGIN_ERROR;
514         }
515
516         return MEDIA_SVC_PLUGIN_ERROR_NONE;
517 }
518
519 int count_delete_items_in_folder(void *handle, const char *storage_id, const char *folder_path, int *count, char **err_msg)
520 {
521         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
522
523         if (handle == NULL) {
524                 __set_error_message(ERR_HANDLE, err_msg);
525                 return MEDIA_SVC_PLUGIN_ERROR;
526         }
527
528         if (count == NULL) {
529                 __set_error_message(ERR_HANDLE, err_msg);
530                 return MEDIA_SVC_PLUGIN_ERROR;
531         }
532
533         if (!STRING_VALID(folder_path)) {
534                 __set_error_message(ERR_FOLDER_PATH, err_msg);
535                 return MEDIA_SVC_PLUGIN_ERROR;
536         }
537
538         ret = media_svc_count_invalid_items_in_folder(handle, storage_id, folder_path, count);
539         if (ret < 0) {
540                 __set_error_message(ret, err_msg);
541                 return MEDIA_SVC_PLUGIN_ERROR;
542         }
543
544         return MEDIA_SVC_PLUGIN_ERROR_NONE;
545 }
546
547 int check_db(void *handle, bool *need_full_scan, uid_t uid, char **err_msg)
548 {
549         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
550
551         /*check db schema*/
552         ret = media_svc_create_table(handle, uid);
553         if (ret < 0) {
554                 __set_error_message(ret, err_msg);
555                 return MEDIA_SVC_PLUGIN_ERROR;
556         }
557
558         /*check db version*/
559         ret = media_svc_check_db_upgrade(handle, need_full_scan, uid);
560         if (ret < 0) {
561                 __set_error_message(ret, err_msg);
562                 return MEDIA_SVC_PLUGIN_ERROR;
563         }
564
565         return MEDIA_SVC_PLUGIN_ERROR_NONE;
566 }
567
568 int check_db_corrupt(void *handle, char **err_msg)
569 {
570         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
571
572         /*check db version*/
573         ret = media_svc_check_db_corrupt(handle);
574         if (ret < 0) {
575                 __set_error_message(ret, err_msg);
576                 return MEDIA_SVC_PLUGIN_ERROR;
577         }
578
579         return MEDIA_SVC_PLUGIN_ERROR_NONE;
580 }
581
582 int get_folder_list(void *handle, const char *storage_id, char *start_path, char ***folder_list, int **modified_time_list, int **item_num_list, int *count, char **err_msg)
583 {
584         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
585
586         if (handle == NULL) {
587                 __set_error_message(ERR_HANDLE, err_msg);
588                 return MEDIA_SVC_PLUGIN_ERROR;
589         }
590
591         if (count == NULL) {
592                 __set_error_message(ERR_HANDLE, err_msg);
593                 return MEDIA_SVC_PLUGIN_ERROR;
594         }
595
596         ret = media_svc_get_folder_list(handle, start_path, folder_list, (time_t **)modified_time_list, item_num_list, count);
597         if (ret < 0) {
598                 __set_error_message(ret, err_msg);
599                 return MEDIA_SVC_PLUGIN_ERROR;
600         }
601
602         return MEDIA_SVC_PLUGIN_ERROR_NONE;
603 }
604
605 int update_folder_time(void *handle, const char *storage_id, char *folder_path, uid_t uid, char **err_msg)
606 {
607         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
608
609         if (handle == NULL) {
610                 __set_error_message(ERR_HANDLE, err_msg);
611                 return MEDIA_SVC_PLUGIN_ERROR;
612         }
613
614         if (folder_path == NULL) {
615                 __set_error_message(ERR_HANDLE, err_msg);
616                 return MEDIA_SVC_PLUGIN_ERROR;
617         }
618
619         ret = media_svc_update_folder_time(handle, storage_id, folder_path, uid);
620         if (ret < 0) {
621                 __set_error_message(ret, err_msg);
622                 return MEDIA_SVC_PLUGIN_ERROR;
623         }
624
625         return MEDIA_SVC_PLUGIN_ERROR_NONE;
626 }
627
628 int get_uuid(void * handle, char **uuid, char **err_msg)
629 {
630         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
631
632         if (handle == NULL) {
633                 __set_error_message(ERR_HANDLE, err_msg);
634                 return MEDIA_SVC_PLUGIN_ERROR;
635         }
636
637         ret = media_svc_generate_uuid(uuid);
638         if (ret < 0) {
639                 __set_error_message(ret, err_msg);
640                 return MEDIA_SVC_PLUGIN_ERROR;
641         }
642
643         return MEDIA_SVC_PLUGIN_ERROR_NONE;
644 }
645
646 int get_mmc_info(void * handle, char **storage_name, char **storage_path, int *validity, bool *info_exist, char **err_msg)
647 {
648         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
649
650         if (handle == NULL) {
651                 __set_error_message(ERR_HANDLE, err_msg);
652                 return MEDIA_SVC_PLUGIN_ERROR;
653         }
654
655         ret = media_svc_get_mmc_info(handle, storage_name, storage_path, validity, info_exist);
656         if (ret < 0) {
657                 __set_error_message(MS_MEDIA_ERR_DB_NO_RECORD, err_msg);
658                 return MEDIA_SVC_PLUGIN_ERROR;
659         }
660
661         return MEDIA_SVC_PLUGIN_ERROR_NONE;
662 }
663
664 int check_storage(void * handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity, char **err_msg)
665 {
666         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
667
668         if (handle == NULL) {
669                 __set_error_message(ERR_HANDLE, err_msg);
670                 return MEDIA_SVC_PLUGIN_ERROR;
671         }
672
673         ret = media_svc_check_storage(handle, storage_id, storage_name, storage_path, validity);
674         if (ret < 0) {
675                 __set_error_message(ret, err_msg);
676                 return MEDIA_SVC_PLUGIN_ERROR;
677         }
678
679         return MEDIA_SVC_PLUGIN_ERROR_NONE;
680 }
681
682 int insert_storage(void *handle, const char *storage_id, int storage_type, const char *storage_name, const char *storage_path, uid_t uid, char **err_msg)
683 {
684         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
685
686         if (handle == NULL) {
687                 __set_error_message(ERR_HANDLE, err_msg);
688                 return MEDIA_SVC_PLUGIN_ERROR;
689         }
690
691         ret = media_svc_insert_storage(handle, storage_id, storage_name, storage_path, NULL, storage_type, uid);
692         if (ret < 0) {
693                 __set_error_message(ret, err_msg);
694                 return MEDIA_SVC_PLUGIN_ERROR;
695         }
696
697         return MEDIA_SVC_PLUGIN_ERROR_NONE;
698 }
699
700 int update_storage(void *handle, const char *storage_id, const char *storage_path, uid_t uid, char **err_msg)
701 {
702         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
703
704         if (handle == NULL) {
705                 __set_error_message(ERR_HANDLE, err_msg);
706                 return MEDIA_SVC_PLUGIN_ERROR;
707         }
708
709         ret = media_svc_update_storage(handle, storage_id, storage_path, uid);
710         if (ret < 0) {
711                 __set_error_message(ret, err_msg);
712                 return MEDIA_SVC_PLUGIN_ERROR;
713         }
714
715         return MEDIA_SVC_PLUGIN_ERROR_NONE;
716 }
717
718 int delete_storage(void * handle, const char *storage_id, const char *storage_name, uid_t uid, char **err_msg)
719 {
720         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
721
722         if (handle == NULL) {
723                 __set_error_message(ERR_HANDLE, err_msg);
724                 return MEDIA_SVC_PLUGIN_ERROR;
725         }
726
727         ret = media_svc_delete_storage(handle, storage_id, storage_name, uid);
728         if (ret < 0) {
729                 __set_error_message(ret, err_msg);
730                 return MEDIA_SVC_PLUGIN_ERROR;
731         }
732
733         return MEDIA_SVC_PLUGIN_ERROR_NONE;
734 }
735
736 int set_storage_validity(void * handle, const char *storage_id, int validity, uid_t uid, char **err_msg)
737 {
738         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
739
740         if (handle == NULL) {
741                 __set_error_message(ERR_HANDLE, err_msg);
742                 return MEDIA_SVC_PLUGIN_ERROR;
743         }
744
745         ret = media_svc_set_storage_validity(handle, storage_id, validity, uid);
746         if (ret < 0) {
747                 __set_error_message(ret, err_msg);
748                 return MEDIA_SVC_PLUGIN_ERROR;
749         }
750
751         return MEDIA_SVC_PLUGIN_ERROR_NONE;
752 }
753
754 int set_all_storage_validity(void * handle, int validity, char **err_msg, uid_t uid)
755 {
756         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
757
758         if (handle == NULL) {
759                 __set_error_message(ERR_HANDLE, err_msg);
760                 return MEDIA_SVC_PLUGIN_ERROR;
761         }
762
763         ret = media_svc_set_storage_validity(handle, NULL, validity, uid);
764         if (ret < 0) {
765                 __set_error_message(ret, err_msg);
766                 return MEDIA_SVC_PLUGIN_ERROR;
767         }
768
769         return MEDIA_SVC_PLUGIN_ERROR_NONE;
770 }
771
772 int get_storage_id(void * handle, const char *path, char *storage_id, char **err_msg)
773 {
774         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
775
776         if (handle == NULL) {
777                 __set_error_message(ERR_HANDLE, err_msg);
778                 return MEDIA_SVC_PLUGIN_ERROR;
779         }
780
781         ret = media_svc_get_storage_id(handle, path, storage_id);
782         if (ret < 0) {
783                 __set_error_message(ret, err_msg);
784                 return MEDIA_SVC_PLUGIN_ERROR;
785         }
786
787         return MEDIA_SVC_PLUGIN_ERROR_NONE;
788 }
789
790 int get_storage_scan_status(void * handle, const char *storage_id, int *status, char **err_msg)
791 {
792         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
793         media_svc_scan_status_type_e storage_status = 0;
794
795         if (handle == NULL) {
796                 __set_error_message(ERR_HANDLE, err_msg);
797                 return MEDIA_SVC_PLUGIN_ERROR;
798         }
799
800         ret = media_svc_get_storage_scan_status(handle, storage_id, &storage_status);
801         if (ret < 0) {
802                 __set_error_message(ret, err_msg);
803                 return MEDIA_SVC_PLUGIN_ERROR;
804         }
805
806         *status = storage_status;
807
808         return MEDIA_SVC_PLUGIN_ERROR_NONE;
809 }
810
811 int set_storage_scan_status(void *handle, const char *storage_id, int status, uid_t uid, char **err_msg)
812 {
813         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
814         media_svc_scan_status_type_e storage_status = status;
815
816         if (handle == NULL) {
817                 __set_error_message(ERR_HANDLE, err_msg);
818                 return MEDIA_SVC_PLUGIN_ERROR;
819         }
820
821         ret = media_svc_set_storage_scan_status(handle, storage_id, storage_status, uid);
822         if (ret < 0) {
823                 __set_error_message(ret, err_msg);
824                 return MEDIA_SVC_PLUGIN_ERROR;
825         }
826
827         return MEDIA_SVC_PLUGIN_ERROR_NONE;
828 }
829
830 int get_storage_list(void *handle, char ***storage_list, char ***storage_id_list,int **scan_status_list, int *count, char **err_msg)
831 {
832         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
833
834         if (handle == NULL) {
835                 __set_error_message(ERR_HANDLE, err_msg);
836                 return MEDIA_SVC_PLUGIN_ERROR;
837         }
838
839         if (count == NULL) {
840                 __set_error_message(ERR_HANDLE, err_msg);
841                 return MEDIA_SVC_PLUGIN_ERROR;
842         }
843
844         ret = media_svc_get_storage_list(handle, storage_list, storage_id_list, scan_status_list, count);
845         if (ret < 0) {
846                 __set_error_message(ret, err_msg);
847                 return MEDIA_SVC_PLUGIN_ERROR;
848         }
849
850         return MEDIA_SVC_PLUGIN_ERROR_NONE;
851 }
852
853 int update_item_begin(void *handle, int item_cnt, char **err_msg)
854 {
855         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
856
857         if (handle == NULL) {
858                 __set_error_message(ERR_HANDLE, err_msg);
859                 return MEDIA_SVC_PLUGIN_ERROR;
860         }
861
862         ret = media_svc_update_item_begin(handle, item_cnt);
863         if (ret < 0) {
864                 __set_error_message(ret, err_msg);
865                 return MEDIA_SVC_PLUGIN_ERROR;
866         }
867
868         return MEDIA_SVC_PLUGIN_ERROR_NONE;
869 }
870
871 int update_item_end(void *handle, uid_t uid, char **err_msg)
872 {
873         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
874
875         if (handle == NULL) {
876                 __set_error_message(ERR_HANDLE, err_msg);
877                 return MEDIA_SVC_PLUGIN_ERROR;
878         }
879
880         ret = media_svc_update_item_end(handle, uid);
881         if (ret < 0) {
882                 __set_error_message(ret, err_msg);
883                 return MEDIA_SVC_PLUGIN_ERROR;
884         }
885
886         return MEDIA_SVC_PLUGIN_ERROR_NONE;
887 }
888
889 int update_item_meta(void *handle, const char *file_path, int storage_type, uid_t uid, char **err_msg)
890 {
891         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
892
893         if (handle == NULL) {
894                 __set_error_message(ERR_HANDLE, err_msg);
895                 return MEDIA_SVC_PLUGIN_ERROR;
896         }
897
898         if (file_path == NULL) {
899                 __set_error_message(ERR_HANDLE, err_msg);
900                 return MEDIA_SVC_PLUGIN_ERROR;
901         }
902
903         ret = media_svc_update_item_meta(handle, file_path, storage_type, uid);
904         if (ret < 0) {
905                 __set_error_message(ret, err_msg);
906                 return MEDIA_SVC_PLUGIN_ERROR;
907         }
908
909         return MEDIA_SVC_PLUGIN_ERROR_NONE;
910 }
911
912 int insert_item_scan(void * handle, const char *storage_id, const char *file_path, int storage_type, uid_t uid, char **err_msg)
913 {
914         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
915
916         if (handle == NULL) {
917                 __set_error_message(ERR_HANDLE, err_msg);
918                 return MEDIA_SVC_PLUGIN_ERROR;
919         }
920
921         if (!STRING_VALID(file_path)) {
922                 __set_error_message(ERR_FILE_PATH, err_msg);
923                 return MEDIA_SVC_PLUGIN_ERROR;
924         }
925
926         if (!STORAGE_VALID(storage_type)) {
927                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
928                 return MEDIA_SVC_PLUGIN_ERROR;
929         }
930
931         ret = media_svc_insert_item_pass1(handle, storage_id, storage_type, file_path, FALSE, uid);
932         if (ret < 0) {
933                 __set_error_message(ret, err_msg);
934                 return MEDIA_SVC_PLUGIN_ERROR;
935         }
936
937         return MEDIA_SVC_PLUGIN_ERROR_NONE;
938 }
939
940 int update_item_extract(void * handle, const char *storage_id, int storage_type, int scan_type, uid_t uid, const char *path, char **err_msg)
941 {
942         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
943
944         if (handle == NULL) {
945                 __set_error_message(ERR_HANDLE, err_msg);
946                 return MEDIA_SVC_PLUGIN_ERROR;
947         }
948
949         if (!STORAGE_VALID(storage_type)) {
950                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
951                 return MEDIA_SVC_PLUGIN_ERROR;
952         }
953
954         ret = media_svc_insert_item_pass2(handle, storage_id, storage_type, scan_type, path, FALSE, uid);
955         if (ret < 0) {
956                 __set_error_message(ret, err_msg);
957                 return MEDIA_SVC_PLUGIN_ERROR;
958         }
959
960         return MEDIA_SVC_PLUGIN_ERROR_NONE;
961 }
962
963 int insert_folder_begin(void * handle, int item_cnt, char **err_msg)
964 {
965         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
966
967         if (handle == NULL) {
968                 __set_error_message(ERR_HANDLE, err_msg);
969                 return MEDIA_SVC_PLUGIN_ERROR;
970         }
971
972         ret = media_svc_insert_folder_begin(handle, item_cnt);
973         if (ret < 0) {
974                 __set_error_message(ret, err_msg);
975                 return MEDIA_SVC_PLUGIN_ERROR;
976         }
977
978         return MEDIA_SVC_PLUGIN_ERROR_NONE;
979 }
980
981 int insert_folder_end(void *handle, uid_t uid, char **err_msg)
982 {
983         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
984
985         if (handle == NULL) {
986                 __set_error_message(ERR_HANDLE, err_msg);
987                 return MEDIA_SVC_PLUGIN_ERROR;
988         }
989
990         ret = media_svc_insert_folder_end(handle, uid);
991         if (ret < 0) {
992                 __set_error_message(ret, err_msg);
993                 return MEDIA_SVC_PLUGIN_ERROR;
994         }
995
996         return MEDIA_SVC_PLUGIN_ERROR_NONE;
997 }
998
999 int insert_folder(void * handle, const char *storage_id, const char *file_path, int storage_type, uid_t uid, char **err_msg)
1000 {
1001         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
1002
1003         if (handle == NULL) {
1004                 __set_error_message(ERR_HANDLE, err_msg);
1005                 return MEDIA_SVC_PLUGIN_ERROR;
1006         }
1007
1008         if (!STRING_VALID(file_path)) {
1009                 __set_error_message(ERR_FILE_PATH, err_msg);
1010                 return MEDIA_SVC_PLUGIN_ERROR;
1011         }
1012
1013         if (!STORAGE_VALID(storage_type)) {
1014                 __set_error_message(ERR_STORAGE_TYPE, err_msg);
1015                 return MEDIA_SVC_PLUGIN_ERROR;
1016         }
1017
1018         ret = media_svc_insert_folder(handle, storage_id, storage_type, file_path, uid);
1019         if (ret < 0) {
1020                 __set_error_message(ret, err_msg);
1021                 return MEDIA_SVC_PLUGIN_ERROR;
1022         }
1023
1024         return MEDIA_SVC_PLUGIN_ERROR_NONE;
1025 }
1026
1027 int delete_invalid_folder(void * handle, const char *storage_id, uid_t uid, char **err_msg)
1028 {
1029         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
1030
1031         if (handle == NULL) {
1032                 __set_error_message(ERR_HANDLE, err_msg);
1033                 return MEDIA_SVC_PLUGIN_ERROR;
1034         }
1035
1036         ret = media_svc_delete_invalid_folder(handle, storage_id, uid);
1037         if (ret < 0) {
1038                 __set_error_message(ret, err_msg);
1039                 return MEDIA_SVC_PLUGIN_ERROR;
1040         }
1041
1042         return MEDIA_SVC_PLUGIN_ERROR_NONE;
1043 }
1044
1045 int set_folder_validity(void * handle, const char *storage_id, const char* start_path, int validity, bool is_recursive, uid_t uid, char **err_msg)
1046 {
1047         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
1048
1049         if (handle == NULL) {
1050                 __set_error_message(ERR_HANDLE, err_msg);
1051                 return MEDIA_SVC_PLUGIN_ERROR;
1052         }
1053
1054         ret = media_svc_set_folder_validity(handle, storage_id, start_path, validity, is_recursive, uid);
1055         if (ret < 0) {
1056                 __set_error_message(ret, err_msg);
1057                 return MEDIA_SVC_PLUGIN_ERROR;
1058         }
1059
1060         return MEDIA_SVC_PLUGIN_ERROR_NONE;
1061 }
1062
1063 int delete_invalid_folder_by_path(void * handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count, char **err_msg)
1064 {
1065         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
1066
1067         if (handle == NULL) {
1068                 __set_error_message(ERR_HANDLE, err_msg);
1069                 return MEDIA_SVC_PLUGIN_ERROR;
1070         }
1071
1072         ret = media_svc_delete_invalid_folder_by_path(handle, storage_id, folder_path, uid, delete_count);
1073         if (ret < 0) {
1074                 __set_error_message(ret, err_msg);
1075                 return MEDIA_SVC_PLUGIN_ERROR;
1076         }
1077
1078         return MEDIA_SVC_PLUGIN_ERROR_NONE;
1079 }
1080
1081 int check_folder_exist(void * handle, const char *storage_id, const char *folder_path, char **err_msg)
1082 {
1083         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
1084
1085         if (handle == NULL) {
1086                 __set_error_message(ERR_HANDLE, err_msg);
1087                 return MEDIA_SVC_PLUGIN_ERROR;
1088         }
1089
1090         ret = media_svc_check_folder_exist_by_path(handle, storage_id, folder_path);
1091         if (ret < 0) {
1092                 __set_error_message(ret, err_msg);
1093                 return MEDIA_SVC_PLUGIN_ERROR;
1094         }
1095
1096         return MEDIA_SVC_PLUGIN_ERROR_NONE;
1097 }
1098
1099 int count_subfolder(void * handle, const char *storage_id, const char *folder_path, int *count, char **err_msg)
1100 {
1101         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
1102         int cnt = 0;
1103
1104         if (handle == NULL) {
1105                 __set_error_message(ERR_HANDLE, err_msg);
1106                 return MEDIA_SVC_PLUGIN_ERROR;
1107         }
1108
1109         ret = media_svc_check_subfolder_by_path(handle, storage_id, folder_path, &cnt);
1110         if (ret < 0) {
1111                 __set_error_message(ret, err_msg);
1112                 return MEDIA_SVC_PLUGIN_ERROR;
1113         }
1114
1115         *count = cnt;
1116
1117         return MEDIA_SVC_PLUGIN_ERROR_NONE;
1118 }
1119
1120 int get_folder_id(void * handle, const char *storage_id, const char *path, char *folder_id, char **err_msg)
1121 {
1122         int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
1123
1124         if (handle == NULL) {
1125                 __set_error_message(ERR_HANDLE, err_msg);
1126                 return MEDIA_SVC_PLUGIN_ERROR;
1127         }
1128
1129         ret = media_svc_get_folder_id(handle, storage_id, path, folder_id);
1130         if (ret < 0) {
1131                 __set_error_message(ret, err_msg);
1132                 return MEDIA_SVC_PLUGIN_ERROR;
1133         }
1134
1135         return MEDIA_SVC_PLUGIN_ERROR_NONE;
1136 }
1137
1138
1139