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