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