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