Move function definition to aul header
[platform/core/appfw/aul-1.git] / src / mime.c
1 /*
2  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <string.h>
20
21 #include <xdgmime.h>
22
23 #include "aul.h"
24 #include "aul_api.h"
25 #include "miregex.h"
26 #include "menu_db_util.h"
27 #include "aul_util.h"
28
29 static int __match_content_with_regex(const char *content, regex_t *regex_preg)
30 {
31         if (regexec(regex_preg, content, 0, NULL, 0) == 0)
32                 return 1;
33         else
34                 return 0;
35 }
36
37 API int aul_get_mime_from_content(const char *content, char *mimetype,
38                                      int len)
39 {
40         char *founded = NULL;
41         regex_tbl *miregex_tbl = NULL;
42
43         if (content == NULL)
44                 return AUL_R_EINVAL;
45
46         if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
47                 _E("load miregex_table fail");
48                 return AUL_R_ERROR;
49         }
50
51         while (miregex_tbl) {
52                 if (__match_content_with_regex(content,
53                         &(miregex_tbl->regex_preg))) {
54                         founded = miregex_tbl->mimetype;
55                         SECURE_LOGD("content %s => mimetype %s", content, founded);
56                         break;
57                 }
58                 miregex_tbl = miregex_tbl->next;
59         }
60
61         if (founded != NULL)
62                 snprintf(mimetype, len, "%s", founded);
63         else {
64                 /* TODO : should to try to extract from share mime info's data*/
65                 return AUL_R_ERROR;
66         }
67
68         return AUL_R_OK;
69 }
70
71 API int aul_get_mime_description(const char *mimetype, char *desc, int len)
72 {
73         regex_tbl *miregex_tbl = NULL;
74         char *founded = NULL;
75
76         if (mimetype == NULL)
77                 return AUL_R_EINVAL;
78
79         if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
80                 _E("load miregex_table fail");
81                 return AUL_R_ERROR;
82         }
83
84         while (miregex_tbl) {
85                 if (strcmp(miregex_tbl->mimetype, mimetype) == 0) {
86                         founded = miregex_tbl->desc;
87                         _D("mimetype %s => desc %s", mimetype, founded);
88                         break;
89                 }
90                 miregex_tbl = miregex_tbl->next;
91         }
92
93         if (founded != NULL)
94                 snprintf(desc, len, "%s", founded);
95         else {
96                 /* TODO : should to try to extract from share mime info's comment */
97                 return AUL_R_ERROR;
98         }
99
100         return AUL_R_OK;
101 }
102
103 API int aul_get_mime_extension(const char *mimetype, char *ext, int len)
104 {
105         const char **extlist;
106         int totlen = 0;
107         const char *unaliased_mimetype;
108
109         if (mimetype == NULL || ext == NULL || len <= 0)
110                 return AUL_R_EINVAL;
111
112         unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
113         if (unaliased_mimetype == NULL)
114                 return AUL_R_ERROR;
115
116         extlist = xdg_mime_get_file_names_from_mime_type(unaliased_mimetype);
117         if (extlist == NULL)
118                 return AUL_R_ERROR;
119
120         if (extlist[0] == NULL)
121                 return AUL_R_ERROR;
122
123         ext[0] = 0;
124         while (*extlist != NULL) {
125                 if (*(extlist + 1) == NULL) {
126                         snprintf(&ext[totlen], len - totlen, "%s", *extlist);
127                         break;
128                 } else {
129                         snprintf(&ext[totlen], len - totlen, "%s,", *extlist);
130                         if (strlen(*extlist) > len - totlen - 1)
131                                 break;
132                         totlen += strlen(*extlist) + 1;
133                         extlist++;
134                 }
135         }
136
137         return AUL_R_OK;
138 }
139
140 API int aul_get_mime_icon(const char *mimetype, char *iconname, int len)
141 {
142         const char *icon;
143         const char *unaliased_mimetype;
144
145         if (mimetype == NULL || iconname == NULL || len <= 0)
146                 return AUL_R_EINVAL;
147
148         unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
149         if (unaliased_mimetype == NULL)
150                 return AUL_R_ERROR;
151
152         icon = xdg_mime_get_icon(unaliased_mimetype);
153         if (icon == NULL)
154                 icon = xdg_mime_get_generic_icon(unaliased_mimetype);
155
156         if (icon != NULL) {
157                 snprintf(iconname, len, "%s", icon);
158                 return AUL_R_OK;
159         } else
160                 return AUL_R_ERROR;
161 }
162
163 API int aul_get_mime_from_file(const char *filename, char *mimetype, int len)
164 {
165         const char *mime;
166         if (filename == NULL)
167                 return AUL_R_EINVAL;
168
169         if (access(filename, F_OK) != 0)
170                 return AUL_R_EINVAL;
171
172         mime = xdg_mime_get_mime_type_for_file(filename, 0);
173         if (strcmp(mime, "application/octet-stream") == 0)
174                 mime = xdg_mime_get_mime_type_from_file_name(filename);
175
176         snprintf(mimetype, len, "%s", mime);
177         return AUL_R_OK;
178 }
179
180 API int aul_set_defapp_with_mime(const char *mimetype, const char *defapp)
181 {
182         /* removed */
183         return 0;
184 }
185
186 API int aul_get_defapp_from_mime(const char *mimetype, char *defapp, int len)
187 {
188         /* removed */
189         return 0;
190 }
191
192 API int aul_open_content(const char *content)
193 {
194         /* removed */
195         return 0;
196 }
197
198 API int aul_open_file_with_mimetype(const char *filename,
199                                        const char *mimetype)
200 {
201         /* removed */
202         return 0;
203 }
204
205 API int aul_open_file(const char *filename)
206 {
207         /* removed */
208         return 0;
209 }
210