tizen 2.4 release
[framework/appfw/aul-1.git] / src / mime.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@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 #define _GNU_SOURCE
23 #include "aul.h"
24 #include "aul_api.h"
25 #include "miregex.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <xdgmime.h>
32 #include <bundle.h>
33 #include <errno.h>
34
35 #include "menu_db_util.h"
36 #include "simple_util.h"
37
38 static int __match_content_with_regex(const char *content, regex_t *regex_preg)
39 {
40         if (regexec(regex_preg, content, 0, NULL, 0) == 0)
41                 return 1;
42         else
43                 return 0;
44 }
45
46 SLPAPI int aul_get_mime_from_content(const char *content, char *mimetype,
47                                      int len)
48 {
49         char *founded = NULL;
50         regex_tbl *miregex_tbl = NULL;
51
52         if (content == NULL)
53                 return AUL_R_EINVAL;
54
55         if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
56                 _E("load miregex_table fail\n");
57                 return AUL_R_ERROR;
58         }
59
60         while (miregex_tbl) {
61                 if (__match_content_with_regex(content,
62                         &(miregex_tbl->regex_preg))) {
63                         founded = miregex_tbl->mimetype;
64                         SECURE_LOGD("content %s => mimetype %s\n", content, founded);
65                         break;
66                 }
67                 miregex_tbl = miregex_tbl->next;
68         }
69
70         if (founded != NULL)
71                 snprintf(mimetype, len, "%s", founded);
72         else {
73                 /* TODO : should to try to extract from share mime info's data*/
74                 return AUL_R_ERROR;
75         }
76
77         return AUL_R_OK;
78 }
79
80 SLPAPI int aul_get_mime_description(const char *mimetype, char *desc, int len)
81 {
82         regex_tbl *miregex_tbl = NULL;
83         char *founded = NULL;
84
85         if (mimetype == NULL)
86                 return AUL_R_EINVAL;
87
88         if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
89                 _E("load miregex_table fail\n");
90                 return AUL_R_ERROR;
91         }
92
93         while (miregex_tbl) {
94                 if (strcmp(miregex_tbl->mimetype, mimetype) == 0) {
95                         founded = miregex_tbl->desc;
96                         _D("mimetype %s => desc %s\n", mimetype, founded);
97                         break;
98                 }
99                 miregex_tbl = miregex_tbl->next;
100         }
101
102         if (founded != NULL)
103                 snprintf(desc, len, "%s", founded);
104         else {
105                 /* TODO : should to try to extract from
106                    share mime info's comment */
107                 return AUL_R_ERROR;
108         }
109
110         return AUL_R_OK;
111 }
112
113 SLPAPI int aul_get_mime_extension(const char *mimetype, char *ext, int len)
114 {
115         const char **extlist;
116         int totlen = 0;
117         const char *unaliased_mimetype;
118
119         if (mimetype == NULL || ext == NULL || len <= 0)
120                 return AUL_R_EINVAL;
121
122         unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
123         if (unaliased_mimetype == NULL)
124                 return AUL_R_ERROR;
125
126         extlist = xdg_mime_get_file_names_from_mime_type(unaliased_mimetype);
127         if (extlist == NULL)
128                 return AUL_R_ERROR;
129
130         if (extlist[0] == NULL)
131                 return AUL_R_ERROR;
132
133         ext[0] = 0;
134         while (*extlist != NULL) {
135                 if (*(extlist + 1) == NULL) {
136                         snprintf(&ext[totlen], len - totlen, "%s", *extlist);
137                         break;
138                 } else {
139                         snprintf(&ext[totlen], len - totlen, "%s,", *extlist);
140                         if (strlen(*extlist) > len - totlen - 1)
141                                 break;
142                         totlen += strlen(*extlist) + 1;
143                         extlist++;
144                 }
145         }
146
147         return AUL_R_OK;
148 }
149
150 SLPAPI int aul_get_mime_icon(const char *mimetype, char *iconname, int len)
151 {
152         const char *icon;
153         const char *unaliased_mimetype;
154
155         if (mimetype == NULL || iconname == NULL || len <= 0)
156                 return AUL_R_EINVAL;
157
158         unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
159         if (unaliased_mimetype == NULL)
160                 return AUL_R_ERROR;
161
162         icon = xdg_mime_get_icon(unaliased_mimetype);
163         if (icon == NULL)
164                 icon = xdg_mime_get_generic_icon(unaliased_mimetype);
165
166         if (icon != NULL) {
167                 snprintf(iconname, len, "%s", icon);
168                 return AUL_R_OK;
169         } else
170                 return AUL_R_ERROR;
171 }
172
173 SLPAPI int aul_get_mime_from_file(const char *filename, char *mimetype, int len)
174 {
175         const char *mime;
176         struct stat statbuf;
177         char err_buf[128] = {0,};
178
179         if (filename == NULL)
180                 return AUL_R_EINVAL;
181
182         if (access(filename, F_OK) != 0) {
183                 if (!strerror_r(errno, err_buf, sizeof(err_buf)))
184                         _E("access fail(%s)", err_buf);
185                 else
186                         _E("access fail(%d)", errno);
187                 return AUL_R_EINVAL;
188         }
189
190         if (stat(filename, &statbuf) != 0) {
191                 if (!strerror_r(errno, err_buf, sizeof(err_buf)))
192                         _E("Unable to get stat, error is %s", err_buf);
193                 else
194                         _E("Unable to get stat, error is %d", errno);
195                 return AUL_R_ERROR;
196         }
197         if(S_ISDIR(statbuf.st_mode))
198                 return AUL_R_EINVAL;
199
200         mime = xdg_mime_get_mime_type_for_file(filename, 0);
201         if (strcmp(mime, "application/octet-stream") == 0) {
202                 mime = xdg_mime_get_mime_type_from_file_name(filename);
203         }
204
205         snprintf(mimetype, len, "%s", mime);
206         return AUL_R_OK;
207 }