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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
416 return MEDIA_SVC_PLUGIN_ERROR_NONE;
419 __set_error_message(ERR_CHECK_ITEM, err_msg); //not exist in DB so can't delete item.
420 return MEDIA_SVC_PLUGIN_ERROR;
423 int delete_all_items_in_storage(void * handle, int storage_type, uid_t uid, char ** err_msg)
425 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
428 __set_error_message(ERR_HANDLE, err_msg);
429 return MEDIA_SVC_PLUGIN_ERROR;
432 if(!STORAGE_VALID(storage_type)) {
433 __set_error_message(ERR_STORAGE_TYPE, err_msg);
434 return MEDIA_SVC_PLUGIN_ERROR;
437 ret = media_svc_delete_all_items_in_storage(handle, storage_type, uid);
439 __set_error_message(ret, err_msg);
440 return MEDIA_SVC_PLUGIN_ERROR;
443 return MEDIA_SVC_PLUGIN_ERROR_NONE;
446 int delete_all_invalid_items_in_storage(void * handle, int storage_type, uid_t uid, char ** err_msg)
448 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
451 __set_error_message(ERR_HANDLE, err_msg);
452 return MEDIA_SVC_PLUGIN_ERROR;
455 if(!STORAGE_VALID(storage_type)) {
456 __set_error_message(ERR_STORAGE_TYPE, err_msg);
457 return MEDIA_SVC_PLUGIN_ERROR;
460 ret = media_svc_delete_invalid_items_in_storage(handle, storage_type, uid);
462 __set_error_message(ret, err_msg);
463 return MEDIA_SVC_PLUGIN_ERROR;
466 return MEDIA_SVC_PLUGIN_ERROR_NONE;
469 int delete_all_invalid_items_in_folder(void * handle, const char *folder_path, uid_t uid, char ** err_msg)
471 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
474 __set_error_message(ERR_HANDLE, err_msg);
475 return MEDIA_SVC_PLUGIN_ERROR;
478 if (!STRING_VALID(folder_path)) {
479 __set_error_message(ERR_FOLDER_PATH, err_msg);
480 return MEDIA_SVC_PLUGIN_ERROR;
483 ret = media_svc_delete_invalid_items_in_folder(handle, folder_path, uid);
485 __set_error_message(ret, err_msg);
486 return MEDIA_SVC_PLUGIN_ERROR;
489 return MEDIA_SVC_PLUGIN_ERROR_NONE;
493 int update_begin(void)
495 return MEDIA_SVC_PLUGIN_ERROR_NONE;
498 int update_end(uid_t uid)
500 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
502 ret = thumbnail_request_extract_all_thumbs(uid);
504 return MEDIA_SVC_PLUGIN_ERROR;
507 return MEDIA_SVC_PLUGIN_ERROR_NONE;
510 int send_dir_update_noti(void * handle, const char *dir_path, char **err_msg)
512 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
514 if (!STRING_VALID(dir_path)) {
515 __set_error_message(ERR_FOLDER_PATH, err_msg);
516 return MEDIA_SVC_PLUGIN_ERROR;
519 ret = media_svc_send_dir_update_noti(handle, dir_path);
521 __set_error_message(ret, err_msg);
522 return MEDIA_SVC_PLUGIN_ERROR;
525 return MEDIA_SVC_PLUGIN_ERROR_NONE;
528 int count_delete_items_in_folder(void * handle, const char *folder_path, int *count, char ** err_msg)
530 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
533 __set_error_message(ERR_HANDLE, err_msg);
534 return MEDIA_SVC_PLUGIN_ERROR;
538 __set_error_message(ERR_HANDLE, err_msg);
539 return MEDIA_SVC_PLUGIN_ERROR;
542 if (!STRING_VALID(folder_path)) {
543 __set_error_message(ERR_FOLDER_PATH, err_msg);
544 return MEDIA_SVC_PLUGIN_ERROR;
547 ret = media_svc_count_invalid_items_in_folder(handle, folder_path, count);
549 __set_error_message(ret, err_msg);
550 return MEDIA_SVC_PLUGIN_ERROR;
553 return MEDIA_SVC_PLUGIN_ERROR_NONE;
556 int check_db(void * handle, uid_t uid, char **err_msg)
558 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
561 ret = media_svc_create_table(handle, uid);
563 __set_error_message(ret, err_msg);
564 return MEDIA_SVC_PLUGIN_ERROR;
568 ret = media_svc_check_db_upgrade(handle, uid);
570 __set_error_message(ret, err_msg);
571 return MEDIA_SVC_PLUGIN_ERROR;
574 return MEDIA_SVC_PLUGIN_ERROR_NONE;
577 int check_db_corrupt(void * handle, char **err_msg)
579 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
582 ret = media_svc_check_db_corrupt(handle);
584 __set_error_message(ret, err_msg);
585 return MEDIA_SVC_PLUGIN_ERROR;
588 return MEDIA_SVC_PLUGIN_ERROR_NONE;
591 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)
593 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
596 __set_error_message(ERR_HANDLE, err_msg);
597 return MEDIA_SVC_PLUGIN_ERROR;
601 __set_error_message(ERR_HANDLE, err_msg);
602 return MEDIA_SVC_PLUGIN_ERROR;
605 ret = media_svc_get_folder_list(handle, start_path, folder_list, (time_t**)modified_time_list, item_num_list, count);
607 __set_error_message(ret, err_msg);
608 return MEDIA_SVC_PLUGIN_ERROR;
611 return MEDIA_SVC_PLUGIN_ERROR_NONE;
614 int update_folder_time(void * handle, char *folder_path, uid_t uid, char ** err_msg)
616 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
619 __set_error_message(ERR_HANDLE, err_msg);
620 return MEDIA_SVC_PLUGIN_ERROR;
623 if(folder_path == NULL) {
624 __set_error_message(ERR_HANDLE, err_msg);
625 return MEDIA_SVC_PLUGIN_ERROR;
628 ret = media_svc_update_folder_time(handle, folder_path, uid);
630 __set_error_message(ret, err_msg);
631 return MEDIA_SVC_PLUGIN_ERROR;
634 return MEDIA_SVC_PLUGIN_ERROR_NONE;
637 int update_item_begin(void * handle, int item_cnt, char ** err_msg)
639 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
642 __set_error_message(ERR_HANDLE, err_msg);
643 return MEDIA_SVC_PLUGIN_ERROR;
646 ret = media_svc_update_item_begin(handle, item_cnt);
648 __set_error_message(ret, err_msg);
649 return MEDIA_SVC_PLUGIN_ERROR;
652 return MEDIA_SVC_PLUGIN_ERROR_NONE;
655 int update_item_end(void * handle, uid_t uid, char ** err_msg)
657 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
660 __set_error_message(ERR_HANDLE, err_msg);
661 return MEDIA_SVC_PLUGIN_ERROR;
664 ret = media_svc_update_item_end(handle, uid);
666 __set_error_message(ret, err_msg);
667 return MEDIA_SVC_PLUGIN_ERROR;
670 return MEDIA_SVC_PLUGIN_ERROR_NONE;
673 int update_item_meta(void * handle, const char *file_path, int storage_type, uid_t uid, char ** err_msg)
675 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
678 __set_error_message(ERR_HANDLE, err_msg);
679 return MEDIA_SVC_PLUGIN_ERROR;
682 if(file_path == NULL) {
683 __set_error_message(ERR_HANDLE, err_msg);
684 return MEDIA_SVC_PLUGIN_ERROR;
687 ret = media_svc_update_item_meta(handle, file_path, storage_type, uid);
689 __set_error_message(ret, err_msg);
690 return MEDIA_SVC_PLUGIN_ERROR;
693 return MEDIA_SVC_PLUGIN_ERROR_NONE;