Each user can disable global app separately
[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 "
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
630         info->background_category = __get_background_category(bg_category_str);
631
632         if (_appinfo_get_label(db, info->appid, locale, &info->label)) {
633                 pkgmgrinfo_basic_free_application(info);
634                 sqlite3_finalize(stmt);
635                 return PMINFO_R_ERROR;
636         }
637
638         if (_appinfo_get_icon(db, info->appid, locale, &info->icon)) {
639                 pkgmgrinfo_basic_free_application(info);
640                 sqlite3_finalize(stmt);
641                 return PMINFO_R_ERROR;
642         }
643
644         if (_appinfo_get_category(db, info->appid, &info->category)) {
645                 pkgmgrinfo_basic_free_application(info);
646                 sqlite3_finalize(stmt);
647                 return PMINFO_R_ERROR;
648         }
649
650         if (_appinfo_get_app_control(db, info->appid, &info->appcontrol)) {
651                 pkgmgrinfo_basic_free_application(info);
652                 sqlite3_finalize(stmt);
653                 return PMINFO_R_ERROR;
654         }
655
656         if (_appinfo_get_data_control(db, info->appid, &info->datacontrol)) {
657                 pkgmgrinfo_basic_free_application(info);
658                 sqlite3_finalize(stmt);
659                 return PMINFO_R_ERROR;
660         }
661
662         if (_appinfo_get_metadata(db, info->appid, &info->metadata)) {
663                 pkgmgrinfo_basic_free_application(info);
664                 sqlite3_finalize(stmt);
665                 return PMINFO_R_ERROR;
666         }
667
668         *application = info;
669
670         sqlite3_finalize(stmt);
671
672         return PMINFO_R_OK;
673 }
674
675 static int _appinfo_get_appinfo(const char *appid, uid_t uid,
676                 pkgmgr_appinfo_x **appinfo)
677 {
678         int ret;
679         sqlite3 *db;
680         const char *dbpath;
681         char *locale;
682         pkgmgr_appinfo_x *info;
683
684         dbpath = getUserPkgParserDBPathUID(uid);
685         if (dbpath == NULL)
686                 return PMINFO_R_ERROR;
687
688         locale = _get_system_locale();
689         if (locale == NULL)
690                 return PMINFO_R_ERROR;
691
692         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
693         if (ret != SQLITE_OK) {
694                 _LOGE("failed to open db: %d", ret);
695                 free(locale);
696                 return PMINFO_R_ERROR;
697         }
698
699         info = calloc(1, sizeof(pkgmgr_appinfo_x));
700         if (info == NULL) {
701                 _LOGE("out of memory");
702                 free(locale);
703                 sqlite3_close_v2(db);
704                 return PMINFO_R_ERROR;
705         }
706
707         ret = _appinfo_get_application(db, appid, locale, &info->app_info);
708         if (ret != PMINFO_R_OK) {
709                 free(info);
710                 free(locale);
711                 sqlite3_close_v2(db);
712                 return ret;
713         }
714
715         info->locale = locale;
716         info->package = strdup(info->app_info->package);
717
718         *appinfo = info;
719
720         sqlite3_close_v2(db);
721
722         return ret;
723 }
724
725 API int pkgmgrinfo_appinfo_get_usr_appinfo(const char *appid, uid_t uid,
726                 pkgmgrinfo_appinfo_h *handle)
727 {
728         int ret;
729
730         if (appid == NULL || handle == NULL) {
731                 LOGE("invalid parameter");
732                 return PMINFO_R_EINVAL;
733         }
734
735         ret = _appinfo_get_appinfo(appid, uid, (pkgmgr_appinfo_x **)handle);
736         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
737                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
738                                 (pkgmgr_appinfo_x **)handle);
739
740         if (ret != PMINFO_R_OK)
741                 _LOGE("failed to get appinfo of %s for user %d", appid, uid);
742
743         return ret;
744 }
745
746 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
747 {
748         return pkgmgrinfo_appinfo_get_usr_appinfo(appid, GLOBAL_USER, handle);
749 }
750
751 static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
752                 pkgmgrinfo_filter_x *filter, pkgmgrinfo_app_list_cb app_list_cb,
753                 void *user_data)
754 {
755         int ret;
756         pkgmgr_appinfo_x *info;
757         GList *list = NULL;
758         GList *tmp;
759         char *appid;
760         int stop = 0;
761
762         ret = _appinfo_get_filtered_list(filter, uid, &list);
763         if (ret != PMINFO_R_OK)
764                 return PMINFO_R_ERROR;
765
766         for (tmp = list; tmp; tmp = tmp->next) {
767                 appid = (char *)tmp->data;
768                 if (stop == 0) {
769                         ret = _appinfo_get_appinfo(appid, uid, &info);
770                         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
771                                 ret = _appinfo_get_appinfo(appid, GLOBAL_USER,
772                                                 &info);
773                         if (ret != PMINFO_R_OK) {
774                                 free(appid);
775                                 continue;
776                         }
777                         if (app_list_cb(info, user_data) < 0)
778                                 stop = 1;
779                         pkgmgrinfo_appinfo_destroy_appinfo(info);
780                 }
781                 free(appid);
782         }
783
784         g_list_free(list);
785
786         return PMINFO_R_OK;
787 }
788
789 API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle,
790                 pkgmgrinfo_app_component component,
791                 pkgmgrinfo_app_list_cb app_func, void *user_data, uid_t uid)
792 {
793         int ret;
794         pkgmgrinfo_appinfo_filter_h filter;
795         char *pkgid;
796         const char *comp_str = NULL;
797
798         if (handle == NULL || app_func == NULL) {
799                 LOGE("invalied parameter");
800                 return PMINFO_R_EINVAL;
801         }
802
803         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid)) {
804                 LOGE("invalid parameter");
805                 return PMINFO_R_EINVAL;
806         }
807
808         if (pkgmgrinfo_appinfo_filter_create(&filter))
809                 return PMINFO_R_ERROR;
810
811         if (pkgmgrinfo_appinfo_filter_add_string(filter,
812                                 PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid)) {
813                 pkgmgrinfo_appinfo_filter_destroy(filter);
814                 return PMINFO_R_ERROR;
815         }
816
817         if (uid == GLOBAL_USER) {
818                 if (pkgmgrinfo_appinfo_filter_add_int(filter,
819                                         PMINFO_APPINFO_PROP_APP_DISABLE_FOR_USER, (int)getuid())) {
820                         pkgmgrinfo_appinfo_filter_destroy(filter);
821                         return PMINFO_R_ERROR;
822                 }
823         }
824
825         switch (component) {
826         case PMINFO_UI_APP:
827                 comp_str = PMINFO_APPINFO_UI_APP;
828                 break;
829         case PMINFO_SVC_APP:
830                 comp_str = PMINFO_APPINFO_SVC_APP;
831                 break;
832         default:
833                 break;
834         }
835
836         if (comp_str) {
837                 if (pkgmgrinfo_appinfo_filter_add_string(filter,
838                                         PMINFO_APPINFO_PROP_APP_COMPONENT,
839                                         comp_str)) {
840                         pkgmgrinfo_appinfo_filter_destroy(filter);
841                         return PMINFO_R_ERROR;
842                 }
843         }
844
845         ret = _appinfo_get_filtered_foreach_appinfo(uid, filter, app_func,
846                         user_data);
847
848         pkgmgrinfo_appinfo_filter_destroy(filter);
849
850         return ret;
851 }
852
853 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
854                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
855 {
856         return pkgmgrinfo_appinfo_get_usr_list(handle, component, app_func, user_data, GLOBAL_USER);
857 }
858
859 API int pkgmgrinfo_appinfo_get_usr_install_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
860 {
861         if (app_func == NULL) {
862                 LOGE("invalid parameter");
863                 return PMINFO_R_EINVAL;
864         }
865
866         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
867                         user_data);
868 }
869
870 API int pkgmgrinfo_appinfo_get_install_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
871 {
872         return pkgmgrinfo_appinfo_get_usr_install_list(app_func, GLOBAL_USER, user_data);
873 }
874
875 API int pkgmgrinfo_appinfo_get_usr_installed_list(pkgmgrinfo_app_list_cb app_func, uid_t uid, void *user_data)
876 {
877         if (app_func == NULL) {
878                 LOGE("invalid parameter");
879                 return PMINFO_R_EINVAL;
880         }
881
882         return _appinfo_get_filtered_foreach_appinfo(uid, NULL, app_func,
883                         user_data);
884 }
885
886 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
887 {
888         return pkgmgrinfo_appinfo_get_usr_installed_list(app_func, GLOBAL_USER, user_data);
889 }
890
891 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h handle, char **appid)
892 {
893         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
894
895         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
896         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
897
898         if (info->app_info == NULL || info->app_info->appid == NULL)
899                 return PMINFO_R_ERROR;
900         *appid = (char *)info->app_info->appid;
901
902         return PMINFO_R_OK;
903 }
904
905 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, char **pkg_name)
906 {
907         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
908
909         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
910         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
911
912         if (info->package == NULL)
913                 return PMINFO_R_ERROR;
914
915         *pkg_name = (char *)info->package;
916
917         return PMINFO_R_OK;
918 }
919
920 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h handle, char **pkgid)
921 {
922         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
923
924         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
925         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
926
927         if (info->package == NULL)
928                 return PMINFO_R_ERROR;
929
930         *pkgid = (char *)info->package;
931
932         return PMINFO_R_OK;
933 }
934
935 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, char **exec)
936 {
937         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
938
939         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
940         retvm_if(exec == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
941
942         if (info->app_info == NULL || info->app_info->exec == NULL)
943                 return PMINFO_R_ERROR;
944         *exec = (char *)info->app_info->exec;
945
946         return PMINFO_R_OK;
947 }
948
949
950 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, char **icon)
951 {
952         const char *locale;
953         icon_x *ptr;
954         GList *tmp;
955         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
956
957         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
958         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
959
960         locale = info->locale;
961         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
962
963         if (info->app_info == NULL)
964                 return PMINFO_R_ERROR;
965
966         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
967                 ptr = (icon_x *)tmp->data;
968                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
969                                 !strcasecmp(ptr->text, "(null)") ||
970                                 strcmp(ptr->lang, locale))
971                         continue;
972                 *icon = (char *)ptr->text;
973                 return PMINFO_R_OK;
974         }
975
976         locale = DEFAULT_LOCALE;
977         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
978                 ptr = (icon_x *)tmp->data;
979                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
980                                 strcmp(ptr->lang, locale))
981                         continue;
982                 *icon = (char *)ptr->text;
983                 return PMINFO_R_OK;
984         }
985
986         return PMINFO_R_ERROR;
987 }
988
989
990 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, char **label)
991 {
992         const char *locale;
993         label_x *ptr;
994         GList *tmp;
995         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
996
997         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
998         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
999
1000         locale = info->locale;
1001         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1002
1003         if (info->app_info == NULL)
1004                 return PMINFO_R_ERROR;
1005
1006         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
1007                 ptr = (label_x *)tmp->data;
1008                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1009                                 strcmp(ptr->lang, locale))
1010                         continue;
1011                 *label = (char *)ptr->text;
1012                 return PMINFO_R_OK;
1013         }
1014
1015         locale = DEFAULT_LOCALE;
1016         for (tmp = info->app_info->label; tmp; tmp = tmp->next) {
1017                 ptr = (label_x *)tmp->data;
1018                 if (ptr == NULL || ptr->text == NULL || ptr->lang == NULL ||
1019                                 strcmp(ptr->lang, locale))
1020                         continue;
1021                 *label = (char *)ptr->text;
1022                 return PMINFO_R_OK;
1023         }
1024
1025         return PMINFO_R_ERROR;
1026 }
1027
1028 static char *_get_localed_label(const char *appid, const char *locale, uid_t uid)
1029 {
1030         char *result = NULL;
1031         char *query = NULL;
1032         sqlite3_stmt *stmt = NULL;
1033         sqlite3 *db = NULL;
1034         char *val;
1035         const char *manifest_db;
1036
1037         manifest_db = getUserPkgParserDBPathUID(uid);
1038         if (manifest_db == NULL) {
1039                 _LOGE("Failed to get manifest db path");
1040                 goto err;
1041         }
1042
1043         if (sqlite3_open_v2(manifest_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
1044                 _LOGE("DB open fail\n");
1045                 goto err;
1046         }
1047
1048         query = sqlite3_mprintf("select app_label from package_app_localized_info where app_id=%Q and app_locale=%Q", appid, locale);
1049         if (query == NULL) {
1050                 _LOGE("Out of memory");
1051                 goto err;
1052         }
1053
1054         if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) {
1055                 _LOGE("prepare_v2 fail\n");
1056                 goto err;
1057         }
1058
1059         if (sqlite3_step(stmt) == SQLITE_ROW) {
1060                 val = (char *)sqlite3_column_text(stmt, 0);
1061                 if (val != NULL)
1062                         result = strdup(val);
1063         }
1064
1065 err:
1066         sqlite3_finalize(stmt);
1067         sqlite3_free(query);
1068         sqlite3_close(db);
1069
1070         return result;
1071 }
1072
1073 API int pkgmgrinfo_appinfo_usr_get_localed_label(const char *appid, const char *locale, uid_t uid, char **label)
1074 {
1075         char *val;
1076
1077         retvm_if(appid == NULL || locale == NULL || label == NULL, PMINFO_R_EINVAL, "Argument is NULL");
1078
1079         val = _get_localed_label(appid, locale, uid);
1080         if (val == NULL)
1081                 val = _get_localed_label(appid, DEFAULT_LOCALE, uid);
1082
1083         if (val == NULL)
1084                 return PMINFO_R_ERROR;
1085
1086         *label = val;
1087
1088         return PMINFO_R_OK;
1089 }
1090
1091 API int pkgmgrinfo_appinfo_get_localed_label(const char *appid, const char *locale, char **label)
1092 {
1093         return pkgmgrinfo_appinfo_usr_get_localed_label(appid, locale, GLOBAL_USER, label);
1094 }
1095
1096 static pkgmgrinfo_app_component __appcomponent_convert(const char *comp)
1097 {
1098         if ( strcasecmp(comp, "uiapp") == 0)
1099                 return PMINFO_UI_APP;
1100         else if ( strcasecmp(comp, "svcapp") == 0)
1101                 return PMINFO_SVC_APP;
1102         else
1103                 return -1;
1104 }
1105
1106 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_component *component)
1107 {
1108         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1109         int comp;
1110
1111         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1112         retvm_if(component == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1113
1114         if (info->app_info == NULL)
1115                 return PMINFO_R_ERROR;
1116
1117         comp = __appcomponent_convert(info->app_info->component);
1118         if (comp < 0)
1119                 return PMINFO_R_ERROR;
1120
1121         *component = comp;
1122
1123         return PMINFO_R_OK;
1124 }
1125
1126 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, char **app_type)
1127 {
1128         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1129
1130         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1131         retvm_if(app_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1132
1133         if (info->app_info == NULL || info->app_info->type == NULL)
1134                 return PMINFO_R_ERROR;
1135         *app_type = (char *)info->app_info->type;
1136
1137         return PMINFO_R_OK;
1138 }
1139
1140 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
1141                                         int *operation_count, char ***operation)
1142 {
1143         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1144         retvm_if(operation == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1145         retvm_if(operation_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1146         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1147         *operation_count = data->operation_count;
1148         *operation = data->operation;
1149         return PMINFO_R_OK;
1150 }
1151
1152 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
1153                                         int *uri_count, char ***uri)
1154 {
1155         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1156         retvm_if(uri == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1157         retvm_if(uri_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1158         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1159         *uri_count = data->uri_count;
1160         *uri = data->uri;
1161         return PMINFO_R_OK;
1162 }
1163
1164 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
1165                                         int *mime_count, char ***mime)
1166 {
1167         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1168         retvm_if(mime == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1169         retvm_if(mime_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1170         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1171         *mime_count = data->mime_count;
1172         *mime = data->mime;
1173         return PMINFO_R_OK;
1174 }
1175
1176 API int pkgmgrinfo_appinfo_get_subapp(pkgmgrinfo_appcontrol_h  handle,
1177                                         int *subapp_count, char ***subapp)
1178 {
1179         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1180         retvm_if(subapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1181         retvm_if(subapp_count == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1182         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
1183         *subapp_count = data->subapp_count;
1184         *subapp = data->subapp;
1185         return PMINFO_R_OK;
1186 }
1187
1188 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1189 {
1190         char *val;
1191         icon_x *ptr;
1192         GList *tmp;
1193         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1194
1195         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1196         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1197
1198         if (info->app_info == NULL)
1199                 return PMINFO_R_ERROR;
1200
1201         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1202                 ptr = (icon_x *)tmp->data;
1203                 if (ptr == NULL || ptr->section == NULL)
1204                         continue;
1205
1206                 val = (char *)ptr->section;
1207                 if (val && strcmp(val, "setting") == 0) {
1208                         *icon = (char *)ptr->text;
1209                         return PMINFO_R_OK;
1210                 }
1211         }
1212
1213         return PMINFO_R_ERROR;
1214 }
1215
1216
1217 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h handle, char **icon)
1218 {
1219         char *val;
1220         icon_x *ptr;
1221         GList *tmp;
1222         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1223
1224         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1225         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1226
1227         if (info->app_info == NULL)
1228                 return PMINFO_R_ERROR;
1229
1230         for (tmp = info->app_info->icon; tmp; tmp = tmp->next) {
1231                 ptr = (icon_x *)tmp->data;
1232                 if (ptr == NULL || ptr->section == NULL)
1233                         continue;
1234
1235                 val = (char *)ptr->section;
1236                 if (val && strcmp(val, "notification") == 0){
1237                         *icon = (char *)ptr->text;
1238                         return PMINFO_R_OK;
1239                 }
1240         }
1241
1242         return PMINFO_R_ERROR;
1243 }
1244
1245 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_recentimage *type)
1246 {
1247         char *val;
1248         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1249
1250         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1251         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1252
1253         if (info->app_info == NULL || info->app_info->recentimage == NULL)
1254                 return PMINFO_R_ERROR;
1255
1256         val = (char *)info->app_info->recentimage;
1257         if (strcasecmp(val, "capture") == 0)
1258                 *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
1259         else if (strcasecmp(val, "icon") == 0)
1260                 *type = PMINFO_RECENTIMAGE_USE_ICON;
1261         else
1262                 *type = PMINFO_RECENTIMAGE_USE_NOTHING;
1263
1264         return PMINFO_R_OK;
1265 }
1266
1267 API int pkgmgrinfo_appinfo_get_preview_image(pkgmgrinfo_appinfo_h handle, char **preview_img)
1268 {
1269         char *val;
1270         image_x *ptr;
1271         GList *tmp;
1272         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1273
1274         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1275         retvm_if(preview_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1276
1277         if (info->app_info == NULL)
1278                 return PMINFO_R_ERROR;
1279
1280         for (tmp = info->app_info->image; tmp; tmp = tmp->next) {
1281                 ptr = (image_x *)tmp->data;
1282                 if (ptr == NULL || ptr->section == NULL)
1283                         continue;
1284
1285                 val = (char *)ptr->section;
1286                 if (val && strcmp(val, "preview") == 0) {
1287                         *preview_img = (char *)ptr->text;
1288                         return PMINFO_R_OK;
1289                 }
1290         }
1291
1292         return PMINFO_R_ERROR;
1293 }
1294
1295 API int pkgmgrinfo_appinfo_get_permission_type(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_permission_type *permission)
1296 {
1297         const char *val;
1298         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1299
1300         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1301         retvm_if(permission == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1302
1303         val = info->app_info->permission_type;
1304         if (val == NULL)
1305                 return PMINFO_R_ERROR;
1306
1307         if (strcmp(val, "signature") == 0)
1308                 *permission = PMINFO_PERMISSION_SIGNATURE;
1309         else if (strcmp(val, "privilege") == 0)
1310                 *permission = PMINFO_PERMISSION_PRIVILEGE;
1311         else
1312                 *permission = PMINFO_PERMISSION_NORMAL;
1313
1314         return PMINFO_R_OK;
1315 }
1316
1317 API int pkgmgrinfo_appinfo_get_component_type(pkgmgrinfo_appinfo_h handle, char **component_type)
1318 {
1319         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1320
1321         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1322         retvm_if(component_type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1323
1324         if (info->app_info == NULL || info->app_info->component_type == NULL)
1325                 return PMINFO_R_ERROR;
1326
1327         *component_type = (char *)info->app_info->component_type;
1328
1329         return PMINFO_R_OK;
1330 }
1331
1332 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
1333 {
1334         char *val;
1335         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1336
1337         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1338         retvm_if(hwacceleration == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1339
1340         if (info->app_info == NULL || info->app_info->hwacceleration == NULL)
1341                 return PMINFO_R_ERROR;
1342
1343         val = (char *)info->app_info->hwacceleration;
1344         if (strcasecmp(val, "not-use-GL") == 0)
1345                 *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
1346         else if (strcasecmp(val, "use-GL") == 0)
1347                 *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
1348         else
1349                 *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
1350
1351         return PMINFO_R_OK;
1352 }
1353
1354 API int pkgmgrinfo_appinfo_get_screenreader(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_screenreader *screenreader)
1355 {
1356         char *val;
1357         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1358
1359         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1360         retvm_if(screenreader == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1361
1362         if (info->app_info == NULL || info->app_info->screenreader == NULL)
1363                 return PMINFO_R_ERROR;
1364
1365         val = (char *)info->app_info->screenreader;
1366         if (strcasecmp(val, "screenreader-off") == 0)
1367                 *screenreader = PMINFO_SCREENREADER_OFF;
1368         else if (strcasecmp(val, "screenreader-on") == 0)
1369                 *screenreader = PMINFO_SCREENREADER_ON;
1370         else
1371                 *screenreader = PMINFO_SCREENREADER_USE_SYSTEM_SETTING;
1372
1373         return PMINFO_R_OK;
1374 }
1375
1376 API int pkgmgrinfo_appinfo_get_effectimage(pkgmgrinfo_appinfo_h handle, char **portrait_img, char **landscape_img)
1377 {
1378         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1379
1380         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1381         retvm_if(portrait_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1382         retvm_if(landscape_img == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1383
1384         if (info->app_info == NULL || info->app_info->portraitimg ||
1385                         info->app_info->landscapeimg == NULL)
1386                 return PMINFO_R_ERROR;
1387
1388         *portrait_img = (char *)info->app_info->portraitimg;
1389         *landscape_img = (char *)info->app_info->landscapeimg;
1390
1391         return PMINFO_R_OK;
1392 }
1393
1394 API int pkgmgrinfo_appinfo_get_effectimage_type(pkgmgrinfo_appinfo_h handle, char **effectimage_type)
1395 {
1396         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1397
1398         if (handle == NULL || effectimage_type == NULL) {
1399                 LOGE("invalid parameter");
1400                 return PMINFO_R_EINVAL;
1401         }
1402
1403         if (info->app_info == NULL || info->app_info->effectimage_type == NULL)
1404                 return PMINFO_R_ERROR;
1405
1406         *effectimage_type = (char *)info->app_info->effectimage_type;
1407
1408         return PMINFO_R_OK;
1409 }
1410
1411 API int pkgmgrinfo_appinfo_get_submode_mainid(pkgmgrinfo_appinfo_h  handle, char **submode_mainid)
1412 {
1413         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1414
1415         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1416         retvm_if(submode_mainid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1417
1418         if (info->app_info == NULL || info->app_info->submode_mainid == NULL)
1419                 return PMINFO_R_ERROR;
1420
1421         *submode_mainid = (char *)info->app_info->submode_mainid;
1422
1423         return PMINFO_R_OK;
1424 }
1425
1426 API int pkgmgrinfo_appinfo_get_installed_storage_location(pkgmgrinfo_appinfo_h handle, pkgmgrinfo_installed_storage *storage)
1427 {
1428         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1429         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1430
1431         if (info->app_info && info->app_info->installed_storage){
1432                  if (strcmp(info->app_info->installed_storage,"installed_internal") == 0)
1433                         *storage = PMINFO_INTERNAL_STORAGE;
1434                  else if (strcmp(info->app_info->installed_storage,"installed_external") == 0)
1435                          *storage = PMINFO_EXTERNAL_STORAGE;
1436                  else
1437                          return PMINFO_R_ERROR;
1438         }else
1439                 return PMINFO_R_ERROR;
1440
1441         return PMINFO_R_OK;
1442 }
1443
1444 API int pkgmgrinfo_appinfo_get_launch_mode(pkgmgrinfo_appinfo_h handle, char **mode)
1445 {
1446         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1447
1448         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1449         retvm_if(mode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1450
1451         if (info->app_info->launch_mode == NULL)
1452                 return PMINFO_R_ERROR;
1453
1454         *mode = (char *)(info->app_info->launch_mode);
1455
1456         return PMINFO_R_OK;
1457 }
1458
1459 API int pkgmgrinfo_appinfo_get_alias_appid(pkgmgrinfo_appinfo_h handle, char **alias_appid)
1460 {
1461         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1462
1463         if (handle == NULL || alias_appid == NULL) {
1464                 LOGE("invalid parameter");
1465                 return PMINFO_R_EINVAL;
1466         }
1467
1468         if (info->app_info == NULL || info->app_info->alias_appid == NULL)
1469                 return PMINFO_R_ERROR;
1470
1471         *alias_appid = (char *)info->app_info->alias_appid;
1472
1473         return PMINFO_R_OK;
1474 }
1475
1476 API int pkgmgrinfo_appinfo_get_effective_appid(pkgmgrinfo_appinfo_h handle, char **effective_appid)
1477 {
1478         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1479
1480         if (handle == NULL || effective_appid == NULL) {
1481                 LOGE("invalid parameter");
1482                 return PMINFO_R_EINVAL;
1483         }
1484
1485         if (info->app_info == NULL || info->app_info->effective_appid == NULL)
1486                 return PMINFO_R_ERROR;
1487
1488         *effective_appid = (char *)info->app_info->effective_appid;
1489
1490         return PMINFO_R_OK;
1491 }
1492
1493 API int pkgmgrinfo_appinfo_usr_get_datacontrol_info(const char *providerid, const char *type, uid_t uid, char **appid, char **access)
1494 {
1495         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1496         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1497         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1498         retvm_if(access == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1499
1500         int ret = PMINFO_R_OK;
1501         char *query = NULL;
1502         sqlite3_stmt *stmt = NULL;
1503
1504         /*open db*/
1505         ret = __open_manifest_db(uid, true);
1506         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1507
1508         /*Start constructing query*/
1509         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q and type=%Q", providerid, type);
1510
1511         /*prepare query*/
1512         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1513         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1514
1515         /*step query*/
1516         ret = sqlite3_step(stmt);
1517         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1518
1519         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1520         *access = strdup((char *)sqlite3_column_text(stmt, 2));
1521
1522         ret = PMINFO_R_OK;
1523
1524 catch:
1525         sqlite3_free(query);
1526         sqlite3_finalize(stmt);
1527         __close_manifest_db();
1528         return ret;
1529 }
1530
1531 API int pkgmgrinfo_appinfo_get_datacontrol_info(const char *providerid, const char *type, char **appid, char **access)
1532 {
1533         return pkgmgrinfo_appinfo_usr_get_datacontrol_info(providerid, type, GLOBAL_USER, appid, access);
1534 }
1535
1536 API int pkgmgrinfo_appinfo_usr_get_datacontrol_appid(const char *providerid, uid_t uid, char **appid)
1537 {
1538         retvm_if(providerid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
1539         retvm_if(appid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1540
1541         int ret = PMINFO_R_OK;
1542         char *query = NULL;
1543         sqlite3_stmt *stmt = NULL;
1544
1545         /*open db*/
1546         ret = __open_manifest_db(uid, true);
1547         retvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "connect db [%s] failed!", MANIFEST_DB);
1548
1549         /*Start constructing query*/
1550         query = sqlite3_mprintf("select * from package_app_data_control where providerid=%Q", providerid);
1551
1552         /*prepare query*/
1553         ret = sqlite3_prepare_v2(GET_DB(manifest_db), query, strlen(query), &stmt, NULL);
1554         tryvm_if(ret != PMINFO_R_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 failed[%s]\n", query);
1555
1556         /*step query*/
1557         ret = sqlite3_step(stmt);
1558         tryvm_if((ret != SQLITE_ROW) || (ret == SQLITE_DONE), ret = PMINFO_R_ERROR, "No records found");
1559
1560         *appid = strdup((char *)sqlite3_column_text(stmt, 0));
1561
1562         ret = PMINFO_R_OK;
1563
1564 catch:
1565         sqlite3_free(query);
1566         sqlite3_finalize(stmt);
1567         __close_manifest_db();
1568         return ret;
1569 }
1570
1571 API int pkgmgrinfo_appinfo_get_datacontrol_appid(const char *providerid, char **appid)
1572 {
1573         return pkgmgrinfo_appinfo_usr_get_datacontrol_appid(providerid, GLOBAL_USER, appid);
1574 }
1575
1576 API int pkgmgrinfo_appinfo_foreach_permission(pkgmgrinfo_appinfo_h handle,
1577                         pkgmgrinfo_app_permission_list_cb permission_func, void *user_data)
1578 {
1579         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1580         retvm_if(permission_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1581         int ret = -1;
1582         permission_x *ptr;
1583         GList *tmp;
1584         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1585
1586         if (info->app_info == NULL)
1587                 return PMINFO_R_ERROR;
1588
1589         for (tmp = info->app_info->permission; tmp; tmp = tmp->next) {
1590                 ptr = (permission_x *)tmp->data;
1591                 if (ptr == NULL)
1592                         continue;
1593                 if (ptr->value) {
1594                         ret = permission_func(ptr->value, user_data);
1595                         if (ret < 0)
1596                                 break;
1597                 }
1598         }
1599         return PMINFO_R_OK;
1600 }
1601
1602 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
1603                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
1604 {
1605         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1606         retvm_if(category_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1607         int ret = -1;
1608         const char *category;
1609         GList *tmp;
1610         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1611
1612         if (info->app_info == NULL)
1613                 return PMINFO_R_ERROR;
1614
1615         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
1616                 category = (const char *)tmp->data;
1617                 if (category) {
1618                         ret = category_func(category, user_data);
1619                         if (ret < 0)
1620                                 break;
1621                 }
1622         }
1623         return PMINFO_R_OK;
1624 }
1625
1626 API int pkgmgrinfo_appinfo_foreach_metadata(pkgmgrinfo_appinfo_h handle,
1627                         pkgmgrinfo_app_metadata_list_cb metadata_func, void *user_data)
1628 {
1629         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1630         retvm_if(metadata_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1631         int ret = -1;
1632         metadata_x *ptr;
1633         GList *tmp;
1634         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1635
1636         if (info->app_info == NULL)
1637                 return PMINFO_R_ERROR;
1638
1639         for (tmp = info->app_info->metadata; tmp; tmp = tmp->next) {
1640                 ptr = (metadata_x *)tmp->data;
1641                 if (ptr == NULL)
1642                         continue;
1643                 if (ptr->key) {
1644                         ret = metadata_func(ptr->key, ptr->value, user_data);
1645                         if (ret < 0)
1646                                 break;
1647                 }
1648         }
1649         return PMINFO_R_OK;
1650 }
1651
1652 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
1653                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
1654 {
1655         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1656         retvm_if(appcontrol_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1657         int ret;
1658         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1659         appcontrol_x *appcontrol;
1660         GList *tmp;
1661
1662         if (info->app_info == NULL)
1663                 return PMINFO_R_ERROR;
1664
1665         for (tmp = info->app_info->appcontrol; tmp; tmp = tmp->next) {
1666                 appcontrol = (appcontrol_x *)tmp->data;
1667                 if (appcontrol == NULL)
1668                         continue;
1669                 ret = appcontrol_func(appcontrol->operation, appcontrol->uri, appcontrol->mime, user_data);
1670                 if (ret < 0)
1671                         break;
1672         }
1673
1674         return PMINFO_R_OK;
1675 }
1676
1677 API int pkgmgrinfo_appinfo_foreach_background_category(
1678                 pkgmgrinfo_appinfo_h handle,
1679                 pkgmgrinfo_app_background_category_list_cb category_func,
1680                 void *user_data)
1681 {
1682         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1683         GList *tmp;
1684         char *category;
1685
1686         if (handle == NULL || category_func == NULL || info->app_info == NULL) {
1687                 LOGE("invalid parameter");
1688                 return PMINFO_R_EINVAL;
1689         }
1690
1691         for (tmp = info->app_info->background_category; tmp; tmp = tmp->next) {
1692                 category = (char *)tmp->data;
1693                 if (category == NULL)
1694                         continue;
1695
1696                 if (category_func(category, user_data) < 0)
1697                         break;
1698         }
1699
1700         return PMINFO_R_OK;
1701 }
1702
1703 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h handle, bool *nodisplay)
1704 {
1705         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1706         retvm_if(nodisplay == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1707         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1708
1709         if (info->app_info == NULL || info->app_info->nodisplay == NULL)
1710                 return PMINFO_R_ERROR;
1711
1712         *nodisplay = _get_bool_value(info->app_info->nodisplay);
1713
1714         return PMINFO_R_OK;
1715 }
1716
1717 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h handle, bool *multiple)
1718 {
1719         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1720         retvm_if(multiple == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1721         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1722
1723         if (info->app_info == NULL || info->app_info->multiple == NULL)
1724                 return PMINFO_R_ERROR;
1725
1726         *multiple = _get_bool_value(info->app_info->multiple);
1727
1728         return PMINFO_R_OK;
1729 }
1730
1731 API int pkgmgrinfo_appinfo_is_indicator_display_allowed(pkgmgrinfo_appinfo_h handle, bool *indicator_disp)
1732 {
1733         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1734         retvm_if(indicator_disp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1735         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1736
1737         if (info->app_info == NULL || info->app_info->indicatordisplay == NULL)
1738                 return PMINFO_R_ERROR;
1739
1740         *indicator_disp = _get_bool_value(info->app_info->indicatordisplay);
1741
1742         return PMINFO_R_OK;
1743 }
1744
1745 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
1746 {
1747         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1748         retvm_if(taskmanage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1749         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1750
1751         if (info->app_info == NULL || info->app_info->taskmanage == NULL)
1752                 return PMINFO_R_ERROR;
1753
1754         *taskmanage = _get_bool_value(info->app_info->taskmanage);
1755
1756         return PMINFO_R_OK;
1757 }
1758
1759 API int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h  handle, bool *enabled)
1760 {
1761         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1762         retvm_if(enabled == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1763         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1764
1765         if (info->app_info == NULL || info->app_info->enabled == NULL)
1766                 return PMINFO_R_ERROR;
1767
1768         *enabled = _get_bool_value(info->app_info->enabled);
1769
1770         return PMINFO_R_OK;
1771 }
1772
1773 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
1774 {
1775         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1776         retvm_if(onboot == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1777         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1778
1779         if (info->app_info == NULL || info->app_info->onboot == NULL)
1780                 return PMINFO_R_ERROR;
1781
1782         *onboot = _get_bool_value(info->app_info->onboot);
1783
1784         return PMINFO_R_OK;
1785 }
1786
1787 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
1788 {
1789         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1790         retvm_if(autorestart == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1791         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1792
1793         if (info->app_info == NULL || info->app_info->autorestart == NULL)
1794                 return PMINFO_R_ERROR;
1795
1796         *autorestart = _get_bool_value(info->app_info->autorestart);
1797
1798         return PMINFO_R_OK;
1799 }
1800
1801 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
1802 {
1803         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1804         retvm_if(mainapp == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1805         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1806
1807         if (info->app_info == NULL || info->app_info->mainapp == NULL)
1808                 return PMINFO_R_ERROR;
1809
1810         *mainapp = _get_bool_value(info->app_info->mainapp);
1811
1812         return PMINFO_R_OK;
1813 }
1814
1815 API int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, bool *preload)
1816 {
1817         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1818         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1819         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1820
1821         if (info->app_info == NULL || info->app_info->preload == NULL)
1822                 return PMINFO_R_ERROR;
1823
1824         *preload = _get_bool_value(info->app_info->preload);
1825
1826         return PMINFO_R_OK;
1827 }
1828
1829 API int pkgmgrinfo_appinfo_is_submode(pkgmgrinfo_appinfo_h handle, bool *submode)
1830 {
1831         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
1832         retvm_if(submode == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1833         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1834
1835         if (info->app_info == NULL || info->app_info->submode == NULL)
1836                 return PMINFO_R_ERROR;
1837
1838         *submode = _get_bool_value(info->app_info->submode);
1839
1840         return PMINFO_R_OK;
1841 }
1842
1843 API int pkgmgrinfo_appinfo_is_process_pool(pkgmgrinfo_appinfo_h handle, bool *process_pool)
1844 {
1845         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1846
1847         if (handle == NULL || process_pool == NULL) {
1848                 LOGE("invalid parameter");
1849                 return PMINFO_R_EINVAL;
1850         }
1851
1852         if (info->app_info == NULL)
1853                 return PMINFO_R_ERROR;
1854
1855         *process_pool = _get_bool_value(info->app_info->process_pool);
1856
1857         return PMINFO_R_OK;
1858 }
1859
1860 API int pkgmgrinfo_appinfo_is_category_exist(pkgmgrinfo_appinfo_h handle, const char *category, bool *exist)
1861 {
1862         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1863         retvm_if(category == NULL, PMINFO_R_EINVAL, "category is NULL");
1864         retvm_if(exist == NULL, PMINFO_R_EINVAL, "exist is NULL");
1865
1866         const char *val;
1867         GList *tmp;
1868         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1869
1870         if (info->app_info == NULL)
1871                 return PMINFO_R_ERROR;
1872
1873         *exist = 0;
1874         for (tmp = info->app_info->category; tmp; tmp = tmp->next) {
1875                 val = (const char *)tmp->data;
1876                 if (val == NULL)
1877                         continue;
1878                 if (strcasecmp(val, category) == 0) {
1879                         *exist = 1;
1880                         break;
1881                 }
1882         }
1883
1884         return PMINFO_R_OK;
1885 }
1886
1887 API int pkgmgrinfo_appinfo_is_ui_gadget(pkgmgrinfo_appinfo_h handle,
1888                 bool *ui_gadget)
1889 {
1890         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1891
1892         if (info == NULL || info->app_info == NULL || ui_gadget == NULL) {
1893                 _LOGE("invalid parameter");
1894                 return PMINFO_R_EINVAL;
1895         }
1896
1897         *ui_gadget = _get_bool_value(info->app_info->ui_gadget);
1898
1899         return PMINFO_R_OK;
1900 }
1901
1902 API int pkgmgrinfo_appinfo_is_support_disable(pkgmgrinfo_appinfo_h handle,
1903                 bool *support_disable)
1904 {
1905         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1906
1907         if (info == NULL || info->app_info == NULL || support_disable == NULL) {
1908                 _LOGE("invalid parameter");
1909                 return PMINFO_R_EINVAL;
1910         }
1911
1912         *support_disable = _get_bool_value(info->app_info->support_disable);
1913
1914         return PMINFO_R_OK;
1915 }
1916
1917 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle)
1918 {
1919         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL");
1920         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
1921         __cleanup_appinfo(info);
1922         return PMINFO_R_OK;
1923 }
1924
1925 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
1926 {
1927         return (pkgmgrinfo_pkginfo_filter_create(handle));
1928 }
1929
1930 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
1931 {
1932         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
1933 }
1934
1935 static gint __compare_func(gconstpointer data1, gconstpointer data2)
1936 {
1937         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
1938         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
1939         if (node1->prop == node2->prop)
1940                 return 0;
1941         else if (node1->prop > node2->prop)
1942                 return 1;
1943         else
1944                 return -1;
1945 }
1946
1947 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
1948                                 const char *property, const int value)
1949 {
1950         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1951         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1952         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
1953         char *val = NULL;
1954         GSList *link = NULL;
1955         int prop = -1;
1956         prop = _pminfo_appinfo_convert_to_prop_int(property);
1957         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
1958                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
1959                 _LOGE("Invalid Integer Property\n");
1960                 return PMINFO_R_EINVAL;
1961         }
1962         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
1963         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
1964         if (node == NULL) {
1965                 _LOGE("Out of Memory!!!\n");
1966                 return PMINFO_R_ERROR;
1967         }
1968         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
1969         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
1970         if (val == NULL) {
1971                 _LOGE("Out of Memory\n");
1972                 free(node);
1973                 node = NULL;
1974                 return PMINFO_R_ERROR;
1975         }
1976         node->prop = prop;
1977         node->value = val;
1978         /*If API is called multiple times for same property, we should override the previous values.
1979         Last value set will be used for filtering.*/
1980         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1981         if (link)
1982                 filter->list = g_slist_delete_link(filter->list, link);
1983         filter->list = g_slist_append(filter->list, (gpointer)node);
1984         return PMINFO_R_OK;
1985
1986 }
1987
1988 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
1989                                 const char *property, const bool value)
1990 {
1991         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1992         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1993         char *val = NULL;
1994         GSList *link = NULL;
1995         int prop = -1;
1996         prop = _pminfo_appinfo_convert_to_prop_bool(property);
1997         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
1998                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
1999                 _LOGE("Invalid Boolean Property\n");
2000                 return PMINFO_R_EINVAL;
2001         }
2002         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2003         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2004         if (node == NULL) {
2005                 _LOGE("Out of Memory!!!\n");
2006                 return PMINFO_R_ERROR;
2007         }
2008         if (value)
2009                 val = strndup("('true','True')", 15);
2010         else
2011                 val = strndup("('false','False')", 17);
2012         if (val == NULL) {
2013                 _LOGE("Out of Memory\n");
2014                 free(node);
2015                 node = NULL;
2016                 return PMINFO_R_ERROR;
2017         }
2018         node->prop = prop;
2019         node->value = val;
2020         /*If API is called multiple times for same property, we should override the previous values.
2021         Last value set will be used for filtering.*/
2022         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2023         if (link)
2024                 filter->list = g_slist_delete_link(filter->list, link);
2025         filter->list = g_slist_append(filter->list, (gpointer)node);
2026         return PMINFO_R_OK;
2027
2028 }
2029
2030 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
2031                                 const char *property, const char *value)
2032 {
2033         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2034         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2035         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
2036         char *val = NULL;
2037         pkgmgrinfo_node_x *ptr = NULL;
2038         char prev[PKG_STRING_LEN_MAX] = {'\0'};
2039         char temp[PKG_STRING_LEN_MAX] = {'\0'};
2040         GSList *link = NULL;
2041         int prop = -1;
2042         prop = _pminfo_appinfo_convert_to_prop_str(property);
2043         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
2044                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
2045                 _LOGE("Invalid String Property\n");
2046                 return PMINFO_R_EINVAL;
2047         }
2048         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
2049         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
2050         if (node == NULL) {
2051                 _LOGE("Out of Memory!!!\n");
2052                 return PMINFO_R_ERROR;
2053         }
2054         node->prop = prop;
2055         switch (prop) {
2056         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
2057                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
2058                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
2059                 else
2060                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
2061                 node->value = val;
2062                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2063                 if (link)
2064                         filter->list = g_slist_delete_link(filter->list, link);
2065                 filter->list = g_slist_append(filter->list, (gpointer)node);
2066                 break;
2067         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
2068         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
2069         case E_PMINFO_APPINFO_PROP_APP_URI:
2070         case E_PMINFO_APPINFO_PROP_APP_MIME:
2071                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
2072                 if (val == NULL) {
2073                         _LOGE("Out of Memory\n");
2074                         free(node);
2075                         node = NULL;
2076                         return PMINFO_R_ERROR;
2077                 }
2078                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2079                 if (link) {
2080                         ptr = (pkgmgrinfo_node_x *)link->data;
2081                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
2082                         _LOGE("Previous value is %s\n", prev);
2083                         filter->list = g_slist_delete_link(filter->list, link);
2084                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
2085                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
2086                         _LOGE("New value is %s\n", val);
2087                         node->value = val;
2088                         filter->list = g_slist_append(filter->list, (gpointer)node);
2089                         memset(temp, '\0', PKG_STRING_LEN_MAX);
2090                 } else {
2091                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
2092                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
2093                         _LOGE("First value is %s\n", val);
2094                         node->value = val;
2095                         filter->list = g_slist_append(filter->list, (gpointer)node);
2096                         memset(temp, '\0', PKG_STRING_LEN_MAX);
2097                 }
2098                 break;
2099         default:
2100                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
2101                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
2102                 if (link)
2103                         filter->list = g_slist_delete_link(filter->list, link);
2104                 filter->list = g_slist_append(filter->list, (gpointer)node);
2105                 break;
2106         }
2107         return PMINFO_R_OK;
2108 }
2109
2110 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
2111 {
2112         int ret;
2113         GList *list = NULL;
2114
2115         if (handle == NULL || count == NULL) {
2116                 _LOGE("invalid parameter");
2117                 return PMINFO_R_EINVAL;
2118         }
2119
2120         ret = _appinfo_get_filtered_list(handle, uid, &list);
2121         if (ret != PMINFO_R_OK)
2122                 return PMINFO_R_ERROR;
2123
2124         *count = g_list_length(list);
2125
2126         g_list_free_full(list, free);
2127
2128         return PMINFO_R_OK;
2129 }
2130
2131 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
2132 {
2133         return pkgmgrinfo_appinfo_usr_filter_count(handle, count, GLOBAL_USER);
2134 }
2135
2136 API int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(
2137                 pkgmgrinfo_appinfo_filter_h handle,
2138                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2139 {
2140         if (handle == NULL || app_cb == NULL) {
2141                 LOGE("invalid parameter");
2142                 return PMINFO_R_EINVAL;
2143         }
2144
2145         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2146                         user_data);
2147 }
2148
2149 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
2150                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
2151 {
2152         return pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_cb, user_data, GLOBAL_USER);
2153 }
2154
2155 API int pkgmgrinfo_appinfo_metadata_filter_create(pkgmgrinfo_appinfo_metadata_filter_h *handle)
2156 {
2157         return (pkgmgrinfo_pkginfo_filter_create(handle));
2158 }
2159
2160 API int pkgmgrinfo_appinfo_metadata_filter_destroy(pkgmgrinfo_appinfo_metadata_filter_h handle)
2161 {
2162         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
2163 }
2164
2165 API int pkgmgrinfo_appinfo_metadata_filter_add(
2166                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2167                 const char *key, const char *value)
2168 {
2169         int ret;
2170
2171         ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2172                         PMINFO_APPINFO_PROP_APP_METADATA_KEY, key);
2173         if (ret != PMINFO_R_OK)
2174                 return ret;
2175
2176         /* value can be NULL.
2177          * In that case all apps with specified key should be displayed
2178          */
2179         if (value) {
2180                 ret = pkgmgrinfo_appinfo_filter_add_string(handle,
2181                                 PMINFO_APPINFO_PROP_APP_METADATA_VALUE, value);
2182                 if (ret != PMINFO_R_OK)
2183                         return ret;
2184         }
2185
2186         return PMINFO_R_OK;
2187 }
2188
2189 API int pkgmgrinfo_appinfo_usr_metadata_filter_foreach(
2190                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2191                 pkgmgrinfo_app_list_cb app_cb, void *user_data, uid_t uid)
2192 {
2193         if (handle == NULL || app_cb == NULL) {
2194                 LOGE("invalid parameter");
2195                 return PMINFO_R_EINVAL;
2196         }
2197
2198         return _appinfo_get_filtered_foreach_appinfo(uid, handle, app_cb,
2199                         user_data);
2200 }
2201
2202 API int pkgmgrinfo_appinfo_metadata_filter_foreach(
2203                 pkgmgrinfo_appinfo_metadata_filter_h handle,
2204                 pkgmgrinfo_app_list_cb app_cb, void *user_data)
2205 {
2206         return pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb,
2207                         user_data, GLOBAL_USER);
2208 }
2209
2210 API int pkgmgrinfo_appinfo_is_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool *status)
2211 {
2212         const char *val;
2213         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
2214
2215         retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
2216         retvm_if(status == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
2217
2218         val = info->app_info->guestmode_visibility;
2219         *status = _get_bool_value(val);
2220         return PMINFO_R_OK;
2221 }