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