Parse support-disable
[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                 "app_support_disable, "
568                 "component_type, package "
569                 "FROM package_app_info WHERE app_id=%Q";
570         int ret;
571         char *query;
572         sqlite3_stmt *stmt;
573         int idx;
574         application_x *info;
575
576         query = sqlite3_mprintf(query_raw, appid);
577         if (query == NULL) {
578                 LOGE("out of memory");
579                 return PMINFO_R_ERROR;
580         }
581
582         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
583         sqlite3_free(query);
584         if (ret != SQLITE_OK) {
585                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
586                 return PMINFO_R_ERROR;
587         }
588
589         ret = sqlite3_step(stmt);
590         if (ret == SQLITE_DONE) {
591                 sqlite3_finalize(stmt);
592                 return PMINFO_R_ENOENT;
593         } else if (ret != SQLITE_ROW) {
594                 LOGE("step failed: %s", sqlite3_errmsg(db));
595                 sqlite3_finalize(stmt);
596                 return PMINFO_R_ERROR;
597         }
598
599         info = calloc(1, sizeof(application_x));
600         if (info == NULL) {
601                 LOGE("out of memory");
602                 sqlite3_finalize(stmt);
603                 return PMINFO_R_ERROR;
604         }
605         idx = 0;
606         _save_column_str(stmt, idx++, &info->appid);
607         _save_column_str(stmt, idx++, &info->component);
608         _save_column_str(stmt, idx++, &info->exec);
609         _save_column_str(stmt, idx++, &info->nodisplay);
610         _save_column_str(stmt, idx++, &info->type);
611         _save_column_str(stmt, idx++, &info->onboot);
612         _save_column_str(stmt, idx++, &info->multiple);
613         _save_column_str(stmt, idx++, &info->autorestart);
614         _save_column_str(stmt, idx++, &info->taskmanage);
615         _save_column_str(stmt, idx++, &info->enabled);
616         _save_column_str(stmt, idx++, &info->hwacceleration);
617         _save_column_str(stmt, idx++, &info->screenreader);
618         _save_column_str(stmt, idx++, &info->mainapp);
619         _save_column_str(stmt, idx++, &info->recentimage);
620         _save_column_str(stmt, idx++, &info->launchcondition);
621         _save_column_str(stmt, idx++, &info->indicatordisplay);
622         _save_column_str(stmt, idx++, &info->portraitimg);
623         _save_column_str(stmt, idx++, &info->landscapeimg);
624         _save_column_str(stmt, idx++, &info->guestmode_visibility);
625         _save_column_str(stmt, idx++, &info->permission_type);
626         _save_column_str(stmt, idx++, &info->preload);
627         _save_column_str(stmt, idx++, &info->submode);
628         _save_column_str(stmt, idx++, &info->submode_mainid);
629         _save_column_str(stmt, idx++, &info->launch_mode);
630         _save_column_str(stmt, idx++, &info->ui_gadget);
631         _save_column_str(stmt, idx++, &info->support_disable);
632         _save_column_str(stmt, idx++, &info->component_type);
633         _save_column_str(stmt, idx++, &info->package);
634
635         if (_appinfo_get_label(db, info->appid, locale, &info->label)) {
636                 pkgmgrinfo_basic_free_application(info);
637                 sqlite3_finalize(stmt);
638                 return PMINFO_R_ERROR;
639         }
640
641         if (_appinfo_get_icon(db, info->appid, locale, &info->icon)) {
642                 pkgmgrinfo_basic_free_application(info);
643                 sqlite3_finalize(stmt);
644                 return PMINFO_R_ERROR;
645         }
646
647         if (_appinfo_get_category(db, info->appid, &info->category)) {
648                 pkgmgrinfo_basic_free_application(info);
649                 sqlite3_finalize(stmt);
650                 return PMINFO_R_ERROR;
651         }
652
653         if (_appinfo_get_app_control(db, info->appid, &info->appcontrol)) {
654                 pkgmgrinfo_basic_free_application(info);
655                 sqlite3_finalize(stmt);
656                 return PMINFO_R_ERROR;
657         }
658
659         if (_appinfo_get_data_control(db, info->appid, &info->datacontrol)) {
660                 pkgmgrinfo_basic_free_application(info);
661                 sqlite3_finalize(stmt);
662                 return PMINFO_R_ERROR;
663         }
664
665         if (_appinfo_get_metadata(db, info->appid, &info->metadata)) {
666                 pkgmgrinfo_basic_free_application(info);
667                 sqlite3_finalize(stmt);
668                 return PMINFO_R_ERROR;
669         }
670
671         *application = info;
672
673         sqlite3_finalize(stmt);
674
675         return PMINFO_R_OK;
676 }
677
678 static int _appinfo_get_appinfo(const char *appid, uid_t uid,
679                 pkgmgr_appinfo_x **appinfo)
680 {
681         int ret;
682         sqlite3 *db;
683         const char *dbpath;
684         char *locale;
685         pkgmgr_appinfo_x *info;
686
687         dbpath = getUserPkgParserDBPathUID(uid);
688         if (dbpath == NULL)
689                 return PMINFO_R_ERROR;
690
691         locale = _get_system_locale();
692         if (locale == NULL)
693                 return PMINFO_R_ERROR;
694
695         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
696         if (ret != SQLITE_OK) {
697                 _LOGE("failed to open db: %d", ret);
698                 free(locale);
699                 return PMINFO_R_ERROR;
700         }
701
702         info = calloc(1, sizeof(pkgmgr_appinfo_x));
703         if (info == NULL) {
704                 _LOGE("out of memory");
705                 free(locale);
706                 sqlite3_close_v2(db);
707                 return PMINFO_R_ERROR;
708         }
709
710         ret = _appinfo_get_application(db, appid, locale, &info->app_info);
711         if (ret == PMINFO_R_OK) {
712                 info->locale = strdup(locale);
713                 info->package = strdup(info->app_info->package);
714         }
715
716         *appinfo = info;
717
718         free(locale);
719         sqlite3_close_v2(db);
720
721         return ret;
722 }
723
724 API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid,
725                 pkgmgrinfo_appinfo_h *handle)
726 {
727         int ret;
728
729         if (appid == NULL || handle == NULL) {
730                 LOGE("invalid parameter");
731                 return PMINFO_R_EINVAL;
732         }
733
734         ret = _appinfo_get_appinfo(appid, uid, (pkgmgr_appinfo_x **)handle);
735         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
736                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
737                                 (pkgmgr_appinfo_x **)handle);
738
739         if (ret != PMINFO_R_OK)
740                 _LOGE("failed to get appinfo of %s for user %d", appid, uid);
741
742         return ret;
743 }
744
745 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
746 {
747         return pkgmgrinfo_appinfo_get_usr_appinfo(appid, GLOBAL_USER, handle);
748 }
749
750 static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
751                 pkgmgrinfo_filter_x *filter, pkgmgrinfo_app_list_cb app_list_cb,
752                 void *user_data)
753 {
754         int ret;
755         pkgmgr_appinfo_x *info;
756         GList *list = NULL;
757         GList *tmp;
758         char *appid;
759         int stop = 0;
760
761         ret = _appinfo_get_filtered_list(filter, uid, &list);
762         if (ret != PMINFO_R_OK)
763                 return PMINFO_R_ERROR;
764
765         for (tmp = list; tmp; tmp = tmp->next) {
766                 appid = (char *)tmp->data;
767                 if (stop == 0) {
768                         ret = _appinfo_get_appinfo(appid, uid, &info);
769                         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
770                                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
771                                                 &info);
772                         if (ret != PMINFO_R_OK) {
773                                 free(appid);
774                                 continue;
775                         }
776                         if (app_list_cb(info, user_data) < 0)
777                                 stop = 1;
778                         pkgmgrinfo_appinfo_destroy_appinfo(info);
779                 }
780                 free(appid);
781         }
782
783         g_list_free(list);
784
785         return PMINFO_R_OK;
786 }
787
788 API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle,
789                 pkgmgrinfo_app_component component,
790                 pkgmgrinfo_app_list_cb app_func, void *user_data, uid_t uid)
791 {
792         int ret;
793         pkgmgrinfo_appinfo_filter_h filter;
794         char *pkgid;
795         const char *comp_str = NULL;
796
797         if (handle == NULL || app_func == NULL) {
798                 LOGE("invalied parameter");
799                 return PMINFO_R_EINVAL;
800         }
801
802         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid)) {
803                 LOGE("invalid parameter");
804                 return PMINFO_R_EINVAL;
805         }
806
807         if (pkgmgrinfo_appinfo_filter_create(&filter))
808                 return PMINFO_R_ERROR;
809
810         if (pkgmgrinfo_appinfo_filter_add_string(filter,
811                                 PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid)) {
812                 pkgmgrinfo_appinfo_filter_destroy(filter);
813                 return PMINFO_R_ERROR;
814         }
815
816
817         switch (component) {
818         case PMINFO_UI_APP:
819                 comp_str = PMINFO_APPINFO_UI_APP;
820                 break;
821         case PMINFO_SVC_APP:
822                 comp_str = PMINFO_APPINFO_SVC_APP;
823                 break;
824         default:
825                 break;
826         }
827
828         if (comp_str) {
829                 if (pkgmgrinfo_appinfo_filter_add_string(filter,
830                                         PMINFO_APPINFO_PROP_APP_COMPONENT,
831                                         comp_str)) {
832                         pkgmgrinfo_appinfo_filter_destroy(filter);
833                         return PMINFO_R_ERROR;
834                 }
835         }
836
837         ret = _appinfo_get_filtered_foreach_appinfo(uid, filter, app_func,
838                         user_data);
839
840         pkgmgrinfo_appinfo_filter_destroy(filter);
841
842         return ret;
843 }
844
845 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
846                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
847 {
848         return pkgmgrinfo_appinfo_get_usr_list(handle, component, app_func, user_data, GLOBAL_USER);
849 }
850
851 API int pkgmgrinfo_appinfo_get_usr_install_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
852 {
853         if (app_func == NULL) {
854                 LOGE("invalid parameter");
855                 return PMINFO_R_EINVAL;
856         }
857
858         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
859                         user_data);
860 }
861
862 API int pkgmgrinfo_appinfo_get_install_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
863 {
864         return pkgmgrinfo_appinfo_get_usr_install_list(app_func, GLOBAL_USER, user_data);
865 }
866
867 API int pkgmgrinfo_appinfo_get_usr_installed_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
868 {
869         if (app_func == NULL) {
870                 LOGE("invalid parameter");
871                 return PMINFO_R_EINVAL;
872         }
873
874         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
875                         user_data);
876 }
877
878 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
879 {
880         return pkgmgrinfo_appinfo_get_usr_installed_list(app_func, GLOBAL_USER, user_data);
881 }
882
883 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h handle, char **appid)
884 {
885         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
886
887         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
888         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
889
890         if (info->app_info == NULL || info->app_info->appid == NULL)
891                 return PMINFO_R_ERROR;
892         *appid = (char *)info->app_info->appid;
893
894         return PMINFO_R_OK;
895 }
896
897 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, char **pkg_name)
898 {
899         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
900
901         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
902         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
903
904         if (info->package == NULL)
905                 return PMINFO_R_ERROR;
906
907         *pkg_name = (char *)info->package;
908
909         return PMINFO_R_OK;
910 }
911
912 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h handle, char **pkgid)
913 {
914         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
915
916         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
917         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
918
919         if (info->package == NULL)
920                 return PMINFO_R_ERROR;
921
922         *pkgid = (char *)info->package;
923
924         return PMINFO_R_OK;
925 }
926
927 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, char **exec)
928 {
929         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
930
931         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
932         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
933
934         if (info->app_info == NULL || info->app_info->exec == NULL)
935                 return PMINFO_R_ERROR;
936         *exec = (char *)info->app_info->exec;
937
938         return PMINFO_R_OK;
939 }
940
941
942 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, char **icon)
943 {
944         char *locale;
945         icon_x *ptr;
946         icon_x *start;
947         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
948
949         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
950         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
951
952         locale = info->locale;
953         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
954
955         start = info->app_info->icon;
956         for (ptr = start; ptr != NULL; ptr = ptr->next) {
957                 if (ptr->lang == NULL)
958                         continue;
959
960                 if (strcmp(ptr->lang, locale) == 0) {
961                         *icon = (char *)ptr->text;
962                         if (strcasecmp(*icon, "(null)") == 0) {
963                                 locale = DEFAULT_LOCALE;
964                                 continue;
965                         } else {
966                                 return PMINFO_R_OK;
967                         }
968                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
969                         *icon = (char *)ptr->text;
970                         return PMINFO_R_OK;
971                 }
972         }
973
974         return PMINFO_R_ERROR;
975 }
976
977
978 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, char **label)
979 {
980         char *locale;
981         label_x *ptr;
982         label_x *start;
983         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
984
985         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
986         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
987
988         locale = info->locale;
989         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
990
991         start = info->app_info->label;
992         for (ptr = start; ptr != NULL; ptr = ptr->next) {
993                 if (ptr->lang == NULL)
994                         continue;
995
996                 if (strcmp(ptr->lang, locale) == 0) {
997                         *label = (char *)ptr->text;
998                         if (strcasecmp(*label, "(null)") == 0) {
999                                 locale = DEFAULT_LOCALE;
1000                                 continue;
1001                         } else {
1002                                 return PMINFO_R_OK;
1003                         }
1004                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1005                         *label = (char *)ptr->text;
1006                         return PMINFO_R_OK;
1007                 }
1008         }
1009
1010         return PMINFO_R_ERROR;
1011 }
1012
1013 static char *_get_localed_label(const char *appid, const char *locale, uid_t uid)
1014 {
1015         char *result = NULL;
1016         char *query = NULL;
1017         sqlite3_stmt *stmt = NULL;
1018         sqlite3 *db = NULL;
1019         char *val;
1020         const char *manifest_db;
1021
1022         manifest_db = getUserPkgParserDBPathUID(uid);
1023         if (manifest_db == NULL) {
1024                 _LOGE("Failed to get manifest db path");
1025                 goto err;
1026         }
1027
1028         if (sqlite3_open_v2(manifest_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
1029                 _LOGE("DB open fail\n");
1030                 goto err;
1031         }
1032
1033         query = sqlite3_mprintf("select app_label from package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
1034         if (query == NULL) {
1035                 _LOGE("Out of memory");
1036                 goto err;
1037         }
1038
1039         if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) {
1040                 _LOGE("prepare_v2 fail\n");
1041                 goto err;
1042         }
1043
1044         if (sqlite3_step(stmt) == SQLITE_ROW) {
1045                 val = (char *)sqlite3_column_text(stmt, 0);
1046                 if (val != NULL)
1047                         result = strdup(val);
1048         }
1049
1050 err:
1051         sqlite3_finalize(stmt);
1052         sqlite3_free(query);
1053         sqlite3_close(db);
1054
1055         return result;
1056 }
1057
1058 API int pkgmgrinfo_appinfo_usr_get_localed_label(const char *appid, const char *locale, uid_t uid, char **label)
1059 {
1060         char *val;
1061
1062         retvm_if(appid == NULL || locale == NULL || label == NULL, PMINFO_R_EINVAL, "Argument is NULL");
1063
1064         val = _get_localed_label(appid, locale, uid);
1065         if (val == NULL)
1066                 val = _get_localed_label(appid, DEFAULT_LOCALE, uid);
1067
1068         if (val == NULL)
1069                 return PMINFO_R_ERROR;
1070
1071         *label = val;
1072
1073         return PMINFO_R_OK;
1074 }
1075
1076 API int pkgmgrinfo_appinfo_get_localed_label(const char *appid, const char *locale, char **label)
1077 {
1078         return pkgmgrinfo_appinfo_usr_get_localed_label(appid, locale, GLOBAL_USER, label);
1079 }
1080
1081 static pkgmgrinfo_app_component __appcomponent_convert(const char *comp)
1082 {
1083         if ( strcasecmp(comp, "uiapp") == 0)
1084                 return PMINFO_UI_APP;
1085         else if ( strcasecmp(comp, "svcapp") == 0)
1086                 return PMINFO_SVC_APP;
1087         else
1088                 return -1;
1089 }
1090
1091 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_component *component)
1092 {
1093         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1094         int comp;
1095
1096         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1097         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1098
1099         if (info->app_info == NULL)
1100                 return PMINFO_R_ERROR;
1101
1102         comp = __appcomponent_convert(info->app_info->component);
1103         if (comp < 0)
1104                 return PMINFO_R_ERROR;
1105
1106         *component = comp;
1107
1108         return PMINFO_R_OK;
1109 }
1110
1111 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, char **app_type)
1112 {
1113         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1114
1115         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1116         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1117
1118         if (info->app_info == NULL || info->app_info->type == NULL)
1119                 return PMINFO_R_ERROR;
1120         *app_type = (char *)info->app_info->type;
1121
1122         return PMINFO_R_OK;
1123 }
1124
1125 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
1126                                         int *operation_count, char ***operation)
1127 {
1128         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1129         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1130         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1131         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1132         *operation_count = data->operation_count;
1133         *operation = data->operation;
1134         return PMINFO_R_OK;
1135 }
1136
1137 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
1138                                         int *uri_count, char ***uri)
1139 {
1140         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1141         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1142         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1143         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1144         *uri_count = data->uri_count;
1145         *uri = data->uri;
1146         return PMINFO_R_OK;
1147 }
1148
1149 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
1150                                         int *mime_count, char ***mime)
1151 {
1152         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1153         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1154         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1155         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1156         *mime_count = data->mime_count;
1157         *mime = data->mime;
1158         return PMINFO_R_OK;
1159 }
1160
1161 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
1162                                         int *subapp_count, char ***subapp)
1163 {
1164         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1165         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1166         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1167         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1168         *subapp_count = data->subapp_count;
1169         *subapp = data->subapp;
1170         return PMINFO_R_OK;
1171 }
1172
1173 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1174 {
1175         char *val;
1176         icon_x *ptr;
1177         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1178
1179         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1180         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1181
1182         for (ptr = info->app_info->icon; ptr != NULL; ptr = ptr->next) {
1183                 if (ptr->section == NULL)
1184                         continue;
1185
1186                 val = (char *)ptr->section;
1187                 if (val && strcmp(val, "setting") == 0) {
1188                         *icon = (char *)ptr->text;
1189                         return PMINFO_R_OK;
1190                 }
1191         }
1192
1193         return PMINFO_R_ERROR;
1194 }
1195
1196
1197 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1198 {
1199         char *val;
1200         icon_x *ptr;
1201         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1202
1203         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1204         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1205
1206         for (ptr = info->app_info->icon; ptr != NULL; ptr = ptr->next) {
1207                 if (ptr->section == NULL)
1208                         continue;
1209
1210                 val = (char *)ptr->section;
1211                 if (val && strcmp(val, "notification") == 0){
1212                         *icon = (char *)ptr->text;
1213                         return PMINFO_R_OK;
1214                 }
1215         }
1216
1217         return PMINFO_R_ERROR;
1218 }
1219
1220 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_recentimage *type)
1221 {
1222         char *val;
1223         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1224
1225         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1226         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1227
1228         if (info->app_info == NULL || info->app_info->recentimage == NULL)
1229                 return PMINFO_R_ERROR;
1230
1231         val = (char *)info->app_info->recentimage;
1232         if (strcasecmp(val, "capture") == 0)
1233                 *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
1234         else if (strcasecmp(val, "icon") == 0)
1235                 *type = PMINFO_RECENTIMAGE_USE_ICON;
1236         else
1237                 *type = PMINFO_RECENTIMAGE_USE_NOTHING;
1238
1239         return PMINFO_R_OK;
1240 }
1241
1242 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h handle, char **preview_img)
1243 {
1244         char *val;
1245         image_x *ptr;
1246         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1247
1248         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1249         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1250
1251         for (ptr = info->app_info->image; ptr != NULL; ptr = ptr->next) {
1252                 if (ptr->section == NULL)
1253                         continue;
1254
1255                 val = (char *)ptr->section;
1256                 if (val && strcmp(val, "preview") == 0) {
1257                         *preview_img = (char *)ptr->text;
1258                         return PMINFO_R_OK;
1259                 }
1260         }
1261
1262         return PMINFO_R_ERROR;
1263 }
1264
1265 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_permission_type *permission)
1266 {
1267         const char *val;
1268         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1269
1270         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1271         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1272
1273         val = info->app_info->permission_type;
1274         if (val == NULL)
1275                 return PMINFO_R_ERROR;
1276
1277         if (strcmp(val, "signature") == 0)
1278                 *permission = PMINFO_PERMISSION_SIGNATURE;
1279         else if (strcmp(val, "privilege") == 0)
1280                 *permission = PMINFO_PERMISSION_PRIVILEGE;
1281         else
1282                 *permission = PMINFO_PERMISSION_NORMAL;
1283
1284         return PMINFO_R_OK;
1285 }
1286
1287 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h handle, char **component_type)
1288 {
1289         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1290
1291         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1292         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1293
1294         if (info->app_info == NULL || info->app_info->component_type == NULL)
1295                 return PMINFO_R_ERROR;
1296
1297         *component_type = (char *)info->app_info->component_type;
1298
1299         return PMINFO_R_OK;
1300 }
1301
1302 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
1303 {
1304         char *val;
1305         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1306
1307         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1308         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1309
1310         if (info->app_info == NULL || info->app_info->hwacceleration == NULL)
1311                 return PMINFO_R_ERROR;
1312
1313         val = (char *)info->app_info->hwacceleration;
1314         if (strcasecmp(val, "not-use-GL") == 0)
1315                 *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
1316         else if (strcasecmp(val, "use-GL") == 0)
1317                 *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
1318         else
1319                 *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
1320
1321         return PMINFO_R_OK;
1322 }
1323
1324 API int pkgmgrinfo_appinfo_get_screenreader(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_screenreader *screenreader)
1325 {
1326         char *val;
1327         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1328
1329         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1330         retvm_if(screenreader == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1331
1332         if (info->app_info == NULL || info->app_info->screenreader == NULL)
1333                 return PMINFO_R_ERROR;
1334
1335         val = (char *)info->app_info->screenreader;
1336         if (strcasecmp(val, "screenreader-off") == 0)
1337                 *screenreader = PMINFO_SCREENREADER_OFF;
1338         else if (strcasecmp(val, "screenreader-on") == 0)
1339                 *screenreader = PMINFO_SCREENREADER_ON;
1340         else
1341                 *screenreader = PMINFO_SCREENREADER_USE_SYSTEM_SETTING;
1342
1343         return PMINFO_R_OK;
1344 }
1345
1346 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h handle, char **portrait_img, char **landscape_img)
1347 {
1348         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1349
1350         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1351         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1352         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1353
1354         if (info->app_info == NULL || info->app_info->portraitimg ||
1355                         info->app_info->landscapeimg == NULL)
1356                 return PMINFO_R_ERROR;
1357
1358         *portrait_img = (char *)info->app_info->portraitimg;
1359         *landscape_img = (char *)info->app_info->landscapeimg;
1360
1361         return PMINFO_R_OK;
1362 }
1363
1364 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
1365 {
1366         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1367
1368         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1369         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1370
1371         if (info->app_info == NULL || info->app_info->submode_mainid == NULL)
1372                 return PMINFO_R_ERROR;
1373
1374         *submode_mainid = (char *)info->app_info->submode_mainid;
1375
1376         return PMINFO_R_OK;
1377 }
1378
1379 API int pkgmgrinfo_appinfo_get_launch_mode(pkgmgrinfo_appinfo_h handle, char **mode)
1380 {
1381         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1382
1383         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1384         retvm_if(mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1385
1386         if (info->app_info->launch_mode == NULL)
1387                 return PMINFO_R_ERROR;
1388
1389         *mode = (char *)(info->app_info->launch_mode);
1390
1391         return PMINFO_R_OK;
1392 }
1393
1394 API int pkgmgrinfo_appinfo_usr_get_datacontrol_info(const char *providerid, const char *type, uid_t uid, char **appid, char **access)
1395 {
1396         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1397         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1398         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1399         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1400
1401         int ret = PMINFO_R_OK;
1402         char *query = NULL;
1403         sqlite3_stmt *stmt = NULL;
1404
1405         /*open db*/
1406         ret = __open_manifest_db(uid, true);
1407         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1408
1409         /*Start constructing query*/
1410         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q and type=%Q", providerid, type);
1411
1412         /*prepare query*/
1413         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1414         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1415
1416         /*step query*/
1417         ret = sqlite3_step(stmt);
1418         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1419
1420         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1421         *access = strdup((char *)sqlite3_column_text(stmt, 2));
1422
1423         ret = PMINFO_R_OK;
1424
1425 catch:
1426         sqlite3_free(query);
1427         sqlite3_finalize(stmt);
1428         __close_manifest_db();
1429         return ret;
1430 }
1431
1432 API int pkgmgrinfo_appinfo_get_datacontrol_info(const char *providerid, const char *type, char **appid, char **access)
1433 {
1434         return pkgmgrinfo_appinfo_usr_get_datacontrol_info(providerid, type, GLOBAL_USER, appid, access);
1435 }
1436
1437 API int pkgmgrinfo_appinfo_usr_get_datacontrol_appid(const char *providerid, uid_t uid, char **appid)
1438 {
1439         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1440         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1441
1442         int ret = PMINFO_R_OK;
1443         char *query = NULL;
1444         sqlite3_stmt *stmt = NULL;
1445
1446         /*open db*/
1447         ret = __open_manifest_db(uid, true);
1448         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1449
1450         /*Start constructing query*/
1451         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q", providerid);
1452
1453         /*prepare query*/
1454         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1455         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1456
1457         /*step query*/
1458         ret = sqlite3_step(stmt);
1459         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1460
1461         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1462
1463         ret = PMINFO_R_OK;
1464
1465 catch:
1466         sqlite3_free(query);
1467         sqlite3_finalize(stmt);
1468         __close_manifest_db();
1469         return ret;
1470 }
1471
1472 API int pkgmgrinfo_appinfo_get_datacontrol_appid(const char *providerid, char **appid)
1473 {
1474         return pkgmgrinfo_appinfo_usr_get_datacontrol_appid(providerid, GLOBAL_USER, appid);
1475 }
1476
1477 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
1478                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
1479 {
1480         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1481         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1482         int ret = -1;
1483         permission_x *ptr = NULL;
1484         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1485
1486         if (info->app_info == NULL)
1487                 return PMINFO_R_ERROR;
1488
1489         for (ptr = info->app_info->permission; ptr; ptr = ptr->next) {
1490                 if (ptr->value) {
1491                         ret = permission_func(ptr->value, user_data);
1492                         if (ret < 0)
1493                                 break;
1494                 }
1495         }
1496         return PMINFO_R_OK;
1497 }
1498
1499 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
1500                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
1501 {
1502         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1503         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1504         int ret = -1;
1505         category_x *ptr = NULL;
1506         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1507
1508         if (info->app_info == NULL)
1509                 return PMINFO_R_ERROR;
1510
1511         for (ptr = info->app_info->category; ptr; ptr = ptr->next) {
1512                 if (ptr->name) {
1513                         ret = category_func(ptr->name, user_data);
1514                         if (ret < 0)
1515                                 break;
1516                 }
1517         }
1518         return PMINFO_R_OK;
1519 }
1520
1521 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
1522                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
1523 {
1524         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1525         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1526         int ret = -1;
1527         metadata_x *ptr = NULL;
1528         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1529
1530         if (info->app_info == NULL)
1531                 return PMINFO_R_ERROR;
1532
1533         for (ptr = info->app_info->metadata; ptr; ptr = ptr->next) {
1534                 if (ptr->key) {
1535                         ret = metadata_func(ptr->key, ptr->value, user_data);
1536                         if (ret < 0)
1537                                 break;
1538                 }
1539         }
1540         return PMINFO_R_OK;
1541 }
1542
1543 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
1544                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
1545 {
1546         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1547         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1548         int ret;
1549         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1550         appcontrol_x *appcontrol;
1551
1552         if (info->uiapp_info == NULL)
1553                 return PMINFO_R_ERROR;
1554
1555         for (appcontrol = info->app_info->appcontrol; appcontrol; appcontrol = appcontrol->next) {
1556                 ret = appcontrol_func(appcontrol->operation, appcontrol->uri, appcontrol->mime, user_data);
1557                 if (ret < 0)
1558                         break;
1559         }
1560
1561         return PMINFO_R_OK;
1562 }
1563
1564 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h handle, bool *nodisplay)
1565 {
1566         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1567         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1568         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1569
1570         if (info->app_info == NULL)
1571                 return PMINFO_R_ERROR;
1572
1573         *nodisplay = _get_bool_value(info->app_info->nodisplay);
1574         return PMINFO_R_OK;
1575 }
1576
1577 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h handle, bool *multiple)
1578 {
1579         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1580         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1581         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1582
1583         if (info->app_info == NULL)
1584                 return PMINFO_R_ERROR;
1585
1586         *multiple = _get_bool_value(info->app_info->multiple);
1587         return PMINFO_R_OK;
1588 }
1589
1590 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
1591 {
1592         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1593         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1594         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1595
1596         if (info->app_info == NULL)
1597                 return PMINFO_R_ERROR;
1598
1599         *indicator_disp = _get_bool_value(info->app_info->indicatordisplay);
1600         return PMINFO_R_OK;
1601 }
1602
1603 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
1604 {
1605         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1606         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1607         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1608
1609         if (info->app_info == NULL)
1610                 return PMINFO_R_ERROR;
1611
1612         *taskmanage = _get_bool_value(info->app_info->taskmanage);
1613         return PMINFO_R_OK;
1614 }
1615
1616 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
1617 {
1618         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1619         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1620         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1621
1622         if (info->app_info == NULL)
1623                 return PMINFO_R_ERROR;
1624
1625         *enabled = _get_bool_value(info->app_info->enabled);
1626         return PMINFO_R_OK;
1627
1628 }
1629
1630 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
1631 {
1632         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1633         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1634         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1635
1636         if (info->app_info == NULL)
1637                 return PMINFO_R_ERROR;
1638
1639         *onboot = _get_bool_value(info->app_info->onboot);
1640         return PMINFO_R_OK;
1641 }
1642
1643 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
1644 {
1645         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1646         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1647         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1648
1649         if (info->app_info == NULL)
1650                 return PMINFO_R_ERROR;
1651
1652         *autorestart = _get_bool_value(info->app_info->autorestart);
1653         return PMINFO_R_OK;
1654 }
1655
1656 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
1657 {
1658         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1659         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1660         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1661
1662         if (info->app_info == NULL)
1663                 return PMINFO_R_ERROR;
1664
1665         *mainapp = _get_bool_value(info->app_info->mainapp);
1666         return PMINFO_R_OK;
1667 }
1668
1669 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
1670 {
1671         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1672         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1673         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1674
1675         if (info->app_info == NULL)
1676                 return PMINFO_R_ERROR;
1677
1678         *preload = _get_bool_value(info->app_info->preload);
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)
1689                 return PMINFO_R_ERROR;
1690
1691         *submode = _get_bool_value(info->app_info->submode);
1692         return PMINFO_R_OK;
1693 }
1694
1695 API int pkgmgrinfo_appinfo_is_category_exist(pkgmgrinfo_appinfo_h handle, const char *category, bool *exist)
1696 {
1697         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1698         retvm_if(category == NULL, PMINFO_R_EINVAL, "category is NULL");
1699         retvm_if(exist == NULL, PMINFO_R_EINVAL, "exist is NULL");
1700
1701         category_x *ptr = NULL;
1702         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1703
1704         if (info->app_info == NULL)
1705                 return PMINFO_R_ERROR;
1706
1707         *exist = 0;
1708         for (ptr = info->app_info->category; ptr; ptr = ptr->next) {
1709                 if (ptr->name) {
1710                         if (strcasecmp(ptr->name, category) == 0) {
1711                                 *exist = 1;
1712                                 break;
1713                         }
1714                 }
1715         }
1716
1717         return PMINFO_R_OK;
1718 }
1719
1720 API int pkgmgrinfo_appinfo_is_ui_gadget(pkgmgrinfo_appinfo_h handle,
1721                 bool *ui_gadget)
1722 {
1723         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1724
1725         if (info == NULL || info->app_info == NULL || ui_gadget == NULL) {
1726                 _LOGE("invalid parameter");
1727                 return PMINFO_R_EINVAL;
1728         }
1729
1730         *ui_gadget = _get_bool_value(info->app_info->ui_gadget);
1731
1732         return PMINFO_R_OK;
1733 }
1734
1735 API int pkgmgrinfo_appinfo_is_support_disable(pkgmgrinfo_appinfo_h handle,
1736                 bool *support_disable)
1737 {
1738         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1739
1740         if (info == NULL || info->app_info == NULL || support_disable == NULL) {
1741                 _LOGE("invalid parameter");
1742                 return PMINFO_R_EINVAL;
1743         }
1744
1745         *support_disable = _get_bool_value(info->app_info->support_disable);
1746
1747         return PMINFO_R_OK;
1748 }
1749
1750 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle)
1751 {
1752         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1753         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1754         __cleanup_appinfo(info);
1755         return PMINFO_R_OK;
1756 }
1757
1758 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
1759 {
1760         return (pkgmgrinfo_pkginfo_filter_create(handle));
1761 }
1762
1763 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
1764 {
1765         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
1766 }
1767
1768 static gint __compare_func(gconstpointer data1, gconstpointer data2)
1769 {
1770         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
1771         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
1772         if (node1->prop == node2->prop)
1773                 return 0;
1774         else if (node1->prop > node2->prop)
1775                 return 1;
1776         else
1777                 return -1;
1778 }
1779
1780 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
1781                                 const char *property, const int value)
1782 {
1783         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1784         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1785         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
1786         char *val = NULL;
1787         GSList *link = NULL;
1788         int prop = -1;
1789         prop = _pminfo_appinfo_convert_to_prop_int(property);
1790         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
1791                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
1792                 _LOGE("Invalid Integer Property\n");
1793                 return PMINFO_R_EINVAL;
1794         }
1795         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1796         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1797         if (node == NULL) {
1798                 _LOGE("Out of Memory!!!\n");
1799                 return PMINFO_R_ERROR;
1800         }
1801         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
1802         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
1803         if (val == NULL) {
1804                 _LOGE("Out of Memory\n");
1805                 free(node);
1806                 node = NULL;
1807                 return PMINFO_R_ERROR;
1808         }
1809         node->prop = prop;
1810         node->value = val;
1811         /*If API is called multiple times for same property, we should override the previous values.
1812         Last value set will be used for filtering.*/
1813         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1814         if (link)
1815                 filter->list = g_slist_delete_link(filter->list, link);
1816         filter->list = g_slist_append(filter->list, (gpointer)node);
1817         return PMINFO_R_OK;
1818
1819 }
1820
1821 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
1822                                 const char *property, const bool value)
1823 {
1824         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1825         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1826         char *val = NULL;
1827         GSList *link = NULL;
1828         int prop = -1;
1829         prop = _pminfo_appinfo_convert_to_prop_bool(property);
1830         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
1831                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
1832                 _LOGE("Invalid Boolean Property\n");
1833                 return PMINFO_R_EINVAL;
1834         }
1835         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1836         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1837         if (node == NULL) {
1838                 _LOGE("Out of Memory!!!\n");
1839                 return PMINFO_R_ERROR;
1840         }
1841         if (value)
1842                 val = strndup("('true','True')", 15);
1843         else
1844                 val = strndup("('false','False')", 17);
1845         if (val == NULL) {
1846                 _LOGE("Out of Memory\n");
1847                 free(node);
1848                 node = NULL;
1849                 return PMINFO_R_ERROR;
1850         }
1851         node->prop = prop;
1852         node->value = val;
1853         /*If API is called multiple times for same property, we should override the previous values.
1854         Last value set will be used for filtering.*/
1855         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1856         if (link)
1857                 filter->list = g_slist_delete_link(filter->list, link);
1858         filter->list = g_slist_append(filter->list, (gpointer)node);
1859         return PMINFO_R_OK;
1860
1861 }
1862
1863 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
1864                                 const char *property, const char *value)
1865 {
1866         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1867         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1868         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1869         char *val = NULL;
1870         pkgmgrinfo_node_x *ptr = NULL;
1871         char prev[PKG_STRING_LEN_MAX] = {'\0'};
1872         char temp[PKG_STRING_LEN_MAX] = {'\0'};
1873         GSList *link = NULL;
1874         int prop = -1;
1875         prop = _pminfo_appinfo_convert_to_prop_str(property);
1876         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
1877                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
1878                 _LOGE("Invalid String Property\n");
1879                 return PMINFO_R_EINVAL;
1880         }
1881         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1882         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1883         if (node == NULL) {
1884                 _LOGE("Out of Memory!!!\n");
1885                 return PMINFO_R_ERROR;
1886         }
1887         node->prop = prop;
1888         switch (prop) {
1889         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
1890                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
1891                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
1892                 else
1893                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
1894                 node->value = val;
1895                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1896                 if (link)
1897                         filter->list = g_slist_delete_link(filter->list, link);
1898                 filter->list = g_slist_append(filter->list, (gpointer)node);
1899                 break;
1900         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
1901         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
1902         case E_PMINFO_APPINFO_PROP_APP_URI:
1903         case E_PMINFO_APPINFO_PROP_APP_MIME:
1904                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
1905                 if (val == NULL) {
1906                         _LOGE("Out of Memory\n");
1907                         free(node);
1908                         node = NULL;
1909                         return PMINFO_R_ERROR;
1910                 }
1911                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1912                 if (link) {
1913                         ptr = (pkgmgrinfo_node_x *)link->data;
1914                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
1915                         _LOGE("Previous value is %s\n", prev);
1916                         filter->list = g_slist_delete_link(filter->list, link);
1917                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
1918                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
1919                         _LOGE("New value is %s\n", val);
1920                         node->value = val;
1921                         filter->list = g_slist_append(filter->list, (gpointer)node);
1922                         memset(temp, '\0', PKG_STRING_LEN_MAX);
1923                 } else {
1924                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
1925                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
1926                         _LOGE("First value is %s\n", val);
1927                         node->value = val;
1928                         filter->list = g_slist_append(filter->list, (gpointer)node);
1929                         memset(temp, '\0', PKG_STRING_LEN_MAX);
1930                 }
1931                 break;
1932         default:
1933                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
1934                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1935                 if (link)
1936                         filter->list = g_slist_delete_link(filter->list, link);
1937                 filter->list = g_slist_append(filter->list, (gpointer)node);
1938                 break;
1939         }
1940         return PMINFO_R_OK;
1941 }
1942
1943 static int __count_cb(void *data, int ncols, char **coltxt, char **colname)
1944 {
1945         int *p = (int*)data;
1946         *p = atoi(coltxt[0]);
1947         _LOGE("count value is %d\n", *p);
1948         return 0;
1949 }
1950
1951 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
1952 {
1953         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1954         retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1955         char *locale = NULL;
1956         char *condition = NULL;
1957         char *error_message = NULL;
1958         char query[MAX_QUERY_LEN] = {'\0'};
1959         char where[MAX_QUERY_LEN] = {'\0'};
1960         GSList *list;
1961         int ret = 0;
1962
1963         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1964         /*Get current locale*/
1965         locale = _get_system_locale();
1966         if (locale == NULL) {
1967                 _LOGE("manifest locale is NULL\n");
1968                 return PMINFO_R_ERROR;
1969         }
1970
1971         ret = __open_manifest_db(uid, true);
1972         if (ret == -1) {
1973                 _LOGE("Fail to open manifest DB\n");
1974                 free(locale);
1975                 return PMINFO_R_ERROR;
1976         }
1977
1978         /*Start constructing query*/
1979         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_APP, locale);
1980
1981         /*Get where clause*/
1982         for (list = filter->list; list; list = g_slist_next(list)) {
1983                 __get_filter_condition(list->data, &condition);
1984                 if (condition) {
1985                         strncat(where, condition, sizeof(where) - strlen(where) -1);
1986                         where[sizeof(where) - 1] = '\0';
1987                         free(condition);
1988                         condition = NULL;
1989                 }
1990                 if (g_slist_next(list)) {
1991                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
1992                         where[sizeof(where) - 1] = '\0';
1993                 }
1994         }
1995         if (strlen(where) > 0) {
1996                 strncat(query, where, sizeof(query) - strlen(query) - 1);
1997                 query[sizeof(query) - 1] = '\0';
1998         }
1999
2000         /*Execute Query*/
2001         if (SQLITE_OK !=
2002             sqlite3_exec(GET_DB(manifest_db), query, __count_cb, (void *)count, &error_message)) {
2003                 _LOGE("Don't execute query = %s error message = %s\n", query,
2004                        error_message);
2005                 sqlite3_free(error_message);
2006                 ret = PMINFO_R_ERROR;
2007                 *count = 0;
2008                 goto err;
2009         }
2010         ret = PMINFO_R_OK;
2011 err:
2012         if (locale) {
2013                 free(locale);
2014                 locale = NULL;
2015         }
2016         __close_manifest_db();
2017         return ret;
2018 }
2019
2020 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
2021 {
2022         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, GLOBAL_USER);
2023 }
2024
2025 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(
2026                 pkgmgrinfo_appinfo_filter_h handle,
2027                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2028 {
2029         if (handle == NULL || app_cb == NULL) {
2030                 LOGE("invalid parameter");
2031                 return PMINFO_R_EINVAL;
2032         }
2033
2034         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2035                         user_data);
2036 }
2037
2038 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
2039                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
2040 {
2041         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, GLOBAL_USER);
2042 }
2043
2044 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
2045 {
2046         return (pkgmgrinfo_pkginfo_filter_create(handle));
2047 }
2048
2049 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
2050 {
2051         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
2052 }
2053
2054 API int pkgmgrinfo_appinfo_metadata_filter_add(
2055                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2056                 const char *key, const char *value)
2057 {
2058         int ret;
2059
2060         ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2061                         PMINFO_APPINFO_PROP_APP_METADATA_KEY, key);
2062         if (ret != PMINFO_R_OK)
2063                 return ret;
2064
2065         /* value can be NULL.
2066          * In that case all apps with specified key should be displayed
2067          */
2068         if (value) {
2069                 ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2070                                 PMINFO_APPINFO_PROP_APP_METADATA_VALUE, value);
2071                 if (ret != PMINFO_R_OK)
2072                         return ret;
2073         }
2074
2075         return PMINFO_R_OK;
2076 }
2077
2078 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(
2079                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2080                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2081 {
2082         if (handle == NULL || app_cb == NULL) {
2083                 LOGE("invalid parameter");
2084                 return PMINFO_R_EINVAL;
2085         }
2086
2087         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2088                         user_data);
2089 }
2090
2091 API int pkgmgrinfo_appinfo_metadata_filter_foreach(
2092                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2093                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
2094 {
2095         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb,
2096                         user_data, GLOBAL_USER);
2097 }
2098
2099 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
2100 {
2101         const char *val;
2102         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2103
2104         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2105         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2106
2107         val = info->uiapp_info->guestmode_visibility;
2108         *status = _get_bool_value(val);
2109         return PMINFO_R_OK;
2110 }