ff0f7db370ac9d83fc77aa162c4d1241f7e16788
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgr-info.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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <db-util.h>
28 #include <sqlite3.h>
29 #include <vconf.h>
30 #include <glib.h>
31 #include <assert.h>
32
33 #include <libxml/parser.h>
34 #include <libxml/xmlreader.h>
35 #include <libxml/xmlschemas.h>
36
37 #include "pkgmgr_parser.h"
38 #include "pkgmgr-info-internal.h"
39 #include "pkgmgr-info.h"
40 #include <dirent.h>
41 #include <sys/stat.h>
42
43 #define ASC_CHAR(s) (const char *)s
44 #define XML_CHAR(s) (const xmlChar *)s
45
46 #define MANIFEST_DB     "/opt/dbspace/.pkgmgr_parser.db"
47 #define MAX_QUERY_LEN   4096
48 #define CERT_DB         "/opt/dbspace/.pkgmgr_cert.db"
49 #define DATACONTROL_DB  "/opt/usr/dbspace/.app-package.db"
50 #define PKG_TYPE_STRING_LEN_MAX         128
51 #define PKG_VERSION_STRING_LEN_MAX      128
52 #define PKG_VALUE_STRING_LEN_MAX                512
53 #define PKG_RW_PATH "/opt/usr/apps/"
54 #define PKG_RO_PATH "/usr/apps/"
55 #define BLOCK_SIZE      4096 /*in bytes*/
56
57 #define MMC_PATH "/opt/storage/sdcard"
58 #define PKG_SD_PATH MMC_PATH"/app2sd/"
59 #define PKG_INSTALLATION_PATH "/opt/usr/apps/"
60
61 #define FILTER_QUERY_COUNT_PACKAGE      "select count(DISTINCT package_info.package) " \
62                                 "from package_info LEFT OUTER JOIN package_localized_info " \
63                                 "ON package_info.package=package_localized_info.package " \
64                                 "and package_localized_info.package_locale='%s' where "
65
66 #define FILTER_QUERY_LIST_PACKAGE       "select DISTINCT package_info.package " \
67                                 "from package_info LEFT OUTER JOIN package_localized_info " \
68                                 "ON package_info.package=package_localized_info.package " \
69                                 "and package_localized_info.package_locale='%s' where "
70
71 #define FILTER_QUERY_COUNT_APP  "select count(DISTINCT package_app_info.app_id) " \
72                                 "from package_app_info LEFT OUTER JOIN package_app_localized_info " \
73                                 "ON package_app_info.app_id=package_app_localized_info.app_id " \
74                                 "and package_app_localized_info.app_locale='%s' " \
75                                 "LEFT OUTER JOIN package_app_app_svc " \
76                                 "ON package_app_info.app_id=package_app_app_svc.app_id " \
77                                 "LEFT OUTER JOIN package_app_app_category " \
78                                 "ON package_app_info.app_id=package_app_app_category.app_id where "
79
80 #define FILTER_QUERY_LIST_APP   "select DISTINCT package_app_info.app_id, package_app_info.app_component " \
81                                 "from package_app_info LEFT OUTER JOIN package_app_localized_info " \
82                                 "ON package_app_info.app_id=package_app_localized_info.app_id " \
83                                 "and package_app_localized_info.app_locale='%s' " \
84                                 "LEFT OUTER JOIN package_app_app_svc " \
85                                 "ON package_app_info.app_id=package_app_app_svc.app_id " \
86                                 "LEFT OUTER JOIN package_app_app_category " \
87                                 "ON package_app_info.app_id=package_app_app_category.app_id where "
88
89 #define retv_if(expr, val) do { \
90         if(expr) { \
91                 _LOGE("(%s) -> %s() return\n", #expr, __FUNCTION__); \
92                 return (val); \
93         } \
94 } while (0)
95
96 #define LANGUAGE_LENGTH 2
97
98 typedef struct _pkgmgr_instcertinfo_x {
99         char *pkgid;
100         char *auth_signer_cert;
101         char *auth_im_cert;
102         char *auth_root_cert;
103         char *dist_signer_cert;
104         char *dist_im_cert;
105         char *dist_root_cert;
106         char *dist2_signer_cert;
107         char *dist2_im_cert;
108         char *dist2_root_cert;
109 } pkgmgr_instcertinfo_x;
110
111 sqlite3 *cert_db = NULL;
112
113 typedef struct _pkgmgr_pkginfo_x {
114         manifest_x *manifest_info;
115         char *tmp;
116         char *tmp_dup;
117
118         struct _pkgmgr_pkginfo_x *prev;
119         struct _pkgmgr_pkginfo_x *next;
120 } pkgmgr_pkginfo_x;
121
122 typedef struct _pkgmgr_cert_x {
123         const char *pkgid;
124         const char *certvalue;
125 } pkgmgr_cert_x;
126
127 typedef struct _pkgmgr_datacontrol_x {
128         char *appid;
129         char *access;
130 } pkgmgr_datacontrol_x;
131
132 typedef struct _pkgmgr_iconpath_x {
133         char *appid;
134         char *iconpath;
135 } pkgmgr_iconpath_x;
136
137 typedef struct _pkgmgr_locale_x {
138         char *locale;
139 } pkgmgr_locale_x;
140
141 typedef struct _pkgmgr_appinfo_x {
142         const char *package;
143         pkgmgrinfo_app_component app_component;
144         union {
145                 uiapplication_x *uiapp_info;
146                 serviceapplication_x *svcapp_info;
147         };
148 } pkgmgr_appinfo_x;
149
150 typedef struct _pkgmgr_certinfo_x {
151         char *pkgid;
152         char *auth_signer_cert;
153         char *auth_im_cert;
154         char *auth_root_cert;
155         char *dist_signer_cert;
156         char *dist_im_cert;
157         char *dist_root_cert;
158         char *dist2_signer_cert;
159         char *dist2_im_cert;
160         char *dist2_root_cert;
161 } pkgmgr_certinfo_x;
162
163 /*For filter APIs*/
164 typedef struct _pkgmgrinfo_filter_x {
165         GSList *list;
166 } pkgmgrinfo_filter_x;
167
168 typedef struct _pkgmgrinfo_node_x {
169         int prop;
170         char *value;
171 } pkgmgrinfo_node_x;
172
173 typedef struct _pkgmgrinfo_appcontrol_x {
174         int operation_count;
175         int uri_count;
176         int mime_count;
177         char **operation;
178         char **uri;
179         char **mime;
180 } pkgmgrinfo_appcontrol_x;
181
182 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
183
184 char *pkgtype = "rpm";
185 sqlite3 *manifest_db = NULL;
186 sqlite3 *datacontrol_db = NULL;
187 int gflag[9];/*one for each cert type*/
188 char *gpkgcert[9];/*To store pkg cert values*/
189
190 static int __open_manifest_db();
191 static int __exec_pkginfo_query(char *query, void *data);
192 static int __exec_appinfo_query(char *query, void *data);
193 static int __exec_certinfo_query(char *query, void *data);
194 static int __exec_sqlite_query(char *query, sqlite_query_callback callback, void *data);
195 static int __pkginfo_cb(void *data, int ncols, char **coltxt, char **colname);
196 static int __appinfo_cb(void *data, int ncols, char **coltxt, char **colname);
197 static int __certinfo_cb(void *data, int ncols, char **coltxt, char **colname);
198 static int __validate_cb(void *data, int ncols, char **coltxt, char **colname);
199 static int __delete_certinfo_cb(void *data, int ncols, char **coltxt, char **colname);
200 static int __count_cb(void *data, int ncols, char **coltxt, char **colname);
201 static int __uiapp_list_cb(void *data, int ncols, char **coltxt, char **colname);
202 static int __svcapp_list_cb(void *data, int ncols, char **coltxt, char **colname);
203 static int __pkg_list_cb(void *data, int ncols, char **coltxt, char **colname);
204 static int __app_list_cb(void *data, int ncols, char **coltxt, char **colname);
205 static int __pkgmgr_appinfo_new_handle_id();
206 static int __pkgmgr_pkginfo_new_handle_id();
207 static void __cleanup_pkginfo(pkgmgr_pkginfo_x *data);
208 static void __cleanup_appinfo(pkgmgr_appinfo_x *data);
209 static char* __convert_system_locale_to_manifest_locale(char *syslocale);
210 static void __destroy_each_node(gpointer data, gpointer user_data);
211 static void __get_filter_condition(gpointer data, char **condition);
212 static gint __compare_func(gconstpointer data1, gconstpointer data2);
213
214 static gint __compare_func(gconstpointer data1, gconstpointer data2)
215 {
216         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
217         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
218         if (node1->prop == node2->prop)
219                 return 0;
220         else if (node1->prop > node2->prop)
221                 return 1;
222         else
223                 return -1;
224 }
225
226 static int __count_cb(void *data, int ncols, char **coltxt, char **colname)
227 {
228         int *p = (int*)data;
229         *p = atoi(coltxt[0]);
230         _LOGE("count value is %d\n", *p);
231         return 0;
232 }
233
234 static void __destroy_each_node(gpointer data, gpointer user_data)
235 {
236         if (data == NULL)
237                 return;
238         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
239         if (node->value) {
240                 free(node->value);
241                 node->value = NULL;
242         }
243         free(node);
244         node = NULL;
245 }
246
247 static void __get_filter_condition(gpointer data, char **condition)
248 {
249         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
250         char buf[MAX_QUERY_LEN + 1] = {'\0'};
251         char temp[PKG_STRING_LEN_MAX] = {'\0'};
252         switch (node->prop) {
253         case E_PMINFO_PKGINFO_PROP_PACKAGE_ID:
254                 snprintf(buf, MAX_QUERY_LEN, "package_info.package='%s'", node->value);
255                 break;
256         case E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE:
257                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_type='%s'", node->value);
258                 break;
259         case E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION:
260                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_version='%s'", node->value);
261                 break;
262         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION:
263                 snprintf(buf, MAX_QUERY_LEN, "package_info.install_location='%s'", node->value);
264                 break;
265         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME:
266                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_name='%s'", node->value);
267                 break;
268         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF:
269                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_href='%s'", node->value);
270                 break;
271         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL:
272                 snprintf(buf, MAX_QUERY_LEN, "package_info.author_email='%s'", node->value);
273                 break;
274         case E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE:
275                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_size='%s'", node->value);
276                 break;
277         case E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE:
278                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_removable IN %s", node->value);
279                 break;
280         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD:
281                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_preload IN %s", node->value);
282                 break;
283         case E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY:
284                 snprintf(buf, MAX_QUERY_LEN, "package_info.package_readonly IN %s", node->value);
285                 break;
286         case E_PMINFO_APPINFO_PROP_APP_ID:
287                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_id='%s'", node->value);
288                 break;
289         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
290                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_component='%s'", node->value);
291                 break;
292         case E_PMINFO_APPINFO_PROP_APP_EXEC:
293                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_exec='%s'", node->value);
294                 break;
295         case E_PMINFO_APPINFO_PROP_APP_ICON:
296                 snprintf(buf, MAX_QUERY_LEN, "package_app_localized_info.app_icon='%s'", node->value);
297                 break;
298         case E_PMINFO_APPINFO_PROP_APP_TYPE:
299                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_type='%s'", node->value);
300                 break;
301         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
302                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
303                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.operation IN %s", temp);
304                 break;
305         case E_PMINFO_APPINFO_PROP_APP_URI:
306                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
307                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.uri_scheme IN %s", temp);
308                 break;
309         case E_PMINFO_APPINFO_PROP_APP_MIME:
310                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
311                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_svc.mime_type IN %s", temp);
312                 break;
313         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
314                 snprintf(temp, PKG_STRING_LEN_MAX, "(%s)", node->value);
315                 snprintf(buf, MAX_QUERY_LEN, "package_app_app_category.category IN %s", temp);
316                 break;
317         case E_PMINFO_APPINFO_PROP_APP_NODISPLAY:
318                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_nodisplay IN %s", node->value);
319                 break;
320         case E_PMINFO_APPINFO_PROP_APP_MULTIPLE:
321                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_multiple IN %s", node->value);
322                 break;
323         case E_PMINFO_APPINFO_PROP_APP_ONBOOT:
324                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_onboot IN %s", node->value);
325                 break;
326         case E_PMINFO_APPINFO_PROP_APP_AUTORESTART:
327                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_autorestart IN %s", node->value);
328                 break;
329         case E_PMINFO_APPINFO_PROP_APP_TASKMANAGE:
330                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_taskmanage IN %s", node->value);
331                 break;
332         case E_PMINFO_APPINFO_PROP_APP_HWACCELERATION:
333                 snprintf(buf, MAX_QUERY_LEN, "package_app_info.app_hwacceleration='%s'", node->value);
334                 break;
335         default:
336                 _LOGE("Invalid Property Type\n");
337                 *condition = NULL;
338                 return;
339         }
340         *condition = strdup(buf);
341         return;
342 }
343
344 static char* __convert_system_locale_to_manifest_locale(char *syslocale)
345 {
346         if (syslocale == NULL)
347                 return strdup(DEFAULT_LOCALE);
348         char *locale = NULL;
349         locale = (char *)calloc(1, 6);
350         if (!locale) {
351                 _LOGE("Malloc Failed\n");
352                 return NULL;
353         }
354         strncpy(locale, syslocale, 2);
355         strncat(locale, "-", 1);
356         locale[3] = syslocale[3] + 32;
357         locale[4] = syslocale[4] + 32;
358         return locale;
359 }
360
361 static void __cleanup_pkginfo(pkgmgr_pkginfo_x *data)
362 {
363         if (data == NULL)
364                 return;
365         if (data->tmp_dup){
366                 free((void *)data->tmp_dup);
367                 data->tmp_dup = NULL;
368         }
369
370         pkgmgr_parser_free_manifest_xml(data->manifest_info);
371         free((void *)data);
372         data = NULL;
373         return;
374 }
375
376 static void __cleanup_appinfo(pkgmgr_appinfo_x *data)
377 {
378         if (data == NULL)
379                 return;
380
381         manifest_x *mfx = calloc(1, sizeof(manifest_x));
382         if (data->app_component == PMINFO_UI_APP)
383                 mfx->uiapplication = data->uiapp_info;
384         else if (data->app_component == PMINFO_SVC_APP)
385                 mfx->serviceapplication = data->svcapp_info;
386         pkgmgr_parser_free_manifest_xml(mfx);
387         free((void *)data);
388         data = NULL;
389         return;
390 }
391
392 static int __open_manifest_db()
393 {
394         int ret = -1;
395         if (access(MANIFEST_DB, F_OK) == 0) {
396                 ret =
397                     db_util_open_with_options(MANIFEST_DB, &manifest_db,
398                                  SQLITE_OPEN_READONLY, NULL);
399                 if (ret != SQLITE_OK) {
400                         _LOGE("connect db [%s] failed!\n", MANIFEST_DB);
401                         return -1;
402                 }
403                 return 0;
404         }
405         _LOGE("Manifest DB does not exists !!\n");
406         return -1;
407 }
408
409 static int __open_datacontrol_db()
410 {
411         int ret = -1;
412         if (access(DATACONTROL_DB, F_OK) == 0) {
413                 ret =
414                     db_util_open_with_options(DATACONTROL_DB, &datacontrol_db,
415                                  SQLITE_OPEN_READONLY, NULL);
416                 if (ret != SQLITE_OK) {
417                         _LOGE("connect db [%s] failed!\n", DATACONTROL_DB);
418                         return -1;
419                 }
420                 return 0;
421         }
422         _LOGE("Datacontrol DB does not exists !!\n");
423         return -1;
424 }
425
426 static int __pkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
427 {
428         pkgmgr_pkginfo_x *udata = (pkgmgr_pkginfo_x *)data;
429         int i = 0;
430         pkgmgr_pkginfo_x *info = NULL;
431         info = calloc(1, sizeof(pkgmgr_pkginfo_x));
432         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
433
434         LISTADD(udata, info);
435         for(i = 0; i < ncols; i++)
436         {
437                 if (strcmp(colname[i], "package") == 0) {
438                         if (coltxt[i])
439                                 info->manifest_info->package = strdup(coltxt[i]);
440                         else
441                                 info->manifest_info->package = NULL;
442                 } else
443                         continue;
444         }
445
446         return 0;
447 }
448
449 static int __app_list_cb(void *data, int ncols, char **coltxt, char **colname)
450 {
451         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
452         int i = 0;
453         int j = 0;
454         uiapplication_x *uiapp = NULL;
455         serviceapplication_x *svcapp = NULL;
456         for(i = 0; i < ncols; i++)
457         {
458                 if (strcmp(colname[i], "app_component") == 0) {
459                         if (coltxt[i]) {
460                                 if (strcmp(coltxt[i], "uiapp") == 0) {
461                                         uiapp = calloc(1, sizeof(uiapplication_x));
462                                         if (uiapp == NULL) {
463                                                 _LOGE("Out of Memory!!!\n");
464                                                 return -1;
465                                         }
466                                         LISTADD(info->manifest_info->uiapplication, uiapp);
467                                         for(j = 0; j < ncols; j++)
468                                         {
469                                                 if (strcmp(colname[j], "app_id") == 0) {
470                                                         if (coltxt[j])
471                                                                 info->manifest_info->uiapplication->appid = strdup(coltxt[j]);
472                                                 } else if (strcmp(colname[j], "package") == 0) {
473                                                         if (coltxt[j])
474                                                                 info->manifest_info->uiapplication->package = strdup(coltxt[j]);
475                                                 } else
476                                                         continue;
477                                         }
478                                 } else {
479                                         svcapp = calloc(1, sizeof(serviceapplication_x));
480                                         if (svcapp == NULL) {
481                                                 _LOGE("Out of Memory!!!\n");
482                                                 return -1;
483                                         }
484                                         LISTADD(info->manifest_info->serviceapplication, svcapp);
485                                         for(j = 0; j < ncols; j++)
486                                         {
487                                                 if (strcmp(colname[j], "app_id") == 0) {
488                                                         if (coltxt[j])
489                                                                 info->manifest_info->serviceapplication->appid = strdup(coltxt[j]);
490                                                 } else if (strcmp(colname[j], "package") == 0) {
491                                                         if (coltxt[j])
492                                                                 info->manifest_info->serviceapplication->package = strdup(coltxt[j]);
493                                                 } else
494                                                         continue;
495                                         }
496                                 }
497                         }
498                 } else
499                         continue;
500         }
501
502         return 0;
503 }
504
505
506 static int __uiapp_list_cb(void *data, int ncols, char **coltxt, char **colname)
507 {
508         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
509         int i = 0;
510         uiapplication_x *uiapp = NULL;
511         icon_x *icon = NULL;
512         label_x *label = NULL;
513
514         uiapp = calloc(1, sizeof(uiapplication_x));
515         LISTADD(info->manifest_info->uiapplication, uiapp);
516         icon = calloc(1, sizeof(icon_x));
517         LISTADD(info->manifest_info->uiapplication->icon, icon);
518         label = calloc(1, sizeof(label_x));
519         LISTADD(info->manifest_info->uiapplication->label, label);
520
521         for(i = 0; i < ncols; i++)
522         {
523                 if (strcmp(colname[i], "app_id") == 0) {
524                         if (coltxt[i])
525                                 info->manifest_info->uiapplication->appid = strdup(coltxt[i]);
526                         else
527                                 info->manifest_info->uiapplication->appid = NULL;
528                 } else if (strcmp(colname[i], "app_exec") == 0) {
529                         if (coltxt[i])
530                                 info->manifest_info->uiapplication->exec = strdup(coltxt[i]);
531                         else
532                                 info->manifest_info->uiapplication->exec = NULL;
533                 } else if (strcmp(colname[i], "app_type") == 0 ){
534                         if (coltxt[i])
535                                 info->manifest_info->uiapplication->type = strdup(coltxt[i]);
536                         else
537                                 info->manifest_info->uiapplication->type = NULL;
538                 } else if (strcmp(colname[i], "app_nodisplay") == 0 ){
539                         if (coltxt[i])
540                                 info->manifest_info->uiapplication->nodisplay = strdup(coltxt[i]);
541                         else
542                                 info->manifest_info->uiapplication->nodisplay = NULL;
543                 } else if (strcmp(colname[i], "app_multiple") == 0 ){
544                         if (coltxt[i])
545                                 info->manifest_info->uiapplication->multiple = strdup(coltxt[i]);
546                         else
547                                 info->manifest_info->uiapplication->multiple = NULL;
548                 } else if (strcmp(colname[i], "app_taskmanage") == 0 ){
549                         if (coltxt[i])
550                                 info->manifest_info->uiapplication->taskmanage = strdup(coltxt[i]);
551                         else
552                                 info->manifest_info->uiapplication->taskmanage = NULL;
553                 } else if (strcmp(colname[i], "app_hwacceleration") == 0 ){
554                         if (coltxt[i])
555                                 info->manifest_info->uiapplication->hwacceleration = strdup(coltxt[i]);
556                         else
557                                 info->manifest_info->uiapplication->hwacceleration = NULL;
558                 } else if (strcmp(colname[i], "package") == 0 ){
559                         if (coltxt[i])
560                                 info->manifest_info->uiapplication->package = strdup(coltxt[i]);
561                         else
562                                 info->manifest_info->uiapplication->package = NULL;
563                 } else if (strcmp(colname[i], "app_icon") == 0) {
564                         if (coltxt[i])
565                                 info->manifest_info->uiapplication->icon->text = strdup(coltxt[i]);
566                         else
567                                 info->manifest_info->uiapplication->icon->text = NULL;
568                 } else if (strcmp(colname[i], "app_label") == 0 ) {
569                         if (coltxt[i])
570                                 info->manifest_info->uiapplication->label->text = strdup(coltxt[i]);
571                         else
572                                 info->manifest_info->uiapplication->label->text = NULL;
573                 } else if (strcmp(colname[i], "app_locale") == 0 ) {
574                         if (coltxt[i]) {
575                                 info->manifest_info->uiapplication->icon->lang = strdup(coltxt[i]);
576                                 info->manifest_info->uiapplication->label->lang = strdup(coltxt[i]);
577                         }
578                         else {
579                                 info->manifest_info->uiapplication->icon->lang = NULL;
580                                 info->manifest_info->uiapplication->label->lang = NULL;
581                         }
582                 } else
583                         continue;
584         }
585         return 0;
586 }
587
588 static int __svcapp_list_cb(void *data, int ncols, char **coltxt, char **colname)
589 {
590         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
591         int i = 0;
592         serviceapplication_x *svcapp = NULL;
593         icon_x *icon = NULL;
594         label_x *label = NULL;
595         svcapp = calloc(1, sizeof(serviceapplication_x));
596         LISTADD(info->manifest_info->serviceapplication, svcapp);
597         icon = calloc(1, sizeof(icon_x));
598         LISTADD(info->manifest_info->serviceapplication->icon, icon);
599         label = calloc(1, sizeof(label_x));
600         LISTADD(info->manifest_info->serviceapplication->label, label);
601         for(i = 0; i < ncols; i++)
602         {
603                 if (strcmp(colname[i], "app_id") == 0) {
604                         if (coltxt[i])
605                                 info->manifest_info->serviceapplication->appid = strdup(coltxt[i]);
606                         else
607                                 info->manifest_info->serviceapplication->appid = NULL;
608                 } else if (strcmp(colname[i], "app_exec") == 0) {
609                         if (coltxt[i])
610                                 info->manifest_info->serviceapplication->exec = strdup(coltxt[i]);
611                         else
612                                 info->manifest_info->serviceapplication->exec = NULL;
613                 } else if (strcmp(colname[i], "app_type") == 0 ){
614                         if (coltxt[i])
615                                 info->manifest_info->serviceapplication->type = strdup(coltxt[i]);
616                         else
617                                 info->manifest_info->serviceapplication->type = NULL;
618                 } else if (strcmp(colname[i], "app_onboot") == 0 ){
619                         if (coltxt[i])
620                                 info->manifest_info->serviceapplication->onboot = strdup(coltxt[i]);
621                         else
622                                 info->manifest_info->serviceapplication->onboot = NULL;
623                 } else if (strcmp(colname[i], "app_autorestart") == 0 ){
624                         if (coltxt[i])
625                                 info->manifest_info->serviceapplication->autorestart = strdup(coltxt[i]);
626                         else
627                                 info->manifest_info->serviceapplication->autorestart = NULL;
628                 } else if (strcmp(colname[i], "package") == 0 ){
629                         if (coltxt[i])
630                                 info->manifest_info->serviceapplication->package = strdup(coltxt[i]);
631                         else
632                                 info->manifest_info->serviceapplication->package = NULL;
633                 } else if (strcmp(colname[i], "app_icon") == 0) {
634                         if (coltxt[i])
635                                 info->manifest_info->serviceapplication->icon->text = strdup(coltxt[i]);
636                         else
637                                 info->manifest_info->serviceapplication->icon->text = NULL;
638                 } else if (strcmp(colname[i], "app_label") == 0 ) {
639                         if (coltxt[i])
640                                 info->manifest_info->serviceapplication->label->text = strdup(coltxt[i]);
641                         else
642                                 info->manifest_info->serviceapplication->label->text = NULL;
643                 } else if (strcmp(colname[i], "app_locale") == 0 ) {
644                         if (coltxt[i]) {
645                                 info->manifest_info->serviceapplication->icon->lang = strdup(coltxt[i]);
646                                 info->manifest_info->serviceapplication->label->lang = strdup(coltxt[i]);
647                         }
648                         else {
649                                 info->manifest_info->serviceapplication->icon->lang = NULL;
650                                 info->manifest_info->serviceapplication->label->lang = NULL;
651                         }
652                 } else
653                         continue;
654         }
655         return 0;
656 }
657
658 static int __allapp_list_cb(void *data, int ncols, char **coltxt, char **colname)
659 {
660         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
661         int i = 0;
662         int j = 0;
663         uiapplication_x *uiapp = NULL;
664         serviceapplication_x *svcapp = NULL;
665         for(j = 0; j < ncols; j++)
666         {
667                 if (strcmp(colname[j], "app_component") == 0) {
668                         if (coltxt[j]) {
669                                 if (strcmp(coltxt[j], "uiapp") == 0) {
670                                         uiapp = calloc(1, sizeof(uiapplication_x));
671                                         if (uiapp == NULL) {
672                                                 _LOGE("Out of Memory!!!\n");
673                                                 return -1;
674                                         }
675                                         LISTADD(info->manifest_info->uiapplication, uiapp);
676                                         for(i = 0; i < ncols; i++)
677                                         {
678                                                 if (strcmp(colname[i], "app_id") == 0) {
679                                                         if (coltxt[i])
680                                                                 info->manifest_info->uiapplication->appid = strdup(coltxt[i]);
681                                                         else
682                                                                 info->manifest_info->uiapplication->appid = NULL;
683                                                 } else if (strcmp(colname[i], "app_exec") == 0) {
684                                                         if (coltxt[i])
685                                                                 info->manifest_info->uiapplication->exec = strdup(coltxt[i]);
686                                                         else
687                                                                 info->manifest_info->uiapplication->exec = NULL;
688                                                 } else if (strcmp(colname[i], "app_type") == 0 ){
689                                                         if (coltxt[i])
690                                                                 info->manifest_info->uiapplication->type = strdup(coltxt[i]);
691                                                         else
692                                                                 info->manifest_info->uiapplication->type = NULL;
693                                                 } else if (strcmp(colname[i], "app_nodisplay") == 0 ){
694                                                         if (coltxt[i])
695                                                                 info->manifest_info->uiapplication->nodisplay = strdup(coltxt[i]);
696                                                         else
697                                                                 info->manifest_info->uiapplication->nodisplay = NULL;
698                                                 } else if (strcmp(colname[i], "app_multiple") == 0 ){
699                                                         if (coltxt[i])
700                                                                 info->manifest_info->uiapplication->multiple = strdup(coltxt[i]);
701                                                         else
702                                                                 info->manifest_info->uiapplication->multiple = NULL;
703                                                 } else if (strcmp(colname[i], "app_taskmanage") == 0 ){
704                                                         if (coltxt[i])
705                                                                 info->manifest_info->uiapplication->taskmanage = strdup(coltxt[i]);
706                                                         else
707                                                                 info->manifest_info->uiapplication->taskmanage = NULL;
708                                                 } else if (strcmp(colname[i], "app_hwacceleration") == 0 ){
709                                                         if (coltxt[i])
710                                                                 info->manifest_info->uiapplication->hwacceleration = strdup(coltxt[i]);
711                                                         else
712                                                                 info->manifest_info->uiapplication->hwacceleration = NULL;
713                                                 } else if (strcmp(colname[i], "package") == 0 ){
714                                                         if (coltxt[i])
715                                                                 info->manifest_info->uiapplication->package = strdup(coltxt[i]);
716                                                         else
717                                                                 info->manifest_info->uiapplication->package = NULL;
718                                                 } else if (strcmp(colname[i], "app_icon") == 0) {
719                                                         if (coltxt[i])
720                                                                 info->manifest_info->uiapplication->icon->text = strdup(coltxt[i]);
721                                                         else
722                                                                 info->manifest_info->uiapplication->icon->text = NULL;
723                                                 } else if (strcmp(colname[i], "app_label") == 0 ) {
724                                                         if (coltxt[i])
725                                                                 info->manifest_info->uiapplication->label->text = strdup(coltxt[i]);
726                                                         else
727                                                                 info->manifest_info->uiapplication->label->text = NULL;
728                                                 } else if (strcmp(colname[i], "app_locale") == 0 ) {
729                                                         if (coltxt[i]) {
730                                                                 info->manifest_info->uiapplication->icon->lang = strdup(coltxt[i]);
731                                                                 info->manifest_info->uiapplication->label->lang = strdup(coltxt[i]);
732                                                         }
733                                                         else {
734                                                                 info->manifest_info->uiapplication->icon->lang = NULL;
735                                                                 info->manifest_info->uiapplication->label->lang = NULL;
736                                                         }
737                                                 } else
738                                                         continue;
739                                         }
740                                 } else {
741                                         svcapp = calloc(1, sizeof(serviceapplication_x));
742                                         if (svcapp == NULL) {
743                                                 _LOGE("Out of Memory!!!\n");
744                                                 return -1;
745                                         }
746                                         LISTADD(info->manifest_info->serviceapplication, svcapp);
747                                         for(i = 0; i < ncols; i++)
748                                         {
749                                                 if (strcmp(colname[i], "app_id") == 0) {
750                                                         if (coltxt[i])
751                                                                 info->manifest_info->serviceapplication->appid = strdup(coltxt[i]);
752                                                         else
753                                                                 info->manifest_info->serviceapplication->appid = NULL;
754                                                 } else if (strcmp(colname[i], "app_exec") == 0) {
755                                                         if (coltxt[i])
756                                                                 info->manifest_info->serviceapplication->exec = strdup(coltxt[i]);
757                                                         else
758                                                                 info->manifest_info->serviceapplication->exec = NULL;
759                                                 } else if (strcmp(colname[i], "app_type") == 0 ){
760                                                         if (coltxt[i])
761                                                                 info->manifest_info->serviceapplication->type = strdup(coltxt[i]);
762                                                         else
763                                                                 info->manifest_info->serviceapplication->type = NULL;
764                                                 } else if (strcmp(colname[i], "app_onboot") == 0 ){
765                                                         if (coltxt[i])
766                                                                 info->manifest_info->serviceapplication->onboot = strdup(coltxt[i]);
767                                                         else
768                                                                 info->manifest_info->serviceapplication->onboot = NULL;
769                                                 } else if (strcmp(colname[i], "app_autorestart") == 0 ){
770                                                         if (coltxt[i])
771                                                                 info->manifest_info->serviceapplication->autorestart = strdup(coltxt[i]);
772                                                         else
773                                                                 info->manifest_info->serviceapplication->autorestart = NULL;
774                                                 } else if (strcmp(colname[i], "package") == 0 ){
775                                                         if (coltxt[i])
776                                                                 info->manifest_info->serviceapplication->package = strdup(coltxt[i]);
777                                                         else
778                                                                 info->manifest_info->serviceapplication->package = NULL;
779                                                 } else if (strcmp(colname[i], "app_icon") == 0) {
780                                                         if (coltxt[i])
781                                                                 info->manifest_info->serviceapplication->icon->text = strdup(coltxt[i]);
782                                                         else
783                                                                 info->manifest_info->serviceapplication->icon->text = NULL;
784                                                 } else if (strcmp(colname[i], "app_label") == 0 ) {
785                                                         if (coltxt[i])
786                                                                 info->manifest_info->serviceapplication->label->text = strdup(coltxt[i]);
787                                                         else
788                                                                 info->manifest_info->serviceapplication->label->text = NULL;
789                                                 } else if (strcmp(colname[i], "app_locale") == 0 ) {
790                                                         if (coltxt[i]) {
791                                                                 info->manifest_info->serviceapplication->icon->lang = strdup(coltxt[i]);
792                                                                 info->manifest_info->serviceapplication->label->lang = strdup(coltxt[i]);
793                                                         }
794                                                         else {
795                                                                 info->manifest_info->serviceapplication->icon->lang = NULL;
796                                                                 info->manifest_info->serviceapplication->label->lang = NULL;
797                                                         }
798                                                 } else
799                                                         continue;
800                                         }
801                                 }
802                         }
803                 } else
804                         continue;
805         }
806
807         return 0;
808 }
809
810
811
812 static int __validate_cb(void *data, int ncols, char **coltxt, char **colname)
813 {
814         int *p = (int*)data;
815         *p = atoi(coltxt[0]);
816         return 0;
817 }
818
819 static int __pkginfo_cb(void *data, int ncols, char **coltxt, char **colname)
820 {
821         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)data;
822         int i = 0;
823         author_x *author = NULL;
824         icon_x *icon = NULL;
825         label_x *label = NULL;
826         description_x *description = NULL;
827
828         author = calloc(1, sizeof(author_x));
829         LISTADD(info->manifest_info->author, author);
830         icon = calloc(1, sizeof(icon_x));
831         LISTADD(info->manifest_info->icon, icon);
832         label = calloc(1, sizeof(label_x));
833         LISTADD(info->manifest_info->label, label);
834         description = calloc(1, sizeof(description_x));
835         LISTADD(info->manifest_info->description, description);
836         for(i = 0; i < ncols; i++)
837         {
838                 if (strcmp(colname[i], "package_version") == 0) {
839                         if (coltxt[i])
840                                 info->manifest_info->version = strdup(coltxt[i]);
841                         else
842                                 info->manifest_info->version = NULL;
843                 } else if (strcmp(colname[i], "package_type") == 0) {
844                         if (coltxt[i])
845                                 info->manifest_info->type = strdup(coltxt[i]);
846                         else
847                                 info->manifest_info->type = NULL;
848                 } else if (strcmp(colname[i], "install_location") == 0) {
849                         if (coltxt[i])
850                                 info->manifest_info->installlocation = strdup(coltxt[i]);
851                         else
852                                 info->manifest_info->installlocation = NULL;
853                 } else if (strcmp(colname[i], "package_size") == 0) {
854                         if (coltxt[i])
855                                 info->manifest_info->package_size = strdup(coltxt[i]);
856                         else
857                                 info->manifest_info->package_size = NULL;
858                 } else if (strcmp(colname[i], "author_email") == 0 ){
859                         if (coltxt[i])
860                                 info->manifest_info->author->email = strdup(coltxt[i]);
861                         else
862                                 info->manifest_info->author->email = NULL;
863                 } else if (strcmp(colname[i], "author_href") == 0 ){
864                         if (coltxt[i])
865                                 info->manifest_info->author->href = strdup(coltxt[i]);
866                         else
867                                 info->manifest_info->author->href = NULL;
868                 } else if (strcmp(colname[i], "package_label") == 0 ){
869                         if (coltxt[i])
870                                 info->manifest_info->label->text = strdup(coltxt[i]);
871                         else
872                                 info->manifest_info->label->text = NULL;
873                 } else if (strcmp(colname[i], "package_icon") == 0 ){
874                         if (coltxt[i])
875                                 info->manifest_info->icon->text = strdup(coltxt[i]);
876                         else
877                                 info->manifest_info->icon->text = NULL;
878                 } else if (strcmp(colname[i], "package_description") == 0 ){
879                         if (coltxt[i])
880                                 info->manifest_info->description->text = strdup(coltxt[i]);
881                         else
882                                 info->manifest_info->description->text = NULL;
883                 } else if (strcmp(colname[i], "package_author") == 0 ){
884                         if (coltxt[i])
885                                 info->manifest_info->author->text = strdup(coltxt[i]);
886                         else
887                                 info->manifest_info->author->text = NULL;
888                 } else if (strcmp(colname[i], "package_removable") == 0 ){
889                         if (coltxt[i])
890                                 info->manifest_info->removable = strdup(coltxt[i]);
891                         else
892                                 info->manifest_info->removable = NULL;
893                 } else if (strcmp(colname[i], "package_preload") == 0 ){
894                         if (coltxt[i])
895                                 info->manifest_info->preload = strdup(coltxt[i]);
896                         else
897                                 info->manifest_info->preload = NULL;
898                 } else if (strcmp(colname[i], "package_readonly") == 0 ){
899                         if (coltxt[i])
900                                 info->manifest_info->readonly = strdup(coltxt[i]);
901                         else
902                                 info->manifest_info->readonly = NULL;
903                 } else if (strcmp(colname[i], "installed_time") == 0 ){
904                         if (coltxt[i])
905                                 info->manifest_info->installed_time = strdup(coltxt[i]);
906                         else
907                                 info->manifest_info->installed_time = NULL;
908                 } else if (strcmp(colname[i], "mainapp_id") == 0 ){
909                         if (coltxt[i])
910                                 info->manifest_info->mainapp_id = strdup(coltxt[i]);
911                         else
912                                 info->manifest_info->mainapp_id = NULL;
913
914                 } else if (strcmp(colname[i], "package_locale") == 0 ){
915                         if (coltxt[i]) {
916                                 info->manifest_info->author->lang = strdup(coltxt[i]);
917                                 info->manifest_info->icon->lang = strdup(coltxt[i]);
918                                 info->manifest_info->label->lang = strdup(coltxt[i]);
919                                 info->manifest_info->description->lang = strdup(coltxt[i]);
920                         }
921                         else {
922                                 info->manifest_info->author->lang = NULL;
923                                 info->manifest_info->icon->lang = NULL;
924                                 info->manifest_info->label->lang = NULL;
925                                 info->manifest_info->description->lang = NULL;
926                         }
927                 } else
928                         continue;
929         }
930         return 0;
931 }
932
933
934 static pkgmgrinfo_app_component __appcomponent_convert(const char *comp)
935 {
936         if ( strcasecmp(comp, "uiapp") == 0)
937                 return PMINFO_UI_APP;
938         else if ( strcasecmp(comp, "svcapp") == 0)
939                 return PMINFO_SVC_APP;
940         else
941                 return -1;
942 }
943
944 static int __delete_certinfo_cb(void *data, int ncols, char **coltxt, char **colname)
945 {
946         const char *pkgid = (const char *)data;
947         int i = 0;
948         char *error_message = NULL;
949         int ret =0;
950         char query[MAX_QUERY_LEN] = {'\0'};
951         pkgmgr_instcertinfo_x *certinfo = NULL;
952         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
953         if (certinfo == NULL) {
954                 _LOGE("Out of Memory!!!\n");
955                 return PMINFO_R_ERROR;
956         }
957         for (i  = 0; i < ncols; i++) {
958                 if (strcmp(colname[i], "package") == 0) {
959                         if (coltxt[i])
960                                 certinfo->pkgid = coltxt[i];
961                 } else if (strcmp(colname[i], "author_signer_cert") == 0) {
962                         if (coltxt[i]) {
963                                 if (strcmp(coltxt[i], pkgid) == 0) {
964                                         if (gflag[PMINFO_AUTHOR_SIGNER_CERT] && gpkgcert[PMINFO_AUTHOR_SIGNER_CERT]) {
965                                                 certinfo->auth_signer_cert = strdup(gpkgcert[PMINFO_AUTHOR_SIGNER_CERT]);
966                                                 continue;
967                                         }
968                                         snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info " \
969                                                 "where package='%s'", pkgid);
970                                         if (SQLITE_OK !=
971                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
972                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
973                                                        error_message);
974                                                 sqlite3_free(error_message);
975                                                 ret = PMINFO_R_ERROR;
976                                                 goto err;
977                                         }
978                                         gpkgcert[PMINFO_AUTHOR_SIGNER_CERT] = strdup(certinfo->pkgid);
979                                         gflag[PMINFO_AUTHOR_SIGNER_CERT] = 1;
980                                 } else {
981                                         certinfo->auth_signer_cert = strdup(coltxt[i]);
982                                 }
983                         }
984                         continue;
985                 } else if (strcmp(colname[i], "author_im_cert") == 0) {
986                         if (coltxt[i]) {
987                                 if (strcmp(coltxt[i], pkgid) == 0) {
988                                         if (gflag[PMINFO_AUTHOR_INTERMEDIATE_CERT] && gpkgcert[PMINFO_AUTHOR_INTERMEDIATE_CERT]) {
989                                                 certinfo->auth_im_cert = strdup(gpkgcert[PMINFO_AUTHOR_INTERMEDIATE_CERT]);
990                                                 continue;
991                                         }
992                                         snprintf(query, MAX_QUERY_LEN, "select author_im_cert from package_cert_info " \
993                                                 "where package='%s'", pkgid);
994                                         if (SQLITE_OK !=
995                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
996                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
997                                                        error_message);
998                                                 sqlite3_free(error_message);
999                                                 ret = PMINFO_R_ERROR;
1000                                                 goto err;
1001                                         }
1002                                         gpkgcert[PMINFO_AUTHOR_INTERMEDIATE_CERT] = strdup(certinfo->pkgid);
1003                                         gflag[PMINFO_AUTHOR_INTERMEDIATE_CERT] = 1;
1004                                 } else {
1005                                         certinfo->auth_im_cert = strdup(coltxt[i]);
1006                                 }
1007                         }
1008                         continue;
1009                 } else if (strcmp(colname[i], "author_root_cert") == 0) {
1010                         if (coltxt[i]) {
1011                                 if (strcmp(coltxt[i], pkgid) == 0) {
1012                                         if (gflag[PMINFO_AUTHOR_ROOT_CERT] && gpkgcert[PMINFO_AUTHOR_ROOT_CERT]) {
1013                                                 certinfo->auth_root_cert = strdup(gpkgcert[PMINFO_AUTHOR_ROOT_CERT]);
1014                                                 continue;
1015                                         }
1016                                         snprintf(query, MAX_QUERY_LEN, "select author_root_cert from package_cert_info " \
1017                                                 "where package='%s'", pkgid);
1018                                         if (SQLITE_OK !=
1019                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
1020                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
1021                                                        error_message);
1022                                                 sqlite3_free(error_message);
1023                                                 ret = PMINFO_R_ERROR;
1024                                                 goto err;
1025                                         }
1026                                         gpkgcert[PMINFO_AUTHOR_ROOT_CERT] = strdup(certinfo->pkgid);
1027                                         gflag[PMINFO_AUTHOR_ROOT_CERT] = 1;
1028                                 } else {
1029                                         certinfo->auth_root_cert = strdup(coltxt[i]);
1030                                 }
1031                         }
1032                         continue;
1033                 } else if (strcmp(colname[i], "dist_signer_cert") == 0 ) {
1034                         if (coltxt[i]) {
1035                                 if (strcmp(coltxt[i], pkgid) == 0) {
1036                                         if (gflag[PMINFO_DISTRIBUTOR_SIGNER_CERT] && gpkgcert[PMINFO_DISTRIBUTOR_SIGNER_CERT]) {
1037                                                 certinfo->dist_signer_cert = strdup(gpkgcert[PMINFO_DISTRIBUTOR_SIGNER_CERT]);
1038                                                 continue;
1039                                         }
1040                                         snprintf(query, MAX_QUERY_LEN, "select dist_signer_cert from package_cert_info " \
1041                                                 "where package='%s'", pkgid);
1042                                         if (SQLITE_OK !=
1043                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
1044                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
1045                                                        error_message);
1046                                                 sqlite3_free(error_message);
1047                                                 ret = PMINFO_R_ERROR;
1048                                                 goto err;
1049                                         }
1050                                         gpkgcert[PMINFO_DISTRIBUTOR_SIGNER_CERT] = strdup(certinfo->pkgid);
1051                                         gflag[PMINFO_DISTRIBUTOR_SIGNER_CERT] = 1;
1052                                 } else {
1053                                         certinfo->dist_signer_cert = strdup(coltxt[i]);
1054                                 }
1055                         }
1056                         continue;
1057                 } else if (strcmp(colname[i], "dist_im_cert") == 0 ) {
1058                         if (coltxt[i]) {
1059                                 if (strcmp(coltxt[i], pkgid) == 0) {
1060                                         if (gflag[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] && gpkgcert[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT]) {
1061                                                 certinfo->dist_im_cert = strdup(gpkgcert[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT]);
1062                                                 continue;
1063                                         }
1064                                         snprintf(query, MAX_QUERY_LEN, "select dist_im_cert from package_cert_info " \
1065                                                 "where package='%s'", pkgid);
1066                                         if (SQLITE_OK !=
1067                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
1068                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
1069                                                        error_message);
1070                                                 sqlite3_free(error_message);
1071                                                 ret = PMINFO_R_ERROR;
1072                                                 goto err;
1073                                         }
1074                                         gpkgcert[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] = strdup(certinfo->pkgid);
1075                                         gflag[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] = 1;
1076                                 } else {
1077                                         certinfo->dist_im_cert = strdup(coltxt[i]);
1078                                 }
1079                         }
1080                         continue;
1081                 } else if (strcmp(colname[i], "dist_root_cert") == 0 ) {
1082                         if (coltxt[i]) {
1083                                 if (strcmp(coltxt[i], pkgid) == 0) {
1084                                         if (gflag[PMINFO_DISTRIBUTOR_ROOT_CERT] && gpkgcert[PMINFO_DISTRIBUTOR_ROOT_CERT]) {
1085                                                 certinfo->dist_root_cert = strdup(gpkgcert[PMINFO_DISTRIBUTOR_ROOT_CERT]);
1086                                                 continue;
1087                                         }
1088                                         snprintf(query, MAX_QUERY_LEN, "select dist_root_cert from package_cert_info " \
1089                                                 "where package='%s'", pkgid);
1090                                         if (SQLITE_OK !=
1091                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
1092                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
1093                                                        error_message);
1094                                                 sqlite3_free(error_message);
1095                                                 ret = PMINFO_R_ERROR;
1096                                                 goto err;
1097                                         }
1098                                         gpkgcert[PMINFO_DISTRIBUTOR_ROOT_CERT] = strdup(certinfo->pkgid);
1099                                         gflag[PMINFO_DISTRIBUTOR_ROOT_CERT] = 1;
1100                                 } else {
1101                                         certinfo->dist_root_cert = strdup(coltxt[i]);
1102                                 }
1103                         }
1104                         continue;
1105                 } else if (strcmp(colname[i], "dist2_signer_cert") == 0 ) {
1106                         if (coltxt[i]) {
1107                                 if (strcmp(coltxt[i], pkgid) == 0) {
1108                                         if (gflag[PMINFO_DISTRIBUTOR2_SIGNER_CERT] && gpkgcert[PMINFO_DISTRIBUTOR2_SIGNER_CERT]) {
1109                                                 certinfo->dist2_signer_cert = strdup(gpkgcert[PMINFO_DISTRIBUTOR2_SIGNER_CERT]);
1110                                                 continue;
1111                                         }
1112                                         snprintf(query, MAX_QUERY_LEN, "select dist2_signer_cert from package_cert_info " \
1113                                                 "where package='%s'", pkgid);
1114                                         if (SQLITE_OK !=
1115                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
1116                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
1117                                                        error_message);
1118                                                 sqlite3_free(error_message);
1119                                                 ret = PMINFO_R_ERROR;
1120                                                 goto err;
1121                                         }
1122                                         gpkgcert[PMINFO_DISTRIBUTOR2_SIGNER_CERT] = strdup(certinfo->pkgid);
1123                                         gflag[PMINFO_DISTRIBUTOR2_SIGNER_CERT] = 1;
1124                                 } else {
1125                                         certinfo->dist2_signer_cert = strdup(coltxt[i]);
1126                                 }
1127                         }
1128                         continue;
1129                 } else if (strcmp(colname[i], "dist2_im_cert") == 0 ) {
1130                         if (coltxt[i]) {
1131                                 if (strcmp(coltxt[i], pkgid) == 0) {
1132                                         if (gflag[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] && gpkgcert[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT]) {
1133                                                 certinfo->dist2_im_cert = strdup(gpkgcert[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT]);
1134                                                 continue;
1135                                         }
1136                                         snprintf(query, MAX_QUERY_LEN, "select dist2_im_cert from package_cert_info " \
1137                                                 "where package='%s'", pkgid);
1138                                         if (SQLITE_OK !=
1139                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
1140                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
1141                                                        error_message);
1142                                                 sqlite3_free(error_message);
1143                                                 ret = PMINFO_R_ERROR;
1144                                                 goto err;
1145                                         }
1146                                         gpkgcert[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] = strdup(certinfo->pkgid);
1147                                         gflag[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] = 1;
1148                                 } else {
1149                                         certinfo->dist2_im_cert = strdup(coltxt[i]);
1150                                 }
1151                         }
1152                         continue;
1153                 } else if (strcmp(colname[i], "dist2_root_cert") == 0 ) {
1154                         if (coltxt[i]) {
1155                                 if (strcmp(coltxt[i], pkgid) == 0) {
1156                                         if (gflag[PMINFO_DISTRIBUTOR2_ROOT_CERT] && gpkgcert[PMINFO_DISTRIBUTOR2_ROOT_CERT]) {
1157                                                 certinfo->dist2_root_cert = strdup(gpkgcert[PMINFO_DISTRIBUTOR2_ROOT_CERT]);
1158                                                 continue;
1159                                         }
1160                                         snprintf(query, MAX_QUERY_LEN, "select dist2_root_cert from package_cert_info " \
1161                                                 "where package='%s'", pkgid);
1162                                         if (SQLITE_OK !=
1163                                             sqlite3_exec(cert_db, query, __certinfo_cb, (void *)certinfo, &error_message)) {
1164                                                 _LOGE("Don't execute query = %s error message = %s\n", query,
1165                                                        error_message);
1166                                                 sqlite3_free(error_message);
1167                                                 ret = PMINFO_R_ERROR;
1168                                                 goto err;
1169                                         }
1170                                         gpkgcert[PMINFO_DISTRIBUTOR2_ROOT_CERT] = strdup(certinfo->pkgid);
1171                                         gflag[PMINFO_DISTRIBUTOR2_ROOT_CERT] = 1;
1172                                 } else {
1173                                         certinfo->dist2_root_cert = strdup(coltxt[i]);
1174                                 }
1175                         }
1176                         continue;
1177                 }
1178         }
1179         /*Update cert info db*/
1180         pkgmgrinfo_save_certinfo(certinfo->pkgid, (void *)certinfo);
1181         ret = PMINFO_R_OK;
1182 err:
1183         if (certinfo->auth_signer_cert) {
1184                 free(certinfo->auth_signer_cert);
1185                 certinfo->auth_signer_cert = NULL;
1186         }
1187         if (certinfo->auth_im_cert) {
1188                 free(certinfo->auth_im_cert);
1189                 certinfo->auth_im_cert = NULL;
1190         }
1191         if (certinfo->auth_root_cert) {
1192                 free(certinfo->auth_root_cert);
1193                 certinfo->auth_root_cert = NULL;
1194         }
1195         if (certinfo->dist_signer_cert) {
1196                 free(certinfo->dist_signer_cert);
1197                 certinfo->dist_signer_cert = NULL;
1198         }
1199         if (certinfo->dist_im_cert) {
1200                 free(certinfo->dist_im_cert);
1201                 certinfo->dist_im_cert = NULL;
1202         }
1203         if (certinfo->dist_root_cert) {
1204                 free(certinfo->dist_root_cert);
1205                 certinfo->dist_root_cert = NULL;
1206         }
1207         if (certinfo->dist2_signer_cert) {
1208                 free(certinfo->dist2_signer_cert);
1209                 certinfo->dist2_signer_cert = NULL;
1210         }
1211         if (certinfo->dist2_im_cert) {
1212                 free(certinfo->dist2_im_cert);
1213                 certinfo->dist2_im_cert = NULL;
1214         }
1215         if (certinfo->dist2_root_cert) {
1216                 free(certinfo->dist2_root_cert);
1217                 certinfo->dist2_root_cert = NULL;
1218         }
1219         free(certinfo);
1220         certinfo = NULL;
1221         return ret;
1222 }
1223
1224 static int __certinfo_cb(void *data, int ncols, char **coltxt, char **colname)
1225 {
1226         pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)data;
1227         int i = 0;
1228         for(i = 0; i < ncols; i++)
1229         {
1230                 if (strcmp(colname[i], "package") == 0) {
1231                         if (coltxt[i])
1232                                 info->pkgid = strdup(coltxt[i]);
1233                         else
1234                                 info->pkgid = NULL;
1235                 } else if (strcmp(colname[i], "author_signer_cert") == 0) {
1236                         if (coltxt[i])
1237                                 info->auth_signer_cert = strdup(coltxt[i]);
1238                         else
1239                                 info->auth_signer_cert = NULL;
1240                 } else if (strcmp(colname[i], "author_im_cert") == 0) {
1241                         if (coltxt[i])
1242                                 info->auth_im_cert = strdup(coltxt[i]);
1243                         else
1244                                 info->auth_im_cert = NULL;
1245                 } else if (strcmp(colname[i], "author_root_cert") == 0) {
1246                         if (coltxt[i])
1247                                 info->auth_root_cert = strdup(coltxt[i]);
1248                         else
1249                                 info->auth_root_cert = NULL;
1250                 } else if (strcmp(colname[i], "dist_signer_cert") == 0 ){
1251                         if (coltxt[i])
1252                                 info->dist_signer_cert = strdup(coltxt[i]);
1253                         else
1254                                 info->dist_signer_cert = NULL;
1255                 } else if (strcmp(colname[i], "dist_im_cert") == 0 ){
1256                         if (coltxt[i])
1257                                 info->dist_im_cert = strdup(coltxt[i]);
1258                         else
1259                                 info->dist_im_cert = NULL;
1260                 } else if (strcmp(colname[i], "dist_root_cert") == 0 ){
1261                         if (coltxt[i])
1262                                 info->dist_root_cert = strdup(coltxt[i]);
1263                         else
1264                                 info->dist_root_cert = NULL;
1265                 } else if (strcmp(colname[i], "dist2_signer_cert") == 0 ){
1266                         if (coltxt[i])
1267                                 info->dist2_signer_cert = strdup(coltxt[i]);
1268                         else
1269                                 info->dist2_signer_cert = NULL;
1270                 } else if (strcmp(colname[i], "dist2_im_cert") == 0 ){
1271                         if (coltxt[i])
1272                                 info->dist2_im_cert = strdup(coltxt[i]);
1273                         else
1274                                 info->dist2_im_cert = NULL;
1275                 } else if (strcmp(colname[i], "dist2_root_cert") == 0 ){
1276                         if (coltxt[i])
1277                                 info->dist2_root_cert = strdup(coltxt[i]);
1278                         else
1279                                 info->dist2_root_cert = NULL;
1280                 } else
1281                         continue;
1282         }
1283         return 0;
1284 }
1285
1286 static int __appinfo_cb(void *data, int ncols, char **coltxt, char **colname)
1287 {
1288         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)data;
1289         int i = 0;
1290         icon_x *icon = NULL;
1291         label_x *label = NULL;
1292         category_x *category = NULL;
1293
1294         switch (info->app_component) {
1295         case PMINFO_UI_APP:
1296                 icon = calloc(1, sizeof(icon_x));
1297                 LISTADD(info->uiapp_info->icon, icon);
1298                 label = calloc(1, sizeof(label_x));
1299                 LISTADD(info->uiapp_info->label, label);
1300                 category = calloc(1, sizeof(category_x));
1301                 LISTADD(info->uiapp_info->category, category);
1302                 for(i = 0; i < ncols; i++)
1303                 {
1304                         if (strcmp(colname[i], "app_id") == 0) {
1305                                 if (coltxt[i])
1306                                         info->uiapp_info->appid = strdup(coltxt[i]);
1307                                 else
1308                                         info->uiapp_info->appid = NULL;
1309                         } else if (strcmp(colname[i], "app_exec") == 0) {
1310                                 if (coltxt[i])
1311                                         info->uiapp_info->exec = strdup(coltxt[i]);
1312                                 else
1313                                         info->uiapp_info->exec = NULL;
1314                         } else if (strcmp(colname[i], "app_nodisplay") == 0) {
1315                                 if (coltxt[i])
1316                                         info->uiapp_info->nodisplay = strdup(coltxt[i]);
1317                                 else
1318                                         info->uiapp_info->nodisplay = NULL;
1319                         } else if (strcmp(colname[i], "app_type") == 0 ) {
1320                                 if (coltxt[i])
1321                                         info->uiapp_info->type = strdup(coltxt[i]);
1322                                 else
1323                                         info->uiapp_info->type = NULL;
1324                         } else if (strcmp(colname[i], "app_icon") == 0) {
1325                                 if (coltxt[i])
1326                                         info->uiapp_info->icon->text = strdup(coltxt[i]);
1327                                 else
1328                                         info->uiapp_info->icon->text = NULL;
1329                         } else if (strcmp(colname[i], "app_label") == 0 ) {
1330                                 if (coltxt[i])
1331                                         info->uiapp_info->label->text = strdup(coltxt[i]);
1332                                 else
1333                                         info->uiapp_info->label->text = NULL;
1334                         } else if (strcmp(colname[i], "app_multiple") == 0 ) {
1335                                 if (coltxt[i])
1336                                         info->uiapp_info->multiple = strdup(coltxt[i]);
1337                                 else
1338                                         info->uiapp_info->multiple = NULL;
1339                         } else if (strcmp(colname[i], "app_taskmanage") == 0 ) {
1340                                 if (coltxt[i])
1341                                         info->uiapp_info->taskmanage = strdup(coltxt[i]);
1342                                 else
1343                                         info->uiapp_info->taskmanage = NULL;
1344                         } else if (strcmp(colname[i], "app_hwacceleration") == 0 ) {
1345                                 if (coltxt[i])
1346                                         info->uiapp_info->hwacceleration = strdup(coltxt[i]);
1347                                 else
1348                                         info->uiapp_info->hwacceleration = NULL;
1349                         } else if (strcmp(colname[i], "category") == 0 ) {
1350                                 if (coltxt[i])
1351                                         info->uiapp_info->category->name = strdup(coltxt[i]);
1352                                 else
1353                                         info->uiapp_info->category->name = NULL;
1354                         } else if (strcmp(colname[i], "app_locale") == 0 ) {
1355                                 if (coltxt[i]) {
1356                                         info->uiapp_info->icon->lang = strdup(coltxt[i]);
1357                                         info->uiapp_info->label->lang = strdup(coltxt[i]);
1358                                 }
1359                                 else {
1360                                         info->uiapp_info->icon->lang = NULL;
1361                                         info->uiapp_info->label->lang = NULL;
1362                                 }
1363                         } else
1364                                 continue;
1365                 }
1366                 break;
1367         case PMINFO_SVC_APP:
1368                 icon = calloc(1, sizeof(icon_x));
1369                 LISTADD(info->svcapp_info->icon, icon);
1370                 label = calloc(1, sizeof(label_x));
1371                 LISTADD(info->svcapp_info->label, label);
1372                 category = calloc(1, sizeof(category_x));
1373                 LISTADD(info->svcapp_info->category, category);
1374                 for(i = 0; i < ncols; i++)
1375                 {
1376                         if (strcmp(colname[i], "app_id") == 0) {
1377                                 if (coltxt[i])
1378                                         info->svcapp_info->appid = strdup(coltxt[i]);
1379                                 else
1380                                         info->svcapp_info->appid = NULL;
1381                         } else if (strcmp(colname[i], "app_exec") == 0) {
1382                                 if (coltxt[i])
1383                                         info->svcapp_info->exec = strdup(coltxt[i]);
1384                                 else
1385                                         info->svcapp_info->exec = NULL;
1386                         } else if (strcmp(colname[i], "app_icon") == 0) {
1387                                 if (coltxt[i])
1388                                         info->svcapp_info->icon->text = strdup(coltxt[i]);
1389                                 else
1390                                         info->svcapp_info->icon->text = NULL;
1391                         } else if (strcmp(colname[i], "app_label") == 0 ) {
1392                                 if (coltxt[i])
1393                                         info->svcapp_info->label->text = strdup(coltxt[i]);
1394                                 else
1395                                         info->svcapp_info->label->text = NULL;
1396                         } else if (strcmp(colname[i], "app_type") == 0 ) {
1397                                 if (coltxt[i])
1398                                         info->svcapp_info->type = strdup(coltxt[i]);
1399                                 else
1400                                         info->svcapp_info->type = NULL;
1401                         } else if (strcmp(colname[i], "app_onboot") == 0 ) {
1402                                 if (coltxt[i])
1403                                         info->svcapp_info->onboot = strdup(coltxt[i]);
1404                                 else
1405                                         info->svcapp_info->onboot = NULL;
1406                         } else if (strcmp(colname[i], "app_autorestart") == 0 ) {
1407                                 if (coltxt[i])
1408                                         info->svcapp_info->autorestart = strdup(coltxt[i]);
1409                                 else
1410                                         info->svcapp_info->autorestart = NULL;
1411                         } else if (strcmp(colname[i], "category") == 0 ) {
1412                                 if (coltxt[i])
1413                                         info->svcapp_info->category->name = strdup(coltxt[i]);
1414                                 else
1415                                         info->svcapp_info->category->name = NULL;
1416                         } else if (strcmp(colname[i], "app_locale") == 0 ) {
1417                                 if (coltxt[i]) {
1418                                         info->svcapp_info->icon->lang = strdup(coltxt[i]);
1419                                         info->svcapp_info->label->lang = strdup(coltxt[i]);
1420                                 }
1421                                 else {
1422                                         info->svcapp_info->icon->lang = NULL;
1423                                         info->svcapp_info->label->lang = NULL;
1424                                 }
1425                         } else
1426                                 continue;
1427                 }
1428                 break;
1429         default:
1430                 break;
1431         }
1432
1433         return 0;
1434 }
1435
1436
1437 static int __appcomponent_cb(void *data, int ncols, char **coltxt, char **colname)
1438 {
1439         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)data;
1440         int i = 0;
1441         for(i = 0; i < ncols; i++)
1442         {
1443                 if (strcmp(colname[i], "app_component") == 0) {
1444                         info->app_component = __appcomponent_convert(coltxt[i]);
1445                 } else if (strcmp(colname[i], "package") == 0) {
1446                         info->package = strdup(coltxt[i]);
1447                 }
1448         }
1449
1450         return 0;
1451 }
1452
1453 static int __datacontrol_cb(void *data, int ncols, char **coltxt, char **colname)
1454 {
1455         pkgmgr_datacontrol_x *info = (pkgmgr_datacontrol_x *)data;
1456         int i = 0;
1457         for(i = 0; i < ncols; i++)
1458         {
1459                 if (strcmp(colname[i], "PACKAGE_NAME") == 0) {
1460                         if (coltxt[i])
1461                                 info->appid = strdup(coltxt[i]);
1462                         else
1463                                 info->appid = NULL;
1464                 } else if (strcmp(colname[i], "ACCESS") == 0 ){
1465                         if (coltxt[i])
1466                                 info->access = strdup(coltxt[i]);
1467                         else
1468                                 info->access = NULL;
1469                 } else
1470                         continue;
1471         }
1472         return 0;
1473 }
1474
1475 static int __icon_name_cb(void *data, int ncols, char **coltxt, char **colname)
1476 {
1477         pkgmgr_iconpath_x *icon_name = (pkgmgr_iconpath_x *)data;
1478         int i = 0;
1479         for(i = 0; i < ncols; i++)
1480         {
1481                 if (strcmp(colname[i], "app_icon") == 0) {
1482                         if (coltxt[i])
1483                                 icon_name->iconpath = strdup(coltxt[i]);
1484                         else
1485                                 icon_name->iconpath = NULL;
1486                 } else
1487                         continue;
1488         }
1489         return 0;
1490 }
1491
1492 static int __cert_cb(void *data, int ncols, char **coltxt, char **colname)
1493 {
1494         pkgmgr_cert_x *info = (pkgmgr_cert_x *)data;
1495         int i = 0;
1496
1497         for(i = 0; i < ncols; i++)
1498         {
1499                 if (strcmp(colname[i], "author_signer_cert") == 0) {
1500                         if (coltxt[i])
1501                                 info->certvalue= strdup(coltxt[i]);
1502                         else
1503                                 info->certvalue = NULL;
1504                 } else if (strcmp(colname[i], "package") == 0) {
1505                         if (coltxt[i])
1506                                 info->pkgid= strdup(coltxt[i]);
1507                         else
1508                                 info->pkgid = NULL;
1509                 } else
1510                         continue;
1511         }
1512         return 0;
1513 }
1514
1515 /* get the first locale value*/
1516 static int __fallback_locale_cb(void *data, int ncols, char **coltxt, char **colname)
1517 {
1518         pkgmgr_locale_x *info = (pkgmgr_locale_x *)data;
1519
1520         if (ncols >= 1)
1521                 info->locale = strdup(coltxt[0]);
1522         else
1523                 info->locale = NULL;
1524
1525         return 0;
1526 }
1527
1528 static int __exec_pkginfo_query(char *query, void *data)
1529 {
1530         char *error_message = NULL;
1531         if (SQLITE_OK !=
1532             sqlite3_exec(manifest_db, query, __pkginfo_cb, data, &error_message)) {
1533                 _LOGE("Don't execute query = %s error message = %s\n", query,
1534                        error_message);
1535                 sqlite3_free(error_message);
1536                 return -1;
1537         }
1538         sqlite3_free(error_message);
1539         return 0;
1540 }
1541
1542 static int __exec_certinfo_query(char *query, void *data)
1543 {
1544         char *error_message = NULL;
1545         if (SQLITE_OK !=
1546             sqlite3_exec(cert_db, query, __certinfo_cb, data, &error_message)) {
1547                 _LOGE("Don't execute query = %s error message = %s\n", query,
1548                        error_message);
1549                 sqlite3_free(error_message);
1550                 return -1;
1551         }
1552         sqlite3_free(error_message);
1553         return 0;
1554 }
1555
1556 static int __exec_appcomponent_query(char *query, void *data)
1557 {
1558         char *error_message = NULL;
1559         if (SQLITE_OK !=
1560             sqlite3_exec(manifest_db, query, __appcomponent_cb, data, &error_message)) {
1561                 _LOGE("Don't execute query = %s error message = %s\n", query,
1562                        error_message);
1563                 sqlite3_free(error_message);
1564                 return -1;
1565         }
1566         sqlite3_free(error_message);
1567         return 0;
1568 }
1569
1570
1571 static int __exec_appinfo_query(char *query, void *data)
1572 {
1573         char *error_message = NULL;
1574         if (SQLITE_OK !=
1575             sqlite3_exec(manifest_db, query, __appinfo_cb, data, &error_message)) {
1576                 _LOGE("Don't execute query = %s error message = %s\n", query,
1577                        error_message);
1578                 sqlite3_free(error_message);
1579                 return -1;
1580         }
1581         sqlite3_free(error_message);
1582         return 0;
1583 }
1584
1585 static int __exec_sqlite_query(char *query, sqlite_query_callback callback, void *data)
1586 {
1587         char *error_message = NULL;
1588         if (SQLITE_OK !=
1589             sqlite3_exec(manifest_db, query, callback, data, &error_message)) {
1590                 _LOGE("Don't execute query = %s error message = %s\n", query,
1591                        error_message);
1592                 sqlite3_free(error_message);
1593                 return -1;
1594         }
1595         sqlite3_free(error_message);
1596         return 0;
1597 }
1598
1599
1600 static int __child_element(xmlTextReaderPtr reader, int depth)
1601 {
1602         int ret = xmlTextReaderRead(reader);
1603         int cur = xmlTextReaderDepth(reader);
1604         while (ret == 1) {
1605
1606                 switch (xmlTextReaderNodeType(reader)) {
1607                 case XML_READER_TYPE_ELEMENT:
1608                         if (cur == depth + 1)
1609                                 return 1;
1610                         break;
1611                 case XML_READER_TYPE_TEXT:
1612                         /*text is handled by each function separately*/
1613                         if (cur == depth + 1)
1614                                 return 0;
1615                         break;
1616                 case XML_READER_TYPE_END_ELEMENT:
1617                         if (cur == depth)
1618                                 return 0;
1619                         break;
1620                 default:
1621                         if (cur <= depth)
1622                                 return 0;
1623                         break;
1624                 }
1625                 ret = xmlTextReaderRead(reader);
1626                 cur = xmlTextReaderDepth(reader);
1627         }
1628         return ret;
1629 }
1630
1631 static char *__get_package_from_icon(char *icon)
1632 {
1633         char *package;
1634         char *extension;
1635
1636         retv_if(!icon, NULL);
1637
1638         package = strdup(icon);
1639         retv_if(!package, NULL);
1640         extension = rindex(package, '.');
1641         if (extension) {
1642                 *extension = '\0';
1643         } else {
1644                 _LOGE("cannot extract from icon [%s] to package.", icon);
1645         }
1646
1647         return package;
1648 }
1649
1650 static char *__get_icon_with_path(char *icon)
1651 {
1652         retv_if(!icon, NULL);
1653
1654         if (index(icon, '/') == NULL) {
1655                 char *package;
1656                 char *theme = NULL;
1657                 char *icon_with_path = NULL;
1658                 int len;
1659
1660                 package = __get_package_from_icon(icon);
1661                 retv_if(!package, NULL);
1662
1663                 theme = vconf_get_str("db/setting/theme");
1664                 if (!theme) {
1665                         theme = strdup("default");
1666                         if(!theme) {
1667                                 free(package);
1668                                 return NULL;
1669                         }
1670                 }
1671
1672                 len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
1673                 icon_with_path = malloc(len);
1674                 if(icon_with_path == NULL) {
1675                         _LOGE("(icon_with_path == NULL) return\n");
1676                         free(package);
1677                         free(theme);
1678                         return NULL;
1679                 }
1680
1681                 memset(icon_with_path, 0, len);
1682
1683                 sqlite3_snprintf( len, icon_with_path,"/opt/share/icons/%q/small/%q", theme, icon);
1684                 do {
1685                         if (access(icon_with_path, R_OK) == 0) break;
1686                         sqlite3_snprintf( len, icon_with_path,"/usr/share/icons/%q/small/%q", theme, icon);
1687                         if (access(icon_with_path, R_OK) == 0) break;
1688                         _LOGE("cannot find icon %s", icon_with_path);
1689                         sqlite3_snprintf( len, icon_with_path, "/opt/share/icons/default/small/%q", icon);
1690                         if (access(icon_with_path, R_OK) == 0) break;
1691                         sqlite3_snprintf( len, icon_with_path, "/usr/share/icons/default/small/%q", icon);
1692                         if (access(icon_with_path, R_OK) == 0) break;
1693
1694                         #if 1 /* this will be remove when finish the work for moving icon path */
1695                         _LOGE("icon file must be moved to %s", icon_with_path);
1696                         sqlite3_snprintf( len, icon_with_path,  "/opt/apps/%q/res/icons/%q/small/%q", package, theme, icon);
1697                         if (access(icon_with_path, R_OK) == 0) break;
1698                         sqlite3_snprintf( len, icon_with_path, "/usr/apps/%q/res/icons/%q/small/%q", package, theme, icon);
1699                         if (access(icon_with_path, R_OK) == 0) break;
1700                         _LOGE("cannot find icon %s", icon_with_path);
1701                         sqlite3_snprintf( len, icon_with_path, "/opt/apps/%q/res/icons/default/small/%q", package, icon);
1702                         if (access(icon_with_path, R_OK) == 0) break;
1703                         sqlite3_snprintf( len, icon_with_path, "/usr/apps/%q/res/icons/default/small/%q", package, icon);
1704                         if (access(icon_with_path, R_OK) == 0) break;
1705                         #endif
1706                 } while (0);
1707
1708                 free(theme);
1709                 free(package);
1710
1711                 _LOGD("Icon path : %s ---> %s", icon, icon_with_path);
1712
1713                 return icon_with_path;
1714         } else {
1715                 char* confirmed_icon = NULL;
1716
1717                 confirmed_icon = strdup(icon);
1718                 retv_if(!confirmed_icon, NULL);
1719                 return confirmed_icon;
1720         }
1721 }
1722
1723 static int __check_validation_of_qurey_cb(void *data, int ncols, char **coltxt, char **colname)
1724 {
1725         int *p = (int*)data;
1726         *p = atoi(coltxt[0]);
1727         return 0;
1728 }
1729
1730 static int __check_app_locale_from_app_localized_info_by_exact(const char *appid, const char *locale)
1731 {
1732         int result_query = -1;
1733         char query[MAX_QUERY_LEN];
1734
1735         snprintf(query, MAX_QUERY_LEN, "select exists(select app_locale from package_app_localized_info where app_id='%s' and app_locale='%s')", appid, locale);
1736         __exec_sqlite_query(query, __check_validation_of_qurey_cb, (void *)&result_query);
1737
1738         return result_query;
1739 }
1740
1741 static int __check_app_locale_from_app_localized_info_by_fallback(const char *appid, const char *locale)
1742 {
1743         int result_query = -1;
1744         char wildcard[2] = {'%','\0'};
1745         char query[MAX_QUERY_LEN];
1746         char lang[3] = {'\0'};
1747         strncpy(lang, locale, LANGUAGE_LENGTH);
1748
1749         snprintf(query, MAX_QUERY_LEN, "select exists(select app_locale from package_app_localized_info where app_id='%s' and app_locale like '%s%s')", appid, lang, wildcard);
1750         __exec_sqlite_query(query, __check_validation_of_qurey_cb, (void *)&result_query);
1751
1752         return result_query;
1753 }
1754
1755 static char* __get_app_locale_from_app_localized_info_by_fallback(const char *appid, const char *locale)
1756 {
1757         char wildcard[2] = {'%','\0'};
1758         char lang[3] = {'\0'};
1759         char query[MAX_QUERY_LEN];
1760         char *locale_new = NULL;
1761         pkgmgr_locale_x *info = NULL;
1762
1763         info = (pkgmgr_locale_x *)malloc(sizeof(pkgmgr_locale_x));
1764         if (info == NULL) {
1765                 _LOGE("Out of Memory!!!\n");
1766                 return NULL;
1767         }
1768         memset(info, NULL, sizeof(*info));
1769
1770         strncpy(lang, locale, 2);
1771         snprintf(query, MAX_QUERY_LEN, "select app_locale from package_app_localized_info where app_id='%s' and app_locale like '%s%s'", appid, lang, wildcard);
1772         __exec_sqlite_query(query, __fallback_locale_cb, (void *)info);
1773         locale_new = info->locale;
1774         free(info);
1775
1776         return locale_new;
1777 }
1778
1779 static char* __convert_syslocale_to_manifest_locale(char *syslocale)
1780 {
1781         char *locale = malloc(6);
1782         if (!locale) {
1783                 _LOGE("Malloc Failed\n");
1784                 return NULL;
1785         }
1786
1787         sprintf(locale, "%c%c-%c%c", syslocale[0], syslocale[1], tolower(syslocale[3]), tolower(syslocale[4]));
1788         return locale;
1789 }
1790
1791 static char* __get_app_locale_by_fallback(const char *appid, const char *syslocale)
1792 {
1793         assert(appid);
1794         assert(syslocale);
1795
1796         char *locale = NULL;
1797         char *locale_new = NULL;
1798         int check_result = 0;
1799
1800         locale = __convert_syslocale_to_manifest_locale(syslocale);
1801
1802         /*check exact matching */
1803         check_result = __check_app_locale_from_app_localized_info_by_exact(appid, locale);
1804
1805         /* Exact found */
1806         if (check_result == 1) {
1807                 _LOGD("%s find exact locale(%s)\n", appid, locale);
1808                 return locale;
1809         }
1810
1811         /* fallback matching */
1812         check_result = __check_app_locale_from_app_localized_info_by_fallback(appid, locale);
1813         if(check_result == 1) {
1814                    locale_new = __get_app_locale_from_app_localized_info_by_fallback(appid, locale);
1815                    _LOGD("%s found (%s) language-locale in DB by fallback!\n", appid, locale_new);
1816                    free(locale);
1817                    if (locale_new == NULL)
1818                            locale_new =  strdup(DEFAULT_LOCALE);
1819                    return locale_new;
1820         }
1821
1822         /* default locale */
1823         free(locale);
1824         _LOGD("%s DEFAULT_LOCALE)\n", appid);
1825         return  strdup(DEFAULT_LOCALE);
1826 }
1827
1828 long long _pkgmgr_calculate_dir_size(char *dirname)
1829 {
1830         long long total = 0;
1831         long long ret = 0;
1832         int q = 0; /*quotient*/
1833         int r = 0; /*remainder*/
1834         DIR *dp = NULL;
1835         struct dirent *ep = NULL;
1836         struct stat fileinfo;
1837         char abs_filename[FILENAME_MAX] = { 0, };
1838         if (dirname == NULL) {
1839                 _LOGE("dirname is NULL");
1840                 return PMINFO_R_ERROR;
1841         }
1842         dp = opendir(dirname);
1843         if (dp != NULL) {
1844                 while ((ep = readdir(dp)) != NULL) {
1845                         if (!strcmp(ep->d_name, ".") ||
1846                                 !strcmp(ep->d_name, "..")) {
1847                                 continue;
1848                         }
1849                         snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
1850                                  ep->d_name);
1851                         if (lstat(abs_filename, &fileinfo) < 0)
1852                                 perror(abs_filename);
1853                         else {
1854                                 if (S_ISDIR(fileinfo.st_mode)) {
1855                                         total += fileinfo.st_size;
1856                                         if (strcmp(ep->d_name, ".")
1857                                             && strcmp(ep->d_name, "..")) {
1858                                                 ret = _pkgmgr_calculate_dir_size
1859                                                     (abs_filename);
1860                                                 total = total + ret;
1861                                         }
1862                                 } else if (S_ISLNK(fileinfo.st_mode)) {
1863                                         continue;
1864                                 } else {
1865                                         /*It is a file. Calculate the actual
1866                                         size occupied (in terms of 4096 blocks)*/
1867                                 q = (fileinfo.st_size / BLOCK_SIZE);
1868                                 r = (fileinfo.st_size % BLOCK_SIZE);
1869                                 if (r) {
1870                                         q = q + 1;
1871                                 }
1872                                 total += q * BLOCK_SIZE;
1873                                 }
1874                         }
1875                 }
1876                 (void)closedir(dp);
1877         } else {
1878                 _LOGE("Couldn't open the directory\n");
1879                 return -1;
1880         }
1881         return total;
1882
1883 }
1884
1885 API int pkgmgrinfo_pkginfo_get_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
1886 {
1887         if (pkg_list_cb == NULL) {
1888                 _LOGE("callback function is NULL\n");
1889                 return PMINFO_R_EINVAL;
1890         }
1891         char *error_message = NULL;
1892         int ret = PMINFO_R_OK;
1893         char query[MAX_QUERY_LEN] = {'\0'};
1894         char *syslocale = NULL;
1895         char *locale = NULL;
1896         pkgmgr_pkginfo_x *pkginfo = NULL;
1897         label_x *tmp1 = NULL;
1898         icon_x *tmp2 = NULL;
1899         description_x *tmp3 = NULL;
1900         author_x *tmp4 = NULL;
1901
1902         syslocale = vconf_get_str(VCONFKEY_LANGSET);
1903         if (syslocale == NULL) {
1904                 _LOGE("current locale is NULL\n");
1905                 ret = PMINFO_R_ERROR;
1906                 goto err;
1907         }
1908         locale = __convert_system_locale_to_manifest_locale(syslocale);
1909         if (locale == NULL) {
1910                 _LOGE("manifest locale is NULL\n");
1911                 ret = PMINFO_R_EINVAL;
1912                 goto err;
1913         }
1914
1915         ret = __open_manifest_db();
1916         if (ret == -1) {
1917                 _LOGE("Fail to open manifest DB\n");
1918                 ret = PMINFO_R_ERROR;
1919                 goto err;
1920         }
1921         pkgmgr_pkginfo_x *tmphead = calloc(1, sizeof(pkgmgr_pkginfo_x));
1922         pkgmgr_pkginfo_x *node = NULL;
1923         pkgmgr_pkginfo_x *temp_node = NULL;
1924
1925         snprintf(query, MAX_QUERY_LEN, "select * from package_info");
1926         if (SQLITE_OK !=
1927             sqlite3_exec(manifest_db, query, __pkg_list_cb, (void *)tmphead, &error_message)) {
1928                 _LOGE("Don't execute query = %s error message = %s\n", query,
1929                        error_message);
1930                 sqlite3_free(error_message);
1931                 sqlite3_close(manifest_db);
1932                 ret = PMINFO_R_ERROR;
1933                 goto err;
1934         }
1935
1936         LISTHEAD(tmphead, node);
1937
1938         for(node = node->next; node ; node = node->next) {
1939                 pkginfo = node;
1940
1941                 /*populate manifest_info from DB*/
1942                 snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkginfo->manifest_info->package);
1943                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
1944                 if (ret == -1) {
1945                         _LOGE("Package Info DB Information retrieval failed\n");
1946                         ret = PMINFO_R_ERROR;
1947                         goto err;
1948                 }
1949                 memset(query, '\0', MAX_QUERY_LEN);
1950                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
1951                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, locale);
1952                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
1953                 if (ret == -1) {
1954                         _LOGE("Package Info DB Information retrieval failed\n");
1955                         ret = PMINFO_R_ERROR;
1956                         goto err;
1957                 }
1958                 /*Also store the values corresponding to default locales*/
1959                 memset(query, '\0', MAX_QUERY_LEN);
1960                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
1961                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, DEFAULT_LOCALE);
1962                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
1963                 if (ret == -1) {
1964                         _LOGE("Package Info DB Information retrieval failed\n");
1965                         ret = PMINFO_R_ERROR;
1966                         goto err;
1967                 }
1968                 if (pkginfo->manifest_info->label) {
1969                         LISTHEAD(pkginfo->manifest_info->label, tmp1);
1970                         pkginfo->manifest_info->label = tmp1;
1971                 }
1972                 if (pkginfo->manifest_info->icon) {
1973                         LISTHEAD(pkginfo->manifest_info->icon, tmp2);
1974                         pkginfo->manifest_info->icon = tmp2;
1975                 }
1976                 if (pkginfo->manifest_info->description) {
1977                         LISTHEAD(pkginfo->manifest_info->description, tmp3);
1978                         pkginfo->manifest_info->description = tmp3;
1979                 }
1980                 if (pkginfo->manifest_info->author) {
1981                         LISTHEAD(pkginfo->manifest_info->author, tmp4);
1982                         pkginfo->manifest_info->author = tmp4;
1983                 }
1984         }
1985
1986         LISTHEAD(tmphead, node);
1987
1988         for(node = node->next; node ; node = node->next) {
1989                 pkginfo = node;
1990                 ret = pkg_list_cb( (void *)pkginfo, user_data);
1991                 if(ret < 0)
1992                         break;
1993         }
1994
1995         ret = PMINFO_R_OK;
1996
1997 err:
1998         sqlite3_close(manifest_db);
1999         if (syslocale) {
2000                 free(syslocale);
2001                 syslocale = NULL;
2002         }
2003         if (locale) {
2004                 free(locale);
2005                 locale = NULL;
2006         }
2007         LISTHEAD(tmphead, node);
2008         temp_node = node->next;
2009         node = temp_node;
2010         while (node) {
2011                 temp_node = node->next;
2012                 __cleanup_pkginfo(node);
2013                 node = temp_node;
2014         }
2015         __cleanup_pkginfo(tmphead);
2016         return ret;
2017 }
2018
2019
2020 API int pkgmgrinfo_pkginfo_get_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
2021 {
2022         if (pkgid == NULL) {
2023                 _LOGE("package name is NULL\n");
2024                 return PMINFO_R_EINVAL;
2025         }
2026         if (handle == NULL) {
2027                 _LOGE("Argument supplied to hold return value is NULL\n");
2028                 return PMINFO_R_EINVAL;
2029         }
2030         pkgmgr_pkginfo_x *pkginfo = NULL;
2031         char *error_message = NULL;
2032         int ret = PMINFO_R_OK;
2033         char query[MAX_QUERY_LEN] = {'\0'};
2034         char *syslocale = NULL;
2035         char *locale = NULL;
2036         int exist = 0;
2037         label_x *tmp1 = NULL;
2038         icon_x *tmp2 = NULL;
2039         description_x *tmp3 = NULL;
2040         author_x *tmp4 = NULL;
2041
2042         /*validate pkgid*/
2043         ret = __open_manifest_db();
2044         if (ret == -1) {
2045                 _LOGE("Fail to open manifest DB\n");
2046                 ret = PMINFO_R_ERROR;
2047                 goto err;
2048         }
2049         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_info where package='%s')", pkgid);
2050         if (SQLITE_OK !=
2051             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
2052                 _LOGE("Don't execute query = %s error message = %s\n", query,
2053                        error_message);
2054                 sqlite3_free(error_message);
2055                 sqlite3_close(manifest_db);
2056                 return PMINFO_R_ERROR;
2057         }
2058         if (exist == 0) {
2059                 _LOGE("Package not found in DB\n");
2060                 ret = PMINFO_R_ERROR;
2061                 goto err;
2062         }
2063
2064         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2065         if (syslocale == NULL) {
2066                 _LOGE("current locale is NULL\n");
2067                 ret = PMINFO_R_ERROR;
2068                 goto err;
2069         }
2070         locale = __convert_system_locale_to_manifest_locale(syslocale);
2071         if (locale == NULL) {
2072                 _LOGE("manifest locale is NULL\n");
2073                 ret = PMINFO_R_EINVAL;
2074                 goto err;
2075         }
2076         pkginfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
2077         if (pkginfo == NULL) {
2078                 _LOGE("Failed to allocate memory for pkginfo\n");
2079                 return PMINFO_R_ERROR;
2080         }
2081
2082         pkginfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
2083         if (pkginfo->manifest_info == NULL) {
2084                 _LOGE("Failed to allocate memory for manifest info\n");
2085                 ret = PMINFO_R_ERROR;
2086                 goto err;
2087         }
2088         pkginfo->manifest_info->package = strdup(pkgid);
2089         /*populate manifest_info from DB*/
2090         snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkgid);
2091         ret = __exec_pkginfo_query(query, (void *)pkginfo);
2092         if (ret == -1) {
2093                 _LOGE("Package Info DB Information retrieval failed\n");
2094                 ret = PMINFO_R_ERROR;
2095                 goto err;
2096         }
2097         memset(query, '\0', MAX_QUERY_LEN);
2098         snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2099                 " package='%s' and package_locale='%s'", pkgid, locale);
2100         ret = __exec_pkginfo_query(query, (void *)pkginfo);
2101         if (ret == -1) {
2102                 _LOGE("Package Info DB Information retrieval failed\n");
2103                 ret = PMINFO_R_ERROR;
2104                 goto err;
2105         }
2106         /*Also store the values corresponding to default locales*/
2107         memset(query, '\0', MAX_QUERY_LEN);
2108         snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
2109                 " package='%s' and package_locale='%s'", pkgid, DEFAULT_LOCALE);
2110         ret = __exec_pkginfo_query(query, (void *)pkginfo);
2111         if (ret == -1) {
2112                 _LOGE("Package Info DB Information retrieval failed\n");
2113                 ret = PMINFO_R_ERROR;
2114                 goto err;
2115         }
2116         if (pkginfo->manifest_info->label) {
2117                 LISTHEAD(pkginfo->manifest_info->label, tmp1);
2118                 pkginfo->manifest_info->label = tmp1;
2119         }
2120         if (pkginfo->manifest_info->icon) {
2121                 LISTHEAD(pkginfo->manifest_info->icon, tmp2);
2122                 pkginfo->manifest_info->icon = tmp2;
2123         }
2124         if (pkginfo->manifest_info->description) {
2125                 LISTHEAD(pkginfo->manifest_info->description, tmp3);
2126                 pkginfo->manifest_info->description = tmp3;
2127         }
2128         if (pkginfo->manifest_info->author) {
2129                 LISTHEAD(pkginfo->manifest_info->author, tmp4);
2130                 pkginfo->manifest_info->author = tmp4;
2131         }
2132         *handle = (void *)pkginfo;
2133         sqlite3_close(manifest_db);
2134         if (syslocale) {
2135                 free(syslocale);
2136                 syslocale = NULL;
2137         }
2138         if (locale) {
2139                 free(locale);
2140                 locale = NULL;
2141         }
2142         return PMINFO_R_OK;
2143
2144 err:
2145         *handle = NULL;
2146         __cleanup_pkginfo(pkginfo);
2147         sqlite3_close(manifest_db);
2148         if (syslocale) {
2149                 free(syslocale);
2150                 syslocale = NULL;
2151         }
2152         if (locale) {
2153                 free(locale);
2154                 locale = NULL;
2155         }
2156         return ret;
2157 }
2158
2159
2160 API int pkgmgrinfo_pkginfo_get_pkgname(pkgmgrinfo_pkginfo_h handle, char **pkg_name)
2161 {
2162         if (handle == NULL) {
2163                 _LOGE("pkginfo handle is NULL\n");
2164                 return PMINFO_R_EINVAL;
2165         }
2166         if (pkg_name == NULL) {
2167                 _LOGE("Argument supplied to hold return value is NULL\n");
2168                 return PMINFO_R_EINVAL;
2169         }
2170         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2171         if (info->manifest_info->package)
2172                 *pkg_name = info->manifest_info->package;
2173         else
2174                 return PMINFO_R_ERROR;
2175
2176         return PMINFO_R_OK;
2177 }
2178
2179 API int pkgmgrinfo_pkginfo_get_pkgid(pkgmgrinfo_pkginfo_h handle, char **pkgid)
2180 {
2181         if (handle == NULL) {
2182                 _LOGE("pkginfo handle is NULL\n");
2183                 return PMINFO_R_EINVAL;
2184         }
2185         if (pkgid == NULL) {
2186                 _LOGE("Argument supplied to hold return value is NULL\n");
2187                 return PMINFO_R_EINVAL;
2188         }
2189         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2190         if (info->manifest_info->package)
2191                 *pkgid = info->manifest_info->package;
2192         else
2193                 return PMINFO_R_ERROR;
2194
2195         return PMINFO_R_OK;
2196 }
2197
2198 API int pkgmgrinfo_pkginfo_get_type(pkgmgrinfo_pkginfo_h handle, char **type)
2199 {
2200         if (handle == NULL) {
2201                 _LOGE("pkginfo handle is NULL\n");
2202                 return PMINFO_R_EINVAL;
2203         }
2204         if (type == NULL) {
2205                 _LOGE("Argument supplied to hold return value is NULL\n");
2206                 return PMINFO_R_EINVAL;
2207         }
2208         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2209         if (info->manifest_info->type)
2210                 *type = info->manifest_info->type;
2211         else
2212                 *type = pkgtype;
2213         return PMINFO_R_OK;
2214 }
2215
2216 API int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, char **version)
2217 {
2218         if (handle == NULL) {
2219                 _LOGE("pkginfo handle is NULL\n");
2220                 return PMINFO_R_EINVAL;
2221         }
2222         if (version == NULL) {
2223                 _LOGE("Argument supplied to hold return value is NULL\n");
2224                 return PMINFO_R_EINVAL;
2225         }
2226         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2227         *version = (char *)info->manifest_info->version;
2228         return PMINFO_R_OK;
2229 }
2230
2231 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
2232 {
2233         if (handle == NULL) {
2234                 _LOGE("pkginfo handle is NULL\n");
2235                 return PMINFO_R_EINVAL;
2236         }
2237         if (location == NULL) {
2238                 _LOGE("Argument supplied to hold return value is NULL\n");
2239                 return PMINFO_R_EINVAL;
2240         }
2241         char *val = NULL;
2242         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2243         val = (char *)info->manifest_info->installlocation;
2244         if (val) {
2245                 if (strcmp(val, "internal-only") == 0)
2246                         *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
2247                 else if (strcmp(val, "prefer-external") == 0)
2248                         *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
2249                 else
2250                         *location = PMINFO_INSTALL_LOCATION_AUTO;
2251         }
2252         return PMINFO_R_OK;
2253 }
2254
2255 API int pkgmgrinfo_pkginfo_get_package_size(pkgmgrinfo_pkginfo_h handle, int *size)
2256 {
2257         if (handle == NULL) {
2258                 _LOGE("pkginfo handle is NULL\n");
2259                 return PMINFO_R_EINVAL;
2260         }
2261         if (size == NULL) {
2262                 _LOGE("Argument supplied to hold return value is NULL\n");
2263                 return PMINFO_R_EINVAL;
2264         }
2265         char *val = NULL;
2266         char *location = NULL;
2267         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2268         location = (char *)info->manifest_info->installlocation;
2269         if (strcmp(location, "prefer-external") == 0)
2270         {
2271                 val = (char *)info->manifest_info->package_size;
2272                 if (val) {
2273                         *size = atoi(val);
2274                 } else {
2275                         *size = 0;
2276                         _LOGE("package size is not specified\n");
2277                         return PMINFO_R_ERROR;
2278                 }
2279         } else {
2280                 *size = 0;
2281         }
2282         return PMINFO_R_OK;
2283 }
2284
2285 API int pkgmgrinfo_pkginfo_get_total_size(pkgmgrinfo_pkginfo_h handle, int *size)
2286 {
2287         if (handle == NULL) {
2288                 _LOGE("pkginfo handle is NULL\n");
2289                 return PMINFO_R_EINVAL;
2290         }
2291         if (size == NULL) {
2292                 _LOGE("Argument supplied to hold return value is NULL\n");
2293                 return PMINFO_R_EINVAL;
2294         }
2295
2296         char *pkgid = NULL;
2297         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
2298         long long rw_size = 0;
2299         long long ro_size= 0;
2300         long long tmp_size= 0;
2301         long long total_size= 0;
2302         struct stat fileinfo;
2303         int ret = -1;
2304
2305         ret = pkgmgrinfo_pkginfo_get_pkgid(handle,&pkgid);
2306         if(ret < 0)
2307                 return PMINFO_R_ERROR;
2308
2309         /* RW area */
2310         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RW_PATH, pkgid);
2311         if (lstat(device_path, &fileinfo) == 0) {
2312                 if (!S_ISLNK(fileinfo.st_mode)) {
2313                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2314                         if (tmp_size > 0)
2315                                 rw_size += tmp_size;
2316                 }
2317         }
2318
2319         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RW_PATH, pkgid);
2320         if (lstat(device_path, &fileinfo) == 0) {
2321                 if (!S_ISLNK(fileinfo.st_mode)) {
2322                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2323                         if (tmp_size > 0)
2324                         rw_size += tmp_size;
2325                 }
2326         }
2327
2328         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RW_PATH, pkgid);
2329         if (lstat(device_path, &fileinfo) == 0) {
2330                 if (!S_ISLNK(fileinfo.st_mode)) {
2331                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2332                         if (tmp_size > 0)
2333                         rw_size += tmp_size;
2334                 }
2335         }
2336
2337         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
2338         if (lstat(device_path, &fileinfo) == 0) {
2339                 if (!S_ISLNK(fileinfo.st_mode)) {
2340                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2341                         if (tmp_size > 0)
2342                                 rw_size += tmp_size;
2343                 }
2344         }
2345
2346         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RW_PATH, pkgid);
2347         if (lstat(device_path, &fileinfo) == 0) {
2348                 if (!S_ISLNK(fileinfo.st_mode)) {
2349                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2350                         if (tmp_size > 0)
2351                                 rw_size += tmp_size;
2352         }
2353         }
2354
2355         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RW_PATH, pkgid);
2356         if (lstat(device_path, &fileinfo) == 0) {
2357                 if (!S_ISLNK(fileinfo.st_mode)) {
2358                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2359                         if (tmp_size > 0)
2360                                 rw_size += tmp_size;
2361                 }
2362         }
2363
2364         /* RO area */
2365         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RO_PATH, pkgid);
2366         if (lstat(device_path, &fileinfo) == 0) {
2367                 if (!S_ISLNK(fileinfo.st_mode)) {
2368                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2369                         if (tmp_size > 0)
2370                                 ro_size += tmp_size;
2371                 }
2372         }
2373
2374         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RO_PATH, pkgid);
2375         if (lstat(device_path, &fileinfo) == 0) {
2376                 if (!S_ISLNK(fileinfo.st_mode)) {
2377                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2378                         if (tmp_size > 0)
2379                                 ro_size += tmp_size;
2380                 }
2381         }
2382
2383         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RO_PATH, pkgid);
2384         if (lstat(device_path, &fileinfo) == 0) {
2385                 if (!S_ISLNK(fileinfo.st_mode)) {
2386                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2387                         if (tmp_size > 0)
2388                                 ro_size += tmp_size;
2389                 }
2390         }
2391
2392         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RO_PATH, pkgid);
2393         if (lstat(device_path, &fileinfo) == 0) {
2394                 if (!S_ISLNK(fileinfo.st_mode)) {
2395                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2396                         if (tmp_size > 0)
2397                                 ro_size += tmp_size;
2398                 }
2399         }
2400
2401         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RO_PATH, pkgid);
2402         if (lstat(device_path, &fileinfo) == 0) {
2403                 if (!S_ISLNK(fileinfo.st_mode)) {
2404                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2405                         if (tmp_size > 0)
2406                                 ro_size += tmp_size;
2407         }
2408         }
2409
2410         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RO_PATH, pkgid);
2411         if (lstat(device_path, &fileinfo) == 0) {
2412                 if (!S_ISLNK(fileinfo.st_mode)) {
2413                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
2414                         if (tmp_size > 0)
2415                                 ro_size += tmp_size;
2416                 }
2417         }
2418
2419         /* Total size */
2420         total_size = rw_size + ro_size;
2421         *size = (int)total_size;
2422
2423         return PMINFO_R_OK;
2424 }
2425
2426 API int pkgmgrinfo_pkginfo_get_data_size(pkgmgrinfo_pkginfo_h handle, int *size)
2427 {
2428         if (handle == NULL) {
2429                 _LOGE("pkginfo handle is NULL\n");
2430                 return PMINFO_R_EINVAL;
2431         }
2432         if (size == NULL) {
2433                 _LOGE("Argument supplied to hold return value is NULL\n");
2434                 return PMINFO_R_EINVAL;
2435         }
2436
2437         char *pkgid = NULL;
2438         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
2439         long long total_size= 0;
2440         int ret = -1;
2441
2442         ret = pkgmgrinfo_pkginfo_get_pkgid(handle,&pkgid);
2443         if(ret < 0)
2444                 return PMINFO_R_ERROR;
2445
2446         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
2447         if (access(device_path, R_OK) == 0)
2448                 total_size = _pkgmgr_calculate_dir_size(device_path);
2449         if (total_size < 0)
2450                 return PMINFO_R_ERROR;
2451
2452         *size = (int)total_size;
2453
2454         return PMINFO_R_OK;
2455 }
2456
2457 API int pkgmgrinfo_pkginfo_get_icon(pkgmgrinfo_pkginfo_h handle, char **icon)
2458 {
2459 #if 0
2460         if (handle == NULL) {
2461                 _LOGE("pkginfo handle is NULL\n");
2462                 return PMINFO_R_EINVAL;
2463         }
2464         if (icon == NULL) {
2465                 _LOGE("Argument supplied to hold return value is NULL\n");
2466                 return PMINFO_R_EINVAL;
2467         }
2468         char *syslocale = NULL;
2469         char *locale = NULL;
2470         char *save = NULL;
2471         icon_x *ptr = NULL;
2472         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2473         if (syslocale == NULL) {
2474                 _LOGE("current locale is NULL\n");
2475                 return PMINFO_R_EINVAL;
2476         }
2477         locale = __convert_system_locale_to_manifest_locale(syslocale);
2478         if (locale == NULL) {
2479                 _LOGE("manifest locale is NULL\n");
2480                 return PMINFO_R_EINVAL;
2481         }
2482         save = locale;
2483         *icon = NULL;
2484         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2485         for(ptr = info->manifest_info->icon; ptr != NULL; ptr = ptr->next)
2486         {
2487                 if (ptr->lang) {
2488                         if (strcmp(ptr->lang, locale) == 0) {
2489                                 *icon = (char *)ptr->text;
2490                                 if (strcasecmp(*icon, "(null)") == 0) {
2491                                         locale = DEFAULT_LOCALE;
2492                                         continue;
2493                                 } else
2494                                         break;
2495                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
2496                                 *icon = (char *)ptr->text;
2497                                 break;
2498                         }
2499                 }
2500         }
2501
2502         if (syslocale) {
2503                 free(syslocale);
2504                 syslocale = NULL;
2505         }
2506         locale = save;
2507         if (locale) {
2508                 free(locale);
2509                 locale = NULL;
2510         }
2511 #else
2512         pkgmgr_pkginfo_x *info_tmp = (pkgmgr_pkginfo_x *)handle;
2513         pkgmgrinfo_appinfo_h apphandle;
2514
2515         pkgmgrinfo_appinfo_get_appinfo(info_tmp->manifest_info->mainapp_id, &apphandle);
2516         pkgmgrinfo_appinfo_get_icon(apphandle, &info_tmp->tmp);
2517         if (info_tmp->tmp_dup){
2518                 free((void *)info_tmp->tmp_dup);
2519                 info_tmp->tmp_dup = NULL;
2520         }
2521         info_tmp->tmp_dup= strdup(info_tmp->tmp);
2522         *icon = info_tmp->tmp_dup;
2523         pkgmgrinfo_appinfo_destroy_appinfo(apphandle);
2524 #endif
2525         return PMINFO_R_OK;
2526 }
2527
2528 API int pkgmgrinfo_pkginfo_get_label(pkgmgrinfo_pkginfo_h handle, char **label)
2529 {
2530 #if 0
2531         if (handle == NULL) {
2532                 _LOGE("pkginfo handle is NULL\n");
2533                 return PMINFO_R_EINVAL;
2534         }
2535         if (label == NULL) {
2536                 _LOGE("Argument supplied to hold return value is NULL\n");
2537                 return PMINFO_R_EINVAL;
2538         }
2539         char *syslocale = NULL;
2540         char *locale = NULL;
2541         char *save = NULL;
2542         label_x *ptr = NULL;
2543         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2544         if (syslocale == NULL) {
2545                 _LOGE("current locale is NULL\n");
2546                 return PMINFO_R_EINVAL;
2547         }
2548         locale = __convert_system_locale_to_manifest_locale(syslocale);
2549         if (locale == NULL) {
2550                 _LOGE("manifest locale is NULL\n");
2551                 return PMINFO_R_EINVAL;
2552         }
2553         save = locale;
2554         *label = NULL;
2555         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2556         for(ptr = info->manifest_info->label; ptr != NULL; ptr = ptr->next)
2557         {
2558                 if (ptr->lang) {
2559                         if (strcmp(ptr->lang, locale) == 0) {
2560                                 *label = (char *)ptr->text;
2561                                 if (strcasecmp(*label, "(null)") == 0) {
2562                                         locale = DEFAULT_LOCALE;
2563                                         continue;
2564                                 } else
2565                                         break;
2566                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
2567                                 *label = (char *)ptr->text;
2568                                 break;
2569                         }
2570                 }
2571         }
2572
2573         if (syslocale) {
2574                 free(syslocale);
2575                 syslocale = NULL;
2576         }
2577         locale = save;
2578         if (locale) {
2579                 free(locale);
2580                 locale = NULL;
2581         }
2582 #else
2583         pkgmgr_pkginfo_x *info_tmp = (pkgmgr_pkginfo_x *)handle;
2584         pkgmgrinfo_appinfo_h apphandle;
2585
2586         pkgmgrinfo_appinfo_get_appinfo(info_tmp->manifest_info->mainapp_id, &apphandle);
2587         pkgmgrinfo_appinfo_get_label(apphandle, &info_tmp->tmp);
2588         if (info_tmp->tmp_dup){
2589                 free((void *)info_tmp->tmp_dup);
2590                 info_tmp->tmp_dup = NULL;
2591         }
2592         info_tmp->tmp_dup = strdup(info_tmp->tmp);
2593         *label = info_tmp->tmp_dup;
2594         pkgmgrinfo_appinfo_destroy_appinfo(apphandle);
2595 #endif
2596         return PMINFO_R_OK;
2597 }
2598
2599 API int pkgmgrinfo_pkginfo_get_description(pkgmgrinfo_pkginfo_h handle, char **description)
2600 {
2601         if (handle == NULL) {
2602                 _LOGE("pkginfo handle is NULL\n");
2603                 return PMINFO_R_EINVAL;
2604         }
2605         if (description == NULL) {
2606                 _LOGE("Argument supplied to hold return value is NULL\n");
2607                 return PMINFO_R_EINVAL;
2608         }
2609         char *syslocale = NULL;
2610         char *locale = NULL;
2611         char *save = NULL;
2612         description_x *ptr = NULL;
2613         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2614         if (syslocale == NULL) {
2615                 _LOGE("current locale is NULL\n");
2616                 return PMINFO_R_EINVAL;
2617         }
2618         locale = __convert_system_locale_to_manifest_locale(syslocale);
2619         if (locale == NULL) {
2620                 _LOGE("manifest locale is NULL\n");
2621                 return PMINFO_R_EINVAL;
2622         }
2623         save = locale;
2624         *description = NULL;
2625         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2626         for(ptr = info->manifest_info->description; ptr != NULL; ptr = ptr->next)
2627         {
2628                 if (ptr->lang) {
2629                         if (strcmp(ptr->lang, locale) == 0) {
2630                                 *description = (char *)ptr->text;
2631                                 if (strcasecmp(*description, "(null)") == 0) {
2632                                         locale = DEFAULT_LOCALE;
2633                                         continue;
2634                                 } else
2635                                         break;
2636                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
2637                                 *description = (char *)ptr->text;
2638                                 break;
2639                         }
2640                 }
2641         }
2642         if (syslocale) {
2643                 free(syslocale);
2644                 syslocale = NULL;
2645         }
2646         locale = save;
2647         if (locale) {
2648                 free(locale);
2649                 locale = NULL;
2650         }
2651         return PMINFO_R_OK;
2652 }
2653
2654 API int pkgmgrinfo_pkginfo_get_author_name(pkgmgrinfo_pkginfo_h handle, char **author_name)
2655 {
2656         if (handle == NULL) {
2657                 _LOGE("pkginfo handle is NULL\n");
2658                 return PMINFO_R_EINVAL;
2659         }
2660         if (author_name == NULL) {
2661                 _LOGE("Argument supplied to hold return value is NULL\n");
2662                 return PMINFO_R_EINVAL;
2663         }
2664         char *syslocale = NULL;
2665         char *locale = NULL;
2666         char *save = NULL;
2667         author_x *ptr = NULL;
2668         syslocale = vconf_get_str(VCONFKEY_LANGSET);
2669         if (syslocale == NULL) {
2670                 _LOGE("current locale is NULL\n");
2671                 return PMINFO_R_EINVAL;
2672         }
2673         locale = __convert_system_locale_to_manifest_locale(syslocale);
2674         if (locale == NULL) {
2675                 _LOGE("manifest locale is NULL\n");
2676                 return PMINFO_R_EINVAL;
2677         }
2678         save = locale;
2679         *author_name = NULL;
2680         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2681         for(ptr = info->manifest_info->author; ptr != NULL; ptr = ptr->next)
2682         {
2683                 if (ptr->lang) {
2684                         if (strcmp(ptr->lang, locale) == 0) {
2685                                 *author_name = (char *)ptr->text;
2686                                 if (strcasecmp(*author_name, "(null)") == 0) {
2687                                         locale = DEFAULT_LOCALE;
2688                                         continue;
2689                                 } else
2690                                         break;
2691                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
2692                                 *author_name = (char *)ptr->text;
2693                                 break;
2694                         }
2695                 }
2696         }
2697         if (syslocale) {
2698                 free(syslocale);
2699                 syslocale = NULL;
2700         }
2701         locale = save;
2702         if (locale) {
2703                 free(locale);
2704                 locale = NULL;
2705         }
2706         return PMINFO_R_OK;
2707 }
2708
2709 API int pkgmgrinfo_pkginfo_get_author_email(pkgmgrinfo_pkginfo_h handle, char **author_email)
2710 {
2711         if (handle == NULL) {
2712                 _LOGE("pkginfo handle is NULL\n");
2713                 return PMINFO_R_EINVAL;
2714         }
2715         if (author_email == NULL) {
2716                 _LOGE("Argument supplied to hold return value is NULL\n");
2717                 return PMINFO_R_EINVAL;
2718         }
2719         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2720         *author_email = (char *)info->manifest_info->author->email;
2721         return PMINFO_R_OK;
2722 }
2723
2724 API int pkgmgrinfo_pkginfo_get_author_href(pkgmgrinfo_pkginfo_h handle, char **author_href)
2725 {
2726         if (handle == NULL) {
2727                 _LOGE("pkginfo handle is NULL\n");
2728                 return PMINFO_R_EINVAL;
2729         }
2730         if (author_href == NULL) {
2731                 _LOGE("Argument supplied to hold return value is NULL\n");
2732                 return PMINFO_R_EINVAL;
2733         }
2734         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2735         *author_href = (char *)info->manifest_info->author->href;
2736         return PMINFO_R_OK;
2737 }
2738
2739 API int pkgmgrinfo_pkginfo_get_installed_storage(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_installed_storage *storage)
2740 {
2741         int ret = -1;
2742         char *pkgid;
2743
2744         pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
2745         if (pkgid == NULL){
2746                  _LOGE("invalid func parameters\n");
2747                  return PMINFO_R_ERROR;
2748         }
2749
2750         FILE *fp = NULL;
2751         char app_mmc_path[FILENAME_MAX] = { 0, };
2752         char app_dir_path[FILENAME_MAX] = { 0, };
2753         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
2754         snprintf(app_dir_path, FILENAME_MAX,
2755         "%s%s", PKG_INSTALLATION_PATH, pkgid);
2756         snprintf(app_mmc_path, FILENAME_MAX,
2757         "%s%s", PKG_SD_PATH, pkgid);
2758         snprintf(app_mmc_internal_path, FILENAME_MAX,
2759         "%s%s/.mmc", PKG_INSTALLATION_PATH, pkgid);
2760
2761         /*check whether application is in external memory or not */
2762         fp = fopen(app_mmc_path, "r");
2763         if (fp == NULL) {
2764                 _LOGE(" app path in external memory not accesible\n");
2765         } else {
2766                 fclose(fp);
2767                 fp = NULL;
2768                 *storage = PMINFO_EXTERNAL_STORAGE;
2769                 return PMINFO_R_OK;
2770         }
2771
2772         /*check whether application is in internal or not */
2773         fp = fopen(app_dir_path, "r");
2774         if (fp == NULL) {
2775                 _LOGE(" app path in internal memory not accesible\n");
2776                 *storage = -1;
2777                 return PMINFO_R_ERROR;
2778         } else {
2779                 fclose(fp);
2780                 /*check whether the application is installed in SD card
2781                         but SD card is not present*/
2782                 fp = fopen(app_mmc_internal_path, "r");
2783                 if (fp == NULL) {
2784                         *storage = PMINFO_INTERNAL_STORAGE;
2785                         return PMINFO_R_OK;
2786                 } else {
2787                         fclose(fp);
2788                         *storage = PMINFO_EXTERNAL_STORAGE;
2789                         return PMINFO_R_OK;
2790                 }
2791         }
2792 }
2793
2794 API int pkgmgrinfo_pkginfo_get_installed_time(pkgmgrinfo_pkginfo_h handle, int *installed_time)
2795 {
2796         if (handle == NULL) {
2797                 _LOGE("pkginfo handle is NULL\n");
2798                 return PMINFO_R_EINVAL;
2799         }
2800         if (installed_time == NULL) {
2801                 _LOGE("Argument supplied to hold return value is NULL\n");
2802                 return PMINFO_R_EINVAL;
2803         }
2804         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2805         if (info->manifest_info->installed_time)
2806                 *installed_time = atoi(info->manifest_info->installed_time);
2807         else
2808                 return PMINFO_R_ERROR;
2809
2810         return PMINFO_R_OK;
2811 }
2812
2813 API int pkgmgrinfo_pkginfo_get_storeclientid(pkgmgrinfo_pkginfo_h handle, char **storeclientid)
2814 {
2815         if (handle == NULL) {
2816                 _LOGE("pkginfo handle is NULL\n");
2817                 return PMINFO_R_EINVAL;
2818         }
2819         if (storeclientid == NULL) {
2820                 _LOGE("Argument supplied to hold return value is NULL\n");
2821                 return PMINFO_R_EINVAL;
2822         }
2823         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2824         *storeclientid = (char *)info->manifest_info->storeclient_id;
2825         return PMINFO_R_OK;
2826 }
2827
2828 API int pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid)
2829 {
2830         if (handle == NULL) {
2831                 _LOGE("pkginfo handle is NULL\n");
2832                 return PMINFO_R_EINVAL;
2833         }
2834         if (mainappid == NULL) {
2835                 _LOGE("Argument supplied to hold return value is NULL\n");
2836                 return PMINFO_R_EINVAL;
2837         }
2838         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2839         *mainappid = (char *)info->manifest_info->mainapp_id;
2840         return PMINFO_R_OK;
2841 }
2842
2843 API int pkgmgrinfo_pkginfo_get_url(pkgmgrinfo_pkginfo_h handle, char **url)
2844 {
2845         if (handle == NULL) {
2846                 _LOGE("pkginfo handle is NULL\n");
2847                 return PMINFO_R_EINVAL;
2848         }
2849         if (url == NULL) {
2850                 _LOGE("Argument supplied to hold return value is NULL\n");
2851                 return PMINFO_R_EINVAL;
2852         }
2853         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
2854         *url = (char *)info->manifest_info->package_url;
2855         return PMINFO_R_OK;
2856 }
2857
2858 API int pkgmgrinfo_pkginfo_get_size_from_xml(const char *manifest, int *size)
2859 {
2860         char *val = NULL;
2861         const xmlChar *node;
2862         xmlTextReaderPtr reader;
2863
2864         if (manifest == NULL) {
2865                 _LOGE("input argument is NULL\n");
2866                 return PMINFO_R_ERROR;
2867         }
2868
2869         if (size == NULL) {
2870                 _LOGE("output argument is NULL\n");
2871                 return PMINFO_R_ERROR;
2872         }
2873
2874         xmlInitParser();
2875         reader = xmlReaderForFile(manifest, NULL, 0);
2876
2877         if (reader){
2878                 if (__child_element(reader, -1)) {
2879                         node = xmlTextReaderConstName(reader);
2880                         if (!node) {
2881                                 _LOGE("xmlTextReaderConstName value is NULL\n");
2882                                 xmlFreeTextReader(reader);
2883                                 xmlCleanupParser();
2884                                 return PMINFO_R_ERROR;
2885                         }
2886
2887                         if (!strcmp(ASC_CHAR(node), "manifest")) {
2888                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("size")))
2889                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("size")));
2890
2891                                 if (val) {
2892                                         *size = atoi(val);
2893                                 } else {
2894                                         *size = 0;
2895                                         _LOGE("package size is not specified\n");
2896                                         xmlFreeTextReader(reader);
2897                                         xmlCleanupParser();
2898                                         return PMINFO_R_ERROR;
2899                                 }
2900                         } else {
2901                                 _LOGE("Unable to create xml reader\n");
2902                                 xmlFreeTextReader(reader);
2903                                 xmlCleanupParser();
2904                                 return PMINFO_R_ERROR;
2905                         }
2906                 }
2907         } else {
2908                 _LOGE("xmlReaderForFile value is NULL\n");
2909                 xmlCleanupParser();
2910                 return PMINFO_R_ERROR;
2911         }
2912
2913         xmlFreeTextReader(reader);
2914         xmlCleanupParser();
2915
2916         return PMINFO_R_OK;
2917 }
2918
2919 API int pkgmgrinfo_pkginfo_get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
2920 {
2921         char *val = NULL;
2922         const xmlChar *node;
2923         xmlTextReaderPtr reader;
2924
2925         if (manifest == NULL) {
2926                 _LOGE("input argument is NULL\n");
2927                 return PMINFO_R_ERROR;
2928         }
2929
2930         if (location == NULL) {
2931                 _LOGE("output argument is NULL\n");
2932                 return PMINFO_R_ERROR;
2933         }
2934
2935         xmlInitParser();
2936         reader = xmlReaderForFile(manifest, NULL, 0);
2937
2938         if (reader){
2939                 if ( __child_element(reader, -1)) {
2940                         node = xmlTextReaderConstName(reader);
2941                         if (!node) {
2942                                 _LOGE("xmlTextReaderConstName value is NULL\n");
2943                                 xmlFreeTextReader(reader);
2944                                 xmlCleanupParser();
2945                                 return PMINFO_R_ERROR;
2946                         }
2947
2948                         if (!strcmp(ASC_CHAR(node), "manifest")) {
2949                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")))
2950                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")));
2951
2952                                 if (val) {
2953                                         if (strcmp(val, "internal-only") == 0)
2954                                                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
2955                                         else if (strcmp(val, "prefer-external") == 0)
2956                                                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
2957                                         else
2958                                                 *location = PMINFO_INSTALL_LOCATION_AUTO;
2959                                 }
2960                         } else {
2961                                 _LOGE("Unable to create xml reader\n");
2962                                 xmlFreeTextReader(reader);
2963                                 xmlCleanupParser();
2964                                 return PMINFO_R_ERROR;
2965                         }
2966                 }
2967         } else {
2968                 _LOGE("xmlReaderForFile value is NULL\n");
2969                 xmlCleanupParser();
2970                 return PMINFO_R_ERROR;
2971         }
2972
2973         xmlFreeTextReader(reader);
2974         xmlCleanupParser();
2975
2976         return PMINFO_R_OK;
2977 }
2978
2979 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(const char *lhs_package_id, const char *rhs_package_id, pkgmgrinfo_cert_compare_result_type_e *compare_result)
2980 {
2981         if (lhs_package_id == NULL || rhs_package_id == NULL)
2982         {
2983                 _LOGE("pkginfo id is NULL\n");
2984                 return PMINFO_R_EINVAL;
2985         }
2986         if (compare_result == NULL) {
2987                 _LOGE("Argument supplied to hold return value is NULL\n");
2988                 return PMINFO_R_EINVAL;
2989         }
2990
2991         int ret = PMINFO_R_OK;
2992         char query[MAX_QUERY_LEN] = {'\0'};
2993         char *error_message = NULL;
2994         pkgmgr_cert_x *info= NULL;
2995         char *lcert = NULL;
2996         char *rcert = NULL;
2997         int exist = -1;
2998
2999         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
3000         if (info == NULL) {
3001                 _LOGE("Out of Memory!!!\n");
3002                 return PMINFO_R_ERROR;
3003         }
3004
3005         ret = db_util_open_with_options(CERT_DB, &cert_db,
3006                                         SQLITE_OPEN_READONLY, NULL);
3007         if (ret != SQLITE_OK) {
3008                 _LOGE("connect db [%s] failed!\n", CERT_DB);
3009                 free(info);
3010                 info = NULL;
3011                 return PMINFO_R_ERROR;
3012         }
3013
3014         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", lhs_package_id);
3015         if (SQLITE_OK !=
3016             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3017                 _LOGE("Don't execute query = %s error message = %s\n", query,
3018                        error_message);
3019                 ret = PMINFO_R_ERROR;
3020                 goto err;
3021         }
3022
3023         if (exist == 0) {
3024                 lcert = NULL;
3025         } else {
3026                 snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", lhs_package_id);
3027                 if (SQLITE_OK !=
3028                         sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3029                         _LOGE("Don't execute query = %s error message = %s\n", query,
3030                                    error_message);
3031                         ret = PMINFO_R_ERROR;
3032                         goto err;
3033                 }
3034                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", info->certvalue);
3035                 if (SQLITE_OK !=
3036                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3037                         _LOGE("Don't execute query = %s error message = %s\n", query,
3038                                error_message);
3039                         sqlite3_free(error_message);
3040                         return PMINFO_R_ERROR;
3041                 }
3042                 if (exist == 0)
3043                         lcert = info->certvalue;
3044                 else {
3045                         memset(query, '\0', MAX_QUERY_LEN);
3046                         snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", info->certvalue);
3047                         free(info->certvalue);
3048                         info->certvalue = NULL;
3049                         if (SQLITE_OK !=
3050                                 sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3051                                 _LOGE("Don't execute query = %s error message = %s\n", query,
3052                                            error_message);
3053                                 ret = PMINFO_R_ERROR;
3054                                 goto err;
3055                         }
3056                         lcert = info->certvalue;
3057                 }
3058         }
3059
3060         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", rhs_package_id);
3061         if (SQLITE_OK !=
3062                 sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3063                 _LOGE("Don't execute query = %s error message = %s\n", query,
3064                            error_message);
3065                 ret = PMINFO_R_ERROR;
3066                 goto err;
3067         }
3068
3069         if (exist == 0) {
3070                 rcert = NULL;
3071         } else {
3072                 snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", rhs_package_id);
3073                 if (SQLITE_OK !=
3074                         sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3075                         _LOGE("Don't execute query = %s error message = %s\n", query,
3076                                    error_message);
3077                         ret = PMINFO_R_ERROR;
3078                         goto err;
3079                 }
3080                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", info->certvalue);
3081                 if (SQLITE_OK !=
3082                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3083                         _LOGE("Don't execute query = %s error message = %s\n", query,
3084                                error_message);
3085                         sqlite3_free(error_message);
3086                         return PMINFO_R_ERROR;
3087                 }
3088                 if (exist == 0)
3089                         rcert = info->certvalue;
3090                 else {
3091                         memset(query, '\0', MAX_QUERY_LEN);
3092                         snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", info->certvalue);
3093                         free(info->certvalue);
3094                         info->certvalue = NULL;
3095                         if (SQLITE_OK !=
3096                                 sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3097                                 _LOGE("Don't execute query = %s error message = %s\n", query,
3098                                            error_message);
3099                                 ret = PMINFO_R_ERROR;
3100                                 goto err;
3101                         }
3102                         rcert = info->certvalue;
3103                 }
3104         }
3105
3106         if ((lcert == NULL) || (rcert == NULL))
3107         {
3108                 if ((lcert == NULL) && (rcert == NULL))
3109                         *compare_result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
3110                 else if (lcert == NULL)
3111                         *compare_result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
3112                 else if (rcert == NULL)
3113                         *compare_result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
3114         } else {
3115                 if (strcmp(lcert, rcert) == 0)
3116                         *compare_result = PMINFO_CERT_COMPARE_MATCH;
3117                 else
3118                         *compare_result = PMINFO_CERT_COMPARE_MISMATCH;
3119         }
3120
3121 err:
3122         sqlite3_free(error_message);
3123         sqlite3_close(cert_db);
3124         if (info) {
3125                 free(info);
3126                 info = NULL;
3127         }
3128
3129         return ret;
3130 }
3131
3132
3133 API int pkgmgrinfo_pkginfo_compare_app_cert_info(const char *lhs_app_id, const char *rhs_app_id, pkgmgrinfo_cert_compare_result_type_e *compare_result)
3134 {
3135         if (lhs_app_id == NULL || rhs_app_id == NULL)
3136         {
3137                 _LOGE("pkginfo id is NULL\n");
3138                 return PMINFO_R_EINVAL;
3139         }
3140         if (compare_result == NULL) {
3141                 _LOGE("Argument supplied to hold return value is NULL\n");
3142                 return PMINFO_R_EINVAL;
3143         }
3144
3145         int ret = PMINFO_R_OK;
3146         char query[MAX_QUERY_LEN] = {'\0'};
3147         char *error_message = NULL;
3148         pkgmgr_cert_x *info= NULL;
3149         char *lcert = NULL;
3150         char *rcert = NULL;
3151         char *lhs_package_id = NULL;
3152         char *rhs_package_id = NULL;
3153         int exist = -1;
3154
3155         info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
3156         if (info == NULL) {
3157                 _LOGE("Out of Memory!!!\n");
3158                 return PMINFO_R_ERROR;
3159         }
3160
3161         ret = db_util_open_with_options(MANIFEST_DB, &manifest_db,
3162                                         SQLITE_OPEN_READONLY, NULL);
3163         if (ret != SQLITE_OK) {
3164                 _LOGE("connect db [%s] failed!\n", MANIFEST_DB);
3165                 free(info);
3166                 info = NULL;
3167                 return PMINFO_R_ERROR;
3168         }
3169         ret = db_util_open_with_options(CERT_DB, &cert_db,
3170                                         SQLITE_OPEN_READONLY, NULL);
3171         if (ret != SQLITE_OK) {
3172                 _LOGE("connect db [%s] failed!\n", CERT_DB);
3173                 sqlite3_close(manifest_db);
3174                 free(info);
3175                 info = NULL;
3176                 return PMINFO_R_ERROR;
3177         }
3178
3179         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", lhs_app_id);
3180         if (SQLITE_OK !=
3181             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
3182                 _LOGE("Don't execute query = %s error message = %s\n", query,
3183                        error_message);
3184                 ret = PMINFO_R_ERROR;
3185                 goto err;
3186         }
3187
3188         if (exist == 0) {
3189                 lcert = NULL;
3190         } else {
3191                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", lhs_app_id);
3192                 if (SQLITE_OK !=
3193                         sqlite3_exec(manifest_db, query, __cert_cb, (void *)info, &error_message)) {
3194                         _LOGE("Don't execute query = %s error message = %s\n", query,
3195                                    error_message);
3196                         ret = PMINFO_R_ERROR;
3197                         goto err;
3198                 }
3199
3200                 snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", info->pkgid);
3201                 if (SQLITE_OK !=
3202                         sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3203                         _LOGE("Don't execute query = %s error message = %s\n", query,
3204                                    error_message);
3205                         ret = PMINFO_R_ERROR;
3206                         goto err;
3207                 }
3208                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", info->certvalue);
3209                 if (SQLITE_OK !=
3210                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3211                         _LOGE("Don't execute query = %s error message = %s\n", query,
3212                                error_message);
3213                         sqlite3_free(error_message);
3214                         return PMINFO_R_ERROR;
3215                 }
3216                 if (exist == 0)
3217                         lcert = info->certvalue;
3218                 else {
3219                         memset(query, '\0', MAX_QUERY_LEN);
3220                         snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", info->certvalue);
3221                         free(info->certvalue);
3222                         info->certvalue = NULL;
3223                         if (SQLITE_OK !=
3224                                 sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3225                                 _LOGE("Don't execute query = %s error message = %s\n", query,
3226                                            error_message);
3227                                 ret = PMINFO_R_ERROR;
3228                                 goto err;
3229                         }
3230                         lcert = info->certvalue;
3231                 }
3232         }
3233
3234         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", rhs_app_id);
3235         if (SQLITE_OK !=
3236             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
3237                 _LOGE("Don't execute query = %s error message = %s\n", query,
3238                        error_message);
3239                 ret = PMINFO_R_ERROR;
3240                 goto err;
3241         }
3242
3243         if (exist == 0) {
3244                 rcert = NULL;
3245         } else {
3246                 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", rhs_app_id);
3247                 if (SQLITE_OK !=
3248                         sqlite3_exec(manifest_db, query, __cert_cb, (void *)info, &error_message)) {
3249                         _LOGE("Don't execute query = %s error message = %s\n", query,
3250                                    error_message);
3251                         ret = PMINFO_R_ERROR;
3252                         goto err;
3253                 }
3254
3255                 snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", info->pkgid);
3256                 if (SQLITE_OK !=
3257                         sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3258                         _LOGE("Don't execute query = %s error message = %s\n", query,
3259                                    error_message);
3260                         ret = PMINFO_R_ERROR;
3261                         goto err;
3262                 }
3263                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", info->certvalue);
3264                 if (SQLITE_OK !=
3265                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
3266                         _LOGE("Don't execute query = %s error message = %s\n", query,
3267                                error_message);
3268                         sqlite3_free(error_message);
3269                         return PMINFO_R_ERROR;
3270                 }
3271                 if (exist == 0)
3272                         rcert = info->certvalue;
3273                 else {
3274                         memset(query, '\0', MAX_QUERY_LEN);
3275                         snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info where package='%s'", info->certvalue);
3276                         free(info->certvalue);
3277                         info->certvalue = NULL;
3278                         if (SQLITE_OK !=
3279                                 sqlite3_exec(cert_db, query, __cert_cb, (void *)info, &error_message)) {
3280                                 _LOGE("Don't execute query = %s error message = %s\n", query,
3281                                            error_message);
3282                                 ret = PMINFO_R_ERROR;
3283                                 goto err;
3284                         }
3285                         rcert = info->certvalue;
3286                 }
3287         }
3288
3289         if ((lcert == NULL) || (rcert == NULL))
3290         {
3291                 if ((lcert == NULL) && (rcert == NULL))
3292                         *compare_result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
3293                 else if (lcert == NULL)
3294                         *compare_result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
3295                 else if (rcert == NULL)
3296                         *compare_result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
3297         } else {
3298                 if (strcmp(lcert, rcert) == 0)
3299                         *compare_result = PMINFO_CERT_COMPARE_MATCH;
3300                 else
3301                         *compare_result = PMINFO_CERT_COMPARE_MISMATCH;
3302         }
3303
3304 err:
3305         sqlite3_free(error_message);
3306         sqlite3_close(manifest_db);
3307         sqlite3_close(cert_db);
3308         if (info) {
3309                 free(info);
3310                 info = NULL;
3311         }
3312
3313         return ret;
3314 }
3315
3316 API int pkgmgrinfo_pkginfo_is_accessible(pkgmgrinfo_pkginfo_h handle, bool *accessible)
3317 {
3318         char *pkgid;
3319
3320         pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
3321         if (pkgid == NULL){
3322                  _LOGD("invalid func parameters\n");
3323                  return PMINFO_R_ERROR;
3324         }
3325          _LOGD("pkgmgr_get_pkg_external_validation() called\n");
3326
3327         FILE *fp = NULL;
3328         char app_mmc_path[FILENAME_MAX] = { 0, };
3329         char app_dir_path[FILENAME_MAX] = { 0, };
3330         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
3331         snprintf(app_dir_path, FILENAME_MAX,"%s%s", PKG_INSTALLATION_PATH, pkgid);
3332         snprintf(app_mmc_path, FILENAME_MAX,"%s%s", PKG_SD_PATH, pkgid);
3333         snprintf(app_mmc_internal_path, FILENAME_MAX,"%s%s/.mmc", PKG_INSTALLATION_PATH, pkgid);
3334
3335         /*check whether application is in external memory or not */
3336         fp = fopen(app_mmc_path, "r");
3337         if (fp == NULL){
3338                 _LOGD(" app path in external memory not accesible\n");
3339         } else {
3340                 fclose(fp);
3341                 fp = NULL;
3342                 *accessible = 1;
3343                 _LOGD("pkgmgr_get_pkg_external_validation() : SD_CARD \n");
3344                 return PMINFO_R_OK;
3345         }
3346
3347         /*check whether application is in internal or not */
3348         fp = fopen(app_dir_path, "r");
3349         if (fp == NULL) {
3350                 _LOGD(" app path in internal memory not accesible\n");
3351                 *accessible = 0;
3352                 return PMINFO_R_ERROR;
3353         } else {
3354                 fclose(fp);
3355                 /*check whether the application is installed in SD card
3356                 but SD card is not present*/
3357                 fp = fopen(app_mmc_internal_path, "r");
3358                 if (fp == NULL){
3359                         *accessible = 1;
3360                         _LOGD("pkgmgr_get_pkg_external_validation() : INTERNAL_MEM \n");
3361                         return PMINFO_R_OK;
3362                 }
3363                 else{
3364                         *accessible = 0;
3365                         _LOGD("pkgmgr_get_pkg_external_validation() : ERROR_MMC_STATUS \n");
3366                 }
3367                 fclose(fp);
3368         }
3369
3370         _LOGD("pkgmgr_get_pkg_external_validation() end\n");
3371         return PMINFO_R_OK;
3372 }
3373
3374 API int pkgmgrinfo_pkginfo_is_removable(pkgmgrinfo_pkginfo_h handle, bool *removable)
3375 {
3376         if (handle == NULL) {
3377                 _LOGE("pkginfo handle is NULL\n");
3378                 return PMINFO_R_EINVAL;
3379         }
3380         if (removable == NULL) {
3381                 _LOGE("Argument supplied to hold return value is NULL\n");
3382                 return PMINFO_R_EINVAL;
3383         }
3384         char *val = NULL;
3385         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3386         val = (char *)info->manifest_info->removable;
3387         if (val) {
3388                 if (strcasecmp(val, "true") == 0)
3389                         *removable = 1;
3390                 else if (strcasecmp(val, "false") == 0)
3391                         *removable = 0;
3392                 else
3393                         *removable = 1;
3394         }
3395         return PMINFO_R_OK;
3396 }
3397
3398 API int pkgmgrinfo_pkginfo_is_preload(pkgmgrinfo_pkginfo_h handle, bool *preload)
3399 {
3400         if (handle == NULL) {
3401                 _LOGE("pkginfo handle is NULL\n");
3402                 return PMINFO_R_EINVAL;
3403         }
3404         if (preload == NULL) {
3405                 _LOGE("Argument supplied to hold return value is NULL\n");
3406                 return PMINFO_R_EINVAL;
3407         }
3408         char *val = NULL;
3409         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3410         val = (char *)info->manifest_info->preload;
3411         if (val) {
3412                 if (strcasecmp(val, "true") == 0)
3413                         *preload = 1;
3414                 else if (strcasecmp(val, "false") == 0)
3415                         *preload = 0;
3416                 else
3417                         *preload = 0;
3418         }
3419         return PMINFO_R_OK;
3420 }
3421
3422 API int pkgmgrinfo_pkginfo_is_readonly(pkgmgrinfo_pkginfo_h handle, bool *readonly)
3423 {
3424         if (handle == NULL) {
3425                 _LOGE("pkginfo handle is NULL\n");
3426                 return PMINFO_R_EINVAL;
3427         }
3428         if (readonly == NULL) {
3429                 _LOGE("Argument supplied to hold return value is NULL\n");
3430                 return PMINFO_R_EINVAL;
3431         }
3432         char *val = NULL;
3433         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3434         val = (char *)info->manifest_info->readonly;
3435         if (val) {
3436                 if (strcasecmp(val, "true") == 0)
3437                         *readonly = 1;
3438                 else if (strcasecmp(val, "false") == 0)
3439                         *readonly = 0;
3440                 else
3441                         *readonly = 0;
3442         }
3443         return PMINFO_R_OK;
3444 }
3445
3446 API int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle)
3447 {
3448         if (handle == NULL) {
3449                 _LOGE("pkginfo handle is NULL\n");
3450                 return PMINFO_R_EINVAL;
3451         }
3452         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3453         __cleanup_pkginfo(info);
3454         return PMINFO_R_OK;
3455 }
3456
3457 API int pkgmgrinfo_pkginfo_filter_create(pkgmgrinfo_pkginfo_filter_h *handle)
3458 {
3459         if (handle == NULL) {
3460                 _LOGE("Filter handle output parameter is NULL\n");
3461                 return PMINFO_R_EINVAL;
3462         }
3463         *handle = NULL;
3464         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)calloc(1, sizeof(pkgmgrinfo_filter_x));
3465         if (filter == NULL) {
3466                 _LOGE("Out of Memory!!!");
3467                 return PMINFO_R_ERROR;
3468         }
3469         *handle = filter;
3470         return PMINFO_R_OK;
3471 }
3472
3473 API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
3474 {
3475         if (handle == NULL) {
3476                 _LOGE("Filter handle input parameter is NULL\n");
3477                 return PMINFO_R_EINVAL;
3478         }
3479         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3480         if (filter->list){
3481                 g_slist_foreach(filter->list, __destroy_each_node, NULL);
3482                 g_slist_free(filter->list);
3483         }
3484         free(filter);
3485         filter = NULL;
3486         return PMINFO_R_OK;
3487 }
3488
3489 API int pkgmgrinfo_pkginfo_filter_add_int(pkgmgrinfo_pkginfo_filter_h handle,
3490                                 const char *property, const int value)
3491 {
3492         if (handle == NULL || property == NULL) {
3493                 _LOGE("Filter handle input parameter is NULL\n");
3494                 return PMINFO_R_EINVAL;
3495         }
3496         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
3497         char *val = NULL;
3498         GSList *link = NULL;
3499         int prop = -1;
3500         prop = _pminfo_pkginfo_convert_to_prop_int(property);
3501         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT ||
3502                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT) {
3503                 _LOGE("Invalid Integer Property\n");
3504                 return PMINFO_R_EINVAL;
3505         }
3506         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3507         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
3508         if (node == NULL) {
3509                 _LOGE("Out of Memory!!!\n");
3510                 return PMINFO_R_ERROR;
3511         }
3512         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
3513         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
3514         if (val == NULL) {
3515                 _LOGE("Out of Memory\n");
3516                 free(node);
3517                 node = NULL;
3518                 return PMINFO_R_ERROR;
3519         }
3520         node->prop = prop;
3521         node->value = val;
3522         /*If API is called multiple times for same property, we should override the previous values.
3523         Last value set will be used for filtering.*/
3524         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3525         if (link)
3526                 filter->list = g_slist_delete_link(filter->list, link);
3527         filter->list = g_slist_append(filter->list, (gpointer)node);
3528         return PMINFO_R_OK;
3529
3530 }
3531
3532 API int pkgmgrinfo_pkginfo_filter_add_bool(pkgmgrinfo_pkginfo_filter_h handle,
3533                                 const char *property, const bool value)
3534 {
3535         if (handle == NULL || property == NULL) {
3536                 _LOGE("Filter handle input parameter is NULL\n");
3537                 return PMINFO_R_EINVAL;
3538         }
3539         char *val = NULL;
3540         GSList *link = NULL;
3541         int prop = -1;
3542         prop = _pminfo_pkginfo_convert_to_prop_bool(property);
3543         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL ||
3544                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL) {
3545                 _LOGE("Invalid Boolean Property\n");
3546                 return PMINFO_R_EINVAL;
3547         }
3548         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3549         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
3550         if (node == NULL) {
3551                 _LOGE("Out of Memory!!!\n");
3552                 return PMINFO_R_ERROR;
3553         }
3554         if (value)
3555                 val = strndup("('true','True')", 15);
3556         else
3557                 val = strndup("('false','False')", 17);
3558         if (val == NULL) {
3559                 _LOGE("Out of Memory\n");
3560                 free(node);
3561                 node = NULL;
3562                 return PMINFO_R_ERROR;
3563         }
3564         node->prop = prop;
3565         node->value = val;
3566         /*If API is called multiple times for same property, we should override the previous values.
3567         Last value set will be used for filtering.*/
3568         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3569         if (link)
3570                 filter->list = g_slist_delete_link(filter->list, link);
3571         filter->list = g_slist_append(filter->list, (gpointer)node);
3572         return PMINFO_R_OK;
3573
3574 }
3575
3576 API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
3577                                 const char *property, const char *value)
3578 {
3579         if (handle == NULL || property == NULL || value == NULL) {
3580                 _LOGE("Filter handle input parameter is NULL\n");
3581                 return PMINFO_R_EINVAL;
3582         }
3583         char *val = NULL;
3584         GSList *link = NULL;
3585         int prop = -1;
3586         prop = _pminfo_pkginfo_convert_to_prop_str(property);
3587         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR ||
3588                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR) {
3589                 _LOGE("Invalid String Property\n");
3590                 return PMINFO_R_EINVAL;
3591         }
3592         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3593         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
3594         if (node == NULL) {
3595                 _LOGE("Out of Memory!!!\n");
3596                 return PMINFO_R_ERROR;
3597         }
3598         if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_AUTO) == 0)
3599                 val = strndup("auto", PKG_STRING_LEN_MAX - 1);
3600         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_INTERNAL) == 0)
3601                 val = strndup("internal-only", PKG_STRING_LEN_MAX - 1);
3602         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_EXTERNAL) == 0)
3603                 val = strndup("prefer-external", PKG_STRING_LEN_MAX - 1);
3604         else
3605                 val = strndup(value, PKG_STRING_LEN_MAX - 1);
3606         if (val == NULL) {
3607                 _LOGE("Out of Memory\n");
3608                 free(node);
3609                 node = NULL;
3610                 return PMINFO_R_ERROR;
3611         }
3612         node->prop = prop;
3613         node->value = val;
3614         /*If API is called multiple times for same property, we should override the previous values.
3615         Last value set will be used for filtering.*/
3616         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
3617         if (link)
3618                 filter->list = g_slist_delete_link(filter->list, link);
3619         filter->list = g_slist_append(filter->list, (gpointer)node);
3620         return PMINFO_R_OK;
3621
3622 }
3623
3624 API int pkgmgrinfo_pkginfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)
3625 {
3626         if (handle == NULL || count == NULL) {
3627                 _LOGE("Filter handle input parameter is NULL\n");
3628                 return PMINFO_R_EINVAL;
3629         }
3630         char *syslocale = NULL;
3631         char *locale = NULL;
3632         char *condition = NULL;
3633         char *error_message = NULL;
3634         char query[MAX_QUERY_LEN] = {'\0'};
3635         char where[MAX_QUERY_LEN] = {'\0'};
3636         GSList *list;
3637         int ret = 0;
3638
3639         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3640         /*Get current locale*/
3641         syslocale = vconf_get_str(VCONFKEY_LANGSET);
3642         if (syslocale == NULL) {
3643                 _LOGE("current locale is NULL\n");
3644                 return PMINFO_R_ERROR;
3645         }
3646         locale = __convert_system_locale_to_manifest_locale(syslocale);
3647         if (locale == NULL) {
3648                 _LOGE("manifest locale is NULL\n");
3649                 free(syslocale);
3650                 return PMINFO_R_ERROR;
3651         }
3652         ret = __open_manifest_db();
3653         if (ret == -1) {
3654                 _LOGE("Fail to open manifest DB\n");
3655                 ret = PMINFO_R_ERROR;
3656                 goto err;
3657         }
3658
3659         /*Start constructing query*/
3660         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_PACKAGE, locale);
3661
3662         /*Get where clause*/
3663         for (list = filter->list; list; list = g_slist_next(list)) {
3664                 __get_filter_condition(list->data, &condition);
3665                 if (condition) {
3666                         strncat(where, condition, sizeof(where) - strlen(where) -1);
3667                         where[sizeof(where) - 1] = '\0';
3668                         free(condition);
3669                         condition = NULL;
3670                 }
3671                 if (g_slist_next(list)) {
3672                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
3673                         where[sizeof(where) - 1] = '\0';
3674                 }
3675         }
3676         _LOGE("where = %s\n", where);
3677         if (strlen(where) > 0) {
3678                 strncat(query, where, sizeof(query) - strlen(query) - 1);
3679                 query[sizeof(query) - 1] = '\0';
3680         }
3681         _LOGE("query = %s\n", query);
3682
3683         /*Execute Query*/
3684         if (SQLITE_OK !=
3685             sqlite3_exec(manifest_db, query, __count_cb, (void *)count, &error_message)) {
3686                 _LOGE("Don't execute query = %s error message = %s\n", query,
3687                        error_message);
3688                 sqlite3_free(error_message);
3689                 sqlite3_close(manifest_db);
3690                 ret = PMINFO_R_ERROR;
3691                 *count = 0;
3692                 goto err;
3693         }
3694         ret = PMINFO_R_OK;
3695 err:
3696         if (locale) {
3697                 free(locale);
3698                 locale = NULL;
3699         }
3700         if (syslocale) {
3701                 free(syslocale);
3702                 syslocale = NULL;
3703         }
3704         sqlite3_close(manifest_db);
3705         return ret;
3706 }
3707
3708 API int pkgmgrinfo_pkginfo_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
3709                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data)
3710 {
3711         if (handle == NULL || pkg_cb == NULL) {
3712                 _LOGE("Filter handle input parameter is NULL\n");
3713                 return PMINFO_R_EINVAL;
3714         }
3715         char *syslocale = NULL;
3716         char *locale = NULL;
3717         char *condition = NULL;
3718         char *error_message = NULL;
3719         char query[MAX_QUERY_LEN] = {'\0'};
3720         char where[MAX_QUERY_LEN] = {'\0'};
3721         GSList *list;
3722         int ret = 0;
3723         label_x *tmp1 = NULL;
3724         icon_x *tmp2 = NULL;
3725         description_x *tmp3 = NULL;
3726         author_x *tmp4 = NULL;
3727         pkgmgr_pkginfo_x *node = NULL;
3728         pkgmgr_pkginfo_x *tmphead = NULL;
3729         pkgmgr_pkginfo_x *pkginfo = NULL;
3730
3731         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
3732         /*Get current locale*/
3733         syslocale = vconf_get_str(VCONFKEY_LANGSET);
3734         if (syslocale == NULL) {
3735                 _LOGE("current locale is NULL\n");
3736                 return PMINFO_R_ERROR;
3737         }
3738         locale = __convert_system_locale_to_manifest_locale(syslocale);
3739         if (locale == NULL) {
3740                 _LOGE("manifest locale is NULL\n");
3741                 free(syslocale);
3742                 return PMINFO_R_ERROR;
3743         }
3744         ret = __open_manifest_db();
3745         if (ret == -1) {
3746                 _LOGE("Fail to open manifest DB\n");
3747                 ret = PMINFO_R_ERROR;
3748                 goto err;
3749         }
3750         /*Start constructing query*/
3751         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_PACKAGE, locale);
3752
3753         /*Get where clause*/
3754         for (list = filter->list; list; list = g_slist_next(list)) {
3755                 __get_filter_condition(list->data, &condition);
3756                 if (condition) {
3757                         strncat(where, condition, sizeof(where) - strlen(where) -1);
3758                         where[sizeof(where) - 1] = '\0';
3759                         free(condition);
3760                         condition = NULL;
3761                 }
3762                 if (g_slist_next(list)) {
3763                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
3764                         where[sizeof(where) - 1] = '\0';
3765                 }
3766         }
3767         _LOGE("where = %s\n", where);
3768         if (strlen(where) > 0) {
3769                 strncat(query, where, sizeof(query) - strlen(query) - 1);
3770                 query[sizeof(query) - 1] = '\0';
3771         }
3772         _LOGE("query = %s\n", query);
3773         tmphead = calloc(1, sizeof(pkgmgr_pkginfo_x));
3774         if (tmphead == NULL) {
3775                 _LOGE("Out of Memory!!!\n");
3776                 ret = PMINFO_R_ERROR;
3777                 goto err;
3778         }
3779
3780         if (SQLITE_OK !=
3781             sqlite3_exec(manifest_db, query, __pkg_list_cb, (void *)tmphead, &error_message)) {
3782                 _LOGE("Don't execute query = %s error message = %s\n", query,
3783                        error_message);
3784                 sqlite3_free(error_message);
3785                 sqlite3_close(manifest_db);
3786                 ret = PMINFO_R_ERROR;
3787                 goto err;
3788         }
3789
3790         LISTHEAD(tmphead, node);
3791         for(node = node->next ; node ; node = node->next) {
3792                 pkginfo = node;
3793
3794                 /*populate manifest_info from DB*/
3795                 snprintf(query, MAX_QUERY_LEN, "select * from package_info where package='%s' ", pkginfo->manifest_info->package);
3796                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
3797                 if (ret == -1) {
3798                         _LOGE("Package Info DB Information retrieval failed\n");
3799                         ret = PMINFO_R_ERROR;
3800                         goto err;
3801                 }
3802                 memset(query, '\0', MAX_QUERY_LEN);
3803                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
3804                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, locale);
3805                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
3806                 if (ret == -1) {
3807                         _LOGE("Package Info DB Information retrieval failed\n");
3808                         ret = PMINFO_R_ERROR;
3809                         goto err;
3810                 }
3811                 /*Also store the values corresponding to default locales*/
3812                 memset(query, '\0', MAX_QUERY_LEN);
3813                 snprintf(query, MAX_QUERY_LEN, "select * from package_localized_info where" \
3814                         " package='%s' and package_locale='%s'", pkginfo->manifest_info->package, DEFAULT_LOCALE);
3815                 ret = __exec_pkginfo_query(query, (void *)pkginfo);
3816                 if (ret == -1) {
3817                         _LOGE("Package Info DB Information retrieval failed\n");
3818                         ret = PMINFO_R_ERROR;
3819                         goto err;
3820                 }
3821                 if (pkginfo->manifest_info->label) {
3822                         LISTHEAD(pkginfo->manifest_info->label, tmp1);
3823                         pkginfo->manifest_info->label = tmp1;
3824                 }
3825                 if (pkginfo->manifest_info->icon) {
3826                         LISTHEAD(pkginfo->manifest_info->icon, tmp2);
3827                         pkginfo->manifest_info->icon = tmp2;
3828                 }
3829                 if (pkginfo->manifest_info->description) {
3830                         LISTHEAD(pkginfo->manifest_info->description, tmp3);
3831                         pkginfo->manifest_info->description = tmp3;
3832                 }
3833                 if (pkginfo->manifest_info->author) {
3834                         LISTHEAD(pkginfo->manifest_info->author, tmp4);
3835                         pkginfo->manifest_info->author = tmp4;
3836                 }
3837         }
3838
3839         LISTHEAD(tmphead, node);
3840
3841         for(node = node->next ; node ; node = node->next) {
3842                 pkginfo = node;
3843                 ret = pkg_cb( (void *)pkginfo, user_data);
3844                 if(ret < 0)
3845                         break;
3846         }
3847         ret = PMINFO_R_OK;
3848
3849 err:
3850         if (locale) {
3851                 free(locale);
3852                 locale = NULL;
3853         }
3854         if (syslocale) {
3855                 free(syslocale);
3856                 syslocale = NULL;
3857         }
3858         sqlite3_close(manifest_db);
3859         __cleanup_pkginfo(tmphead);
3860         return ret;
3861 }
3862
3863 API int pkgmgrinfo_appinfo_get_list(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_app_component component,
3864                                                 pkgmgrinfo_app_list_cb app_func, void *user_data)
3865 {
3866         if (handle == NULL) {
3867                 _LOGE("pkginfo handle is NULL\n");
3868                 return PMINFO_R_EINVAL;
3869         }
3870         if (app_func == NULL) {
3871                 _LOGE("callback pointer is NULL\n");
3872                 return PMINFO_R_EINVAL;
3873         }
3874         if (component != PMINFO_UI_APP && component != PMINFO_SVC_APP && component != PMINFO_ALL_APP) {
3875                 _LOGE("Invalid App Component Type\n");
3876                 return PMINFO_R_EINVAL;
3877         }
3878         char *error_message = NULL;
3879         char *syslocale = NULL;
3880         char *locale = NULL;
3881         int ret = -1;
3882         char query[MAX_QUERY_LEN] = {'\0'};
3883         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
3884         pkgmgr_pkginfo_x *allinfo = NULL;
3885         pkgmgr_appinfo_x *appinfo = NULL;
3886         icon_x *ptr1 = NULL;
3887         label_x *ptr2 = NULL;
3888
3889         syslocale = vconf_get_str(VCONFKEY_LANGSET);
3890         if (syslocale == NULL) {
3891                 _LOGE("current locale is NULL\n");
3892                 ret = PMINFO_R_ERROR;
3893                 goto err;
3894         }
3895         locale = __convert_system_locale_to_manifest_locale(syslocale);
3896         if (locale == NULL) {
3897                 _LOGE("manifest locale is NULL\n");
3898                 ret = PMINFO_R_EINVAL;
3899                 goto err;
3900         }
3901
3902         allinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
3903         if (allinfo == NULL) {
3904                 _LOGE("Failed to allocate memory for appinfo\n");
3905                 ret = PMINFO_R_ERROR;
3906                 goto err;
3907         }
3908         allinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
3909         if (allinfo->manifest_info == NULL) {
3910                 _LOGE("Out of Memory!!!\n");
3911                 ret = PMINFO_R_ERROR;
3912                 goto err;
3913         }
3914
3915         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
3916         if (appinfo == NULL) {
3917                 _LOGE("Failed to allocate memory for appinfo\n");
3918                 ret = PMINFO_R_ERROR;
3919                 goto err;
3920         }
3921         if (component == PMINFO_UI_APP)
3922                 appinfo->app_component = PMINFO_UI_APP;
3923         if (component == PMINFO_SVC_APP)
3924                 appinfo->app_component = PMINFO_SVC_APP;
3925         if (component == PMINFO_ALL_APP)
3926                 appinfo->app_component = PMINFO_ALL_APP;
3927         ret = __open_manifest_db();
3928         if (ret == -1) {
3929                 _LOGE("Fail to open manifest DB\n");
3930                 ret = PMINFO_R_ERROR;
3931                 goto err;
3932         }
3933
3934         appinfo->package = strdup(info->manifest_info->package);
3935         snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
3936                         "from package_app_info where " \
3937                         "package='%s' and app_component='%s'",
3938                         info->manifest_info->package,
3939                         (appinfo->app_component==PMINFO_UI_APP ? "uiapp" : "svcapp"));
3940
3941         switch(component) {
3942         case PMINFO_UI_APP:
3943                 if (SQLITE_OK !=
3944                 sqlite3_exec(manifest_db, query, __uiapp_list_cb, (void *)info, &error_message)) {
3945                         _LOGE("Don't execute query = %s error message = %s\n", query,
3946                                error_message);
3947                         sqlite3_free(error_message);
3948                         sqlite3_close(manifest_db);
3949                         ret = PMINFO_R_ERROR;
3950                         goto err;
3951                 }
3952                 uiapplication_x *tmp = NULL;
3953                 if (info->manifest_info->uiapplication) {
3954                         LISTHEAD(info->manifest_info->uiapplication, tmp);
3955                         info->manifest_info->uiapplication = tmp;
3956                 }
3957                 /*Populate localized info for default locales and call callback*/
3958                 /*If the callback func return < 0 we break and no more call back is called*/
3959                 while(tmp != NULL)
3960                 {
3961                         appinfo->uiapp_info = tmp;
3962
3963                         if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
3964                                 if (locale) {
3965                                         free(locale);
3966                                 }
3967                                 locale = __get_app_locale_by_fallback(appinfo->uiapp_info->appid, syslocale);
3968                         }
3969
3970                         memset(query, '\0', MAX_QUERY_LEN);
3971                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
3972                                 " app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
3973                         ret = __exec_appinfo_query(query, (void *)appinfo);
3974                         if (ret == -1) {
3975                                 _LOGE("App Localized Info DB Information retrieval failed\n");
3976                                 ret = PMINFO_R_ERROR;
3977                                 goto err;
3978                         }
3979                         memset(query, '\0', MAX_QUERY_LEN);
3980                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
3981                                 " app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, DEFAULT_LOCALE);
3982                         ret = __exec_appinfo_query(query, (void *)appinfo);
3983                         if (ret == -1) {
3984                                 _LOGE("App Localized Info DB Information retrieval failed\n");
3985                                 ret = PMINFO_R_ERROR;
3986                                 goto err;
3987                         }
3988                         if (appinfo->uiapp_info->label) {
3989                                 LISTHEAD(appinfo->uiapp_info->label, ptr2);
3990                                 appinfo->uiapp_info->label = ptr2;
3991                         }
3992                         if (appinfo->uiapp_info->icon) {
3993                                 LISTHEAD(appinfo->uiapp_info->icon, ptr1);
3994                                 appinfo->uiapp_info->icon = ptr1;
3995                         }
3996                         ret = app_func((void *)appinfo, user_data);
3997                         if (ret < 0)
3998                                 break;
3999                         tmp = tmp->next;
4000                 }
4001                 break;
4002         case PMINFO_SVC_APP:
4003                 if (SQLITE_OK !=
4004                 sqlite3_exec(manifest_db, query, __svcapp_list_cb, (void *)info, &error_message)) {
4005                         _LOGE("Don't execute query = %s error message = %s\n", query,
4006                                error_message);
4007                         sqlite3_free(error_message);
4008                         sqlite3_close(manifest_db);
4009                         ret = PMINFO_R_ERROR;
4010                         goto err;
4011                 }
4012                 serviceapplication_x *tmp1 = NULL;
4013                 if (info->manifest_info->serviceapplication) {
4014                         LISTHEAD(info->manifest_info->serviceapplication, tmp1);
4015                         info->manifest_info->serviceapplication = tmp1;
4016                 }
4017                 /*Populate localized info for default locales and call callback*/
4018                 /*If the callback func return < 0 we break and no more call back is called*/
4019                 while(tmp1 != NULL)
4020                 {
4021                         appinfo->svcapp_info = tmp1;
4022                         memset(query, '\0', MAX_QUERY_LEN);
4023                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
4024                                 " app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, locale);
4025                         ret = __exec_appinfo_query(query, (void *)appinfo);
4026                         if (ret == -1) {
4027                                 _LOGE("App Localized Info DB Information retrieval failed\n");
4028                                 ret = PMINFO_R_ERROR;
4029                                 goto err;
4030                         }
4031                         memset(query, '\0', MAX_QUERY_LEN);
4032                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
4033                                 " app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, DEFAULT_LOCALE);
4034                         ret = __exec_appinfo_query(query, (void *)appinfo);
4035                         if (ret == -1) {
4036                                 _LOGE("App Localized Info DB Information retrieval failed\n");
4037                                 ret = PMINFO_R_ERROR;
4038                                 goto err;
4039                         }
4040                         if (appinfo->svcapp_info->label) {
4041                                 LISTHEAD(appinfo->svcapp_info->label, ptr2);
4042                                 appinfo->svcapp_info->label = ptr2;
4043                         }
4044                         if (appinfo->svcapp_info->icon) {
4045                                 LISTHEAD(appinfo->svcapp_info->icon, ptr1);
4046                                 appinfo->svcapp_info->icon = ptr1;
4047                         }
4048                         ret = app_func((void *)appinfo, user_data);
4049                         if (ret < 0)
4050                                 break;
4051                         tmp1 = tmp1->next;
4052                 }
4053                 break;
4054         case PMINFO_ALL_APP:
4055                 memset(query, '\0', MAX_QUERY_LEN);
4056                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where package='%s'", info->manifest_info->package);
4057                 if (SQLITE_OK !=
4058                 sqlite3_exec(manifest_db, query, __allapp_list_cb, (void *)allinfo, &error_message)) {
4059                         _LOGE("Don't execute query = %s error message = %s\n", query,
4060                                    error_message);
4061                         sqlite3_free(error_message);
4062                         sqlite3_close(manifest_db);
4063                         ret = PMINFO_R_ERROR;
4064                         goto err;
4065                 }
4066
4067                 /*UI Apps*/
4068                 appinfo->app_component = PMINFO_UI_APP;
4069                 uiapplication_x *tmp2 = NULL;
4070                 if (allinfo->manifest_info->uiapplication) {
4071                         LISTHEAD(allinfo->manifest_info->uiapplication, tmp2);
4072                         allinfo->manifest_info->uiapplication = tmp2;
4073                 }
4074                 /*Populate localized info for default locales and call callback*/
4075                 /*If the callback func return < 0 we break and no more call back is called*/
4076                 while(tmp2 != NULL)
4077                 {
4078                         appinfo->uiapp_info = tmp2;
4079                         memset(query, '\0', MAX_QUERY_LEN);
4080                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
4081                                 " app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, locale);
4082                         ret = __exec_appinfo_query(query, (void *)appinfo);
4083                         if (ret == -1) {
4084                                 _LOGE("App Localized Info DB Information retrieval failed\n");
4085                                 ret = PMINFO_R_ERROR;
4086                                 goto err;
4087                         }
4088                         memset(query, '\0', MAX_QUERY_LEN);
4089                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
4090                                 " app_id='%s' and app_locale='%s'", appinfo->uiapp_info->appid, DEFAULT_LOCALE);
4091                         ret = __exec_appinfo_query(query, (void *)appinfo);
4092                         if (ret == -1) {
4093                                 _LOGE("App Localized Info DB Information retrieval failed\n");
4094                                 ret = PMINFO_R_ERROR;
4095                                 goto err;
4096                         }
4097                         if (appinfo->uiapp_info->label) {
4098                                 LISTHEAD(appinfo->uiapp_info->label, ptr2);
4099                                 appinfo->uiapp_info->label = ptr2;
4100                         }
4101                         if (appinfo->uiapp_info->icon) {
4102                                 LISTHEAD(appinfo->uiapp_info->icon, ptr1);
4103                                 appinfo->uiapp_info->icon = ptr1;
4104                         }
4105                         ret = app_func((void *)appinfo, user_data);
4106                         if (ret < 0)
4107                                 break;
4108                         tmp2 = tmp2->next;
4109                 }
4110
4111                 /*SVC Apps*/
4112                 appinfo->app_component = PMINFO_SVC_APP;
4113                 serviceapplication_x *tmp3 = NULL;
4114                 if (allinfo->manifest_info->serviceapplication) {
4115                         LISTHEAD(allinfo->manifest_info->serviceapplication, tmp3);
4116                         allinfo->manifest_info->serviceapplication = tmp3;
4117                 }
4118                 /*Populate localized info for default locales and call callback*/
4119                 /*If the callback func return < 0 we break and no more call back is called*/
4120                 while(tmp3 != NULL)
4121                 {
4122                         appinfo->svcapp_info = tmp3;
4123                         memset(query, '\0', MAX_QUERY_LEN);
4124                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
4125                                 " app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, locale);
4126                         ret = __exec_appinfo_query(query, (void *)appinfo);
4127                         if (ret == -1) {
4128                                 _LOGE("App Localized Info DB Information retrieval failed\n");
4129                                 ret = PMINFO_R_ERROR;
4130                                 goto err;
4131                         }
4132                         memset(query, '\0', MAX_QUERY_LEN);
4133                         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
4134                                 " app_id='%s' and app_locale='%s'", appinfo->svcapp_info->appid, DEFAULT_LOCALE);
4135                         ret = __exec_appinfo_query(query, (void *)appinfo);
4136                         if (ret == -1) {
4137                                 _LOGE("App Localized Info DB Information retrieval failed\n");
4138                                 ret = PMINFO_R_ERROR;
4139                                 goto err;
4140                         }
4141                         if (appinfo->svcapp_info->label) {
4142                                 LISTHEAD(appinfo->svcapp_info->label, ptr2);
4143                                 appinfo->svcapp_info->label = ptr2;
4144                         }
4145                         if (appinfo->svcapp_info->icon) {
4146                                 LISTHEAD(appinfo->svcapp_info->icon, ptr1);
4147                                 appinfo->svcapp_info->icon = ptr1;
4148                         }
4149                         ret = app_func((void *)appinfo, user_data);
4150                         if (ret < 0)
4151                                 break;
4152                         tmp3 = tmp3->next;
4153                 }
4154                 appinfo->app_component = PMINFO_ALL_APP;
4155                 break;
4156
4157         }
4158
4159         ret = PMINFO_R_OK;
4160 err:
4161         if (locale) {
4162                 free(locale);
4163                 locale = NULL;
4164         }
4165         if (syslocale) {
4166                 free(syslocale);
4167                 syslocale = NULL;
4168         }
4169         if (appinfo) {
4170                 if (appinfo->package) {
4171                         free(appinfo->package);
4172                         appinfo->package = NULL;
4173                 }
4174                 free(appinfo);
4175                 appinfo = NULL;
4176         }
4177         __cleanup_pkginfo(allinfo);
4178
4179         sqlite3_close(manifest_db);
4180         return ret;
4181 }
4182
4183 API int pkgmgrinfo_appinfo_get_installed_list(pkgmgrinfo_app_list_cb app_func, void *user_data)
4184 {
4185         if (app_func == NULL) {
4186                 _LOGE("callback function is NULL\n");
4187                 return PMINFO_R_EINVAL;
4188         }
4189         char *error_message = NULL;
4190         int ret = PMINFO_R_OK;
4191         char query[MAX_QUERY_LEN] = {'\0'};
4192         char *syslocale = NULL;
4193         char *locale = NULL;
4194         pkgmgr_appinfo_x *appinfo = NULL;
4195         uiapplication_x *ptr1 = NULL;
4196         serviceapplication_x *ptr2 = NULL;
4197         label_x *tmp1 = NULL;
4198         icon_x *tmp2 = NULL;
4199
4200         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4201         if (syslocale == NULL) {
4202                 _LOGE("current locale is NULL\n");
4203                 ret = PMINFO_R_ERROR;
4204                 goto err;
4205         }
4206         locale = __convert_system_locale_to_manifest_locale(syslocale);
4207         if (locale == NULL) {
4208                 _LOGE("manifest locale is NULL\n");
4209                 ret = PMINFO_R_EINVAL;
4210                 goto err;
4211         }
4212
4213         ret = __open_manifest_db();
4214         if (ret == -1) {
4215                 _LOGE("Fail to open manifest DB\n");
4216                 ret = PMINFO_R_ERROR;
4217                 goto err;
4218         }
4219         pkgmgr_pkginfo_x *info = NULL;
4220         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
4221         if (info == NULL) {
4222                 _LOGE("Out of Memory!!!\n");
4223                 ret = PMINFO_R_ERROR;
4224                 goto err;
4225         }
4226         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
4227         if (info->manifest_info == NULL) {
4228                 _LOGE("Out of Memory!!!\n");
4229                 ret = PMINFO_R_ERROR;
4230                 goto err;
4231         }
4232         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4233         if (appinfo == NULL) {
4234                 _LOGE("Out of Memory!!!\n");
4235                 ret = PMINFO_R_ERROR;
4236                 goto err;
4237         }
4238
4239         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info");
4240         if (SQLITE_OK !=
4241             sqlite3_exec(manifest_db, query, __app_list_cb, (void *)info, &error_message)) {
4242                 _LOGE("Don't execute query = %s error message = %s\n", query,
4243                        error_message);
4244                 sqlite3_free(error_message);
4245                 sqlite3_close(manifest_db);
4246                 ret = PMINFO_R_ERROR;
4247                 goto err;
4248         }
4249         memset(query, '\0', MAX_QUERY_LEN);
4250         if (info->manifest_info->uiapplication) {
4251                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
4252                 info->manifest_info->uiapplication = ptr1;
4253         }
4254         if (info->manifest_info->serviceapplication) {
4255                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
4256                 info->manifest_info->serviceapplication = ptr2;
4257         }
4258
4259         /*UI Apps*/
4260         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
4261         {
4262                 appinfo->app_component = PMINFO_UI_APP;
4263                 appinfo->package = strdup(ptr1->package);
4264                 appinfo->uiapp_info = ptr1;
4265                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4266                                 "from package_app_info where " \
4267                                 "app_id='%s'", ptr1->appid);
4268                 ret = __exec_appinfo_query(query, (void *)appinfo);
4269                 if (ret == -1) {
4270                         _LOGE("App Info DB Information retrieval failed\n");
4271                         ret = PMINFO_R_ERROR;
4272                         goto err;
4273                 }
4274
4275                 if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
4276                         if (locale) {
4277                                 free(locale);
4278                         }
4279                         locale = __get_app_locale_by_fallback(ptr1->appid, syslocale);
4280                 }
4281
4282                 memset(query, '\0', MAX_QUERY_LEN);
4283                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4284                                 "from package_app_localized_info where " \
4285                                 "app_id='%s' and app_locale='%s'",
4286                                 ptr1->appid, locale);
4287                 ret = __exec_appinfo_query(query, (void *)appinfo);
4288                 if (ret == -1) {
4289                         _LOGE("App Localized Info DB Information retrieval failed\n");
4290                         ret = PMINFO_R_ERROR;
4291                         goto err;
4292                 }
4293                 memset(query, '\0', MAX_QUERY_LEN);
4294                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4295                                 "from package_app_localized_info where " \
4296                                 "app_id='%s' and app_locale='%s'",
4297                                 ptr1->appid, DEFAULT_LOCALE);
4298                 ret = __exec_appinfo_query(query, (void *)appinfo);
4299                 if (ret == -1) {
4300                         _LOGE("App Localized Info DB Information retrieval failed\n");
4301                         ret = PMINFO_R_ERROR;
4302                         goto err;
4303                 }
4304                 if (appinfo->uiapp_info->label) {
4305                         LISTHEAD(appinfo->uiapp_info->label, tmp1);
4306                         appinfo->uiapp_info->label = tmp1;
4307                 }
4308                 if (appinfo->uiapp_info->icon) {
4309                         LISTHEAD(appinfo->uiapp_info->icon, tmp2);
4310                         appinfo->uiapp_info->icon= tmp2;
4311                 }
4312                 ret = app_func((void *)appinfo, user_data);
4313                 if (ret < 0)
4314                         break;
4315                 free(appinfo->package);
4316                 appinfo->package = NULL;
4317         }
4318         /*Service Apps*/
4319         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
4320         {
4321                 appinfo->app_component = PMINFO_SVC_APP;
4322                 appinfo->package = strdup(ptr2->package);
4323                 appinfo->svcapp_info = ptr2;
4324                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4325                                 "from package_app_info where " \
4326                                 "app_id='%s'", ptr2->appid);
4327                 ret = __exec_appinfo_query(query, (void *)appinfo);
4328                 if (ret == -1) {
4329                         _LOGE("App Info DB Information retrieval failed\n");
4330                         ret = PMINFO_R_ERROR;
4331                         goto err;
4332                 }
4333                 memset(query, '\0', MAX_QUERY_LEN);
4334                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4335                                 "from package_app_localized_info where " \
4336                                 "app_id='%s' and app_locale='%s'",
4337                                 ptr2->appid, locale);
4338                 ret = __exec_appinfo_query(query, (void *)appinfo);
4339                 if (ret == -1) {
4340                         _LOGE("App Localized Info DB Information retrieval failed\n");
4341                         ret = PMINFO_R_ERROR;
4342                         goto err;
4343                 }
4344                 memset(query, '\0', MAX_QUERY_LEN);
4345                 snprintf(query, MAX_QUERY_LEN, "select DISTINCT * " \
4346                                 "from package_app_localized_info where " \
4347                                 "app_id='%s' and app_locale='%s'",
4348                                 ptr2->appid, DEFAULT_LOCALE);
4349                 ret = __exec_appinfo_query(query, (void *)appinfo);
4350                 if (ret == -1) {
4351                         _LOGE("App Localized Info DB Information retrieval failed\n");
4352                         ret = PMINFO_R_ERROR;
4353                         goto err;
4354                 }
4355                 if (appinfo->svcapp_info->label) {
4356                         LISTHEAD(appinfo->svcapp_info->label, tmp1);
4357                         appinfo->svcapp_info->label = tmp1;
4358                 }
4359                 if (appinfo->svcapp_info->icon) {
4360                         LISTHEAD(appinfo->svcapp_info->icon, tmp2);
4361                         appinfo->svcapp_info->icon= tmp2;
4362                 }
4363                 ret = app_func((void *)appinfo, user_data);
4364                 if (ret < 0)
4365                         break;
4366                 free(appinfo->package);
4367                 appinfo->package = NULL;
4368         }
4369         ret = PMINFO_R_OK;
4370
4371 err:
4372         if (locale) {
4373                 free(locale);
4374                 locale = NULL;
4375         }
4376         if (syslocale) {
4377                 free(syslocale);
4378                 syslocale = NULL;
4379         }
4380         sqlite3_close(manifest_db);
4381         if (appinfo) {
4382                 free(appinfo);
4383                 appinfo = NULL;
4384         }
4385         __cleanup_pkginfo(info);
4386         return ret;
4387 }
4388
4389 API int pkgmgrinfo_appinfo_get_appinfo(const char *appid, pkgmgrinfo_appinfo_h *handle)
4390 {
4391         if (appid == NULL) {
4392                 _LOGE("appid is NULL\n");
4393                 return PMINFO_R_EINVAL;
4394         }
4395         if (handle == NULL) {
4396                 _LOGE("Argument supplied to hold return value is NULL\n");
4397                 return PMINFO_R_EINVAL;
4398         }
4399         pkgmgr_appinfo_x *appinfo = NULL;
4400         char *error_message = NULL;
4401         char *syslocale = NULL;
4402         char *locale = NULL;
4403         int ret = -1;
4404         int exist = 0;
4405         label_x *tmp1 = NULL;
4406         icon_x *tmp2 = NULL;
4407         category_x *tmp3 = NULL;
4408         char query[MAX_QUERY_LEN] = {'\0'};
4409
4410         /*Validate appid*/
4411         ret = __open_manifest_db();
4412         if (ret == -1) {
4413                 _LOGE("Fail to open manifest DB\n");
4414                 ret = PMINFO_R_ERROR;
4415                 goto err;
4416         }
4417         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", appid);
4418         if (SQLITE_OK !=
4419             sqlite3_exec(manifest_db, query, __validate_cb, (void *)&exist, &error_message)) {
4420                 _LOGE("Don't execute query = %s error message = %s\n", query,
4421                        error_message);
4422                 sqlite3_free(error_message);
4423                 sqlite3_close(manifest_db);
4424                 return PMINFO_R_ERROR;
4425         }
4426         if (exist == 0) {
4427                 _LOGE("Appid not found in DB\n");
4428                 ret = PMINFO_R_ERROR;
4429                 goto err;
4430         }
4431         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4432         if (syslocale == NULL) {
4433                 _LOGE("current locale is NULL\n");
4434                 ret = PMINFO_R_ERROR;
4435                 goto err;
4436         }
4437         locale = __convert_system_locale_to_manifest_locale(syslocale);
4438         if (locale == NULL) {
4439                 _LOGE("manifest locale is NULL\n");
4440                 ret = PMINFO_R_ERROR;
4441                 goto err;
4442         }
4443         appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
4444         if (appinfo == NULL) {
4445                 _LOGE("Failed to allocate memory for appinfo\n");
4446                 ret = PMINFO_R_ERROR;
4447                 goto err;
4448         }
4449
4450         /*check app_component from DB*/
4451         snprintf(query, MAX_QUERY_LEN, "select app_component, package from package_app_info where app_id='%s' ", appid);
4452         ret = __exec_appcomponent_query(query, (void *)appinfo);
4453         if (ret == -1) {
4454                 _LOGE("App Info DB Information retrieval failed\n");
4455                 ret = PMINFO_R_ERROR;
4456                 goto err;
4457         }
4458
4459         if (appinfo->app_component == PMINFO_UI_APP) {
4460                 appinfo->uiapp_info = (uiapplication_x *)calloc(1, sizeof(uiapplication_x));
4461                 if (appinfo->uiapp_info == NULL) {
4462                         _LOGE("Failed to allocate memory for uiapp info\n");
4463                         ret = PMINFO_R_ERROR;
4464                         goto err;
4465                 }
4466         } else {
4467                 appinfo->svcapp_info = (serviceapplication_x *)calloc(1, sizeof(serviceapplication_x));
4468                 if (appinfo->svcapp_info == NULL) {
4469                         _LOGE("Failed to allocate memory for svcapp info\n");
4470                         ret = PMINFO_R_ERROR;
4471                         goto err;
4472                 }
4473         }
4474
4475         /*populate app_info from DB*/
4476         snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' ", appid);
4477         ret = __exec_appinfo_query(query, (void *)appinfo);
4478         if (ret == -1) {
4479                 _LOGE("App Info DB Information retrieval failed\n");
4480                 ret = PMINFO_R_ERROR;
4481                 goto err;
4482         }
4483
4484         if (strcmp(appinfo->uiapp_info->type,"c++app") == 0){
4485                 if (locale) {
4486                         free(locale);
4487                 }
4488                 locale = __get_app_locale_by_fallback(appid, syslocale);
4489         }
4490
4491         memset(query, '\0', MAX_QUERY_LEN);
4492         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
4493                 " app_id='%s' and app_locale='%s'", appid, locale);
4494         ret = __exec_appinfo_query(query, (void *)appinfo);
4495         if (ret == -1) {
4496                 _LOGE("App Localized Info DB Information retrieval failed\n");
4497                 ret = PMINFO_R_ERROR;
4498                 goto err;
4499         }
4500         /*Also store the values corresponding to default locales*/
4501         memset(query, '\0', MAX_QUERY_LEN);
4502         snprintf(query, MAX_QUERY_LEN, "select * from package_app_localized_info where" \
4503                 " app_id='%s' and app_locale='%s'", appid, DEFAULT_LOCALE);
4504         ret = __exec_appinfo_query(query, (void *)appinfo);
4505         if (ret == -1) {
4506                 _LOGE("App Localized Info DB Information retrieval failed\n");
4507                 ret = PMINFO_R_ERROR;
4508                 goto err;
4509         }
4510         /*Populate app category*/
4511         memset(query, '\0', MAX_QUERY_LEN);
4512         snprintf(query, MAX_QUERY_LEN, "select * from package_app_app_category where" \
4513                 " app_id='%s'", appid);
4514         ret = __exec_appinfo_query(query, (void *)appinfo);
4515         if (ret == -1) {
4516                 _LOGE("App Category Info DB Information retrieval failed\n");
4517                 ret = PMINFO_R_ERROR;
4518                 goto err;
4519         }
4520         switch (appinfo->app_component) {
4521         case PMINFO_UI_APP:
4522                 if (appinfo->uiapp_info->label) {
4523                         LISTHEAD(appinfo->uiapp_info->label, tmp1);
4524                         appinfo->uiapp_info->label = tmp1;
4525                 }
4526                 if (appinfo->uiapp_info->icon) {
4527                         LISTHEAD(appinfo->uiapp_info->icon, tmp2);
4528                         appinfo->uiapp_info->icon = tmp2;
4529                 }
4530                 if (appinfo->uiapp_info->category) {
4531                         LISTHEAD(appinfo->uiapp_info->category, tmp3);
4532                         appinfo->uiapp_info->category = tmp3;
4533                 }
4534                 break;
4535         case PMINFO_SVC_APP:
4536                 if (appinfo->svcapp_info->label) {
4537                         LISTHEAD(appinfo->svcapp_info->label, tmp1);
4538                         appinfo->svcapp_info->label = tmp1;
4539                 }
4540                 if (appinfo->svcapp_info->icon) {
4541                         LISTHEAD(appinfo->svcapp_info->icon, tmp2);
4542                         appinfo->svcapp_info->icon = tmp2;
4543                 }
4544                 if (appinfo->svcapp_info->category) {
4545                         LISTHEAD(appinfo->svcapp_info->category, tmp3);
4546                         appinfo->svcapp_info->category = tmp3;
4547                 }
4548                 break;
4549         default:
4550                 break;
4551         }
4552
4553         *handle = (void*)appinfo;
4554         sqlite3_close(manifest_db);
4555         if (syslocale) {
4556                 free(syslocale);
4557                 syslocale = NULL;
4558         }
4559         if (locale) {
4560                 free(locale);
4561                 locale = NULL;
4562         }
4563         return PMINFO_R_OK;
4564 err:
4565         *handle = NULL;
4566         __cleanup_appinfo(appinfo);
4567         sqlite3_close(manifest_db);
4568         if (syslocale) {
4569                 free(syslocale);
4570                 syslocale = NULL;
4571         }
4572         if (locale) {
4573                 free(locale);
4574                 locale = NULL;
4575         }
4576         return ret;
4577 }
4578
4579
4580 API int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h  handle, char **appid)
4581 {
4582         if (handle == NULL) {
4583                 _LOGE("appinfo handle is NULL\n");
4584                 return PMINFO_R_EINVAL;
4585         }
4586         if (appid == NULL) {
4587                 _LOGE("Argument supplied to hold return value is NULL\n");
4588                 return PMINFO_R_EINVAL;
4589         }
4590         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4591
4592         if (info->app_component == PMINFO_UI_APP)
4593                 *appid = (char *)info->uiapp_info->appid;
4594         else if (info->app_component == PMINFO_SVC_APP)
4595                 *appid = (char *)info->svcapp_info->appid;
4596
4597         return PMINFO_R_OK;
4598 }
4599
4600 API int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h  handle, char **pkg_name)
4601 {
4602         if (handle == NULL) {
4603                 _LOGE("appinfo handle is NULL\n");
4604                 return PMINFO_R_EINVAL;
4605         }
4606         if (pkg_name == NULL) {
4607                 _LOGE("Argument supplied to hold return value is NULL\n");
4608                 return PMINFO_R_EINVAL;
4609         }
4610         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4611
4612         *pkg_name = (char *)info->package;
4613
4614         return PMINFO_R_OK;
4615 }
4616
4617 API int pkgmgrinfo_appinfo_get_pkgid(pkgmgrinfo_appinfo_h  handle, char **pkgid)
4618 {
4619         if (handle == NULL) {
4620                 _LOGE("appinfo handle is NULL\n");
4621                 return PMINFO_R_EINVAL;
4622         }
4623         if (pkgid == NULL) {
4624                 _LOGE("Argument supplied to hold return value is NULL\n");
4625                 return PMINFO_R_EINVAL;
4626         }
4627         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4628
4629         *pkgid = (char *)info->package;
4630
4631         return PMINFO_R_OK;
4632 }
4633
4634 API int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h  handle, char **exec)
4635 {
4636         if (handle == NULL) {
4637                 _LOGE("appinfo handle is NULL\n");
4638                 return PMINFO_R_EINVAL;
4639         }
4640         if (exec == NULL) {
4641                 _LOGE("Argument supplied to hold return value is NULL\n");
4642                 return PMINFO_R_EINVAL;
4643         }
4644         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4645
4646         if (info->app_component == PMINFO_UI_APP)
4647                 *exec = (char *)info->uiapp_info->exec;
4648         if (info->app_component == PMINFO_SVC_APP)
4649                 *exec = (char *)info->svcapp_info->exec;
4650
4651         return PMINFO_R_OK;
4652 }
4653
4654
4655 API int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
4656 {
4657         if (handle == NULL) {
4658                 _LOGE("appinfo handle is NULL\n");
4659                 return PMINFO_R_EINVAL;
4660         }
4661         if (icon == NULL) {
4662                 _LOGE("Argument supplied to hold return value is NULL\n");
4663                 return PMINFO_R_EINVAL;
4664         }
4665         char *syslocale = NULL;
4666         char *locale = NULL;
4667         char *save = NULL;
4668         icon_x *ptr = NULL;
4669         icon_x *start = NULL;
4670         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4671         if (syslocale == NULL) {
4672                 _LOGE("current locale is NULL\n");
4673                 return PMINFO_R_EINVAL;
4674         }
4675         locale = __convert_system_locale_to_manifest_locale(syslocale);
4676         if (locale == NULL) {
4677                 _LOGE("manifest locale is NULL\n");
4678                 return PMINFO_R_EINVAL;
4679         }
4680         save = locale;
4681         *icon = NULL;
4682         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4683         if (info->app_component == PMINFO_UI_APP)
4684                 start = info->uiapp_info->icon;
4685         if (info->app_component == PMINFO_SVC_APP)
4686                 start = info->svcapp_info->icon;
4687         for(ptr = start; ptr != NULL; ptr = ptr->next)
4688         {
4689                 if (ptr->lang) {
4690                         if (strcmp(ptr->lang, locale) == 0) {
4691                                 *icon = (char *)ptr->text;
4692                                 if (strcasecmp(*icon, "(null)") == 0) {
4693                                         locale = DEFAULT_LOCALE;
4694                                         continue;
4695                                 } else
4696                                         break;
4697                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
4698                                 *icon = (char *)ptr->text;
4699                                 break;
4700                         }
4701                 }
4702         }
4703         if (syslocale) {
4704                 free(syslocale);
4705                 syslocale = NULL;
4706         }
4707         locale = save;
4708         if (locale) {
4709                 free(locale);
4710                 locale = NULL;
4711         }
4712         return PMINFO_R_OK;
4713 }
4714
4715
4716 API int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h  handle, char **label)
4717 {
4718         if (handle == NULL) {
4719                 _LOGE("appinfo handle is NULL\n");
4720                 return PMINFO_R_EINVAL;
4721         }
4722         if (label == NULL) {
4723                 _LOGE("Argument supplied to hold return value is NULL\n");
4724                 return PMINFO_R_EINVAL;
4725         }
4726         char *syslocale = NULL;
4727         char *locale = NULL;
4728         char *save = NULL;
4729         label_x *ptr = NULL;
4730         label_x *start = NULL;
4731         syslocale = vconf_get_str(VCONFKEY_LANGSET);
4732         if (syslocale == NULL) {
4733                 _LOGE("current locale is NULL\n");
4734                 return PMINFO_R_EINVAL;
4735         }
4736         locale = __convert_system_locale_to_manifest_locale(syslocale);
4737         if (locale == NULL) {
4738                 _LOGE("manifest locale is NULL\n");
4739                 return PMINFO_R_EINVAL;
4740         }
4741
4742         save = locale;
4743         *label = NULL;
4744         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4745         if (info->app_component == PMINFO_UI_APP)
4746                 start = info->uiapp_info->label;
4747         if (info->app_component == PMINFO_SVC_APP)
4748                 start = info->svcapp_info->label;
4749         for(ptr = start; ptr != NULL; ptr = ptr->next)
4750         {
4751                 if (ptr->lang) {
4752                         if (strcmp(ptr->lang, locale) == 0) {
4753                                 *label = (char *)ptr->text;
4754                                 if (strcasecmp(*label, "(null)") == 0) {
4755                                         locale = DEFAULT_LOCALE;
4756                                         continue;
4757                                 } else
4758                                         break;
4759                         } else if (strncasecmp(ptr->lang, locale, 2) == 0) {
4760                                 *label = (char *)ptr->text;
4761                                 if (strcasecmp(*label, "(null)") == 0) {
4762                                                 locale = DEFAULT_LOCALE;
4763                                                 continue;
4764                                 } else
4765                                                 break;
4766                         } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
4767                                 *label = (char *)ptr->text;
4768                                 break;
4769                         }
4770                 }
4771         }
4772         if (syslocale) {
4773                 free(syslocale);
4774                 syslocale = NULL;
4775         }
4776         locale = save;
4777         if (locale) {
4778                 free(locale);
4779                 locale = NULL;
4780         }
4781         return PMINFO_R_OK;
4782 }
4783
4784
4785 API int pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_component *component)
4786 {
4787         if (handle == NULL) {
4788                 _LOGE("appinfo handle is NULL\n");
4789                 return PMINFO_R_EINVAL;
4790         }
4791         if (component == NULL) {
4792                 _LOGE("Argument supplied to hold return value is NULL\n");
4793                 return PMINFO_R_EINVAL;
4794         }
4795         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4796
4797         if (info->app_component == PMINFO_UI_APP)
4798                 *component = PMINFO_UI_APP;
4799         else if (info->app_component == PMINFO_SVC_APP)
4800                 *component = PMINFO_SVC_APP;
4801         else
4802                 return PMINFO_R_ERROR;
4803
4804         return PMINFO_R_OK;
4805 }
4806
4807 API int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h  handle, char **app_type)
4808 {
4809         if (handle == NULL) {
4810                 _LOGE("appinfo handle is NULL\n");
4811                 return PMINFO_R_EINVAL;
4812         }
4813         if (app_type == NULL) {
4814                 _LOGE("Argument supplied to hold return value is NULL\n");
4815                 return PMINFO_R_EINVAL;
4816         }
4817         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
4818
4819         if (info->app_component == PMINFO_UI_APP)
4820                 *app_type = (char *)info->uiapp_info->type;
4821         if (info->app_component == PMINFO_SVC_APP)
4822                 *app_type = (char *)info->svcapp_info->type;
4823
4824         return PMINFO_R_OK;
4825 }
4826
4827 API int pkgmgrinfo_appinfo_get_operation(pkgmgrinfo_appcontrol_h  handle,
4828                                         int *operation_count, char ***operation)
4829 {
4830         if (handle == NULL) {
4831                 _LOGE("appcontrol handle is NULL\n");
4832                 return PMINFO_R_EINVAL;
4833         }
4834         if (operation_count == NULL || operation == NULL) {
4835                 _LOGE("Argument supplied to hold return value is NULL\n");
4836                 return PMINFO_R_EINVAL;
4837         }
4838         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
4839         *operation_count = data->operation_count;
4840         *operation = data->operation;
4841         return PMINFO_R_OK;
4842 }
4843
4844 API int pkgmgrinfo_appinfo_get_uri(pkgmgrinfo_appcontrol_h  handle,
4845                                         int *uri_count, char ***uri)
4846 {
4847         if (handle == NULL) {
4848                 _LOGE("appcontrol handle is NULL\n");
4849                 return PMINFO_R_EINVAL;
4850         }
4851         if (uri_count == NULL || uri == NULL) {
4852                 _LOGE("Argument supplied to hold return value is NULL\n");
4853                 return PMINFO_R_EINVAL;
4854         }
4855         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
4856         *uri_count = data->uri_count;
4857         *uri = data->uri;
4858         return PMINFO_R_OK;
4859 }
4860
4861 API int pkgmgrinfo_appinfo_get_mime(pkgmgrinfo_appcontrol_h  handle,
4862                                         int *mime_count, char ***mime)
4863 {
4864         if (handle == NULL) {
4865                 _LOGE("appcontrol handle is NULL\n");
4866                 return PMINFO_R_EINVAL;
4867         }
4868         if (mime_count == NULL || mime == NULL) {
4869                 _LOGE("Argument supplied to hold return value is NULL\n");
4870                 return PMINFO_R_EINVAL;
4871         }
4872         pkgmgrinfo_appcontrol_x *data = (pkgmgrinfo_appcontrol_x *)handle;
4873         *mime_count = data->mime_count;
4874         *mime = data->mime;
4875         return PMINFO_R_OK;
4876 }
4877
4878 API int pkgmgrinfo_appinfo_get_setting_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
4879 {
4880         if (handle == NULL) {
4881                 _LOGE("appinfo handle is NULL\n");
4882                 return PMINFO_R_EINVAL;
4883         }
4884         if (icon == NULL) {
4885                 _LOGE("Argument supplied to hold return value is NULL\n");
4886                 return PMINFO_R_EINVAL;
4887         }
4888         int ret = -1;
4889         char *error_message = NULL;
4890         char query[MAX_QUERY_LEN] = {'\0'};
4891         char path_buf[PKG_STRING_LEN_MAX] = {'\0'};
4892         pkgmgr_iconpath_x *data = NULL;
4893         char *icon_path;
4894         char *readpath;
4895         char *appid;
4896
4897         ret = __open_manifest_db();
4898         if (ret == -1) {
4899                 _LOGE("Fail to open manifest DB\n");
4900                 return PMINFO_R_ERROR;
4901         }
4902
4903         ret = pkgmgrinfo_appinfo_get_appid(handle,&appid);
4904         if(ret < 0 || appid == NULL)
4905                 return PMINFO_R_ERROR;
4906
4907         data = (pkgmgr_iconpath_x *)calloc(1, sizeof(pkgmgr_iconpath_x));
4908         if (data == NULL) {
4909                 _LOGE("Failed to allocate memory for pkgmgr_datacontrol_x\n");
4910                 sqlite3_close(manifest_db);
4911                 return PMINFO_R_ERROR;
4912         }
4913
4914         snprintf(query, MAX_QUERY_LEN, "select app_icon from package_app_icon_localized_info where app_id='%s' and app_icon_section ='setting'", appid);
4915         if (SQLITE_OK !=
4916                 sqlite3_exec(manifest_db, query, __icon_name_cb, (void *)data, &error_message)) {
4917                 _LOGE("Don't execute query = %s error message = %s\n", query,
4918                            error_message);
4919                 free(data);
4920                 sqlite3_free(error_message);
4921                 sqlite3_close(manifest_db);
4922                 return PMINFO_R_ERROR;
4923         }
4924         icon_path = __get_icon_with_path(data->iconpath);
4925         *icon = (char *)icon_path;
4926
4927         if (data) {
4928            free(data);
4929            data = NULL;
4930         }
4931         sqlite3_close(manifest_db);
4932         return PMINFO_R_OK;
4933 }
4934
4935
4936 API int pkgmgrinfo_appinfo_get_notification_icon(pkgmgrinfo_appinfo_h  handle, char **icon)
4937 {
4938         if (handle == NULL) {
4939                 _LOGE("appinfo handle is NULL\n");
4940                 return PMINFO_R_EINVAL;
4941         }
4942         if (icon == NULL) {
4943                 _LOGE("Argument supplied to hold return value is NULL\n");
4944                 return PMINFO_R_EINVAL;
4945         }
4946         int ret = -1;
4947         char *error_message = NULL;
4948         char query[MAX_QUERY_LEN] = {'\0'};
4949         char path_buf[PKG_STRING_LEN_MAX] = {'\0'};
4950         pkgmgr_iconpath_x *data = NULL;
4951         char *icon_path;
4952         char *readpath;
4953         char *appid;
4954
4955         ret = __open_manifest_db();
4956         if (ret == -1) {
4957                 _LOGE("Fail to open manifest DB\n");
4958                 return PMINFO_R_ERROR;
4959         }
4960
4961         ret = pkgmgrinfo_appinfo_get_appid(handle,&appid);
4962         if(ret < 0 || appid == NULL)
4963                 return PMINFO_R_ERROR;
4964
4965         data = (pkgmgr_iconpath_x *)calloc(1, sizeof(pkgmgr_iconpath_x));
4966         if (data == NULL) {
4967                 _LOGE("Failed to allocate memory for pkgmgr_datacontrol_x\n");
4968                 sqlite3_close(manifest_db);
4969                 return PMINFO_R_ERROR;
4970         }
4971
4972         snprintf(query, MAX_QUERY_LEN, "select app_icon from package_app_icon_localized_info where app_id='%s' and app_icon_section ='notification'", appid);
4973         if (SQLITE_OK !=
4974                 sqlite3_exec(manifest_db, query, __icon_name_cb, (void *)data, &error_message)) {
4975                 _LOGE("Don't execute query = %s error message = %s\n", query,
4976                            error_message);
4977                 free(data);
4978                 sqlite3_free(error_message);
4979                 sqlite3_close(manifest_db);
4980                 return PMINFO_R_ERROR;
4981         }
4982         icon_path = __get_icon_with_path(data->iconpath);
4983         *icon = (char *)icon_path;
4984
4985         if (data) {
4986            free(data);
4987            data = NULL;
4988         }
4989         sqlite3_close(manifest_db);
4990         return PMINFO_R_OK;
4991 }
4992
4993 API int pkgmgrinfo_appinfo_get_recent_image_type(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_recentimage *type)
4994 {
4995         if (handle == NULL) {
4996                 _LOGE("appinfo handle is NULL\n");
4997                 return PMINFO_R_EINVAL;
4998         }
4999         if (type == NULL) {
5000                 _LOGE("Argument supplied to hold return value is NULL\n");
5001                 return PMINFO_R_EINVAL;
5002         }
5003         char *val = NULL;
5004         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5005         val = (char *)info->uiapp_info->recentimage;
5006         if (val) {
5007                 if (strcasecmp(val, "capture") == 0)
5008                         *type = PMINFO_RECENTIMAGE_USE_CAPTURE;
5009                 else if (strcasecmp(val, "icon") == 0)
5010                         *type = PMINFO_RECENTIMAGE_USE_ICON;
5011                 else
5012                         *type = PMINFO_RECENTIMAGE_USE_NOTHING;
5013         }
5014
5015         return PMINFO_R_OK;
5016 }
5017
5018 API int pkgmgrinfo_appinfo_foreach_category(pkgmgrinfo_appinfo_h handle,
5019                         pkgmgrinfo_app_category_list_cb category_func, void *user_data)
5020 {
5021         if (handle == NULL) {
5022                 _LOGE("appinfo handle is NULL\n");
5023                 return PMINFO_R_EINVAL;
5024         }
5025         if (category_func == NULL) {
5026                 _LOGE("Callback function is NULL\n");
5027                 return PMINFO_R_EINVAL;
5028         }
5029         int ret = -1;
5030         category_x *ptr = NULL;
5031         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5032         if (info->app_component == PMINFO_UI_APP)
5033                 ptr = info->uiapp_info->category;
5034         else if (info->app_component == PMINFO_SVC_APP)
5035                 ptr = info->svcapp_info->category;
5036         else
5037                 return PMINFO_R_EINVAL;
5038         for (; ptr; ptr = ptr->next) {
5039                 ret = category_func(ptr->name, user_data);
5040                 if (ret < 0)
5041                         break;
5042         }
5043         return PMINFO_R_OK;
5044 }
5045
5046 API int pkgmgrinfo_appinfo_foreach_appcontrol(pkgmgrinfo_appinfo_h handle,
5047                         pkgmgrinfo_app_control_list_cb appcontrol_func, void *user_data)
5048 {
5049         if (handle == NULL) {
5050                 _LOGE("appinfo handle is NULL\n");
5051                 return PMINFO_R_EINVAL;
5052         }
5053         if (appcontrol_func == NULL) {
5054                 _LOGE("Callback function is NULL\n");
5055                 return PMINFO_R_EINVAL;
5056         }
5057         int i = 0;
5058         int ret = -1;
5059         int oc = 0;
5060         int mc = 0;
5061         int uc = 0;
5062         char *pkgid = NULL;
5063         char *manifest = NULL;
5064         char **operation = NULL;
5065         char **uri = NULL;
5066         char **mime = NULL;
5067         appcontrol_x *appcontrol = NULL;
5068         manifest_x *mfx = NULL;
5069         operation_x *op = NULL;
5070         uri_x *ui = NULL;
5071         mime_x *mi = NULL;
5072         pkgmgrinfo_app_component component;
5073         pkgmgrinfo_appcontrol_x *ptr = NULL;
5074         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
5075         if (ret < 0) {
5076                 _LOGE("Failed to get package name\n");
5077                 return PMINFO_R_ERROR;
5078         }
5079         ret = pkgmgrinfo_appinfo_get_component(handle, &component);
5080         if (ret < 0) {
5081                 _LOGE("Failed to get app component name\n");
5082                 return PMINFO_R_ERROR;
5083         }
5084         manifest = pkgmgr_parser_get_manifest_file(pkgid);
5085         if (manifest == NULL) {
5086                 _LOGE("Failed to fetch package manifest file\n");
5087                 return PMINFO_R_ERROR;
5088         }
5089         mfx = pkgmgr_parser_process_manifest_xml(manifest);
5090         if (mfx == NULL) {
5091                 _LOGE("Failed to parse package manifest file\n");
5092                 free(manifest);
5093                 manifest = NULL;
5094                 return PMINFO_R_ERROR;
5095         }
5096         free(manifest);
5097         ptr  = calloc(1, sizeof(pkgmgrinfo_appcontrol_x));
5098         if (ptr == NULL) {
5099                 _LOGE("Out of Memory!!!\n");
5100                 pkgmgr_parser_free_manifest_xml(mfx);
5101                 return PMINFO_R_ERROR;
5102         }
5103         /*Get Operation, Uri, Mime*/
5104         switch (component) {
5105         case PMINFO_UI_APP:
5106                 if (mfx->uiapplication) {
5107                         if (mfx->uiapplication->appcontrol) {
5108                                 appcontrol = mfx->uiapplication->appcontrol;
5109                         }
5110                 }
5111                 break;
5112         case PMINFO_SVC_APP:
5113                 if (mfx->serviceapplication) {
5114                         if (mfx->serviceapplication->appcontrol) {
5115                                 appcontrol = mfx->serviceapplication->appcontrol;
5116                         }
5117                 }
5118                 break;
5119         default:
5120                 break;
5121         }
5122         for (; appcontrol; appcontrol = appcontrol->next) {
5123                 op = appcontrol->operation;
5124                 for (; op; op = op->next)
5125                         oc = oc + 1;
5126                 op = appcontrol->operation;
5127
5128                 ui = appcontrol->uri;
5129                 for (; ui; ui = ui->next)
5130                         uc = uc + 1;
5131                 ui = appcontrol->uri;
5132
5133                 mi = appcontrol->mime;
5134                 for (; mi; mi = mi->next)
5135                         mc = mc + 1;
5136                 mi = appcontrol->mime;
5137
5138                 operation = (char **)calloc(oc, sizeof(char *));
5139                 for (i = 0; i < oc; i++) {
5140                         operation[i] = strndup(op->name, PKG_STRING_LEN_MAX - 1);
5141                         op = op->next;
5142                 }
5143
5144                 uri = (char **)calloc(uc, sizeof(char *));
5145                 for (i = 0; i < uc; i++) {
5146                         uri[i] = strndup(ui->name, PKG_STRING_LEN_MAX - 1);
5147                         ui = ui->next;
5148                 }
5149
5150                 mime = (char **)calloc(mc, sizeof(char *));
5151                 for (i = 0; i < mc; i++) {
5152                         mime[i] = strndup(mi->name, PKG_STRING_LEN_MAX - 1);
5153                         mi = mi->next;
5154                 }
5155                 /*populate appcontrol handle*/
5156                 ptr->operation_count = oc;
5157                 ptr->uri_count = uc;
5158                 ptr->mime_count = mc;
5159                 ptr->operation = operation;
5160                 ptr->uri = uri;
5161                 ptr->mime = mime;
5162                 ret = appcontrol_func((void *)ptr, user_data);
5163                 for (i = 0; i < oc; i++) {
5164                         if (operation[i]) {
5165                                 free(operation[i]);
5166                                 operation[i] = NULL;
5167                         }
5168                 }
5169                 if (operation) {
5170                         free(operation);
5171                         operation = NULL;
5172                 }
5173                 for (i = 0; i < uc; i++) {
5174                         if (uri[i]) {
5175                                 free(uri[i]);
5176                                 uri[i] = NULL;
5177                         }
5178                 }
5179                 if (uri) {
5180                         free(uri);
5181                         uri = NULL;
5182                 }
5183                 for (i = 0; i < mc; i++) {
5184                         if (mime[i]) {
5185                                 free(mime[i]);
5186                                 mime[i] = NULL;
5187                         }
5188                 }
5189                 if (mime) {
5190                         free(mime);
5191                         mime = NULL;
5192                 }
5193                 if (ret < 0)
5194                         break;
5195                 uc = 0;
5196                 mc = 0;
5197                 oc = 0;
5198         }
5199         pkgmgr_parser_free_manifest_xml(mfx);
5200         if (ptr) {
5201                 free(ptr);
5202                 ptr = NULL;
5203         }
5204         return PMINFO_R_OK;
5205 }
5206
5207 API int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h  handle, bool *nodisplay)
5208 {
5209         if (handle == NULL) {
5210                 _LOGE("appinfo handle is NULL\n");
5211                 return PMINFO_R_EINVAL;
5212         }
5213         if (nodisplay == NULL) {
5214                 _LOGE("Argument supplied to hold return value is NULL\n");
5215                 return PMINFO_R_EINVAL;
5216         }
5217         char *val = NULL;
5218         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5219         val = (char *)info->uiapp_info->nodisplay;
5220         if (val) {
5221                 if (strcasecmp(val, "true") == 0)
5222                         *nodisplay = 1;
5223                 else if (strcasecmp(val, "false") == 0)
5224                         *nodisplay = 0;
5225                 else
5226                         *nodisplay = 0;
5227         }
5228         return PMINFO_R_OK;
5229 }
5230
5231 API int pkgmgrinfo_appinfo_is_multiple(pkgmgrinfo_appinfo_h  handle, bool *multiple)
5232 {
5233         if (handle == NULL) {
5234                 _LOGE("appinfo handle is NULL\n");
5235                 return PMINFO_R_EINVAL;
5236         }
5237         if (multiple == NULL) {
5238                 _LOGE("Argument supplied to hold return value is NULL\n");
5239                 return PMINFO_R_EINVAL;
5240         }
5241         char *val = NULL;
5242         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5243         val = (char *)info->uiapp_info->multiple;
5244         if (val) {
5245                 if (strcasecmp(val, "true") == 0)
5246                         *multiple = 1;
5247                 else if (strcasecmp(val, "false") == 0)
5248                         *multiple = 0;
5249                 else
5250                         *multiple = 0;
5251         }
5252         return PMINFO_R_OK;
5253 }
5254
5255 API int pkgmgrinfo_appinfo_is_taskmanage(pkgmgrinfo_appinfo_h  handle, bool *taskmanage)
5256 {
5257         if (handle == NULL) {
5258                 _LOGE("appinfo handle is NULL\n");
5259                 return PMINFO_R_EINVAL;
5260         }
5261         if (taskmanage == NULL) {
5262                 _LOGE("Argument supplied to hold return value is NULL\n");
5263                 return PMINFO_R_EINVAL;
5264         }
5265         char *val = NULL;
5266         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5267         val = (char *)info->uiapp_info->taskmanage;
5268         if (val) {
5269                 if (strcasecmp(val, "true") == 0)
5270                         *taskmanage = 1;
5271                 else if (strcasecmp(val, "false") == 0)
5272                         *taskmanage = 0;
5273                 else
5274                         *taskmanage = 0;
5275         }
5276         return PMINFO_R_OK;
5277 }
5278
5279 API int pkgmgrinfo_appinfo_get_hwacceleration(pkgmgrinfo_appinfo_h  handle, pkgmgrinfo_app_hwacceleration *hwacceleration)
5280 {
5281         if (handle == NULL) {
5282                 _LOGE("appinfo handle is NULL\n");
5283                 return PMINFO_R_EINVAL;
5284         }
5285         if (hwacceleration == NULL) {
5286                 _LOGE("Argument supplied to hold return value is NULL\n");
5287                 return PMINFO_R_EINVAL;
5288         }
5289         char *val = NULL;
5290         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5291         val = (char *)info->uiapp_info->hwacceleration;
5292         if (val) {
5293                 if (strcasecmp(val, "not-use-GL") == 0)
5294                         *hwacceleration = PMINFO_HWACCELERATION_NOT_USE_GL;
5295                 else if (strcasecmp(val, "use-GL") == 0)
5296                         *hwacceleration = PMINFO_HWACCELERATION_USE_GL;
5297                 else
5298                         *hwacceleration = PMINFO_HWACCELERATION_USE_SYSTEM_SETTING;
5299         }
5300         return PMINFO_R_OK;
5301 }
5302
5303 API int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h  handle, bool *onboot)
5304 {
5305         if (handle == NULL) {
5306                 _LOGE("appinfo handle is NULL\n");
5307                 return PMINFO_R_EINVAL;
5308         }
5309         if (onboot == NULL) {
5310                 _LOGE("Argument supplied to hold return value is NULL\n");
5311                 return PMINFO_R_EINVAL;
5312         }
5313         char *val = NULL;
5314         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5315         val = (char *)info->svcapp_info->onboot;
5316         if (val) {
5317                 if (strcasecmp(val, "true") == 0)
5318                         *onboot = 1;
5319                 else if (strcasecmp(val, "false") == 0)
5320                         *onboot = 0;
5321                 else
5322                         *onboot = 0;
5323         }
5324         return PMINFO_R_OK;
5325 }
5326
5327 API int pkgmgrinfo_appinfo_is_autorestart(pkgmgrinfo_appinfo_h  handle, bool *autorestart)
5328 {
5329         if (handle == NULL) {
5330                 _LOGE("appinfo handle is NULL\n");
5331                 return PMINFO_R_EINVAL;
5332         }
5333         if (autorestart == NULL) {
5334                 _LOGE("Argument supplied to hold return value is NULL\n");
5335                 return PMINFO_R_EINVAL;
5336         }
5337         char *val = NULL;
5338         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5339         val = (char *)info->svcapp_info->autorestart;
5340         if (val) {
5341                 if (strcasecmp(val, "true") == 0)
5342                         *autorestart = 1;
5343                 else if (strcasecmp(val, "false") == 0)
5344                         *autorestart = 0;
5345                 else
5346                         *autorestart = 0;
5347         }
5348         return PMINFO_R_OK;
5349 }
5350
5351 API int pkgmgrinfo_appinfo_is_mainapp(pkgmgrinfo_appinfo_h  handle, bool *mainapp)
5352 {
5353         if (handle == NULL) {
5354                 _LOGE("appinfo handle is NULL\n");
5355                 return PMINFO_R_EINVAL;
5356         }
5357         if (mainapp == NULL) {
5358                 _LOGE("Argument supplied to hold return value is NULL\n");
5359                 return PMINFO_R_EINVAL;
5360         }
5361         char *val = NULL;
5362         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5363         val = (char *)info->uiapp_info->mainapp;
5364         if (val) {
5365                 if (strcasecmp(val, "true") == 0)
5366                         *mainapp = 1;
5367                 else if (strcasecmp(val, "false") == 0)
5368                         *mainapp = 0;
5369                 else
5370                         *mainapp = 0;
5371         }
5372         return PMINFO_R_OK;
5373 }
5374
5375 API int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h  handle)
5376 {
5377         if (handle == NULL) {
5378                 _LOGE("appinfo handle is NULL\n");
5379                 return PMINFO_R_EINVAL;
5380         }
5381         pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
5382         __cleanup_appinfo(info);
5383         return PMINFO_R_OK;
5384 }
5385
5386 API int pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h *handle)
5387 {
5388         return (pkgmgrinfo_pkginfo_filter_create(handle));
5389 }
5390
5391 API int pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle)
5392 {
5393         return (pkgmgrinfo_pkginfo_filter_destroy(handle));
5394 }
5395
5396 API int pkgmgrinfo_appinfo_filter_add_int(pkgmgrinfo_appinfo_filter_h handle,
5397                                 const char *property, const int value)
5398 {
5399         if (handle == NULL || property == NULL) {
5400                 _LOGE("Filter handle input parameter is NULL\n");
5401                 return PMINFO_R_EINVAL;
5402         }
5403         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
5404         char *val = NULL;
5405         GSList *link = NULL;
5406         int prop = -1;
5407         prop = _pminfo_appinfo_convert_to_prop_int(property);
5408         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_INT ||
5409                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_INT) {
5410                 _LOGE("Invalid Integer Property\n");
5411                 return PMINFO_R_EINVAL;
5412         }
5413         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5414         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
5415         if (node == NULL) {
5416                 _LOGE("Out of Memory!!!\n");
5417                 return PMINFO_R_ERROR;
5418         }
5419         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
5420         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
5421         if (val == NULL) {
5422                 _LOGE("Out of Memory\n");
5423                 free(node);
5424                 node = NULL;
5425                 return PMINFO_R_ERROR;
5426         }
5427         node->prop = prop;
5428         node->value = val;
5429         /*If API is called multiple times for same property, we should override the previous values.
5430         Last value set will be used for filtering.*/
5431         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5432         if (link)
5433                 filter->list = g_slist_delete_link(filter->list, link);
5434         filter->list = g_slist_append(filter->list, (gpointer)node);
5435         return PMINFO_R_OK;
5436
5437 }
5438
5439 API int pkgmgrinfo_appinfo_filter_add_bool(pkgmgrinfo_appinfo_filter_h handle,
5440                                 const char *property, const bool value)
5441 {
5442         if (handle == NULL || property == NULL) {
5443                 _LOGE("Filter handle input parameter is NULL\n");
5444                 return PMINFO_R_EINVAL;
5445         }
5446         char *val = NULL;
5447         GSList *link = NULL;
5448         int prop = -1;
5449         prop = _pminfo_appinfo_convert_to_prop_bool(property);
5450         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_BOOL ||
5451                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_BOOL) {
5452                 _LOGE("Invalid Boolean Property\n");
5453                 return PMINFO_R_EINVAL;
5454         }
5455         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5456         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
5457         if (node == NULL) {
5458                 _LOGE("Out of Memory!!!\n");
5459                 return PMINFO_R_ERROR;
5460         }
5461         if (value)
5462                 val = strndup("('true','True')", 15);
5463         else
5464                 val = strndup("('false','False')", 17);
5465         if (val == NULL) {
5466                 _LOGE("Out of Memory\n");
5467                 free(node);
5468                 node = NULL;
5469                 return PMINFO_R_ERROR;
5470         }
5471         node->prop = prop;
5472         node->value = val;
5473         /*If API is called multiple times for same property, we should override the previous values.
5474         Last value set will be used for filtering.*/
5475         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5476         if (link)
5477                 filter->list = g_slist_delete_link(filter->list, link);
5478         filter->list = g_slist_append(filter->list, (gpointer)node);
5479         return PMINFO_R_OK;
5480
5481 }
5482
5483 API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
5484                                 const char *property, const char *value)
5485 {
5486         if (handle == NULL || property == NULL || value == NULL) {
5487                 _LOGE("Filter handle input parameter is NULL\n");
5488                 return PMINFO_R_EINVAL;
5489         }
5490         char *val = NULL;
5491         pkgmgrinfo_node_x *ptr = NULL;
5492         char prev[PKG_STRING_LEN_MAX] = {'\0'};
5493         char temp[PKG_STRING_LEN_MAX] = {'\0'};
5494         GSList *link = NULL;
5495         int prop = -1;
5496         prop = _pminfo_appinfo_convert_to_prop_str(property);
5497         if (prop < E_PMINFO_APPINFO_PROP_APP_MIN_STR ||
5498                 prop > E_PMINFO_APPINFO_PROP_APP_MAX_STR) {
5499                 _LOGE("Invalid String Property\n");
5500                 return PMINFO_R_EINVAL;
5501         }
5502         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5503         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)calloc(1, sizeof(pkgmgrinfo_node_x));
5504         if (node == NULL) {
5505                 _LOGE("Out of Memory!!!\n");
5506                 return PMINFO_R_ERROR;
5507         }
5508         node->prop = prop;
5509         switch (prop) {
5510         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
5511                 if (strcmp(value, PMINFO_APPINFO_UI_APP) == 0)
5512                         val = strndup("uiapp", PKG_STRING_LEN_MAX - 1);
5513                 else
5514                         val = strndup("svcapp", PKG_STRING_LEN_MAX - 1);
5515                 node->value = val;
5516                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5517                 if (link)
5518                         filter->list = g_slist_delete_link(filter->list, link);
5519                 filter->list = g_slist_append(filter->list, (gpointer)node);
5520                 break;
5521         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
5522         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
5523         case E_PMINFO_APPINFO_PROP_APP_URI:
5524         case E_PMINFO_APPINFO_PROP_APP_MIME:
5525                 val = (char *)calloc(1, PKG_STRING_LEN_MAX);
5526                 if (val == NULL) {
5527                         _LOGE("Out of Memory\n");
5528                         free(node);
5529                         node = NULL;
5530                         return PMINFO_R_ERROR;
5531                 }
5532                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5533                 if (link) {
5534                         ptr = (pkgmgrinfo_node_x *)link->data;
5535                         strncpy(prev, ptr->value, PKG_STRING_LEN_MAX - 1);
5536                         _LOGE("Previous value is %s\n", prev);
5537                         filter->list = g_slist_delete_link(filter->list, link);
5538                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "%s , '%s'", prev, value);
5539                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
5540                         _LOGE("New value is %s\n", val);
5541                         node->value = val;
5542                         filter->list = g_slist_append(filter->list, (gpointer)node);
5543                         memset(temp, '\0', PKG_STRING_LEN_MAX);
5544                 } else {
5545                         snprintf(temp, PKG_STRING_LEN_MAX - 1, "'%s'", value);
5546                         strncpy(val, temp, PKG_STRING_LEN_MAX - 1);
5547                         _LOGE("First value is %s\n", val);
5548                         node->value = val;
5549                         filter->list = g_slist_append(filter->list, (gpointer)node);
5550                         memset(temp, '\0', PKG_STRING_LEN_MAX);
5551                 }
5552                 break;
5553         default:
5554                 node->value = strndup(value, PKG_STRING_LEN_MAX - 1);
5555                 link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
5556                 if (link)
5557                         filter->list = g_slist_delete_link(filter->list, link);
5558                 filter->list = g_slist_append(filter->list, (gpointer)node);
5559                 break;
5560         }
5561         return PMINFO_R_OK;
5562 }
5563
5564 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
5565 {
5566         if (handle == NULL || count == NULL) {
5567                 _LOGE("Filter handle input parameter is NULL\n");
5568                 return PMINFO_R_EINVAL;
5569         }
5570         char *syslocale = NULL;
5571         char *locale = NULL;
5572         char *condition = NULL;
5573         char *error_message = NULL;
5574         char query[MAX_QUERY_LEN] = {'\0'};
5575         char where[MAX_QUERY_LEN] = {'\0'};
5576         GSList *list;
5577         int ret = 0;
5578
5579         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5580         /*Get current locale*/
5581         syslocale = vconf_get_str(VCONFKEY_LANGSET);
5582         if (syslocale == NULL) {
5583                 _LOGE("current locale is NULL\n");
5584                 return PMINFO_R_ERROR;
5585         }
5586         locale = __convert_system_locale_to_manifest_locale(syslocale);
5587         if (locale == NULL) {
5588                 _LOGE("manifest locale is NULL\n");
5589                 free(syslocale);
5590                 return PMINFO_R_ERROR;
5591         }
5592         ret = __open_manifest_db();
5593         if (ret == -1) {
5594                 _LOGE("Fail to open manifest DB\n");
5595                 ret = PMINFO_R_ERROR;
5596                 goto err;
5597         }
5598
5599         /*Start constructing query*/
5600         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_APP, locale);
5601
5602         /*Get where clause*/
5603         for (list = filter->list; list; list = g_slist_next(list)) {
5604                 __get_filter_condition(list->data, &condition);
5605                 if (condition) {
5606                         strncat(where, condition, sizeof(where) - strlen(where) -1);
5607                         where[sizeof(where) - 1] = '\0';
5608                         free(condition);
5609                         condition = NULL;
5610                 }
5611                 if (g_slist_next(list)) {
5612                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
5613                         where[sizeof(where) - 1] = '\0';
5614                 }
5615         }
5616         _LOGE("where = %s\n", where);
5617         if (strlen(where) > 0) {
5618                 strncat(query, where, sizeof(query) - strlen(query) - 1);
5619                 query[sizeof(query) - 1] = '\0';
5620         }
5621         _LOGE("query = %s\n", query);
5622
5623         /*Execute Query*/
5624         if (SQLITE_OK !=
5625             sqlite3_exec(manifest_db, query, __count_cb, (void *)count, &error_message)) {
5626                 _LOGE("Don't execute query = %s error message = %s\n", query,
5627                        error_message);
5628                 sqlite3_free(error_message);
5629                 sqlite3_close(manifest_db);
5630                 ret = PMINFO_R_ERROR;
5631                 *count = 0;
5632                 goto err;
5633         }
5634         ret = PMINFO_R_OK;
5635 err:
5636         if (locale) {
5637                 free(locale);
5638                 locale = NULL;
5639         }
5640         if (syslocale) {
5641                 free(syslocale);
5642                 syslocale = NULL;
5643         }
5644         sqlite3_close(manifest_db);
5645         return ret;
5646 }
5647
5648 API int pkgmgrinfo_appinfo_filter_foreach_appinfo(pkgmgrinfo_appinfo_filter_h handle,
5649                                 pkgmgrinfo_app_list_cb app_cb, void * user_data)
5650 {
5651         if (handle == NULL || app_cb == NULL) {
5652                 _LOGE("Filter handle input parameter is NULL\n");
5653                 return PMINFO_R_EINVAL;
5654         }
5655         char *syslocale = NULL;
5656         char *locale = NULL;
5657         char *condition = NULL;
5658         char *error_message = NULL;
5659         char query[MAX_QUERY_LEN] = {'\0'};
5660         char where[MAX_QUERY_LEN] = {'\0'};
5661         GSList *list;
5662         int ret = 0;
5663         uiapplication_x *ptr1 = NULL;
5664         serviceapplication_x *ptr2 = NULL;
5665         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
5666         /*Get current locale*/
5667         syslocale = vconf_get_str(VCONFKEY_LANGSET);
5668         if (syslocale == NULL) {
5669                 _LOGE("current locale is NULL\n");
5670                 return PMINFO_R_ERROR;
5671         }
5672         locale = __convert_system_locale_to_manifest_locale(syslocale);
5673         if (locale == NULL) {
5674                 _LOGE("manifest locale is NULL\n");
5675                 free(syslocale);
5676                 return PMINFO_R_ERROR;
5677         }
5678         ret = __open_manifest_db();
5679         if (ret == -1) {
5680                 _LOGE("Fail to open manifest DB\n");
5681                 ret = PMINFO_R_ERROR;
5682                 goto err;
5683         }
5684         /*Start constructing query*/
5685         snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_LIST_APP, locale);
5686         /*Get where clause*/
5687         for (list = filter->list; list; list = g_slist_next(list)) {
5688                 __get_filter_condition(list->data, &condition);
5689                 if (condition) {
5690                         strncat(where, condition, sizeof(where) - strlen(where) -1);
5691                         where[sizeof(where) - 1] = '\0';
5692                         free(condition);
5693                         condition = NULL;
5694                 }
5695                 if (g_slist_next(list)) {
5696                         strncat(where, " and ", sizeof(where) - strlen(where) - 1);
5697                         where[sizeof(where) - 1] = '\0';
5698                 }
5699         }
5700         _LOGE("where = %s\n", where);
5701         if (strlen(where) > 0) {
5702                 strncat(query, where, sizeof(query) - strlen(query) - 1);
5703                 query[sizeof(query) - 1] = '\0';
5704         }
5705         _LOGE("query = %s\n", query);
5706         /*To get filtered list*/
5707         pkgmgr_pkginfo_x *info = NULL;
5708         info = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5709         if (info == NULL) {
5710                 _LOGE("Out of Memory!!!\n");
5711                 ret = PMINFO_R_ERROR;
5712                 goto err;
5713         }
5714         info->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5715         if (info->manifest_info == NULL) {
5716                 _LOGE("Out of Memory!!!\n");
5717                 ret = PMINFO_R_ERROR;
5718                 goto err;
5719         }
5720         /*To get detail app info for each member of filtered list*/
5721         pkgmgr_pkginfo_x *filtinfo = NULL;
5722         filtinfo = (pkgmgr_pkginfo_x *)calloc(1, sizeof(pkgmgr_pkginfo_x));
5723         if (filtinfo == NULL) {
5724                 _LOGE("Out of Memory!!!\n");
5725                 ret = PMINFO_R_ERROR;
5726                 goto err;
5727         }
5728         filtinfo->manifest_info = (manifest_x *)calloc(1, sizeof(manifest_x));
5729         if (filtinfo->manifest_info == NULL) {
5730                 _LOGE("Out of Memory!!!\n");
5731                 ret = PMINFO_R_ERROR;
5732                 goto err;
5733         }
5734         pkgmgr_appinfo_x *appinfo = (pkgmgr_appinfo_x *)calloc(1, sizeof(pkgmgr_appinfo_x));
5735         if (appinfo == NULL) {
5736                 _LOGE("Out of Memory!!!\n");
5737                 ret = PMINFO_R_ERROR;
5738                 goto err;
5739         }
5740         if (SQLITE_OK !=
5741             sqlite3_exec(manifest_db, query, __app_list_cb, (void *)info, &error_message)) {
5742                 _LOGE("Don't execute query = %s error message = %s\n", query,
5743                        error_message);
5744                 sqlite3_free(error_message);
5745                 sqlite3_close(manifest_db);
5746                 ret = PMINFO_R_ERROR;
5747                 goto err;
5748         }
5749         memset(query, '\0', MAX_QUERY_LEN);
5750         if (info->manifest_info->uiapplication) {
5751                 LISTHEAD(info->manifest_info->uiapplication, ptr1);
5752                 info->manifest_info->uiapplication = ptr1;
5753         }
5754         if (info->manifest_info->serviceapplication) {
5755                 LISTHEAD(info->manifest_info->serviceapplication, ptr2);
5756                 info->manifest_info->serviceapplication = ptr2;
5757         }
5758         /*Filtered UI Apps*/
5759         for(ptr1 = info->manifest_info->uiapplication; ptr1; ptr1 = ptr1->next)
5760         {
5761                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
5762                                                         ptr1->appid, "uiapp");
5763                 if (SQLITE_OK !=
5764                 sqlite3_exec(manifest_db, query, __uiapp_list_cb, (void *)filtinfo, &error_message)) {
5765                         _LOGE("Don't execute query = %s error message = %s\n", query,
5766                                error_message);
5767                         sqlite3_free(error_message);
5768                         sqlite3_close(manifest_db);
5769                         ret = PMINFO_R_ERROR;
5770                         goto err;
5771                 }
5772         }
5773         for(ptr2 = info->manifest_info->serviceapplication; ptr2; ptr2 = ptr2->next)
5774         {
5775                 snprintf(query, MAX_QUERY_LEN, "select * from package_app_info where app_id='%s' and app_component='%s'",
5776                                                         ptr2->appid, "svcapp");
5777                 if (SQLITE_OK !=
5778                 sqlite3_exec(manifest_db, query, __svcapp_list_cb, (void *)filtinfo, &error_message)) {
5779                         _LOGE("Don't execute query = %s error message = %s\n", query,
5780                                error_message);
5781                         sqlite3_free(error_message);
5782                         sqlite3_close(manifest_db);
5783                         ret = PMINFO_R_ERROR;
5784                         goto err;
5785                 }
5786         }
5787         if (filtinfo->manifest_info->uiapplication) {
5788                 LISTHEAD(filtinfo->manifest_info->uiapplication, ptr1);
5789                 filtinfo->manifest_info->uiapplication = ptr1;
5790         }
5791         /*If the callback func return < 0 we break and no more call back is called*/
5792         while(ptr1 != NULL)
5793         {
5794                 appinfo->uiapp_info = ptr1;
5795                 appinfo->app_component = PMINFO_UI_APP;
5796                 ret = app_cb((void *)appinfo, user_data);
5797                 if (ret < 0)
5798                         break;
5799                 ptr1 = ptr1->next;
5800         }
5801         /*Filtered Service Apps*/
5802         if (filtinfo->manifest_info->serviceapplication) {
5803                 LISTHEAD(filtinfo->manifest_info->serviceapplication, ptr2);
5804                 filtinfo->manifest_info->serviceapplication = ptr2;
5805         }
5806         /*If the callback func return < 0 we break and no more call back is called*/
5807         while(ptr2 != NULL)
5808         {
5809                 appinfo->svcapp_info = ptr2;
5810                 appinfo->app_component = PMINFO_SVC_APP;
5811                 ret = app_cb((void *)appinfo, user_data);
5812                 if (ret < 0)
5813                         break;
5814                 ptr2 = ptr2->next;
5815         }
5816         ret = PMINFO_R_OK;
5817 err:
5818         if (locale) {
5819                 free(locale);
5820                 locale = NULL;
5821         }
5822         if (syslocale) {
5823                 free(syslocale);
5824                 syslocale = NULL;
5825         }
5826         sqlite3_close(manifest_db);
5827         if (appinfo) {
5828                 free(appinfo);
5829                 appinfo = NULL;
5830         }
5831         __cleanup_pkginfo(info);
5832         __cleanup_pkginfo(filtinfo);
5833         return ret;
5834 }
5835
5836 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
5837 {
5838         if (!handle) {
5839                 _LOGE("Argument supplied is NULL\n");
5840                 return PMINFO_R_EINVAL;
5841         }
5842         int ret = 0;
5843         pkgmgr_certinfo_x *certinfo = NULL;
5844         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
5845         if (!certinfo) {
5846                 _LOGE("Malloc Failed\n");
5847                 return PMINFO_R_ERROR;
5848         }
5849         *handle = (void *)certinfo;
5850         /*Open db. It will be closed in destroy handle API*/
5851         ret = db_util_open_with_options(CERT_DB, &cert_db,
5852                                         SQLITE_OPEN_READONLY, NULL);
5853         if (ret != SQLITE_OK) {
5854                 _LOGE("connect db [%s] failed!\n", MANIFEST_DB);
5855                 free(certinfo);
5856                 return PMINFO_R_ERROR;
5857         }
5858         return PMINFO_R_OK;
5859 }
5860
5861 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle)
5862 {
5863         if (pkgid == NULL) {
5864                 _LOGE("package ID is NULL\n");
5865                 return PMINFO_R_EINVAL;
5866         }
5867         if (handle == NULL) {
5868                 _LOGE("Certinfo handle is NULL\n");
5869                 return PMINFO_R_EINVAL;
5870         }
5871         pkgmgr_certinfo_x *certinfo = NULL;
5872         char *error_message = NULL;
5873         int ret = PMINFO_R_OK;
5874         char query[MAX_QUERY_LEN] = {'\0'};
5875         int exist = 0;
5876
5877         /*validate pkgid*/
5878         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
5879         if (SQLITE_OK !=
5880             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
5881                 _LOGE("Don't execute query = %s error message = %s\n", query,
5882                        error_message);
5883                 sqlite3_free(error_message);
5884                 return PMINFO_R_ERROR;
5885         }
5886         if (exist == 0) {
5887                 _LOGE("Package not found in DB\n");
5888                 ret = PMINFO_R_ERROR;
5889                 goto err;
5890         }
5891         certinfo = (pkgmgr_certinfo_x *)handle;
5892         /*populate certinfo from DB*/
5893         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
5894         ret = __exec_certinfo_query(query, (void *)certinfo);
5895         if (ret == -1) {
5896                 _LOGE("Package Cert Info DB Information retrieval failed\n");
5897                 ret = PMINFO_R_ERROR;
5898                 goto err;
5899         }
5900         return PMINFO_R_OK;
5901 err:
5902         return ret;
5903 }
5904
5905 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle, pkgmgrinfo_cert_type cert_type, const char **cert_value)
5906 {
5907         if (!handle || !cert_value) {
5908                 _LOGE("Argument supplied is NULL\n");
5909                 return PMINFO_R_EINVAL;
5910         }
5911         if ((cert_type < PMINFO_AUTHOR_ROOT_CERT) || (cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT)) {
5912                 _LOGE("Invalid certificate type\n");
5913                 return PMINFO_R_EINVAL;
5914         }
5915         pkgmgr_certinfo_x *certinfo = NULL;
5916         char *error_message = NULL;
5917         int ret = PMINFO_R_OK;
5918         char query[MAX_QUERY_LEN] = {'\0'};
5919         int exist = 0;
5920         certinfo = (pkgmgr_certinfo_x *)handle;
5921         *cert_value = NULL;
5922         switch(cert_type) {
5923         case PMINFO_AUTHOR_SIGNER_CERT:
5924                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->auth_signer_cert);
5925                 if (SQLITE_OK !=
5926                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
5927                         _LOGE("Don't execute query = %s error message = %s\n", query,
5928                                error_message);
5929                         sqlite3_free(error_message);
5930                         return PMINFO_R_ERROR;
5931                 }
5932                 if (exist == 0)
5933                         *cert_value = certinfo->auth_signer_cert;
5934                 else {
5935                         memset(query, '\0', MAX_QUERY_LEN);
5936                         snprintf(query, MAX_QUERY_LEN, "select author_signer_cert from package_cert_info " \
5937                                 "where package='%s'", certinfo->auth_signer_cert);
5938                         free(certinfo->auth_signer_cert);
5939                         certinfo->auth_signer_cert = NULL;
5940                         ret = __exec_certinfo_query(query, (void *)certinfo);
5941                         if (ret == -1) {
5942                                 _LOGE("Cert Info DB Information retrieval failed\n");
5943                                 return PMINFO_R_ERROR;
5944                         }
5945                         *cert_value = certinfo->auth_signer_cert;
5946                 }
5947                 break;
5948         case PMINFO_AUTHOR_INTERMEDIATE_CERT:
5949                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->auth_im_cert);
5950                 if (SQLITE_OK !=
5951                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
5952                         _LOGE("Don't execute query = %s error message = %s\n", query,
5953                                error_message);
5954                         sqlite3_free(error_message);
5955                         return PMINFO_R_ERROR;
5956                 }
5957                 if (exist == 0)
5958                         *cert_value = certinfo->auth_im_cert;
5959                 else {
5960                         memset(query, '\0', MAX_QUERY_LEN);
5961                         snprintf(query, MAX_QUERY_LEN, "select author_im_cert from package_cert_info " \
5962                                 "where package='%s'", certinfo->auth_im_cert);
5963                         free(certinfo->auth_im_cert);
5964                         certinfo->auth_im_cert = NULL;
5965                         ret = __exec_certinfo_query(query, (void *)certinfo);
5966                         if (ret == -1) {
5967                                 _LOGE("Cert Info DB Information retrieval failed\n");
5968                                 return PMINFO_R_ERROR;
5969                         }
5970                         *cert_value = certinfo->auth_im_cert;
5971                 }
5972                 break;
5973         case PMINFO_AUTHOR_ROOT_CERT:
5974                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->auth_root_cert);
5975                 if (SQLITE_OK !=
5976                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
5977                         _LOGE("Don't execute query = %s error message = %s\n", query,
5978                                error_message);
5979                         sqlite3_free(error_message);
5980                         return PMINFO_R_ERROR;
5981                 }
5982                 if (exist == 0)
5983                         *cert_value = certinfo->auth_root_cert;
5984                 else {
5985                         memset(query, '\0', MAX_QUERY_LEN);
5986                         snprintf(query, MAX_QUERY_LEN, "select author_root_cert from package_cert_info " \
5987                                 "where package='%s'", certinfo->auth_root_cert);
5988                         free(certinfo->auth_root_cert);
5989                         certinfo->auth_root_cert = NULL;
5990                         ret = __exec_certinfo_query(query, (void *)certinfo);
5991                         if (ret == -1) {
5992                                 _LOGE("Cert Info DB Information retrieval failed\n");
5993                                 return PMINFO_R_ERROR;
5994                         }
5995                         *cert_value = certinfo->auth_root_cert;
5996                 }
5997                 break;
5998         case PMINFO_DISTRIBUTOR_SIGNER_CERT:
5999                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->dist_signer_cert);
6000                 if (SQLITE_OK !=
6001                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6002                         _LOGE("Don't execute query = %s error message = %s\n", query,
6003                                error_message);
6004                         sqlite3_free(error_message);
6005                         return PMINFO_R_ERROR;
6006                 }
6007                 if (exist == 0)
6008                         *cert_value = certinfo->dist_signer_cert;
6009                 else {
6010                         memset(query, '\0', MAX_QUERY_LEN);
6011                         snprintf(query, MAX_QUERY_LEN, "select dist_signer_cert from package_cert_info " \
6012                                 "where package='%s'", certinfo->dist_signer_cert);
6013                         free(certinfo->dist_signer_cert);
6014                         certinfo->dist_signer_cert = NULL;
6015                         ret = __exec_certinfo_query(query, (void *)certinfo);
6016                         if (ret == -1) {
6017                                 _LOGE("Cert Info DB Information retrieval failed\n");
6018                                 return PMINFO_R_ERROR;
6019                         }
6020                         *cert_value = certinfo->dist_signer_cert;
6021                 }
6022                 break;
6023         case PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT:
6024                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->dist_im_cert);
6025                 if (SQLITE_OK !=
6026                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6027                         _LOGE("Don't execute query = %s error message = %s\n", query,
6028                                error_message);
6029                         sqlite3_free(error_message);
6030                         return PMINFO_R_ERROR;
6031                 }
6032                 if (exist == 0)
6033                         *cert_value = certinfo->dist_im_cert;
6034                 else {
6035                         memset(query, '\0', MAX_QUERY_LEN);
6036                         snprintf(query, MAX_QUERY_LEN, "select dist_im_cert from package_cert_info " \
6037                                 "where package='%s'", certinfo->dist_im_cert);
6038                         free(certinfo->dist_im_cert);
6039                         certinfo->dist_im_cert = NULL;
6040                         ret = __exec_certinfo_query(query, (void *)certinfo);
6041                         if (ret == -1) {
6042                                 _LOGE("Cert Info DB Information retrieval failed\n");
6043                                 return PMINFO_R_ERROR;
6044                         }
6045                         *cert_value = certinfo->dist_im_cert;
6046                 }
6047                 break;
6048         case PMINFO_DISTRIBUTOR_ROOT_CERT:
6049                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->dist_root_cert);
6050                 if (SQLITE_OK !=
6051                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6052                         _LOGE("Don't execute query = %s error message = %s\n", query,
6053                                error_message);
6054                         sqlite3_free(error_message);
6055                         return PMINFO_R_ERROR;
6056                 }
6057                 if (exist == 0)
6058                         *cert_value = certinfo->dist_root_cert;
6059                 else {
6060                         memset(query, '\0', MAX_QUERY_LEN);
6061                         snprintf(query, MAX_QUERY_LEN, "select dist_root_cert from package_cert_info " \
6062                                 "where package='%s'", certinfo->dist_root_cert);
6063                         free(certinfo->dist_root_cert);
6064                         certinfo->dist_root_cert = NULL;
6065                         ret = __exec_certinfo_query(query, (void *)certinfo);
6066                         if (ret == -1) {
6067                                 _LOGE("Cert Info DB Information retrieval failed\n");
6068                                 return PMINFO_R_ERROR;
6069                         }
6070                         *cert_value = certinfo->dist_root_cert;
6071                 }
6072                 break;
6073         case PMINFO_DISTRIBUTOR2_SIGNER_CERT:
6074                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->dist2_signer_cert);
6075                 if (SQLITE_OK !=
6076                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6077                         _LOGE("Don't execute query = %s error message = %s\n", query,
6078                                error_message);
6079                         sqlite3_free(error_message);
6080                         return PMINFO_R_ERROR;
6081                 }
6082                 if (exist == 0)
6083                         *cert_value = certinfo->dist2_signer_cert;
6084                 else {
6085                         memset(query, '\0', MAX_QUERY_LEN);
6086                         snprintf(query, MAX_QUERY_LEN, "select dist2_signer_cert from package_cert_info " \
6087                                 "where package='%s'", certinfo->dist2_signer_cert);
6088                         free(certinfo->dist2_signer_cert);
6089                         certinfo->dist2_signer_cert = NULL;
6090                         ret = __exec_certinfo_query(query, (void *)certinfo);
6091                         if (ret == -1) {
6092                                 _LOGE("Cert Info DB Information retrieval failed\n");
6093                                 return PMINFO_R_ERROR;
6094                         }
6095                         *cert_value = certinfo->dist2_signer_cert;
6096                 }
6097                 break;
6098         case PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT:
6099                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->dist2_im_cert);
6100                 if (SQLITE_OK !=
6101                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6102                         _LOGE("Don't execute query = %s error message = %s\n", query,
6103                                error_message);
6104                         sqlite3_free(error_message);
6105                         return PMINFO_R_ERROR;
6106                 }
6107                 if (exist == 0)
6108                         *cert_value = certinfo->dist2_im_cert;
6109                 else {
6110                         memset(query, '\0', MAX_QUERY_LEN);
6111                         snprintf(query, MAX_QUERY_LEN, "select dist2_im_cert from package_cert_info " \
6112                                 "where package='%s'", certinfo->dist2_im_cert);
6113                         free(certinfo->dist2_im_cert);
6114                         certinfo->dist2_im_cert = NULL;
6115                         ret = __exec_certinfo_query(query, (void *)certinfo);
6116                         if (ret == -1) {
6117                                 _LOGE("Cert Info DB Information retrieval failed\n");
6118                                 return PMINFO_R_ERROR;
6119                         }
6120                         *cert_value = certinfo->dist2_im_cert;
6121                 }
6122                 break;
6123         case PMINFO_DISTRIBUTOR2_ROOT_CERT:
6124                 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", certinfo->dist2_root_cert);
6125                 if (SQLITE_OK !=
6126                     sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6127                         _LOGE("Don't execute query = %s error message = %s\n", query,
6128                                error_message);
6129                         sqlite3_free(error_message);
6130                         return PMINFO_R_ERROR;
6131                 }
6132                 if (exist == 0)
6133                         *cert_value = certinfo->dist2_root_cert;
6134                 else {
6135                         memset(query, '\0', MAX_QUERY_LEN);
6136                         snprintf(query, MAX_QUERY_LEN, "select dist2_root_cert from package_cert_info " \
6137                                 "where package='%s'", certinfo->dist2_root_cert);
6138                         free(certinfo->dist2_root_cert);
6139                         certinfo->dist2_root_cert = NULL;
6140                         ret = __exec_certinfo_query(query, (void *)certinfo);
6141                         if (ret == -1) {
6142                                 _LOGE("Cert Info DB Information retrieval failed\n");
6143                                 return PMINFO_R_ERROR;
6144                         }
6145                         *cert_value = certinfo->dist2_root_cert;
6146                 }
6147                 break;
6148         }
6149         return PMINFO_R_OK;
6150 }
6151
6152 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
6153 {
6154         if (!handle) {
6155                 _LOGE("Argument supplied is NULL\n");
6156                 return PMINFO_R_EINVAL;
6157         }
6158         pkgmgr_certinfo_x *certinfo = NULL;
6159         certinfo = (pkgmgr_certinfo_x *)handle;
6160         if (certinfo->pkgid) {
6161                 free(certinfo->pkgid);
6162                 certinfo->pkgid = NULL;
6163         }
6164         if (certinfo->auth_signer_cert) {
6165                 free(certinfo->auth_signer_cert);
6166                 certinfo->auth_signer_cert = NULL;
6167         }
6168         if (certinfo->auth_im_cert) {
6169                 free(certinfo->auth_im_cert);
6170                 certinfo->auth_im_cert = NULL;
6171         }
6172         if (certinfo->auth_root_cert) {
6173                 free(certinfo->auth_root_cert);
6174                 certinfo->auth_root_cert = NULL;
6175         }
6176         if (certinfo->dist_signer_cert) {
6177                 free(certinfo->dist_signer_cert);
6178                 certinfo->dist_signer_cert = NULL;
6179         }
6180         if (certinfo->dist_im_cert) {
6181                 free(certinfo->dist_im_cert);
6182                 certinfo->dist_im_cert = NULL;
6183         }
6184         if (certinfo->dist_root_cert) {
6185                 free(certinfo->dist_root_cert);
6186                 certinfo->dist_root_cert = NULL;
6187         }
6188         if (certinfo->dist2_signer_cert) {
6189                 free(certinfo->dist2_signer_cert);
6190                 certinfo->dist2_signer_cert = NULL;
6191         }
6192         if (certinfo->dist2_im_cert) {
6193                 free(certinfo->dist2_im_cert);
6194                 certinfo->dist2_im_cert = NULL;
6195         }
6196         if (certinfo->dist2_root_cert) {
6197                 free(certinfo->dist2_root_cert);
6198                 certinfo->dist2_root_cert = NULL;
6199         }
6200         free(certinfo);
6201         certinfo = NULL;
6202         sqlite3_close(cert_db);
6203         return PMINFO_R_OK;
6204 }
6205
6206 API int pkgmgrinfo_create_certinfo_set_handle(pkgmgrinfo_instcertinfo_h *handle)
6207 {
6208         if (!handle) {
6209                 _LOGE("Argument supplied is NULL\n");
6210                 return PMINFO_R_EINVAL;
6211         }
6212         pkgmgr_instcertinfo_x *certinfo = NULL;
6213         int ret = 0;
6214         certinfo = calloc(1, sizeof(pkgmgr_instcertinfo_x));
6215         if (!certinfo) {
6216                 _LOGE("Malloc Failed\n");
6217                 return PMINFO_R_ERROR;
6218         }
6219         *handle = (void *)certinfo;
6220         /*Open db. It will be closed in destroy handle API*/
6221         ret = db_util_open(CERT_DB, &cert_db,
6222                 DB_UTIL_REGISTER_HOOK_METHOD);
6223         if (ret != SQLITE_OK) {
6224                 _LOGE("connect db [%s] failed!\n", CERT_DB);
6225                 free(certinfo);
6226                 return PMINFO_R_ERROR;
6227         }
6228         return PMINFO_R_OK;
6229 }
6230
6231 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_instcert_type cert_type, char *cert_value)
6232 {
6233         if (!handle || !cert_value) {
6234                 _LOGE("Argument supplied is NULL\n");
6235                 return PMINFO_R_EINVAL;
6236         }
6237         if ((cert_type < PMINFO_SET_AUTHOR_ROOT_CERT) || (cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT)) {
6238                 _LOGE("Invalid certificate type\n");
6239                 return PMINFO_R_EINVAL;
6240         }
6241         char query[MAX_QUERY_LEN] = {'\0'};
6242         pkgmgr_instcertinfo_x *certinfo = NULL;
6243         int ret = 0;
6244         certinfo = (pkgmgr_instcertinfo_x *)handle;
6245         switch(cert_type) {
6246         case PMINFO_SET_AUTHOR_SIGNER_CERT:
6247                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6248                         "where author_signer_cert='%s'", cert_value);
6249                 ret = __exec_certinfo_query(query, (void *)certinfo);
6250                 if (ret == -1) {
6251                         _LOGE("Cert Info DB Information retrieval failed\n");
6252                         return PMINFO_R_ERROR;
6253                 }
6254                 if (certinfo->pkgid)
6255                         certinfo->auth_signer_cert = strdup(certinfo->pkgid);
6256                 else
6257                         certinfo->auth_signer_cert = strdup(cert_value);
6258                 break;
6259         case PMINFO_SET_AUTHOR_INTERMEDIATE_CERT:
6260                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6261                         "where author_im_cert='%s'", cert_value);
6262                 ret = __exec_certinfo_query(query, (void *)certinfo);
6263                 if (ret == -1) {
6264                         _LOGE("Cert Info DB Information retrieval failed\n");
6265                         return PMINFO_R_ERROR;
6266                 }
6267                 if (certinfo->pkgid)
6268                         certinfo->auth_im_cert = strdup(certinfo->pkgid);
6269                 else
6270                         certinfo->auth_im_cert = strdup(cert_value);
6271                 break;
6272         case PMINFO_SET_AUTHOR_ROOT_CERT:
6273                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6274                         "where author_root_cert='%s'", cert_value);
6275                 ret = __exec_certinfo_query(query, (void *)certinfo);
6276                 if (ret == -1) {
6277                         _LOGE("Cert Info DB Information retrieval failed\n");
6278                         return PMINFO_R_ERROR;
6279                 }
6280                 if (certinfo->pkgid)
6281                         certinfo->auth_root_cert = strdup(certinfo->pkgid);
6282                 else
6283                         certinfo->auth_root_cert = strdup(cert_value);
6284                 break;
6285         case PMINFO_SET_DISTRIBUTOR_SIGNER_CERT:
6286                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6287                         "where dist_signer_cert='%s'", cert_value);
6288                 ret = __exec_certinfo_query(query, (void *)certinfo);
6289                 if (ret == -1) {
6290                         _LOGE("Cert Info DB Information retrieval failed\n");
6291                         return PMINFO_R_ERROR;
6292                 }
6293                 if (certinfo->pkgid)
6294                         certinfo->dist_signer_cert = strdup(certinfo->pkgid);
6295                 else
6296                         certinfo->dist_signer_cert = strdup(cert_value);
6297                 break;
6298         case PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT:
6299                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6300                         "where dist_im_cert='%s'", cert_value);
6301                 ret = __exec_certinfo_query(query, (void *)certinfo);
6302                 if (ret == -1) {
6303                         _LOGE("Cert Info DB Information retrieval failed\n");
6304                         return PMINFO_R_ERROR;
6305                 }
6306                 if (certinfo->pkgid)
6307                         certinfo->dist_im_cert = strdup(certinfo->pkgid);
6308                 else
6309                         certinfo->dist_im_cert = strdup(cert_value);
6310                 break;
6311         case PMINFO_SET_DISTRIBUTOR_ROOT_CERT:
6312                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6313                         "where dist_root_cert='%s'", cert_value);
6314                 ret = __exec_certinfo_query(query, (void *)certinfo);
6315                 if (ret == -1) {
6316                         _LOGE("Cert Info DB Information retrieval failed\n");
6317                         return PMINFO_R_ERROR;
6318                 }
6319                 if (certinfo->pkgid)
6320                         certinfo->dist_root_cert = strdup(certinfo->pkgid);
6321                 else
6322                         certinfo->dist_root_cert = strdup(cert_value);
6323                 break;
6324         case PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT:
6325                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6326                         "where dist2_signer_cert='%s'", cert_value);
6327                 ret = __exec_certinfo_query(query, (void *)certinfo);
6328                 if (ret == -1) {
6329                         _LOGE("Cert Info DB Information retrieval failed\n");
6330                         return PMINFO_R_ERROR;
6331                 }
6332                 if (certinfo->pkgid)
6333                         certinfo->dist2_signer_cert = strdup(certinfo->pkgid);
6334                 else
6335                         certinfo->dist2_signer_cert = strdup(cert_value);
6336                 break;
6337         case PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT:
6338                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6339                         "where dist2_im_cert='%s'", cert_value);
6340                 ret = __exec_certinfo_query(query, (void *)certinfo);
6341                 if (ret == -1) {
6342                         _LOGE("Cert Info DB Information retrieval failed\n");
6343                         return PMINFO_R_ERROR;
6344                 }
6345                 if (certinfo->pkgid)
6346                         certinfo->dist2_im_cert = strdup(certinfo->pkgid);
6347                 else
6348                         certinfo->dist2_im_cert = strdup(cert_value);
6349                 break;
6350         case PMINFO_SET_DISTRIBUTOR2_ROOT_CERT:
6351                 snprintf(query, MAX_QUERY_LEN, "select package from package_cert_info " \
6352                         "where dist2_root_cert='%s'", cert_value);
6353                 ret = __exec_certinfo_query(query, (void *)certinfo);
6354                 if (ret == -1) {
6355                         _LOGE("Cert Info DB Information retrieval failed\n");
6356                         return PMINFO_R_ERROR;
6357                 }
6358                 if (certinfo->pkgid)
6359                         certinfo->dist2_root_cert = strdup(certinfo->pkgid);
6360                 else
6361                         certinfo->dist2_root_cert = strdup(cert_value);
6362                 break;
6363         }
6364         if (certinfo->pkgid) {
6365                 free(certinfo->pkgid);
6366                 certinfo->pkgid = NULL;
6367         }
6368         return PMINFO_R_OK;
6369 }
6370
6371 API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle)
6372 {
6373         if (!handle || !pkgid) {
6374                 _LOGE("Argument supplied is NULL\n");
6375                 return PMINFO_R_EINVAL;
6376         }
6377         int ret = -1;
6378         char *error_message = NULL;
6379         int exist = -1;
6380         char query[MAX_QUERY_LEN] = {'\0'};
6381         char *vquery = NULL;
6382         int len = 0;
6383         pkgmgr_instcertinfo_x *info = (pkgmgr_instcertinfo_x *)handle;
6384         info->pkgid = strdup(pkgid);
6385         snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
6386         if (SQLITE_OK !=
6387             sqlite3_exec(cert_db, query, __validate_cb, (void *)&exist, &error_message)) {
6388                 _LOGE("Don't execute query = %s error message = %s\n", query,
6389                        error_message);
6390                 sqlite3_free(error_message);
6391                 return PMINFO_R_ERROR;
6392         }
6393
6394         len = 4096;
6395         if (info->auth_root_cert)
6396                 len += strlen(info->auth_root_cert);
6397         if (info->auth_im_cert)
6398                 len += strlen(info->auth_im_cert);
6399         if (info->auth_signer_cert)
6400                 len += strlen(info->auth_signer_cert);
6401         if (info->dist_root_cert)
6402                 len += strlen(info->dist_root_cert);
6403         if (info->dist_im_cert)
6404                 len += strlen(info->dist_im_cert);
6405         if (info->dist_signer_cert)
6406                 len += strlen(info->dist_signer_cert);
6407         if (info->dist2_root_cert)
6408                 len += strlen(info->dist2_root_cert);
6409         if (info->dist2_im_cert)
6410                 len += strlen(info->dist2_im_cert);
6411         if (info->dist2_signer_cert)
6412                 len += strlen(info->dist2_signer_cert);
6413         vquery = (char *)calloc(1, len);
6414
6415         if (exist == 0) {
6416                 _LOGE("pkgid not found in DB\n");
6417                 /*insert*/
6418                 snprintf(vquery, len,
6419                          "insert into package_cert_info(package, author_root_cert, author_im_cert, author_signer_cert, dist_root_cert, " \
6420                         "dist_im_cert, dist_signer_cert, dist2_root_cert, dist2_im_cert, dist2_signer_cert) " \
6421                         "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
6422                          info->pkgid, info->auth_root_cert, info->auth_im_cert, info->auth_signer_cert, info->dist_root_cert, info->dist_im_cert,
6423                          info->dist_signer_cert, info->dist2_root_cert, info->dist2_im_cert, info->dist2_signer_cert);
6424         } else {
6425                 _LOGE("pkgid exists in DB..Update it\n");
6426                 /*Update*/
6427                 snprintf(vquery, len,
6428                          "update package_cert_info set author_root_cert='%s', author_im_cert='%s', author_signer_cert='%s', dist_root_cert='%s', " \
6429                         "dist_im_cert='%s', dist_signer_cert='%s', dist2_root_cert='%s', dist2_im_cert='%s', dist2_signer_cert='%s' " \
6430                         "where package='%s'",\
6431                          info->auth_root_cert, info->auth_im_cert, info->auth_signer_cert, info->dist_root_cert, info->dist_im_cert,
6432                          info->dist_signer_cert, info->dist2_root_cert, info->dist2_im_cert, info->dist2_signer_cert, info->pkgid);
6433         }
6434         if (SQLITE_OK !=
6435             sqlite3_exec(cert_db, vquery, NULL, NULL, &error_message)) {
6436                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
6437                        error_message);
6438                 sqlite3_free(error_message);
6439                 return PMINFO_R_ERROR;
6440         }
6441         return PMINFO_R_OK;
6442 }
6443
6444 API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
6445 {
6446         if (!handle) {
6447                 _LOGE("Argument supplied is NULL\n");
6448                 return PMINFO_R_EINVAL;
6449         }
6450         pkgmgr_instcertinfo_x *certinfo = NULL;
6451         certinfo = (pkgmgr_instcertinfo_x *)handle;
6452         if (certinfo->pkgid) {
6453                 free(certinfo->pkgid);
6454                 certinfo->pkgid = NULL;
6455         }
6456         if (certinfo->auth_signer_cert) {
6457                 free(certinfo->auth_signer_cert);
6458                 certinfo->auth_signer_cert = NULL;
6459         }
6460         if (certinfo->auth_im_cert) {
6461                 free(certinfo->auth_im_cert);
6462                 certinfo->auth_im_cert = NULL;
6463         }
6464         if (certinfo->auth_root_cert) {
6465                 free(certinfo->auth_root_cert);
6466                 certinfo->auth_root_cert = NULL;
6467         }
6468         if (certinfo->dist_signer_cert) {
6469                 free(certinfo->dist_signer_cert);
6470                 certinfo->dist_signer_cert = NULL;
6471         }
6472         if (certinfo->dist_im_cert) {
6473                 free(certinfo->dist_im_cert);
6474                 certinfo->dist_im_cert = NULL;
6475         }
6476         if (certinfo->dist_root_cert) {
6477                 free(certinfo->dist_root_cert);
6478                 certinfo->dist_root_cert = NULL;
6479         }
6480         if (certinfo->dist2_signer_cert) {
6481                 free(certinfo->dist2_signer_cert);
6482                 certinfo->dist2_signer_cert = NULL;
6483         }
6484         if (certinfo->dist2_im_cert) {
6485                 free(certinfo->dist2_im_cert);
6486                 certinfo->dist2_im_cert = NULL;
6487         }
6488         if (certinfo->dist2_root_cert) {
6489                 free(certinfo->dist2_root_cert);
6490                 certinfo->dist2_root_cert = NULL;
6491         }
6492         free(certinfo);
6493         certinfo = NULL;
6494         sqlite3_close(cert_db);
6495         return PMINFO_R_OK;
6496 }
6497
6498 API int pkgmgrinfo_delete_certinfo(const char *pkgid)
6499 {
6500         if (!pkgid) {
6501                 _LOGE("Argument supplied is NULL\n");
6502                 return PMINFO_R_EINVAL;
6503         }
6504         int ret = -1;
6505         int i = 0;
6506         char *error_message = NULL;
6507         char query[MAX_QUERY_LEN] = {'\0'};
6508         ret = db_util_open(CERT_DB, &cert_db,
6509                 DB_UTIL_REGISTER_HOOK_METHOD);
6510         if (ret != SQLITE_OK) {
6511                 _LOGE("connect db [%s] failed!\n", CERT_DB);
6512                 return PMINFO_R_ERROR;
6513         }
6514         /*First make copy of all entries for which other packages have an index here*/
6515         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package!='%s'", pkgid);
6516         if (SQLITE_OK !=
6517             sqlite3_exec(cert_db, query, __delete_certinfo_cb, (void *)pkgid, &error_message)) {
6518                 _LOGE("Don't execute query = %s error message = %s\n", query,
6519                        error_message);
6520                 sqlite3_free(error_message);
6521                 sqlite3_close(cert_db);
6522                 ret = PMINFO_R_ERROR;
6523                 goto err;
6524         }
6525         /*Now delete the entry from db*/
6526         snprintf(query, MAX_QUERY_LEN, "delete from package_cert_info where package='%s'", pkgid);
6527         if (SQLITE_OK !=
6528             sqlite3_exec(cert_db, query, NULL, NULL, &error_message)) {
6529                 _LOGE("Don't execute query = %s error message = %s\n", query,
6530                        error_message);
6531                 sqlite3_free(error_message);
6532                 sqlite3_close(cert_db);
6533                 ret = PMINFO_R_ERROR;
6534                 goto err;
6535         }
6536         ret = PMINFO_R_OK;
6537 err:
6538         sqlite3_close(cert_db);
6539         for (i = 0; i < 9; i++) {
6540                 gflag[i] = 0;
6541                 if (gpkgcert[i]) {
6542                         free(gpkgcert[i]);
6543                         gpkgcert[i] = NULL;
6544                 }
6545         }
6546         return ret;
6547 }
6548
6549 API int pkgmgrinfo_create_pkgdbinfo(const char *pkgid, pkgmgrinfo_pkgdbinfo_h *handle)
6550 {
6551         if (!pkgid || !handle) {
6552                 _LOGE("Argument supplied is NULL\n");
6553                 return PMINFO_R_EINVAL;
6554         }
6555         manifest_x *mfx = NULL;
6556         mfx = calloc(1, sizeof(manifest_x));
6557         if (!mfx) {
6558                 _LOGE("Malloc Failed\n");
6559                 return PMINFO_R_ERROR;
6560         }
6561         mfx->package = strdup(pkgid);
6562         *handle = (void *)mfx;
6563         return PMINFO_R_OK;
6564 }
6565
6566 API int pkgmgrinfo_set_type_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *type)
6567 {
6568         if (!type || !handle) {
6569                 _LOGE("Argument supplied is NULL\n");
6570                 return PMINFO_R_EINVAL;
6571         }
6572         int len = strlen(type);
6573         manifest_x *mfx = (manifest_x *)handle;
6574         if (len > PKG_TYPE_STRING_LEN_MAX) {
6575                 _LOGE("pkg type length exceeds the max limit\n");
6576                 return PMINFO_R_EINVAL;
6577         }
6578         if (mfx->type == NULL)
6579                 mfx->type = strndup(type, PKG_TYPE_STRING_LEN_MAX);
6580         else
6581                 mfx->type = type;
6582
6583         return PMINFO_R_OK;
6584 }
6585
6586 API int pkgmgrinfo_set_version_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *version)
6587 {
6588         if (!version || !handle) {
6589                 _LOGE("Argument supplied is NULL\n");
6590                 return PMINFO_R_EINVAL;
6591         }
6592         int len = strlen(version);
6593         manifest_x *mfx = (manifest_x *)handle;
6594         if (len > PKG_VERSION_STRING_LEN_MAX) {
6595                 _LOGE("pkg version length exceeds the max limit\n");
6596                 return PMINFO_R_EINVAL;
6597         }
6598         if (mfx->version == NULL)
6599                 mfx->version = strndup(version, PKG_VERSION_STRING_LEN_MAX);
6600         else
6601                 mfx->version = version;
6602
6603         return PMINFO_R_OK;
6604 }
6605
6606 API int pkgmgrinfo_set_install_location_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
6607 {
6608         if (!handle) {
6609                 _LOGE("Argument supplied is NULL\n");
6610                 return PMINFO_R_EINVAL;
6611         }
6612         if (location < 0 || location > 1) {
6613                 _LOGE("Argument supplied is invalid\n");
6614                 return PMINFO_R_EINVAL;
6615         }
6616         manifest_x *mfx = (manifest_x *)handle;
6617         if (mfx->installlocation == NULL) {
6618                 mfx->installlocation = (char *)calloc(1, strlen("prefer-external"));
6619                 if (mfx->installlocation == NULL) {
6620                         _LOGE("Malloc Failed\n");
6621                         return PMINFO_R_ERROR;
6622                 }
6623         }
6624         if (location == INSTALL_INTERNAL) {
6625                 strcpy(mfx->installlocation, "internal-only");
6626         } else if (location == INSTALL_EXTERNAL) {
6627                 strcpy(mfx->installlocation, "prefer-external");
6628         } else {
6629                 _LOGE("Invalid location type\n");
6630                 return PMINFO_R_ERROR;
6631         }
6632         return PMINFO_R_OK;
6633 }
6634
6635 API int pkgmgrinfo_set_size_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *size)
6636 {
6637         if (!handle) {
6638                 _LOGE("Argument supplied is NULL\n");
6639                 return PMINFO_R_EINVAL;
6640         }
6641         if (size == NULL) {
6642                 _LOGE("Argument supplied is NULL\n");
6643                 return PMINFO_R_EINVAL;
6644         }
6645         manifest_x *mfx = (manifest_x *)handle;
6646         if (mfx->installlocation == NULL) {
6647                 _LOGE("cant set size without specifying install location\n");
6648                 return PMINFO_R_ERROR;
6649         }
6650         if (strcmp(mfx->installlocation, "prefer-external") == 0) {
6651                 if (mfx->package_size == NULL)
6652                         mfx->package_size = strdup(size);
6653                 else
6654                         mfx->package_size = size;
6655         } else {
6656                 _LOGE("cant set size for internal location\n");
6657                 return PMINFO_R_ERROR;
6658         }
6659         return PMINFO_R_OK;
6660 }
6661 API int pkgmgrinfo_set_label_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *label_txt, const char *locale)
6662 {
6663         if (!handle || !label_txt) {
6664                 _LOGE("Argument supplied is NULL\n");
6665                 return PMINFO_R_EINVAL;
6666         }
6667         int len = strlen(label_txt);
6668         manifest_x *mfx = (manifest_x *)handle;
6669         if (len > PKG_VALUE_STRING_LEN_MAX) {
6670                 _LOGE("label length exceeds the max limit\n");
6671                 return PMINFO_R_EINVAL;
6672         }
6673         label_x *label = calloc(1, sizeof(label_x));
6674         if (label == NULL) {
6675                 _LOGE("Malloc Failed\n");
6676                 return PMINFO_R_ERROR;
6677         }
6678         LISTADD(mfx->label, label);
6679         if (locale)
6680                 mfx->label->lang = strdup(locale);
6681         else
6682                 mfx->label->lang = strdup(DEFAULT_LOCALE);
6683         mfx->label->text = strdup(label_txt);
6684
6685         return PMINFO_R_OK;
6686 }
6687
6688 API int pkgmgrinfo_set_icon_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *icon_txt, const char *locale)
6689 {
6690         if (!handle || !icon_txt) {
6691                 _LOGE("Argument supplied is NULL\n");
6692                 return PMINFO_R_EINVAL;
6693         }
6694         int len = strlen(icon_txt);
6695         manifest_x *mfx = (manifest_x *)handle;
6696         if (len > PKG_VALUE_STRING_LEN_MAX) {
6697                 _LOGE("icon length exceeds the max limit\n");
6698                 return PMINFO_R_EINVAL;
6699         }
6700         icon_x *icon = calloc(1, sizeof(icon_x));
6701         if (icon == NULL) {
6702                 _LOGE("Malloc Failed\n");
6703                 return PMINFO_R_ERROR;
6704         }
6705         LISTADD(mfx->icon, icon);
6706         if (locale)
6707                 mfx->icon->lang = strdup(locale);
6708         else
6709                 mfx->icon->lang = strdup(DEFAULT_LOCALE);
6710         mfx->icon->text = strdup(icon_txt);
6711
6712         return PMINFO_R_OK;
6713 }
6714
6715 API int pkgmgrinfo_set_description_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *desc_txt, const char *locale)
6716 {
6717         if (!handle || !desc_txt) {
6718                 _LOGE("Argument supplied is NULL\n");
6719                 return PMINFO_R_EINVAL;
6720         }
6721         int len = strlen(desc_txt);
6722         manifest_x *mfx = (manifest_x *)handle;
6723         if (len > PKG_VALUE_STRING_LEN_MAX) {
6724                 _LOGE("description length exceeds the max limit\n");
6725                 return PMINFO_R_EINVAL;
6726         }
6727         description_x *description = calloc(1, sizeof(description_x));
6728         if (description == NULL) {
6729                 _LOGE("Malloc Failed\n");
6730                 return PMINFO_R_ERROR;
6731         }
6732         LISTADD(mfx->description, description);
6733         if (locale)
6734                 mfx->description->lang = strdup(locale);
6735         else
6736                 mfx->description->lang = strdup(DEFAULT_LOCALE);
6737         mfx->description->text = strdup(desc_txt);
6738
6739         return PMINFO_R_OK;
6740 }
6741
6742 API int pkgmgrinfo_set_author_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *author_name,
6743                 const char *author_email, const char *author_href, const char *locale)
6744 {
6745         if (!handle) {
6746                 _LOGE("Argument supplied is NULL\n");
6747                 return PMINFO_R_EINVAL;
6748         }
6749         manifest_x *mfx = (manifest_x *)handle;
6750         author_x *author = calloc(1, sizeof(author_x));
6751         if (author == NULL) {
6752                 _LOGE("Malloc Failed\n");
6753                 return PMINFO_R_ERROR;
6754         }
6755         LISTADD(mfx->author, author);
6756         if (author_name)
6757                 mfx->author->text = strdup(author_name);
6758         if (author_email)
6759                 mfx->author->email = strdup(author_email);
6760         if (author_href)
6761                 mfx->author->href = strdup(author_href);
6762         if (locale)
6763                 mfx->author->lang = strdup(locale);
6764         else
6765                 mfx->author->lang = strdup(DEFAULT_LOCALE);
6766         return PMINFO_R_OK;
6767 }
6768
6769 API int pkgmgrinfo_set_removable_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int removable)
6770 {
6771         if (!handle) {
6772                 _LOGE("Argument supplied is NULL\n");
6773                 return PMINFO_R_EINVAL;
6774         }
6775         if (removable < 0 || removable > 1) {
6776                 _LOGE("Argument supplied is invalid\n");
6777                 return PMINFO_R_EINVAL;
6778         }
6779         manifest_x *mfx = (manifest_x *)handle;
6780         if (mfx->removable == NULL) {
6781                 mfx->removable = (char *)calloc(1, strlen("false"));
6782                 if (mfx->removable == NULL) {
6783                         _LOGE("Malloc Failed\n");
6784                         return PMINFO_R_ERROR;
6785                 }
6786         }
6787         if (removable == 0) {
6788                 strcpy(mfx->removable, "false");
6789         } else if (removable == 1) {
6790                 strcpy(mfx->removable, "true");
6791         } else {
6792                 _LOGE("Invalid removable type\n");
6793                 return PMINFO_R_ERROR;
6794         }
6795         return PMINFO_R_OK;
6796 }
6797
6798 API int pkgmgrinfo_set_preload_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int preload)
6799 {
6800         if (!handle) {
6801                 _LOGE("Argument supplied is NULL\n");
6802                 return PMINFO_R_EINVAL;
6803         }
6804         if (preload < 0 || preload > 1) {
6805                 _LOGE("Argument supplied is invalid\n");
6806                 return PMINFO_R_EINVAL;
6807         }
6808         manifest_x *mfx = (manifest_x *)handle;
6809         if (mfx->preload == NULL) {
6810                 mfx->preload = (char *)calloc(1, strlen("false"));
6811                 if (mfx->preload == NULL) {
6812                         _LOGE("Malloc Failed\n");
6813                         return PMINFO_R_ERROR;
6814                 }
6815         }
6816         if (preload == 0) {
6817                 strcpy(mfx->preload, "false");
6818         } else if (preload == 1) {
6819                 strcpy(mfx->preload, "true");
6820         } else {
6821                 _LOGE("Invalid preload type\n");
6822                 return PMINFO_R_ERROR;
6823         }
6824         return PMINFO_R_OK;
6825 }
6826
6827 API int pkgmgrinfo_save_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
6828 {
6829         if (!handle) {
6830                 _LOGE("Argument supplied is NULL\n");
6831                 return PMINFO_R_EINVAL;
6832         }
6833         int ret = 0;
6834         manifest_x *mfx = NULL;
6835         label_x *tmp1 = NULL;
6836         icon_x *tmp2 = NULL;
6837         description_x *tmp3 = NULL;
6838         author_x *tmp4 = NULL;
6839         mfx = (manifest_x *)handle;
6840         /*First move to head of all list pointers*/
6841         if (mfx->label) {
6842                 LISTHEAD(mfx->label, tmp1);
6843                 mfx->label = tmp1;
6844         }
6845         if (mfx->icon) {
6846                 LISTHEAD(mfx->icon, tmp2);
6847                 mfx->icon = tmp2;
6848         }
6849         if (mfx->description) {
6850                 LISTHEAD(mfx->description, tmp3);
6851                 mfx->description= tmp3;
6852         }
6853         if (mfx->author) {
6854                 LISTHEAD(mfx->author, tmp4);
6855                 mfx->author = tmp4;
6856         }
6857         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
6858         if (ret == 0) {
6859                 _LOGE("Successfully stored info in DB\n");
6860                 return PMINFO_R_OK;
6861         } else {
6862                 _LOGE("Failed to store info in DB\n");
6863                 return PMINFO_R_ERROR;
6864         }
6865 }
6866
6867 API int pkgmgrinfo_destroy_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
6868 {
6869         if (!handle) {
6870                 _LOGE("Argument supplied is NULL\n");
6871                 return PMINFO_R_EINVAL;
6872         }
6873         manifest_x *mfx = NULL;
6874         mfx = (manifest_x *)handle;
6875         pkgmgr_parser_free_manifest_xml(mfx);
6876         return PMINFO_R_OK;
6877 }
6878
6879 API int pkgmgrinfo_datacontrol_get_info(const char *providerid, const char * type, char **appid, char **access)
6880 {
6881         if (providerid == NULL) {
6882                 _LOGE("Argument supplied is NULL\n");
6883                 return PMINFO_R_EINVAL;
6884         }
6885         if (type == NULL) {
6886                 _LOGE("Argument supplied is NULL\n");
6887                 return PMINFO_R_EINVAL;
6888         }
6889         if (appid == NULL) {
6890                 _LOGE("Argument supplied to hold return value is NULL\n");
6891                 return PMINFO_R_EINVAL;
6892         }
6893         if (access == NULL) {
6894                 _LOGE("Argument supplied to hold return value is NULL\n");
6895                 return PMINFO_R_EINVAL;
6896         }
6897
6898         int ret = PMINFO_R_OK;
6899         char query[MAX_QUERY_LEN] = {'\0'};
6900         char *error_message = NULL;
6901         pkgmgr_datacontrol_x *data = NULL;
6902
6903         ret = __open_datacontrol_db();
6904         if (ret == -1) {
6905                 _LOGE("Fail to open datacontrol DB\n");
6906                 return PMINFO_R_ERROR;
6907         }
6908
6909         data = (pkgmgr_datacontrol_x *)calloc(1, sizeof(pkgmgr_datacontrol_x));
6910         if (data == NULL) {
6911                 _LOGE("Failed to allocate memory for pkgmgr_datacontrol_x\n");
6912                 sqlite3_close(datacontrol_db);
6913                 return PMINFO_R_ERROR;
6914         }
6915
6916         snprintf(query, MAX_QUERY_LEN, 
6917                 "select appinfo.package_name, datacontrol.access from appinfo, datacontrol where datacontrol.id=appinfo.unique_id and datacontrol.provider_id = '%s' and datacontrol.type='%s' COLLATE NOCASE",
6918                 providerid, type);
6919
6920         if (SQLITE_OK !=
6921                 sqlite3_exec(datacontrol_db, query, __datacontrol_cb, (void *)data, &error_message)) {
6922                 _LOGE("Don't execute query = %s error message = %s\n", query,
6923                            error_message);
6924                 sqlite3_free(error_message);
6925                 sqlite3_close(datacontrol_db);
6926                 return PMINFO_R_ERROR;
6927         }
6928
6929         *appid = (char *)data->appid;
6930         *access = (char *)data->access;
6931         free(data);
6932         sqlite3_close(datacontrol_db);
6933
6934         return PMINFO_R_OK;
6935 }