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