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