4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <media-thumbnail.h>
26 #include "media-svc.h"
27 #include "media-svc-util.h"
29 #define MEDIA_SVC_PLUGIN_ERROR_NONE 0
30 #define MEDIA_SVC_PLUGIN_ERROR -1
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)) ? TRUE : FALSE)
47 } media_svc_error_type_e;
49 static void __set_error_message(int err_type, char **err_msg);
51 static void __set_error_message(int err_type, char **err_msg)
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");
87 *err_msg = strdup("error unknown");
92 int connect_db(void **handle, uid_t uid, char **err_msg)
94 int ret = media_svc_connect(handle, uid);
97 __set_error_message(ret, err_msg);
98 return MEDIA_SVC_PLUGIN_ERROR;
101 return MEDIA_SVC_PLUGIN_ERROR_NONE;
104 int disconnect_db(void *handle, char **err_msg)
106 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
108 if (handle == NULL) {
109 __set_error_message(ERR_HANDLE, err_msg);
110 return MEDIA_SVC_PLUGIN_ERROR;
113 ret = media_svc_disconnect(handle);
115 __set_error_message(ret, err_msg);
116 return MEDIA_SVC_PLUGIN_ERROR;
119 return MEDIA_SVC_PLUGIN_ERROR_NONE;
122 int check_item_exist(void *handle, const char *file_path, bool *modified, char **err_msg)
124 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
127 if (handle == NULL) {
128 __set_error_message(ERR_HANDLE, err_msg);
129 return MEDIA_SVC_PLUGIN_ERROR;
132 if (!STRING_VALID(file_path)) {
133 __set_error_message(ERR_FILE_PATH, err_msg);
134 return MEDIA_SVC_PLUGIN_ERROR;
137 ret = media_svc_check_item_exist_by_path(handle, file_path);
138 if (ret == MS_MEDIA_ERR_NONE) {
139 time_t modified_time = 0;
140 unsigned long long file_size = 0;
143 ret = media_svc_get_file_info(handle, file_path, &modified_time, &file_size);
144 if (ret == MS_MEDIA_ERR_NONE) {
145 memset(&st, 0, sizeof(struct stat));
146 if (stat(file_path, &st) == 0) {
147 if ((st.st_mtime != modified_time) || (st.st_size != file_size))
154 return MEDIA_SVC_PLUGIN_ERROR_NONE; /*exist */
157 __set_error_message(ERR_CHECK_ITEM, err_msg);
159 return MEDIA_SVC_PLUGIN_ERROR; /*not exist */
162 int insert_item_begin(void *handle, int item_cnt, int with_noti, int from_pid, char **err_msg)
164 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
166 if (handle == NULL) {
167 __set_error_message(ERR_HANDLE, err_msg);
168 return MEDIA_SVC_PLUGIN_ERROR;
171 ret = media_svc_insert_item_begin(handle, item_cnt, with_noti, from_pid);
173 __set_error_message(ret, err_msg);
174 return MEDIA_SVC_PLUGIN_ERROR;
177 return MEDIA_SVC_PLUGIN_ERROR_NONE;
180 int insert_item_end(void *handle, uid_t uid, char **err_msg)
182 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
184 if (handle == NULL) {
185 __set_error_message(ERR_HANDLE, err_msg);
186 return MEDIA_SVC_PLUGIN_ERROR;
189 ret = media_svc_insert_item_end(handle, uid);
191 __set_error_message(ret, err_msg);
192 return MEDIA_SVC_PLUGIN_ERROR;
195 return MEDIA_SVC_PLUGIN_ERROR_NONE;
198 int insert_item(void *handle, const char *file_path, int storage_type, uid_t uid, char **err_msg)
200 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
202 if (handle == NULL) {
203 __set_error_message(ERR_HANDLE, err_msg);
204 return MEDIA_SVC_PLUGIN_ERROR;
207 if (!STRING_VALID(file_path)) {
208 __set_error_message(ERR_FILE_PATH, err_msg);
209 return MEDIA_SVC_PLUGIN_ERROR;
212 if (!STORAGE_VALID(storage_type)) {
213 __set_error_message(ERR_STORAGE_TYPE, err_msg);
214 return MEDIA_SVC_PLUGIN_ERROR;
217 ret = media_svc_insert_item_bulk(handle, storage_type, file_path, FALSE, uid);
219 __set_error_message(ret, err_msg);
220 return MEDIA_SVC_PLUGIN_ERROR;
223 return MEDIA_SVC_PLUGIN_ERROR_NONE;
226 int insert_item_immediately(void *handle, const char *file_path, int storage_type, uid_t uid, char **err_msg)
228 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
230 if (handle == NULL) {
231 __set_error_message(ERR_HANDLE, err_msg);
232 return MEDIA_SVC_PLUGIN_ERROR;
235 if (!STRING_VALID(file_path)) {
236 __set_error_message(ERR_FILE_PATH, err_msg);
237 return MEDIA_SVC_PLUGIN_ERROR;
240 if (!STORAGE_VALID(storage_type)) {
241 __set_error_message(ERR_STORAGE_TYPE, err_msg);
242 return MEDIA_SVC_PLUGIN_ERROR;
245 ret = media_svc_insert_item_immediately(handle, storage_type, file_path, uid);
247 __set_error_message(ret, err_msg);
248 return MEDIA_SVC_PLUGIN_ERROR;
251 return MEDIA_SVC_PLUGIN_ERROR_NONE;
254 int insert_burst_item(void *handle, const char *file_path, int storage_type, uid_t uid, char **err_msg)
256 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
258 if (handle == NULL) {
259 __set_error_message(ERR_HANDLE, err_msg);
260 return MEDIA_SVC_PLUGIN_ERROR;
263 if (!STRING_VALID(file_path)) {
264 __set_error_message(ERR_FILE_PATH, err_msg);
265 return MEDIA_SVC_PLUGIN_ERROR;
268 if (!STORAGE_VALID(storage_type)) {
269 __set_error_message(ERR_STORAGE_TYPE, err_msg);
270 return MEDIA_SVC_PLUGIN_ERROR;
273 ret = media_svc_insert_item_bulk(handle, storage_type, file_path, TRUE, uid);
275 __set_error_message(ret, err_msg);
276 return MEDIA_SVC_PLUGIN_ERROR;
279 return MEDIA_SVC_PLUGIN_ERROR_NONE;
282 int set_all_storage_items_validity(void *handle, int storage_type, int validity, uid_t uid, char **err_msg)
284 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
286 if (handle == NULL) {
287 __set_error_message(ERR_HANDLE, err_msg);
288 return MEDIA_SVC_PLUGIN_ERROR;
291 if (!STORAGE_VALID(storage_type)) {
292 __set_error_message(ERR_STORAGE_TYPE, err_msg);
293 return MEDIA_SVC_PLUGIN_ERROR;
296 ret = media_svc_set_all_storage_items_validity(handle, storage_type, validity, uid);
298 __set_error_message(ret, err_msg);
299 return MEDIA_SVC_PLUGIN_ERROR;
302 return MEDIA_SVC_PLUGIN_ERROR_NONE;
305 int set_folder_item_validity(void *handle, const char *folder_path, int validity, int recursive, uid_t uid, char **err_msg)
307 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
309 if (handle == NULL) {
310 __set_error_message(ERR_HANDLE, err_msg);
311 return MEDIA_SVC_PLUGIN_ERROR;
314 if (!STRING_VALID(folder_path)) {
315 __set_error_message(ERR_FOLDER_PATH, err_msg);
316 return MEDIA_SVC_PLUGIN_ERROR;
319 ret = media_svc_set_folder_items_validity(handle, folder_path, validity, recursive, uid);
321 __set_error_message(ret, err_msg);
322 return MEDIA_SVC_PLUGIN_ERROR;
325 return MEDIA_SVC_PLUGIN_ERROR_NONE;
328 int set_item_validity_begin(void *handle, int item_cnt, char **err_msg)
330 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
332 if (handle == NULL) {
333 __set_error_message(ERR_HANDLE, err_msg);
334 return MEDIA_SVC_PLUGIN_ERROR;
337 ret = media_svc_set_item_validity_begin(handle, item_cnt);
339 __set_error_message(ret, err_msg);
340 return MEDIA_SVC_PLUGIN_ERROR;
343 return MEDIA_SVC_PLUGIN_ERROR_NONE;
346 int set_item_validity_end(void *handle, uid_t uid, char **err_msg)
348 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
350 if (handle == NULL) {
351 __set_error_message(ERR_HANDLE, err_msg);
352 return MEDIA_SVC_PLUGIN_ERROR;
355 ret = media_svc_set_item_validity_end(handle, uid);
357 __set_error_message(ret, err_msg);
358 return MEDIA_SVC_PLUGIN_ERROR;
361 return MEDIA_SVC_PLUGIN_ERROR_NONE;
364 int set_item_validity(void *handle, const char *file_path, int storage_type, int validity, uid_t uid, char **err_msg)
366 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
368 if (handle == NULL) {
369 __set_error_message(ERR_HANDLE, err_msg);
370 return MEDIA_SVC_PLUGIN_ERROR;
373 if (!STRING_VALID(file_path)) {
374 __set_error_message(ERR_FILE_PATH, err_msg);
375 return MEDIA_SVC_PLUGIN_ERROR;
378 if (!STORAGE_VALID(storage_type)) {
379 __set_error_message(ERR_STORAGE_TYPE, err_msg);
380 return MEDIA_SVC_PLUGIN_ERROR;
383 ret = media_svc_set_item_validity(handle, file_path, validity, uid);
386 __set_error_message(ret, err_msg);
387 return MEDIA_SVC_PLUGIN_ERROR;
390 return MEDIA_SVC_PLUGIN_ERROR_NONE;
393 int delete_item(void *handle, const char *file_path, uid_t uid, char **err_msg)
395 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
397 if (handle == NULL) {
398 __set_error_message(ERR_HANDLE, err_msg);
399 return MEDIA_SVC_PLUGIN_ERROR;
402 if (!STRING_VALID(file_path)) {
403 __set_error_message(ERR_FILE_PATH, err_msg);
404 return MEDIA_SVC_PLUGIN_ERROR;
407 ret = media_svc_check_item_exist_by_path(handle, file_path);
409 ret = media_svc_delete_item_by_path(handle, "media", file_path, uid);
412 __set_error_message(ret, err_msg);
413 return MEDIA_SVC_PLUGIN_ERROR;
415 return MEDIA_SVC_PLUGIN_ERROR_NONE;
418 __set_error_message(ERR_CHECK_ITEM, err_msg); /*not exist in DB so can't delete item. */
419 return MEDIA_SVC_PLUGIN_ERROR;
422 int delete_all_items_in_storage(void *handle, int storage_type, uid_t uid, char **err_msg)
424 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
426 if (handle == NULL) {
427 __set_error_message(ERR_HANDLE, err_msg);
428 return MEDIA_SVC_PLUGIN_ERROR;
431 if (!STORAGE_VALID(storage_type)) {
432 __set_error_message(ERR_STORAGE_TYPE, err_msg);
433 return MEDIA_SVC_PLUGIN_ERROR;
436 ret = media_svc_delete_all_items_in_storage(handle, storage_type, uid);
438 __set_error_message(ret, err_msg);
439 return MEDIA_SVC_PLUGIN_ERROR;
442 return MEDIA_SVC_PLUGIN_ERROR_NONE;
445 int delete_all_invalid_items_in_storage(void *handle, int storage_type, uid_t uid, char **err_msg)
447 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
449 if (handle == NULL) {
450 __set_error_message(ERR_HANDLE, err_msg);
451 return MEDIA_SVC_PLUGIN_ERROR;
454 if (!STORAGE_VALID(storage_type)) {
455 __set_error_message(ERR_STORAGE_TYPE, err_msg);
456 return MEDIA_SVC_PLUGIN_ERROR;
459 ret = media_svc_delete_invalid_items_in_storage(handle, storage_type, uid);
461 __set_error_message(ret, err_msg);
462 return MEDIA_SVC_PLUGIN_ERROR;
465 return MEDIA_SVC_PLUGIN_ERROR_NONE;
468 int delete_all_invalid_items_in_folder(void *handle, const char *folder_path, uid_t uid, char **err_msg)
470 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
472 if (handle == NULL) {
473 __set_error_message(ERR_HANDLE, err_msg);
474 return MEDIA_SVC_PLUGIN_ERROR;
477 if (!STRING_VALID(folder_path)) {
478 __set_error_message(ERR_FOLDER_PATH, err_msg);
479 return MEDIA_SVC_PLUGIN_ERROR;
482 ret = media_svc_delete_invalid_items_in_folder(handle, folder_path, uid);
484 __set_error_message(ret, err_msg);
485 return MEDIA_SVC_PLUGIN_ERROR;
488 return MEDIA_SVC_PLUGIN_ERROR_NONE;
492 int update_begin(void)
494 return MEDIA_SVC_PLUGIN_ERROR_NONE;
497 int update_end(uid_t uid)
499 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
501 ret = thumbnail_request_extract_all_thumbs(uid);
503 return MEDIA_SVC_PLUGIN_ERROR;
506 return MEDIA_SVC_PLUGIN_ERROR_NONE;
509 int send_dir_update_noti(void *handle, const char *dir_path, char **err_msg)
511 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
513 if (!STRING_VALID(dir_path)) {
514 __set_error_message(ERR_FOLDER_PATH, err_msg);
515 return MEDIA_SVC_PLUGIN_ERROR;
518 ret = media_svc_send_dir_update_noti(handle, dir_path);
520 __set_error_message(ret, err_msg);
521 return MEDIA_SVC_PLUGIN_ERROR;
524 return MEDIA_SVC_PLUGIN_ERROR_NONE;
527 int count_delete_items_in_folder(void *handle, const char *folder_path, int *count, char **err_msg)
529 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
531 if (handle == NULL) {
532 __set_error_message(ERR_HANDLE, err_msg);
533 return MEDIA_SVC_PLUGIN_ERROR;
537 __set_error_message(ERR_HANDLE, err_msg);
538 return MEDIA_SVC_PLUGIN_ERROR;
541 if (!STRING_VALID(folder_path)) {
542 __set_error_message(ERR_FOLDER_PATH, err_msg);
543 return MEDIA_SVC_PLUGIN_ERROR;
546 ret = media_svc_count_invalid_items_in_folder(handle, folder_path, count);
548 __set_error_message(ret, err_msg);
549 return MEDIA_SVC_PLUGIN_ERROR;
552 return MEDIA_SVC_PLUGIN_ERROR_NONE;
555 int check_db(void *handle, uid_t uid, char **err_msg)
557 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
560 ret = media_svc_create_table(handle, uid);
562 __set_error_message(ret, err_msg);
563 return MEDIA_SVC_PLUGIN_ERROR;
567 ret = media_svc_check_db_upgrade(handle, uid);
569 __set_error_message(ret, err_msg);
570 return MEDIA_SVC_PLUGIN_ERROR;
573 return MEDIA_SVC_PLUGIN_ERROR_NONE;
576 int check_db_corrupt(void *handle, char **err_msg)
578 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
581 ret = media_svc_check_db_corrupt(handle);
583 __set_error_message(ret, err_msg);
584 return MEDIA_SVC_PLUGIN_ERROR;
587 return MEDIA_SVC_PLUGIN_ERROR_NONE;
590 int get_folder_list(void *handle, char *start_path, char ***folder_list, int **modified_time_list, int **item_num_list, int *count, char **err_msg)
592 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
594 if (handle == NULL) {
595 __set_error_message(ERR_HANDLE, err_msg);
596 return MEDIA_SVC_PLUGIN_ERROR;
600 __set_error_message(ERR_HANDLE, err_msg);
601 return MEDIA_SVC_PLUGIN_ERROR;
604 ret = media_svc_get_folder_list(handle, start_path, folder_list, (time_t **)modified_time_list, item_num_list, count);
606 __set_error_message(ret, err_msg);
607 return MEDIA_SVC_PLUGIN_ERROR;
610 return MEDIA_SVC_PLUGIN_ERROR_NONE;
613 int update_folder_time(void *handle, char *folder_path, uid_t uid, char **err_msg)
615 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
617 if (handle == NULL) {
618 __set_error_message(ERR_HANDLE, err_msg);
619 return MEDIA_SVC_PLUGIN_ERROR;
622 if (folder_path == NULL) {
623 __set_error_message(ERR_HANDLE, err_msg);
624 return MEDIA_SVC_PLUGIN_ERROR;
627 ret = media_svc_update_folder_time(handle, folder_path, uid);
629 __set_error_message(ret, err_msg);
630 return MEDIA_SVC_PLUGIN_ERROR;
633 return MEDIA_SVC_PLUGIN_ERROR_NONE;
636 int update_item_begin(void *handle, int item_cnt, char **err_msg)
638 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
640 if (handle == NULL) {
641 __set_error_message(ERR_HANDLE, err_msg);
642 return MEDIA_SVC_PLUGIN_ERROR;
645 ret = media_svc_update_item_begin(handle, item_cnt);
647 __set_error_message(ret, err_msg);
648 return MEDIA_SVC_PLUGIN_ERROR;
651 return MEDIA_SVC_PLUGIN_ERROR_NONE;
654 int update_item_end(void *handle, uid_t uid, char **err_msg)
656 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
658 if (handle == NULL) {
659 __set_error_message(ERR_HANDLE, err_msg);
660 return MEDIA_SVC_PLUGIN_ERROR;
663 ret = media_svc_update_item_end(handle, uid);
665 __set_error_message(ret, err_msg);
666 return MEDIA_SVC_PLUGIN_ERROR;
669 return MEDIA_SVC_PLUGIN_ERROR_NONE;
672 int update_item_meta(void *handle, const char *file_path, int storage_type, uid_t uid, char **err_msg)
674 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
676 if (handle == NULL) {
677 __set_error_message(ERR_HANDLE, err_msg);
678 return MEDIA_SVC_PLUGIN_ERROR;
681 if (file_path == NULL) {
682 __set_error_message(ERR_HANDLE, err_msg);
683 return MEDIA_SVC_PLUGIN_ERROR;
686 ret = media_svc_update_item_meta(handle, file_path, storage_type, uid);
688 __set_error_message(ret, err_msg);
689 return MEDIA_SVC_PLUGIN_ERROR;
692 return MEDIA_SVC_PLUGIN_ERROR_NONE;