Add filter prop for package privilege
[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 };
76
77 struct _appinfo_str_map_t {
78         pkgmgrinfo_appinfo_filter_prop_str prop;
79         const char *property;
80 };
81
82 static struct _appinfo_str_map_t appinfo_str_prop_map[] = {
83         {E_PMINFO_APPINFO_PROP_APP_ID,          PMINFO_APPINFO_PROP_APP_ID},
84         {E_PMINFO_APPINFO_PROP_APP_COMPONENT,   PMINFO_APPINFO_PROP_APP_COMPONENT},
85         {E_PMINFO_APPINFO_PROP_APP_EXEC,        PMINFO_APPINFO_PROP_APP_EXEC},
86         {E_PMINFO_APPINFO_PROP_APP_ICON,        PMINFO_APPINFO_PROP_APP_ICON},
87         {E_PMINFO_APPINFO_PROP_APP_TYPE,        PMINFO_APPINFO_PROP_APP_TYPE},
88         {E_PMINFO_APPINFO_PROP_APP_OPERATION,   PMINFO_APPINFO_PROP_APP_OPERATION},
89         {E_PMINFO_APPINFO_PROP_APP_URI,         PMINFO_APPINFO_PROP_APP_URI},
90         {E_PMINFO_APPINFO_PROP_APP_MIME,        PMINFO_APPINFO_PROP_APP_MIME},
91         {E_PMINFO_APPINFO_PROP_APP_CATEGORY,    PMINFO_APPINFO_PROP_APP_CATEGORY},
92         {E_PMINFO_APPINFO_PROP_APP_HWACCELERATION,      PMINFO_APPINFO_PROP_APP_HWACCELERATION},
93         {E_PMINFO_APPINFO_PROP_APP_SCREENREADER,        PMINFO_APPINFO_PROP_APP_SCREENREADER},
94         {E_PMINFO_APPINFO_PROP_APP_PACKAGE,     PMINFO_APPINFO_PROP_APP_PACKAGE}
95 };
96
97 struct _appinfo_int_map_t {
98         pkgmgrinfo_appinfo_filter_prop_int prop;
99         const char *property;
100 };
101
102 static struct _appinfo_int_map_t appinfo_int_prop_map[] = {
103         /*Currently No Fields*/
104 };
105
106 struct _appinfo_bool_map_t {
107         pkgmgrinfo_appinfo_filter_prop_bool prop;
108         const char *property;
109 };
110
111 static struct _appinfo_bool_map_t appinfo_bool_prop_map[] = {
112         {E_PMINFO_APPINFO_PROP_APP_NODISPLAY,           PMINFO_APPINFO_PROP_APP_NODISPLAY},
113         {E_PMINFO_APPINFO_PROP_APP_MULTIPLE,            PMINFO_APPINFO_PROP_APP_MULTIPLE},
114         {E_PMINFO_APPINFO_PROP_APP_ONBOOT,              PMINFO_APPINFO_PROP_APP_ONBOOT},
115         {E_PMINFO_APPINFO_PROP_APP_AUTORESTART,         PMINFO_APPINFO_PROP_APP_AUTORESTART},
116         {E_PMINFO_APPINFO_PROP_APP_TASKMANAGE,          PMINFO_APPINFO_PROP_APP_TASKMANAGE},
117         {E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION,             PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION},
118         {E_PMINFO_APPINFO_PROP_APP_UI_GADGET,           PMINFO_APPINFO_PROP_APP_UI_GADGET}
119 };
120
121 inline pkgmgrinfo_pkginfo_filter_prop_str _pminfo_pkginfo_convert_to_prop_str(const char *property)
122 {
123         int i = 0;
124         int max = 0;
125         pkgmgrinfo_pkginfo_filter_prop_str prop = -1;
126
127         if (property == NULL)
128                 return -1;
129         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR + 1;
130         for (i = 0 ; i < max; i++) {
131                 if (strcmp(property, pkginfo_str_prop_map[i].property) == 0) {
132                         prop =  pkginfo_str_prop_map[i].prop;
133                         break;
134                 }
135         }
136         return prop;
137 }
138
139 inline pkgmgrinfo_pkginfo_filter_prop_int _pminfo_pkginfo_convert_to_prop_int(const char *property)
140 {
141         int i = 0;
142         int max = 0;
143         pkgmgrinfo_pkginfo_filter_prop_int prop = -1;
144
145         if (property == NULL)
146                 return -1;
147         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT + 1;
148         for (i = 0 ; i < max; i++) {
149                 if (strcmp(property, pkginfo_int_prop_map[i].property) == 0) {
150                         prop =  pkginfo_int_prop_map[i].prop;
151                         break;
152                 }
153         }
154         return prop;
155 }
156
157 inline pkgmgrinfo_pkginfo_filter_prop_bool _pminfo_pkginfo_convert_to_prop_bool(const char *property)
158 {
159         int i = 0;
160         int max = 0;
161         pkgmgrinfo_pkginfo_filter_prop_bool prop = -1;
162
163         if (property == NULL)
164                 return -1;
165         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL + 1;
166         for (i = 0 ; i < max; i++) {
167                 if (strcmp(property, pkginfo_bool_prop_map[i].property) == 0) {
168                         prop =  pkginfo_bool_prop_map[i].prop;
169                         break;
170                 }
171         }
172         return prop;
173 }
174
175 inline pkgmgrinfo_appinfo_filter_prop_str _pminfo_appinfo_convert_to_prop_str(const char *property)
176 {
177         int i = 0;
178         int max = 0;
179         pkgmgrinfo_appinfo_filter_prop_str prop = -1;
180
181         if (property == NULL)
182                 return -1;
183         max = E_PMINFO_APPINFO_PROP_APP_MAX_STR - E_PMINFO_APPINFO_PROP_APP_MIN_STR + 1;
184         for (i = 0 ; i < max; i++) {
185                 if (strcmp(property, appinfo_str_prop_map[i].property) == 0) {
186                         prop =  appinfo_str_prop_map[i].prop;
187                         break;
188                 }
189         }
190         return prop;
191 }
192
193 inline pkgmgrinfo_appinfo_filter_prop_int _pminfo_appinfo_convert_to_prop_int(const char *property)
194 {
195         int i = 0;
196         int max = 0;
197         pkgmgrinfo_appinfo_filter_prop_int prop = -1;
198
199         if (property == NULL)
200                 return -1;
201         max = E_PMINFO_APPINFO_PROP_APP_MAX_INT - E_PMINFO_APPINFO_PROP_APP_MIN_INT + 1;
202         for (i = 0 ; i < max; i++) {
203                 if (strcmp(property, appinfo_int_prop_map[i].property) == 0) {
204                         prop =  appinfo_int_prop_map[i].prop;
205                         break;
206                 }
207         }
208         return prop;
209 }
210
211 inline pkgmgrinfo_appinfo_filter_prop_bool _pminfo_appinfo_convert_to_prop_bool(const char *property)
212 {
213         int i = 0;
214         int max = 0;
215         pkgmgrinfo_appinfo_filter_prop_bool prop = -1;
216
217         if (property == NULL)
218                 return -1;
219         max = E_PMINFO_APPINFO_PROP_APP_MAX_BOOL - E_PMINFO_APPINFO_PROP_APP_MIN_BOOL + 1;
220         for (i = 0 ; i < max; i++) {
221                 if (strcmp(property, appinfo_bool_prop_map[i].property) == 0) {
222                         prop =  appinfo_bool_prop_map[i].prop;
223                         break;
224                 }
225         }
226         return prop;
227 }
228
229 void __get_filter_condition(gpointer data, char **condition)
230 {
231         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
232         char buf[MAX_QUERY_LEN + 1] = {'\0'};
233         char temp[PKG_STRING_LEN_MAX] = {'\0'};
234         switch (node->prop) {
235         case E_PMINFO_PKGINFO_PROP_PACKAGE_ID:
236                 snprintf(buf, MAX_QUERY_LEN, "package_info.package='%s'", node->value);
237                 break;
238         case E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE:
239                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_type='%s'", node->value);
240                 break;
241         case E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION:
242                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_version='%s'", node->value);
243                 break;
244         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION:
245                 snprintf(buf, MAX_QUERY_LEN, "package_info.install_location='%s'", node->value);
246                 break;
247         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE:
248                 snprintf(buf, MAX_QUERY_LEN, "package_info.installed_storage='%s'", node->value);
249                 break;
250         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME:
251                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_name='%s'", node->value);
252                 break;
253         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF:
254                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_href='%s'", node->value);
255                 break;
256         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL:
257                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_email='%s'", node->value);
258                 break;
259         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE:
260                 snprintf(buf, MAX_QUERY_LEN, "package_privilege_info.privilege='%s'", node->value);
261                 break;
262         case E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE:
263                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_size='%s'", node->value);
264                 break;
265         case E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE:
266                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_removable IN %s", node->value);
267                 break;
268         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD:
269                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_preload IN %s", node->value);
270                 break;
271         case E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY:
272                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_readonly IN %s", node->value);
273                 break;
274         case E_PMINFO_PKGINFO_PROP_PACKAGE_UPDATE:
275                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_update IN %s", node->value);
276                 break;
277         case E_PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING:
278                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_appsetting IN %s", node->value);
279                 break;
280         case E_PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING:
281                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_nodisplay IN %s", node->value);
282                 break;
283
284         case E_PMINFO_APPINFO_PROP_APP_ID:
285                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_id='%s'", node->value);
286                 break;
287         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
288                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_component='%s'", node->value);
289                 break;
290         case E_PMINFO_APPINFO_PROP_APP_EXEC:
291                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_exec='%s'", node->value);
292                 break;
293         case E_PMINFO_APPINFO_PROP_APP_ICON:
294                 snprintf(buf, MAX_QUERY_LEN, "package_app_localized_info.app_icon='%s'", node->value);
295                 break;
296         case E_PMINFO_APPINFO_PROP_APP_TYPE:
297                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_type='%s'", node->value);
298                 break;
299         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
300                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
301                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.operation IN %s", temp);
302                 break;
303         case E_PMINFO_APPINFO_PROP_APP_URI:
304                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
305                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.uri_scheme IN %s", temp);
306                 break;
307         case E_PMINFO_APPINFO_PROP_APP_MIME:
308                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
309                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.mime_type IN %s", temp);
310                 break;
311         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
312                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
313                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_category.category IN %s", temp);
314                 break;
315         case E_PMINFO_APPINFO_PROP_APP_NODISPLAY:
316                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_nodisplay IN %s", node->value);
317                 break;
318         case E_PMINFO_APPINFO_PROP_APP_MULTIPLE:
319                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_multiple IN %s", node->value);
320                 break;
321         case E_PMINFO_APPINFO_PROP_APP_ONBOOT:
322                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_onboot IN %s", node->value);
323                 break;
324         case E_PMINFO_APPINFO_PROP_APP_AUTORESTART:
325                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_autorestart IN %s", node->value);
326                 break;
327         case E_PMINFO_APPINFO_PROP_APP_TASKMANAGE:
328                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_taskmanage IN %s", node->value);
329                 break;
330         case E_PMINFO_APPINFO_PROP_APP_HWACCELERATION:
331                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_hwacceleration='%s'", node->value);
332                 break;
333         case E_PMINFO_APPINFO_PROP_APP_SCREENREADER:
334                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_screenreader='%s'", node->value);
335                 break;
336         case E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION:
337                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_launchcondition IN %s", node->value);
338                 break;
339         case E_PMINFO_APPINFO_PROP_APP_PACKAGE:
340                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.package='%s'", node->value);
341                 break;
342         case E_PMINFO_APPINFO_PROP_APP_UI_GADGET:
343                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_ui_gadget IN %s", node->value);
344                 break;
345         default:
346                 _LOGE("Invalid Property Type\n");
347                 *condition = NULL;
348                 return;
349         }
350         *condition = strdup(buf);
351         return;
352 }
353
354 char *_get_system_locale(void)
355 {
356         char *lang;
357         char *locale;
358
359         lang = vconf_get_str(VCONFKEY_LANGSET);
360         if (lang == NULL) {
361                 locale = strdup(DEFAULT_LOCALE);
362                 if (locale == NULL) {
363                         LOGE("out of memory");
364                         return NULL;
365                 }
366                 return locale;
367         }
368
369         locale = malloc(sizeof(char) * 6);
370         if (locale == NULL) {
371                 LOGE("out of memory");
372                 free(lang);
373                 return NULL;
374         }
375
376         strncpy(locale, lang, 2);
377         locale[2] = '-';
378         locale[3] = tolower(lang[3]);
379         locale[4] = tolower(lang[4]);
380         locale[5] = '\0';
381
382         free(lang);
383
384         return locale;
385 }
386