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.
32 #include "menu_db_util.h"
33 #include "simple_util.h"
35 #define MIME_APP_SELECTOR "org.tizen.app-selector"
37 static int __match_content_with_regex(const char *content, regex_t *regex_preg);
38 static int get_defapp_from_desktop(const char *mimetype, char *defapp, int len);
39 static int _aul_get_defapp_from_mime(const char *mimetype, char *unaliased,
40 char *defapp, int len_unaliased,
42 static int __launch_with_defapp(const char *mime_type,
43 const char *mime_content);
47 static int __match_content_with_regex(const char *content, regex_t *regex_preg)
49 if (regexec(regex_preg, content, 0, NULL, 0) == 0)
55 SLPAPI int aul_get_mime_from_content(const char *content, char *mimetype,
59 regex_tbl *miregex_tbl = NULL;
64 if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
65 _E("load miregex_table fail\n");
70 if (__match_content_with_regex(content,
71 &(miregex_tbl->regex_preg))) {
72 founded = miregex_tbl->mimetype;
73 _D("content %s => mimetype %s\n", content, founded);
76 miregex_tbl = miregex_tbl->next;
80 snprintf(mimetype, len, "%s", founded);
82 /* TODO : should to try to extract from share mime info's data*/
89 SLPAPI int aul_get_mime_description(const char *mimetype, char *desc, int len)
91 regex_tbl *miregex_tbl = NULL;
97 if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
98 _E("load miregex_table fail\n");
102 while (miregex_tbl) {
103 if (strcmp(miregex_tbl->mimetype, mimetype) == 0) {
104 founded = miregex_tbl->desc;
105 _D("mimetype %s => desc %s\n", mimetype, founded);
108 miregex_tbl = miregex_tbl->next;
112 snprintf(desc, len, "%s", founded);
114 /* TODO : should to try to extract from
115 share mime info's comment */
122 SLPAPI int aul_get_mime_extension(const char *mimetype, char *ext, int len)
124 const char **extlist;
126 const char *unaliased_mimetype;
128 if (mimetype == NULL || ext == NULL || len <= 0)
131 unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
132 if (unaliased_mimetype == NULL)
135 extlist = xdg_mime_get_file_names_from_mime_type(unaliased_mimetype);
139 if (extlist[0] == NULL)
143 while (*extlist != NULL) {
144 if (*(extlist + 1) == NULL) {
145 snprintf(&ext[totlen], len - totlen, "%s", *extlist);
148 snprintf(&ext[totlen], len - totlen, "%s,", *extlist);
149 if (strlen(*extlist) > len - totlen - 1)
151 totlen += strlen(*extlist) + 1;
159 SLPAPI int aul_get_mime_icon(const char *mimetype, char *iconname, int len)
162 const char *unaliased_mimetype;
164 if (mimetype == NULL || iconname == NULL || len <= 0)
167 unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
168 if (unaliased_mimetype == NULL)
171 icon = xdg_mime_get_icon(unaliased_mimetype);
173 icon = xdg_mime_get_generic_icon(unaliased_mimetype);
176 snprintf(iconname, len, "%s", icon);
182 SLPAPI int aul_get_mime_from_file(const char *filename, char *mimetype, int len)
185 if (filename == NULL)
188 if (access(filename, F_OK) != 0)
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);
196 snprintf(mimetype, len, "%s", mime);
200 SLPAPI int aul_set_defapp_with_mime(const char *mimetype, const char *defapp)
202 const char *unaliased_mimetype;
204 if (mimetype == NULL || defapp == NULL) {
209 unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
210 if (unaliased_mimetype == NULL)
213 if (mida_add_app(unaliased_mimetype, defapp) < 0) {
214 _E("fail to add: mimtype-%s and defapp-%s", unaliased_mimetype,
222 static ail_cb_ret_e __defapp_with_mime_func(
223 const ail_appinfo_h appinfo, void *user_data)
225 char **package = (char **)user_data;
228 ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &str);
230 _D("defapp from desktop = %s", str);
232 *package = strdup(str);
234 return AIL_CB_RET_CANCEL; /*return AIL_CB_RET_CONTINUE;*/
237 static int get_defapp_from_desktop(const char *mimetype, char *defapp, int len)
244 ret = ail_filter_new(&filter);
245 if (ret != AIL_ERROR_OK)
248 ret = ail_filter_add_str(filter, AIL_PROP_MIMETYPE_STR, mimetype);
249 if (ret != AIL_ERROR_OK) {
254 ail_filter_count_appinfo(filter, &pkg_count);
256 if (pkg_count == 1) {
257 ail_filter_list_appinfo_foreach(filter,
258 __defapp_with_mime_func, (void *)&tmp);
261 strncpy(defapp,tmp,len);
262 _D("defapp from desktop = %s", defapp);
263 aul_set_defapp_with_mime(mimetype, defapp);
271 ail_filter_destroy(filter);
275 SLPAPI int aul_get_defapp_from_mime(const char *mimetype, char *defapp, int len)
278 const char *unaliased_mimetype;
280 if (mimetype == NULL || defapp == NULL || len <= 0)
283 unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
284 if (unaliased_mimetype == NULL)
288 if ((res = mida_get_app(unaliased_mimetype)) != NULL) {
289 snprintf(defapp, len, "%s", res);
291 _D("Found %s for %s from mime db", defapp, unaliased_mimetype);
295 if (get_defapp_from_desktop(unaliased_mimetype, defapp, len) != 0)
301 static int _aul_get_defapp_from_mime(const char *mimetype, char *unaliased,
302 char *defapp, int len_unaliased,
306 const char *unaliased_mimetype;
308 if (mimetype == NULL || unaliased == NULL || len_unaliased <= 0
309 || defapp == NULL || len_defapp <= 0)
312 unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
313 if (unaliased_mimetype == NULL)
316 snprintf(unaliased, len_unaliased, "%s", unaliased_mimetype);
319 if ((res = mida_get_app(unaliased_mimetype)) != NULL) {
320 snprintf(defapp, len_defapp, "%s", res);
322 _D("Found %s for %s from mime db", defapp, unaliased_mimetype);
326 if (get_defapp_from_desktop(unaliased_mimetype, defapp, len_defapp) < 0)
332 static int __launch_with_defapp(const char *mime_type, const char *mime_content)
334 ail_appinfo_h handle;
336 char defapp[MAX_LOCAL_BUFSZ];
337 char unaliased_mime_type[MAX_LOCAL_BUFSZ];
339 int ret = AUL_R_ERROR;
341 kb = bundle_create();
343 _E("bundle creation fail");
346 bundle_add(kb, AUL_K_MIME_TYPE, mime_type);
347 bundle_add(kb, AUL_K_MIME_CONTENT, mime_content);
350 if (_aul_get_defapp_from_mime
351 (mime_type, unaliased_mime_type, defapp,
352 sizeof(unaliased_mime_type), sizeof(defapp)) < 0) {
353 _D("mimetype : %s, unaliased mimetype : %s, mime_content : %s,"
354 " no default app", mime_type,
355 unaliased_mime_type, mime_content);
356 bundle_add(kb, AUL_K_UNALIASED_MIME_TYPE, unaliased_mime_type);
357 ret = aul_launch_app(MIME_APP_SELECTOR, kb);
358 /* TODO: When launching MIME APP SELECTOR, what should
359 be the return value? */
360 /* Currently, it returns 0 if the app selector is launched */
364 ail_ret = ail_package_get_appinfo(defapp, &handle);
366 if (ail_ret == AIL_ERROR_OK) {
367 ail_package_destroy_appinfo(handle);
368 _D("mimetype : %s, unaliased mimetype : %s, "
369 "mime_content : %s, defapp : %s", mime_type,
371 mime_content, defapp);
372 bundle_add(kb, AUL_K_UNALIASED_MIME_TYPE,
373 unaliased_mime_type);
374 ret = aul_launch_app(defapp, kb);
375 } else if (ail_ret == AIL_ERROR_NO_DATA) {
376 _D("defapp %s for mimetype : %s, mime_content : %s "
377 "does NOT exist", defapp,
378 mime_type, mime_content);
379 mida_delete_with_pkgname(defapp);
380 ail_package_destroy_appinfo(handle);
383 _E("ail_package_get_appinfo with %s failed", defapp);
395 SLPAPI int aul_open_content(const char *content)
398 char mime[MAX_LOCAL_BUFSZ];
399 if ((ret = aul_get_mime_from_content(content, mime, sizeof(mime))) < 0)
402 return __launch_with_defapp(mime, content);
405 SLPAPI int aul_open_file_with_mimetype(const char *filename,
406 const char *mimetype)
408 if (mimetype == NULL)
411 return __launch_with_defapp(mimetype, filename);
414 SLPAPI int aul_open_file(const char *filename)
417 char mime[MAX_LOCAL_BUFSZ];
418 if ((ret = aul_get_mime_from_file(filename, mime, sizeof(mime))) < 0)
421 return __launch_with_defapp(mime, filename);