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