4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <sys/types.h>
35 #include "menu_db_util.h"
36 #include "simple_util.h"
38 static int __match_content_with_regex(const char *content, regex_t *regex_preg)
40 if (regexec(regex_preg, content, 0, NULL, 0) == 0)
46 SLPAPI int aul_get_mime_from_content(const char *content, char *mimetype,
50 regex_tbl *miregex_tbl = NULL;
55 if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
56 _E("load miregex_table fail\n");
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);
67 miregex_tbl = miregex_tbl->next;
71 snprintf(mimetype, len, "%s", founded);
73 /* TODO : should to try to extract from share mime info's data*/
80 SLPAPI int aul_get_mime_description(const char *mimetype, char *desc, int len)
82 regex_tbl *miregex_tbl = NULL;
88 if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
89 _E("load miregex_table fail\n");
94 if (strcmp(miregex_tbl->mimetype, mimetype) == 0) {
95 founded = miregex_tbl->desc;
96 _D("mimetype %s => desc %s\n", mimetype, founded);
99 miregex_tbl = miregex_tbl->next;
103 snprintf(desc, len, "%s", founded);
105 /* TODO : should to try to extract from
106 share mime info's comment */
113 SLPAPI int aul_get_mime_extension(const char *mimetype, char *ext, int len)
115 const char **extlist;
117 const char *unaliased_mimetype;
119 if (mimetype == NULL || ext == NULL || len <= 0)
122 unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
123 if (unaliased_mimetype == NULL)
126 extlist = xdg_mime_get_file_names_from_mime_type(unaliased_mimetype);
130 if (extlist[0] == NULL)
134 while (*extlist != NULL) {
135 if (*(extlist + 1) == NULL) {
136 snprintf(&ext[totlen], len - totlen, "%s", *extlist);
139 snprintf(&ext[totlen], len - totlen, "%s,", *extlist);
140 if (strlen(*extlist) > len - totlen - 1)
142 totlen += strlen(*extlist) + 1;
150 SLPAPI int aul_get_mime_icon(const char *mimetype, char *iconname, int len)
153 const char *unaliased_mimetype;
155 if (mimetype == NULL || iconname == NULL || len <= 0)
158 unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
159 if (unaliased_mimetype == NULL)
162 icon = xdg_mime_get_icon(unaliased_mimetype);
164 icon = xdg_mime_get_generic_icon(unaliased_mimetype);
167 snprintf(iconname, len, "%s", icon);
173 SLPAPI int aul_get_mime_from_file(const char *filename, char *mimetype, int len)
177 char err_buf[128] = {0,};
179 if (filename == NULL)
182 if (access(filename, F_OK) != 0) {
183 if (!strerror_r(errno, err_buf, sizeof(err_buf)))
184 _E("access fail(%s)", err_buf);
186 _E("access fail(%d)", errno);
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);
194 _E("Unable to get stat, error is %d", errno);
197 if(S_ISDIR(statbuf.st_mode))
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);
205 snprintf(mimetype, len, "%s", mime);