Add new APIs related to application component
[platform/core/api/app-manager.git] / src / app_info.c
1 /*
2  * Copyright (c) 2011 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23
24 #include <pkgmgr-info.h>
25 #include <package-manager.h>
26 #include <dlog.h>
27 #include <cynara-client.h>
28
29 #include "app_info.h"
30 #include "app_manager.h"
31 #include "app_manager_internal.h"
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36
37 #define LOG_TAG "TIZEN_N_APP_MANAGER"
38
39 #define SMACK_LABEL_LEN 255
40
41 struct app_info_s {
42         char *app_id;
43         pkgmgrinfo_appinfo_h pkg_app_info;
44 };
45
46 struct app_info_filter_s {
47         pkgmgrinfo_appinfo_filter_h pkg_app_info_filter;
48 };
49
50 struct app_info_metadata_filter_s {
51         pkgmgrinfo_appinfo_metadata_filter_h pkg_app_info_metadata_filter;
52 };
53
54 typedef struct _foreach_context_ {
55         app_manager_app_info_cb callback;
56         void *user_data;
57 } foreach_context_s;
58
59 typedef struct _foreach_metada_context_ {
60         app_info_metadata_cb callback;
61         void *user_data;
62 } foreach_metadata_context_s;
63
64 typedef struct _foreach_category_ {
65         app_info_category_cb callback;
66         void *user_data;
67 } foreach_category_context_s;
68
69 static int app_info_convert_str_property(const char *property, char **converted_property)
70 {
71         if (property == NULL)
72                 return -1;
73
74         if (strcmp(property, PACKAGE_INFO_PROP_APP_ID) == 0)
75                 *converted_property = PMINFO_APPINFO_PROP_APP_ID;
76         else if (strcmp(property, PACKAGE_INFO_PROP_APP_TYPE) == 0)
77                 *converted_property = PMINFO_APPINFO_PROP_APP_TYPE;
78         else if (strcmp(property, PACKAGE_INFO_PROP_APP_CATEGORY) == 0)
79                 *converted_property = PMINFO_APPINFO_PROP_APP_CATEGORY;
80         else if (strcmp(property, PACKAGE_INFO_PROP_APP_INSTALLED_STORAGE) == 0)
81                 *converted_property = PMINFO_APPINFO_PROP_APP_INSTALLED_STORAGE;
82         else if (strcmp(property, PACKAGE_INFO_PROP_APP_COMPONENT_TYPE) == 0)
83                 *converted_property = PMINFO_APPINFO_PROP_APP_COMPONENT;
84         else
85                 return -1;
86
87         return 0;
88 }
89
90 static int app_info_convert_bool_property(const char *property, char **converted_property)
91 {
92         if (property == NULL)
93                 return -1;
94
95         if (strcmp(property, PACKAGE_INFO_PROP_APP_NODISPLAY) == 0)
96                 *converted_property = PMINFO_APPINFO_PROP_APP_NODISPLAY;
97         else if (strcmp(property, PACKAGE_INFO_PROP_APP_TASKMANAGE) == 0)
98                 *converted_property = PMINFO_APPINFO_PROP_APP_TASKMANAGE;
99         else if (strcmp(property, PACKAGE_INFO_PROP_APP_DISABLED) == 0)
100                 *converted_property = PMINFO_APPINFO_PROP_APP_DISABLE;
101         else
102                 return -1;
103
104         return 0;
105 }
106
107 static int app_info_convert_app_component(pkgmgrinfo_app_component component, app_info_app_component_type_e *converted_component)
108 {
109         if (component == PMINFO_UI_APP)
110                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_UI_APP;
111         else if (component == PMINFO_SVC_APP)
112                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_SERVICE_APP;
113         else if (component == PMINFO_WIDGET_APP)
114                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_WIDGET_APP;
115         else if (component == PMINFO_WATCH_APP)
116                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_WATCH_APP;
117         else
118                 return -1;
119
120         return 0;
121 }
122
123 static int app_info_foreach_app_filter_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
124 {
125         int retval = 0;
126         char *appid = NULL;
127         app_info_h info = NULL;
128         bool iteration_next = true;
129
130         info = calloc(1, sizeof(struct app_info_s));
131         if (info == NULL)
132                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
133
134         foreach_context_s *foreach_context = user_data;
135         if (handle == NULL || foreach_context == NULL) {
136                 free(info);
137                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
138         }
139
140         retval = pkgmgrinfo_appinfo_get_appid(handle, &appid);
141         if (retval < 0) {
142                 free(info);
143                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
144         }
145
146         info->app_id = strdup(appid);
147         if (info->app_id == NULL) {
148                 if (info) {
149                         free(info);
150                         info = NULL;
151                 }
152                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
153         }
154         info->pkg_app_info = handle;
155
156         iteration_next = foreach_context->callback(info, foreach_context->user_data);
157
158         if (info->app_id) {
159                 free(info->app_id);
160                 info->app_id = NULL;
161         }
162
163         if (info) {
164                 free(info);
165                 info = NULL;
166         }
167
168         if (iteration_next == true)
169                 return PMINFO_R_OK;
170         else
171                 return PMINFO_R_ERROR;
172 }
173
174 static int app_info_foreach_app_metadata_cb(const char *metadata_key, const char *metadata_value, void *user_data)
175 {
176         foreach_metadata_context_s *foreach_context = user_data;
177         bool iteration_next = true;
178
179         if (metadata_value == NULL || foreach_context == NULL)
180                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
181
182         iteration_next = foreach_context->callback(metadata_key, metadata_value, foreach_context->user_data);
183         if (iteration_next == true)
184                 return PMINFO_R_OK;
185         else
186                 return PMINFO_R_ERROR;
187 }
188
189 static int app_info_foreach_category_cb(const char *category_name, void *user_data)
190 {
191         foreach_category_context_s *foreach_category = user_data;
192         bool iteration_next = true;
193
194         if (category_name == NULL || foreach_category == NULL)
195                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
196
197         iteration_next = foreach_category->callback(category_name, foreach_category->user_data);
198         if (iteration_next == true)
199                 return PMINFO_R_OK;
200         else
201                 return PMINFO_R_ERROR;
202 }
203
204 static int app_info_foreach_app_info_cb(pkgmgrinfo_appinfo_h handle, void *cb_data)
205 {
206         foreach_context_s *foreach_context = cb_data;
207         app_info_h app_info = NULL;
208         char *appid = NULL;
209         int ret = 0;
210         bool iteration_next = true;
211
212         if (handle == NULL || foreach_context == NULL) {
213                 app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
214                 return PMINFO_R_ERROR;
215         }
216
217         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
218         if (ret != PMINFO_R_OK) {
219                 app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
220                 return PMINFO_R_ERROR;
221         }
222
223         app_info = calloc(1, sizeof(struct app_info_s));
224         if (app_info == NULL) {
225                 app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
226                 return PMINFO_R_ERROR;
227         }
228
229         app_info->app_id = strdup(appid);
230         app_info->pkg_app_info = handle;
231         iteration_next = foreach_context->callback(app_info, foreach_context->user_data);
232
233         free(app_info->app_id);
234         free(app_info);
235
236         if (iteration_next == true)
237                 return PMINFO_R_OK;
238         else
239                 return PMINFO_R_ERROR;
240 }
241
242 int app_info_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
243 {
244         foreach_context_s foreach_context = {
245                 .callback = callback,
246                 .user_data = user_data,
247         };
248
249         if (callback == NULL)
250                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
251
252         pkgmgrinfo_appinfo_get_usr_installed_list(app_info_foreach_app_info_cb, getuid(), &foreach_context);
253
254         return APP_MANAGER_ERROR_NONE;
255 }
256
257 static int _check_privilege(char *privilege)
258 {
259         cynara *p_cynara;
260         int fd;
261         int ret;
262
263         char client[SMACK_LABEL_LEN + 1] = "";
264         char uid[10] = {0,};
265         char *client_session = "";
266
267         if (privilege == NULL) {
268                 LOGE("invalid parameter");
269                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
270         }
271
272         ret = cynara_initialize(&p_cynara, NULL);
273         if (ret != CYNARA_API_SUCCESS) {
274                 LOGE("cynara_initialize [%d] failed!", ret);
275                 return APP_MANAGER_ERROR_IO_ERROR;
276         }
277
278         fd = open("/proc/self/attr/current", O_RDONLY);
279         if (fd < 0) {
280                 LOGE("open [%d] failed!", errno);
281                 ret = APP_MANAGER_ERROR_IO_ERROR;
282                 goto out;
283         }
284
285         ret = read(fd, client, SMACK_LABEL_LEN);
286         if (ret < 0) {
287                 LOGE("read [%d] failed!", errno);
288                 close(fd);
289                 ret = APP_MANAGER_ERROR_IO_ERROR;
290                 goto out;
291         }
292         close(fd);
293         snprintf(uid, 10, "%d", getuid());
294
295         ret = cynara_check(p_cynara, client, client_session, uid, privilege);
296         if (ret != CYNARA_API_ACCESS_ALLOWED) {
297                 LOGE("cynara access check [%d] failed!", ret);
298
299                 if (ret == CYNARA_API_ACCESS_DENIED)
300                         ret = APP_MANAGER_ERROR_PERMISSION_DENIED;
301                 else
302                         ret = APP_MANAGER_ERROR_IO_ERROR;
303
304                 goto out;
305         }
306         ret = APP_MANAGER_ERROR_NONE;
307
308 out:
309         if (p_cynara)
310                 cynara_finish(p_cynara);
311
312         return ret;
313 }
314
315 API int app_info_create(const char *app_id, app_info_h *app_info)
316 {
317         pkgmgrinfo_pkginfo_h pkginfo = NULL;
318         pkgmgrinfo_appinfo_h appinfo = NULL;
319         app_info_h info = NULL;
320         int retval = 0;
321         char *main_appid = NULL;
322
323         if (app_id == NULL || app_info == NULL)
324                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
325
326         info = calloc(1, sizeof(struct app_info_s));
327         if (info == NULL)
328                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
329
330         retval = pkgmgrinfo_appinfo_get_usr_appinfo(app_id, getuid(), &appinfo);
331         if (!retval) {
332                 info->app_id = strdup(app_id);
333                 info->pkg_app_info = appinfo;
334                 *app_info = info;
335                 return APP_MANAGER_ERROR_NONE;
336         }
337
338         retval = pkgmgrinfo_pkginfo_get_usr_pkginfo(app_id, getuid(), &pkginfo);
339         if (retval < 0) {
340                 free(info);
341                 return app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
342         }
343
344         retval = pkgmgrinfo_pkginfo_get_mainappid(pkginfo, &main_appid);
345         if (retval < 0) {
346                 free(info);
347                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
348                 return app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
349         }
350         if (pkgmgrinfo_appinfo_get_usr_appinfo(main_appid, getuid(), &appinfo)) {
351                 free(info);
352                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
353                 return app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
354         }
355
356         info->app_id = strdup(main_appid);
357         info->pkg_app_info = appinfo;
358         *app_info = info;
359
360         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
361
362         return APP_MANAGER_ERROR_NONE;
363 }
364
365 API int app_info_destroy(app_info_h app_info)
366 {
367         if (app_info == NULL)
368                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
369
370         if (app_info->app_id) {
371                 free(app_info->app_id);
372                 app_info->app_id = NULL;
373         }
374
375         pkgmgrinfo_appinfo_destroy_appinfo(app_info->pkg_app_info);
376         free(app_info);
377         return APP_MANAGER_ERROR_NONE;
378 }
379
380 API int app_info_get_app_id(app_info_h app_info, char **app_id)
381 {
382         char *app_id_dup = NULL;
383
384         if (app_info == NULL || app_id == NULL)
385                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
386
387         if (app_info->app_id == NULL)
388                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
389
390         app_id_dup = strdup(app_info->app_id);
391         if (app_id_dup == NULL)
392                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
393
394         *app_id = app_id_dup;
395
396         return APP_MANAGER_ERROR_NONE;
397 }
398
399 API int app_info_get_exec(app_info_h app_info, char **exec)
400 {
401         char *val = NULL;
402         char *app_exec_dup = NULL;
403         int ret = -1;
404
405         if (app_info == NULL || exec == NULL)
406                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
407
408         ret = pkgmgrinfo_appinfo_get_exec(app_info->pkg_app_info, &val);
409         if (ret != PMINFO_R_OK || val == NULL)
410                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
411
412         app_exec_dup = strdup(val);
413         if (app_exec_dup == NULL)
414                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
415
416         *exec = app_exec_dup;
417
418         return APP_MANAGER_ERROR_NONE;
419 }
420
421 API int app_info_get_label(app_info_h app_info, char **label)
422 {
423         char *val = NULL;
424         char *app_label_dup = NULL;
425         int ret = 0;
426
427         if (app_info == NULL || label == NULL)
428                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
429
430         ret = pkgmgrinfo_appinfo_get_label(app_info->pkg_app_info, &val);
431         if (ret < 0 || val == NULL)
432                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
433
434         app_label_dup = strdup(val);
435         if (app_label_dup == NULL)
436                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
437
438         *label = app_label_dup;
439
440         return APP_MANAGER_ERROR_NONE;
441 }
442
443 API int app_info_get_localed_label(const char *app_id, const char *locale, char **label)
444 {
445         char *val = NULL;
446         char *app_label_dup = NULL;
447
448         if (app_id == NULL || locale == NULL || label == NULL)
449                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
450
451         if (pkgmgrinfo_appinfo_usr_get_localed_label(app_id, locale, getuid(), &val))
452                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
453
454         app_label_dup = strdup(val);
455         if (app_label_dup == NULL) {
456                 if (val) {
457                         free(val);
458                         val = NULL;
459                 }
460                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
461         }
462
463         *label = app_label_dup;
464         free(val);
465
466         return APP_MANAGER_ERROR_NONE;
467 }
468
469 API int app_info_get_icon(app_info_h app_info, char **path)
470 {
471         char *val = NULL;
472         char *app_icon_dup = NULL;
473         int ret = -1;
474
475         if (app_info == NULL || path == NULL)
476                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
477
478         ret = pkgmgrinfo_appinfo_get_icon(app_info->pkg_app_info, &val);
479         if (ret != PMINFO_R_OK || val == NULL)
480                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
481
482         app_icon_dup = strdup(val);
483         if (app_icon_dup == NULL)
484                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
485
486         *path = app_icon_dup;
487
488         return APP_MANAGER_ERROR_NONE;
489 }
490
491 API int app_info_get_package(app_info_h app_info, char **package)
492 {
493         char *val = NULL;
494         char *app_package_dup = NULL;
495         int ret = 0;
496
497         if (app_info == NULL || package == NULL)
498                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
499
500         ret = pkgmgrinfo_appinfo_get_pkgname(app_info->pkg_app_info, &val);
501         if (ret < 0)
502                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
503         if (val == NULL)
504                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
505
506         app_package_dup = strdup(val);
507         if (app_package_dup == NULL)
508                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
509
510         *package = app_package_dup;
511
512         return APP_MANAGER_ERROR_NONE;
513 }
514
515 API int app_info_get_type(app_info_h app_info, char **type)
516 {
517         char *val = NULL;
518         char *app_type_dup = NULL;
519         int ret = 0;
520
521         if (app_info == NULL || type == NULL)
522                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
523
524         ret = pkgmgrinfo_appinfo_get_apptype(app_info->pkg_app_info, &val);
525         if (ret < 0)
526                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
527         if (val == NULL)
528                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
529
530         app_type_dup = strdup(val);
531         if (app_type_dup == NULL)
532                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
533
534         *type = app_type_dup;
535
536         return APP_MANAGER_ERROR_NONE;
537 }
538
539 API int app_info_get_app_component_type(app_info_h app_info, app_info_app_component_type_e *type)
540 {
541         pkgmgrinfo_app_component comp_val;
542         app_info_app_component_type_e converted_comp_val;
543         int ret = 0;
544
545         if (app_info == NULL || type == NULL)
546                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
547
548         ret = pkgmgrinfo_appinfo_get_component(app_info->pkg_app_info, &comp_val);
549         if (ret < 0)
550                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
551
552         ret = app_info_convert_app_component(comp_val, &converted_comp_val);
553         if (ret < 0)
554                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
555
556         *type = converted_comp_val;
557
558         return APP_MANAGER_ERROR_NONE;
559 }
560
561 API int app_info_foreach_metadata(app_info_h app_info, app_info_metadata_cb callback, void *user_data)
562 {
563         int retval = 0;
564
565         if (app_info == NULL || callback == NULL)
566                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
567
568         foreach_metadata_context_s foreach_context = {
569                 .callback = callback,
570                 .user_data = user_data,
571         };
572
573         retval = pkgmgrinfo_appinfo_foreach_metadata(app_info->pkg_app_info, app_info_foreach_app_metadata_cb, &foreach_context);
574         if (retval < 0)
575                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
576
577         return APP_MANAGER_ERROR_NONE;
578 }
579
580 API int app_info_is_nodisplay(app_info_h app_info, bool *nodisplay)
581 {
582         bool val;
583
584         if (app_info == NULL || nodisplay == NULL)
585                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
586
587         if (pkgmgrinfo_appinfo_is_nodisplay(app_info->pkg_app_info, &val) < 0)
588                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
589
590         *nodisplay = val;
591         return APP_MANAGER_ERROR_NONE;
592 }
593
594 API int app_info_is_enabled(app_info_h app_info, bool *enabled)
595 {
596         bool val;
597
598         if (app_info == NULL || enabled == NULL)
599                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
600
601         if (pkgmgrinfo_appinfo_is_enabled(app_info->pkg_app_info, &val) < 0)
602                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
603
604         *enabled = val;
605         return APP_MANAGER_ERROR_NONE;
606
607 }
608
609 API int app_info_is_equal(app_info_h lhs, app_info_h rhs, bool *equal)
610 {
611         if (lhs == NULL || rhs == NULL || equal == NULL)
612                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
613
614         if (!strcmp(lhs->app_id, rhs->app_id))
615                 *equal = true;
616         else
617                 *equal = false;
618
619         return APP_MANAGER_ERROR_NONE;
620 }
621
622 API int app_info_is_onboot(app_info_h app_info, bool *onboot)
623 {
624         bool val;
625
626         if (app_info == NULL || onboot == NULL)
627                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
628
629         if (pkgmgrinfo_appinfo_is_onboot(app_info->pkg_app_info, &val) < 0)
630                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
631
632         *onboot = val;
633         return APP_MANAGER_ERROR_NONE;
634 }
635
636 API int app_info_is_preload(app_info_h app_info, bool *preload)
637 {
638         bool val;
639
640         if (app_info == NULL || preload == NULL)
641                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
642
643         if (pkgmgrinfo_appinfo_is_preload(app_info->pkg_app_info, &val) < 0)
644                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
645
646         *preload = val;
647         return APP_MANAGER_ERROR_NONE;
648 }
649
650 API int app_info_clone(app_info_h *clone, app_info_h app_info)
651 {
652         app_info_h info;
653
654         if (clone == NULL || app_info == NULL)
655                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
656
657         info = calloc(1, sizeof(struct app_info_s));
658         if (info == NULL)
659                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
660
661         info->app_id = strdup(app_info->app_id);
662         if (info->app_id == NULL) {
663                 free(info);
664                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
665         }
666
667         if (pkgmgrinfo_appinfo_clone_appinfo(app_info->pkg_app_info, &(info->pkg_app_info)) < 0) {
668                 free(info->app_id);
669                 free(info);
670                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
671         }
672
673         *clone = info;
674
675         return APP_MANAGER_ERROR_NONE;
676 }
677
678 API int app_info_foreach_category(app_info_h app_info, app_info_category_cb callback, void *user_data)
679 {
680         int retval;
681         if (app_info == NULL || callback == NULL)
682                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
683
684         retval = _check_privilege(PRIVILEGE_PACKAGE_MANAGER_ADMIN);
685         if (retval != APP_MANAGER_ERROR_NONE)
686                 return app_manager_error(APP_MANAGER_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
687
688         foreach_category_context_s foreach_category = {
689                 .callback = callback,
690                 .user_data = user_data,
691         };
692
693         retval = pkgmgrinfo_appinfo_foreach_category(app_info->pkg_app_info, app_info_foreach_category_cb, &foreach_category);
694         if (retval != PMINFO_R_OK)
695                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
696
697         return APP_MANAGER_ERROR_NONE;
698 }
699
700 API int app_info_filter_create(app_info_filter_h *handle)
701 {
702         int retval = 0;
703         app_info_filter_h filter_created = NULL;
704         pkgmgrinfo_appinfo_filter_h filter_h = NULL;
705
706         if (handle == NULL)
707                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
708
709         retval = pkgmgrinfo_appinfo_filter_create(&filter_h);
710         if (retval < 0)
711                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
712
713         filter_created = calloc(1, sizeof(struct app_info_filter_s));
714         if (filter_created == NULL) {
715                 free(filter_h);
716                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
717         }
718
719         filter_created->pkg_app_info_filter = filter_h;
720
721         *handle = filter_created;
722
723         return APP_MANAGER_ERROR_NONE;
724 }
725
726 API int app_info_filter_destroy(app_info_filter_h handle)
727 {
728         int retval = 0;
729
730         if (handle == NULL)
731                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
732
733         retval = pkgmgrinfo_appinfo_filter_destroy(handle->pkg_app_info_filter);
734         if (retval < 0)
735                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
736
737         free(handle);
738         return APP_MANAGER_ERROR_NONE;
739 }
740
741 API int app_info_filter_add_bool(app_info_filter_h handle, const char *property, const bool value)
742 {
743         int retval = 0;
744         char *converted_property = NULL;
745
746         if ((handle == NULL) || (property == NULL))
747                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
748
749         retval = app_info_convert_bool_property(property, &converted_property);
750         if (retval < 0)
751                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
752
753         retval = pkgmgrinfo_appinfo_filter_add_bool(handle->pkg_app_info_filter, converted_property, value);
754         if (retval < 0)
755                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
756
757         return APP_MANAGER_ERROR_NONE;
758 }
759
760 API int app_info_filter_add_string(app_info_filter_h handle, const char *property, const char *value)
761 {
762         int retval = 0;
763         char *converted_property = NULL;
764
765         if ((handle == NULL) || (property == NULL))
766                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
767
768         retval = app_info_convert_str_property(property, &converted_property);
769         if (retval < 0)
770                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
771
772         retval = pkgmgrinfo_appinfo_filter_add_string(handle->pkg_app_info_filter, converted_property, value);
773         if (retval < 0)
774                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
775
776         return APP_MANAGER_ERROR_NONE;
777 }
778
779 API int app_info_filter_count_appinfo(app_info_filter_h handle, int *count)
780 {
781         int retval = 0;
782
783         if ((handle == NULL) || (count == NULL))
784                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
785
786         retval = pkgmgrinfo_appinfo_filter_count(handle->pkg_app_info_filter, count);
787         if (retval < 0)
788                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
789
790         return APP_MANAGER_ERROR_NONE;
791 }
792
793 API int app_info_filter_foreach_appinfo(app_info_filter_h handle, app_info_filter_cb callback, void *user_data)
794 {
795         int retval = 0;
796
797         foreach_context_s foreach_context = {
798                 .callback = callback,
799                 .user_data = user_data,
800         };
801
802         if ((handle == NULL) || (callback == NULL))
803                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
804
805         retval = pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle->pkg_app_info_filter, app_info_foreach_app_filter_cb, &foreach_context, getuid());
806         if (retval < 0)
807                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
808
809         return APP_MANAGER_ERROR_NONE;
810 }
811
812 API int app_info_metadata_filter_create(app_info_metadata_filter_h *handle)
813 {
814         int retval = 0;
815         app_info_metadata_filter_h filter_created = NULL;
816         pkgmgrinfo_appinfo_metadata_filter_h filter_h = NULL;
817
818         if (handle == NULL)
819                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
820
821         filter_created = calloc(1, sizeof(struct app_info_metadata_filter_s));
822         if (filter_created == NULL)
823                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
824
825         retval = pkgmgrinfo_appinfo_metadata_filter_create(&filter_h);
826         if (retval < 0) {
827                 free(filter_created);
828                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
829         }
830
831         filter_created->pkg_app_info_metadata_filter = filter_h;
832
833         *handle = filter_created;
834
835         return APP_MANAGER_ERROR_NONE;
836 }
837
838 API int app_info_metadata_filter_destroy(app_info_metadata_filter_h handle)
839 {
840         int retval = 0;
841
842         if (handle == NULL)
843                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
844
845         retval = pkgmgrinfo_appinfo_metadata_filter_destroy(handle->pkg_app_info_metadata_filter);
846         if (retval < 0)
847                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
848
849         free(handle);
850         return APP_MANAGER_ERROR_NONE;
851 }
852
853 API int app_info_metadata_filter_add(app_info_metadata_filter_h handle, const char *key, const char *value)
854 {
855         int retval = 0;
856
857         if ((handle == NULL) || (key == NULL))
858                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
859
860         retval = pkgmgrinfo_appinfo_metadata_filter_add(handle->pkg_app_info_metadata_filter, key, value);
861         if (retval < 0)
862                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
863
864         return APP_MANAGER_ERROR_NONE;
865 }
866
867 API int app_info_metadata_filter_foreach(app_info_metadata_filter_h handle, app_info_filter_cb callback, void *user_data)
868 {
869         int retval = 0;
870
871         foreach_context_s foreach_context = {
872                 .callback = callback,
873                 .user_data = user_data,
874         };
875
876         if (handle == NULL)
877                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
878
879         if (callback == NULL)
880                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
881
882         retval = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle->pkg_app_info_metadata_filter, app_info_foreach_app_filter_cb, &foreach_context, getuid());
883         if (retval < 0)
884                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
885
886         return APP_MANAGER_ERROR_NONE;
887 }
888