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