Apply to insert backgroud_category value in db.
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_appinfo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <dlfcn.h>
9
10 #include <sqlite3.h>
11 #include <glib.h>
12
13 #include "pkgmgr-info.h"
14 #include "pkgmgrinfo_debug.h"
15 #include "pkgmgrinfo_private.h"
16 #include "pkgmgr_parser.h"
17
18 static bool _get_bool_value(const char *str)
19 {
20         if (str == NULL)
21                 return false;
22         else if (!strcasecmp(str, "true"))
23                 return true;
24         else
25                 return false;
26 }
27
28 static void __cleanup_appinfo(pkgmgr_appinfo_x *data)
29 {
30         pkgmgr_appinfo_x *info = data;
31         pkgmgr_appinfo_x *tmp;
32
33         while (info != NULL) {
34                 tmp = info->next;
35                 if (info->package)
36                         free((void *)info->package);
37                 if (info->locale)
38                         free((void *)info->locale);
39
40                 pkgmgrinfo_basic_free_application(info->app_info);
41                 free((void *)info);
42                 info = tmp;
43         }
44         return;
45 }
46
47 static char *_get_filtered_query(const char *query_raw,
48                 pkgmgrinfo_filter_x *filter)
49 {
50         char buf[MAX_QUERY_LEN] = { 0, };
51         char *condition;
52         size_t len;
53         GSList *list;
54         GSList *head = NULL;
55
56         if (filter)
57                 head = filter->list;
58
59         strncat(buf, query_raw, MAX_QUERY_LEN - 1);
60         len = strlen(buf);
61         for (list = head; list; list = list->next) {
62                 /* TODO: revise condition getter function */
63                 __get_filter_condition(list->data, &condition);
64                 if (condition == NULL)
65                         continue;
66                 if (buf[strlen(query_raw)] == '\0') {
67                         len += strlen(" WHERE ");
68                         strncat(buf, " WHERE ", MAX_QUERY_LEN - len - 1);
69                 } else {
70                         len += strlen(" AND ");
71                         strncat(buf, " AND ", MAX_QUERY_LEN -len - 1);
72                 }
73                 len += strlen(condition);
74                 strncat(buf, condition, sizeof(buf) - len - 1);
75                 free(condition);
76                 condition = NULL;
77         }
78
79         return strdup(buf);
80 }
81
82 static gint __list_strcmp(gconstpointer a, gconstpointer b)
83 {
84         return strcmp((char *)a, (char *)b);
85 }
86
87 static gint _appinfo_get_list(sqlite3 *db, const char *locale,
88                 pkgmgrinfo_filter_x *filter, GList **list)
89 {
90         static const char query_raw[] =
91                 "SELECT DISTINCT package_app_info.app_id FROM package_app_info"
92                 " LEFT OUTER JOIN package_app_localized_info"
93                 "  ON package_app_info.app_id=package_app_localized_info.app_id"
94                 "  AND package_app_localized_info.app_locale=%Q"
95                 " LEFT OUTER JOIN package_app_app_category"
96                 "  ON package_app_info.app_id=package_app_app_category.app_id"
97                 " LEFT OUTER JOIN package_app_app_control"
98                 "  ON package_app_info.app_id=package_app_app_control.app_id"
99                 " LEFT OUTER JOIN package_app_app_metadata"
100                 "  ON package_app_info.app_id=package_app_app_metadata.app_id ";
101         int ret;
102         char *query;
103         char *query_localized;
104         sqlite3_stmt *stmt;
105         char *appid = NULL;
106
107         query = _get_filtered_query(query_raw, filter);
108         if (query == NULL)
109                 return PMINFO_R_ERROR;
110         query_localized = sqlite3_mprintf(query, locale);
111         free(query);
112         if (query_localized == NULL)
113                 return PMINFO_R_ERROR;
114
115         ret = sqlite3_prepare_v2(db, query_localized,
116                         strlen(query_localized), &stmt, NULL);
117         sqlite3_free(query_localized);
118         if (ret != SQLITE_OK) {
119                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
120                 return PMINFO_R_ERROR;
121         }
122
123         while (sqlite3_step(stmt) == SQLITE_ROW) {
124                 _save_column_str(stmt, 0, (const char **)&appid);
125                 if (appid != NULL)
126                         *list = g_list_insert_sorted(*list, appid,
127                                         __list_strcmp);
128         }
129
130         sqlite3_finalize(stmt);
131
132         return PMINFO_R_OK;
133 }
134
135 static int _appinfo_get_filtered_list(pkgmgrinfo_filter_x *filter, uid_t uid,
136                 GList **list)
137 {
138         int ret;
139         sqlite3 *db;
140         const char *dbpath;
141         char *locale;
142         GList *tmp;
143         GList *tmp2;
144
145         locale = _get_system_locale();
146         if (locale == NULL)
147                 return PMINFO_R_ERROR;
148
149         dbpath = getUserPkgParserDBPathUID(uid);
150         if (dbpath == NULL) {
151                 free(locale);
152                 return PMINFO_R_ERROR;
153         }
154
155         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
156         if (ret != SQLITE_OK) {
157                 _LOGE("failed to open db: %d", ret);
158                 free(locale);
159                 return PMINFO_R_ERROR;
160         }
161
162         if (_appinfo_get_list(db, locale, filter, list)) {
163                 free(locale);
164                 sqlite3_close_v2(db);
165                 return PMINFO_R_ERROR;
166         }
167         sqlite3_close_v2(db);
168
169         if (uid == GLOBAL_USER) {
170                 free(locale);
171                 return PMINFO_R_OK;
172         }
173
174         /* search again from global */
175         dbpath = getUserPkgParserDBPathUID(GLOBAL_USER);
176         if (dbpath == NULL) {
177                 free(locale);
178                 return PMINFO_R_ERROR;
179         }
180
181         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
182         if (ret != SQLITE_OK) {
183                 _LOGE("failed to open db: %d", ret);
184                 free(locale);
185                 return PMINFO_R_ERROR;
186         }
187
188         if (_appinfo_get_list(db, locale, filter, list)) {
189                 free(locale);
190                 sqlite3_close_v2(db);
191                 return PMINFO_R_ERROR;
192         }
193         sqlite3_close_v2(db);
194
195         /* remove duplicate element:
196          * since the list is sorted, we can remove duplicates in linear time
197          */
198         for (tmp = *list, tmp2 = g_list_next(tmp); tmp;
199                         tmp = tmp2, tmp2 = g_list_next(tmp)) {
200                 if (tmp->prev == NULL || tmp->data == NULL)
201                         continue;
202                 if (strcmp((const char *)tmp->prev->data,
203                                         (const char *)tmp->data) == 0)
204                         *list = g_list_delete_link(*list, tmp);
205         }
206
207         free(locale);
208
209         return PMINFO_R_OK;
210 }
211
212 static int _appinfo_get_label(sqlite3 *db, const char *appid,
213                 const char *locale, GList **label)
214 {
215         static const char query_raw[] =
216                 "SELECT app_label, app_locale "
217                 "FROM package_app_localized_info "
218                 "WHERE app_id=%Q AND app_locale IN (%Q, %Q)";
219         int ret;
220         char *query;
221         sqlite3_stmt *stmt;
222         int idx;
223         label_x *info;
224
225         query = sqlite3_mprintf(query_raw, appid, locale, DEFAULT_LOCALE);
226         if (query == NULL) {
227                 LOGE("out of memory");
228                 return PMINFO_R_ERROR;
229         }
230
231         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
232         sqlite3_free(query);
233         if (ret != SQLITE_OK) {
234                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
235                 return PMINFO_R_ERROR;
236         }
237
238         while (sqlite3_step(stmt) == SQLITE_ROW) {
239                 info = calloc(1, sizeof(label_x));
240                 if (info == NULL) {
241                         LOGE("out of memory");
242                         sqlite3_finalize(stmt);
243                         return PMINFO_R_ERROR;
244                 }
245                 idx = 0;
246                 _save_column_str(stmt, idx++, &info->text);
247                 _save_column_str(stmt, idx++, &info->lang);
248                 *label = g_list_append(*label, info);
249         }
250
251         sqlite3_finalize(stmt);
252
253         return PMINFO_R_OK;
254 }
255
256 static int _appinfo_get_icon(sqlite3 *db, const char *appid, const char *locale,
257                 GList **icon)
258 {
259         static const char query_raw[] =
260                 "SELECT app_icon, app_locale "
261                 "FROM package_app_localized_info "
262                 "WHERE app_id=%Q AND app_locale IN (%Q, %Q)";
263         int ret;
264         char *query;
265         sqlite3_stmt *stmt;
266         int idx;
267         icon_x *info;
268
269         query = sqlite3_mprintf(query_raw, appid, locale, DEFAULT_LOCALE);
270         if (query == NULL) {
271                 LOGE("out of memory");
272                 return PMINFO_R_ERROR;
273         }
274
275         ret = sqlite3_prepare_v2(db, query, strlen(query),
276                         &stmt, NULL);
277         sqlite3_free(query);
278         if (ret != SQLITE_OK) {
279                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
280                 return PMINFO_R_ERROR;
281         }
282
283         while (sqlite3_step(stmt) == SQLITE_ROW) {
284                 info = calloc(1, sizeof(icon_x));
285                 if (info == NULL) {
286                         LOGE("out of memory");
287                         sqlite3_finalize(stmt);
288                         return PMINFO_R_ERROR;
289                 }
290                 idx = 0;
291                 _save_column_str(stmt, idx++, &info->text);
292                 _save_column_str(stmt, idx++, &info->lang);
293                 *icon = g_list_append(*icon, info);
294         }
295
296         sqlite3_finalize(stmt);
297
298         return PMINFO_R_OK;
299 }
300
301 static int _appinfo_get_category(sqlite3 *db, const char *appid,
302                 GList **category)
303 {
304         static const char query_raw[] =
305                 "SELECT category FROM package_app_app_category WHERE app_id=%Q";
306         int ret;
307         char *query;
308         sqlite3_stmt *stmt;
309         const char *val;
310
311         query = sqlite3_mprintf(query_raw, appid);
312         if (query == NULL) {
313                 LOGE("out of memory");
314                 return PMINFO_R_ERROR;
315         }
316
317         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
318         sqlite3_free(query);
319         if (ret != SQLITE_OK) {
320                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
321                 return PMINFO_R_ERROR;
322         }
323
324         while (sqlite3_step(stmt) == SQLITE_ROW) {
325                 val = NULL;
326                 _save_column_str(stmt, 0, &val);
327                 if (val)
328                         *category = g_list_append(*category, (gpointer)val);
329         }
330
331         sqlite3_finalize(stmt);
332
333         return PMINFO_R_OK;
334 }
335
336 static void __parse_appcontrol(GList **appcontrol, char *appcontrol_str)
337 {
338         char *dup;
339         char *token;
340         char *ptr = NULL;
341         appcontrol_x *ac;
342
343         if (appcontrol_str == NULL)
344                 return;
345
346         dup = strdup(appcontrol_str);
347         do {
348                 ac = calloc(1, sizeof(appcontrol_x));
349                 if (ac == NULL) {
350                         _LOGE("out of memory");
351                         break;
352                 }
353                 token = strtok_r(dup, "|", &ptr);
354                 if (token && strcmp(token, "NULL"))
355                         ac->operation = strdup(token);
356                 token = strtok_r(NULL, "|", &ptr);
357                 if (token && strcmp(token, "NULL"))
358                         ac->uri = strdup(token);
359                 token = strtok_r(NULL, "|", &ptr);
360                 if (token && strcmp(token, "NULL"))
361                         ac->mime = strdup(token);
362                 *appcontrol = g_list_append(*appcontrol, ac);
363         } while ((token = strtok_r(NULL, ";", &ptr)));
364
365         free(dup);
366 }
367
368 static int _appinfo_get_app_control(sqlite3 *db, const char *appid,
369                 GList **appcontrol)
370 {
371         static const char query_raw[] =
372                 "SELECT app_control FROM package_app_app_control "
373                 "WHERE app_id=%Q";
374         int ret;
375         char *query;
376         sqlite3_stmt *stmt;
377         char *str;
378
379         query = sqlite3_mprintf(query_raw, appid);
380         if (query == NULL) {
381                 LOGE("out of memory");
382                 return PMINFO_R_ERROR;
383         }
384
385         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
386         sqlite3_free(query);
387         if (ret != SQLITE_OK) {
388                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
389                 return PMINFO_R_ERROR;
390         }
391
392         while (sqlite3_step(stmt) == SQLITE_ROW) {
393                 str = NULL;
394                 _save_column_str(stmt, 0, (const char **)&str);
395                 /* TODO: revise */
396                 __parse_appcontrol(appcontrol, str);
397                 free(str);
398         }
399
400         sqlite3_finalize(stmt);
401
402         return PMINFO_R_OK;
403 }
404
405 static int _appinfo_get_data_control(sqlite3 *db, const char *appid,
406                 GList **datacontrol)
407 {
408         static const char query_raw[] =
409                 "SELECT providerid, access, type "
410                 "FROM package_app_data_control WHERE app_id=%Q";
411         int ret;
412         char *query;
413         sqlite3_stmt *stmt;
414         int idx;
415         datacontrol_x *info;
416
417         query = sqlite3_mprintf(query_raw, appid);
418         if (query == NULL) {
419                 LOGE("out of memory");
420                 return PMINFO_R_ERROR;
421         }
422
423         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
424         sqlite3_free(query);
425         if (ret != SQLITE_OK) {
426                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
427                 return PMINFO_R_ERROR;
428         }
429
430         while (sqlite3_step(stmt) == SQLITE_ROW) {
431                 info = calloc(1, sizeof(datacontrol_x));
432                 if (info == NULL) {
433                         LOGE("out of memory");
434                         sqlite3_finalize(stmt);
435                         return PMINFO_R_ERROR;
436                 }
437                 idx = 0;
438                 _save_column_str(stmt, idx++, &info->providerid);
439                 _save_column_str(stmt, idx++, &info->access);
440                 _save_column_str(stmt, idx++, &info->type);
441                 *datacontrol = g_list_append(*datacontrol, info);
442         }
443
444         sqlite3_finalize(stmt);
445
446         return PMINFO_R_OK;
447 }
448
449 static int _appinfo_get_metadata(sqlite3 *db, const char *appid,
450                 GList **metadata)
451 {
452         static const char query_raw[] =
453                 "SELECT md_key, md_value "
454                 "FROM package_app_app_metadata WHERE app_id=%Q";
455         int ret;
456         char *query;
457         sqlite3_stmt *stmt;
458         int idx;
459         metadata_x *info;
460
461         query = sqlite3_mprintf(query_raw, appid);
462         if (query == NULL) {
463                 LOGE("out of memory");
464                 return PMINFO_R_ERROR;
465         }
466
467         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
468         sqlite3_free(query);
469         if (ret != SQLITE_OK) {
470                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
471                 return PMINFO_R_ERROR;
472         }
473
474         while (sqlite3_step(stmt) == SQLITE_ROW) {
475                 info = calloc(1, sizeof(metadata_x));
476                 if (info == NULL) {
477                         LOGE("out of memory");
478                         sqlite3_finalize(stmt);
479                         return PMINFO_R_ERROR;
480                 }
481                 idx = 0;
482                 _save_column_str(stmt, idx++, &info->key);
483                 _save_column_str(stmt, idx++, &info->value);
484                 *metadata = g_list_append(*metadata, info);
485         }
486
487         sqlite3_finalize(stmt);
488
489         return PMINFO_R_OK;
490
491 }
492
493 static GList *__get_background_category(char *value)
494 {
495         GList *category_list = NULL;
496         int convert_value = 0;
497         if (!value || strlen(value) == 0)
498                 return NULL;
499
500         convert_value = atoi(value);
501         if (convert_value < 0)
502                 return NULL;
503
504         if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
505                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
506         else
507                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
508
509         if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
510                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_MEDIA_STR));
511
512         if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
513                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
514
515         if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
516                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_BGNETWORK_STR));
517
518         if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
519                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_LOCATION_STR));
520
521         if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
522                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_SENSOR_STR));
523
524         if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
525                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_IOTCOMM_STR));
526
527         if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
528                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_SYSTEM));
529
530         return category_list;
531
532 }
533
534 static int _appinfo_get_application(sqlite3 *db, const char *appid,
535                 const char *locale, application_x **application)
536 {
537         static const char query_raw[] =
538                 "SELECT app_id, app_component, app_exec, app_nodisplay, "
539                 "app_type, app_onboot, app_multiple, app_autorestart, "
540                 "app_taskmanage, app_enabled, app_hwacceleration, "
541                 "app_screenreader, app_mainapp, app_recentimage, "
542                 "app_launchcondition, app_indicatordisplay, app_portraitimg, "
543                 "app_landscapeimg, app_guestmodevisibility, "
544                 "app_permissiontype, app_preload, app_submode, "
545                 "app_submode_mainid, app_launch_mode, app_ui_gadget, "
546                 "app_support_disable, "
547                 "component_type, package, app_process_pool, app_installed_storage, "
548                 "app_background_category "
549                 "FROM package_app_info WHERE app_id=%Q";
550         int ret;
551         char *query;
552         sqlite3_stmt *stmt;
553         int idx;
554         application_x *info;
555         char *bg_category_str = NULL;
556
557         query = sqlite3_mprintf(query_raw, appid);
558         if (query == NULL) {
559                 LOGE("out of memory");
560                 return PMINFO_R_ERROR;
561         }
562
563         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
564         sqlite3_free(query);
565         if (ret != SQLITE_OK) {
566                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
567                 return PMINFO_R_ERROR;
568         }
569
570         ret = sqlite3_step(stmt);
571         if (ret == SQLITE_DONE) {
572                 sqlite3_finalize(stmt);
573                 return PMINFO_R_ENOENT;
574         } else if (ret != SQLITE_ROW) {
575                 LOGE("step failed: %s", sqlite3_errmsg(db));
576                 sqlite3_finalize(stmt);
577                 return PMINFO_R_ERROR;
578         }
579
580         info = calloc(1, sizeof(application_x));
581         if (info == NULL) {
582                 LOGE("out of memory");
583                 sqlite3_finalize(stmt);
584                 return PMINFO_R_ERROR;
585         }
586         idx = 0;
587         _save_column_str(stmt, idx++, &info->appid);
588         _save_column_str(stmt, idx++, &info->component);
589         _save_column_str(stmt, idx++, &info->exec);
590         _save_column_str(stmt, idx++, &info->nodisplay);
591         _save_column_str(stmt, idx++, &info->type);
592         _save_column_str(stmt, idx++, &info->onboot);
593         _save_column_str(stmt, idx++, &info->multiple);
594         _save_column_str(stmt, idx++, &info->autorestart);
595         _save_column_str(stmt, idx++, &info->taskmanage);
596         _save_column_str(stmt, idx++, &info->enabled);
597         _save_column_str(stmt, idx++, &info->hwacceleration);
598         _save_column_str(stmt, idx++, &info->screenreader);
599         _save_column_str(stmt, idx++, &info->mainapp);
600         _save_column_str(stmt, idx++, &info->recentimage);
601         _save_column_str(stmt, idx++, &info->launchcondition);
602         _save_column_str(stmt, idx++, &info->indicatordisplay);
603         _save_column_str(stmt, idx++, &info->portraitimg);
604         _save_column_str(stmt, idx++, &info->landscapeimg);
605         _save_column_str(stmt, idx++, &info->guestmode_visibility);
606         _save_column_str(stmt, idx++, &info->permission_type);
607         _save_column_str(stmt, idx++, &info->preload);
608         _save_column_str(stmt, idx++, &info->submode);
609         _save_column_str(stmt, idx++, &info->submode_mainid);
610         _save_column_str(stmt, idx++, &info->launch_mode);
611         _save_column_str(stmt, idx++, &info->ui_gadget);
612         _save_column_str(stmt, idx++, &info->support_disable);
613         _save_column_str(stmt, idx++, &info->component_type);
614         _save_column_str(stmt, idx++, &info->package);
615         _save_column_str(stmt, idx++, &info->process_pool);
616         _save_column_str(stmt, idx++, &info->installed_storage);
617         _save_column_str(stmt, idx++, &bg_category_str);
618
619         info->background_category = __get_background_category(bg_category_str);
620
621         if (_appinfo_get_label(db, info->appid, locale, &info->label)) {
622                 pkgmgrinfo_basic_free_application(info);
623                 sqlite3_finalize(stmt);
624                 return PMINFO_R_ERROR;
625         }
626
627         if (_appinfo_get_icon(db, info->appid, locale, &info->icon)) {
628                 pkgmgrinfo_basic_free_application(info);
629                 sqlite3_finalize(stmt);
630                 return PMINFO_R_ERROR;
631         }
632
633         if (_appinfo_get_category(db, info->appid, &info->category)) {
634                 pkgmgrinfo_basic_free_application(info);
635                 sqlite3_finalize(stmt);
636                 return PMINFO_R_ERROR;
637         }
638
639         if (_appinfo_get_app_control(db, info->appid, &info->appcontrol)) {
640                 pkgmgrinfo_basic_free_application(info);
641                 sqlite3_finalize(stmt);
642                 return PMINFO_R_ERROR;
643         }
644
645         if (_appinfo_get_data_control(db, info->appid, &info->datacontrol)) {
646                 pkgmgrinfo_basic_free_application(info);
647                 sqlite3_finalize(stmt);
648                 return PMINFO_R_ERROR;
649         }
650
651         if (_appinfo_get_metadata(db, info->appid, &info->metadata)) {
652                 pkgmgrinfo_basic_free_application(info);
653                 sqlite3_finalize(stmt);
654                 return PMINFO_R_ERROR;
655         }
656
657         *application = info;
658
659         sqlite3_finalize(stmt);
660
661         return PMINFO_R_OK;
662 }
663
664 static int _appinfo_get_appinfo(const char *appid, uid_t uid,
665                 pkgmgr_appinfo_x **appinfo)
666 {
667         int ret;
668         sqlite3 *db;
669         const char *dbpath;
670         char *locale;
671         pkgmgr_appinfo_x *info;
672
673         dbpath = getUserPkgParserDBPathUID(uid);
674         if (dbpath == NULL)
675                 return PMINFO_R_ERROR;
676
677         locale = _get_system_locale();
678         if (locale == NULL)
679                 return PMINFO_R_ERROR;
680
681         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
682         if (ret != SQLITE_OK) {
683                 _LOGE("failed to open db: %d", ret);
684                 free(locale);
685                 return PMINFO_R_ERROR;
686         }
687
688         info = calloc(1, sizeof(pkgmgr_appinfo_x));
689         if (info == NULL) {
690                 _LOGE("out of memory");
691                 free(locale);
692                 sqlite3_close_v2(db);
693                 return PMINFO_R_ERROR;
694         }
695
696         ret = _appinfo_get_application(db, appid, locale, &info->app_info);
697         if (ret != PMINFO_R_OK) {
698                 free(info);
699                 free(locale);
700                 sqlite3_close_v2(db);
701                 return ret;
702         }
703
704         info->locale = locale;
705         info->package = strdup(info->app_info->package);
706
707         *appinfo = info;
708
709         sqlite3_close_v2(db);
710
711         return ret;
712 }
713
714 API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid,
715                 pkgmgrinfo_appinfo_h *handle)
716 {
717         int ret;
718
719         if (appid == NULL || handle == NULL) {
720                 LOGE("invalid parameter");
721                 return PMINFO_R_EINVAL;
722         }
723
724         ret = _appinfo_get_appinfo(appid, uid, (pkgmgr_appinfo_x **)handle);
725         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
726                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
727                                 (pkgmgr_appinfo_x **)handle);
728
729         if (ret != PMINFO_R_OK)
730                 _LOGE("failed to get appinfo of %s for user %d", appid, uid);
731
732         return ret;
733 }
734
735 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
736 {
737         return pkgmgrinfo_appinfo_get_usr_appinfo(appid, GLOBAL_USER, handle);
738 }
739
740 static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
741                 pkgmgrinfo_filter_x *filter, pkgmgrinfo_app_list_cb app_list_cb,
742                 void *user_data)
743 {
744         int ret;
745         pkgmgr_appinfo_x *info;
746         GList *list = NULL;
747         GList *tmp;
748         char *appid;
749         int stop = 0;
750
751         ret = _appinfo_get_filtered_list(filter, uid, &list);
752         if (ret != PMINFO_R_OK)
753                 return PMINFO_R_ERROR;
754
755         for (tmp = list; tmp; tmp = tmp->next) {
756                 appid = (char *)tmp->data;
757                 if (stop == 0) {
758                         ret = _appinfo_get_appinfo(appid, uid, &info);
759                         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
760                                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
761                                                 &info);
762                         if (ret != PMINFO_R_OK) {
763                                 free(appid);
764                                 continue;
765                         }
766                         if (app_list_cb(info, user_data) < 0)
767                                 stop = 1;
768                         pkgmgrinfo_appinfo_destroy_appinfo(info);
769                 }
770                 free(appid);
771         }
772
773         g_list_free(list);
774
775         return PMINFO_R_OK;
776 }
777
778 API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle,
779                 pkgmgrinfo_app_component component,
780                 pkgmgrinfo_app_list_cb app_func, void *user_data, uid_t uid)
781 {
782         int ret;
783         pkgmgrinfo_appinfo_filter_h filter;
784         char *pkgid;
785         const char *comp_str = NULL;
786
787         if (handle == NULL || app_func == NULL) {
788                 LOGE("invalied parameter");
789                 return PMINFO_R_EINVAL;
790         }
791
792         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid)) {
793                 LOGE("invalid parameter");
794                 return PMINFO_R_EINVAL;
795         }
796
797         if (pkgmgrinfo_appinfo_filter_create(&filter))
798                 return PMINFO_R_ERROR;
799
800         if (pkgmgrinfo_appinfo_filter_add_string(filter,
801                                 PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid)) {
802                 pkgmgrinfo_appinfo_filter_destroy(filter);
803                 return PMINFO_R_ERROR;
804         }
805
806
807         switch (component) {
808         case PMINFO_UI_APP:
809                 comp_str = PMINFO_APPINFO_UI_APP;
810                 break;
811         case PMINFO_SVC_APP:
812                 comp_str = PMINFO_APPINFO_SVC_APP;
813                 break;
814         default:
815                 break;
816         }
817
818         if (comp_str) {
819                 if (pkgmgrinfo_appinfo_filter_add_string(filter,
820                                         PMINFO_APPINFO_PROP_APP_COMPONENT,
821                                         comp_str)) {
822                         pkgmgrinfo_appinfo_filter_destroy(filter);
823                         return PMINFO_R_ERROR;
824                 }
825         }
826
827         ret = _appinfo_get_filtered_foreach_appinfo(uid, filter, app_func,
828                         user_data);
829
830         pkgmgrinfo_appinfo_filter_destroy(filter);
831
832         return ret;
833 }
834
835 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
836                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
837 {
838         return pkgmgrinfo_appinfo_get_usr_list(handle, component, app_func, user_data, GLOBAL_USER);
839 }
840
841 API int pkgmgrinfo_appinfo_get_usr_install_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
842 {
843         if (app_func == NULL) {
844                 LOGE("invalid parameter");
845                 return PMINFO_R_EINVAL;
846         }
847
848         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
849                         user_data);
850 }
851
852 API int pkgmgrinfo_appinfo_get_install_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
853 {
854         return pkgmgrinfo_appinfo_get_usr_install_list(app_func, GLOBAL_USER, user_data);
855 }
856
857 API int pkgmgrinfo_appinfo_get_usr_installed_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
858 {
859         if (app_func == NULL) {
860                 LOGE("invalid parameter");
861                 return PMINFO_R_EINVAL;
862         }
863
864         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
865                         user_data);
866 }
867
868 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
869 {
870         return pkgmgrinfo_appinfo_get_usr_installed_list(app_func, GLOBAL_USER, user_data);
871 }
872
873 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h handle, char **appid)
874 {
875         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
876
877         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
878         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
879
880         if (info->app_info == NULL || info->app_info->appid == NULL)
881                 return PMINFO_R_ERROR;
882         *appid = (char *)info->app_info->appid;
883
884         return PMINFO_R_OK;
885 }
886
887 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, char **pkg_name)
888 {
889         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
890
891         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
892         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
893
894         if (info->package == NULL)
895                 return PMINFO_R_ERROR;
896
897         *pkg_name = (char *)info->package;
898
899         return PMINFO_R_OK;
900 }
901
902 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h handle, char **pkgid)
903 {
904         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
905
906         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
907         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
908
909         if (info->package == NULL)
910                 return PMINFO_R_ERROR;
911
912         *pkgid = (char *)info->package;
913
914         return PMINFO_R_OK;
915 }
916
917 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, char **exec)
918 {
919         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
920
921         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
922         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
923
924         if (info->app_info == NULL || info->app_info->exec == NULL)
925                 return PMINFO_R_ERROR;
926         *exec = (char *)info->app_info->exec;
927
928         return PMINFO_R_OK;
929 }
930
931
932 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, char **icon)
933 {
934         const char *locale;
935         icon_x *ptr;
936         GList *tmp;
937         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
938
939         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
940         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
941
942         locale = info->locale;
943         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
944
945         if (info->app_info == NULL)
946                 return PMINFO_R_ERROR;
947
948         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
949                 ptr = (icon_x *)tmp->data;
950                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
951                                 !strcasecmp(ptr->text, "(null)") ||
952                                 strcmp(ptr->lang, locale))
953                         continue;
954                 *icon = (char *)ptr->text;
955                 return PMINFO_R_OK;
956         }
957
958         locale = DEFAULT_LOCALE;
959         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
960                 ptr = (icon_x *)tmp->data;
961                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
962                                 strcmp(ptr->lang, locale))
963                         continue;
964                 *icon = (char *)ptr->text;
965                 return PMINFO_R_OK;
966         }
967
968         return PMINFO_R_ERROR;
969 }
970
971
972 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, char **label)
973 {
974         const char *locale;
975         label_x *ptr;
976         GList *tmp;
977         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
978
979         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
980         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
981
982         locale = info->locale;
983         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
984
985         if (info->app_info == NULL)
986                 return PMINFO_R_ERROR;
987
988         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
989                 ptr = (label_x *)tmp->data;
990                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
991                                 strcmp(ptr->lang, locale))
992                         continue;
993                 *label = (char *)ptr->text;
994                 return PMINFO_R_OK;
995         }
996
997         locale = DEFAULT_LOCALE;
998         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
999                 ptr = (label_x *)tmp->data;
1000                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1001                                 strcmp(ptr->lang, locale))
1002                         continue;
1003                 *label = (char *)ptr->text;
1004                 return PMINFO_R_OK;
1005         }
1006
1007         return PMINFO_R_ERROR;
1008 }
1009
1010 static char *_get_localed_label(const char *appid, const char *locale, uid_t uid)
1011 {
1012         char *result = NULL;
1013         char *query = NULL;
1014         sqlite3_stmt *stmt = NULL;
1015         sqlite3 *db = NULL;
1016         char *val;
1017         const char *manifest_db;
1018
1019         manifest_db = getUserPkgParserDBPathUID(uid);
1020         if (manifest_db == NULL) {
1021                 _LOGE("Failed to get manifest db path");
1022                 goto err;
1023         }
1024
1025         if (sqlite3_open_v2(manifest_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
1026                 _LOGE("DB open fail\n");
1027                 goto err;
1028         }
1029
1030         query = sqlite3_mprintf("select app_label from package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
1031         if (query == NULL) {
1032                 _LOGE("Out of memory");
1033                 goto err;
1034         }
1035
1036         if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) {
1037                 _LOGE("prepare_v2 fail\n");
1038                 goto err;
1039         }
1040
1041         if (sqlite3_step(stmt) == SQLITE_ROW) {
1042                 val = (char *)sqlite3_column_text(stmt, 0);
1043                 if (val != NULL)
1044                         result = strdup(val);
1045         }
1046
1047 err:
1048         sqlite3_finalize(stmt);
1049         sqlite3_free(query);
1050         sqlite3_close(db);
1051
1052         return result;
1053 }
1054
1055 API int pkgmgrinfo_appinfo_usr_get_localed_label(const char *appid, const char *locale, uid_t uid, char **label)
1056 {
1057         char *val;
1058
1059         retvm_if(appid == NULL || locale == NULL || label == NULL, PMINFO_R_EINVAL, "Argument is NULL");
1060
1061         val = _get_localed_label(appid, locale, uid);
1062         if (val == NULL)
1063                 val = _get_localed_label(appid, DEFAULT_LOCALE, uid);
1064
1065         if (val == NULL)
1066                 return PMINFO_R_ERROR;
1067
1068         *label = val;
1069
1070         return PMINFO_R_OK;
1071 }
1072
1073 API int pkgmgrinfo_appinfo_get_localed_label(const char *appid, const char *locale, char **label)
1074 {
1075         return pkgmgrinfo_appinfo_usr_get_localed_label(appid, locale, GLOBAL_USER, label);
1076 }
1077
1078 static pkgmgrinfo_app_component __appcomponent_convert(const char *comp)
1079 {
1080         if ( strcasecmp(comp, "uiapp") == 0)
1081                 return PMINFO_UI_APP;
1082         else if ( strcasecmp(comp, "svcapp") == 0)
1083                 return PMINFO_SVC_APP;
1084         else
1085                 return -1;
1086 }
1087
1088 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_component *component)
1089 {
1090         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1091         int comp;
1092
1093         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1094         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1095
1096         if (info->app_info == NULL)
1097                 return PMINFO_R_ERROR;
1098
1099         comp = __appcomponent_convert(info->app_info->component);
1100         if (comp < 0)
1101                 return PMINFO_R_ERROR;
1102
1103         *component = comp;
1104
1105         return PMINFO_R_OK;
1106 }
1107
1108 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, char **app_type)
1109 {
1110         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1111
1112         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1113         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1114
1115         if (info->app_info == NULL || info->app_info->type == NULL)
1116                 return PMINFO_R_ERROR;
1117         *app_type = (char *)info->app_info->type;
1118
1119         return PMINFO_R_OK;
1120 }
1121
1122 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
1123                                         int *operation_count, char ***operation)
1124 {
1125         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1126         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1127         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1128         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1129         *operation_count = data->operation_count;
1130         *operation = data->operation;
1131         return PMINFO_R_OK;
1132 }
1133
1134 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
1135                                         int *uri_count, char ***uri)
1136 {
1137         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1138         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1139         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1140         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1141         *uri_count = data->uri_count;
1142         *uri = data->uri;
1143         return PMINFO_R_OK;
1144 }
1145
1146 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
1147                                         int *mime_count, char ***mime)
1148 {
1149         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1150         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1151         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1152         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1153         *mime_count = data->mime_count;
1154         *mime = data->mime;
1155         return PMINFO_R_OK;
1156 }
1157
1158 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
1159                                         int *subapp_count, char ***subapp)
1160 {
1161         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1162         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1163         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1164         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1165         *subapp_count = data->subapp_count;
1166         *subapp = data->subapp;
1167         return PMINFO_R_OK;
1168 }
1169
1170 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1171 {
1172         char *val;
1173         icon_x *ptr;
1174         GList *tmp;
1175         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1176
1177         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1178         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1179
1180         if (info->app_info == NULL)
1181                 return PMINFO_R_ERROR;
1182
1183         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1184                 ptr = (icon_x *)tmp->data;
1185                 if (ptr == NULL || ptr->section == NULL)
1186                         continue;
1187
1188                 val = (char *)ptr->section;
1189                 if (val && strcmp(val, "setting") == 0) {
1190                         *icon = (char *)ptr->text;
1191                         return PMINFO_R_OK;
1192                 }
1193         }
1194
1195         return PMINFO_R_ERROR;
1196 }
1197
1198
1199 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1200 {
1201         char *val;
1202         icon_x *ptr;
1203         GList *tmp;
1204         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1205
1206         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1207         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1208
1209         if (info->app_info == NULL)
1210                 return PMINFO_R_ERROR;
1211
1212         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1213                 ptr = (icon_x *)tmp->data;
1214                 if (ptr == NULL || ptr->section == NULL)
1215                         continue;
1216
1217                 val = (char *)ptr->section;
1218                 if (val && strcmp(val, "notification") == 0){
1219                         *icon = (char *)ptr->text;
1220                         return PMINFO_R_OK;
1221                 }
1222         }
1223
1224         return PMINFO_R_ERROR;
1225 }
1226
1227 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_recentimage *type)
1228 {
1229         char *val;
1230         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1231
1232         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1233         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1234
1235         if (info->app_info == NULL || info->app_info->recentimage == NULL)
1236                 return PMINFO_R_ERROR;
1237
1238         val = (char *)info->app_info->recentimage;
1239         if (strcasecmp(val, "capture") == 0)
1240                 *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
1241         else if (strcasecmp(val, "icon") == 0)
1242                 *type = PMINFO_RECENTIMAGE_USE_ICON;
1243         else
1244                 *type = PMINFO_RECENTIMAGE_USE_NOTHING;
1245
1246         return PMINFO_R_OK;
1247 }
1248
1249 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h handle, char **preview_img)
1250 {
1251         char *val;
1252         image_x *ptr;
1253         GList *tmp;
1254         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1255
1256         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1257         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1258
1259         if (info->app_info == NULL)
1260                 return PMINFO_R_ERROR;
1261
1262         for (tmp = info->app_info->image; tmp; tmp = tmp->next) {
1263                 ptr = (image_x *)tmp->data;
1264                 if (ptr == NULL || ptr->section == NULL)
1265                         continue;
1266
1267                 val = (char *)ptr->section;
1268                 if (val && strcmp(val, "preview") == 0) {
1269                         *preview_img = (char *)ptr->text;
1270                         return PMINFO_R_OK;
1271                 }
1272         }
1273
1274         return PMINFO_R_ERROR;
1275 }
1276
1277 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_permission_type *permission)
1278 {
1279         const char *val;
1280         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1281
1282         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1283         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1284
1285         val = info->app_info->permission_type;
1286         if (val == NULL)
1287                 return PMINFO_R_ERROR;
1288
1289         if (strcmp(val, "signature") == 0)
1290                 *permission = PMINFO_PERMISSION_SIGNATURE;
1291         else if (strcmp(val, "privilege") == 0)
1292                 *permission = PMINFO_PERMISSION_PRIVILEGE;
1293         else
1294                 *permission = PMINFO_PERMISSION_NORMAL;
1295
1296         return PMINFO_R_OK;
1297 }
1298
1299 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h handle, char **component_type)
1300 {
1301         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1302
1303         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1304         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1305
1306         if (info->app_info == NULL || info->app_info->component_type == NULL)
1307                 return PMINFO_R_ERROR;
1308
1309         *component_type = (char *)info->app_info->component_type;
1310
1311         return PMINFO_R_OK;
1312 }
1313
1314 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
1315 {
1316         char *val;
1317         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1318
1319         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1320         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1321
1322         if (info->app_info == NULL || info->app_info->hwacceleration == NULL)
1323                 return PMINFO_R_ERROR;
1324
1325         val = (char *)info->app_info->hwacceleration;
1326         if (strcasecmp(val, "not-use-GL") == 0)
1327                 *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
1328         else if (strcasecmp(val, "use-GL") == 0)
1329                 *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
1330         else
1331                 *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
1332
1333         return PMINFO_R_OK;
1334 }
1335
1336 API int pkgmgrinfo_appinfo_get_screenreader(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_screenreader *screenreader)
1337 {
1338         char *val;
1339         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1340
1341         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1342         retvm_if(screenreader == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1343
1344         if (info->app_info == NULL || info->app_info->screenreader == NULL)
1345                 return PMINFO_R_ERROR;
1346
1347         val = (char *)info->app_info->screenreader;
1348         if (strcasecmp(val, "screenreader-off") == 0)
1349                 *screenreader = PMINFO_SCREENREADER_OFF;
1350         else if (strcasecmp(val, "screenreader-on") == 0)
1351                 *screenreader = PMINFO_SCREENREADER_ON;
1352         else
1353                 *screenreader = PMINFO_SCREENREADER_USE_SYSTEM_SETTING;
1354
1355         return PMINFO_R_OK;
1356 }
1357
1358 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h handle, char **portrait_img, char **landscape_img)
1359 {
1360         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1361
1362         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1363         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1364         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1365
1366         if (info->app_info == NULL || info->app_info->portraitimg ||
1367                         info->app_info->landscapeimg == NULL)
1368                 return PMINFO_R_ERROR;
1369
1370         *portrait_img = (char *)info->app_info->portraitimg;
1371         *landscape_img = (char *)info->app_info->landscapeimg;
1372
1373         return PMINFO_R_OK;
1374 }
1375
1376 API int pkgmgrinfo_appinfo_get_effectimage_type(pkgmgrinfo_appinfo_h handle, char **effectimage_type)
1377 {
1378         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1379
1380         if (handle == NULL || effectimage_type == NULL) {
1381                 LOGE("invalid parameter");
1382                 return PMINFO_R_EINVAL;
1383         }
1384
1385         if (info->app_info == NULL || info->app_info->effectimage_type == NULL)
1386                 return PMINFO_R_ERROR;
1387
1388         *effectimage_type = (char *)info->app_info->effectimage_type;
1389
1390         return PMINFO_R_OK;
1391 }
1392
1393 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
1394 {
1395         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1396
1397         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1398         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1399
1400         if (info->app_info == NULL || info->app_info->submode_mainid == NULL)
1401                 return PMINFO_R_ERROR;
1402
1403         *submode_mainid = (char *)info->app_info->submode_mainid;
1404
1405         return PMINFO_R_OK;
1406 }
1407
1408 API int pkgmgrinfo_appinfo_get_installed_storage_location(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_installed_storage *storage)
1409 {
1410         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1411         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1412
1413         if (info->app_info && info->app_info->installed_storage){
1414                  if (strcmp(info->app_info->installed_storage,"installed_internal") == 0)
1415                         *storage = PMINFO_INTERNAL_STORAGE;
1416                  else if (strcmp(info->app_info->installed_storage,"installed_external") == 0)
1417                          *storage = PMINFO_EXTERNAL_STORAGE;
1418                  else
1419                          return PMINFO_R_ERROR;
1420         }else
1421                 return PMINFO_R_ERROR;
1422
1423         return PMINFO_R_OK;
1424 }
1425
1426 API int pkgmgrinfo_appinfo_get_launch_mode(pkgmgrinfo_appinfo_h handle, char **mode)
1427 {
1428         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1429
1430         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1431         retvm_if(mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1432
1433         if (info->app_info->launch_mode == NULL)
1434                 return PMINFO_R_ERROR;
1435
1436         *mode = (char *)(info->app_info->launch_mode);
1437
1438         return PMINFO_R_OK;
1439 }
1440
1441 API int pkgmgrinfo_appinfo_get_alias_appid(pkgmgrinfo_appinfo_h handle, char **alias_appid)
1442 {
1443         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1444
1445         if (handle == NULL || alias_appid == NULL) {
1446                 LOGE("invalid parameter");
1447                 return PMINFO_R_EINVAL;
1448         }
1449
1450         if (info->app_info == NULL || info->app_info->alias_appid == NULL)
1451                 return PMINFO_R_ERROR;
1452
1453         *alias_appid = (char *)info->app_info->alias_appid;
1454
1455         return PMINFO_R_OK;
1456 }
1457
1458 API int pkgmgrinfo_appinfo_get_effective_appid(pkgmgrinfo_appinfo_h handle, char **effective_appid)
1459 {
1460         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1461
1462         if (handle == NULL || effective_appid == NULL) {
1463                 LOGE("invalid parameter");
1464                 return PMINFO_R_EINVAL;
1465         }
1466
1467         if (info->app_info == NULL || info->app_info->effective_appid == NULL)
1468                 return PMINFO_R_ERROR;
1469
1470         *effective_appid = (char *)info->app_info->effective_appid;
1471
1472         return PMINFO_R_OK;
1473 }
1474
1475 API int pkgmgrinfo_appinfo_usr_get_datacontrol_info(const char *providerid, const char *type, uid_t uid, char **appid, char **access)
1476 {
1477         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1478         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1479         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1480         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1481
1482         int ret = PMINFO_R_OK;
1483         char *query = NULL;
1484         sqlite3_stmt *stmt = NULL;
1485
1486         /*open db*/
1487         ret = __open_manifest_db(uid, true);
1488         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1489
1490         /*Start constructing query*/
1491         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q and type=%Q", providerid, type);
1492
1493         /*prepare query*/
1494         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1495         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1496
1497         /*step query*/
1498         ret = sqlite3_step(stmt);
1499         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1500
1501         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1502         *access = strdup((char *)sqlite3_column_text(stmt, 2));
1503
1504         ret = PMINFO_R_OK;
1505
1506 catch:
1507         sqlite3_free(query);
1508         sqlite3_finalize(stmt);
1509         __close_manifest_db();
1510         return ret;
1511 }
1512
1513 API int pkgmgrinfo_appinfo_get_datacontrol_info(const char *providerid, const char *type, char **appid, char **access)
1514 {
1515         return pkgmgrinfo_appinfo_usr_get_datacontrol_info(providerid, type, GLOBAL_USER, appid, access);
1516 }
1517
1518 API int pkgmgrinfo_appinfo_usr_get_datacontrol_appid(const char *providerid, uid_t uid, char **appid)
1519 {
1520         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1521         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1522
1523         int ret = PMINFO_R_OK;
1524         char *query = NULL;
1525         sqlite3_stmt *stmt = NULL;
1526
1527         /*open db*/
1528         ret = __open_manifest_db(uid, true);
1529         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1530
1531         /*Start constructing query*/
1532         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q", providerid);
1533
1534         /*prepare query*/
1535         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1536         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1537
1538         /*step query*/
1539         ret = sqlite3_step(stmt);
1540         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1541
1542         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1543
1544         ret = PMINFO_R_OK;
1545
1546 catch:
1547         sqlite3_free(query);
1548         sqlite3_finalize(stmt);
1549         __close_manifest_db();
1550         return ret;
1551 }
1552
1553 API int pkgmgrinfo_appinfo_get_datacontrol_appid(const char *providerid, char **appid)
1554 {
1555         return pkgmgrinfo_appinfo_usr_get_datacontrol_appid(providerid, GLOBAL_USER, appid);
1556 }
1557
1558 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
1559                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
1560 {
1561         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1562         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1563         int ret = -1;
1564         permission_x *ptr;
1565         GList *tmp;
1566         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1567
1568         if (info->app_info == NULL)
1569                 return PMINFO_R_ERROR;
1570
1571         for (tmp = info->app_info->permission; tmp; tmp = tmp->next) {
1572                 ptr = (permission_x *)tmp->data;
1573                 if (ptr == NULL)
1574                         continue;
1575                 if (ptr->value) {
1576                         ret = permission_func(ptr->value, user_data);
1577                         if (ret < 0)
1578                                 break;
1579                 }
1580         }
1581         return PMINFO_R_OK;
1582 }
1583
1584 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
1585                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
1586 {
1587         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1588         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1589         int ret = -1;
1590         const char *category;
1591         GList *tmp;
1592         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1593
1594         if (info->app_info == NULL)
1595                 return PMINFO_R_ERROR;
1596
1597         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
1598                 category = (const char *)tmp->data;
1599                 if (category) {
1600                         ret = category_func(category, user_data);
1601                         if (ret < 0)
1602                                 break;
1603                 }
1604         }
1605         return PMINFO_R_OK;
1606 }
1607
1608 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
1609                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
1610 {
1611         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1612         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1613         int ret = -1;
1614         metadata_x *ptr;
1615         GList *tmp;
1616         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1617
1618         if (info->app_info == NULL)
1619                 return PMINFO_R_ERROR;
1620
1621         for (tmp = info->app_info->metadata; tmp; tmp = tmp->next) {
1622                 ptr = (metadata_x *)tmp->data;
1623                 if (ptr == NULL)
1624                         continue;
1625                 if (ptr->key) {
1626                         ret = metadata_func(ptr->key, ptr->value, user_data);
1627                         if (ret < 0)
1628                                 break;
1629                 }
1630         }
1631         return PMINFO_R_OK;
1632 }
1633
1634 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
1635                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
1636 {
1637         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1638         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1639         int ret;
1640         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1641         appcontrol_x *appcontrol;
1642         GList *tmp;
1643
1644         if (info->app_info == NULL)
1645                 return PMINFO_R_ERROR;
1646
1647         for (tmp = info->app_info->appcontrol; tmp; tmp = tmp->next) {
1648                 appcontrol = (appcontrol_x *)tmp->data;
1649                 if (appcontrol == NULL)
1650                         continue;
1651                 ret = appcontrol_func(appcontrol->operation, appcontrol->uri, appcontrol->mime, user_data);
1652                 if (ret < 0)
1653                         break;
1654         }
1655
1656         return PMINFO_R_OK;
1657 }
1658
1659 API int pkgmgrinfo_appinfo_foreach_background_category(
1660                 pkgmgrinfo_appinfo_h handle,
1661                 pkgmgrinfo_app_background_category_list_cb category_func,
1662                 void *user_data)
1663 {
1664         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1665         GList *tmp;
1666         char *category;
1667
1668         if (handle == NULL || category_func == NULL || info->app_info == NULL) {
1669                 LOGE("invalid parameter");
1670                 return PMINFO_R_EINVAL;
1671         }
1672
1673         for (tmp = info->app_info->background_category; tmp; tmp = tmp->next) {
1674                 category = (char *)tmp->data;
1675                 if (category == NULL)
1676                         continue;
1677
1678                 if (category_func(category, user_data) < 0)
1679                         break;
1680         }
1681
1682         return PMINFO_R_OK;
1683 }
1684
1685 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h handle, bool *nodisplay)
1686 {
1687         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1688         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1689         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1690
1691         if (info->app_info == NULL || info->app_info->nodisplay == NULL)
1692                 return PMINFO_R_ERROR;
1693
1694         *nodisplay = _get_bool_value(info->app_info->nodisplay);
1695
1696         return PMINFO_R_OK;
1697 }
1698
1699 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h handle, bool *multiple)
1700 {
1701         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1702         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1703         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1704
1705         if (info->app_info == NULL || info->app_info->multiple == NULL)
1706                 return PMINFO_R_ERROR;
1707
1708         *multiple = _get_bool_value(info->app_info->multiple);
1709
1710         return PMINFO_R_OK;
1711 }
1712
1713 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
1714 {
1715         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1716         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1717         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1718
1719         if (info->app_info == NULL || info->app_info->indicatordisplay == NULL)
1720                 return PMINFO_R_ERROR;
1721
1722         *indicator_disp = _get_bool_value(info->app_info->indicatordisplay);
1723
1724         return PMINFO_R_OK;
1725 }
1726
1727 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
1728 {
1729         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1730         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1731         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1732
1733         if (info->app_info == NULL || info->app_info->taskmanage == NULL)
1734                 return PMINFO_R_ERROR;
1735
1736         *taskmanage = _get_bool_value(info->app_info->taskmanage);
1737
1738         return PMINFO_R_OK;
1739 }
1740
1741 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
1742 {
1743         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1744         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1745         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1746
1747         if (info->app_info == NULL || info->app_info->enabled == NULL)
1748                 return PMINFO_R_ERROR;
1749
1750         *enabled = _get_bool_value(info->app_info->enabled);
1751
1752         return PMINFO_R_OK;
1753 }
1754
1755 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
1756 {
1757         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1758         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1759         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1760
1761         if (info->app_info == NULL || info->app_info->onboot == NULL)
1762                 return PMINFO_R_ERROR;
1763
1764         *onboot = _get_bool_value(info->app_info->onboot);
1765
1766         return PMINFO_R_OK;
1767 }
1768
1769 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
1770 {
1771         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1772         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1773         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1774
1775         if (info->app_info == NULL || info->app_info->autorestart == NULL)
1776                 return PMINFO_R_ERROR;
1777
1778         *autorestart = _get_bool_value(info->app_info->autorestart);
1779
1780         return PMINFO_R_OK;
1781 }
1782
1783 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
1784 {
1785         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1786         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1787         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1788
1789         if (info->app_info == NULL || info->app_info->mainapp == NULL)
1790                 return PMINFO_R_ERROR;
1791
1792         *mainapp = _get_bool_value(info->app_info->mainapp);
1793
1794         return PMINFO_R_OK;
1795 }
1796
1797 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
1798 {
1799         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1800         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1801         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1802
1803         if (info->app_info == NULL || info->app_info->preload == NULL)
1804                 return PMINFO_R_ERROR;
1805
1806         *preload = _get_bool_value(info->app_info->preload);
1807
1808         return PMINFO_R_OK;
1809 }
1810
1811 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
1812 {
1813         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1814         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1815         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1816
1817         if (info->app_info == NULL || info->app_info->submode == NULL)
1818                 return PMINFO_R_ERROR;
1819
1820         *submode = _get_bool_value(info->app_info->submode);
1821
1822         return PMINFO_R_OK;
1823 }
1824
1825 API int pkgmgrinfo_appinfo_is_process_pool(pkgmgrinfo_appinfo_h handle, bool *process_pool)
1826 {
1827         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1828
1829         if (handle == NULL || process_pool == NULL) {
1830                 LOGE("invalid parameter");
1831                 return PMINFO_R_EINVAL;
1832         }
1833
1834         if (info->app_info == NULL)
1835                 return PMINFO_R_ERROR;
1836
1837         *process_pool = _get_bool_value(info->app_info->process_pool);
1838
1839         return PMINFO_R_OK;
1840 }
1841
1842 API int pkgmgrinfo_appinfo_is_category_exist(pkgmgrinfo_appinfo_h handle, const char *category, bool *exist)
1843 {
1844         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1845         retvm_if(category == NULL, PMINFO_R_EINVAL, "category is NULL");
1846         retvm_if(exist == NULL, PMINFO_R_EINVAL, "exist is NULL");
1847
1848         const char *val;
1849         GList *tmp;
1850         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1851
1852         if (info->app_info == NULL)
1853                 return PMINFO_R_ERROR;
1854
1855         *exist = 0;
1856         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
1857                 val = (const char *)tmp->data;
1858                 if (val == NULL)
1859                         continue;
1860                 if (strcasecmp(val, category) == 0) {
1861                         *exist = 1;
1862                         break;
1863                 }
1864         }
1865
1866         return PMINFO_R_OK;
1867 }
1868
1869 API int pkgmgrinfo_appinfo_is_ui_gadget(pkgmgrinfo_appinfo_h handle,
1870                 bool *ui_gadget)
1871 {
1872         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1873
1874         if (info == NULL || info->app_info == NULL || ui_gadget == NULL) {
1875                 _LOGE("invalid parameter");
1876                 return PMINFO_R_EINVAL;
1877         }
1878
1879         *ui_gadget = _get_bool_value(info->app_info->ui_gadget);
1880
1881         return PMINFO_R_OK;
1882 }
1883
1884 API int pkgmgrinfo_appinfo_is_support_disable(pkgmgrinfo_appinfo_h handle,
1885                 bool *support_disable)
1886 {
1887         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1888
1889         if (info == NULL || info->app_info == NULL || support_disable == NULL) {
1890                 _LOGE("invalid parameter");
1891                 return PMINFO_R_EINVAL;
1892         }
1893
1894         *support_disable = _get_bool_value(info->app_info->support_disable);
1895
1896         return PMINFO_R_OK;
1897 }
1898
1899 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle)
1900 {
1901         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1902         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1903         __cleanup_appinfo(info);
1904         return PMINFO_R_OK;
1905 }
1906
1907 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
1908 {
1909         return (pkgmgrinfo_pkginfo_filter_create(handle));
1910 }
1911
1912 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
1913 {
1914         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
1915 }
1916
1917 static gint __compare_func(gconstpointer data1, gconstpointer data2)
1918 {
1919         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
1920         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
1921         if (node1->prop == node2->prop)
1922                 return 0;
1923         else if (node1->prop > node2->prop)
1924                 return 1;
1925         else
1926                 return -1;
1927 }
1928
1929 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
1930                                 const char *property, const int value)
1931 {
1932         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1933         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1934         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
1935         char *val = NULL;
1936         GSList *link = NULL;
1937         int prop = -1;
1938         prop = _pminfo_appinfo_convert_to_prop_int(property);
1939         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
1940                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
1941                 _LOGE("Invalid Integer Property\n");
1942                 return PMINFO_R_EINVAL;
1943         }
1944         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1945         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1946         if (node == NULL) {
1947                 _LOGE("Out of Memory!!!\n");
1948                 return PMINFO_R_ERROR;
1949         }
1950         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
1951         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
1952         if (val == NULL) {
1953                 _LOGE("Out of Memory\n");
1954                 free(node);
1955                 node = NULL;
1956                 return PMINFO_R_ERROR;
1957         }
1958         node->prop = prop;
1959         node->value = val;
1960         /*If API is called multiple times for same property, we should override the previous values.
1961         Last value set will be used for filtering.*/
1962         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1963         if (link)
1964                 filter->list = g_slist_delete_link(filter->list, link);
1965         filter->list = g_slist_append(filter->list, (gpointer)node);
1966         return PMINFO_R_OK;
1967
1968 }
1969
1970 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
1971                                 const char *property, const bool value)
1972 {
1973         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1974         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1975         char *val = NULL;
1976         GSList *link = NULL;
1977         int prop = -1;
1978         prop = _pminfo_appinfo_convert_to_prop_bool(property);
1979         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
1980                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
1981                 _LOGE("Invalid Boolean Property\n");
1982                 return PMINFO_R_EINVAL;
1983         }
1984         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1985         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1986         if (node == NULL) {
1987                 _LOGE("Out of Memory!!!\n");
1988                 return PMINFO_R_ERROR;
1989         }
1990         if (value)
1991                 val = strndup("('true','True')", 15);
1992         else
1993                 val = strndup("('false','False')", 17);
1994         if (val == NULL) {
1995                 _LOGE("Out of Memory\n");
1996                 free(node);
1997                 node = NULL;
1998                 return PMINFO_R_ERROR;
1999         }
2000         node->prop = prop;
2001         node->value = val;
2002         /*If API is called multiple times for same property, we should override the previous values.
2003         Last value set will be used for filtering.*/
2004         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2005         if (link)
2006                 filter->list = g_slist_delete_link(filter->list, link);
2007         filter->list = g_slist_append(filter->list, (gpointer)node);
2008         return PMINFO_R_OK;
2009
2010 }
2011
2012 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
2013                                 const char *property, const char *value)
2014 {
2015         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2016         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2017         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2018         char *val = NULL;
2019         pkgmgrinfo_node_x *ptr = NULL;
2020         char prev[PKG_STRING_LEN_MAX] = {'\0'};
2021         char temp[PKG_STRING_LEN_MAX] = {'\0'};
2022         GSList *link = NULL;
2023         int prop = -1;
2024         prop = _pminfo_appinfo_convert_to_prop_str(property);
2025         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
2026                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
2027                 _LOGE("Invalid String Property\n");
2028                 return PMINFO_R_EINVAL;
2029         }
2030         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2031         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2032         if (node == NULL) {
2033                 _LOGE("Out of Memory!!!\n");
2034                 return PMINFO_R_ERROR;
2035         }
2036         node->prop = prop;
2037         switch (prop) {
2038         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
2039                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
2040                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
2041                 else
2042                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
2043                 node->value = val;
2044                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2045                 if (link)
2046                         filter->list = g_slist_delete_link(filter->list, link);
2047                 filter->list = g_slist_append(filter->list, (gpointer)node);
2048                 break;
2049         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
2050         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
2051         case E_PMINFO_APPINFO_PROP_APP_URI:
2052         case E_PMINFO_APPINFO_PROP_APP_MIME:
2053                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
2054                 if (val == NULL) {
2055                         _LOGE("Out of Memory\n");
2056                         free(node);
2057                         node = NULL;
2058                         return PMINFO_R_ERROR;
2059                 }
2060                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2061                 if (link) {
2062                         ptr = (pkgmgrinfo_node_x *)link->data;
2063                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
2064                         _LOGE("Previous value is %s\n", prev);
2065                         filter->list = g_slist_delete_link(filter->list, link);
2066                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
2067                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
2068                         _LOGE("New value is %s\n", val);
2069                         node->value = val;
2070                         filter->list = g_slist_append(filter->list, (gpointer)node);
2071                         memset(temp, '\0', PKG_STRING_LEN_MAX);
2072                 } else {
2073                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
2074                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
2075                         _LOGE("First value is %s\n", val);
2076                         node->value = val;
2077                         filter->list = g_slist_append(filter->list, (gpointer)node);
2078                         memset(temp, '\0', PKG_STRING_LEN_MAX);
2079                 }
2080                 break;
2081         default:
2082                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
2083                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2084                 if (link)
2085                         filter->list = g_slist_delete_link(filter->list, link);
2086                 filter->list = g_slist_append(filter->list, (gpointer)node);
2087                 break;
2088         }
2089         return PMINFO_R_OK;
2090 }
2091
2092 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
2093 {
2094         int ret;
2095         GList *list = NULL;
2096
2097         if (handle == NULL || count == NULL) {
2098                 _LOGE("invalid parameter");
2099                 return PMINFO_R_EINVAL;
2100         }
2101
2102         ret = _appinfo_get_filtered_list(handle, uid, &list);
2103         if (ret != PMINFO_R_OK)
2104                 return PMINFO_R_ERROR;
2105
2106         *count = g_list_length(list);
2107
2108         g_list_free_full(list, free);
2109
2110         return PMINFO_R_OK;
2111 }
2112
2113 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
2114 {
2115         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, GLOBAL_USER);
2116 }
2117
2118 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(
2119                 pkgmgrinfo_appinfo_filter_h handle,
2120                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2121 {
2122         if (handle == NULL || app_cb == NULL) {
2123                 LOGE("invalid parameter");
2124                 return PMINFO_R_EINVAL;
2125         }
2126
2127         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2128                         user_data);
2129 }
2130
2131 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
2132                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
2133 {
2134         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, GLOBAL_USER);
2135 }
2136
2137 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
2138 {
2139         return (pkgmgrinfo_pkginfo_filter_create(handle));
2140 }
2141
2142 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
2143 {
2144         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
2145 }
2146
2147 API int pkgmgrinfo_appinfo_metadata_filter_add(
2148                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2149                 const char *key, const char *value)
2150 {
2151         int ret;
2152
2153         ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2154                         PMINFO_APPINFO_PROP_APP_METADATA_KEY, key);
2155         if (ret != PMINFO_R_OK)
2156                 return ret;
2157
2158         /* value can be NULL.
2159          * In that case all apps with specified key should be displayed
2160          */
2161         if (value) {
2162                 ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2163                                 PMINFO_APPINFO_PROP_APP_METADATA_VALUE, value);
2164                 if (ret != PMINFO_R_OK)
2165                         return ret;
2166         }
2167
2168         return PMINFO_R_OK;
2169 }
2170
2171 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(
2172                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2173                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2174 {
2175         if (handle == NULL || app_cb == NULL) {
2176                 LOGE("invalid parameter");
2177                 return PMINFO_R_EINVAL;
2178         }
2179
2180         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2181                         user_data);
2182 }
2183
2184 API int pkgmgrinfo_appinfo_metadata_filter_foreach(
2185                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2186                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
2187 {
2188         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb,
2189                         user_data, GLOBAL_USER);
2190 }
2191
2192 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
2193 {
2194         const char *val;
2195         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2196
2197         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2198         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2199
2200         val = info->app_info->guestmode_visibility;
2201         *status = _get_bool_value(val);
2202         return PMINFO_R_OK;
2203 }