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