Git init
[framework/multimedia/libmedia-thumbnail.git] / src / media-thumbnail.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@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 "media-thumbnail.h"
23 #include "media-thumb-debug.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-internal.h"
26 #include "media-thumb-ipc.h"
27 #include "media-thumb-db.h"
28
29 #include <glib.h>
30 #include <media-svc.h>
31
32 int thumbnail_request_from_db(const char *origin_path, char *thumb_path, int max_length)
33 {
34         int err = -1;
35         int need_update_db = 0;
36         media_thumb_info thumb_info;
37
38         if (origin_path == NULL || thumb_path == NULL) {
39                 thumb_err("Invalid parameter");
40                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
41         }
42
43         if (!g_file_test
44             (origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
45                         thumb_err("Original path(%s) doesn't exist.", origin_path);
46                         return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
47         }
48
49         if (max_length <= 0) {
50                 thumb_err("Length is invalid");
51                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
52         }
53
54         int store_type = -1;
55         store_type = _media_thumb_get_store_type_by_path(origin_path);
56
57         if ((store_type != THUMB_PHONE) && (store_type != THUMB_MMC)) {
58                 thumb_err("origin path(%s) is invalid", origin_path);
59                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
60         }
61
62         thumb_err("Path : %s", origin_path);
63
64         err = minfo_init();
65
66         err = _media_thumb_get_thumb_from_db(origin_path, thumb_path, max_length, &need_update_db);
67         if (err == 0) {
68                 minfo_finalize();
69                 return MEDIA_THUMB_ERROR_NONE;
70         }
71
72         /* Request for thumb file to the daemon "Thumbnail generator" */
73         err = _media_thumb_request(THUMB_REQUEST_DB, MEDIA_THUMB_LARGE, origin_path, thumb_path, max_length, &thumb_info);
74         if (err < 0) {
75                 thumb_err("_media_thumb_request failed : %d", err);
76                 minfo_finalize();
77                 return err;
78         }
79
80         /* Need to update DB once generating thumb is done */
81         if (need_update_db) {
82                 err = _media_thumb_update_db(origin_path, thumb_path, thumb_info.origin_width, thumb_info.origin_height);
83                 if (err < 0) {
84                         thumb_err("_media_thumb_update_db failed : %d", err);
85                 }
86         }
87
88         minfo_finalize();
89         return MEDIA_THUMB_ERROR_NONE;
90 }
91
92 int thumbnail_request_save_to_file(const char *origin_path, media_thumb_type thumb_type, const char *thumb_path)
93 {
94         int err = -1;
95
96         if (origin_path == NULL || thumb_path == NULL) {
97                 thumb_err("Invalid parameter");
98                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
99         }
100
101         media_thumb_info thumb_info;
102         char tmp_thumb_path[MAX_PATH_SIZE] = {0,};
103
104         strncpy(tmp_thumb_path, thumb_path, sizeof(tmp_thumb_path));
105
106         /* Request for thumb file to the daemon "Thumbnail generator" */
107         err = _media_thumb_request(THUMB_REQUEST_SAVE, thumb_type, origin_path, tmp_thumb_path, sizeof(tmp_thumb_path), &thumb_info);
108         if (err < 0) {
109                 thumb_err("_media_thumb_request failed : %d", err);
110                 return err;
111         }
112
113         return MEDIA_THUMB_ERROR_NONE;
114 }
115
116