e81cf5113a588059499f56a1cd89b760ef37d432
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_private.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <dlfcn.h>
28
29 #include <vconf.h>
30 #include <glib.h>
31 #include <unzip.h>
32
33 #include "pkgmgr-info.h"
34 #include "pkgmgrinfo_debug.h"
35 #include "pkgmgrinfo_private.h"
36 #include "pkgmgr_parser.h"
37
38 static GHashTable *plugin_set_list;
39
40 struct _pkginfo_str_map_t {
41         pkgmgrinfo_pkginfo_filter_prop_str prop;
42         const char *property;
43 };
44
45 static struct _pkginfo_str_map_t pkginfo_str_prop_map[] = {
46         {E_PMINFO_PKGINFO_PROP_PACKAGE_ID,      PMINFO_PKGINFO_PROP_PACKAGE_ID},
47         {E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE,    PMINFO_PKGINFO_PROP_PACKAGE_TYPE},
48         {E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION, PMINFO_PKGINFO_PROP_PACKAGE_VERSION},
49         {E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION,        PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION},
50         {E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE,       PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE},
51         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME,     PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME},
52         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL,    PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL},
53         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF,     PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF},
54         {E_PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE,       PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE},
55         {E_PMINFO_PKGINFO_PROP_PACKAGE_RES_TYPE,        PMINFO_PKGINFO_PROP_PACKAGE_RES_TYPE}
56 };
57
58 struct _pkginfo_int_map_t {
59         pkgmgrinfo_pkginfo_filter_prop_int prop;
60         const char *property;
61 };
62
63 static struct _pkginfo_int_map_t pkginfo_int_prop_map[] = {
64         {E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE,    PMINFO_PKGINFO_PROP_PACKAGE_SIZE}
65 };
66
67 struct _pkginfo_bool_map_t {
68         pkgmgrinfo_pkginfo_filter_prop_bool prop;
69         const char *property;
70 };
71
72 static struct _pkginfo_bool_map_t pkginfo_bool_prop_map[] = {
73         {E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE,       PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE},
74         {E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD,         PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD},
75         {E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY,        PMINFO_PKGINFO_PROP_PACKAGE_READONLY},
76         {E_PMINFO_PKGINFO_PROP_PACKAGE_UPDATE,          PMINFO_PKGINFO_PROP_PACKAGE_UPDATE},
77         {E_PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING,      PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING},
78         {E_PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING,       PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING},
79         {E_PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE, PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE},
80         {E_PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, PMINFO_PKGINFO_PROP_PACKAGE_DISABLE},
81         {E_PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE,   PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE},
82         {E_PMINFO_PKGINFO_PROP_PACKAGE_SYSTEM,          PMINFO_PKGINFO_PROP_PACKAGE_SYSTEM}
83 };
84
85 struct _appinfo_str_map_t {
86         pkgmgrinfo_appinfo_filter_prop_str prop;
87         const char *property;
88 };
89
90 static struct _appinfo_str_map_t appinfo_str_prop_map[] = {
91         {E_PMINFO_APPINFO_PROP_APP_ID,          PMINFO_APPINFO_PROP_APP_ID},
92         {E_PMINFO_APPINFO_PROP_APP_COMPONENT,   PMINFO_APPINFO_PROP_APP_COMPONENT},
93         {E_PMINFO_APPINFO_PROP_APP_EXEC,        PMINFO_APPINFO_PROP_APP_EXEC},
94         {E_PMINFO_APPINFO_PROP_APP_ICON,        PMINFO_APPINFO_PROP_APP_ICON},
95         {E_PMINFO_APPINFO_PROP_APP_TYPE,        PMINFO_APPINFO_PROP_APP_TYPE},
96         {E_PMINFO_APPINFO_PROP_APP_OPERATION,   PMINFO_APPINFO_PROP_APP_OPERATION},
97         {E_PMINFO_APPINFO_PROP_APP_URI, PMINFO_APPINFO_PROP_APP_URI},
98         {E_PMINFO_APPINFO_PROP_APP_MIME,        PMINFO_APPINFO_PROP_APP_MIME},
99         {E_PMINFO_APPINFO_PROP_APP_CATEGORY,    PMINFO_APPINFO_PROP_APP_CATEGORY},
100         {E_PMINFO_APPINFO_PROP_APP_HWACCELERATION,      PMINFO_APPINFO_PROP_APP_HWACCELERATION},
101         {E_PMINFO_APPINFO_PROP_APP_SCREENREADER,        PMINFO_APPINFO_PROP_APP_SCREENREADER},
102         {E_PMINFO_APPINFO_PROP_APP_PACKAGE,     PMINFO_APPINFO_PROP_APP_PACKAGE},
103         {E_PMINFO_APPINFO_PROP_APP_INSTALLED_STORAGE,   PMINFO_APPINFO_PROP_APP_INSTALLED_STORAGE},
104         {E_PMINFO_APPINFO_PROP_PRIVILEGE,       PMINFO_APPINFO_PROP_PRIVILEGE}
105 };
106
107 struct _appinfo_int_map_t {
108         pkgmgrinfo_appinfo_filter_prop_int prop;
109         const char *property;
110 };
111
112 static struct _appinfo_int_map_t appinfo_int_prop_map[] = {
113         {E_PMINFO_APPINFO_PROP_APP_SUPPORT_MODE,        PMINFO_APPINFO_PROP_APP_SUPPORT_MODE}
114 };
115
116 struct _appinfo_bool_map_t {
117         pkgmgrinfo_appinfo_filter_prop_bool prop;
118         const char *property;
119 };
120
121 static struct _appinfo_bool_map_t appinfo_bool_prop_map[] = {
122         {E_PMINFO_APPINFO_PROP_APP_NODISPLAY,           PMINFO_APPINFO_PROP_APP_NODISPLAY},
123         {E_PMINFO_APPINFO_PROP_APP_MULTIPLE,            PMINFO_APPINFO_PROP_APP_MULTIPLE},
124         {E_PMINFO_APPINFO_PROP_APP_ONBOOT,              PMINFO_APPINFO_PROP_APP_ONBOOT},
125         {E_PMINFO_APPINFO_PROP_APP_AUTORESTART,         PMINFO_APPINFO_PROP_APP_AUTORESTART},
126         {E_PMINFO_APPINFO_PROP_APP_TASKMANAGE,          PMINFO_APPINFO_PROP_APP_TASKMANAGE},
127         {E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION,             PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION},
128         {E_PMINFO_APPINFO_PROP_APP_UI_GADGET,           PMINFO_APPINFO_PROP_APP_UI_GADGET},
129         {E_PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE,             PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE},
130         {E_PMINFO_APPINFO_PROP_APP_DISABLE,             PMINFO_APPINFO_PROP_APP_DISABLE},
131         {E_PMINFO_APPINFO_PROP_APP_CHECK_STORAGE,               PMINFO_APPINFO_PROP_APP_CHECK_STORAGE},
132         {E_PMINFO_APPINFO_PROP_PKG_DISABLE,             PMINFO_APPINFO_PROP_PKG_DISABLE}
133 };
134
135 API pkgmgrinfo_pkginfo_filter_prop_str _pminfo_pkginfo_convert_to_prop_str(const char *property)
136 {
137         int i = 0;
138         int max = 0;
139         pkgmgrinfo_pkginfo_filter_prop_str prop = -1;
140
141         if (property == NULL)
142                 return -1;
143         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR + 1;
144         for (i = 0 ; i < max; i++) {
145                 if (strcmp(property, pkginfo_str_prop_map[i].property) == 0) {
146                         prop =  pkginfo_str_prop_map[i].prop;
147                         break;
148                 }
149         }
150         return prop;
151 }
152
153 API pkgmgrinfo_pkginfo_filter_prop_int _pminfo_pkginfo_convert_to_prop_int(const char *property)
154 {
155         int i = 0;
156         int max = 0;
157         pkgmgrinfo_pkginfo_filter_prop_int prop = -1;
158
159         if (property == NULL)
160                 return -1;
161         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT + 1;
162         for (i = 0 ; i < max; i++) {
163                 if (strcmp(property, pkginfo_int_prop_map[i].property) == 0) {
164                         prop =  pkginfo_int_prop_map[i].prop;
165                         break;
166                 }
167         }
168         return prop;
169 }
170
171 API pkgmgrinfo_pkginfo_filter_prop_bool _pminfo_pkginfo_convert_to_prop_bool(const char *property)
172 {
173         int i = 0;
174         int max = 0;
175         pkgmgrinfo_pkginfo_filter_prop_bool prop = -1;
176
177         if (property == NULL)
178                 return -1;
179         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL + 1;
180         for (i = 0 ; i < max; i++) {
181                 if (strcmp(property, pkginfo_bool_prop_map[i].property) == 0) {
182                         prop =  pkginfo_bool_prop_map[i].prop;
183                         break;
184                 }
185         }
186         return prop;
187 }
188
189 API pkgmgrinfo_appinfo_filter_prop_str _pminfo_appinfo_convert_to_prop_str(const char *property)
190 {
191         int i = 0;
192         int max = 0;
193         pkgmgrinfo_appinfo_filter_prop_str prop = -1;
194
195         if (property == NULL)
196                 return -1;
197         max = E_PMINFO_APPINFO_PROP_APP_MAX_STR - E_PMINFO_APPINFO_PROP_APP_MIN_STR + 1;
198         for (i = 0 ; i < max; i++) {
199                 if (strcmp(property, appinfo_str_prop_map[i].property) == 0) {
200                         prop =  appinfo_str_prop_map[i].prop;
201                         break;
202                 }
203         }
204         return prop;
205 }
206
207 API pkgmgrinfo_appinfo_filter_prop_int _pminfo_appinfo_convert_to_prop_int(const char *property)
208 {
209         int i = 0;
210         int max = 0;
211         pkgmgrinfo_appinfo_filter_prop_int prop = -1;
212
213         if (property == NULL)
214                 return -1;
215         max = E_PMINFO_APPINFO_PROP_APP_MAX_INT - E_PMINFO_APPINFO_PROP_APP_MIN_INT + 1;
216         for (i = 0 ; i < max; i++) {
217                 if (strcmp(property, appinfo_int_prop_map[i].property) == 0) {
218                         prop =  appinfo_int_prop_map[i].prop;
219                         break;
220                 }
221         }
222         return prop;
223 }
224
225 API pkgmgrinfo_appinfo_filter_prop_bool _pminfo_appinfo_convert_to_prop_bool(const char *property)
226 {
227         int i = 0;
228         int max = 0;
229         pkgmgrinfo_appinfo_filter_prop_bool prop = -1;
230
231         if (property == NULL)
232                 return -1;
233         max = E_PMINFO_APPINFO_PROP_APP_MAX_BOOL - E_PMINFO_APPINFO_PROP_APP_MIN_BOOL + 1;
234         for (i = 0 ; i < max; i++) {
235                 if (strcmp(property, appinfo_bool_prop_map[i].property) == 0) {
236                         prop =  appinfo_bool_prop_map[i].prop;
237                         break;
238                 }
239         }
240         return prop;
241 }
242
243 void _pkgmgrinfo_node_destroy(pkgmgrinfo_node_x *node)
244 {
245         if (node == NULL)
246                 return;
247
248         free(node->value);
249         free(node->key);
250         free(node);
251 }
252
253 API int __get_filter_condition(gpointer data, uid_t uid, char **condition, GList **params)
254 {
255         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)data;
256         char buf[MAX_QUERY_LEN] = {'\0'};
257         int flag = 0;
258         char *value;
259         char *ptr = NULL;
260         char *saveptr = NULL;
261
262         switch (node->prop) {
263         case E_PMINFO_PKGINFO_PROP_PACKAGE_ID:
264                 snprintf(buf, sizeof(buf), "pi.package=?");
265                 break;
266         case E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE:
267                 snprintf(buf, sizeof(buf), "pi.package_type=?");
268                 break;
269         case E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION:
270                 snprintf(buf, sizeof(buf), "pi.package_version=?");
271                 break;
272         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION:
273                 snprintf(buf, sizeof(buf), "pi.install_location=?");
274                 break;
275         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE:
276                 snprintf(buf, sizeof(buf), "pi.installed_storage=?");
277                 break;
278         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME:
279                 snprintf(buf, sizeof(buf), "pi.author_name=?");
280                 break;
281         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF:
282                 snprintf(buf, sizeof(buf), "pi.author_href=?");
283                 break;
284         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL:
285                 snprintf(buf, sizeof(buf), "pi.author_email=?");
286                 break;
287         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE:
288                 snprintf(buf, sizeof(buf), "package_privilege_info.privilege=?");
289                 flag = E_PMINFO_PKGINFO_JOIN_PRIVILEGE_INFO;
290                 break;
291         case E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE:
292                 snprintf(buf, sizeof(buf), "pi.package_size=?");
293                 break;
294         case E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE:
295                 snprintf(buf, sizeof(buf), "pi.package_removable=? COLLATE NOCASE");
296                 break;
297         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD:
298                 snprintf(buf, sizeof(buf), "pi.package_preload=? COLLATE NOCASE");
299                 break;
300         case E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY:
301                 snprintf(buf, sizeof(buf), "pi.package_readonly=? COLLATE NOCASE");
302                 break;
303         case E_PMINFO_PKGINFO_PROP_PACKAGE_UPDATE:
304                 snprintf(buf, sizeof(buf), "pi.package_update=? COLLATE NOCASE");
305                 break;
306         case E_PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING:
307                 snprintf(buf, sizeof(buf), "pi.package_appsetting=? COLLATE NOCASE");
308                 break;
309         case E_PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING:
310                 snprintf(buf, sizeof(buf), "pi.package_nodisplay=? COLLATE NOCASE");
311                 break;
312         case E_PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE:
313                 snprintf(buf, sizeof(buf), "pi.package_support_disable=? COLLATE NOCASE");
314                 break;
315         case E_PMINFO_PKGINFO_PROP_PACKAGE_DISABLE:
316                 snprintf(buf, sizeof(buf), "pi.package_disable=? COLLATE NOCASE");
317                 break;
318         case E_PMINFO_PKGINFO_PROP_PACKAGE_SYSTEM:
319                 snprintf(buf, sizeof(buf), "pi.package_system=? COLLATE NOCASE");
320                 break;
321         case E_PMINFO_PKGINFO_PROP_PACKAGE_RES_TYPE:
322                 if (node->value == NULL || strlen(node->value) == 0) {
323                         *condition = strdup("package_res_info.res_type IS NOT NULL COLLATE NOCASE");
324                         if (*condition == NULL) {
325                                 _LOGE("out of memeory");
326                                 return 0;
327                         }
328                         return E_PMINFO_PKGINFO_JOIN_RES_INFO;
329                 } else {
330                         snprintf(buf, sizeof(buf), "package_res_info.res_type=? COLLATE NOCASE");
331                         flag = E_PMINFO_PKGINFO_JOIN_RES_INFO;
332                         break;
333                 }
334         case E_PMINFO_APPINFO_PROP_APP_ID:
335                 snprintf(buf, sizeof(buf), "ai.app_id=?");
336                 break;
337         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
338                 snprintf(buf, sizeof(buf), "ai.app_component=?");
339                 break;
340         case E_PMINFO_APPINFO_PROP_APP_EXEC:
341                 snprintf(buf, sizeof(buf), "ai.app_exec=?");
342                 break;
343         case E_PMINFO_APPINFO_PROP_APP_ICON:
344                 snprintf(buf, sizeof(buf), "package_app_localized_info.app_icon=?");
345                 flag = E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO;
346                 break;
347         case E_PMINFO_APPINFO_PROP_APP_TYPE:
348                 snprintf(buf, sizeof(buf), "ai.app_type=?");
349                 break;
350         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
351                 snprintf(buf, sizeof(buf), "package_app_app_control.app_control LIKE ?||'|%%%%|%%%%'");
352                 flag = E_PMINFO_APPINFO_JOIN_APP_CONTROL;
353                 break;
354         case E_PMINFO_APPINFO_PROP_APP_URI:
355                 snprintf(buf, sizeof(buf), "package_app_app_control.app_control LIKE '%%%%|'||?||'|%%%%'");
356                 flag = E_PMINFO_APPINFO_JOIN_APP_CONTROL;
357                 break;
358         case E_PMINFO_APPINFO_PROP_APP_MIME:
359                 snprintf(buf, sizeof(buf), "package_app_app_control.app_control LIKE '%%%%|%%%%|'||?");
360                 flag = E_PMINFO_APPINFO_JOIN_APP_CONTROL;
361                 break;
362         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
363                 snprintf(buf, sizeof(buf), "package_app_app_category.category IN (");
364                 value = strdup(node->value);
365                 if (value == NULL) {
366                         _LOGE("out of memeory");
367                         return 0;
368                 }
369                 ptr = strtok_r(value, ",", &saveptr);
370                 if (ptr == NULL) {
371                         free(value);
372                         return 0;
373                 }
374                 strncat(buf, "?", sizeof(buf) - strlen(buf) - 1);
375                 *params = g_list_append(*params, strdup(ptr));
376                 while ((ptr = strtok_r(NULL, ",", &saveptr))) {
377                         strncat(buf, ", ?", sizeof(buf) - strlen(buf) - 1);
378                         *params = g_list_append(*params, strdup(ptr));
379                 }
380                 strncat(buf, ")", sizeof(buf) - strlen(buf) - 1);
381                 *condition = strdup(buf);
382                 flag = E_PMINFO_APPINFO_JOIN_CATEGORY;
383                 free(value);
384
385                 return flag;
386         case E_PMINFO_APPINFO_PROP_APP_NODISPLAY:
387                 snprintf(buf, sizeof(buf), "ai.app_nodisplay=? COLLATE NOCASE");
388                 break;
389         case E_PMINFO_APPINFO_PROP_APP_MULTIPLE:
390                 snprintf(buf, sizeof(buf), "ai.app_multiple=? COLLATE NOCASE");
391                 break;
392         case E_PMINFO_APPINFO_PROP_APP_ONBOOT:
393                 snprintf(buf, sizeof(buf), "ai.app_onboot=? COLLATE NOCASE");
394                 break;
395         case E_PMINFO_APPINFO_PROP_APP_AUTORESTART:
396                 snprintf(buf, sizeof(buf), "ai.app_autorestart=? COLLATE NOCASE");
397                 break;
398         case E_PMINFO_APPINFO_PROP_APP_TASKMANAGE:
399                 snprintf(buf, sizeof(buf), "ai.app_taskmanage=? COLLATE NOCASE");
400                 break;
401         case E_PMINFO_APPINFO_PROP_APP_HWACCELERATION:
402                 snprintf(buf, sizeof(buf), "ai.app_hwacceleration=?");
403                 break;
404         case E_PMINFO_APPINFO_PROP_APP_SCREENREADER:
405                 snprintf(buf, sizeof(buf), "ai.app_screenreader=?");
406                 break;
407         case E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION:
408                 snprintf(buf, sizeof(buf), "ai.app_launchcondition=? COLLATE NOCASE");
409                 break;
410         case E_PMINFO_APPINFO_PROP_APP_PACKAGE:
411                 snprintf(buf, sizeof(buf), "ai.package=?");
412                 break;
413         case E_PMINFO_APPINFO_PROP_APP_INSTALLED_STORAGE:
414                 snprintf(buf, sizeof(buf), "ai.app_installed_storage=?");
415                 break;
416         case E_PMINFO_APPINFO_PROP_APP_UI_GADGET:
417                 snprintf(buf, sizeof(buf), "ai.app_ui_gadget=? COLLATE NOCASE");
418                 break;
419         case E_PMINFO_APPINFO_PROP_APP_DISABLE:
420                 if (strcasecmp(node->value, "true") == 0)
421                         snprintf(buf, MAX_QUERY_LEN, "(ai.app_disable=? COLLATE NOCASE " \
422                                         "OR ui.is_disabled='true' COLLATE NOCASE)");
423                 else
424                         snprintf(buf, MAX_QUERY_LEN, "(ai.app_disable=? COLLATE NOCASE " \
425                                         "AND (ui.is_disabled='false' COLLATE NOCASE " \
426                                         "OR ui.is_disabled IS NULL))");
427                 break;
428         case E_PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE:
429                 snprintf(buf, MAX_QUERY_LEN, "ai.app_support_disable=? COLLATE NOCASE");
430                 break;
431         case E_PMINFO_APPINFO_PROP_PKG_DISABLE:
432                 snprintf(buf, MAX_QUERY_LEN,
433                                 "ai.package IN (SELECT package FROM " \
434                                 "package_info WHERE package_disable=?)");
435                 break;
436         case E_PMINFO_APPINFO_PROP_APP_SUPPORT_MODE:
437                 snprintf(buf, sizeof(buf), "ai.app_support_mode=?");
438                 break;
439         case E_PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE:
440         case E_PMINFO_APPINFO_PROP_APP_CHECK_STORAGE:
441                 *condition = NULL;
442                 return 0;
443         case E_PMINFO_APPINFO_PROP_PRIVILEGE:
444                 snprintf(buf, sizeof(buf), "package_privilege_info.privilege=?");
445                 flag = E_PMINFO_APPINFO_JOIN_PRIVILEGE;
446                 break;
447         default:
448                 _LOGE("Invalid Property Type\n");
449                 *condition = NULL;
450                 return 0;
451         }
452         *condition = strdup(buf);
453         *params = g_list_append(*params, strdup(node->value));
454         return flag;
455 }
456
457 API int __get_pkg_metadata_filter_condition(gpointer data, char **condition,
458                 GList **params)
459 {
460         pkgmgrinfo_metadata_node_x *node = (pkgmgrinfo_metadata_node_x *)data;
461         char buf[MAX_QUERY_LEN];
462
463         snprintf(buf, sizeof(buf), "(package_metadata.md_key=?");
464         if (node->value)
465                 strncat(buf, " AND package_metadata.md_value=?",
466                                 sizeof(buf) - strlen(buf) - 1);
467         strncat(buf, ")", sizeof(buf) - strlen(buf) - 1);
468
469         *condition = strdup(buf);
470         *params = g_list_append(*params, strdup(node->key));
471         if (node->value)
472                 *params = g_list_append(*params, strdup(node->value));
473
474         return E_PMINFO_PKGINFO_JOIN_METADATA;
475 }
476
477 API int __get_metadata_filter_condition(gpointer data, char **condition,
478                 GList **params)
479 {
480         pkgmgrinfo_metadata_node_x *node = (pkgmgrinfo_metadata_node_x *)data;
481         char buf[MAX_QUERY_LEN];
482
483         snprintf(buf, sizeof(buf), "(package_app_app_metadata.md_key=?");
484         if (node->value)
485                 strncat(buf, " AND package_app_app_metadata.md_value=?",
486                                 sizeof(buf) - strlen(buf) - 1);
487         strncat(buf, ")", sizeof(buf) - strlen(buf) - 1);
488
489         *condition = strdup(buf);
490         *params = g_list_append(*params, strdup(node->key));
491         if (node->value)
492                 *params = g_list_append(*params, strdup(node->value));
493
494         return E_PMINFO_APPINFO_JOIN_METADATA;
495 }
496
497 API int _add_icon_info_into_list(const char *locale, char *value, GList **icon)
498 {
499         icon_x *info;
500
501         info = calloc(1, sizeof(icon_x));
502         if (info == NULL) {
503                 LOGE("out of memory");
504                 return PMINFO_R_ERROR;
505         }
506         info->text = value;
507         info->lang = strdup(locale);
508         *icon = g_list_prepend(*icon, info);
509
510         return PMINFO_R_OK;
511 }
512
513 API int _add_label_info_into_list(const char *locale, char *value, GList **label)
514 {
515         label_x *info;
516
517         info = calloc(1, sizeof(label_x));
518         if (info == NULL) {
519                 LOGE("out of memory");
520                 return PMINFO_R_ERROR;
521         }
522         info->text = value;
523         info->lang = strdup(locale);
524         *label = g_list_prepend(*label, info);
525
526         return PMINFO_R_OK;
527 }
528
529 API int __pkginfo_check_installed_storage(package_x *pkginfo)
530 {
531         char buf[MAX_QUERY_LEN] = {'\0'};
532         int ret;
533
534         retvm_if(pkginfo->package == NULL, PMINFO_R_OK, "pkgid is NULL\n");
535         retvm_if(pkginfo->installed_storage == NULL, PMINFO_R_ERROR,
536                         "installed_storage is NULL\n");
537
538         if (strcmp(pkginfo->installed_storage, "installed_external") == 0) {
539                 snprintf(buf, MAX_QUERY_LEN - 1, "%s", pkginfo->external_path);
540                 ret = access(buf, R_OK);
541                 if (ret != 0) {
542                         _LOGE("can not access [%s] - %d", buf, ret);
543                         return PMINFO_R_ERROR;
544                 }
545         }
546
547         return PMINFO_R_OK;
548 }
549
550 API int __appinfo_check_installed_storage(application_x *appinfo)
551 {
552         retvm_if(appinfo->installed_storage == NULL, PMINFO_R_ERROR,
553                         "installed_storage is NULL\n");
554
555         if (strcmp(appinfo->installed_storage, "installed_external") == 0) {
556                 retvm_if(appinfo->external_path == NULL, PMINFO_R_ERROR,
557                                 "external path is NULL\n");
558                 if (access(appinfo->external_path, R_OK) != 0) {
559                         _LOGE("can not access [%s]", appinfo->external_path);
560                         return PMINFO_R_ERROR;
561                 }
562         }
563
564         return PMINFO_R_OK;
565 }
566
567 struct type_map {
568         const char *manifest;
569         const char *type;
570 };
571
572 const struct type_map type_map[] = {
573         { "res/wgt/config.xml", "wgt" },
574         { "config.xml", "wgt" },
575         { "tizen-manifest.xml", "tpk" },
576         { NULL, NULL}
577 };
578
579 API char *__get_type_from_path(const char *pkg_path)
580 {
581         const char *type = NULL;
582         unzFile uf;
583         int i;
584
585         uf = unzOpen(pkg_path);
586         if (uf == NULL) {
587                 _LOGE("failed to open zip file %s", pkg_path);
588                 return NULL;
589         }
590
591         for (i = 0; type_map[i].manifest != NULL; i++) {
592                 if (unzLocateFile(uf, type_map[i].manifest, 0) == UNZ_OK) {
593                         _LOGD("pkgtype of %s: [%s]", pkg_path,
594                                         type_map[i].type);
595                         type = type_map[i].type;
596                         break;
597                 }
598         }
599         unzClose(uf);
600
601         if (type == NULL) {
602                 _LOGE("cannot get pkg type of file %s", pkg_path);
603                 return NULL;
604         }
605
606         return strdup(type);
607 }
608
609 static void __get_library_path(const char *pkg_type, const char **path)
610 {
611         char buf[64];
612
613         snprintf(buf, sizeof(buf), "package-manager/backendlib/lib%s.so",
614                         pkg_type);
615
616         *path = tzplatform_mkpath(TZ_SYS_RO_ETC, buf);
617 }
618
619 API pkg_plugin_set *__load_library(const char *pkg_type)
620 {
621         void *library_handle;
622         pkg_plugin_set *plugin_set;
623         bool (*on_load)(pkg_plugin_set *plugin);
624         const char *library_path;
625
626         __get_library_path(pkg_type, &library_path);
627         if (library_path == NULL) {
628                 _LOGE("cannot get library path for %s", pkg_type);
629                 return NULL;
630         }
631
632         if (plugin_set_list == NULL) {
633                 plugin_set_list = g_hash_table_new_full(g_str_hash,
634                                 g_str_equal, free, free);
635                 if (plugin_set_list == NULL) {
636                         _LOGE("out of memory");
637                         return NULL;
638                 }
639         }
640
641         plugin_set = (pkg_plugin_set *)g_hash_table_lookup(plugin_set_list,
642                         (gconstpointer)pkg_type);
643         if (plugin_set) {
644                 _LOGD("already loaded [%s]", library_path);
645                 return plugin_set;
646         }
647
648         library_handle = dlopen(library_path, RTLD_LAZY);
649         if (library_handle == NULL) {
650                 _LOGE("dlopen is failed library_path[%s]", library_path);
651                 return NULL;
652         }
653
654         on_load = dlsym(library_handle, "pkg_plugin_on_load");
655         if (on_load == NULL || dlerror() != NULL) {
656                 _LOGE("cannot find symbol");
657                 dlclose(library_handle);
658                 return NULL;
659         }
660
661         plugin_set = (pkg_plugin_set *)calloc(1, sizeof(pkg_plugin_set));
662         if (plugin_set == NULL) {
663                 _LOGE("out of memory");
664                 dlclose(library_handle);
665                 return NULL;
666         }
667
668         if (on_load(plugin_set) != 0) {
669                 _LOGE("pkg_plugin_on_load failed");
670                 free(plugin_set);
671                 dlclose(library_handle);
672                 plugin_set = NULL;
673                 return NULL;
674         }
675
676         plugin_set->plugin_handle = library_handle;
677         snprintf(plugin_set->pkg_type,
678                         sizeof(plugin_set->pkg_type), "%s", pkg_type);
679
680         g_hash_table_insert(plugin_set_list, (gpointer)strdup(pkg_type),
681                         (gpointer)plugin_set);
682
683         _LOGD("library [%s] is loaded", library_path);
684
685         return plugin_set;
686 }
687
688 API void __unload_library(const char *pkg_type)
689 {
690         pkg_plugin_set *plugin_set;
691
692         if (plugin_set_list == NULL)
693                 return;
694
695         plugin_set = (pkg_plugin_set *)g_hash_table_lookup(plugin_set_list,
696                         (gconstpointer)pkg_type);
697
698         if (plugin_set == NULL) {
699                 _LOGE("pkg plugin for %s is not loaded", pkg_type);
700                 return;
701         }
702
703         plugin_set->plugin_on_unload();
704         dlclose(plugin_set->plugin_handle);
705         g_hash_table_remove(plugin_set_list, (gconstpointer)pkg_type);
706 }