1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/*
* aul
*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "aul.h"
#include "aul_api.h"
#include "mida.h"
#include "miregex.h"
#include <stdio.h>
#include <string.h>
#include <xdgmime.h>
#include <bundle.h>
#include "menu_db_util.h"
#include "simple_util.h"
#define MIME_APP_SELECTOR "org.tizen.app-selector"
static int __match_content_with_regex(const char *content, regex_t *regex_preg);
static int get_defapp_from_desktop(const char *mimetype, char *defapp, int len);
static int _aul_get_defapp_from_mime(const char *mimetype, char *unaliased,
char *defapp, int len_unaliased,
int len_defapp);
static int __launch_with_defapp(const char *mime_type,
const char *mime_content);
static int __match_content_with_regex(const char *content, regex_t *regex_preg)
{
if (regexec(regex_preg, content, 0, NULL, 0) == 0)
return 1;
else
return 0;
}
SLPAPI int aul_get_mime_from_content(const char *content, char *mimetype,
int len)
{
char *founded = NULL;
regex_tbl *miregex_tbl = NULL;
if (content == NULL)
return AUL_R_EINVAL;
if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
_E("load miregex_table fail\n");
return AUL_R_ERROR;
}
while (miregex_tbl) {
if (__match_content_with_regex(content,
&(miregex_tbl->regex_preg))) {
founded = miregex_tbl->mimetype;
_D("content %s => mimetype %s\n", content, founded);
break;
}
miregex_tbl = miregex_tbl->next;
}
if (founded != NULL)
snprintf(mimetype, len, "%s", founded);
else {
/* TODO : should to try to extract from share mime info's data*/
return AUL_R_ERROR;
}
return AUL_R_OK;
}
SLPAPI int aul_get_mime_description(const char *mimetype, char *desc, int len)
{
regex_tbl *miregex_tbl = NULL;
char *founded = NULL;
if (mimetype == NULL)
return AUL_R_EINVAL;
if ((miregex_tbl = miregex_get_regex_table()) == NULL) {
_E("load miregex_table fail\n");
return AUL_R_ERROR;
}
while (miregex_tbl) {
if (strcmp(miregex_tbl->mimetype, mimetype) == 0) {
founded = miregex_tbl->desc;
_D("mimetype %s => desc %s\n", mimetype, founded);
break;
}
miregex_tbl = miregex_tbl->next;
}
if (founded != NULL)
snprintf(desc, len, "%s", founded);
else {
/* TODO : should to try to extract from
share mime info's comment */
return AUL_R_ERROR;
}
return AUL_R_OK;
}
SLPAPI int aul_get_mime_extension(const char *mimetype, char *ext, int len)
{
const char **extlist;
int totlen = 0;
const char *unaliased_mimetype;
if (mimetype == NULL || ext == NULL || len <= 0)
return AUL_R_EINVAL;
unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
if (unaliased_mimetype == NULL)
return AUL_R_ERROR;
extlist = xdg_mime_get_file_names_from_mime_type(unaliased_mimetype);
if (extlist == NULL)
return AUL_R_ERROR;
if (extlist[0] == NULL)
return AUL_R_ERROR;
ext[0] = 0;
while (*extlist != NULL) {
if (*(extlist + 1) == NULL) {
snprintf(&ext[totlen], len - totlen, "%s", *extlist);
break;
} else {
snprintf(&ext[totlen], len - totlen, "%s,", *extlist);
if (strlen(*extlist) > len - totlen - 1)
break;
totlen += strlen(*extlist) + 1;
extlist++;
}
}
return AUL_R_OK;
}
SLPAPI int aul_get_mime_icon(const char *mimetype, char *iconname, int len)
{
const char *icon;
const char *unaliased_mimetype;
if (mimetype == NULL || iconname == NULL || len <= 0)
return AUL_R_EINVAL;
unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
if (unaliased_mimetype == NULL)
return AUL_R_ERROR;
icon = xdg_mime_get_icon(unaliased_mimetype);
if (icon == NULL)
icon = xdg_mime_get_generic_icon(unaliased_mimetype);
if (icon != NULL) {
snprintf(iconname, len, "%s", icon);
return AUL_R_OK;
} else
return AUL_R_ERROR;
}
SLPAPI int aul_get_mime_from_file(const char *filename, char *mimetype, int len)
{
const char *mime;
if (filename == NULL)
return AUL_R_EINVAL;
if (access(filename, F_OK) != 0)
return AUL_R_EINVAL;
mime = xdg_mime_get_mime_type_for_file(filename, 0);
if (strcmp(mime, "application/octet-stream") == 0) {
mime = xdg_mime_get_mime_type_from_file_name(filename);
}
snprintf(mimetype, len, "%s", mime);
return AUL_R_OK;
}
SLPAPI int aul_set_defapp_with_mime(const char *mimetype, const char *defapp)
{
const char *unaliased_mimetype;
if (mimetype == NULL || defapp == NULL) {
_E("invalid arg");
return AUL_R_EINVAL;
}
unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
if (unaliased_mimetype == NULL)
return AUL_R_ERROR;
if (mida_add_app(unaliased_mimetype, defapp) < 0) {
_E("fail to add: mimtype-%s and defapp-%s", unaliased_mimetype,
defapp);
return AUL_R_ERROR;
}
return AUL_R_OK;
}
static ail_cb_ret_e __defapp_with_mime_func(
const ail_appinfo_h appinfo, void *user_data)
{
char **package = (char **)user_data;
char *str;
ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &str);
_D("defapp from desktop = %s", str);
*package = strdup(str);
return AIL_CB_RET_CANCEL; /*return AIL_CB_RET_CONTINUE;*/
}
static int get_defapp_from_desktop(const char *mimetype, char *defapp, int len)
{
ail_filter_h filter;
ail_error_e ret;
int pkg_count = 0;
char *tmp = NULL;
ret = ail_filter_new(&filter);
if (ret != AIL_ERROR_OK)
return -1;
ret = ail_filter_add_str(filter, AIL_PROP_MIMETYPE_STR, mimetype);
if (ret != AIL_ERROR_OK) {
ret = -1;
goto out;
}
ail_filter_count_appinfo(filter, &pkg_count);
if (pkg_count == 1) {
ail_filter_list_appinfo_foreach(filter,
__defapp_with_mime_func, (void *)&tmp);
if(tmp) {
strncpy(defapp,tmp,len);
_D("defapp from desktop = %s", defapp);
aul_set_defapp_with_mime(mimetype, defapp);
ret = 0;
free(tmp);
}
} else
ret = -1;
out:
ail_filter_destroy(filter);
return ret;
}
SLPAPI int aul_get_defapp_from_mime(const char *mimetype, char *defapp, int len)
{
char *res;
const char *unaliased_mimetype;
if (mimetype == NULL || defapp == NULL || len <= 0)
return AUL_R_EINVAL;
unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
if (unaliased_mimetype == NULL)
return AUL_R_ERROR;
/* search mida db*/
if ((res = mida_get_app(unaliased_mimetype)) != NULL) {
snprintf(defapp, len, "%s", res);
free(res);
_D("Found %s for %s from mime db", defapp, unaliased_mimetype);
return AUL_R_OK;
}
if (get_defapp_from_desktop(unaliased_mimetype, defapp, len) != 0)
return AUL_R_ERROR;
else
return AUL_R_OK;
}
static int _aul_get_defapp_from_mime(const char *mimetype, char *unaliased,
char *defapp, int len_unaliased,
int len_defapp)
{
char *res;
const char *unaliased_mimetype;
if (mimetype == NULL || unaliased == NULL || len_unaliased <= 0
|| defapp == NULL || len_defapp <= 0)
return AUL_R_EINVAL;
unaliased_mimetype = xdg_mime_unalias_mime_type(mimetype);
if (unaliased_mimetype == NULL)
return AUL_R_ERROR;
snprintf(unaliased, len_unaliased, "%s", unaliased_mimetype);
/* search mida db*/
if ((res = mida_get_app(unaliased_mimetype)) != NULL) {
snprintf(defapp, len_defapp, "%s", res);
free(res);
_D("Found %s for %s from mime db", defapp, unaliased_mimetype);
return AUL_R_OK;
}
if (get_defapp_from_desktop(unaliased_mimetype, defapp, len_defapp) < 0)
return AUL_R_ERROR;
else
return AUL_R_OK;
}
static int __launch_with_defapp(const char *mime_type, const char *mime_content)
{
ail_appinfo_h handle;
ail_error_e ail_ret;
char defapp[MAX_LOCAL_BUFSZ];
char unaliased_mime_type[MAX_LOCAL_BUFSZ];
bundle *kb = NULL;
int ret = AUL_R_ERROR;
kb = bundle_create();
if (NULL == kb) {
_E("bundle creation fail");
return ret;
}
bundle_add(kb, AUL_K_MIME_TYPE, mime_type);
bundle_add(kb, AUL_K_MIME_CONTENT, mime_content);
retry:
if (_aul_get_defapp_from_mime
(mime_type, unaliased_mime_type, defapp,
sizeof(unaliased_mime_type), sizeof(defapp)) < 0) {
_D("mimetype : %s, unaliased mimetype : %s, mime_content : %s,"
" no default app", mime_type,
unaliased_mime_type, mime_content);
bundle_add(kb, AUL_K_UNALIASED_MIME_TYPE, unaliased_mime_type);
ret = aul_launch_app(MIME_APP_SELECTOR, kb);
/* TODO: When launching MIME APP SELECTOR, what should
be the return value? */
/* Currently, it returns 0 if the app selector is launched */
if (ret > 0)
ret = 0;
} else {
ail_ret = ail_package_get_appinfo(defapp, &handle);
if (ail_ret == AIL_ERROR_OK) {
ail_package_destroy_appinfo(handle);
_D("mimetype : %s, unaliased mimetype : %s, "
"mime_content : %s, defapp : %s", mime_type,
unaliased_mime_type,
mime_content, defapp);
bundle_add(kb, AUL_K_UNALIASED_MIME_TYPE,
unaliased_mime_type);
ret = aul_launch_app(defapp, kb);
} else if (ail_ret == AIL_ERROR_NO_DATA) {
_D("defapp %s for mimetype : %s, mime_content : %s "
"does NOT exist", defapp,
mime_type, mime_content);
mida_delete_with_pkgname(defapp);
ail_package_destroy_appinfo(handle);
goto retry;
} else {
_E("ail_package_get_appinfo with %s failed", defapp);
if (kb) {
bundle_free(kb);
kb = NULL;
}
return ret;
}
}
bundle_free(kb);
return ret;
}
SLPAPI int aul_open_content(const char *content)
{
int ret;
char mime[MAX_LOCAL_BUFSZ];
if ((ret = aul_get_mime_from_content(content, mime, sizeof(mime))) < 0)
return ret;
return __launch_with_defapp(mime, content);
}
SLPAPI int aul_open_file_with_mimetype(const char *filename,
const char *mimetype)
{
if (mimetype == NULL)
return AUL_R_EINVAL;
return __launch_with_defapp(mimetype, filename);
}
SLPAPI int aul_open_file(const char *filename)
{
int ret;
char mime[MAX_LOCAL_BUFSZ];
if ((ret = aul_get_mime_from_file(filename, mime, sizeof(mime))) < 0)
return ret;
return __launch_with_defapp(mime, filename);
}
|