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