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