add parameters to get enabled/disabled app info properly
[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, operation "
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                 _save_column_str(stmt, idx++, &info->operation);
528                 *splashscreens = g_list_append(*splashscreens, info);
529         }
530
531         sqlite3_finalize(stmt);
532
533         return PMINFO_R_OK;
534 }
535
536 static GList *__get_background_category(char *value)
537 {
538         GList *category_list = NULL;
539         int convert_value = 0;
540         if (!value || strlen(value) == 0)
541                 return NULL;
542
543         convert_value = atoi(value);
544         if (convert_value < 0)
545                 return NULL;
546
547         if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
548                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
549         else
550                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
551
552         if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
553                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_MEDIA_STR));
554
555         if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
556                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
557
558         if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
559                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_BGNETWORK_STR));
560
561         if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
562                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_LOCATION_STR));
563
564         if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
565                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_SENSOR_STR));
566
567         if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
568                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_IOTCOMM_STR));
569
570         if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
571                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_SYSTEM));
572
573         return category_list;
574
575 }
576
577 static int _appinfo_get_application(sqlite3 *db, const char *appid,
578                 const char *locale, application_x **application, bool is_disabled, uid_t target_uid)
579 {
580         static const char query_raw[] =
581                 "SELECT app_id, app_component, app_exec, app_nodisplay, "
582                 "app_type, app_onboot, app_multiple, app_autorestart, "
583                 "app_taskmanage, app_enabled, app_hwacceleration, "
584                 "app_screenreader, app_mainapp, app_recentimage, "
585                 "app_launchcondition, app_indicatordisplay, app_portraitimg, "
586                 "app_landscapeimg, app_guestmodevisibility, "
587                 "app_permissiontype, app_preload, app_submode, "
588                 "app_submode_mainid, app_launch_mode, app_ui_gadget, "
589                 "app_support_disable, "
590                 "component_type, package, app_process_pool, app_installed_storage, "
591                 "app_background_category, app_package_type "
592                 "FROM package_app_info WHERE app_id='%s' "
593                 "AND (app_disable='%s' "
594                 "%s app_id %s IN "
595                 "(SELECT app_id from package_app_disable_for_user WHERE uid='%d'))";
596         int ret;
597         char query[MAX_QUERY_LEN] = { '\0' };
598         sqlite3_stmt *stmt;
599         int idx;
600         application_x *info;
601         char *bg_category_str = NULL;
602         snprintf(query, MAX_QUERY_LEN - 1, query_raw,
603                         appid,
604                         is_disabled ? "true" : "false",
605                         is_disabled ? "OR" : "AND",
606                         is_disabled ? "" : "NOT",
607                         (int)target_uid);
608
609         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
610         if (ret != SQLITE_OK) {
611                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
612                 return PMINFO_R_ERROR;
613         }
614
615         ret = sqlite3_step(stmt);
616         if (ret == SQLITE_DONE) {
617                 sqlite3_finalize(stmt);
618                 return PMINFO_R_ENOENT;
619         } else if (ret != SQLITE_ROW) {
620                 LOGE("step failed: %s", sqlite3_errmsg(db));
621                 sqlite3_finalize(stmt);
622                 return PMINFO_R_ERROR;
623         }
624
625         info = calloc(1, sizeof(application_x));
626         if (info == NULL) {
627                 LOGE("out of memory");
628                 sqlite3_finalize(stmt);
629                 return PMINFO_R_ERROR;
630         }
631         idx = 0;
632         _save_column_str(stmt, idx++, &info->appid);
633         _save_column_str(stmt, idx++, &info->component);
634         _save_column_str(stmt, idx++, &info->exec);
635         _save_column_str(stmt, idx++, &info->nodisplay);
636         _save_column_str(stmt, idx++, &info->type);
637         _save_column_str(stmt, idx++, &info->onboot);
638         _save_column_str(stmt, idx++, &info->multiple);
639         _save_column_str(stmt, idx++, &info->autorestart);
640         _save_column_str(stmt, idx++, &info->taskmanage);
641         _save_column_str(stmt, idx++, &info->enabled);
642         _save_column_str(stmt, idx++, &info->hwacceleration);
643         _save_column_str(stmt, idx++, &info->screenreader);
644         _save_column_str(stmt, idx++, &info->mainapp);
645         _save_column_str(stmt, idx++, &info->recentimage);
646         _save_column_str(stmt, idx++, &info->launchcondition);
647         _save_column_str(stmt, idx++, &info->indicatordisplay);
648         _save_column_str(stmt, idx++, &info->portraitimg);
649         _save_column_str(stmt, idx++, &info->landscapeimg);
650         _save_column_str(stmt, idx++, &info->guestmode_visibility);
651         _save_column_str(stmt, idx++, &info->permission_type);
652         _save_column_str(stmt, idx++, &info->preload);
653         _save_column_str(stmt, idx++, &info->submode);
654         _save_column_str(stmt, idx++, &info->submode_mainid);
655         _save_column_str(stmt, idx++, &info->launch_mode);
656         _save_column_str(stmt, idx++, &info->ui_gadget);
657         _save_column_str(stmt, idx++, &info->support_disable);
658         _save_column_str(stmt, idx++, &info->component_type);
659         _save_column_str(stmt, idx++, &info->package);
660         _save_column_str(stmt, idx++, &info->process_pool);
661         _save_column_str(stmt, idx++, &info->installed_storage);
662         _save_column_str(stmt, idx++, &bg_category_str);
663         _save_column_str(stmt, idx++, &info->package_type);
664
665         info->background_category = __get_background_category(bg_category_str);
666
667         if (_appinfo_get_label(db, info->appid, locale, &info->label)) {
668                 pkgmgrinfo_basic_free_application(info);
669                 sqlite3_finalize(stmt);
670                 return PMINFO_R_ERROR;
671         }
672
673         if (_appinfo_get_icon(db, info->appid, locale, &info->icon)) {
674                 pkgmgrinfo_basic_free_application(info);
675                 sqlite3_finalize(stmt);
676                 return PMINFO_R_ERROR;
677         }
678
679         if (_appinfo_get_category(db, info->appid, &info->category)) {
680                 pkgmgrinfo_basic_free_application(info);
681                 sqlite3_finalize(stmt);
682                 return PMINFO_R_ERROR;
683         }
684
685         if (_appinfo_get_app_control(db, info->appid, &info->appcontrol)) {
686                 pkgmgrinfo_basic_free_application(info);
687                 sqlite3_finalize(stmt);
688                 return PMINFO_R_ERROR;
689         }
690
691         if (_appinfo_get_data_control(db, info->appid, &info->datacontrol)) {
692                 pkgmgrinfo_basic_free_application(info);
693                 sqlite3_finalize(stmt);
694                 return PMINFO_R_ERROR;
695         }
696
697         if (_appinfo_get_metadata(db, info->appid, &info->metadata)) {
698                 pkgmgrinfo_basic_free_application(info);
699                 sqlite3_finalize(stmt);
700                 return PMINFO_R_ERROR;
701         }
702
703         if (_appinfo_get_splashscreens(db, info->appid, &info->splashscreens)) {
704                 pkgmgrinfo_basic_free_application(info);
705                 sqlite3_finalize(stmt);
706                 return PMINFO_R_ERROR;
707         }
708
709         *application = info;
710
711         sqlite3_finalize(stmt);
712
713         return PMINFO_R_OK;
714 }
715
716 static int _appinfo_get_appinfo(const char *appid, uid_t db_uid,
717                 uid_t target_uid, bool is_disabled, pkgmgr_appinfo_x **appinfo)
718 {
719         int ret;
720         sqlite3 *db;
721         const char *dbpath;
722         char *locale;
723         pkgmgr_appinfo_x *info;
724
725         dbpath = getUserPkgParserDBPathUID(db_uid);
726         if (dbpath == NULL)
727                 return PMINFO_R_ERROR;
728
729         locale = _get_system_locale();
730         if (locale == NULL)
731                 return PMINFO_R_ERROR;
732
733         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
734         if (ret != SQLITE_OK) {
735                 _LOGE("failed to open db: %d", ret);
736                 free(locale);
737                 return PMINFO_R_ERROR;
738         }
739
740         info = calloc(1, sizeof(pkgmgr_appinfo_x));
741         if (info == NULL) {
742                 _LOGE("out of memory");
743                 free(locale);
744                 sqlite3_close_v2(db);
745                 return PMINFO_R_ERROR;
746         }
747
748         ret = _appinfo_get_application(db, appid, locale, &info->app_info, is_disabled, target_uid);
749         if (ret != PMINFO_R_OK) {
750                 free(info);
751                 free(locale);
752                 sqlite3_close_v2(db);
753                 return ret;
754         }
755
756         info->locale = locale;
757         info->package = strdup(info->app_info->package);
758
759         *appinfo = info;
760
761         sqlite3_close_v2(db);
762
763         return ret;
764 }
765
766 API int pkgmgrinfo_appinfo_get_usr_disabled_appinfo(const char *appid, uid_t uid,
767                 pkgmgrinfo_appinfo_h *handle)
768 {
769         int ret;
770
771         if (appid == NULL || handle == NULL) {
772                 LOGE("invalid parameter");
773                 return PMINFO_R_EINVAL;
774         }
775
776         ret = _appinfo_get_appinfo(appid, uid, uid, true, (pkgmgr_appinfo_x **)handle);
777         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
778                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER, uid, true,
779                                 (pkgmgr_appinfo_x **)handle);
780
781         if (ret != PMINFO_R_OK)
782                 _LOGE("failed to get appinfo of %s for user %d", appid, uid);
783
784         return ret;
785 }
786
787 API int pkgmgrinfo_appinfo_get_disabled_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
788 {
789         return pkgmgrinfo_appinfo_get_usr_disabled_appinfo(appid, GLOBAL_USER, handle);
790 }
791
792 API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid,
793                 pkgmgrinfo_appinfo_h *handle)
794 {
795         int ret;
796
797         if (appid == NULL || handle == NULL) {
798                 LOGE("invalid parameter");
799                 return PMINFO_R_EINVAL;
800         }
801
802         ret = _appinfo_get_appinfo(appid, uid, uid, false, (pkgmgr_appinfo_x **)handle);
803         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
804                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER, uid, false,
805                                 (pkgmgr_appinfo_x **)handle);
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         if (tmp->operation)
965                 splashscreen->operation = strdup(tmp->operation);
966
967         return splashscreen;
968 }
969
970 static int _appinfo_copy_appinfo(application_x **application, application_x *data)
971 {
972         application_x *app_info;
973         int ret;
974
975         app_info = calloc(1, sizeof(application_x));
976         if (app_info == NULL) {
977                 LOGE("memory alloc failed");
978                 return PMINFO_R_ERROR;
979         }
980
981         if (data->appid != NULL)
982                 app_info->appid = strdup(data->appid);
983         if (data->exec != NULL)
984                 app_info->exec = strdup(data->exec);
985         if (data->nodisplay != NULL)
986                 app_info->nodisplay = strdup(data->nodisplay);
987         if (data->multiple != NULL)
988                 app_info->multiple = strdup(data->multiple);
989         if (data->taskmanage != NULL)
990                 app_info->taskmanage = strdup(data->taskmanage);
991         if (data->enabled != NULL)
992                 app_info->enabled = strdup(data->enabled);
993         if (data->type != NULL)
994                 app_info->type = strdup(data->type);
995         if (data->categories != NULL)
996                 app_info->categories = strdup(data->categories);
997         if (data->hwacceleration != NULL)
998                 app_info->hwacceleration = strdup(data->hwacceleration);
999         if (data->screenreader != NULL)
1000                 app_info->screenreader = strdup(data->screenreader);
1001         if (data->mainapp != NULL)
1002                 app_info->mainapp = strdup(data->mainapp);
1003         if (data->package != NULL)
1004                 app_info->package = strdup(data->package);
1005         if (data->recentimage != NULL)
1006                 app_info->recentimage = strdup(data->recentimage);
1007         if (data->launchcondition != NULL)
1008                 app_info->launchcondition = strdup(data->launchcondition);
1009         if (data->indicatordisplay != NULL)
1010                 app_info->indicatordisplay = strdup(data->indicatordisplay);
1011         if (data->portraitimg != NULL)
1012                 app_info->portraitimg = strdup(data->portraitimg);
1013         if (data->landscapeimg != NULL)
1014                 app_info->landscapeimg = strdup(data->landscapeimg);
1015         if (data->guestmode_visibility != NULL)
1016                 app_info->guestmode_visibility = strdup(data->guestmode_visibility);
1017         if (data->component != NULL)
1018                 app_info->component = strdup(data->component);
1019         if (data->permission_type != NULL)
1020                 app_info->permission_type = strdup(data->permission_type);
1021         if (data->component_type != NULL)
1022                 app_info->component_type = strdup(data->component_type);
1023         if (data->preload != NULL)
1024                 app_info->preload = strdup(data->preload);
1025         if (data->submode != NULL)
1026                 app_info->submode = strdup(data->submode);
1027         if (data->submode_mainid != NULL)
1028                 app_info->submode_mainid = strdup(data->submode_mainid);
1029         if (data->process_pool != NULL)
1030                 app_info->process_pool = strdup(data->process_pool);
1031         if (data->installed_storage != NULL)
1032                 app_info->installed_storage = strdup(data->installed_storage);
1033         if (data->autorestart != NULL)
1034                 app_info->autorestart = strdup(data->autorestart);
1035         if (data->onboot != NULL)
1036                 app_info->onboot = strdup(data->onboot);
1037         if (data->support_disable != NULL)
1038                 app_info->support_disable = strdup(data->support_disable);
1039         if (data->ui_gadget != NULL)
1040                 app_info->ui_gadget = strdup(data->ui_gadget);
1041         if (data->launch_mode != NULL)
1042                 app_info->launch_mode = strdup(data->launch_mode);
1043         if (data->package_type != NULL)
1044                 app_info->package_type = strdup(data->package_type);
1045
1046         /* GList */
1047         ret = 0;
1048         app_info->label = g_list_copy_deep(data->label, __copy_label, &ret);
1049         if (ret < 0) {
1050                 LOGE("memory alloc failed");
1051                 pkgmgrinfo_basic_free_application(app_info);
1052                 return PMINFO_R_ERROR;
1053         }
1054
1055         ret = 0;
1056         app_info->icon = g_list_copy_deep(data->icon, __copy_icon, &ret);
1057         if (ret < 0) {
1058                 LOGE("memory alloc failed");
1059                 pkgmgrinfo_basic_free_application(app_info);
1060                 return PMINFO_R_ERROR;
1061         }
1062
1063         ret = 0;
1064         app_info->category = g_list_copy_deep(data->category, __copy_str, &ret);
1065         if (ret < 0) {
1066                 LOGE("memory alloc failed");
1067                 pkgmgrinfo_basic_free_application(app_info);
1068                 return PMINFO_R_ERROR;
1069         }
1070
1071         ret = 0;
1072         app_info->metadata = g_list_copy_deep(data->metadata, __copy_metadata, &ret);
1073         if (ret < 0) {
1074                 LOGE("memory alloc failed");
1075                 pkgmgrinfo_basic_free_application(app_info);
1076                 return PMINFO_R_ERROR;
1077         }
1078
1079         ret = 0;
1080         app_info->datacontrol = g_list_copy_deep(data->datacontrol, __copy_datacontrol, &ret);
1081         if (ret < 0) {
1082                 LOGE("memory alloc failed");
1083                 pkgmgrinfo_basic_free_application(app_info);
1084                 return PMINFO_R_ERROR;
1085         }
1086
1087         ret = 0;
1088         app_info->appcontrol = g_list_copy_deep(data->appcontrol, __copy_appcontrol, &ret);
1089         if (ret < 0) {
1090                 LOGE("memory alloc failed");
1091                 pkgmgrinfo_basic_free_application(app_info);
1092                 return PMINFO_R_ERROR;
1093         }
1094
1095         ret = 0;
1096         app_info->background_category = g_list_copy_deep(data->background_category, __copy_str, &ret);
1097         if (ret < 0) {
1098                 LOGE("memory alloc failed");
1099                 pkgmgrinfo_basic_free_application(app_info);
1100                 return PMINFO_R_ERROR;
1101         }
1102
1103         ret = 0;
1104         app_info->splashscreens = g_list_copy_deep(data->splashscreens, __copy_splashscreens, &ret);
1105         if (ret < 0) {
1106                 LOGE("memory alloc failed");
1107                 pkgmgrinfo_basic_free_application(app_info);
1108                 return PMINFO_R_ERROR;
1109         }
1110
1111         *application = app_info;
1112
1113         return PMINFO_R_OK;
1114 }
1115
1116 API int pkgmgrinfo_appinfo_clone_appinfo(pkgmgrinfo_appinfo_h handle,
1117                 pkgmgrinfo_appinfo_h *clone)
1118 {
1119         pkgmgr_appinfo_x *info;
1120         pkgmgr_appinfo_x *temp = (pkgmgr_appinfo_x *)handle;
1121
1122         if (handle == NULL)
1123                 return PMINFO_R_EINVAL;
1124
1125         info = calloc(1, sizeof(pkgmgr_appinfo_x));
1126         if (info == NULL) {
1127                 LOGE("memory alloc failed");
1128                 return PMINFO_R_ERROR;
1129         }
1130
1131         if (temp->package != NULL)
1132                 info->package = strdup(temp->package);
1133         if (temp->locale != NULL)
1134                 info->locale = strdup(temp->locale);
1135
1136         info->app_component = temp->app_component;
1137
1138         if (_appinfo_copy_appinfo(&info->app_info, temp->app_info) < 0) {
1139                 LOGE("appinfo copy failed");
1140                 if (info->package)
1141                         free((void *)info->package);
1142                 if (info->locale)
1143                         free(info->locale);
1144                 free(info);
1145                 return PMINFO_R_ERROR;
1146         }
1147
1148         *clone = info;
1149
1150         return PMINFO_R_OK;
1151 }
1152
1153 static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
1154                 pkgmgrinfo_filter_x *filter, pkgmgrinfo_app_list_cb app_list_cb,
1155                 void *user_data)
1156 {
1157         int ret;
1158         pkgmgr_appinfo_x *info;
1159         GList *list = NULL;
1160         GList *tmp;
1161         char *appid;
1162         int stop = 0;
1163
1164         ret = _appinfo_get_filtered_list(filter, uid, &list);
1165         if (ret != PMINFO_R_OK)
1166                 return PMINFO_R_ERROR;
1167
1168         for (tmp = list; tmp; tmp = tmp->next) {
1169                 appid = (char *)tmp->data;
1170                 if (stop == 0) {
1171                         ret = _appinfo_get_appinfo(appid, uid, uid, false, &info);
1172                         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
1173                                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER, uid, false,
1174                                                 &info);
1175                         if (ret != PMINFO_R_OK) {
1176                                 free(appid);
1177                                 continue;
1178                         }
1179                         if (app_list_cb(info, user_data) < 0)
1180                                 stop = 1;
1181                         pkgmgrinfo_appinfo_destroy_appinfo(info);
1182                 }
1183                 free(appid);
1184         }
1185
1186         g_list_free(list);
1187
1188         return PMINFO_R_OK;
1189 }
1190
1191 API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle,
1192                 pkgmgrinfo_app_component component,
1193                 pkgmgrinfo_app_list_cb app_func, void *user_data, uid_t uid)
1194 {
1195         int ret;
1196         pkgmgrinfo_appinfo_filter_h filter;
1197         char *pkgid;
1198         const char *comp_str = NULL;
1199
1200         if (handle == NULL || app_func == NULL) {
1201                 LOGE("invalid parameter");
1202                 return PMINFO_R_EINVAL;
1203         }
1204
1205         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid)) {
1206                 LOGE("invalid parameter");
1207                 return PMINFO_R_EINVAL;
1208         }
1209
1210         if (pkgmgrinfo_appinfo_filter_create(&filter))
1211                 return PMINFO_R_ERROR;
1212
1213         if (pkgmgrinfo_appinfo_filter_add_string(filter,
1214                                 PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid)) {
1215                 pkgmgrinfo_appinfo_filter_destroy(filter);
1216                 return PMINFO_R_ERROR;
1217         }
1218
1219         if (uid == GLOBAL_USER) {
1220                 if (pkgmgrinfo_appinfo_filter_add_int(filter,
1221                                         PMINFO_APPINFO_PROP_APP_DISABLE_FOR_USER, (int)getuid())) {
1222                         pkgmgrinfo_appinfo_filter_destroy(filter);
1223                         return PMINFO_R_ERROR;
1224                 }
1225         }
1226
1227         switch (component) {
1228         case PMINFO_UI_APP:
1229                 comp_str = PMINFO_APPINFO_UI_APP;
1230                 break;
1231         case PMINFO_SVC_APP:
1232                 comp_str = PMINFO_APPINFO_SVC_APP;
1233                 break;
1234         default:
1235                 break;
1236         }
1237
1238         if (comp_str) {
1239                 if (pkgmgrinfo_appinfo_filter_add_string(filter,
1240                                         PMINFO_APPINFO_PROP_APP_COMPONENT,
1241                                         comp_str)) {
1242                         pkgmgrinfo_appinfo_filter_destroy(filter);
1243                         return PMINFO_R_ERROR;
1244                 }
1245         }
1246
1247         ret = _appinfo_get_filtered_foreach_appinfo(uid, filter, app_func,
1248                         user_data);
1249
1250         pkgmgrinfo_appinfo_filter_destroy(filter);
1251
1252         return ret;
1253 }
1254
1255 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
1256                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
1257 {
1258         return pkgmgrinfo_appinfo_get_usr_list(handle, component, app_func, user_data, GLOBAL_USER);
1259 }
1260
1261 API int pkgmgrinfo_appinfo_get_usr_install_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
1262 {
1263         if (app_func == NULL) {
1264                 LOGE("invalid parameter");
1265                 return PMINFO_R_EINVAL;
1266         }
1267
1268         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
1269                         user_data);
1270 }
1271
1272 API int pkgmgrinfo_appinfo_get_install_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
1273 {
1274         return pkgmgrinfo_appinfo_get_usr_install_list(app_func, GLOBAL_USER, user_data);
1275 }
1276
1277 API int pkgmgrinfo_appinfo_get_usr_installed_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
1278 {
1279         if (app_func == NULL) {
1280                 LOGE("invalid parameter");
1281                 return PMINFO_R_EINVAL;
1282         }
1283
1284         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
1285                         user_data);
1286 }
1287
1288 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
1289 {
1290         return pkgmgrinfo_appinfo_get_usr_installed_list(app_func, GLOBAL_USER, user_data);
1291 }
1292
1293 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h handle, char **appid)
1294 {
1295         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1296
1297         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1298         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1299
1300         if (info->app_info == NULL || info->app_info->appid == NULL)
1301                 return PMINFO_R_ERROR;
1302         *appid = (char *)info->app_info->appid;
1303
1304         return PMINFO_R_OK;
1305 }
1306
1307 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, char **pkg_name)
1308 {
1309         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1310
1311         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1312         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1313
1314         if (info->package == NULL)
1315                 return PMINFO_R_ERROR;
1316
1317         *pkg_name = (char *)info->package;
1318
1319         return PMINFO_R_OK;
1320 }
1321
1322 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h handle, char **pkgid)
1323 {
1324         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1325
1326         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1327         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1328
1329         if (info->package == NULL)
1330                 return PMINFO_R_ERROR;
1331
1332         *pkgid = (char *)info->package;
1333
1334         return PMINFO_R_OK;
1335 }
1336
1337 API int pkgmgrinfo_appinfo_get_pkgtype(pkgmgrinfo_appinfo_h  handle, char **pkgtype)
1338 {
1339         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1340         retvm_if(pkgtype == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1341         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1342
1343         *pkgtype = (char *)info->app_info->package_type;
1344
1345         return PMINFO_R_OK;
1346 }
1347
1348 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, char **exec)
1349 {
1350         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1351
1352         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1353         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1354
1355         if (info->app_info == NULL || info->app_info->exec == NULL)
1356                 return PMINFO_R_ERROR;
1357         *exec = (char *)info->app_info->exec;
1358
1359         return PMINFO_R_OK;
1360 }
1361
1362
1363 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1364 {
1365         const char *locale;
1366         icon_x *ptr;
1367         GList *tmp;
1368         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1369
1370         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1371         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1372
1373         locale = info->locale;
1374         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1375
1376         if (info->app_info == NULL)
1377                 return PMINFO_R_ERROR;
1378
1379         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1380                 ptr = (icon_x *)tmp->data;
1381                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1382                                 !strcasecmp(ptr->text, "") ||
1383                                 strcmp(ptr->lang, locale))
1384                         continue;
1385                 *icon = (char *)ptr->text;
1386                 return PMINFO_R_OK;
1387         }
1388
1389         locale = DEFAULT_LOCALE;
1390         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1391                 ptr = (icon_x *)tmp->data;
1392                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1393                                 strcmp(ptr->lang, locale))
1394                         continue;
1395                 *icon = (char *)ptr->text;
1396                 return PMINFO_R_OK;
1397         }
1398
1399         return PMINFO_R_ERROR;
1400 }
1401
1402
1403 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, char **label)
1404 {
1405         const char *locale;
1406         label_x *ptr;
1407         GList *tmp;
1408         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1409
1410         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1411         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1412
1413         locale = info->locale;
1414         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1415
1416         if (info->app_info == NULL)
1417                 return PMINFO_R_ERROR;
1418
1419         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
1420                 ptr = (label_x *)tmp->data;
1421                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1422                                 strcmp(ptr->lang, locale))
1423                         continue;
1424                 *label = (char *)ptr->text;
1425                 return PMINFO_R_OK;
1426         }
1427
1428         locale = DEFAULT_LOCALE;
1429         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
1430                 ptr = (label_x *)tmp->data;
1431                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1432                                 strcmp(ptr->lang, locale))
1433                         continue;
1434                 *label = (char *)ptr->text;
1435                 return PMINFO_R_OK;
1436         }
1437
1438         return PMINFO_R_ERROR;
1439 }
1440
1441 static char *_get_localed_label(const char *appid, const char *locale, uid_t uid)
1442 {
1443         char *result = NULL;
1444         char *query = NULL;
1445         sqlite3_stmt *stmt = NULL;
1446         sqlite3 *db = NULL;
1447         char *val;
1448         const char *manifest_db;
1449
1450         manifest_db = getUserPkgParserDBPathUID(uid);
1451         if (manifest_db == NULL) {
1452                 _LOGE("Failed to get manifest db path");
1453                 goto err;
1454         }
1455
1456         if (sqlite3_open_v2(manifest_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
1457                 _LOGE("DB open fail\n");
1458                 goto err;
1459         }
1460
1461         query = sqlite3_mprintf("select app_label from package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
1462         if (query == NULL) {
1463                 _LOGE("Out of memory");
1464                 goto err;
1465         }
1466
1467         if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) {
1468                 _LOGE("prepare_v2 fail\n");
1469                 goto err;
1470         }
1471
1472         if (sqlite3_step(stmt) == SQLITE_ROW) {
1473                 val = (char *)sqlite3_column_text(stmt, 0);
1474                 if (val != NULL)
1475                         result = strdup(val);
1476         }
1477
1478 err:
1479         sqlite3_finalize(stmt);
1480         sqlite3_free(query);
1481         sqlite3_close(db);
1482
1483         return result;
1484 }
1485
1486 API int pkgmgrinfo_appinfo_usr_get_localed_label(const char *appid, const char *locale, uid_t uid, char **label)
1487 {
1488         char *val;
1489
1490         retvm_if(appid == NULL || locale == NULL || label == NULL, PMINFO_R_EINVAL, "Argument is NULL");
1491
1492         val = _get_localed_label(appid, locale, uid);
1493         if (val == NULL)
1494                 val = _get_localed_label(appid, DEFAULT_LOCALE, uid);
1495
1496         if (val == NULL)
1497                 return PMINFO_R_ERROR;
1498
1499         *label = val;
1500
1501         return PMINFO_R_OK;
1502 }
1503
1504 API int pkgmgrinfo_appinfo_get_localed_label(const char *appid, const char *locale, char **label)
1505 {
1506         return pkgmgrinfo_appinfo_usr_get_localed_label(appid, locale, GLOBAL_USER, label);
1507 }
1508
1509 static pkgmgrinfo_app_component __appcomponent_convert(const char *comp)
1510 {
1511         if (strcasecmp(comp, "uiapp") == 0)
1512                 return PMINFO_UI_APP;
1513         else if (strcasecmp(comp, "svcapp") == 0)
1514                 return PMINFO_SVC_APP;
1515         else if (strcasecmp(comp, "widgetapp") == 0)
1516                 return PMINFO_WIDGET_APP;
1517         else if (strcasecmp(comp, "watchapp") == 0)
1518                 return PMINFO_WATCH_APP;
1519         else
1520                 return -1;
1521 }
1522
1523 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_component *component)
1524 {
1525         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1526         int comp;
1527
1528         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1529         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1530
1531         if (info->app_info == NULL)
1532                 return PMINFO_R_ERROR;
1533
1534         comp = __appcomponent_convert(info->app_info->component);
1535         if (comp < 0)
1536                 return PMINFO_R_ERROR;
1537
1538         *component = comp;
1539
1540         return PMINFO_R_OK;
1541 }
1542
1543 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, char **app_type)
1544 {
1545         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1546
1547         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1548         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1549
1550         if (info->app_info == NULL || info->app_info->type == NULL)
1551                 return PMINFO_R_ERROR;
1552         *app_type = (char *)info->app_info->type;
1553
1554         return PMINFO_R_OK;
1555 }
1556
1557 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
1558                                         int *operation_count, char ***operation)
1559 {
1560         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1561         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1562         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1563         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1564         *operation_count = data->operation_count;
1565         *operation = data->operation;
1566         return PMINFO_R_OK;
1567 }
1568
1569 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
1570                                         int *uri_count, char ***uri)
1571 {
1572         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1573         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1574         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1575         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1576         *uri_count = data->uri_count;
1577         *uri = data->uri;
1578         return PMINFO_R_OK;
1579 }
1580
1581 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
1582                                         int *mime_count, char ***mime)
1583 {
1584         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1585         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1586         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1587         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1588         *mime_count = data->mime_count;
1589         *mime = data->mime;
1590         return PMINFO_R_OK;
1591 }
1592
1593 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
1594                                         int *subapp_count, char ***subapp)
1595 {
1596         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1597         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1598         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1599         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1600         *subapp_count = data->subapp_count;
1601         *subapp = data->subapp;
1602         return PMINFO_R_OK;
1603 }
1604
1605 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1606 {
1607         char *val;
1608         icon_x *ptr;
1609         GList *tmp;
1610         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1611
1612         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1613         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1614
1615         if (info->app_info == NULL)
1616                 return PMINFO_R_ERROR;
1617
1618         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1619                 ptr = (icon_x *)tmp->data;
1620                 if (ptr == NULL || ptr->section == NULL)
1621                         continue;
1622
1623                 val = (char *)ptr->section;
1624                 if (val && strcmp(val, "setting") == 0) {
1625                         *icon = (char *)ptr->text;
1626                         return PMINFO_R_OK;
1627                 }
1628         }
1629
1630         return PMINFO_R_ERROR;
1631 }
1632
1633
1634 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1635 {
1636         char *val;
1637         icon_x *ptr;
1638         GList *tmp;
1639         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1640
1641         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1642         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1643
1644         if (info->app_info == NULL)
1645                 return PMINFO_R_ERROR;
1646
1647         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1648                 ptr = (icon_x *)tmp->data;
1649                 if (ptr == NULL || ptr->section == NULL)
1650                         continue;
1651
1652                 val = (char *)ptr->section;
1653                 if (val && strcmp(val, "notification") == 0){
1654                         *icon = (char *)ptr->text;
1655                         return PMINFO_R_OK;
1656                 }
1657         }
1658
1659         return PMINFO_R_ERROR;
1660 }
1661
1662 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_recentimage *type)
1663 {
1664         char *val;
1665         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1666
1667         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1668         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1669
1670         if (info->app_info == NULL || info->app_info->recentimage == NULL)
1671                 return PMINFO_R_ERROR;
1672
1673         val = (char *)info->app_info->recentimage;
1674         if (strcasecmp(val, "capture") == 0)
1675                 *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
1676         else if (strcasecmp(val, "icon") == 0)
1677                 *type = PMINFO_RECENTIMAGE_USE_ICON;
1678         else
1679                 *type = PMINFO_RECENTIMAGE_USE_NOTHING;
1680
1681         return PMINFO_R_OK;
1682 }
1683
1684 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h handle, char **preview_img)
1685 {
1686         char *val;
1687         image_x *ptr;
1688         GList *tmp;
1689         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1690
1691         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1692         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1693
1694         if (info->app_info == NULL)
1695                 return PMINFO_R_ERROR;
1696
1697         for (tmp = info->app_info->image; tmp; tmp = tmp->next) {
1698                 ptr = (image_x *)tmp->data;
1699                 if (ptr == NULL || ptr->section == NULL)
1700                         continue;
1701
1702                 val = (char *)ptr->section;
1703                 if (val && strcmp(val, "preview") == 0) {
1704                         *preview_img = (char *)ptr->text;
1705                         return PMINFO_R_OK;
1706                 }
1707         }
1708
1709         return PMINFO_R_ERROR;
1710 }
1711
1712 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_permission_type *permission)
1713 {
1714         const char *val;
1715         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1716
1717         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1718         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1719
1720         val = info->app_info->permission_type;
1721         if (val == NULL)
1722                 return PMINFO_R_ERROR;
1723
1724         if (strcmp(val, "signature") == 0)
1725                 *permission = PMINFO_PERMISSION_SIGNATURE;
1726         else if (strcmp(val, "privilege") == 0)
1727                 *permission = PMINFO_PERMISSION_PRIVILEGE;
1728         else
1729                 *permission = PMINFO_PERMISSION_NORMAL;
1730
1731         return PMINFO_R_OK;
1732 }
1733
1734 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h handle, char **component_type)
1735 {
1736         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1737
1738         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1739         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1740
1741         if (info->app_info == NULL || info->app_info->component_type == NULL)
1742                 return PMINFO_R_ERROR;
1743
1744         *component_type = (char *)info->app_info->component_type;
1745
1746         return PMINFO_R_OK;
1747 }
1748
1749 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
1750 {
1751         char *val;
1752         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1753
1754         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1755         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1756
1757         if (info->app_info == NULL || info->app_info->hwacceleration == NULL)
1758                 return PMINFO_R_ERROR;
1759
1760         val = (char *)info->app_info->hwacceleration;
1761         if (strcasecmp(val, "not-use-GL") == 0)
1762                 *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
1763         else if (strcasecmp(val, "use-GL") == 0)
1764                 *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
1765         else
1766                 *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
1767
1768         return PMINFO_R_OK;
1769 }
1770
1771 API int pkgmgrinfo_appinfo_get_screenreader(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_screenreader *screenreader)
1772 {
1773         char *val;
1774         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1775
1776         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1777         retvm_if(screenreader == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1778
1779         if (info->app_info == NULL || info->app_info->screenreader == NULL)
1780                 return PMINFO_R_ERROR;
1781
1782         val = (char *)info->app_info->screenreader;
1783         if (strcasecmp(val, "screenreader-off") == 0)
1784                 *screenreader = PMINFO_SCREENREADER_OFF;
1785         else if (strcasecmp(val, "screenreader-on") == 0)
1786                 *screenreader = PMINFO_SCREENREADER_ON;
1787         else
1788                 *screenreader = PMINFO_SCREENREADER_USE_SYSTEM_SETTING;
1789
1790         return PMINFO_R_OK;
1791 }
1792
1793 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h handle, char **portrait_img, char **landscape_img)
1794 {
1795         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1796
1797         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1798         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1799         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1800
1801         if (info->app_info == NULL || (info->app_info->portraitimg == NULL
1802                         && info->app_info->landscapeimg == NULL))
1803                 return PMINFO_R_ERROR;
1804
1805         *portrait_img = (char *)info->app_info->portraitimg;
1806         *landscape_img = (char *)info->app_info->landscapeimg;
1807
1808         return PMINFO_R_OK;
1809 }
1810
1811 API int pkgmgrinfo_appinfo_get_effectimage_type(pkgmgrinfo_appinfo_h handle, char **effectimage_type)
1812 {
1813         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1814
1815         if (handle == NULL || effectimage_type == NULL) {
1816                 LOGE("invalid parameter");
1817                 return PMINFO_R_EINVAL;
1818         }
1819
1820         if (info->app_info == NULL || info->app_info->effectimage_type == NULL)
1821                 return PMINFO_R_ERROR;
1822
1823         *effectimage_type = (char *)info->app_info->effectimage_type;
1824
1825         return PMINFO_R_OK;
1826 }
1827
1828 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
1829 {
1830         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1831
1832         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1833         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1834
1835         if (info->app_info == NULL || info->app_info->submode_mainid == NULL)
1836                 return PMINFO_R_ERROR;
1837
1838         *submode_mainid = (char *)info->app_info->submode_mainid;
1839
1840         return PMINFO_R_OK;
1841 }
1842
1843 API int pkgmgrinfo_appinfo_get_installed_storage_location(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_installed_storage *storage)
1844 {
1845         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1846         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1847
1848         if (info->app_info && info->app_info->installed_storage){
1849                  if (strcmp(info->app_info->installed_storage,"installed_internal") == 0)
1850                         *storage = PMINFO_INTERNAL_STORAGE;
1851                  else if (strcmp(info->app_info->installed_storage,"installed_external") == 0)
1852                          *storage = PMINFO_EXTERNAL_STORAGE;
1853                  else
1854                          return PMINFO_R_ERROR;
1855         }else
1856                 return PMINFO_R_ERROR;
1857
1858         return PMINFO_R_OK;
1859 }
1860
1861 API int pkgmgrinfo_appinfo_get_launch_mode(pkgmgrinfo_appinfo_h handle, char **mode)
1862 {
1863         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1864
1865         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1866         retvm_if(mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1867
1868         if (info->app_info->launch_mode == NULL)
1869                 return PMINFO_R_ERROR;
1870
1871         *mode = (char *)(info->app_info->launch_mode);
1872
1873         return PMINFO_R_OK;
1874 }
1875
1876 API int pkgmgrinfo_appinfo_get_alias_appid(pkgmgrinfo_appinfo_h handle, char **alias_appid)
1877 {
1878         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1879
1880         if (handle == NULL || alias_appid == NULL) {
1881                 LOGE("invalid parameter");
1882                 return PMINFO_R_EINVAL;
1883         }
1884
1885         if (info->app_info == NULL || info->app_info->alias_appid == NULL)
1886                 return PMINFO_R_ERROR;
1887
1888         *alias_appid = (char *)info->app_info->alias_appid;
1889
1890         return PMINFO_R_OK;
1891 }
1892
1893 API int pkgmgrinfo_appinfo_get_effective_appid(pkgmgrinfo_appinfo_h handle, char **effective_appid)
1894 {
1895         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1896
1897         if (handle == NULL || effective_appid == NULL) {
1898                 LOGE("invalid parameter");
1899                 return PMINFO_R_EINVAL;
1900         }
1901
1902         if (info->app_info == NULL || info->app_info->effective_appid == NULL)
1903                 return PMINFO_R_ERROR;
1904
1905         *effective_appid = (char *)info->app_info->effective_appid;
1906
1907         return PMINFO_R_OK;
1908 }
1909
1910 API int pkgmgrinfo_appinfo_usr_get_datacontrol_info(const char *providerid, const char *type, uid_t uid, char **appid, char **access)
1911 {
1912         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1913         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1914         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1915         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1916
1917         int ret = PMINFO_R_OK;
1918         char *query = NULL;
1919         sqlite3_stmt *stmt = NULL;
1920
1921         /*open db*/
1922         ret = __open_manifest_db(uid, true);
1923         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1924
1925         /*Start constructing query*/
1926         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q and type=%Q", providerid, type);
1927
1928         /*prepare query*/
1929         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1930         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1931
1932         /*step query*/
1933         ret = sqlite3_step(stmt);
1934         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1935
1936         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1937         *access = strdup((char *)sqlite3_column_text(stmt, 2));
1938
1939         ret = PMINFO_R_OK;
1940
1941 catch:
1942         sqlite3_free(query);
1943         sqlite3_finalize(stmt);
1944         __close_manifest_db();
1945         return ret;
1946 }
1947
1948 API int pkgmgrinfo_appinfo_get_datacontrol_info(const char *providerid, const char *type, char **appid, char **access)
1949 {
1950         return pkgmgrinfo_appinfo_usr_get_datacontrol_info(providerid, type, GLOBAL_USER, appid, access);
1951 }
1952
1953 API int pkgmgrinfo_appinfo_usr_get_datacontrol_appid(const char *providerid, uid_t uid, char **appid)
1954 {
1955         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1956         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1957
1958         int ret = PMINFO_R_OK;
1959         char *query = NULL;
1960         sqlite3_stmt *stmt = NULL;
1961
1962         /*open db*/
1963         ret = __open_manifest_db(uid, true);
1964         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1965
1966         /*Start constructing query*/
1967         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q", providerid);
1968
1969         /*prepare query*/
1970         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1971         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1972
1973         /*step query*/
1974         ret = sqlite3_step(stmt);
1975         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1976
1977         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1978
1979         ret = PMINFO_R_OK;
1980
1981 catch:
1982         sqlite3_free(query);
1983         sqlite3_finalize(stmt);
1984         __close_manifest_db();
1985         return ret;
1986 }
1987
1988 API int pkgmgrinfo_appinfo_get_datacontrol_appid(const char *providerid, char **appid)
1989 {
1990         return pkgmgrinfo_appinfo_usr_get_datacontrol_appid(providerid, GLOBAL_USER, appid);
1991 }
1992
1993 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
1994                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
1995 {
1996         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1997         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1998         int ret = -1;
1999         permission_x *ptr;
2000         GList *tmp;
2001         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2002
2003         if (info->app_info == NULL)
2004                 return PMINFO_R_ERROR;
2005
2006         for (tmp = info->app_info->permission; tmp; tmp = tmp->next) {
2007                 ptr = (permission_x *)tmp->data;
2008                 if (ptr == NULL)
2009                         continue;
2010                 if (ptr->value) {
2011                         ret = permission_func(ptr->value, user_data);
2012                         if (ret < 0)
2013                                 break;
2014                 }
2015         }
2016         return PMINFO_R_OK;
2017 }
2018
2019 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
2020                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
2021 {
2022         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2023         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2024         int ret = -1;
2025         const char *category;
2026         GList *tmp;
2027         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2028
2029         if (info->app_info == NULL)
2030                 return PMINFO_R_ERROR;
2031
2032         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
2033                 category = (const char *)tmp->data;
2034                 if (category) {
2035                         ret = category_func(category, user_data);
2036                         if (ret < 0)
2037                                 break;
2038                 }
2039         }
2040         return PMINFO_R_OK;
2041 }
2042
2043 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
2044                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
2045 {
2046         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2047         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2048         int ret = -1;
2049         metadata_x *ptr;
2050         GList *tmp;
2051         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2052
2053         if (info->app_info == NULL)
2054                 return PMINFO_R_ERROR;
2055
2056         for (tmp = info->app_info->metadata; tmp; tmp = tmp->next) {
2057                 ptr = (metadata_x *)tmp->data;
2058                 if (ptr == NULL)
2059                         continue;
2060                 if (ptr->key) {
2061                         ret = metadata_func(ptr->key, ptr->value, user_data);
2062                         if (ret < 0)
2063                                 break;
2064                 }
2065         }
2066         return PMINFO_R_OK;
2067 }
2068
2069 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
2070                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
2071 {
2072         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2073         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2074         int ret;
2075         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2076         appcontrol_x *appcontrol;
2077         GList *tmp;
2078
2079         if (info->app_info == NULL)
2080                 return PMINFO_R_ERROR;
2081
2082         for (tmp = info->app_info->appcontrol; tmp; tmp = tmp->next) {
2083                 appcontrol = (appcontrol_x *)tmp->data;
2084                 if (appcontrol == NULL)
2085                         continue;
2086                 ret = appcontrol_func(appcontrol->operation, appcontrol->uri, appcontrol->mime, user_data);
2087                 if (ret < 0)
2088                         break;
2089         }
2090
2091         return PMINFO_R_OK;
2092 }
2093
2094 API int pkgmgrinfo_appinfo_foreach_background_category(
2095                 pkgmgrinfo_appinfo_h handle,
2096                 pkgmgrinfo_app_background_category_list_cb category_func,
2097                 void *user_data)
2098 {
2099         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2100         GList *tmp;
2101         char *category;
2102
2103         if (handle == NULL || category_func == NULL || info->app_info == NULL) {
2104                 LOGE("invalid parameter");
2105                 return PMINFO_R_EINVAL;
2106         }
2107
2108         for (tmp = info->app_info->background_category; tmp; tmp = tmp->next) {
2109                 category = (char *)tmp->data;
2110                 if (category == NULL)
2111                         continue;
2112
2113                 if (category_func(category, user_data) < 0)
2114                         break;
2115         }
2116
2117         return PMINFO_R_OK;
2118 }
2119
2120 API int pkgmgrinfo_appinfo_foreach_splash_screen(pkgmgrinfo_appinfo_h handle,
2121                 pkgmgrinfo_app_splash_screen_list_cb splash_screen_func,
2122                 void *user_data)
2123 {
2124         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2125         splashscreen_x *splashscreen;
2126         GList *tmp;
2127         int ret;
2128
2129         if (info == NULL || info->app_info == NULL
2130                         || splash_screen_func == NULL) {
2131                 LOGE("invalid parameter");
2132                 return PMINFO_R_EINVAL;
2133         }
2134
2135         for (tmp = info->app_info->splashscreens; tmp; tmp = tmp->next) {
2136                 splashscreen = (splashscreen_x *)tmp->data;
2137                 if (splashscreen == NULL)
2138                         continue;
2139                 ret = splash_screen_func(splashscreen->src,
2140                                 splashscreen->type,
2141                                 splashscreen->orientation,
2142                                 splashscreen->indicatordisplay,
2143                                 splashscreen->operation,
2144                                 user_data);
2145                 if (ret < 0)
2146                         break;
2147         }
2148
2149         return PMINFO_R_OK;
2150 }
2151
2152 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h handle, bool *nodisplay)
2153 {
2154         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2155         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2156         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2157
2158         if (info->app_info == NULL || info->app_info->nodisplay == NULL)
2159                 return PMINFO_R_ERROR;
2160
2161         *nodisplay = _get_bool_value(info->app_info->nodisplay);
2162
2163         return PMINFO_R_OK;
2164 }
2165
2166 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h handle, bool *multiple)
2167 {
2168         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2169         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2170         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2171
2172         if (info->app_info == NULL || info->app_info->multiple == NULL)
2173                 return PMINFO_R_ERROR;
2174
2175         *multiple = _get_bool_value(info->app_info->multiple);
2176
2177         return PMINFO_R_OK;
2178 }
2179
2180 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
2181 {
2182         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2183         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2184         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2185
2186         if (info->app_info == NULL || info->app_info->indicatordisplay == NULL)
2187                 return PMINFO_R_ERROR;
2188
2189         *indicator_disp = _get_bool_value(info->app_info->indicatordisplay);
2190
2191         return PMINFO_R_OK;
2192 }
2193
2194 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
2195 {
2196         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2197         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2198         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2199
2200         if (info->app_info == NULL || info->app_info->taskmanage == NULL)
2201                 return PMINFO_R_ERROR;
2202
2203         *taskmanage = _get_bool_value(info->app_info->taskmanage);
2204
2205         return PMINFO_R_OK;
2206 }
2207
2208 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
2209 {
2210         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2211         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2212         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2213
2214         if (info->app_info == NULL || info->app_info->enabled == NULL)
2215                 return PMINFO_R_ERROR;
2216
2217         *enabled = _get_bool_value(info->app_info->enabled);
2218
2219         return PMINFO_R_OK;
2220 }
2221
2222 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
2223 {
2224         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2225         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2226         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2227
2228         if (info->app_info == NULL || info->app_info->onboot == NULL)
2229                 return PMINFO_R_ERROR;
2230
2231         *onboot = _get_bool_value(info->app_info->onboot);
2232
2233         return PMINFO_R_OK;
2234 }
2235
2236 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
2237 {
2238         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2239         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2240         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2241
2242         if (info->app_info == NULL || info->app_info->autorestart == NULL)
2243                 return PMINFO_R_ERROR;
2244
2245         *autorestart = _get_bool_value(info->app_info->autorestart);
2246
2247         return PMINFO_R_OK;
2248 }
2249
2250 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
2251 {
2252         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2253         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2254         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2255
2256         if (info->app_info == NULL || info->app_info->mainapp == NULL)
2257                 return PMINFO_R_ERROR;
2258
2259         *mainapp = _get_bool_value(info->app_info->mainapp);
2260
2261         return PMINFO_R_OK;
2262 }
2263
2264 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
2265 {
2266         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2267         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2268         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2269
2270         if (info->app_info == NULL || info->app_info->preload == NULL)
2271                 return PMINFO_R_ERROR;
2272
2273         *preload = _get_bool_value(info->app_info->preload);
2274
2275         return PMINFO_R_OK;
2276 }
2277
2278 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
2279 {
2280         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2281         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2282         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2283
2284         if (info->app_info == NULL || info->app_info->submode == NULL)
2285                 return PMINFO_R_ERROR;
2286
2287         *submode = _get_bool_value(info->app_info->submode);
2288
2289         return PMINFO_R_OK;
2290 }
2291
2292 API int pkgmgrinfo_appinfo_is_process_pool(pkgmgrinfo_appinfo_h handle, bool *process_pool)
2293 {
2294         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2295
2296         if (handle == NULL || process_pool == NULL) {
2297                 LOGE("invalid parameter");
2298                 return PMINFO_R_EINVAL;
2299         }
2300
2301         if (info->app_info == NULL)
2302                 return PMINFO_R_ERROR;
2303
2304         *process_pool = _get_bool_value(info->app_info->process_pool);
2305
2306         return PMINFO_R_OK;
2307 }
2308
2309 API int pkgmgrinfo_appinfo_is_category_exist(pkgmgrinfo_appinfo_h handle, const char *category, bool *exist)
2310 {
2311         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2312         retvm_if(category == NULL, PMINFO_R_EINVAL, "category is NULL");
2313         retvm_if(exist == NULL, PMINFO_R_EINVAL, "exist is NULL");
2314
2315         const char *val;
2316         GList *tmp;
2317         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2318
2319         if (info->app_info == NULL)
2320                 return PMINFO_R_ERROR;
2321
2322         *exist = 0;
2323         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
2324                 val = (const char *)tmp->data;
2325                 if (val == NULL)
2326                         continue;
2327                 if (strcasecmp(val, category) == 0) {
2328                         *exist = 1;
2329                         break;
2330                 }
2331         }
2332
2333         return PMINFO_R_OK;
2334 }
2335
2336 API int pkgmgrinfo_appinfo_is_ui_gadget(pkgmgrinfo_appinfo_h handle,
2337                 bool *ui_gadget)
2338 {
2339         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2340
2341         if (info == NULL || info->app_info == NULL || ui_gadget == NULL) {
2342                 _LOGE("invalid parameter");
2343                 return PMINFO_R_EINVAL;
2344         }
2345
2346         *ui_gadget = _get_bool_value(info->app_info->ui_gadget);
2347
2348         return PMINFO_R_OK;
2349 }
2350
2351 API int pkgmgrinfo_appinfo_is_support_disable(pkgmgrinfo_appinfo_h handle,
2352                 bool *support_disable)
2353 {
2354         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2355
2356         if (info == NULL || info->app_info == NULL || support_disable == NULL) {
2357                 _LOGE("invalid parameter");
2358                 return PMINFO_R_EINVAL;
2359         }
2360
2361         *support_disable = _get_bool_value(info->app_info->support_disable);
2362
2363         return PMINFO_R_OK;
2364 }
2365
2366 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle)
2367 {
2368         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2369         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2370         __cleanup_appinfo(info);
2371         return PMINFO_R_OK;
2372 }
2373
2374 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
2375 {
2376         return (pkgmgrinfo_pkginfo_filter_create(handle));
2377 }
2378
2379 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
2380 {
2381         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
2382 }
2383
2384 static gint __compare_func(gconstpointer data1, gconstpointer data2)
2385 {
2386         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
2387         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
2388         if (node1->prop == node2->prop)
2389                 return 0;
2390         else if (node1->prop > node2->prop)
2391                 return 1;
2392         else
2393                 return -1;
2394 }
2395
2396 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
2397                                 const char *property, const int value)
2398 {
2399         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2400         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2401         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
2402         char *val = NULL;
2403         GSList *link = NULL;
2404         int prop = -1;
2405         prop = _pminfo_appinfo_convert_to_prop_int(property);
2406         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
2407                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
2408                 _LOGE("Invalid Integer Property\n");
2409                 return PMINFO_R_EINVAL;
2410         }
2411         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2412         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2413         if (node == NULL) {
2414                 _LOGE("Out of Memory!!!\n");
2415                 return PMINFO_R_ERROR;
2416         }
2417         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
2418         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
2419         if (val == NULL) {
2420                 _LOGE("Out of Memory\n");
2421                 free(node);
2422                 node = NULL;
2423                 return PMINFO_R_ERROR;
2424         }
2425         node->prop = prop;
2426         node->value = val;
2427         /*If API is called multiple times for same property, we should override the previous values.
2428         Last value set will be used for filtering.*/
2429         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2430         if (link)
2431                 filter->list = g_slist_delete_link(filter->list, link);
2432         filter->list = g_slist_append(filter->list, (gpointer)node);
2433         return PMINFO_R_OK;
2434
2435 }
2436
2437 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
2438                                 const char *property, const bool value)
2439 {
2440         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2441         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2442         char *val = NULL;
2443         GSList *link = NULL;
2444         int prop = -1;
2445         prop = _pminfo_appinfo_convert_to_prop_bool(property);
2446         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
2447                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
2448                 _LOGE("Invalid Boolean Property\n");
2449                 return PMINFO_R_EINVAL;
2450         }
2451         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2452         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2453         if (node == NULL) {
2454                 _LOGE("Out of Memory!!!\n");
2455                 return PMINFO_R_ERROR;
2456         }
2457         if (value)
2458                 val = strndup("('true','True')", 15);
2459         else
2460                 val = strndup("('false','False')", 17);
2461         if (val == NULL) {
2462                 _LOGE("Out of Memory\n");
2463                 free(node);
2464                 node = NULL;
2465                 return PMINFO_R_ERROR;
2466         }
2467         node->prop = prop;
2468         node->value = val;
2469         /*If API is called multiple times for same property, we should override the previous values.
2470         Last value set will be used for filtering.*/
2471         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2472         if (link)
2473                 filter->list = g_slist_delete_link(filter->list, link);
2474         filter->list = g_slist_append(filter->list, (gpointer)node);
2475         return PMINFO_R_OK;
2476
2477 }
2478
2479 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
2480                                 const char *property, const char *value)
2481 {
2482         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2483         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2484         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2485         char *val = NULL;
2486         pkgmgrinfo_node_x *ptr = NULL;
2487         char prev[PKG_STRING_LEN_MAX] = {'\0'};
2488         char temp[PKG_STRING_LEN_MAX] = {'\0'};
2489         GSList *link = NULL;
2490         int prop = -1;
2491         prop = _pminfo_appinfo_convert_to_prop_str(property);
2492         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
2493                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
2494                 _LOGE("Invalid String Property\n");
2495                 return PMINFO_R_EINVAL;
2496         }
2497         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2498         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2499         if (node == NULL) {
2500                 _LOGE("Out of Memory!!!\n");
2501                 return PMINFO_R_ERROR;
2502         }
2503         node->prop = prop;
2504         switch (prop) {
2505         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
2506                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
2507                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
2508                 else
2509                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
2510                 node->value = val;
2511                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2512                 if (link)
2513                         filter->list = g_slist_delete_link(filter->list, link);
2514                 filter->list = g_slist_append(filter->list, (gpointer)node);
2515                 break;
2516         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
2517         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
2518         case E_PMINFO_APPINFO_PROP_APP_URI:
2519         case E_PMINFO_APPINFO_PROP_APP_MIME:
2520                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
2521                 if (val == NULL) {
2522                         _LOGE("Out of Memory\n");
2523                         free(node);
2524                         node = NULL;
2525                         return PMINFO_R_ERROR;
2526                 }
2527                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2528                 if (link) {
2529                         ptr = (pkgmgrinfo_node_x *)link->data;
2530                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
2531                         _LOGE("Previous value is %s\n", prev);
2532                         filter->list = g_slist_delete_link(filter->list, link);
2533                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
2534                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
2535                         _LOGE("New value is %s\n", val);
2536                         node->value = val;
2537                         filter->list = g_slist_append(filter->list, (gpointer)node);
2538                         memset(temp, '\0', PKG_STRING_LEN_MAX);
2539                 } else {
2540                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
2541                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
2542                         _LOGE("First value is %s\n", val);
2543                         node->value = val;
2544                         filter->list = g_slist_append(filter->list, (gpointer)node);
2545                         memset(temp, '\0', PKG_STRING_LEN_MAX);
2546                 }
2547                 break;
2548         default:
2549                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
2550                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2551                 if (link)
2552                         filter->list = g_slist_delete_link(filter->list, link);
2553                 filter->list = g_slist_append(filter->list, (gpointer)node);
2554                 break;
2555         }
2556         return PMINFO_R_OK;
2557 }
2558
2559 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
2560 {
2561         int ret;
2562         GList *list = NULL;
2563
2564         if (handle == NULL || count == NULL) {
2565                 _LOGE("invalid parameter");
2566                 return PMINFO_R_EINVAL;
2567         }
2568
2569         ret = _appinfo_get_filtered_list(handle, uid, &list);
2570         if (ret != PMINFO_R_OK)
2571                 return PMINFO_R_ERROR;
2572
2573         *count = g_list_length(list);
2574
2575         g_list_free_full(list, free);
2576
2577         return PMINFO_R_OK;
2578 }
2579
2580 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
2581 {
2582         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, GLOBAL_USER);
2583 }
2584
2585 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(
2586                 pkgmgrinfo_appinfo_filter_h handle,
2587                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2588 {
2589         if (handle == NULL || app_cb == NULL) {
2590                 LOGE("invalid parameter");
2591                 return PMINFO_R_EINVAL;
2592         }
2593
2594         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2595                         user_data);
2596 }
2597
2598 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
2599                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
2600 {
2601         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, GLOBAL_USER);
2602 }
2603
2604 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
2605 {
2606         return (pkgmgrinfo_pkginfo_filter_create(handle));
2607 }
2608
2609 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
2610 {
2611         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
2612 }
2613
2614 API int pkgmgrinfo_appinfo_metadata_filter_add(
2615                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2616                 const char *key, const char *value)
2617 {
2618         int ret;
2619
2620         ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2621                         PMINFO_APPINFO_PROP_APP_METADATA_KEY, key);
2622         if (ret != PMINFO_R_OK)
2623                 return ret;
2624
2625         /* value can be NULL.
2626          * In that case all apps with specified key should be displayed
2627          */
2628         if (value) {
2629                 ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2630                                 PMINFO_APPINFO_PROP_APP_METADATA_VALUE, value);
2631                 if (ret != PMINFO_R_OK)
2632                         return ret;
2633         }
2634
2635         return PMINFO_R_OK;
2636 }
2637
2638 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(
2639                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2640                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2641 {
2642         if (handle == NULL || app_cb == NULL) {
2643                 LOGE("invalid parameter");
2644                 return PMINFO_R_EINVAL;
2645         }
2646
2647         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2648                         user_data);
2649 }
2650
2651 API int pkgmgrinfo_appinfo_metadata_filter_foreach(
2652                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2653                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
2654 {
2655         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb,
2656                         user_data, GLOBAL_USER);
2657 }
2658
2659 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
2660 {
2661         const char *val;
2662         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2663
2664         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2665         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2666
2667         val = info->app_info->guestmode_visibility;
2668         *status = _get_bool_value(val);
2669         return PMINFO_R_OK;
2670 }