Remove the cache when language change event occurs
[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         free(pkg_type);
67         *handle = info;
68
69         return PMINFO_R_OK;
70 }
71
72 API int pkgmgrinfo_archiveinfo_destroy_archiveinfo(
73                 pkgmgrinfo_archiveinfo_h handle)
74 {
75         package_manager_pkg_detail_info_t *info =
76                 (package_manager_pkg_detail_info_t *)handle;
77
78         if (info == NULL) {
79                 _LOGE("invalid parameter");
80                 return PMINFO_R_EINVAL;
81         }
82
83         free(info->icon_buf);
84         g_list_free_full(info->privilege_list, free);
85         g_list_free_full(info->dependency_list, free);
86         free(info);
87
88         return PMINFO_R_OK;
89 }
90
91 API int pkgmgrinfo_archiveinfo_get_pkgid(pkgmgrinfo_archiveinfo_h handle,
92                 const char **pkgid)
93 {
94         package_manager_pkg_detail_info_t *info =
95                 (package_manager_pkg_detail_info_t *)handle;
96
97         if (info == NULL || strlen(info->pkgid) == 0 || pkgid == NULL) {
98                 _LOGE("invalid parameter");
99                 return PMINFO_R_EINVAL;
100         }
101
102         *pkgid = info->pkgid;
103
104         return PMINFO_R_OK;
105 }
106
107 API int pkgmgrinfo_archiveinfo_get_type(pkgmgrinfo_archiveinfo_h handle,
108                 const char **type)
109 {
110         package_manager_pkg_detail_info_t *info =
111                 (package_manager_pkg_detail_info_t *)handle;
112
113         if (info == NULL || strlen(info->pkg_type) == 0 || type == NULL) {
114                 _LOGE("invalid parameter");
115                 return PMINFO_R_EINVAL;
116         }
117
118         *type = info->pkg_type;
119
120         return PMINFO_R_OK;
121 }
122
123 API int pkgmgrinfo_archiveinfo_get_version(pkgmgrinfo_archiveinfo_h handle,
124                 const char **version)
125 {
126         package_manager_pkg_detail_info_t *info =
127                 (package_manager_pkg_detail_info_t *)handle;
128
129         if (info == NULL || strlen(info->version) == 0 || version == NULL) {
130                 _LOGE("invalid parameter");
131                 return PMINFO_R_EINVAL;
132         }
133
134         *version = info->version;
135
136         return PMINFO_R_OK;
137 }
138
139 API int pkgmgrinfo_archiveinfo_get_api_version(pkgmgrinfo_archiveinfo_h handle,
140                 const char **api_version)
141 {
142         package_manager_pkg_detail_info_t *info =
143                 (package_manager_pkg_detail_info_t *)handle;
144
145         if (info == NULL || strlen(info->api_version) == 0 ||
146                         api_version == NULL) {
147                 _LOGE("invalid parameter");
148                 return PMINFO_R_EINVAL;
149         }
150
151         *api_version = info->api_version;
152
153         return PMINFO_R_OK;
154 }
155
156 API int pkgmgrinfo_archiveinfo_get_description(pkgmgrinfo_archiveinfo_h handle,
157                 const char **description)
158 {
159         package_manager_pkg_detail_info_t *info =
160                 (package_manager_pkg_detail_info_t *)handle;
161
162         if (info == NULL || description == NULL) {
163                 _LOGE("invalid parameter");
164                 return PMINFO_R_EINVAL;
165         }
166
167         if (strlen(info->pkg_description) == 0)
168                 return PMINFO_R_ENOENT;
169
170         *description = info->pkg_description;
171
172         return PMINFO_R_OK;
173 }
174
175 API int pkgmgrinfo_archiveinfo_get_label(pkgmgrinfo_archiveinfo_h handle,
176                 const char **label)
177 {
178         package_manager_pkg_detail_info_t *info =
179                 (package_manager_pkg_detail_info_t *)handle;
180
181         if (info == NULL || label == NULL) {
182                 _LOGE("invalid parameter");
183                 return PMINFO_R_EINVAL;
184         }
185
186         if (strlen(info->label) == 0)
187                 return PMINFO_R_ENOENT;
188
189         *label = info->label;
190
191         return PMINFO_R_OK;
192 }
193
194 API int pkgmgrinfo_archiveinfo_get_author(pkgmgrinfo_archiveinfo_h handle,
195                 const char **author)
196 {
197         package_manager_pkg_detail_info_t *info =
198                 (package_manager_pkg_detail_info_t *)handle;
199
200         if (info == NULL || author == NULL) {
201                 _LOGE("invalid parameter");
202                 return PMINFO_R_EINVAL;
203         }
204
205         if (strlen(info->author) == 0)
206                 return PMINFO_R_ENOENT;
207
208         *author = info->author;
209
210         return PMINFO_R_OK;
211 }
212
213 API int pkgmgrinfo_archiveinfo_get_icon(pkgmgrinfo_archiveinfo_h handle,
214                 const unsigned char **icon, size_t *size)
215 {
216         package_manager_pkg_detail_info_t *info =
217                 (package_manager_pkg_detail_info_t *)handle;
218
219         if (info == NULL || icon == NULL || size == NULL) {
220                 _LOGE("invalid parameter");
221                 return PMINFO_R_EINVAL;
222         }
223
224         if (info->icon_buf == NULL)
225                 return PMINFO_R_ENOENT;
226
227         *icon = (unsigned char *)info->icon_buf;
228         *size = info->icon_size;
229
230         return PMINFO_R_OK;
231 }
232
233 API int pkgmgrinfo_archiveinfo_foreach_dependency(
234                 pkgmgrinfo_archiveinfo_h handle,
235                 pkgmgrinfo_pkg_dependency_list_cb callback, void *user_data)
236 {
237         GList *tmp;
238         pkg_dependency_info_t *dependency_info;
239         package_manager_pkg_detail_info_t *info =
240                 (package_manager_pkg_detail_info_t *)handle;
241
242         if (info == NULL || callback == NULL) {
243                 _LOGE("invalid parameter");
244                 return PMINFO_R_EINVAL;
245         }
246
247         for (tmp = info->dependency_list; tmp != NULL;
248                         tmp = g_list_next(tmp)) {
249                 dependency_info = (pkg_dependency_info_t *)tmp->data;
250                 if (callback(info->pkgid,
251                                 dependency_info->pkgid,
252                                 dependency_info->type,
253                                 dependency_info->required_version,
254                                 user_data) != 0)
255                         return PMINFO_R_ERROR;
256         }
257         return PMINFO_R_OK;
258 }