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