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