fix getting appinfo functions to get enabled/disabled appinfo properly
[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
32         if (info != NULL) {
33                 if (info->package)
34                         free((void *)info->package);
35                 if (info->locale)
36                         free((void *)info->locale);
37
38                 pkgmgrinfo_basic_free_application(info->app_info);
39                 free((void *)info);
40         }
41         return;
42 }
43
44 static char *_get_filtered_query(const char *query_raw,
45                 pkgmgrinfo_filter_x *filter)
46 {
47         char buf[MAX_QUERY_LEN] = { 0, };
48         char *condition;
49         size_t len;
50         GSList *list;
51         GSList *head = NULL;
52
53         if (filter)
54                 head = filter->list;
55
56         strncat(buf, query_raw, MAX_QUERY_LEN - 1);
57         len = strlen(buf);
58         for (list = head; list; list = list->next) {
59                 /* TODO: revise condition getter function */
60                 __get_filter_condition(list->data, &condition);
61                 if (condition == NULL)
62                         continue;
63                 if (buf[strlen(query_raw)] == '\0') {
64                         len += strlen(" WHERE ");
65                         strncat(buf, " WHERE ", MAX_QUERY_LEN - len - 1);
66                 } else {
67                         len += strlen(" AND ");
68                         strncat(buf, " AND ", MAX_QUERY_LEN -len - 1);
69                 }
70                 len += strlen(condition);
71                 strncat(buf, condition, sizeof(buf) - len - 1);
72                 free(condition);
73                 condition = NULL;
74         }
75
76         return strdup(buf);
77 }
78
79 static gint __list_strcmp(gconstpointer a, gconstpointer b)
80 {
81         return strcmp((char *)a, (char *)b);
82 }
83
84 static gint _appinfo_get_list(sqlite3 *db, const char *locale,
85                 pkgmgrinfo_filter_x *filter, GList **list)
86 {
87         static const char query_raw[] =
88                 "SELECT DISTINCT package_app_info.app_id FROM package_app_info"
89                 " LEFT OUTER JOIN package_app_localized_info"
90                 "  ON package_app_info.app_id=package_app_localized_info.app_id"
91                 "  AND package_app_localized_info.app_locale=%Q"
92                 " LEFT OUTER JOIN package_app_app_category"
93                 "  ON package_app_info.app_id=package_app_app_category.app_id"
94                 " LEFT OUTER JOIN package_app_app_control"
95                 "  ON package_app_info.app_id=package_app_app_control.app_id"
96                 " LEFT OUTER JOIN package_app_app_metadata"
97                 "  ON package_app_info.app_id=package_app_app_metadata.app_id ";
98         int ret;
99         char *query;
100         char *query_localized;
101         sqlite3_stmt *stmt;
102         char *appid = NULL;
103
104         query = _get_filtered_query(query_raw, filter);
105         if (query == NULL)
106                 return PMINFO_R_ERROR;
107         query_localized = sqlite3_mprintf(query, locale);
108         free(query);
109         if (query_localized == NULL)
110                 return PMINFO_R_ERROR;
111
112         ret = sqlite3_prepare_v2(db, query_localized,
113                         strlen(query_localized), &stmt, NULL);
114         sqlite3_free(query_localized);
115         if (ret != SQLITE_OK) {
116                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
117                 return PMINFO_R_ERROR;
118         }
119
120         while (sqlite3_step(stmt) == SQLITE_ROW) {
121                 _save_column_str(stmt, 0, (const char **)&appid);
122                 if (appid != NULL)
123                         *list = g_list_insert_sorted(*list, appid,
124                                         __list_strcmp);
125         }
126
127         sqlite3_finalize(stmt);
128
129         return PMINFO_R_OK;
130 }
131
132 static int _appinfo_get_filtered_list(pkgmgrinfo_filter_x *filter, uid_t uid,
133                 GList **list)
134 {
135         int ret;
136         sqlite3 *db;
137         const char *dbpath;
138         char *locale;
139         GList *tmp;
140         GList *tmp2;
141
142         locale = _get_system_locale();
143         if (locale == NULL)
144                 return PMINFO_R_ERROR;
145
146         dbpath = getUserPkgParserDBPathUID(uid);
147         if (dbpath == NULL) {
148                 free(locale);
149                 return PMINFO_R_ERROR;
150         }
151
152         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
153         if (ret != SQLITE_OK) {
154                 _LOGE("failed to open db: %d", ret);
155                 free(locale);
156                 return PMINFO_R_ERROR;
157         }
158
159         if (_appinfo_get_list(db, locale, filter, list)) {
160                 free(locale);
161                 sqlite3_close_v2(db);
162                 return PMINFO_R_ERROR;
163         }
164         sqlite3_close_v2(db);
165
166         if (uid == GLOBAL_USER) {
167                 free(locale);
168                 return PMINFO_R_OK;
169         }
170
171         /* search again from global */
172         dbpath = getUserPkgParserDBPathUID(GLOBAL_USER);
173         if (dbpath == NULL) {
174                 free(locale);
175                 return PMINFO_R_ERROR;
176         }
177
178         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
179         if (ret != SQLITE_OK) {
180                 _LOGE("failed to open db: %d", ret);
181                 free(locale);
182                 return PMINFO_R_ERROR;
183         }
184
185         if (_appinfo_get_list(db, locale, filter, list)) {
186                 free(locale);
187                 sqlite3_close_v2(db);
188                 return PMINFO_R_ERROR;
189         }
190         sqlite3_close_v2(db);
191
192         /* remove duplicate element:
193          * since the list is sorted, we can remove duplicates in linear time
194          */
195         for (tmp = *list, tmp2 = g_list_next(tmp); tmp;
196                         tmp = tmp2, tmp2 = g_list_next(tmp)) {
197                 if (tmp->prev == NULL || tmp->data == NULL)
198                         continue;
199                 if (strcmp((const char *)tmp->prev->data,
200                                         (const char *)tmp->data) == 0)
201                         *list = g_list_delete_link(*list, tmp);
202         }
203
204         free(locale);
205
206         return PMINFO_R_OK;
207 }
208
209 static int _appinfo_get_label(sqlite3 *db, const char *appid,
210                 const char *locale, GList **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                         return PMINFO_R_ERROR;
241                 }
242                 idx = 0;
243                 _save_column_str(stmt, idx++, &info->text);
244                 _save_column_str(stmt, idx++, &info->lang);
245                 *label = g_list_append(*label, info);
246         }
247
248         sqlite3_finalize(stmt);
249
250         return PMINFO_R_OK;
251 }
252
253 static void _appinfo_modify_icon(const char *appid, const char **icon)
254 {
255         char buf[PKG_VALUE_STRING_LEN_MAX];
256         const char *tmp;
257
258         if (*icon == NULL || (*icon)[0] == '/' || !strcasecmp(*icon, ""))
259                 return;
260
261         tmp = *icon;
262         snprintf(buf, sizeof(buf), "%s%s.png", getIconPath(getuid()), appid);
263         *icon = strdup(buf);
264         free((char *)tmp);
265 }
266
267 static int _appinfo_get_icon(sqlite3 *db, const char *appid, const char *locale,
268                 GList **icon)
269 {
270         static const char query_raw[] =
271                 "SELECT app_icon, app_locale "
272                 "FROM package_app_localized_info "
273                 "WHERE app_id=%Q AND app_locale IN (%Q, %Q)";
274         int ret;
275         char *query;
276         sqlite3_stmt *stmt;
277         int idx;
278         icon_x *info;
279
280         query = sqlite3_mprintf(query_raw, appid, locale, DEFAULT_LOCALE);
281         if (query == NULL) {
282                 LOGE("out of memory");
283                 return PMINFO_R_ERROR;
284         }
285
286         ret = sqlite3_prepare_v2(db, query, strlen(query),
287                         &stmt, NULL);
288         sqlite3_free(query);
289         if (ret != SQLITE_OK) {
290                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
291                 return PMINFO_R_ERROR;
292         }
293
294         while (sqlite3_step(stmt) == SQLITE_ROW) {
295                 info = calloc(1, sizeof(icon_x));
296                 if (info == NULL) {
297                         LOGE("out of memory");
298                         sqlite3_finalize(stmt);
299                         return PMINFO_R_ERROR;
300                 }
301                 idx = 0;
302                 _save_column_str(stmt, idx++, &info->text);
303                 /* FIXME: this is a workaround. this must be removed later */
304                 _appinfo_modify_icon(appid, &info->text);
305                 _save_column_str(stmt, idx++, &info->lang);
306                 *icon = g_list_append(*icon, info);
307         }
308
309         sqlite3_finalize(stmt);
310
311         return PMINFO_R_OK;
312 }
313
314 static int _appinfo_get_category(sqlite3 *db, const char *appid,
315                 GList **category)
316 {
317         static const char query_raw[] =
318                 "SELECT category FROM package_app_app_category WHERE app_id=%Q";
319         int ret;
320         char *query;
321         sqlite3_stmt *stmt;
322         const char *val;
323
324         query = sqlite3_mprintf(query_raw, appid);
325         if (query == NULL) {
326                 LOGE("out of memory");
327                 return PMINFO_R_ERROR;
328         }
329
330         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
331         sqlite3_free(query);
332         if (ret != SQLITE_OK) {
333                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
334                 return PMINFO_R_ERROR;
335         }
336
337         while (sqlite3_step(stmt) == SQLITE_ROW) {
338                 val = NULL;
339                 _save_column_str(stmt, 0, &val);
340                 if (val)
341                         *category = g_list_append(*category, (gpointer)val);
342         }
343
344         sqlite3_finalize(stmt);
345
346         return PMINFO_R_OK;
347 }
348
349 static void __parse_appcontrol(GList **appcontrol, char *appcontrol_str)
350 {
351         char *dup;
352         char *token;
353         char *ptr = NULL;
354         appcontrol_x *ac;
355
356         if (appcontrol_str == NULL)
357                 return;
358
359         dup = strdup(appcontrol_str);
360         do {
361                 ac = calloc(1, sizeof(appcontrol_x));
362                 if (ac == NULL) {
363                         _LOGE("out of memory");
364                         break;
365                 }
366                 token = strtok_r(dup, "|", &ptr);
367                 if (token && strcmp(token, "NULL"))
368                         ac->operation = strdup(token);
369                 token = strtok_r(NULL, "|", &ptr);
370                 if (token && strcmp(token, "NULL"))
371                         ac->uri = strdup(token);
372                 token = strtok_r(NULL, "|", &ptr);
373                 if (token && strcmp(token, "NULL"))
374                         ac->mime = strdup(token);
375                 *appcontrol = g_list_append(*appcontrol, ac);
376         } while ((token = strtok_r(NULL, ";", &ptr)));
377
378         free(dup);
379 }
380
381 static int _appinfo_get_app_control(sqlite3 *db, const char *appid,
382                 GList **appcontrol)
383 {
384         static const char query_raw[] =
385                 "SELECT app_control FROM package_app_app_control "
386                 "WHERE app_id=%Q";
387         int ret;
388         char *query;
389         sqlite3_stmt *stmt;
390         char *str;
391
392         query = sqlite3_mprintf(query_raw, appid);
393         if (query == NULL) {
394                 LOGE("out of memory");
395                 return PMINFO_R_ERROR;
396         }
397
398         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
399         sqlite3_free(query);
400         if (ret != SQLITE_OK) {
401                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
402                 return PMINFO_R_ERROR;
403         }
404
405         while (sqlite3_step(stmt) == SQLITE_ROW) {
406                 str = NULL;
407                 _save_column_str(stmt, 0, (const char **)&str);
408                 /* TODO: revise */
409                 __parse_appcontrol(appcontrol, str);
410                 free(str);
411         }
412
413         sqlite3_finalize(stmt);
414
415         return PMINFO_R_OK;
416 }
417
418 static int _appinfo_get_data_control(sqlite3 *db, const char *appid,
419                 GList **datacontrol)
420 {
421         static const char query_raw[] =
422                 "SELECT providerid, access, type "
423                 "FROM package_app_data_control WHERE app_id=%Q";
424         int ret;
425         char *query;
426         sqlite3_stmt *stmt;
427         int idx;
428         datacontrol_x *info;
429
430         query = sqlite3_mprintf(query_raw, appid);
431         if (query == NULL) {
432                 LOGE("out of memory");
433                 return PMINFO_R_ERROR;
434         }
435
436         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
437         sqlite3_free(query);
438         if (ret != SQLITE_OK) {
439                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
440                 return PMINFO_R_ERROR;
441         }
442
443         while (sqlite3_step(stmt) == SQLITE_ROW) {
444                 info = calloc(1, sizeof(datacontrol_x));
445                 if (info == NULL) {
446                         LOGE("out of memory");
447                         sqlite3_finalize(stmt);
448                         return PMINFO_R_ERROR;
449                 }
450                 idx = 0;
451                 _save_column_str(stmt, idx++, &info->providerid);
452                 _save_column_str(stmt, idx++, &info->access);
453                 _save_column_str(stmt, idx++, &info->type);
454                 *datacontrol = g_list_append(*datacontrol, info);
455         }
456
457         sqlite3_finalize(stmt);
458
459         return PMINFO_R_OK;
460 }
461
462 static int _appinfo_get_metadata(sqlite3 *db, const char *appid,
463                 GList **metadata)
464 {
465         static const char query_raw[] =
466                 "SELECT md_key, md_value "
467                 "FROM package_app_app_metadata WHERE app_id=%Q";
468         int ret;
469         char *query;
470         sqlite3_stmt *stmt;
471         int idx;
472         metadata_x *info;
473
474         query = sqlite3_mprintf(query_raw, appid);
475         if (query == NULL) {
476                 LOGE("out of memory");
477                 return PMINFO_R_ERROR;
478         }
479
480         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
481         sqlite3_free(query);
482         if (ret != SQLITE_OK) {
483                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
484                 return PMINFO_R_ERROR;
485         }
486
487         while (sqlite3_step(stmt) == SQLITE_ROW) {
488                 info = calloc(1, sizeof(metadata_x));
489                 if (info == NULL) {
490                         LOGE("out of memory");
491                         sqlite3_finalize(stmt);
492                         return PMINFO_R_ERROR;
493                 }
494                 idx = 0;
495                 _save_column_str(stmt, idx++, &info->key);
496                 _save_column_str(stmt, idx++, &info->value);
497                 *metadata = g_list_append(*metadata, info);
498         }
499
500         sqlite3_finalize(stmt);
501
502         return PMINFO_R_OK;
503
504 }
505
506 static int _appinfo_get_splashscreens(sqlite3 *db, const char *appid,
507                 GList **splashscreens)
508 {
509         static const char query_raw[] =
510                 "SELECT src, type, orientation, indicatordisplay "
511                 "FROM package_app_splash_screen WHERE app_id=%Q";
512         int ret;
513         char *query;
514         sqlite3_stmt *stmt;
515         int idx;
516         splashscreen_x *info;
517
518         query = sqlite3_mprintf(query_raw, appid);
519         if (query == NULL) {
520                 LOGE("out of memory");
521                 return PMINFO_R_ERROR;
522         }
523
524         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
525         sqlite3_free(query);
526         if (ret != SQLITE_OK) {
527                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
528                 return PMINFO_R_ERROR;
529         }
530
531         while (sqlite3_step(stmt) == SQLITE_ROW) {
532                 info = calloc(1, sizeof(splashscreen_x));
533                 if (info == NULL) {
534                         LOGE("out of memory");
535                         sqlite3_finalize(stmt);
536                         return PMINFO_R_ERROR;
537                 }
538                 idx = 0;
539                 _save_column_str(stmt, idx++, &info->src);
540                 _save_column_str(stmt, idx++, &info->type);
541                 _save_column_str(stmt, idx++, &info->orientation);
542                 _save_column_str(stmt, idx++, &info->indicatordisplay);
543                 *splashscreens = g_list_append(*splashscreens, info);
544         }
545
546         sqlite3_finalize(stmt);
547
548         return PMINFO_R_OK;
549 }
550
551 static GList *__get_background_category(char *value)
552 {
553         GList *category_list = NULL;
554         int convert_value = 0;
555         if (!value || strlen(value) == 0)
556                 return NULL;
557
558         convert_value = atoi(value);
559         if (convert_value < 0)
560                 return NULL;
561
562         if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
563                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
564         else
565                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
566
567         if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
568                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_MEDIA_STR));
569
570         if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
571                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
572
573         if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
574                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_BGNETWORK_STR));
575
576         if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
577                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_LOCATION_STR));
578
579         if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
580                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_SENSOR_STR));
581
582         if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
583                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_IOTCOMM_STR));
584
585         if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
586                 category_list = g_list_append(category_list, strdup(APP_BG_CATEGORY_SYSTEM));
587
588         return category_list;
589
590 }
591
592 static int _appinfo_get_application(sqlite3 *db, const char *appid,
593                 const char *locale, application_x **application, bool is_disabled)
594 {
595         static const char query_raw[] =
596                 "SELECT app_id, app_component, app_exec, app_nodisplay, "
597                 "app_type, app_onboot, app_multiple, app_autorestart, "
598                 "app_taskmanage, app_enabled, app_hwacceleration, "
599                 "app_screenreader, app_mainapp, app_recentimage, "
600                 "app_launchcondition, app_indicatordisplay, app_portraitimg, "
601                 "app_landscapeimg, app_guestmodevisibility, "
602                 "app_permissiontype, app_preload, app_submode, "
603                 "app_submode_mainid, app_launch_mode, app_ui_gadget, "
604                 "app_support_disable, "
605                 "component_type, package, app_process_pool, app_installed_storage, "
606                 "app_background_category, app_package_type "
607                 "FROM package_app_info WHERE app_id='%s' "
608                 "AND (app_disable='%s' "
609                 "%s app_id %s IN "
610                 "(SELECT app_id from package_app_disable_for_user WHERE uid='%d'))";
611         int ret;
612         char query[MAX_QUERY_LEN] = { '\0' };
613         sqlite3_stmt *stmt;
614         int idx;
615         application_x *info;
616         char *bg_category_str = NULL;
617         snprintf(query, MAX_QUERY_LEN - 1, query_raw,
618                         appid,
619                         is_disabled ? "true" : "false",
620                         is_disabled ? "OR" : "AND",
621                         is_disabled ? "" : "NOT",
622                         (int)getuid());
623
624         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
625         if (ret != SQLITE_OK) {
626                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
627                 return PMINFO_R_ERROR;
628         }
629
630         ret = sqlite3_step(stmt);
631         if (ret == SQLITE_DONE) {
632                 sqlite3_finalize(stmt);
633                 return PMINFO_R_ENOENT;
634         } else if (ret != SQLITE_ROW) {
635                 LOGE("step failed: %s", sqlite3_errmsg(db));
636                 sqlite3_finalize(stmt);
637                 return PMINFO_R_ERROR;
638         }
639
640         info = calloc(1, sizeof(application_x));
641         if (info == NULL) {
642                 LOGE("out of memory");
643                 sqlite3_finalize(stmt);
644                 return PMINFO_R_ERROR;
645         }
646         idx = 0;
647         _save_column_str(stmt, idx++, &info->appid);
648         _save_column_str(stmt, idx++, &info->component);
649         _save_column_str(stmt, idx++, &info->exec);
650         _save_column_str(stmt, idx++, &info->nodisplay);
651         _save_column_str(stmt, idx++, &info->type);
652         _save_column_str(stmt, idx++, &info->onboot);
653         _save_column_str(stmt, idx++, &info->multiple);
654         _save_column_str(stmt, idx++, &info->autorestart);
655         _save_column_str(stmt, idx++, &info->taskmanage);
656         _save_column_str(stmt, idx++, &info->enabled);
657         _save_column_str(stmt, idx++, &info->hwacceleration);
658         _save_column_str(stmt, idx++, &info->screenreader);
659         _save_column_str(stmt, idx++, &info->mainapp);
660         _save_column_str(stmt, idx++, &info->recentimage);
661         _save_column_str(stmt, idx++, &info->launchcondition);
662         _save_column_str(stmt, idx++, &info->indicatordisplay);
663         _save_column_str(stmt, idx++, &info->portraitimg);
664         _save_column_str(stmt, idx++, &info->landscapeimg);
665         _save_column_str(stmt, idx++, &info->guestmode_visibility);
666         _save_column_str(stmt, idx++, &info->permission_type);
667         _save_column_str(stmt, idx++, &info->preload);
668         _save_column_str(stmt, idx++, &info->submode);
669         _save_column_str(stmt, idx++, &info->submode_mainid);
670         _save_column_str(stmt, idx++, &info->launch_mode);
671         _save_column_str(stmt, idx++, &info->ui_gadget);
672         _save_column_str(stmt, idx++, &info->support_disable);
673         _save_column_str(stmt, idx++, &info->component_type);
674         _save_column_str(stmt, idx++, &info->package);
675         _save_column_str(stmt, idx++, &info->process_pool);
676         _save_column_str(stmt, idx++, &info->installed_storage);
677         _save_column_str(stmt, idx++, &bg_category_str);
678         _save_column_str(stmt, idx++, &info->package_type);
679
680         info->background_category = __get_background_category(bg_category_str);
681
682         if (_appinfo_get_label(db, info->appid, locale, &info->label)) {
683                 pkgmgrinfo_basic_free_application(info);
684                 sqlite3_finalize(stmt);
685                 return PMINFO_R_ERROR;
686         }
687
688         if (_appinfo_get_icon(db, info->appid, locale, &info->icon)) {
689                 pkgmgrinfo_basic_free_application(info);
690                 sqlite3_finalize(stmt);
691                 return PMINFO_R_ERROR;
692         }
693
694         if (_appinfo_get_category(db, info->appid, &info->category)) {
695                 pkgmgrinfo_basic_free_application(info);
696                 sqlite3_finalize(stmt);
697                 return PMINFO_R_ERROR;
698         }
699
700         if (_appinfo_get_app_control(db, info->appid, &info->appcontrol)) {
701                 pkgmgrinfo_basic_free_application(info);
702                 sqlite3_finalize(stmt);
703                 return PMINFO_R_ERROR;
704         }
705
706         if (_appinfo_get_data_control(db, info->appid, &info->datacontrol)) {
707                 pkgmgrinfo_basic_free_application(info);
708                 sqlite3_finalize(stmt);
709                 return PMINFO_R_ERROR;
710         }
711
712         if (_appinfo_get_metadata(db, info->appid, &info->metadata)) {
713                 pkgmgrinfo_basic_free_application(info);
714                 sqlite3_finalize(stmt);
715                 return PMINFO_R_ERROR;
716         }
717
718         if (_appinfo_get_splashscreens(db, info->appid, &info->splashscreens)) {
719                 pkgmgrinfo_basic_free_application(info);
720                 sqlite3_finalize(stmt);
721                 return PMINFO_R_ERROR;
722         }
723
724         *application = info;
725
726         sqlite3_finalize(stmt);
727
728         return PMINFO_R_OK;
729 }
730
731 static int _appinfo_get_appinfo(const char *appid, uid_t uid,
732                 pkgmgr_appinfo_x **appinfo, bool is_disabled)
733 {
734         int ret;
735         sqlite3 *db;
736         const char *dbpath;
737         char *locale;
738         pkgmgr_appinfo_x *info;
739
740         dbpath = getUserPkgParserDBPathUID(uid);
741         if (dbpath == NULL)
742                 return PMINFO_R_ERROR;
743
744         locale = _get_system_locale();
745         if (locale == NULL)
746                 return PMINFO_R_ERROR;
747
748         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
749         if (ret != SQLITE_OK) {
750                 _LOGE("failed to open db: %d", ret);
751                 free(locale);
752                 return PMINFO_R_ERROR;
753         }
754
755         info = calloc(1, sizeof(pkgmgr_appinfo_x));
756         if (info == NULL) {
757                 _LOGE("out of memory");
758                 free(locale);
759                 sqlite3_close_v2(db);
760                 return PMINFO_R_ERROR;
761         }
762
763         ret = _appinfo_get_application(db, appid, locale, &info->app_info, is_disabled);
764         if (ret != PMINFO_R_OK) {
765                 free(info);
766                 free(locale);
767                 sqlite3_close_v2(db);
768                 return ret;
769         }
770
771         info->locale = locale;
772         info->package = strdup(info->app_info->package);
773
774         *appinfo = info;
775
776         sqlite3_close_v2(db);
777
778         return ret;
779 }
780
781 API int pkgmgrinfo_appinfo_get_usr_disabled_appinfo(const char *appid, uid_t uid,
782                 pkgmgrinfo_appinfo_h *handle)
783 {
784         int ret;
785
786         if (appid == NULL || handle == NULL) {
787                 LOGE("invalid parameter");
788                 return PMINFO_R_EINVAL;
789         }
790
791         ret = _appinfo_get_appinfo(appid, uid, (pkgmgr_appinfo_x **)handle, true);
792         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
793                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
794                                 (pkgmgr_appinfo_x **)handle, true);
795
796         if (ret != PMINFO_R_OK)
797                 _LOGE("failed to get appinfo of %s for user %d", appid, uid);
798
799         return ret;
800 }
801
802 API int pkgmgrinfo_appinfo_get_disabled_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
803 {
804         return pkgmgrinfo_appinfo_get_usr_disabled_appinfo(appid, GLOBAL_USER, handle);
805 }
806
807 API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid,
808                 pkgmgrinfo_appinfo_h *handle)
809 {
810         int ret;
811
812         if (appid == NULL || handle == NULL) {
813                 LOGE("invalid parameter");
814                 return PMINFO_R_EINVAL;
815         }
816
817         ret = _appinfo_get_appinfo(appid, uid, (pkgmgr_appinfo_x **)handle, false);
818         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
819                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
820                                 (pkgmgr_appinfo_x **)handle, false);
821
822         if (ret != PMINFO_R_OK)
823                 _LOGE("failed to get appinfo of %s for user %d", appid, uid);
824
825         return ret;
826 }
827
828 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
829 {
830         return pkgmgrinfo_appinfo_get_usr_appinfo(appid, GLOBAL_USER, handle);
831 }
832
833 static gpointer __copy_str(gconstpointer src, gpointer data)
834 {
835         const char *tmp = (const char *)src;
836         const char *buffer;
837
838         buffer = strdup(tmp);
839         if (buffer == NULL) {
840                 LOGE("memory alloc failed");
841                 *(int *)data = -1;
842                 return NULL;
843         }
844
845         return buffer;
846 }
847
848 static gpointer __copy_label(gconstpointer src, gpointer data)
849 {
850         label_x *tmp = (label_x *)src;
851         label_x *label;
852
853         label = calloc(1, sizeof(label_x));
854         if (label == NULL) {
855                 LOGE("memory alloc failed");
856                 *(int *)data = -1;
857                 return NULL;
858         }
859
860         if (tmp->name)
861                 label->name = strdup(tmp->name);
862         if (tmp->text)
863                 label->text = strdup(tmp->text);
864         if (tmp->lang)
865                 label->lang = strdup(tmp->lang);
866
867         return label;
868 }
869
870 static gpointer __copy_icon(gconstpointer src, gpointer data)
871 {
872         icon_x *tmp = (icon_x *)src;
873         icon_x *icon;
874
875         icon = calloc(1, sizeof(icon_x));
876         if (icon== NULL) {
877                 LOGE("memory alloc failed");
878                 *(int *)data = -1;
879                 return NULL;
880         }
881
882         if (tmp->text)
883                 icon->text = strdup(tmp->text);
884         if (tmp->lang)
885                 icon->lang = strdup(tmp->lang);
886         if (tmp->section)
887                 icon->section = strdup(tmp->section);
888         if (tmp->size)
889                 icon->size = strdup(tmp->size);
890         if (tmp->resolution)
891                 icon->resolution = strdup(tmp->resolution);
892
893         return icon;
894 }
895
896 static gpointer __copy_metadata(gconstpointer src, gpointer data)
897 {
898         metadata_x *tmp = (metadata_x *)src;
899         metadata_x *metadata;
900
901         metadata = calloc(1, sizeof(metadata_x));
902         if (metadata == NULL) {
903                 LOGE("memory alloc failed");
904                 *(int *)data = -1;
905                 return NULL;
906         }
907
908         if (tmp->key)
909                 metadata->key = strdup(tmp->key);
910         if (tmp->value)
911                 metadata->value = strdup(tmp->value);
912
913         return metadata;
914 }
915
916 static gpointer __copy_datacontrol(gconstpointer src, gpointer data)
917 {
918         datacontrol_x *tmp = (datacontrol_x *)src;
919         datacontrol_x *datacontrol;
920
921         datacontrol = calloc(1, sizeof(datacontrol_x));
922         if (datacontrol == NULL) {
923                 LOGE("memory alloc failed");
924                 *(int *)data = -1;
925                 return NULL;
926         }
927
928         if (tmp->providerid)
929                 datacontrol->providerid = strdup(tmp->providerid);
930         if (tmp->access)
931                 datacontrol->access = strdup(tmp->access);
932         if (tmp->type)
933                 datacontrol->type = strdup(tmp->type);
934
935         return datacontrol;
936 }
937
938 static gpointer __copy_appcontrol(gconstpointer src, gpointer data)
939 {
940         appcontrol_x *tmp = (appcontrol_x *)src;
941         appcontrol_x *appcontrol;
942
943         appcontrol = calloc(1, sizeof(appcontrol_x));
944         if (appcontrol ==NULL) {
945                 LOGE("memory alloc failed");
946                 *(int *)data = -1;
947                 return NULL;
948         }
949
950         if (tmp->operation)
951                 appcontrol->operation = strdup(tmp->operation);
952         if (tmp->uri)
953                 appcontrol->uri = strdup(tmp->uri);
954         if (tmp->mime)
955                 appcontrol->mime = strdup(tmp->mime);
956
957         return appcontrol;
958 }
959
960 static gpointer __copy_splashscreens(gconstpointer src, gpointer data)
961 {
962         splashscreen_x *tmp = (splashscreen_x *)src;
963         splashscreen_x *splashscreen;
964
965         splashscreen = (splashscreen_x *)calloc(1, sizeof(splashscreen_x));
966         if (splashscreen == NULL) {
967                 LOGE("memory alloc failed");
968                 *(int *)data = -1;
969                 return NULL;
970         }
971
972         if (tmp->src)
973                 splashscreen->src = strdup(tmp->src);
974         if (tmp->type)
975                 splashscreen->type = strdup(tmp->type);
976         if (tmp->orientation)
977                 splashscreen->orientation = strdup(tmp->orientation);
978         if (tmp->indicatordisplay)
979                 splashscreen->indicatordisplay = strdup(tmp->indicatordisplay);
980
981         return splashscreen;
982 }
983
984 static int _appinfo_copy_appinfo(application_x **application, application_x *data)
985 {
986         application_x *app_info;
987         int ret;
988
989         app_info = calloc(1, sizeof(application_x));
990         if (app_info == NULL) {
991                 LOGE("memory alloc failed");
992                 return PMINFO_R_ERROR;
993         }
994
995         if (data->appid != NULL)
996                 app_info->appid = strdup(data->appid);
997         if (data->exec != NULL)
998                 app_info->exec = strdup(data->exec);
999         if (data->nodisplay != NULL)
1000                 app_info->nodisplay = strdup(data->nodisplay);
1001         if (data->multiple != NULL)
1002                 app_info->multiple = strdup(data->multiple);
1003         if (data->taskmanage != NULL)
1004                 app_info->taskmanage = strdup(data->taskmanage);
1005         if (data->enabled != NULL)
1006                 app_info->enabled = strdup(data->enabled);
1007         if (data->type != NULL)
1008                 app_info->type = strdup(data->type);
1009         if (data->categories != NULL)
1010                 app_info->categories = strdup(data->categories);
1011         if (data->hwacceleration != NULL)
1012                 app_info->hwacceleration = strdup(data->hwacceleration);
1013         if (data->screenreader != NULL)
1014                 app_info->screenreader = strdup(data->screenreader);
1015         if (data->mainapp != NULL)
1016                 app_info->mainapp = strdup(data->mainapp);
1017         if (data->package != NULL)
1018                 app_info->package = strdup(data->package);
1019         if (data->recentimage != NULL)
1020                 app_info->recentimage = strdup(data->recentimage);
1021         if (data->launchcondition != NULL)
1022                 app_info->launchcondition = strdup(data->launchcondition);
1023         if (data->indicatordisplay != NULL)
1024                 app_info->indicatordisplay = strdup(data->indicatordisplay);
1025         if (data->portraitimg != NULL)
1026                 app_info->portraitimg = strdup(data->portraitimg);
1027         if (data->landscapeimg != NULL)
1028                 app_info->landscapeimg = strdup(data->landscapeimg);
1029         if (data->guestmode_visibility != NULL)
1030                 app_info->guestmode_visibility = strdup(data->guestmode_visibility);
1031         if (data->component != NULL)
1032                 app_info->component = strdup(data->component);
1033         if (data->permission_type != NULL)
1034                 app_info->permission_type = strdup(data->permission_type);
1035         if (data->component_type != NULL)
1036                 app_info->component_type = strdup(data->component_type);
1037         if (data->preload != NULL)
1038                 app_info->preload = strdup(data->preload);
1039         if (data->submode != NULL)
1040                 app_info->submode = strdup(data->submode);
1041         if (data->submode_mainid != NULL)
1042                 app_info->submode_mainid = strdup(data->submode_mainid);
1043         if (data->process_pool != NULL)
1044                 app_info->process_pool = strdup(data->process_pool);
1045         if (data->installed_storage != NULL)
1046                 app_info->installed_storage = strdup(data->installed_storage);
1047         if (data->autorestart != NULL)
1048                 app_info->autorestart = strdup(data->autorestart);
1049         if (data->onboot != NULL)
1050                 app_info->onboot = strdup(data->onboot);
1051         if (data->support_disable != NULL)
1052                 app_info->support_disable = strdup(data->support_disable);
1053         if (data->ui_gadget != NULL)
1054                 app_info->ui_gadget = strdup(data->ui_gadget);
1055         if (data->launch_mode != NULL)
1056                 app_info->launch_mode = strdup(data->launch_mode);
1057         if (data->package_type != NULL)
1058                 app_info->package_type = strdup(data->package_type);
1059
1060         /* GList */
1061         ret = 0;
1062         app_info->label = g_list_copy_deep(data->label, __copy_label, &ret);
1063         if (ret < 0) {
1064                 LOGE("memory alloc failed");
1065                 pkgmgrinfo_basic_free_application(app_info);
1066                 return PMINFO_R_ERROR;
1067         }
1068
1069         ret = 0;
1070         app_info->icon = g_list_copy_deep(data->icon, __copy_icon, &ret);
1071         if (ret < 0) {
1072                 LOGE("memory alloc failed");
1073                 pkgmgrinfo_basic_free_application(app_info);
1074                 return PMINFO_R_ERROR;
1075         }
1076
1077         ret = 0;
1078         app_info->category = g_list_copy_deep(data->category, __copy_str, &ret);
1079         if (ret < 0) {
1080                 LOGE("memory alloc failed");
1081                 pkgmgrinfo_basic_free_application(app_info);
1082                 return PMINFO_R_ERROR;
1083         }
1084
1085         ret = 0;
1086         app_info->metadata = g_list_copy_deep(data->metadata, __copy_metadata, &ret);
1087         if (ret < 0) {
1088                 LOGE("memory alloc failed");
1089                 pkgmgrinfo_basic_free_application(app_info);
1090                 return PMINFO_R_ERROR;
1091         }
1092
1093         ret = 0;
1094         app_info->datacontrol = g_list_copy_deep(data->datacontrol, __copy_datacontrol, &ret);
1095         if (ret < 0) {
1096                 LOGE("memory alloc failed");
1097                 pkgmgrinfo_basic_free_application(app_info);
1098                 return PMINFO_R_ERROR;
1099         }
1100
1101         ret = 0;
1102         app_info->appcontrol = g_list_copy_deep(data->appcontrol, __copy_appcontrol, &ret);
1103         if (ret < 0) {
1104                 LOGE("memory alloc failed");
1105                 pkgmgrinfo_basic_free_application(app_info);
1106                 return PMINFO_R_ERROR;
1107         }
1108
1109         ret = 0;
1110         app_info->background_category = g_list_copy_deep(data->background_category, __copy_str, &ret);
1111         if (ret < 0) {
1112                 LOGE("memory alloc failed");
1113                 pkgmgrinfo_basic_free_application(app_info);
1114                 return PMINFO_R_ERROR;
1115         }
1116
1117         ret = 0;
1118         app_info->splashscreens = g_list_copy_deep(data->splashscreens, __copy_splashscreens, &ret);
1119         if (ret < 0) {
1120                 LOGE("memory alloc failed");
1121                 pkgmgrinfo_basic_free_application(app_info);
1122                 return PMINFO_R_ERROR;
1123         }
1124
1125         *application = app_info;
1126
1127         return PMINFO_R_OK;
1128 }
1129
1130 API int pkgmgrinfo_appinfo_clone_appinfo(pkgmgrinfo_appinfo_h handle,
1131                 pkgmgrinfo_appinfo_h *clone)
1132 {
1133         pkgmgr_appinfo_x *info;
1134         pkgmgr_appinfo_x *temp = (pkgmgr_appinfo_x *)handle;
1135
1136         if (handle == NULL)
1137                 return PMINFO_R_EINVAL;
1138
1139         info = calloc(1, sizeof(pkgmgr_appinfo_x));
1140         if (info == NULL) {
1141                 LOGE("memory alloc failed");
1142                 return PMINFO_R_ERROR;
1143         }
1144
1145         if (temp->package != NULL)
1146                 info->package = strdup(temp->package);
1147         if (temp->locale != NULL)
1148                 info->locale = strdup(temp->locale);
1149
1150         info->app_component = temp->app_component;
1151
1152         if (_appinfo_copy_appinfo(&info->app_info, temp->app_info) < 0) {
1153                 LOGE("appinfo copy failed");
1154                 if (info->package)
1155                         free((void *)info->package);
1156                 if (info->locale)
1157                         free(info->locale);
1158                 free(info);
1159                 return PMINFO_R_ERROR;
1160         }
1161
1162         *clone = info;
1163
1164         return PMINFO_R_OK;
1165 }
1166
1167 static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
1168                 pkgmgrinfo_filter_x *filter, pkgmgrinfo_app_list_cb app_list_cb,
1169                 void *user_data)
1170 {
1171         int ret;
1172         pkgmgr_appinfo_x *info;
1173         GList *list = NULL;
1174         GList *tmp;
1175         char *appid;
1176         int stop = 0;
1177
1178         ret = _appinfo_get_filtered_list(filter, uid, &list);
1179         if (ret != PMINFO_R_OK)
1180                 return PMINFO_R_ERROR;
1181
1182         for (tmp = list; tmp; tmp = tmp->next) {
1183                 appid = (char *)tmp->data;
1184                 if (stop == 0) {
1185                         ret = _appinfo_get_appinfo(appid, uid, &info, false);
1186                         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
1187                                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
1188                                                 &info, false);
1189                         if (ret != PMINFO_R_OK) {
1190                                 free(appid);
1191                                 continue;
1192                         }
1193                         if (app_list_cb(info, user_data) < 0)
1194                                 stop = 1;
1195                         pkgmgrinfo_appinfo_destroy_appinfo(info);
1196                 }
1197                 free(appid);
1198         }
1199
1200         g_list_free(list);
1201
1202         return PMINFO_R_OK;
1203 }
1204
1205 API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle,
1206                 pkgmgrinfo_app_component component,
1207                 pkgmgrinfo_app_list_cb app_func, void *user_data, uid_t uid)
1208 {
1209         int ret;
1210         pkgmgrinfo_appinfo_filter_h filter;
1211         char *pkgid;
1212         const char *comp_str = NULL;
1213
1214         if (handle == NULL || app_func == NULL) {
1215                 LOGE("invalid parameter");
1216                 return PMINFO_R_EINVAL;
1217         }
1218
1219         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid)) {
1220                 LOGE("invalid parameter");
1221                 return PMINFO_R_EINVAL;
1222         }
1223
1224         if (pkgmgrinfo_appinfo_filter_create(&filter))
1225                 return PMINFO_R_ERROR;
1226
1227         if (pkgmgrinfo_appinfo_filter_add_string(filter,
1228                                 PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid)) {
1229                 pkgmgrinfo_appinfo_filter_destroy(filter);
1230                 return PMINFO_R_ERROR;
1231         }
1232
1233         if (uid == GLOBAL_USER) {
1234                 if (pkgmgrinfo_appinfo_filter_add_int(filter,
1235                                         PMINFO_APPINFO_PROP_APP_DISABLE_FOR_USER, (int)getuid())) {
1236                         pkgmgrinfo_appinfo_filter_destroy(filter);
1237                         return PMINFO_R_ERROR;
1238                 }
1239         }
1240
1241         switch (component) {
1242         case PMINFO_UI_APP:
1243                 comp_str = PMINFO_APPINFO_UI_APP;
1244                 break;
1245         case PMINFO_SVC_APP:
1246                 comp_str = PMINFO_APPINFO_SVC_APP;
1247                 break;
1248         default:
1249                 break;
1250         }
1251
1252         if (comp_str) {
1253                 if (pkgmgrinfo_appinfo_filter_add_string(filter,
1254                                         PMINFO_APPINFO_PROP_APP_COMPONENT,
1255                                         comp_str)) {
1256                         pkgmgrinfo_appinfo_filter_destroy(filter);
1257                         return PMINFO_R_ERROR;
1258                 }
1259         }
1260
1261         ret = _appinfo_get_filtered_foreach_appinfo(uid, filter, app_func,
1262                         user_data);
1263
1264         pkgmgrinfo_appinfo_filter_destroy(filter);
1265
1266         return ret;
1267 }
1268
1269 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
1270                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
1271 {
1272         return pkgmgrinfo_appinfo_get_usr_list(handle, component, app_func, user_data, GLOBAL_USER);
1273 }
1274
1275 API int pkgmgrinfo_appinfo_get_usr_install_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
1276 {
1277         if (app_func == NULL) {
1278                 LOGE("invalid parameter");
1279                 return PMINFO_R_EINVAL;
1280         }
1281
1282         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
1283                         user_data);
1284 }
1285
1286 API int pkgmgrinfo_appinfo_get_install_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
1287 {
1288         return pkgmgrinfo_appinfo_get_usr_install_list(app_func, GLOBAL_USER, user_data);
1289 }
1290
1291 API int pkgmgrinfo_appinfo_get_usr_installed_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
1292 {
1293         if (app_func == NULL) {
1294                 LOGE("invalid parameter");
1295                 return PMINFO_R_EINVAL;
1296         }
1297
1298         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
1299                         user_data);
1300 }
1301
1302 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
1303 {
1304         return pkgmgrinfo_appinfo_get_usr_installed_list(app_func, GLOBAL_USER, user_data);
1305 }
1306
1307 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h handle, char **appid)
1308 {
1309         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1310
1311         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1312         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1313
1314         if (info->app_info == NULL || info->app_info->appid == NULL)
1315                 return PMINFO_R_ERROR;
1316         *appid = (char *)info->app_info->appid;
1317
1318         return PMINFO_R_OK;
1319 }
1320
1321 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, char **pkg_name)
1322 {
1323         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1324
1325         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1326         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1327
1328         if (info->package == NULL)
1329                 return PMINFO_R_ERROR;
1330
1331         *pkg_name = (char *)info->package;
1332
1333         return PMINFO_R_OK;
1334 }
1335
1336 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h handle, char **pkgid)
1337 {
1338         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1339
1340         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1341         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1342
1343         if (info->package == NULL)
1344                 return PMINFO_R_ERROR;
1345
1346         *pkgid = (char *)info->package;
1347
1348         return PMINFO_R_OK;
1349 }
1350
1351 API int pkgmgrinfo_appinfo_get_pkgtype(pkgmgrinfo_appinfo_h  handle, char **pkgtype)
1352 {
1353         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1354         retvm_if(pkgtype == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1355         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1356
1357         *pkgtype = (char *)info->app_info->package_type;
1358
1359         return PMINFO_R_OK;
1360 }
1361
1362 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, char **exec)
1363 {
1364         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1365
1366         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1367         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1368
1369         if (info->app_info == NULL || info->app_info->exec == NULL)
1370                 return PMINFO_R_ERROR;
1371         *exec = (char *)info->app_info->exec;
1372
1373         return PMINFO_R_OK;
1374 }
1375
1376
1377 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1378 {
1379         const char *locale;
1380         icon_x *ptr;
1381         GList *tmp;
1382         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1383
1384         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1385         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1386
1387         locale = info->locale;
1388         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1389
1390         if (info->app_info == NULL)
1391                 return PMINFO_R_ERROR;
1392
1393         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1394                 ptr = (icon_x *)tmp->data;
1395                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1396                                 !strcasecmp(ptr->text, "") ||
1397                                 strcmp(ptr->lang, locale))
1398                         continue;
1399                 *icon = (char *)ptr->text;
1400                 return PMINFO_R_OK;
1401         }
1402
1403         locale = DEFAULT_LOCALE;
1404         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1405                 ptr = (icon_x *)tmp->data;
1406                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1407                                 strcmp(ptr->lang, locale))
1408                         continue;
1409                 *icon = (char *)ptr->text;
1410                 return PMINFO_R_OK;
1411         }
1412
1413         return PMINFO_R_ERROR;
1414 }
1415
1416
1417 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, char **label)
1418 {
1419         const char *locale;
1420         label_x *ptr;
1421         GList *tmp;
1422         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1423
1424         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1425         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1426
1427         locale = info->locale;
1428         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1429
1430         if (info->app_info == NULL)
1431                 return PMINFO_R_ERROR;
1432
1433         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
1434                 ptr = (label_x *)tmp->data;
1435                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1436                                 strcmp(ptr->lang, locale))
1437                         continue;
1438                 *label = (char *)ptr->text;
1439                 return PMINFO_R_OK;
1440         }
1441
1442         locale = DEFAULT_LOCALE;
1443         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
1444                 ptr = (label_x *)tmp->data;
1445                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1446                                 strcmp(ptr->lang, locale))
1447                         continue;
1448                 *label = (char *)ptr->text;
1449                 return PMINFO_R_OK;
1450         }
1451
1452         return PMINFO_R_ERROR;
1453 }
1454
1455 static char *_get_localed_label(const char *appid, const char *locale, uid_t uid)
1456 {
1457         char *result = NULL;
1458         char *query = NULL;
1459         sqlite3_stmt *stmt = NULL;
1460         sqlite3 *db = NULL;
1461         char *val;
1462         const char *manifest_db;
1463
1464         manifest_db = getUserPkgParserDBPathUID(uid);
1465         if (manifest_db == NULL) {
1466                 _LOGE("Failed to get manifest db path");
1467                 goto err;
1468         }
1469
1470         if (sqlite3_open_v2(manifest_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
1471                 _LOGE("DB open fail\n");
1472                 goto err;
1473         }
1474
1475         query = sqlite3_mprintf("select app_label from package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
1476         if (query == NULL) {
1477                 _LOGE("Out of memory");
1478                 goto err;
1479         }
1480
1481         if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) {
1482                 _LOGE("prepare_v2 fail\n");
1483                 goto err;
1484         }
1485
1486         if (sqlite3_step(stmt) == SQLITE_ROW) {
1487                 val = (char *)sqlite3_column_text(stmt, 0);
1488                 if (val != NULL)
1489                         result = strdup(val);
1490         }
1491
1492 err:
1493         sqlite3_finalize(stmt);
1494         sqlite3_free(query);
1495         sqlite3_close(db);
1496
1497         return result;
1498 }
1499
1500 API int pkgmgrinfo_appinfo_usr_get_localed_label(const char *appid, const char *locale, uid_t uid, char **label)
1501 {
1502         char *val;
1503
1504         retvm_if(appid == NULL || locale == NULL || label == NULL, PMINFO_R_EINVAL, "Argument is NULL");
1505
1506         val = _get_localed_label(appid, locale, uid);
1507         if (val == NULL)
1508                 val = _get_localed_label(appid, DEFAULT_LOCALE, uid);
1509
1510         if (val == NULL)
1511                 return PMINFO_R_ERROR;
1512
1513         *label = val;
1514
1515         return PMINFO_R_OK;
1516 }
1517
1518 API int pkgmgrinfo_appinfo_get_localed_label(const char *appid, const char *locale, char **label)
1519 {
1520         return pkgmgrinfo_appinfo_usr_get_localed_label(appid, locale, GLOBAL_USER, label);
1521 }
1522
1523 static pkgmgrinfo_app_component __appcomponent_convert(const char *comp)
1524 {
1525         if ( strcasecmp(comp, "uiapp") == 0)
1526                 return PMINFO_UI_APP;
1527         else if ( strcasecmp(comp, "svcapp") == 0)
1528                 return PMINFO_SVC_APP;
1529         else
1530                 return -1;
1531 }
1532
1533 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_component *component)
1534 {
1535         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1536         int comp;
1537
1538         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1539         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1540
1541         if (info->app_info == NULL)
1542                 return PMINFO_R_ERROR;
1543
1544         comp = __appcomponent_convert(info->app_info->component);
1545         if (comp < 0)
1546                 return PMINFO_R_ERROR;
1547
1548         *component = comp;
1549
1550         return PMINFO_R_OK;
1551 }
1552
1553 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, char **app_type)
1554 {
1555         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1556
1557         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1558         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1559
1560         if (info->app_info == NULL || info->app_info->type == NULL)
1561                 return PMINFO_R_ERROR;
1562         *app_type = (char *)info->app_info->type;
1563
1564         return PMINFO_R_OK;
1565 }
1566
1567 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
1568                                         int *operation_count, char ***operation)
1569 {
1570         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1571         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1572         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1573         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1574         *operation_count = data->operation_count;
1575         *operation = data->operation;
1576         return PMINFO_R_OK;
1577 }
1578
1579 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
1580                                         int *uri_count, char ***uri)
1581 {
1582         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1583         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1584         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1585         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1586         *uri_count = data->uri_count;
1587         *uri = data->uri;
1588         return PMINFO_R_OK;
1589 }
1590
1591 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
1592                                         int *mime_count, char ***mime)
1593 {
1594         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1595         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1596         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1597         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1598         *mime_count = data->mime_count;
1599         *mime = data->mime;
1600         return PMINFO_R_OK;
1601 }
1602
1603 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
1604                                         int *subapp_count, char ***subapp)
1605 {
1606         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1607         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1608         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1609         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1610         *subapp_count = data->subapp_count;
1611         *subapp = data->subapp;
1612         return PMINFO_R_OK;
1613 }
1614
1615 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1616 {
1617         char *val;
1618         icon_x *ptr;
1619         GList *tmp;
1620         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1621
1622         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1623         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1624
1625         if (info->app_info == NULL)
1626                 return PMINFO_R_ERROR;
1627
1628         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1629                 ptr = (icon_x *)tmp->data;
1630                 if (ptr == NULL || ptr->section == NULL)
1631                         continue;
1632
1633                 val = (char *)ptr->section;
1634                 if (val && strcmp(val, "setting") == 0) {
1635                         *icon = (char *)ptr->text;
1636                         return PMINFO_R_OK;
1637                 }
1638         }
1639
1640         return PMINFO_R_ERROR;
1641 }
1642
1643
1644 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1645 {
1646         char *val;
1647         icon_x *ptr;
1648         GList *tmp;
1649         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1650
1651         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1652         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1653
1654         if (info->app_info == NULL)
1655                 return PMINFO_R_ERROR;
1656
1657         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1658                 ptr = (icon_x *)tmp->data;
1659                 if (ptr == NULL || ptr->section == NULL)
1660                         continue;
1661
1662                 val = (char *)ptr->section;
1663                 if (val && strcmp(val, "notification") == 0){
1664                         *icon = (char *)ptr->text;
1665                         return PMINFO_R_OK;
1666                 }
1667         }
1668
1669         return PMINFO_R_ERROR;
1670 }
1671
1672 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_recentimage *type)
1673 {
1674         char *val;
1675         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1676
1677         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1678         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1679
1680         if (info->app_info == NULL || info->app_info->recentimage == NULL)
1681                 return PMINFO_R_ERROR;
1682
1683         val = (char *)info->app_info->recentimage;
1684         if (strcasecmp(val, "capture") == 0)
1685                 *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
1686         else if (strcasecmp(val, "icon") == 0)
1687                 *type = PMINFO_RECENTIMAGE_USE_ICON;
1688         else
1689                 *type = PMINFO_RECENTIMAGE_USE_NOTHING;
1690
1691         return PMINFO_R_OK;
1692 }
1693
1694 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h handle, char **preview_img)
1695 {
1696         char *val;
1697         image_x *ptr;
1698         GList *tmp;
1699         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1700
1701         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1702         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1703
1704         if (info->app_info == NULL)
1705                 return PMINFO_R_ERROR;
1706
1707         for (tmp = info->app_info->image; tmp; tmp = tmp->next) {
1708                 ptr = (image_x *)tmp->data;
1709                 if (ptr == NULL || ptr->section == NULL)
1710                         continue;
1711
1712                 val = (char *)ptr->section;
1713                 if (val && strcmp(val, "preview") == 0) {
1714                         *preview_img = (char *)ptr->text;
1715                         return PMINFO_R_OK;
1716                 }
1717         }
1718
1719         return PMINFO_R_ERROR;
1720 }
1721
1722 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_permission_type *permission)
1723 {
1724         const char *val;
1725         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1726
1727         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1728         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1729
1730         val = info->app_info->permission_type;
1731         if (val == NULL)
1732                 return PMINFO_R_ERROR;
1733
1734         if (strcmp(val, "signature") == 0)
1735                 *permission = PMINFO_PERMISSION_SIGNATURE;
1736         else if (strcmp(val, "privilege") == 0)
1737                 *permission = PMINFO_PERMISSION_PRIVILEGE;
1738         else
1739                 *permission = PMINFO_PERMISSION_NORMAL;
1740
1741         return PMINFO_R_OK;
1742 }
1743
1744 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h handle, char **component_type)
1745 {
1746         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1747
1748         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1749         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1750
1751         if (info->app_info == NULL || info->app_info->component_type == NULL)
1752                 return PMINFO_R_ERROR;
1753
1754         *component_type = (char *)info->app_info->component_type;
1755
1756         return PMINFO_R_OK;
1757 }
1758
1759 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
1760 {
1761         char *val;
1762         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1763
1764         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1765         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1766
1767         if (info->app_info == NULL || info->app_info->hwacceleration == NULL)
1768                 return PMINFO_R_ERROR;
1769
1770         val = (char *)info->app_info->hwacceleration;
1771         if (strcasecmp(val, "not-use-GL") == 0)
1772                 *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
1773         else if (strcasecmp(val, "use-GL") == 0)
1774                 *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
1775         else
1776                 *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
1777
1778         return PMINFO_R_OK;
1779 }
1780
1781 API int pkgmgrinfo_appinfo_get_screenreader(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_screenreader *screenreader)
1782 {
1783         char *val;
1784         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1785
1786         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1787         retvm_if(screenreader == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1788
1789         if (info->app_info == NULL || info->app_info->screenreader == NULL)
1790                 return PMINFO_R_ERROR;
1791
1792         val = (char *)info->app_info->screenreader;
1793         if (strcasecmp(val, "screenreader-off") == 0)
1794                 *screenreader = PMINFO_SCREENREADER_OFF;
1795         else if (strcasecmp(val, "screenreader-on") == 0)
1796                 *screenreader = PMINFO_SCREENREADER_ON;
1797         else
1798                 *screenreader = PMINFO_SCREENREADER_USE_SYSTEM_SETTING;
1799
1800         return PMINFO_R_OK;
1801 }
1802
1803 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h handle, char **portrait_img, char **landscape_img)
1804 {
1805         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1806
1807         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1808         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1809         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1810
1811         if (info->app_info == NULL || (info->app_info->portraitimg == NULL
1812                         && info->app_info->landscapeimg == NULL))
1813                 return PMINFO_R_ERROR;
1814
1815         *portrait_img = (char *)info->app_info->portraitimg;
1816         *landscape_img = (char *)info->app_info->landscapeimg;
1817
1818         return PMINFO_R_OK;
1819 }
1820
1821 API int pkgmgrinfo_appinfo_get_effectimage_type(pkgmgrinfo_appinfo_h handle, char **effectimage_type)
1822 {
1823         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1824
1825         if (handle == NULL || effectimage_type == NULL) {
1826                 LOGE("invalid parameter");
1827                 return PMINFO_R_EINVAL;
1828         }
1829
1830         if (info->app_info == NULL || info->app_info->effectimage_type == NULL)
1831                 return PMINFO_R_ERROR;
1832
1833         *effectimage_type = (char *)info->app_info->effectimage_type;
1834
1835         return PMINFO_R_OK;
1836 }
1837
1838 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
1839 {
1840         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1841
1842         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1843         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1844
1845         if (info->app_info == NULL || info->app_info->submode_mainid == NULL)
1846                 return PMINFO_R_ERROR;
1847
1848         *submode_mainid = (char *)info->app_info->submode_mainid;
1849
1850         return PMINFO_R_OK;
1851 }
1852
1853 API int pkgmgrinfo_appinfo_get_installed_storage_location(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_installed_storage *storage)
1854 {
1855         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1856         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1857
1858         if (info->app_info && info->app_info->installed_storage){
1859                  if (strcmp(info->app_info->installed_storage,"installed_internal") == 0)
1860                         *storage = PMINFO_INTERNAL_STORAGE;
1861                  else if (strcmp(info->app_info->installed_storage,"installed_external") == 0)
1862                          *storage = PMINFO_EXTERNAL_STORAGE;
1863                  else
1864                          return PMINFO_R_ERROR;
1865         }else
1866                 return PMINFO_R_ERROR;
1867
1868         return PMINFO_R_OK;
1869 }
1870
1871 API int pkgmgrinfo_appinfo_get_launch_mode(pkgmgrinfo_appinfo_h handle, char **mode)
1872 {
1873         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1874
1875         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1876         retvm_if(mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1877
1878         if (info->app_info->launch_mode == NULL)
1879                 return PMINFO_R_ERROR;
1880
1881         *mode = (char *)(info->app_info->launch_mode);
1882
1883         return PMINFO_R_OK;
1884 }
1885
1886 API int pkgmgrinfo_appinfo_get_alias_appid(pkgmgrinfo_appinfo_h handle, char **alias_appid)
1887 {
1888         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1889
1890         if (handle == NULL || alias_appid == NULL) {
1891                 LOGE("invalid parameter");
1892                 return PMINFO_R_EINVAL;
1893         }
1894
1895         if (info->app_info == NULL || info->app_info->alias_appid == NULL)
1896                 return PMINFO_R_ERROR;
1897
1898         *alias_appid = (char *)info->app_info->alias_appid;
1899
1900         return PMINFO_R_OK;
1901 }
1902
1903 API int pkgmgrinfo_appinfo_get_effective_appid(pkgmgrinfo_appinfo_h handle, char **effective_appid)
1904 {
1905         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1906
1907         if (handle == NULL || effective_appid == NULL) {
1908                 LOGE("invalid parameter");
1909                 return PMINFO_R_EINVAL;
1910         }
1911
1912         if (info->app_info == NULL || info->app_info->effective_appid == NULL)
1913                 return PMINFO_R_ERROR;
1914
1915         *effective_appid = (char *)info->app_info->effective_appid;
1916
1917         return PMINFO_R_OK;
1918 }
1919
1920 API int pkgmgrinfo_appinfo_usr_get_datacontrol_info(const char *providerid, const char *type, uid_t uid, char **appid, char **access)
1921 {
1922         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1923         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1924         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1925         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1926
1927         int ret = PMINFO_R_OK;
1928         char *query = NULL;
1929         sqlite3_stmt *stmt = NULL;
1930
1931         /*open db*/
1932         ret = __open_manifest_db(uid, true);
1933         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1934
1935         /*Start constructing query*/
1936         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q and type=%Q", providerid, type);
1937
1938         /*prepare query*/
1939         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1940         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1941
1942         /*step query*/
1943         ret = sqlite3_step(stmt);
1944         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1945
1946         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1947         *access = strdup((char *)sqlite3_column_text(stmt, 2));
1948
1949         ret = PMINFO_R_OK;
1950
1951 catch:
1952         sqlite3_free(query);
1953         sqlite3_finalize(stmt);
1954         __close_manifest_db();
1955         return ret;
1956 }
1957
1958 API int pkgmgrinfo_appinfo_get_datacontrol_info(const char *providerid, const char *type, char **appid, char **access)
1959 {
1960         return pkgmgrinfo_appinfo_usr_get_datacontrol_info(providerid, type, GLOBAL_USER, appid, access);
1961 }
1962
1963 API int pkgmgrinfo_appinfo_usr_get_datacontrol_appid(const char *providerid, uid_t uid, char **appid)
1964 {
1965         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1966         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1967
1968         int ret = PMINFO_R_OK;
1969         char *query = NULL;
1970         sqlite3_stmt *stmt = NULL;
1971
1972         /*open db*/
1973         ret = __open_manifest_db(uid, true);
1974         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1975
1976         /*Start constructing query*/
1977         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q", providerid);
1978
1979         /*prepare query*/
1980         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1981         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1982
1983         /*step query*/
1984         ret = sqlite3_step(stmt);
1985         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1986
1987         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1988
1989         ret = PMINFO_R_OK;
1990
1991 catch:
1992         sqlite3_free(query);
1993         sqlite3_finalize(stmt);
1994         __close_manifest_db();
1995         return ret;
1996 }
1997
1998 API int pkgmgrinfo_appinfo_get_datacontrol_appid(const char *providerid, char **appid)
1999 {
2000         return pkgmgrinfo_appinfo_usr_get_datacontrol_appid(providerid, GLOBAL_USER, appid);
2001 }
2002
2003 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
2004                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
2005 {
2006         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2007         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2008         int ret = -1;
2009         permission_x *ptr;
2010         GList *tmp;
2011         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2012
2013         if (info->app_info == NULL)
2014                 return PMINFO_R_ERROR;
2015
2016         for (tmp = info->app_info->permission; tmp; tmp = tmp->next) {
2017                 ptr = (permission_x *)tmp->data;
2018                 if (ptr == NULL)
2019                         continue;
2020                 if (ptr->value) {
2021                         ret = permission_func(ptr->value, user_data);
2022                         if (ret < 0)
2023                                 break;
2024                 }
2025         }
2026         return PMINFO_R_OK;
2027 }
2028
2029 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
2030                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
2031 {
2032         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2033         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2034         int ret = -1;
2035         const char *category;
2036         GList *tmp;
2037         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2038
2039         if (info->app_info == NULL)
2040                 return PMINFO_R_ERROR;
2041
2042         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
2043                 category = (const char *)tmp->data;
2044                 if (category) {
2045                         ret = category_func(category, user_data);
2046                         if (ret < 0)
2047                                 break;
2048                 }
2049         }
2050         return PMINFO_R_OK;
2051 }
2052
2053 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
2054                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
2055 {
2056         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2057         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2058         int ret = -1;
2059         metadata_x *ptr;
2060         GList *tmp;
2061         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2062
2063         if (info->app_info == NULL)
2064                 return PMINFO_R_ERROR;
2065
2066         for (tmp = info->app_info->metadata; tmp; tmp = tmp->next) {
2067                 ptr = (metadata_x *)tmp->data;
2068                 if (ptr == NULL)
2069                         continue;
2070                 if (ptr->key) {
2071                         ret = metadata_func(ptr->key, ptr->value, user_data);
2072                         if (ret < 0)
2073                                 break;
2074                 }
2075         }
2076         return PMINFO_R_OK;
2077 }
2078
2079 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
2080                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
2081 {
2082         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2083         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
2084         int ret;
2085         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2086         appcontrol_x *appcontrol;
2087         GList *tmp;
2088
2089         if (info->app_info == NULL)
2090                 return PMINFO_R_ERROR;
2091
2092         for (tmp = info->app_info->appcontrol; tmp; tmp = tmp->next) {
2093                 appcontrol = (appcontrol_x *)tmp->data;
2094                 if (appcontrol == NULL)
2095                         continue;
2096                 ret = appcontrol_func(appcontrol->operation, appcontrol->uri, appcontrol->mime, user_data);
2097                 if (ret < 0)
2098                         break;
2099         }
2100
2101         return PMINFO_R_OK;
2102 }
2103
2104 API int pkgmgrinfo_appinfo_foreach_background_category(
2105                 pkgmgrinfo_appinfo_h handle,
2106                 pkgmgrinfo_app_background_category_list_cb category_func,
2107                 void *user_data)
2108 {
2109         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2110         GList *tmp;
2111         char *category;
2112
2113         if (handle == NULL || category_func == NULL || info->app_info == NULL) {
2114                 LOGE("invalid parameter");
2115                 return PMINFO_R_EINVAL;
2116         }
2117
2118         for (tmp = info->app_info->background_category; tmp; tmp = tmp->next) {
2119                 category = (char *)tmp->data;
2120                 if (category == NULL)
2121                         continue;
2122
2123                 if (category_func(category, user_data) < 0)
2124                         break;
2125         }
2126
2127         return PMINFO_R_OK;
2128 }
2129
2130 API int pkgmgrinfo_appinfo_foreach_splash_screen(pkgmgrinfo_appinfo_h handle,
2131                 pkgmgrinfo_app_splash_screen_list_cb splash_screen_func,
2132                 void *user_data)
2133 {
2134         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2135         splashscreen_x *splashscreen;
2136         GList *tmp;
2137         int ret;
2138
2139         if (info == NULL || info->app_info == NULL
2140                         || splash_screen_func == NULL) {
2141                 LOGE("invalid parameter");
2142                 return PMINFO_R_EINVAL;
2143         }
2144
2145         for (tmp = info->app_info->splashscreens; tmp; tmp = tmp->next) {
2146                 splashscreen = (splashscreen_x *)tmp->data;
2147                 if (splashscreen == NULL)
2148                         continue;
2149                 ret = splash_screen_func(splashscreen->src,
2150                                 splashscreen->type,
2151                                 splashscreen->orientation,
2152                                 splashscreen->indicatordisplay,
2153                                 user_data);
2154                 if (ret < 0)
2155                         break;
2156         }
2157
2158         return PMINFO_R_OK;
2159 }
2160
2161 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h handle, bool *nodisplay)
2162 {
2163         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2164         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2165         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2166
2167         if (info->app_info == NULL || info->app_info->nodisplay == NULL)
2168                 return PMINFO_R_ERROR;
2169
2170         *nodisplay = _get_bool_value(info->app_info->nodisplay);
2171
2172         return PMINFO_R_OK;
2173 }
2174
2175 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h handle, bool *multiple)
2176 {
2177         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2178         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2179         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2180
2181         if (info->app_info == NULL || info->app_info->multiple == NULL)
2182                 return PMINFO_R_ERROR;
2183
2184         *multiple = _get_bool_value(info->app_info->multiple);
2185
2186         return PMINFO_R_OK;
2187 }
2188
2189 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
2190 {
2191         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2192         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2193         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2194
2195         if (info->app_info == NULL || info->app_info->indicatordisplay == NULL)
2196                 return PMINFO_R_ERROR;
2197
2198         *indicator_disp = _get_bool_value(info->app_info->indicatordisplay);
2199
2200         return PMINFO_R_OK;
2201 }
2202
2203 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
2204 {
2205         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2206         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2207         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2208
2209         if (info->app_info == NULL || info->app_info->taskmanage == NULL)
2210                 return PMINFO_R_ERROR;
2211
2212         *taskmanage = _get_bool_value(info->app_info->taskmanage);
2213
2214         return PMINFO_R_OK;
2215 }
2216
2217 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
2218 {
2219         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2220         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2221         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2222
2223         if (info->app_info == NULL || info->app_info->enabled == NULL)
2224                 return PMINFO_R_ERROR;
2225
2226         *enabled = _get_bool_value(info->app_info->enabled);
2227
2228         return PMINFO_R_OK;
2229 }
2230
2231 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
2232 {
2233         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2234         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2235         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2236
2237         if (info->app_info == NULL || info->app_info->onboot == NULL)
2238                 return PMINFO_R_ERROR;
2239
2240         *onboot = _get_bool_value(info->app_info->onboot);
2241
2242         return PMINFO_R_OK;
2243 }
2244
2245 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
2246 {
2247         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2248         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2249         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2250
2251         if (info->app_info == NULL || info->app_info->autorestart == NULL)
2252                 return PMINFO_R_ERROR;
2253
2254         *autorestart = _get_bool_value(info->app_info->autorestart);
2255
2256         return PMINFO_R_OK;
2257 }
2258
2259 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
2260 {
2261         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2262         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
2263         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2264
2265         if (info->app_info == NULL || info->app_info->mainapp == NULL)
2266                 return PMINFO_R_ERROR;
2267
2268         *mainapp = _get_bool_value(info->app_info->mainapp);
2269
2270         return PMINFO_R_OK;
2271 }
2272
2273 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
2274 {
2275         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2276         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2277         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2278
2279         if (info->app_info == NULL || info->app_info->preload == NULL)
2280                 return PMINFO_R_ERROR;
2281
2282         *preload = _get_bool_value(info->app_info->preload);
2283
2284         return PMINFO_R_OK;
2285 }
2286
2287 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
2288 {
2289         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2290         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2291         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2292
2293         if (info->app_info == NULL || info->app_info->submode == NULL)
2294                 return PMINFO_R_ERROR;
2295
2296         *submode = _get_bool_value(info->app_info->submode);
2297
2298         return PMINFO_R_OK;
2299 }
2300
2301 API int pkgmgrinfo_appinfo_is_process_pool(pkgmgrinfo_appinfo_h handle, bool *process_pool)
2302 {
2303         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2304
2305         if (handle == NULL || process_pool == NULL) {
2306                 LOGE("invalid parameter");
2307                 return PMINFO_R_EINVAL;
2308         }
2309
2310         if (info->app_info == NULL)
2311                 return PMINFO_R_ERROR;
2312
2313         *process_pool = _get_bool_value(info->app_info->process_pool);
2314
2315         return PMINFO_R_OK;
2316 }
2317
2318 API int pkgmgrinfo_appinfo_is_category_exist(pkgmgrinfo_appinfo_h handle, const char *category, bool *exist)
2319 {
2320         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2321         retvm_if(category == NULL, PMINFO_R_EINVAL, "category is NULL");
2322         retvm_if(exist == NULL, PMINFO_R_EINVAL, "exist is NULL");
2323
2324         const char *val;
2325         GList *tmp;
2326         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2327
2328         if (info->app_info == NULL)
2329                 return PMINFO_R_ERROR;
2330
2331         *exist = 0;
2332         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
2333                 val = (const char *)tmp->data;
2334                 if (val == NULL)
2335                         continue;
2336                 if (strcasecmp(val, category) == 0) {
2337                         *exist = 1;
2338                         break;
2339                 }
2340         }
2341
2342         return PMINFO_R_OK;
2343 }
2344
2345 API int pkgmgrinfo_appinfo_is_ui_gadget(pkgmgrinfo_appinfo_h handle,
2346                 bool *ui_gadget)
2347 {
2348         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2349
2350         if (info == NULL || info->app_info == NULL || ui_gadget == NULL) {
2351                 _LOGE("invalid parameter");
2352                 return PMINFO_R_EINVAL;
2353         }
2354
2355         *ui_gadget = _get_bool_value(info->app_info->ui_gadget);
2356
2357         return PMINFO_R_OK;
2358 }
2359
2360 API int pkgmgrinfo_appinfo_is_support_disable(pkgmgrinfo_appinfo_h handle,
2361                 bool *support_disable)
2362 {
2363         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2364
2365         if (info == NULL || info->app_info == NULL || support_disable == NULL) {
2366                 _LOGE("invalid parameter");
2367                 return PMINFO_R_EINVAL;
2368         }
2369
2370         *support_disable = _get_bool_value(info->app_info->support_disable);
2371
2372         return PMINFO_R_OK;
2373 }
2374
2375 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle)
2376 {
2377         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
2378         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2379         __cleanup_appinfo(info);
2380         return PMINFO_R_OK;
2381 }
2382
2383 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
2384 {
2385         return (pkgmgrinfo_pkginfo_filter_create(handle));
2386 }
2387
2388 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
2389 {
2390         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
2391 }
2392
2393 static gint __compare_func(gconstpointer data1, gconstpointer data2)
2394 {
2395         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
2396         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
2397         if (node1->prop == node2->prop)
2398                 return 0;
2399         else if (node1->prop > node2->prop)
2400                 return 1;
2401         else
2402                 return -1;
2403 }
2404
2405 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
2406                                 const char *property, const int value)
2407 {
2408         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2409         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2410         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
2411         char *val = NULL;
2412         GSList *link = NULL;
2413         int prop = -1;
2414         prop = _pminfo_appinfo_convert_to_prop_int(property);
2415         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
2416                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
2417                 _LOGE("Invalid Integer Property\n");
2418                 return PMINFO_R_EINVAL;
2419         }
2420         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2421         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2422         if (node == NULL) {
2423                 _LOGE("Out of Memory!!!\n");
2424                 return PMINFO_R_ERROR;
2425         }
2426         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
2427         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
2428         if (val == NULL) {
2429                 _LOGE("Out of Memory\n");
2430                 free(node);
2431                 node = NULL;
2432                 return PMINFO_R_ERROR;
2433         }
2434         node->prop = prop;
2435         node->value = val;
2436         /*If API is called multiple times for same property, we should override the previous values.
2437         Last value set will be used for filtering.*/
2438         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2439         if (link)
2440                 filter->list = g_slist_delete_link(filter->list, link);
2441         filter->list = g_slist_append(filter->list, (gpointer)node);
2442         return PMINFO_R_OK;
2443
2444 }
2445
2446 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
2447                                 const char *property, const bool value)
2448 {
2449         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2450         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2451         char *val = NULL;
2452         GSList *link = NULL;
2453         int prop = -1;
2454         prop = _pminfo_appinfo_convert_to_prop_bool(property);
2455         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
2456                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
2457                 _LOGE("Invalid Boolean Property\n");
2458                 return PMINFO_R_EINVAL;
2459         }
2460         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2461         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2462         if (node == NULL) {
2463                 _LOGE("Out of Memory!!!\n");
2464                 return PMINFO_R_ERROR;
2465         }
2466         if (value)
2467                 val = strndup("('true','True')", 15);
2468         else
2469                 val = strndup("('false','False')", 17);
2470         if (val == NULL) {
2471                 _LOGE("Out of Memory\n");
2472                 free(node);
2473                 node = NULL;
2474                 return PMINFO_R_ERROR;
2475         }
2476         node->prop = prop;
2477         node->value = val;
2478         /*If API is called multiple times for same property, we should override the previous values.
2479         Last value set will be used for filtering.*/
2480         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2481         if (link)
2482                 filter->list = g_slist_delete_link(filter->list, link);
2483         filter->list = g_slist_append(filter->list, (gpointer)node);
2484         return PMINFO_R_OK;
2485
2486 }
2487
2488 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
2489                                 const char *property, const char *value)
2490 {
2491         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2492         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2493         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2494         char *val = NULL;
2495         pkgmgrinfo_node_x *ptr = NULL;
2496         char prev[PKG_STRING_LEN_MAX] = {'\0'};
2497         char temp[PKG_STRING_LEN_MAX] = {'\0'};
2498         GSList *link = NULL;
2499         int prop = -1;
2500         prop = _pminfo_appinfo_convert_to_prop_str(property);
2501         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
2502                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
2503                 _LOGE("Invalid String Property\n");
2504                 return PMINFO_R_EINVAL;
2505         }
2506         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2507         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2508         if (node == NULL) {
2509                 _LOGE("Out of Memory!!!\n");
2510                 return PMINFO_R_ERROR;
2511         }
2512         node->prop = prop;
2513         switch (prop) {
2514         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
2515                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
2516                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
2517                 else
2518                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
2519                 node->value = val;
2520                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2521                 if (link)
2522                         filter->list = g_slist_delete_link(filter->list, link);
2523                 filter->list = g_slist_append(filter->list, (gpointer)node);
2524                 break;
2525         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
2526         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
2527         case E_PMINFO_APPINFO_PROP_APP_URI:
2528         case E_PMINFO_APPINFO_PROP_APP_MIME:
2529                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
2530                 if (val == NULL) {
2531                         _LOGE("Out of Memory\n");
2532                         free(node);
2533                         node = NULL;
2534                         return PMINFO_R_ERROR;
2535                 }
2536                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2537                 if (link) {
2538                         ptr = (pkgmgrinfo_node_x *)link->data;
2539                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
2540                         _LOGE("Previous value is %s\n", prev);
2541                         filter->list = g_slist_delete_link(filter->list, link);
2542                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
2543                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
2544                         _LOGE("New value is %s\n", val);
2545                         node->value = val;
2546                         filter->list = g_slist_append(filter->list, (gpointer)node);
2547                         memset(temp, '\0', PKG_STRING_LEN_MAX);
2548                 } else {
2549                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
2550                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
2551                         _LOGE("First value is %s\n", val);
2552                         node->value = val;
2553                         filter->list = g_slist_append(filter->list, (gpointer)node);
2554                         memset(temp, '\0', PKG_STRING_LEN_MAX);
2555                 }
2556                 break;
2557         default:
2558                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
2559                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2560                 if (link)
2561                         filter->list = g_slist_delete_link(filter->list, link);
2562                 filter->list = g_slist_append(filter->list, (gpointer)node);
2563                 break;
2564         }
2565         return PMINFO_R_OK;
2566 }
2567
2568 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
2569 {
2570         int ret;
2571         GList *list = NULL;
2572
2573         if (handle == NULL || count == NULL) {
2574                 _LOGE("invalid parameter");
2575                 return PMINFO_R_EINVAL;
2576         }
2577
2578         ret = _appinfo_get_filtered_list(handle, uid, &list);
2579         if (ret != PMINFO_R_OK)
2580                 return PMINFO_R_ERROR;
2581
2582         *count = g_list_length(list);
2583
2584         g_list_free_full(list, free);
2585
2586         return PMINFO_R_OK;
2587 }
2588
2589 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
2590 {
2591         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, GLOBAL_USER);
2592 }
2593
2594 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(
2595                 pkgmgrinfo_appinfo_filter_h handle,
2596                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2597 {
2598         if (handle == NULL || app_cb == NULL) {
2599                 LOGE("invalid parameter");
2600                 return PMINFO_R_EINVAL;
2601         }
2602
2603         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2604                         user_data);
2605 }
2606
2607 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
2608                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
2609 {
2610         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, GLOBAL_USER);
2611 }
2612
2613 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
2614 {
2615         return (pkgmgrinfo_pkginfo_filter_create(handle));
2616 }
2617
2618 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
2619 {
2620         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
2621 }
2622
2623 API int pkgmgrinfo_appinfo_metadata_filter_add(
2624                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2625                 const char *key, const char *value)
2626 {
2627         int ret;
2628
2629         ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2630                         PMINFO_APPINFO_PROP_APP_METADATA_KEY, key);
2631         if (ret != PMINFO_R_OK)
2632                 return ret;
2633
2634         /* value can be NULL.
2635          * In that case all apps with specified key should be displayed
2636          */
2637         if (value) {
2638                 ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2639                                 PMINFO_APPINFO_PROP_APP_METADATA_VALUE, value);
2640                 if (ret != PMINFO_R_OK)
2641                         return ret;
2642         }
2643
2644         return PMINFO_R_OK;
2645 }
2646
2647 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(
2648                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2649                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2650 {
2651         if (handle == NULL || app_cb == NULL) {
2652                 LOGE("invalid parameter");
2653                 return PMINFO_R_EINVAL;
2654         }
2655
2656         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2657                         user_data);
2658 }
2659
2660 API int pkgmgrinfo_appinfo_metadata_filter_foreach(
2661                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2662                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
2663 {
2664         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb,
2665                         user_data, GLOBAL_USER);
2666 }
2667
2668 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
2669 {
2670         const char *val;
2671         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2672
2673         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2674         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2675
2676         val = info->app_info->guestmode_visibility;
2677         *status = _get_bool_value(val);
2678         return PMINFO_R_OK;
2679 }