fix issues on pkgmgr-info
[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
28 #include <vconf.h>
29 #include <sqlite3.h>
30 #include <glib.h>
31
32 #include "pkgmgr-info.h"
33 #include "pkgmgrinfo_debug.h"
34 #include "pkgmgrinfo_private.h"
35 #include "pkgmgr_parser.h"
36
37 struct _pkginfo_str_map_t {
38         pkgmgrinfo_pkginfo_filter_prop_str prop;
39         const char *property;
40 };
41
42 static struct _pkginfo_str_map_t pkginfo_str_prop_map[] = {
43         {E_PMINFO_PKGINFO_PROP_PACKAGE_ID,              PMINFO_PKGINFO_PROP_PACKAGE_ID},
44         {E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE,            PMINFO_PKGINFO_PROP_PACKAGE_TYPE},
45         {E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION,         PMINFO_PKGINFO_PROP_PACKAGE_VERSION},
46         {E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION,PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION},
47         {E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE,PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE},
48         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME,     PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME},
49         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL,    PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL},
50         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF,     PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF},
51         {E_PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE,       PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE}
52 };
53
54 struct _pkginfo_int_map_t {
55         pkgmgrinfo_pkginfo_filter_prop_int prop;
56         const char *property;
57 };
58
59 static struct _pkginfo_int_map_t pkginfo_int_prop_map[] = {
60         {E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE,    PMINFO_PKGINFO_PROP_PACKAGE_SIZE}
61 };
62
63 struct _pkginfo_bool_map_t {
64         pkgmgrinfo_pkginfo_filter_prop_bool prop;
65         const char *property;
66 };
67
68 static struct _pkginfo_bool_map_t pkginfo_bool_prop_map[] = {
69         {E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE,       PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE},
70         {E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD,         PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD},
71         {E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY,        PMINFO_PKGINFO_PROP_PACKAGE_READONLY},
72         {E_PMINFO_PKGINFO_PROP_PACKAGE_UPDATE,          PMINFO_PKGINFO_PROP_PACKAGE_UPDATE},
73         {E_PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING,      PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING},
74         {E_PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING,       PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING},
75         {E_PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE, PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE},
76         {E_PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, PMINFO_PKGINFO_PROP_PACKAGE_DISABLE}
77 };
78
79 struct _appinfo_str_map_t {
80         pkgmgrinfo_appinfo_filter_prop_str prop;
81         const char *property;
82 };
83
84 static struct _appinfo_str_map_t appinfo_str_prop_map[] = {
85         {E_PMINFO_APPINFO_PROP_APP_ID,          PMINFO_APPINFO_PROP_APP_ID},
86         {E_PMINFO_APPINFO_PROP_APP_COMPONENT,   PMINFO_APPINFO_PROP_APP_COMPONENT},
87         {E_PMINFO_APPINFO_PROP_APP_EXEC,        PMINFO_APPINFO_PROP_APP_EXEC},
88         {E_PMINFO_APPINFO_PROP_APP_ICON,        PMINFO_APPINFO_PROP_APP_ICON},
89         {E_PMINFO_APPINFO_PROP_APP_TYPE,        PMINFO_APPINFO_PROP_APP_TYPE},
90         {E_PMINFO_APPINFO_PROP_APP_OPERATION,   PMINFO_APPINFO_PROP_APP_OPERATION},
91         {E_PMINFO_APPINFO_PROP_APP_URI,         PMINFO_APPINFO_PROP_APP_URI},
92         {E_PMINFO_APPINFO_PROP_APP_MIME,        PMINFO_APPINFO_PROP_APP_MIME},
93         {E_PMINFO_APPINFO_PROP_APP_CATEGORY,    PMINFO_APPINFO_PROP_APP_CATEGORY},
94         {E_PMINFO_APPINFO_PROP_APP_HWACCELERATION,      PMINFO_APPINFO_PROP_APP_HWACCELERATION},
95         {E_PMINFO_APPINFO_PROP_APP_SCREENREADER,        PMINFO_APPINFO_PROP_APP_SCREENREADER},
96         {E_PMINFO_APPINFO_PROP_APP_METADATA_KEY,        PMINFO_APPINFO_PROP_APP_METADATA_KEY},
97         {E_PMINFO_APPINFO_PROP_APP_METADATA_VALUE,      PMINFO_APPINFO_PROP_APP_METADATA_VALUE},
98         {E_PMINFO_APPINFO_PROP_APP_PACKAGE,     PMINFO_APPINFO_PROP_APP_PACKAGE}
99 };
100
101 struct _appinfo_int_map_t {
102         pkgmgrinfo_appinfo_filter_prop_int prop;
103         const char *property;
104 };
105
106 static struct _appinfo_int_map_t appinfo_int_prop_map[] = {
107         {E_PMINFO_APPINFO_PROP_APP_DISABLE_FOR_USER,    PMINFO_APPINFO_PROP_APP_DISABLE_FOR_USER}
108 };
109
110 struct _appinfo_bool_map_t {
111         pkgmgrinfo_appinfo_filter_prop_bool prop;
112         const char *property;
113 };
114
115 static struct _appinfo_bool_map_t appinfo_bool_prop_map[] = {
116         {E_PMINFO_APPINFO_PROP_APP_NODISPLAY,           PMINFO_APPINFO_PROP_APP_NODISPLAY},
117         {E_PMINFO_APPINFO_PROP_APP_MULTIPLE,            PMINFO_APPINFO_PROP_APP_MULTIPLE},
118         {E_PMINFO_APPINFO_PROP_APP_ONBOOT,              PMINFO_APPINFO_PROP_APP_ONBOOT},
119         {E_PMINFO_APPINFO_PROP_APP_AUTORESTART,         PMINFO_APPINFO_PROP_APP_AUTORESTART},
120         {E_PMINFO_APPINFO_PROP_APP_TASKMANAGE,          PMINFO_APPINFO_PROP_APP_TASKMANAGE},
121         {E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION,             PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION},
122         {E_PMINFO_APPINFO_PROP_APP_UI_GADGET,           PMINFO_APPINFO_PROP_APP_UI_GADGET},
123         {E_PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE,             PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE},
124         {E_PMINFO_APPINFO_PROP_APP_DISABLE,             PMINFO_APPINFO_PROP_APP_DISABLE}
125 };
126
127 inline pkgmgrinfo_pkginfo_filter_prop_str _pminfo_pkginfo_convert_to_prop_str(const char *property)
128 {
129         int i = 0;
130         int max = 0;
131         pkgmgrinfo_pkginfo_filter_prop_str prop = -1;
132
133         if (property == NULL)
134                 return -1;
135         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR + 1;
136         for (i = 0 ; i < max; i++) {
137                 if (strcmp(property, pkginfo_str_prop_map[i].property) == 0) {
138                         prop =  pkginfo_str_prop_map[i].prop;
139                         break;
140                 }
141         }
142         return prop;
143 }
144
145 inline pkgmgrinfo_pkginfo_filter_prop_int _pminfo_pkginfo_convert_to_prop_int(const char *property)
146 {
147         int i = 0;
148         int max = 0;
149         pkgmgrinfo_pkginfo_filter_prop_int prop = -1;
150
151         if (property == NULL)
152                 return -1;
153         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT + 1;
154         for (i = 0 ; i < max; i++) {
155                 if (strcmp(property, pkginfo_int_prop_map[i].property) == 0) {
156                         prop =  pkginfo_int_prop_map[i].prop;
157                         break;
158                 }
159         }
160         return prop;
161 }
162
163 inline pkgmgrinfo_pkginfo_filter_prop_bool _pminfo_pkginfo_convert_to_prop_bool(const char *property)
164 {
165         int i = 0;
166         int max = 0;
167         pkgmgrinfo_pkginfo_filter_prop_bool prop = -1;
168
169         if (property == NULL)
170                 return -1;
171         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL + 1;
172         for (i = 0 ; i < max; i++) {
173                 if (strcmp(property, pkginfo_bool_prop_map[i].property) == 0) {
174                         prop =  pkginfo_bool_prop_map[i].prop;
175                         break;
176                 }
177         }
178         return prop;
179 }
180
181 inline pkgmgrinfo_appinfo_filter_prop_str _pminfo_appinfo_convert_to_prop_str(const char *property)
182 {
183         int i = 0;
184         int max = 0;
185         pkgmgrinfo_appinfo_filter_prop_str prop = -1;
186
187         if (property == NULL)
188                 return -1;
189         max = E_PMINFO_APPINFO_PROP_APP_MAX_STR - E_PMINFO_APPINFO_PROP_APP_MIN_STR + 1;
190         for (i = 0 ; i < max; i++) {
191                 if (strcmp(property, appinfo_str_prop_map[i].property) == 0) {
192                         prop =  appinfo_str_prop_map[i].prop;
193                         break;
194                 }
195         }
196         return prop;
197 }
198
199 inline pkgmgrinfo_appinfo_filter_prop_int _pminfo_appinfo_convert_to_prop_int(const char *property)
200 {
201         int i = 0;
202         int max = 0;
203         pkgmgrinfo_appinfo_filter_prop_int prop = -1;
204
205         if (property == NULL)
206                 return -1;
207         max = E_PMINFO_APPINFO_PROP_APP_MAX_INT - E_PMINFO_APPINFO_PROP_APP_MIN_INT + 1;
208         for (i = 0 ; i < max; i++) {
209                 if (strcmp(property, appinfo_int_prop_map[i].property) == 0) {
210                         prop =  appinfo_int_prop_map[i].prop;
211                         break;
212                 }
213         }
214         return prop;
215 }
216
217 inline pkgmgrinfo_appinfo_filter_prop_bool _pminfo_appinfo_convert_to_prop_bool(const char *property)
218 {
219         int i = 0;
220         int max = 0;
221         pkgmgrinfo_appinfo_filter_prop_bool prop = -1;
222
223         if (property == NULL)
224                 return -1;
225         max = E_PMINFO_APPINFO_PROP_APP_MAX_BOOL - E_PMINFO_APPINFO_PROP_APP_MIN_BOOL + 1;
226         for (i = 0 ; i < max; i++) {
227                 if (strcmp(property, appinfo_bool_prop_map[i].property) == 0) {
228                         prop =  appinfo_bool_prop_map[i].prop;
229                         break;
230                 }
231         }
232         return prop;
233 }
234
235 int __get_filter_condition(gpointer data, char **condition, GList **params)
236 {
237         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
238         char buf[MAX_QUERY_LEN] = {'\0'};
239         int flag = 0;
240         char *ptr = NULL;
241
242         switch (node->prop) {
243         case E_PMINFO_PKGINFO_PROP_PACKAGE_ID:
244                 snprintf(buf, sizeof(buf), "pi.package=?");
245                 break;
246         case E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE:
247                 snprintf(buf, sizeof(buf), "pi.package_type=?");
248                 break;
249         case E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION:
250                 snprintf(buf, sizeof(buf), "pi.package_version=?");
251                 break;
252         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION:
253                 snprintf(buf, sizeof(buf), "pi.install_location=?");
254                 break;
255         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE:
256                 snprintf(buf, sizeof(buf), "pi.installed_storage=?");
257                 break;
258         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME:
259                 snprintf(buf, sizeof(buf), "pi.author_name=?");
260                 break;
261         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF:
262                 snprintf(buf, sizeof(buf), "pi.author_href=?");
263                 break;
264         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL:
265                 snprintf(buf, sizeof(buf), "pi.author_email=?");
266                 break;
267         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE:
268                 snprintf(buf, sizeof(buf), "pi.privilege=?");
269                 flag = E_PMINFO_PKGINFO_JOIN_PRIVILEGE_INFO;
270                 break;
271         case E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE:
272                 snprintf(buf, sizeof(buf), "pi.package_size=?");
273                 break;
274         case E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE:
275                 snprintf(buf, sizeof(buf), "pi.package_removable=? COLLATE NOCASE");
276                 break;
277         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD:
278                 snprintf(buf, sizeof(buf), "pi.package_preload=? COLLATE NOCASE");
279                 break;
280         case E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY:
281                 snprintf(buf, sizeof(buf), "pi.package_readonly=? COLLATE NOCASE");
282                 break;
283         case E_PMINFO_PKGINFO_PROP_PACKAGE_UPDATE:
284                 snprintf(buf, sizeof(buf), "pi.package_update=? COLLATE NOCASE");
285                 break;
286         case E_PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING:
287                 snprintf(buf, sizeof(buf), "pi.package_appsetting=? COLLATE NOCASE");
288                 break;
289         case E_PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING:
290                 snprintf(buf, sizeof(buf), "pi.package_nodisplay=? COLLATE NOCASE");
291                 break;
292         case E_PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE:
293                 snprintf(buf, sizeof(buf), "pi.package_support_disable=? COLLATE NOCASE");
294                 break;
295         case E_PMINFO_PKGINFO_PROP_PACKAGE_DISABLE:
296                 snprintf(buf, sizeof(buf), "pi.package_disable=? COLLATE NOCASE");
297                 break;
298
299         case E_PMINFO_APPINFO_PROP_APP_ID:
300                 snprintf(buf, sizeof(buf), "ai.app_id=?");
301                 break;
302         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
303                 snprintf(buf, sizeof(buf), "ai.app_component=?");
304                 break;
305         case E_PMINFO_APPINFO_PROP_APP_EXEC:
306                 snprintf(buf, sizeof(buf), "ai.app_exec=?");
307                 break;
308         case E_PMINFO_APPINFO_PROP_APP_ICON:
309                 snprintf(buf, sizeof(buf), "package_app_localized_info.app_icon=?");
310                 flag = E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO;
311                 break;
312         case E_PMINFO_APPINFO_PROP_APP_TYPE:
313                 snprintf(buf, sizeof(buf), "ai.app_type=?");
314                 break;
315         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
316                 snprintf(buf, sizeof(buf), "package_app_app_control.app_control LIKE ?||'|%%%%|%%%%'");
317                 flag = E_PMINFO_APPINFO_JOIN_APP_CONTROL;
318                 break;
319         case E_PMINFO_APPINFO_PROP_APP_URI:
320                 snprintf(buf, sizeof(buf), "package_app_app_control.app_control LIKE '%%%%|'||?||'|%%%%'");
321                 flag = E_PMINFO_APPINFO_JOIN_APP_CONTROL;
322                 break;
323         case E_PMINFO_APPINFO_PROP_APP_MIME:
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_CATEGORY:
328                 snprintf(buf, sizeof(buf), "package_app_app_category.category IN (");
329                 ptr = strtok(node->value, ",");
330                 strncat(buf, "?", MAX_QUERY_LEN - 2);
331                 *params = g_list_append(*params, strdup(ptr));
332                 while (ptr = strtok(NULL, ",")) {
333                         strncat(buf, ", ?", MAX_QUERY_LEN - strlen(", ?") - 1);
334                         *params = g_list_append(*params, strdup(ptr));
335                 }
336                 strncat(buf, ")", MAX_QUERY_LEN - 2);
337                 *condition = strdup(buf);
338                 flag = E_PMINFO_APPINFO_JOIN_CATEGORY;
339
340                 return flag;
341         case E_PMINFO_APPINFO_PROP_APP_NODISPLAY:
342                 snprintf(buf, sizeof(buf), "ai.app_nodisplay=? COLLATE NOCASE");
343                 break;
344         case E_PMINFO_APPINFO_PROP_APP_MULTIPLE:
345                 snprintf(buf, sizeof(buf), "ai.app_multiple=? COLLATE NOCASE");
346                 break;
347         case E_PMINFO_APPINFO_PROP_APP_ONBOOT:
348                 snprintf(buf, sizeof(buf), "ai.app_onboot=? COLLATE NOCASE");
349                 break;
350         case E_PMINFO_APPINFO_PROP_APP_AUTORESTART:
351                 snprintf(buf, sizeof(buf), "ai.app_autorestart=? COLLATE NOCASE");
352                 break;
353         case E_PMINFO_APPINFO_PROP_APP_TASKMANAGE:
354                 snprintf(buf, sizeof(buf), "ai.app_taskmanage=? COLLATE NOCASE");
355                 break;
356         case E_PMINFO_APPINFO_PROP_APP_HWACCELERATION:
357                 snprintf(buf, sizeof(buf), "ai.app_hwacceleration=?");
358                 break;
359         case E_PMINFO_APPINFO_PROP_APP_SCREENREADER:
360                 snprintf(buf, sizeof(buf), "ai.app_screenreader=?");
361                 break;
362         case E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION:
363                 snprintf(buf, sizeof(buf), "ai.app_launchcondition=? COLLATE NOCASE");
364                 break;
365         case E_PMINFO_APPINFO_PROP_APP_PACKAGE:
366                 snprintf(buf, sizeof(buf), "ai.package=?");
367                 break;
368         case E_PMINFO_APPINFO_PROP_APP_UI_GADGET:
369                 snprintf(buf, sizeof(buf), "ai.app_ui_gadget=? COLLATE NOCASE");
370                 break;
371         case E_PMINFO_APPINFO_PROP_APP_METADATA_KEY:
372                 snprintf(buf, sizeof(buf), "package_app_app_metadata.md_key=?");
373                 flag = E_PMINFO_APPINFO_JOIN_METADATA;
374                 break;
375         case E_PMINFO_APPINFO_PROP_APP_METADATA_VALUE:
376                 snprintf(buf, sizeof(buf), "package_app_app_metadata.md_value=?");
377                 flag = E_PMINFO_APPINFO_JOIN_METADATA;
378                 break;
379         case E_PMINFO_APPINFO_PROP_APP_DISABLE:
380                 snprintf(buf, MAX_QUERY_LEN, "ai.app_disable=? COLLATE NOCASE");
381                 break;
382         case E_PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE:
383                 snprintf(buf, MAX_QUERY_LEN, "ai.app_support_disable=? COLLATE NOCASE");
384                 break;
385         case E_PMINFO_APPINFO_PROP_APP_DISABLE_FOR_USER:
386                 snprintf(buf, MAX_QUERY_LEN, "ai.app_id NOT IN "
387                                 "(SELECT app_id from package_app_info_for_uid WHERE uid=? " \
388                                 "AND is_disabled='true' COLLATE NOCASE)");
389                 break;
390         default:
391                 _LOGE("Invalid Property Type\n");
392                 *condition = NULL;
393                 return 0;
394         }
395         *condition = strdup(buf);
396         *params = g_list_append(*params, strdup(node->value));
397         return flag;
398 }
399
400 char *_get_system_locale(void)
401 {
402         char *lang;
403         char *locale;
404
405         lang = vconf_get_str(VCONFKEY_LANGSET);
406         if (lang == NULL) {
407                 locale = strdup(DEFAULT_LOCALE);
408                 if (locale == NULL) {
409                         LOGE("out of memory");
410                         return NULL;
411                 }
412                 return locale;
413         }
414
415         locale = malloc(sizeof(char) * 6);
416         if (locale == NULL) {
417                 LOGE("out of memory");
418                 free(lang);
419                 return NULL;
420         }
421
422         strncpy(locale, lang, 2);
423         locale[2] = '-';
424         locale[3] = tolower(lang[3]);
425         locale[4] = tolower(lang[4]);
426         locale[5] = '\0';
427
428         free(lang);
429
430         return locale;
431 }
432