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