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