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