Add Foreach depdency API for Archiveinfo
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_archiveinfo.c
1 /*
2  * Copyright (c) 2017 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 <stdlib.h>
19 #include <string.h>
20
21 #include "pkgmgrinfo_type.h"
22 #include "pkgmgrinfo_private.h"
23 #include "pkgmgrinfo_debug.h"
24
25 API int pkgmgrinfo_archiveinfo_get_archiveinfo(const char *path,
26                 pkgmgrinfo_archiveinfo_h *handle)
27 {
28         char *pkg_type;
29         pkg_plugin_set *plugin_set;
30         package_manager_pkg_detail_info_t *info;
31
32         if (path == NULL || handle == NULL) {
33                 _LOGE("invalid parameter");
34                 return PMINFO_R_EINVAL;
35         }
36
37         pkg_type = __get_type_from_path(path);
38         if (pkg_type == NULL) {
39                 _LOGE("cannot get pkg type");
40                 return PMINFO_R_ERROR;
41         }
42
43         plugin_set = __load_library(pkg_type);
44         if (plugin_set == NULL) {
45                 _LOGE("failed to load library for %s", pkg_type);
46                 free(pkg_type);
47                 return PMINFO_R_ERROR;
48         }
49
50         info = calloc(1, sizeof(package_manager_pkg_detail_info_t));
51         if (info == NULL) {
52                 _LOGE("out of memory");
53                 __unload_library(pkg_type);
54                 free(pkg_type);
55                 return PMINFO_R_ERROR;
56         }
57
58         if (plugin_set->get_pkg_detail_info_from_package(path, info)) {
59                 _LOGE("failed to get archive info of %s", path);
60                 free(info);
61                 __unload_library(pkg_type);
62                 free(pkg_type);
63                 return PMINFO_R_ERROR;
64         }
65
66         *handle = info;
67
68         return PMINFO_R_OK;
69 }
70
71 API int pkgmgrinfo_archiveinfo_destroy_archiveinfo(
72                 pkgmgrinfo_archiveinfo_h handle)
73 {
74         package_manager_pkg_detail_info_t *info =
75                 (package_manager_pkg_detail_info_t *)handle;
76
77         if (info == NULL) {
78                 _LOGE("invalid parameter");
79                 return PMINFO_R_EINVAL;
80         }
81
82         free(info->icon_buf);
83         g_list_free_full(info->privilege_list, free);
84         g_list_free_full(info->dependency_list, free);
85         free(info);
86
87         return PMINFO_R_OK;
88 }
89
90 API int pkgmgrinfo_archiveinfo_get_pkgid(pkgmgrinfo_archiveinfo_h handle,
91                 const char **pkgid)
92 {
93         package_manager_pkg_detail_info_t *info =
94                 (package_manager_pkg_detail_info_t *)handle;
95
96         if (info == NULL || strlen(info->pkgid) == 0 || pkgid == NULL) {
97                 _LOGE("invalid parameter");
98                 return PMINFO_R_EINVAL;
99         }
100
101         *pkgid = info->pkgid;
102
103         return PMINFO_R_OK;
104 }
105
106 API int pkgmgrinfo_archiveinfo_get_type(pkgmgrinfo_archiveinfo_h handle,
107                 const char **type)
108 {
109         package_manager_pkg_detail_info_t *info =
110                 (package_manager_pkg_detail_info_t *)handle;
111
112         if (info == NULL || strlen(info->pkg_type) == 0 || type == NULL ) {
113                 _LOGE("invalid parameter");
114                 return PMINFO_R_EINVAL;
115         }
116
117         *type = info->pkg_type;
118
119         return PMINFO_R_OK;
120 }
121
122 API int pkgmgrinfo_archiveinfo_get_version(pkgmgrinfo_archiveinfo_h handle,
123                 const char **version)
124 {
125         package_manager_pkg_detail_info_t *info =
126                 (package_manager_pkg_detail_info_t *)handle;
127
128         if (info == NULL || strlen(info->version) == 0 || version == NULL) {
129                 _LOGE("invalid parameter");
130                 return PMINFO_R_EINVAL;
131         }
132
133         *version = info->version;
134
135         return PMINFO_R_OK;
136 }
137
138 API int pkgmgrinfo_archiveinfo_get_api_version(pkgmgrinfo_archiveinfo_h handle,
139                 const char **api_version)
140 {
141         package_manager_pkg_detail_info_t *info =
142                 (package_manager_pkg_detail_info_t *)handle;
143
144         if (info == NULL || strlen(info->api_version) == 0 ||
145                         api_version == NULL) {
146                 _LOGE("invalid parameter");
147                 return PMINFO_R_EINVAL;
148         }
149
150         *api_version = info->api_version;
151
152         return PMINFO_R_OK;
153 }
154
155 API int pkgmgrinfo_archiveinfo_get_description(pkgmgrinfo_archiveinfo_h handle,
156                 const char **description)
157 {
158         package_manager_pkg_detail_info_t *info =
159                 (package_manager_pkg_detail_info_t *)handle;
160
161         if (info == NULL || description == NULL) {
162                 _LOGE("invalid parameter");
163                 return PMINFO_R_EINVAL;
164         }
165
166         if (strlen(info->pkg_description) == 0)
167                 return PMINFO_R_ENOENT;
168
169         *description = info->pkg_description;
170
171         return PMINFO_R_OK;
172 }
173
174 API int pkgmgrinfo_archiveinfo_get_label(pkgmgrinfo_archiveinfo_h handle,
175                 const char **label)
176 {
177         package_manager_pkg_detail_info_t *info =
178                 (package_manager_pkg_detail_info_t *)handle;
179
180         if (info == NULL || label == NULL) {
181                 _LOGE("invalid parameter");
182                 return PMINFO_R_EINVAL;
183         }
184
185         if (strlen(info->label) == 0)
186                 return PMINFO_R_ENOENT;
187
188         *label = info->label;
189
190         return PMINFO_R_OK;
191 }
192
193 API int pkgmgrinfo_archiveinfo_get_author(pkgmgrinfo_archiveinfo_h handle,
194                 const char **author)
195 {
196         package_manager_pkg_detail_info_t *info =
197                 (package_manager_pkg_detail_info_t *)handle;
198
199         if (info == NULL || author == NULL) {
200                 _LOGE("invalid parameter");
201                 return PMINFO_R_EINVAL;
202         }
203
204         if (strlen(info->author) == 0)
205                 return PMINFO_R_ENOENT;
206
207         *author = info->author;
208
209         return PMINFO_R_OK;
210 }
211
212 API int pkgmgrinfo_archiveinfo_get_icon(pkgmgrinfo_archiveinfo_h handle,
213                 const unsigned char **icon, size_t *size)
214 {
215         package_manager_pkg_detail_info_t *info =
216                 (package_manager_pkg_detail_info_t *)handle;
217
218         if (info == NULL || icon == NULL || size == NULL) {
219                 _LOGE("invalid parameter");
220                 return PMINFO_R_EINVAL;
221         }
222
223         if (info->icon_buf == NULL)
224                 return PMINFO_R_ENOENT;
225
226         *icon = (unsigned char *)info->icon_buf;
227         *size = info->icon_size;
228
229         return PMINFO_R_OK;
230 }
231
232 API int pkgmgrinfo_archiveinfo_foreach_dependency(
233                 pkgmgrinfo_archiveinfo_h handle,
234                 pkgmgrinfo_pkg_dependency_list_cb callback, void *user_data)
235 {
236         GList *tmp;
237         pkg_dependency_info_t *dependency_info;
238         package_manager_pkg_detail_info_t *info =
239                 (package_manager_pkg_detail_info_t *)handle;
240
241         if (info == NULL || callback == NULL) {
242                 _LOGE("invalid parameter");
243                 return PMINFO_R_EINVAL;
244         }
245
246         for (tmp = info->dependency_list; tmp != NULL;
247                         tmp = g_list_next(tmp)) {
248                 dependency_info = (pkg_dependency_info_t *)tmp->data;
249                 if (callback(info->pkgid,
250                                 dependency_info->pkgid,
251                                 dependency_info->type,
252                                 dependency_info->required_version,
253                                 user_data) != 0)
254                         return PMINFO_R_ERROR;
255         }
256         return PMINFO_R_OK;
257 }