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