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