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