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