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