improve way to notify updated DB
[platform/core/multimedia/media-server.git] / src / common / media-common-utils.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
22 /**
23  * This file defines api utilities of contents manager engines.
24  *
25  * @file                media-server-utils.c
26  * @author      Yong Yeon Kim(yy9875.kim@samsung.com)
27  * @version     1.0
28  * @brief       This file implements main database operation.
29  */
30
31 #include <errno.h>
32 #include <vconf.h>
33 #include <aul/aul.h>
34
35 #include "media-util.h"
36 #include "media-server-ipc.h"
37 #include "media-common-dbg.h"
38 #include "media-common-drm.h"
39 #include "media-common-utils.h"
40
41 #ifdef FMS_PERF
42 #include <sys/time.h>
43 #define MILLION 1000000L
44 struct timeval g_mmc_start_time;
45 struct timeval g_mmc_end_time;
46 #endif
47
48 #define MS_DRM_CONTENT_TYPE_LENGTH 100
49
50 #ifdef FMS_PERF
51 void
52 ms_check_start_time(struct timeval *start_time)
53 {
54         gettimeofday(start_time, NULL);
55 }
56
57 void
58 ms_check_end_time(struct timeval *end_time)
59 {
60         gettimeofday(end_time, NULL);
61 }
62
63 void
64 ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
65 {
66         struct timeval time;
67         long difftime;
68
69         time.tv_sec = end_time->tv_sec - start_time->tv_sec;
70         time.tv_usec = end_time->tv_usec - start_time->tv_usec;
71         difftime = MILLION * time.tv_sec + time.tv_usec;
72         MS_DBG("The function_to_time took %ld microseconds or %f seconds.",
73                difftime, difftime / (double)MILLION);
74 }
75 #endif
76
77 bool
78 ms_is_mmc_inserted(void)
79 {
80         int data = -1;
81         ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
82         if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
83                 return false;
84         } else {
85                 return true;
86         }
87 }
88
89 ms_storage_type_t
90 ms_get_storage_type_by_full(const char *path)
91 {
92         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
93                 return MS_STORAGE_INTERNAL;
94         } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
95                 return MS_STORAGE_EXTERNAL;
96         } else
97                 return MS_MEDIA_ERR_INVALID_PATH;
98 }
99
100 int
101 ms_get_mime(const char *path, char *mimetype)
102 {
103         int ret = MS_MEDIA_ERR_NONE;
104
105         if (path == NULL)
106                 return MS_MEDIA_ERR_INVALID_PARAMETER;
107
108         /*get content type and mime type from file. */
109         /*in case of drm file. */
110         if (ms_is_drm_file(path)) {
111                 if (ms_get_mime_in_drm_info(path, mimetype) != MS_MEDIA_ERR_NONE) {
112                         MS_DBG_ERR("Fail to get mime from drm API");
113                         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
114                                 MS_DBG_ERR("aul_get_mime_from_file fail");
115                                 ret = MS_MEDIA_ERR_MIME_GET_FAIL;
116                         } else {
117                                 MS_DBG_ERR("aul_get_mime_from_file success");
118                                 ret = MS_MEDIA_ERR_NONE;
119                         }
120                 }
121         } else {
122                 /*in case of normal files */
123                 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
124                         MS_DBG_ERR("aul_get_mime_from_file fail");
125                         ret = MS_MEDIA_ERR_MIME_GET_FAIL;
126                 }
127         }
128
129         return ret;
130 }
131
132 int
133 ms_strappend(char *res, const int size, const char *pattern,
134              const char *str1, const char *str2)
135 {
136         int len = 0;
137         int real_size = size - 1;
138
139         if (!res ||!pattern || !str1 ||!str2 )
140                 return MS_MEDIA_ERR_INVALID_PARAMETER;
141
142         if (real_size < (strlen(str1) + strlen(str2)))
143                 return MS_MEDIA_ERR_INVALID_PARAMETER;
144
145         len = snprintf(res, real_size, pattern, str1, str2);
146         if (len < 0) {
147                 return MS_MEDIA_ERR_INVALID_PARAMETER;
148         }
149
150         res[len] = '\0';
151
152         return MS_MEDIA_ERR_NONE;
153 }
154
155 int
156 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
157 {
158         int len = 0;
159         int real_size = size;
160
161         if (!res || !pattern || !str1) {
162                 MS_DBG_ERR("parameta is invalid");
163                 return MS_MEDIA_ERR_INVALID_PARAMETER;
164         }
165
166         if (real_size < strlen(str1)) {
167                 MS_DBG_ERR("size is wrong");
168                 return MS_MEDIA_ERR_INVALID_PARAMETER;
169         }
170
171         len = snprintf(res, real_size, pattern, str1);
172         if (len < 0) {
173                 MS_DBG_ERR("snprintf failed");
174                 return MS_MEDIA_ERR_INVALID_PARAMETER;
175         }
176
177         res[len] = '\0';
178
179         return MS_MEDIA_ERR_NONE;
180 }
181
182 bool
183 ms_config_get_int(const char *key, int *value)
184 {
185         int err;
186
187         if (!key || !value) {
188                 MS_DBG_ERR("Arguments key or value is NULL");
189                 return false;
190         }
191
192         err = vconf_get_int(key, value);
193         if (err == 0)
194                 return true;
195         else if (err == -1)
196                 return false;
197         else
198                 MS_DBG_ERR("Unexpected error code: %d", err);
199
200         return false;
201 }
202
203 bool
204 ms_config_set_int(const char *key, int value)
205 {
206         int err;
207
208         if (!key) {
209                 MS_DBG_ERR("Arguments key is NULL");
210                 return false;
211         }
212
213         err = vconf_set_int(key, value);
214         if (err == 0)
215                 return true;
216         else if (err == -1)
217                 return false;
218         else
219                 MS_DBG_ERR("Unexpected error code: %d", err);
220
221         return false;
222 }
223
224 bool
225 ms_config_get_str(const char *key, char *value)
226 {
227         char *res;
228         if (!key || !value) {
229                 MS_DBG_ERR("Arguments key or value is NULL");
230                 return false;
231         }
232
233         res = vconf_get_str(key);
234         if (res) {
235                 strncpy(value, res, strlen(res) + 1);
236                 return true;
237         }
238
239         return false;
240 }
241
242 bool
243 ms_config_set_str(const char *key, const char *value)
244 {
245         int err;
246
247         if (!key || !value) {
248                 MS_DBG_ERR("Arguments key or value is NULL");
249                 return false;
250         }
251
252         err = vconf_set_str(key, value);
253         if (err == 0)
254                 return true;
255         else
256                 MS_DBG_ERR("fail to vconf_set_str %d", err);
257
258         return false;
259 }
260