remove appsvc db during uninstall
[platform/core/appfw/pkgmgr-info.git] / parser / pkgmgr_parser_db.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 <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <db-util.h>
30 #include <glib.h>
31 #include "pkgmgr-info.h"
32 #include "pkgmgr_parser_internal.h"
33 #include "pkgmgr_parser_db.h"
34
35 #define PKGMGR_PARSER_DB_FILE "/opt/dbspace/.pkgmgr_parser.db"
36 #define PKGMGR_CERT_DB_FILE "/opt/dbspace/.pkgmgr_cert.db"
37 #define MAX_QUERY_LEN           4096
38 sqlite3 *pkgmgr_parser_db;
39 sqlite3 *pkgmgr_cert_db;
40 GList *pkglocale = NULL;
41 GList *applocale = NULL;
42 GList *appicon = NULL;
43 GList *appimage = NULL;
44 char *prev = NULL;
45
46 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
47                                                 "(package text primary key not null, " \
48                                                 "package_type text DEFAULT 'rpm', " \
49                                                 "package_version text, " \
50                                                 "install_location text, " \
51                                                 "package_size text, " \
52                                                 "package_removable text DEFAULT 'true', " \
53                                                 "package_preload text DEFAULT 'false', " \
54                                                 "package_readonly text DEFAULT 'false', " \
55                                                 "package_update text DEFAULT 'false', " \
56                                                 "package_appsetting text DEFAULT 'false', " \
57                                                 "author_name text, " \
58                                                 "author_email text, " \
59                                                 "author_href text," \
60                                                 "installed_time text," \
61                                                 "installed_storage text," \
62                                                 "storeclient_id text," \
63                                                 "mainapp_id text," \
64                                                 "package_url text," \
65                                                 "root_path text)"
66
67 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
68                                                 "(package text not null, " \
69                                                 "package_locale text DEFAULT 'No Locale', " \
70                                                 "package_label text, " \
71                                                 "package_icon text, " \
72                                                 "package_description text, " \
73                                                 "package_license text, " \
74                                                 "package_author, " \
75                                                 "PRIMARY KEY(package, package_locale), " \
76                                                 "FOREIGN KEY(package) " \
77                                                 "REFERENCES package_info(package) " \
78                                                 "ON DELETE CASCADE)"
79
80 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
81                                                 "(package text not null, " \
82                                                 "privilege text not null, " \
83                                                 "PRIMARY KEY(package, privilege) " \
84                                                 "FOREIGN KEY(package) " \
85                                                 "REFERENCES package_info(package) " \
86                                                 "ON DELETE CASCADE)"
87
88 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
89                                                 "(app_id text primary key not null, " \
90                                                 "app_component text, " \
91                                                 "app_exec text, " \
92                                                 "app_nodisplay text DEFAULT 'false', " \
93                                                 "app_type text, " \
94                                                 "app_onboot text DEFAULT 'false', " \
95                                                 "app_multiple text DEFAULT 'false', " \
96                                                 "app_autorestart text DEFAULT 'false', " \
97                                                 "app_taskmanage text DEFAULT 'false', " \
98                                                 "app_enabled text DEFAULT 'true', " \
99                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
100                                                 "app_mainapp text, " \
101                                                 "app_recentimage text, " \
102                                                 "app_indicatordisplay text DEFAULT 'true', " \
103                                                 "app_portraitimg text, " \
104                                                 "app_landscapeimg text, " \
105                                                 "app_guestmodevisibility text DEFAULT 'true', " \
106                                                 "package text not null, " \
107                                                 "FOREIGN KEY(package) " \
108                                                 "REFERENCES package_info(package) " \
109                                                 "ON DELETE CASCADE)"
110
111 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
112                                                 "(app_id text not null, " \
113                                                 "app_locale text DEFAULT 'No Locale', " \
114                                                 "app_label text, " \
115                                                 "app_icon text, " \
116                                                 "PRIMARY KEY(app_id,app_locale) " \
117                                                 "FOREIGN KEY(app_id) " \
118                                                 "REFERENCES package_app_info(app_id) " \
119                                                 "ON DELETE CASCADE)"
120
121 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
122                                                 "(app_id text not null, " \
123                                                 "app_icon text, " \
124                                                 "app_icon_section text, " \
125                                                 "app_icon_resolution text, " \
126                                                 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
127                                                 "FOREIGN KEY(app_id) " \
128                                                 "REFERENCES package_app_info(app_id) " \
129                                                 "ON DELETE CASCADE)"
130
131 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
132                                                 "(app_id text not null, " \
133                                                 "app_locale text DEFAULT 'No Locale', " \
134                                                 "app_image_section text, " \
135                                                 "app_image text, " \
136                                                 "PRIMARY KEY(app_id,app_image_section) " \
137                                                 "FOREIGN KEY(app_id) " \
138                                                 "REFERENCES package_app_info(app_id) " \
139                                                 "ON DELETE CASCADE)"
140
141 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
142                                                 "(app_id text not null, " \
143                                                 "operation text not null, " \
144                                                 "uri_scheme text, " \
145                                                 "mime_type text, " \
146                                                 "subapp_name text, " \
147                                                 "PRIMARY KEY(app_id,operation,uri_scheme,mime_type,subapp_name) " \
148                                                 "FOREIGN KEY(app_id) " \
149                                                 "REFERENCES package_app_info(app_id) " \
150                                                 "ON DELETE CASCADE)"
151
152 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC "create table if not exists package_app_app_svc " \
153                                                 "(app_id text not null, " \
154                                                 "operation text not null, " \
155                                                 "uri_scheme text, " \
156                                                 "mime_type text, " \
157                                                 "subapp_name text, " \
158                                                 "PRIMARY KEY(app_id,operation,uri_scheme,mime_type,subapp_name) " \
159                                                 "FOREIGN KEY(app_id) " \
160                                                 "REFERENCES package_app_info(app_id) " \
161                                                 "ON DELETE CASCADE)"
162
163 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "create table if not exists package_app_app_category " \
164                                                 "(app_id text not null, " \
165                                                 "category text not null, " \
166                                                 "PRIMARY KEY(app_id,category) " \
167                                                 "FOREIGN KEY(app_id) " \
168                                                 "REFERENCES package_app_info(app_id) " \
169                                                 "ON DELETE CASCADE)"
170
171 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "create table if not exists package_app_app_metadata " \
172                                                 "(app_id text not null, " \
173                                                 "md_key text not null, " \
174                                                 "md_value text not null, " \
175                                                 "PRIMARY KEY(app_id, md_key, md_value) " \
176                                                 "FOREIGN KEY(app_id) " \
177                                                 "REFERENCES package_app_info(app_id) " \
178                                                 "ON DELETE CASCADE)"
179
180 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "create table if not exists package_app_app_permission " \
181                                                 "(app_id text not null, " \
182                                                 "pm_type text not null, " \
183                                                 "pm_value text not null, " \
184                                                 "PRIMARY KEY(app_id) " \
185                                                 "FOREIGN KEY(app_id) " \
186                                                 "REFERENCES package_app_info(app_id) " \
187                                                 "ON DELETE CASCADE)"
188
189 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "create table if not exists package_app_share_allowed " \
190                                                 "(app_id text not null, " \
191                                                 "data_share_path text not null, " \
192                                                 "data_share_allowed text not null, " \
193                                                 "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \
194                                                 "FOREIGN KEY(app_id) " \
195                                                 "REFERENCES package_app_info(app_id) " \
196                                                 "ON DELETE CASCADE)"
197
198 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "create table if not exists package_app_share_request " \
199                                                 "(app_id text not null, " \
200                                                 "data_share_request text not null, " \
201                                                 "PRIMARY KEY(app_id,data_share_request) " \
202                                                 "FOREIGN KEY(app_id) " \
203                                                 "REFERENCES package_app_info(app_id) " \
204                                                 "ON DELETE CASCADE)"
205
206 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO "create table if not exists package_cert_index_info " \
207                                                 "(cert_info text not null, " \
208                                                 "cert_id integer, " \
209                                                 "cert_ref_count integer, " \
210                                                 "PRIMARY KEY(cert_id)) "
211
212 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO "create table if not exists package_cert_info " \
213                                                 "(package text not null, " \
214                                                 "author_root_cert integer, " \
215                                                 "author_im_cert integer, " \
216                                                 "author_signer_cert integer, " \
217                                                 "dist_root_cert integer, " \
218                                                 "dist_im_cert integer, " \
219                                                 "dist_signer_cert integer, " \
220                                                 "dist2_root_cert integer, " \
221                                                 "dist2_im_cert integer, " \
222                                                 "dist2_signer_cert integer, " \
223                                                 "PRIMARY KEY(package)) "
224
225 static int __insert_uiapplication_info(manifest_x *mfx);
226 static int __insert_serviceapplication_info(manifest_x *mfx);
227 static int __insert_uiapplication_appsvc_info(manifest_x *mfx);
228 static int __insert_serviceapplication_appsvc_info(manifest_x *mfx);
229 static int __insert_uiapplication_appcategory_info(manifest_x *mfx);
230 static int __insert_serviceapplication_appcategory_info(manifest_x *mfx);
231 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx);
232 static int __insert_serviceapplication_appmetadata_info(manifest_x *mfx);
233 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx);
234 static int __insert_serviceapplication_appcontrol_info(manifest_x *mfx);
235 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx);
236 static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx);
237 static int __insert_uiapplication_share_request_info(manifest_x *mfx);
238 static int __insert_serviceapplication_share_request_info(manifest_x *mfx);
239 static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata);
240 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata);
241 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
242 static int __insert_manifest_info_in_db(manifest_x *mfx);
243 static int __delete_manifest_info_from_db(manifest_x *mfx);
244 static int __delete_appinfo_from_db(char *db_table, const char *appid);
245 static int __initialize_db(sqlite3 *db_handle, const char *db_query);
246 static int __exec_query(char *query);
247 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
248                 char **label, char **license, char **icon, char **description, char **author);
249 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
250 static void __trimfunc1(gpointer data, gpointer userdata);
251 static void __trimfunc2(gpointer data, gpointer userdata);
252 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath);
253 static void __preserve_guestmode_visibility_value(manifest_x *mfx);
254 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname);
255 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
256
257 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
258 {
259         int ret = -1;
260         sqlite3 *handle;
261         if (access(db_path, F_OK) == 0) {
262                 ret =
263                     db_util_open(db_path, &handle,
264                                  DB_UTIL_REGISTER_HOOK_METHOD);
265                 if (ret != SQLITE_OK) {
266                         DBG("connect db [%s] failed!\n",
267                                db_path);
268                         return -1;
269                 }
270                 *db_handle = handle;
271                 return 0;
272         }
273         DBG("%s DB does not exists. Create one!!\n", db_path);
274
275         ret =
276             db_util_open(db_path, &handle,
277                          DB_UTIL_REGISTER_HOOK_METHOD);
278
279         if (ret != SQLITE_OK) {
280                 DBG("connect db [%s] failed!\n", db_path);
281                 return -1;
282         }
283         *db_handle = handle;
284         return 0;
285 }
286
287 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
288 {
289         manifest_x *mfx = (manifest_x *)data;
290         int i = 0;
291         char *appid = NULL;
292         char *status = NULL;
293         uiapplication_x *uiapp = NULL;
294         for(i = 0; i < ncols; i++)
295         {
296                 uiapp = mfx->uiapplication;
297                 if (strcmp(colname[i], "app_id") == 0) {
298                         if (coltxt[i])
299                                 appid = strdup(coltxt[i]);
300                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) {
301                         if (coltxt[i])
302                                 status = strdup(coltxt[i]);
303                 }
304         }
305         if (appid == NULL) {
306                 DBG("app id is NULL\n");
307                 return -1;
308         }
309         /*update guest mode visibility*/
310         for (; uiapp != NULL; uiapp = uiapp->next) {
311                 if (strcmp(uiapp->appid, appid) == 0) {
312                         free((void *)uiapp->guestmode_visibility);
313                         uiapp->guestmode_visibility = strdup(status);
314                         break;
315                 }
316         }
317         if (appid) {
318                 free(appid);
319                 appid = NULL;
320         }
321         if (status) {
322                 free(status);
323                 status = NULL;
324         }
325
326         return 0;
327 }
328
329 static void __preserve_guestmode_visibility_value(manifest_x *mfx)
330 {
331         char *error_message = NULL;
332         char query[MAX_QUERY_LEN] = {'\0'};
333         snprintf(query, MAX_QUERY_LEN - 1, "select app_id, app_guestmodevisibility from package_app_info where package='%s'", mfx->package);
334         if (SQLITE_OK !=
335             sqlite3_exec(pkgmgr_parser_db, query,
336                          __guestmode_visibility_cb, (void *)mfx, &error_message)) {
337                 DBG("Don't execute query = %s error message = %s\n",
338                        query, error_message);
339                 sqlite3_free(error_message);
340         }
341         return;
342 }
343
344 static int __initialize_db(sqlite3 *db_handle, const char *db_query)
345 {
346         char *error_message = NULL;
347         if (SQLITE_OK !=
348             sqlite3_exec(db_handle, db_query,
349                          NULL, NULL, &error_message)) {
350                 DBG("Don't execute query = %s error message = %s\n",
351                        db_query, error_message);
352                 sqlite3_free(error_message);
353                 return -1;
354         }
355         sqlite3_free(error_message);
356         return 0;
357 }
358
359 static int __exec_query(char *query)
360 {
361         char *error_message = NULL;
362         if (SQLITE_OK !=
363             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
364                 DBG("Don't execute query = %s error message = %s\n", query,
365                        error_message);
366                 sqlite3_free(error_message);
367                 return -1;
368         }
369         sqlite3_free(error_message);
370         return 0;
371 }
372 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath)
373 {
374
375         while(lbl != NULL)
376         {
377                 if (lbl->lang)
378                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
379                 lbl = lbl->next;
380         }
381         while(lcn != NULL)
382         {
383                 if (lcn->lang)
384                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
385                 lcn = lcn->next;
386         }
387         while(icn != NULL)
388         {
389                 if (icn->lang)
390                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
391                 icn = icn->next;
392         }
393         while(dcn != NULL)
394         {
395                 if (dcn->lang)
396                         locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
397                 dcn = dcn->next;
398         }
399         while(ath != NULL)
400         {
401                 if (ath->lang)
402                         locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
403                 ath = ath->next;
404         }
405         return locale;
406
407 }
408
409 static GList *__create_icon_list(GList *locale, icon_x *icn)
410 {
411         while(icn != NULL)
412         {
413                 if (icn->section)
414                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->section, __comparefunc, NULL);
415                 icn = icn->next;
416         }
417         return locale;
418 }
419
420 static GList *__create_image_list(GList *locale, image_x *image)
421 {
422         while(image != NULL)
423         {
424                 if (image->section)
425                         locale = g_list_insert_sorted_with_data(locale, (gpointer)image->section, __comparefunc, NULL);
426                 image = image->next;
427         }
428         return locale;
429 }
430
431 static void __printfunc(gpointer data, gpointer userdata)
432 {
433         DBG("%s  ", (char*)data);
434 }
435
436 static void __trimfunc1(gpointer data, gpointer userdata)
437 {
438         if (prev) {
439                 if (strcmp((char *)data, prev) == 0) {
440                         pkglocale = g_list_remove(pkglocale, data);
441                 } else
442                         prev = (char *)data;
443         }
444         else
445                 prev = (char *)data;
446 }
447
448 static void __trimfunc2(gpointer data, gpointer userdata)
449 {
450         if (prev) {
451                 if (strcmp((char *)data, prev) == 0) {
452                         applocale = g_list_remove(applocale, data);
453                 } else
454                         prev = (char *)data;
455         }
456         else
457                 prev = (char *)data;
458 }
459
460 static void __trimfunc3(gpointer data, gpointer userdata)
461 {
462         if (prev) {
463                 if (strcmp((char *)data, prev) == 0) {
464                         appicon = g_list_remove(appicon, data);
465                 } else
466                         prev = (char *)data;
467         }
468         else
469                 prev = (char *)data;
470 }
471
472 static void __trimfunc4(gpointer data, gpointer userdata)
473 {
474         if (prev) {
475                 if (strcmp((char *)data, prev) == 0) {
476                         appimage = g_list_remove(appimage, data);
477                 } else
478                         prev = (char *)data;
479         }
480         else
481                 prev = (char *)data;
482 }
483
484 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
485 {
486         if (a == NULL || b == NULL)
487                 return 0;
488         if (strcmp((char*)a, (char*)b) == 0)
489                 return 0;
490         if (strcmp((char*)a, (char*)b) < 0)
491                 return -1;
492         if (strcmp((char*)a, (char*)b) > 0)
493                 return 1;
494         return 0;
495 }
496
497 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
498                 char **label, char **license, char **icon, char **description, char **author)
499 {
500         while(lbl != NULL)
501         {
502                 if (lbl->lang) {
503                         if (strcmp(lbl->lang, (char *)data) == 0) {
504                                 *label = (char*)lbl->text;
505                                 break;
506                         }
507                 }
508                 lbl = lbl->next;
509         }
510         while(lcn != NULL)
511         {
512                 if (lcn->lang) {
513                         if (strcmp(lcn->lang, (char *)data) == 0) {
514                                 *license = (char*)lcn->text;
515                                 break;
516                         }
517                 }
518                 lcn = lcn->next;
519         }
520         while(icn != NULL)
521         {
522                 if (icn->lang) {
523                         if (strcmp(icn->lang, (char *)data) == 0) {
524                                 *icon = (char*)icn->text;
525                                 break;
526                         }
527                 }
528                 icn = icn->next;
529         }
530         while(dcn != NULL)
531         {
532                 if (dcn->lang) {
533                         if (strcmp(dcn->lang, (char *)data) == 0) {
534                                 *description = (char*)dcn->text;
535                                 break;
536                         }
537                 }
538                 dcn = dcn->next;
539         }
540         while(ath != NULL)
541         {
542                 if (ath->lang) {
543                         if (strcmp(ath->lang, (char *)data) == 0) {
544                                 *author = (char*)ath->text;
545                                 break;
546                         }
547                 }
548                 ath = ath->next;
549         }
550
551 }
552
553 static void __extract_icon_data(gpointer data, icon_x *icn, char **icon, char **resolution)
554 {
555         while(icn != NULL)
556         {
557                 if (icn->section) {
558                         if (strcmp(icn->section, (char *)data) == 0) {
559                                 *icon = (char*)icn->text;
560                                 *resolution = (char*)icn->resolution;
561                                 break;
562                         }
563                 }
564                 icn = icn->next;
565         }
566 }
567
568 static void __extract_image_data(gpointer data, image_x*image, char **lang, char **img)
569 {
570         while(image != NULL)
571         {
572                 if (image->section) {
573                         if (strcmp(image->section, (char *)data) == 0) {
574                                 *lang = (char*)image->lang;
575                                 *img = (char*)image->text;
576                                 break;
577                         }
578                 }
579                 image = image->next;
580         }
581 }
582
583 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
584 {
585         int ret = -1;
586         char *label = NULL;
587         char *icon = NULL;
588         char *description = NULL;
589         char *license = NULL;
590         char *author = NULL;
591         char query[MAX_QUERY_LEN] = {'\0'};
592
593         manifest_x *mfx = (manifest_x *)userdata;
594         label_x *lbl = mfx->label;
595         license_x *lcn = mfx->license;
596         icon_x *icn = mfx->icon;
597         description_x *dcn = mfx->description;
598         author_x *ath = mfx->author;
599
600         __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
601         if (!label && !description && !icon && !license && !author)
602                 return;
603         snprintf(query, MAX_QUERY_LEN, "insert into package_localized_info(package, package_locale, " \
604                 "package_label, package_icon, package_description, package_license, package_author) values " \
605                 "('%s', '%s', '%s', '%s', '%s', '%s', '%s')", mfx->package, (char*)data,
606                 label, icon, description, license, author);
607         ret = __exec_query(query);
608         if (ret == -1)
609                 DBG("Package Localized Info DB Insert failed\n");
610 }
611
612 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata)
613 {
614         int ret = -1;
615         char *label = NULL;
616         char *icon = NULL;
617         char query[MAX_QUERY_LEN] = {'\0'};
618
619         uiapplication_x *up = (uiapplication_x*)userdata;
620         label_x *lbl = up->label;
621         icon_x *icn = up->icon;
622
623         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
624         if (!label && !icon)
625                 return;
626         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
627                 "app_label, app_icon) values " \
628                 "('%q', '%q', '%q', '%q')", up->appid, (char*)data,
629                 label, icon);
630         ret = __exec_query(query);
631         if (ret == -1)
632                 DBG("Package UiApp Localized Info DB Insert failed\n");
633
634 }
635
636 static void __insert_uiapplication_icon_section_info(gpointer data, gpointer userdata)
637 {
638         int ret = -1;
639         char *icon = NULL;
640         char *resolution = NULL;
641         char query[MAX_QUERY_LEN] = {'\0'};
642
643         uiapplication_x *up = (uiapplication_x*)userdata;
644         icon_x *icn = up->icon;
645
646         __extract_icon_data(data, icn, &icon, &resolution);
647         if (!icon && !resolution)
648                 return;
649         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
650                 "app_icon, app_icon_section, app_icon_resolution) values " \
651                 "('%q', '%q', '%q', '%q')", up->appid,
652                 icon, (char*)data, resolution);
653
654         ret = __exec_query(query);
655         if (ret == -1)
656                 DBG("Package UiApp Localized Info DB Insert failed\n");
657
658 }
659
660 static void __insert_uiapplication_image_info(gpointer data, gpointer userdata)
661 {
662         int ret = -1;
663         char *lang = NULL;
664         char *img = NULL;
665         char query[MAX_QUERY_LEN] = {'\0'};
666
667         uiapplication_x *up = (uiapplication_x*)userdata;
668         image_x *image = up->image;
669
670         __extract_image_data(data, image, &lang, &img);
671         if (!lang && !img)
672                 return;
673         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
674                 "app_image_section, app_image) values " \
675                 "('%q', '%q', '%q', '%q')", up->appid, lang, (char*)data, img);
676
677         ret = __exec_query(query);
678         if (ret == -1)
679                 DBG("Package UiApp image Info DB Insert failed\n");
680
681 }
682
683
684 static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata)
685 {
686         int ret = -1;
687         char *icon = NULL;
688         char *label = NULL;
689         char query[MAX_QUERY_LEN] = {'\0'};
690
691         serviceapplication_x *sp = (serviceapplication_x*)userdata;
692         label_x *lbl = sp->label;
693         icon_x *icn = sp->icon;
694
695         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
696         if (!icon && !label)
697                 return;
698         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
699                 "app_label, app_icon) values " \
700                 "('%q', '%q', '%q', '%q')", sp->appid, (char*)data,
701                 label, icon);
702         ret = __exec_query(query);
703         if (ret == -1)
704                 DBG("Package ServiceApp Localized Info DB Insert failed\n");
705 }
706
707 static int __insert_ui_mainapp_info(manifest_x *mfx)
708 {
709         uiapplication_x *up = mfx->uiapplication;
710         int ret = -1;
711         char query[MAX_QUERY_LEN] = {'\0'};
712         while(up != NULL)
713         {
714                 snprintf(query, MAX_QUERY_LEN,
715                         "update package_app_info set app_mainapp='%s' where app_id='%s'", up->mainapp, up->appid);
716
717                 ret = __exec_query(query);
718                 if (ret == -1) {
719                         DBG("Package UiApp Info DB Insert Failed\n");
720                         return -1;
721                 }
722                 if (strcasecmp(up->mainapp, "True")==0)
723                         mfx->mainapp_id = strdup(up->appid);
724
725                 up = up->next;
726                 memset(query, '\0', MAX_QUERY_LEN);
727         }
728
729         if (mfx->mainapp_id == NULL){
730                 if (mfx->uiapplication
731                 && mfx->uiapplication->appid) {
732                         snprintf(query, MAX_QUERY_LEN,
733                         "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->uiapplication->appid);
734                 } else if (mfx->serviceapplication
735                         && mfx->serviceapplication->appid) {
736                         snprintf(query, MAX_QUERY_LEN,
737                         "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->serviceapplication->appid);
738                 } else {
739                         DBG("Not valid appid\n");
740                         return -1;
741                 }
742
743                 ret = __exec_query(query);
744                 if (ret == -1) {
745                         DBG("Package UiApp Info DB Insert Failed\n");
746                         return -1;
747                 }
748                 if (mfx->uiapplication && mfx->uiapplication->appid)
749                         mfx->mainapp_id = strdup(mfx->uiapplication->appid);
750                 else if (mfx->serviceapplication && mfx->serviceapplication->appid)
751                         mfx->mainapp_id = strdup(mfx->serviceapplication->appid);
752
753 }
754
755         memset(query, '\0', MAX_QUERY_LEN);
756         snprintf(query, MAX_QUERY_LEN,
757                 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
758         ret = __exec_query(query);
759         if (ret == -1) {
760                 DBG("Package Info DB update Failed\n");
761                 return -1;
762         }
763
764         return 0;
765 }
766 /* _PRODUCT_LAUNCHING_ENHANCED_
767 *  up->indicatordisplay, up->portraitimg, up->landscapeimg, up->guestmode_appstatus
768 */
769 static int __insert_uiapplication_info(manifest_x *mfx)
770 {
771         uiapplication_x *up = mfx->uiapplication;
772         int ret = -1;
773         char query[MAX_QUERY_LEN] = {'\0'};
774         while(up != NULL)
775         {
776                 snprintf(query, MAX_QUERY_LEN,
777                          "insert into package_app_info(app_id, app_component, app_exec, app_nodisplay, app_type, app_onboot, " \
778                         "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_mainapp , app_recentimage, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, package) " \
779                         "values('%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
780                          up->appid, "uiapp", up->exec, up->nodisplay, up->type, "\0", up->multiple,
781                          "\0", up->taskmanage, up->enabled, up->hwacceleration,up->mainapp, up->recentimage, up->indicatordisplay, up->portraitimg, up->landscapeimg, up->guestmode_visibility, mfx->package);
782                 ret = __exec_query(query);
783                 if (ret == -1) {
784                         DBG("Package UiApp Info DB Insert Failed\n");
785                         return -1;
786                 }
787                 up = up->next;
788                 memset(query, '\0', MAX_QUERY_LEN);
789         }
790         return 0;
791 }
792
793 static int __insert_uiapplication_appcategory_info(manifest_x *mfx)
794 {
795         uiapplication_x *up = mfx->uiapplication;
796         category_x *ct = NULL;
797         int ret = -1;
798         char query[MAX_QUERY_LEN] = {'\0'};
799         while(up != NULL)
800         {
801                 ct = up->category;
802                 while (ct != NULL)
803                 {
804                         snprintf(query, MAX_QUERY_LEN,
805                                 "insert into package_app_app_category(app_id, category) " \
806                                 "values('%s','%s')",\
807                                  up->appid, ct->name);
808                         ret = __exec_query(query);
809                         if (ret == -1) {
810                                 DBG("Package UiApp Category Info DB Insert Failed\n");
811                                 return -1;
812                         }
813                         ct = ct->next;
814                         memset(query, '\0', MAX_QUERY_LEN);
815                 }
816                 up = up->next;
817         }
818         return 0;
819 }
820
821 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx)
822 {
823         uiapplication_x *up = mfx->uiapplication;
824         metadata_x *md = NULL;
825         int ret = -1;
826         char query[MAX_QUERY_LEN] = {'\0'};
827         while(up != NULL)
828         {
829                 md = up->metadata;
830                 while (md != NULL)
831                 {
832                         if (md->key) {
833                                 snprintf(query, MAX_QUERY_LEN,
834                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
835                                         "values('%s','%s', '%s')",\
836                                          up->appid, md->key, md->value);
837                                 ret = __exec_query(query);
838                                 if (ret == -1) {
839                                         DBG("Package UiApp Metadata Info DB Insert Failed\n");
840                                         return -1;
841                                 }
842                         }
843                         md = md->next;
844                         memset(query, '\0', MAX_QUERY_LEN);
845                 }
846                 up = up->next;
847         }
848         return 0;
849 }
850
851 static int __insert_uiapplication_apppermission_info(manifest_x *mfx)
852 {
853         uiapplication_x *up = mfx->uiapplication;
854         permission_x *pm = NULL;
855         int ret = -1;
856         char query[MAX_QUERY_LEN] = {'\0'};
857         while(up != NULL)
858         {
859                 pm = up->permission;
860                 while (pm != NULL)
861                 {
862                         snprintf(query, MAX_QUERY_LEN,
863                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
864                                 "values('%s','%s', '%s')",\
865                                  up->appid, pm->type, pm->value);
866                         ret = __exec_query(query);
867                         if (ret == -1) {
868                                 DBG("Package UiApp permission Info DB Insert Failed\n");
869                                 return -1;
870                         }
871                         pm = pm->next;
872                         memset(query, '\0', MAX_QUERY_LEN);
873                 }
874                 up = up->next;
875         }
876         return 0;
877 }
878
879 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx)
880 {
881         uiapplication_x *up = mfx->uiapplication;
882         appcontrol_x *acontrol = NULL;
883         operation_x *op = NULL;
884         mime_x *mi = NULL;
885         uri_x *ui = NULL;
886         subapp_x *sub = NULL;
887         int ret = -1;
888         char query[MAX_QUERY_LEN] = {'\0'};
889         const char *operation = NULL;
890         const char *mime = NULL;
891         const char *uri = NULL;
892         const char *subapp = NULL;
893         while(up != NULL)
894         {
895                 acontrol = up->appcontrol;
896                 while(acontrol != NULL)
897                 {
898                         op = acontrol->operation;
899                         while(op != NULL)
900                         {
901                                 if (op)
902                                         operation = op->name;
903                                 mi = acontrol->mime;
904
905                                 do
906                                 {
907                                         if (mi)
908                                                 mime = mi->name;
909                                         sub = acontrol->subapp;
910                                         do
911                                         {
912                                                 if (sub)
913                                                         subapp = sub->name;
914                                                 ui = acontrol->uri;
915                                                 do
916                                                 {
917                                                         if (ui)
918                                                                 uri = ui->name;
919                                                         snprintf(query, MAX_QUERY_LEN,
920                                                                  "insert into package_app_app_control(app_id, operation, uri_scheme, mime_type, subapp_name) " \
921                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
922                                                                  up->appid, operation, uri, mime, subapp);
923
924                                                         ret = __exec_query(query);
925                                                         if (ret == -1) {
926                                                                 DBG("Package UiApp AppSvc DB Insert Failed\n");
927                                                                 return -1;
928                                                         }
929                                                         memset(query, '\0', MAX_QUERY_LEN);
930                                                         if (ui)
931                                                                 ui = ui->next;
932                                                         uri = NULL;
933                                                 } while(ui != NULL);
934                                                 if (sub)
935                                                         sub = sub->next;
936                                                 subapp = NULL;
937                                         }while(sub != NULL);
938                                         if (mi)
939                                                 mi = mi->next;
940                                         mime = NULL;
941                                 }while(mi != NULL);
942                                 if (op)
943                                         op = op->next;
944                                 operation = NULL;
945                         }
946                         acontrol = acontrol->next;
947                 }
948                 up = up->next;
949         }
950         return 0;
951 }
952
953 static int __insert_uiapplication_appsvc_info(manifest_x *mfx)
954 {
955         uiapplication_x *up = mfx->uiapplication;
956         appsvc_x *asvc = NULL;
957         operation_x *op = NULL;
958         mime_x *mi = NULL;
959         uri_x *ui = NULL;
960         subapp_x *sub = NULL;
961         int ret = -1;
962         char query[MAX_QUERY_LEN] = {'\0'};
963         const char *operation = NULL;
964         const char *mime = NULL;
965         const char *uri = NULL;
966         const char *subapp = NULL;
967         while(up != NULL)
968         {
969                 asvc = up->appsvc;
970                 while(asvc != NULL)
971                 {
972                         op = asvc->operation;
973                         while(op != NULL)
974                         {
975                                 if (op)
976                                         operation = op->name;
977                                 mi = asvc->mime;
978
979                                 do
980                                 {
981                                         if (mi)
982                                                 mime = mi->name;
983                                         sub = asvc->subapp;
984                                         do
985                                         {
986                                                 if (sub)
987                                                         subapp = sub->name;
988                                                 ui = asvc->uri;
989                                                 do
990                                                 {
991                                                         if (ui)
992                                                                 uri = ui->name;
993                                                         snprintf(query, MAX_QUERY_LEN,
994                                                                  "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
995                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
996                                                                  up->appid, operation, uri, mime, subapp);
997
998                                                         ret = __exec_query(query);
999                                                         if (ret == -1) {
1000                                                                 DBG("Package UiApp AppSvc DB Insert Failed\n");
1001                                                                 return -1;
1002                                                         }
1003                                                         memset(query, '\0', MAX_QUERY_LEN);
1004                                                         if (ui)
1005                                                                 ui = ui->next;
1006                                                         uri = NULL;
1007                                                 } while(ui != NULL);
1008                                                 if (sub)
1009                                                         sub = sub->next;
1010                                                 subapp = NULL;
1011                                         }while(sub != NULL);
1012                                         if (mi)
1013                                                 mi = mi->next;
1014                                         mime = NULL;
1015                                 }while(mi != NULL);
1016                                 if (op)
1017                                         op = op->next;
1018                                 operation = NULL;
1019                         }
1020                         asvc = asvc->next;
1021                 }
1022                 up = up->next;
1023         }
1024         return 0;
1025 }
1026
1027 static int __insert_uiapplication_share_request_info(manifest_x *mfx)
1028 {
1029         uiapplication_x *up = mfx->uiapplication;
1030         datashare_x *ds = NULL;
1031         request_x *rq = NULL;
1032         int ret = -1;
1033         char query[MAX_QUERY_LEN] = {'\0'};
1034         while(up != NULL)
1035         {
1036                 ds = up->datashare;
1037                 while(ds != NULL)
1038                 {
1039                         rq = ds->request;
1040                         while(rq != NULL)
1041                         {
1042                                 snprintf(query, MAX_QUERY_LEN,
1043                                          "insert into package_app_share_request(app_id, data_share_request) " \
1044                                         "values('%s', '%s')",\
1045                                          up->appid, rq->text);
1046                                 ret = __exec_query(query);
1047                                 if (ret == -1) {
1048                                         DBG("Package UiApp Share Request DB Insert Failed\n");
1049                                         return -1;
1050                                 }
1051                                 memset(query, '\0', MAX_QUERY_LEN);
1052                                 rq = rq->next;
1053                         }
1054                         ds = ds->next;
1055                 }
1056                 up = up->next;
1057         }
1058         return 0;
1059 }
1060
1061 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx)
1062 {
1063         uiapplication_x *up = mfx->uiapplication;
1064         datashare_x *ds = NULL;
1065         define_x *df = NULL;
1066         allowed_x *al = NULL;
1067         int ret = -1;
1068         char query[MAX_QUERY_LEN] = {'\0'};
1069         while(up != NULL)
1070         {
1071                 ds = up->datashare;
1072                 while(ds != NULL)
1073                 {
1074                         df = ds->define;
1075                         while(df != NULL)
1076                         {
1077                                 al = df->allowed;
1078                                 while(al != NULL)
1079                                 {
1080                                         snprintf(query, MAX_QUERY_LEN,
1081                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1082                                                 "values('%s', '%s', '%s')",\
1083                                                  up->appid, df->path, al->text);
1084                                         ret = __exec_query(query);
1085                                         if (ret == -1) {
1086                                                 DBG("Package UiApp Share Allowed DB Insert Failed\n");
1087                                                 return -1;
1088                                         }
1089                                         memset(query, '\0', MAX_QUERY_LEN);
1090                                         al = al->next;
1091                                 }
1092                                 df = df->next;
1093                         }
1094                         ds = ds->next;
1095                 }
1096                 up = up->next;
1097         }
1098         return 0;
1099 }
1100
1101 static int __insert_serviceapplication_info(manifest_x *mfx)
1102 {
1103         serviceapplication_x *sp = mfx->serviceapplication;
1104         int ret = -1;
1105         char query[MAX_QUERY_LEN] = {'\0'};
1106         while(sp != NULL)
1107         {
1108                 snprintf(query, MAX_QUERY_LEN,
1109                          "insert into package_app_info(app_id, app_component, app_exec, app_type, app_onboot, " \
1110                         "app_multiple, app_autorestart, app_enabled, package) " \
1111                         "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1112                          sp->appid, "svcapp", sp->exec, sp->type, sp->onboot, "\0",
1113                          sp->autorestart, sp->enabled, mfx->package);
1114                 ret = __exec_query(query);
1115                 if (ret == -1) {
1116                         DBG("Package ServiceApp Info DB Insert Failed\n");
1117                         return -1;
1118                 }
1119                 sp = sp->next;
1120                 memset(query, '\0', MAX_QUERY_LEN);
1121         }
1122         return 0;
1123 }
1124
1125 static int __insert_serviceapplication_appcategory_info(manifest_x *mfx)
1126 {
1127         serviceapplication_x *sp = mfx->serviceapplication;
1128         category_x *ct = NULL;
1129         int ret = -1;
1130         char query[MAX_QUERY_LEN] = {'\0'};
1131         while(sp != NULL)
1132         {
1133                 ct = sp->category;
1134                 while (ct != NULL)
1135                 {
1136                         snprintf(query, MAX_QUERY_LEN,
1137                                 "insert into package_app_app_category(app_id, category) " \
1138                                 "values('%s','%s')",\
1139                                  sp->appid, ct->name);
1140                         ret = __exec_query(query);
1141                         if (ret == -1) {
1142                                 DBG("Package ServiceApp Category Info DB Insert Failed\n");
1143                                 return -1;
1144                         }
1145                         ct = ct->next;
1146                         memset(query, '\0', MAX_QUERY_LEN);
1147                 }
1148                 sp = sp->next;
1149         }
1150         return 0;
1151 }
1152
1153 static int __insert_serviceapplication_appmetadata_info(manifest_x *mfx)
1154 {
1155         serviceapplication_x *sp = mfx->serviceapplication;
1156         metadata_x *md = NULL;
1157         int ret = -1;
1158         char query[MAX_QUERY_LEN] = {'\0'};
1159         while(sp != NULL)
1160         {
1161                 md = sp->metadata;
1162                 while (md != NULL)
1163                 {
1164                         if (md->key) {
1165                                 snprintf(query, MAX_QUERY_LEN,
1166                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
1167                                         "values('%s','%s', '%s')",\
1168                                          sp->appid, md->key, md->value);
1169                                 ret = __exec_query(query);
1170                                 if (ret == -1) {
1171                                         DBG("Package ServiceApp Metadata Info DB Insert Failed\n");
1172                                         return -1;
1173                                 }
1174                         }
1175                         md = md->next;
1176                         memset(query, '\0', MAX_QUERY_LEN);
1177                 }
1178                 sp = sp->next;
1179         }
1180         return 0;
1181 }
1182
1183 static int __insert_serviceapplication_apppermission_info(manifest_x *mfx)
1184 {
1185         serviceapplication_x *sp = mfx->serviceapplication;
1186         permission_x *pm = NULL;
1187         int ret = -1;
1188         char query[MAX_QUERY_LEN] = {'\0'};
1189         while(sp != NULL)
1190         {
1191                 pm = sp->permission;
1192                 while (pm != NULL)
1193                 {
1194                         snprintf(query, MAX_QUERY_LEN,
1195                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
1196                                 "values('%s','%s', '%s')",\
1197                                  sp->appid, pm->type, pm->value);
1198                         ret = __exec_query(query);
1199                         if (ret == -1) {
1200                                 DBG("Package ServiceApp permission Info DB Insert Failed\n");
1201                                 return -1;
1202                         }
1203                         pm = pm->next;
1204                         memset(query, '\0', MAX_QUERY_LEN);
1205                 }
1206                 sp = sp->next;
1207         }
1208         return 0;
1209 }
1210
1211 static int __insert_serviceapplication_appcontrol_info(manifest_x *mfx)
1212 {
1213         serviceapplication_x *sp = mfx->serviceapplication;
1214         appcontrol_x *acontrol = NULL;
1215         int ret = -1;
1216         char query[MAX_QUERY_LEN] = {'\0'};
1217         operation_x *op = NULL;
1218         mime_x *mi = NULL;
1219         uri_x *ui = NULL;
1220         subapp_x *sub = NULL;
1221         const char *operation = NULL;
1222         const char *mime = NULL;
1223         const char *uri = NULL;
1224         const char *subapp = NULL;
1225         while(sp != NULL)
1226         {
1227                 acontrol = sp->appcontrol;
1228                 while(acontrol != NULL)
1229                 {
1230                         op = acontrol->operation;
1231                         while(op != NULL)
1232                         {
1233                         if (op)
1234                                 operation = op->name;
1235                         mi = acontrol->mime;
1236                                 do
1237                                 {
1238                                 if (mi)
1239                                         mime = mi->name;
1240                                 sub = acontrol->subapp;
1241                                         do
1242                                         {
1243                                         if (sub)
1244                                                 subapp = sub->name;
1245                                         ui = acontrol->uri;
1246                                                 do
1247                                                 {
1248                                                         if (ui)
1249                                                                 uri = ui->name;
1250                                                         snprintf(query, MAX_QUERY_LEN,
1251                                                                  "insert into package_app_app_control(app_id, operation, uri_scheme, mime_type,subapp_name) " \
1252                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
1253                                                                  sp->appid, operation, uri, mime, subapp);
1254                                                         ret = __exec_query(query);
1255                                                         if (ret == -1) {
1256                                                                 DBG("Package UiApp AppSvc DB Insert Failed\n");
1257                                                                 return -1;
1258                                                         }
1259                                                         memset(query, '\0', MAX_QUERY_LEN);
1260                                                         if (ui)
1261                                                                 ui = ui->next;
1262                                                         uri = NULL;
1263                                                 } while(ui != NULL);
1264                                                 if (sub)
1265                                                         sub = sub->next;
1266                                                 subapp = NULL;
1267                                                 }while(sub != NULL);
1268                                         if (mi)
1269                                                 mi = mi->next;
1270                                         mime = NULL;
1271                                 }while(mi != NULL);
1272                                 if (op)
1273                                         op = op->next;
1274                                 operation = NULL;
1275                         }
1276                         acontrol = acontrol->next;
1277                 }
1278                 sp = sp->next;
1279         }
1280         return 0;
1281 }
1282
1283 static int __insert_serviceapplication_appsvc_info(manifest_x *mfx)
1284 {
1285         serviceapplication_x *sp = mfx->serviceapplication;
1286         appsvc_x *asvc = NULL;
1287         int ret = -1;
1288         char query[MAX_QUERY_LEN] = {'\0'};
1289         operation_x *op = NULL;
1290         mime_x *mi = NULL;
1291         uri_x *ui = NULL;
1292         subapp_x *sub = NULL;
1293         const char *operation = NULL;
1294         const char *mime = NULL;
1295         const char *uri = NULL;
1296         const char *subapp = NULL;
1297         while(sp != NULL)
1298         {
1299                 asvc = sp->appsvc;
1300                 while(asvc != NULL)
1301                 {
1302                         op = asvc->operation;
1303                         while(op != NULL)
1304                         {
1305                         if (op)
1306                                 operation = op->name;
1307                         mi = asvc->mime;
1308                                 do
1309                                 {
1310                                 if (mi)
1311                                         mime = mi->name;
1312                                 sub = asvc->subapp;
1313                                         do
1314                                         {
1315                                         if (sub)
1316                                                 subapp = sub->name;
1317                                         ui = asvc->uri;
1318                                                         do
1319                                                         {
1320                                                                 if (ui)
1321                                                                         uri = ui->name;
1322                                                                 snprintf(query, MAX_QUERY_LEN,
1323                                                                          "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1324                                                                         "values('%s', '%s', '%s', '%s', '%s')",\
1325                                                                          sp->appid, operation, uri, mime, subapp);
1326                                                                 ret = __exec_query(query);
1327                                                                 if (ret == -1) {
1328                                                                         DBG("Package UiApp AppSvc DB Insert Failed\n");
1329                                                                         return -1;
1330                                                                 }
1331                                                                 memset(query, '\0', MAX_QUERY_LEN);
1332                                                                 if (ui)
1333                                                                         ui = ui->next;
1334                                                                 uri = NULL;
1335                                                         } while(ui != NULL);
1336                                                 if (sub)
1337                                                         sub     = sub->next;
1338                                                 subapp = NULL;
1339                                         }while(sub != NULL);
1340                                         if (mi)
1341                                                 mi = mi->next;
1342                                         mime = NULL;
1343                                 }while(mi != NULL);
1344                                 if (op)
1345                                         op = op->next;
1346                                 operation = NULL;
1347                         }
1348                         asvc = asvc->next;
1349                 }
1350                 sp = sp->next;
1351         }
1352         return 0;
1353 }
1354
1355
1356
1357 static int __insert_serviceapplication_share_request_info(manifest_x *mfx)
1358 {
1359         serviceapplication_x *sp = mfx->serviceapplication;
1360         datashare_x *ds = NULL;
1361         request_x *rq = NULL;
1362         int ret = -1;
1363         char query[MAX_QUERY_LEN] = {'\0'};
1364         while(sp != NULL)
1365         {
1366                 ds = sp->datashare;
1367                 while(ds != NULL)
1368                 {
1369                         rq = ds->request;
1370                         while(rq != NULL)
1371                         {
1372                                 snprintf(query, MAX_QUERY_LEN,
1373                                          "insert into package_app_share_request(app_id, data_share_request) " \
1374                                         "values('%s', '%s')",\
1375                                          sp->appid, rq->text);
1376                                 ret = __exec_query(query);
1377                                 if (ret == -1) {
1378                                         DBG("Package ServiceApp Share Request DB Insert Failed\n");
1379                                         return -1;
1380                                 }
1381                                 memset(query, '\0', MAX_QUERY_LEN);
1382                                 rq = rq->next;
1383                         }
1384                         ds = ds->next;
1385                 }
1386                 sp = sp->next;
1387         }
1388         return 0;
1389 }
1390
1391
1392
1393 static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx)
1394 {
1395         serviceapplication_x *sp = mfx->serviceapplication;
1396         datashare_x *ds = NULL;
1397         define_x *df = NULL;
1398         allowed_x *al = NULL;
1399         int ret = -1;
1400         char query[MAX_QUERY_LEN] = {'\0'};
1401         while(sp != NULL)
1402         {
1403                 ds = sp->datashare;
1404                 while(ds != NULL)
1405                 {
1406                         df = ds->define;
1407                         while(df != NULL)
1408                         {
1409                                 al = df->allowed;
1410                                 while(al != NULL)
1411                                 {
1412                                         snprintf(query, MAX_QUERY_LEN,
1413                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1414                                                 "values('%s', '%s', '%s')",\
1415                                                  sp->appid, df->path, al->text);
1416                                         ret = __exec_query(query);
1417                                         if (ret == -1) {
1418                                                 DBG("Package App Share Allowed DB Insert Failed\n");
1419                                                 return -1;
1420                                         }
1421                                         memset(query, '\0', MAX_QUERY_LEN);
1422                                         al = al->next;
1423                                 }
1424                                 df = df->next;
1425                         }
1426                         ds = ds->next;
1427                 }
1428                 sp = sp->next;
1429         }
1430         return 0;
1431 }
1432
1433 static int __insert_manifest_info_in_db(manifest_x *mfx)
1434 {
1435         label_x *lbl = mfx->label;
1436         license_x *lcn = mfx->license;
1437         icon_x *icn = mfx->icon;
1438         description_x *dcn = mfx->description;
1439         author_x *ath = mfx->author;
1440         uiapplication_x *up = mfx->uiapplication;
1441         uiapplication_x *up_icn = mfx->uiapplication;
1442         uiapplication_x *up_image = mfx->uiapplication;
1443         serviceapplication_x *sp = mfx->serviceapplication;
1444         privileges_x *pvs = NULL;
1445         privilege_x *pv = NULL;
1446         char query[MAX_QUERY_LEN] = { '\0' };
1447         char root[MAX_QUERY_LEN] = { '\0' };
1448         int ret = -1;
1449         char *type = NULL;
1450         char *path = NULL;
1451         const char *auth_name = NULL;
1452         const char *auth_email = NULL;
1453         const char *auth_href = NULL;
1454         if (ath) {
1455                 if (ath->text)
1456                         auth_name = ath->text;
1457                 if (ath->email)
1458                         auth_email = ath->email;
1459                 if (ath->href)
1460                         auth_href = ath->href;
1461         }
1462
1463         /*Insert in the package_info DB*/
1464         if (mfx->type)
1465                 type = strdup(mfx->type);
1466         else
1467                 type = strdup("rpm");
1468         /*Insert in the package_info DB*/
1469         if (mfx->root_path)
1470                 path = strdup(mfx->root_path);
1471         else{
1472                 if (strcmp(type,"rpm")==0)
1473                         snprintf(root, MAX_QUERY_LEN - 1, "/usr/apps/%s", mfx->package);
1474                 else
1475                         snprintf(root, MAX_QUERY_LEN - 1, "/opt/usr/apps/%s", mfx->package);
1476
1477                 path = strdup(root);
1478         }
1479         snprintf(query, MAX_QUERY_LEN,
1480                  "insert into package_info(package, package_type, package_version, install_location, package_size, " \
1481                 "package_removable, package_preload, package_readonly, package_update, package_appsetting, author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path) " \
1482                 "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1483                  mfx->package, type, mfx->version, mfx->installlocation, mfx->package_size, mfx->removable, mfx->preload,
1484                  mfx->readonly, mfx->update, mfx->appsetting, auth_name, auth_email, auth_href, mfx->installed_time, mfx->installed_storage, mfx->storeclient_id, mfx->mainapp_id, mfx->package_url, path);
1485         ret = __exec_query(query);
1486         if (ret == -1) {
1487                 DBG("Package Info DB Insert Failed\n");
1488                 if (type) {
1489                         free(type);
1490                         type = NULL;
1491                 }
1492                 if (path) {
1493                         free(path);
1494                         path = NULL;
1495                 }
1496                 return -1;
1497         }
1498         if (type) {
1499                 free(type);
1500                 type = NULL;
1501         }
1502         if (path) {
1503                 free(path);
1504                 path = NULL;
1505         }
1506
1507         /*Insert in the package_privilege_info DB*/
1508         pvs = mfx->privileges;
1509         while (pvs != NULL) {
1510                 pv = pvs->privilege;
1511                 while (pv != NULL) {
1512                         memset(query, '\0', MAX_QUERY_LEN);
1513                         snprintf(query, MAX_QUERY_LEN,
1514                                 "insert into package_privilege_info(package, privilege) " \
1515                                 "values('%s','%s')",\
1516                                  mfx->package, pv->text);
1517                         ret = __exec_query(query);
1518                         if (ret == -1) {
1519                                 DBG("Package Privilege Info DB Insert Failed\n");
1520                                 return -1;
1521                         }
1522                         pv = pv->next;
1523                 }
1524                 pvs = pvs->next;
1525         }
1526
1527         /*Insert the package locale and app locale info */
1528         pkglocale = __create_locale_list(pkglocale, lbl, lcn, icn, dcn, ath);
1529         g_list_foreach(pkglocale, __trimfunc1, NULL);
1530         prev = NULL;
1531
1532         while(up != NULL)
1533         {
1534                 applocale = __create_locale_list(applocale, up->label, NULL, up->icon, NULL, NULL);
1535                 up = up->next;
1536         }
1537         while(sp != NULL)
1538         {
1539                 applocale = __create_locale_list(applocale, sp->label, NULL, sp->icon, NULL, NULL);
1540                 sp = sp->next;
1541         }
1542         g_list_foreach(applocale, __trimfunc2, NULL);
1543         prev = NULL;
1544
1545         /*Insert the app icon info */
1546         while(up_icn != NULL)
1547         {
1548                 appicon = __create_icon_list(appicon, up_icn->icon);
1549                 up_icn = up_icn->next;
1550         }
1551         g_list_foreach(appicon, __trimfunc3, NULL);
1552         prev = NULL;
1553
1554         /*Insert the image info */
1555         while(up_image != NULL)
1556         {
1557                 appimage = __create_image_list(appimage, up_image->image);
1558                 up_image = up_image->next;
1559         }
1560         g_list_foreach(appimage, __trimfunc4, NULL);
1561         prev = NULL;
1562
1563         /*g_list_foreach(pkglocale, __printfunc, NULL);*/
1564         /*DBG("\n");*/
1565         /*g_list_foreach(applocale, __printfunc, NULL);*/
1566
1567         /*package locale info*/
1568         g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1569         /*native app locale info*/
1570         up = mfx->uiapplication;
1571         while(up != NULL)
1572         {
1573                 g_list_foreach(applocale, __insert_uiapplication_locale_info, (gpointer)up);
1574                 up = up->next;
1575         }
1576         /*agent app locale info*/
1577         sp = mfx->serviceapplication;
1578         while(sp != NULL)
1579         {
1580                 g_list_foreach(applocale, __insert_serviceapplication_locale_info, (gpointer)sp);
1581                 sp = sp->next;
1582         }
1583
1584         /*app icon locale info*/
1585         up_icn = mfx->uiapplication;
1586         while(up_icn != NULL)
1587         {
1588                 g_list_foreach(appicon, __insert_uiapplication_icon_section_info, (gpointer)up_icn);
1589                 up_icn = up_icn->next;
1590         }
1591
1592         /*app image info*/
1593         up_image = mfx->uiapplication;
1594         while(up_image != NULL)
1595         {
1596                 g_list_foreach(appimage, __insert_uiapplication_image_info, (gpointer)up_image);
1597                 up_image = up_image->next;
1598         }
1599
1600         g_list_free(pkglocale);
1601         pkglocale = NULL;
1602         g_list_free(applocale);
1603         applocale = NULL;
1604         g_list_free(appicon);
1605         appicon = NULL;
1606         g_list_free(appimage);
1607         appimage = NULL;
1608
1609
1610         /*Insert in the package_app_info DB*/
1611         ret = __insert_uiapplication_info(mfx);
1612         if (ret == -1)
1613                 return -1;
1614         ret = __insert_ui_mainapp_info(mfx);
1615         if (ret == -1)
1616                 return -1;
1617         ret = __insert_serviceapplication_info(mfx);
1618         if (ret == -1)
1619                 return -1;
1620
1621         /*Insert in the package_app_app_control DB*/
1622         ret = __insert_uiapplication_appcontrol_info(mfx);
1623         if (ret == -1)
1624                 return -1;
1625         ret = __insert_serviceapplication_appcontrol_info(mfx);
1626         if (ret == -1)
1627                 return -1;
1628
1629         /*Insert in the package_app_app_category DB*/
1630         ret = __insert_uiapplication_appcategory_info(mfx);
1631         if (ret == -1)
1632                 return -1;
1633         ret = __insert_serviceapplication_appcategory_info(mfx);
1634         if (ret == -1)
1635                 return -1;
1636
1637         /*Insert in the package_app_app_metadata DB*/
1638         ret = __insert_uiapplication_appmetadata_info(mfx);
1639         if (ret == -1)
1640                 return -1;
1641         ret = __insert_serviceapplication_appmetadata_info(mfx);
1642         if (ret == -1)
1643                 return -1;
1644
1645         /*Insert in the package_app_app_permission DB*/
1646         ret = __insert_uiapplication_apppermission_info(mfx);
1647         if (ret == -1)
1648                 return -1;
1649         ret = __insert_serviceapplication_apppermission_info(mfx);
1650         if (ret == -1)
1651                 return -1;
1652
1653         /*Insert in the package_app_app_svc DB*/
1654         ret = __insert_uiapplication_appsvc_info(mfx);
1655         if (ret == -1)
1656                 return -1;
1657         ret = __insert_serviceapplication_appsvc_info(mfx);
1658         if (ret == -1)
1659                 return -1;
1660
1661         /*Insert in the package_app_share_allowed DB*/
1662         ret = __insert_uiapplication_share_allowed_info(mfx);
1663         if (ret == -1)
1664                 return -1;
1665         ret = __insert_serviceapplication_share_allowed_info(mfx);
1666         if (ret == -1)
1667                 return -1;
1668
1669         /*Insert in the package_app_share_request DB*/
1670         ret = __insert_uiapplication_share_request_info(mfx);
1671         if (ret == -1)
1672                 return -1;
1673         ret = __insert_serviceapplication_share_request_info(mfx);
1674         if (ret == -1)
1675                 return -1;
1676
1677         return 0;
1678
1679 }
1680
1681 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1682 {
1683         char query[MAX_QUERY_LEN] = { '\0' };
1684         int ret = -1;
1685         memset(query, '\0', MAX_QUERY_LEN);
1686         snprintf(query, MAX_QUERY_LEN,
1687                  "delete from %s where app_id='%s'", db_table, appid);
1688         ret = __exec_query(query);
1689         if (ret == -1) {
1690                 DBG("DB Deletion from table (%s) Failed\n", db_table);
1691                 return -1;
1692         }
1693         return 0;
1694 }
1695
1696 static int __delete_manifest_info_from_db(manifest_x *mfx)
1697 {
1698         char query[MAX_QUERY_LEN] = { '\0' };
1699         int ret = -1;
1700         uiapplication_x *up = mfx->uiapplication;
1701         serviceapplication_x *sp = mfx->serviceapplication;
1702         /*Delete from cert table*/
1703         ret = pkgmgrinfo_delete_certinfo(mfx->package);
1704         if (ret) {
1705                 DBG("Cert Info  DB Delete Failed\n");
1706                 return -1;
1707         }
1708
1709         /*Delete from Package Info DB*/
1710         snprintf(query, MAX_QUERY_LEN,
1711                  "delete from package_info where package='%s'", mfx->package);
1712         ret = __exec_query(query);
1713         if (ret == -1) {
1714                 DBG("Package Info DB Delete Failed\n");
1715                 return -1;
1716         }
1717         memset(query, '\0', MAX_QUERY_LEN);
1718
1719         /*Delete from Package Localized Info*/
1720         snprintf(query, MAX_QUERY_LEN,
1721                  "delete from package_localized_info where package='%s'", mfx->package);
1722         ret = __exec_query(query);
1723         if (ret == -1) {
1724                 DBG("Package Localized Info DB Delete Failed\n");
1725                 return -1;
1726         }
1727
1728         /*Delete from Package Privilege Info*/
1729         snprintf(query, MAX_QUERY_LEN,
1730                  "delete from package_privilege_info where package='%s'", mfx->package);
1731         ret = __exec_query(query);
1732         if (ret == -1) {
1733                 DBG("Package Privilege Info DB Delete Failed\n");
1734                 return -1;
1735         }
1736
1737         while (up != NULL) {
1738                 ret = __delete_appinfo_from_db("package_app_info", up->appid);
1739                 if (ret < 0)
1740                         return ret;
1741                 ret = __delete_appinfo_from_db("package_app_localized_info", up->appid);
1742                 if (ret < 0)
1743                         return ret;
1744                 ret = __delete_appinfo_from_db("package_app_icon_section_info", up->appid);
1745                 if (ret < 0)
1746                         return ret;
1747                 ret = __delete_appinfo_from_db("package_app_image_info", up->appid);
1748                 if (ret < 0)
1749                         return ret;
1750                 ret = __delete_appinfo_from_db("package_app_app_svc", up->appid);
1751                 if (ret < 0)
1752                         return ret;
1753                 ret = __delete_appinfo_from_db("package_app_app_control", up->appid);
1754                 if (ret < 0)
1755                         return ret;
1756                 ret = __delete_appinfo_from_db("package_app_app_category", up->appid);
1757                 if (ret < 0)
1758                         return ret;
1759                 ret = __delete_appinfo_from_db("package_app_app_metadata", up->appid);
1760                 if (ret < 0)
1761                         return ret;
1762                 ret = __delete_appinfo_from_db("package_app_app_permission", up->appid);
1763                 if (ret < 0)
1764                         return ret;
1765                 ret = __delete_appinfo_from_db("package_app_share_allowed", up->appid);
1766                 if (ret < 0)
1767                         return ret;
1768                 ret = __delete_appinfo_from_db("package_app_share_request", up->appid);
1769                 if (ret < 0)
1770                         return ret;
1771                 up = up->next;
1772         }
1773
1774         while (sp != NULL) {
1775                 ret = __delete_appinfo_from_db("package_app_info", sp->appid);
1776                 if (ret < 0)
1777                         return ret;
1778                 ret = __delete_appinfo_from_db("package_app_localized_info", sp->appid);
1779                 if (ret < 0)
1780                         return ret;
1781                 ret = __delete_appinfo_from_db("package_app_icon_section_info", sp->appid);
1782                 if (ret < 0)
1783                         return ret;
1784                 ret = __delete_appinfo_from_db("package_app_image_info", sp->appid);
1785                 if (ret < 0)
1786                         return ret;
1787                 ret = __delete_appinfo_from_db("package_app_app_svc", sp->appid);
1788                 if (ret < 0)
1789                         return ret;
1790                 ret = __delete_appinfo_from_db("package_app_app_control", sp->appid);
1791                 if (ret < 0)
1792                         return ret;
1793                 ret = __delete_appinfo_from_db("package_app_app_category", sp->appid);
1794                 if (ret < 0)
1795                         return ret;
1796                 ret = __delete_appinfo_from_db("package_app_app_metadata", sp->appid);
1797                 if (ret < 0)
1798                         return ret;
1799                 ret = __delete_appinfo_from_db("package_app_app_permission", sp->appid);
1800                 if (ret < 0)
1801                         return ret;
1802                 ret = __delete_appinfo_from_db("package_app_share_allowed", sp->appid);
1803                 if (ret < 0)
1804                         return ret;
1805                 ret = __delete_appinfo_from_db("package_app_share_request", sp->appid);
1806                 if (ret < 0)
1807                         return ret;
1808                 sp = sp->next;
1809         }
1810         return 0;
1811 }
1812
1813
1814 int pkgmgr_parser_initialize_db()
1815 {
1816         int ret = -1;
1817         /*Manifest DB*/
1818         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1819         if (ret == -1) {
1820                 DBG("package info DB initialization failed\n");
1821                 return ret;
1822         }
1823         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
1824         if (ret == -1) {
1825                 DBG("package localized info DB initialization failed\n");
1826                 return ret;
1827         }
1828         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
1829         if (ret == -1) {
1830                 DBG("package app app privilege DB initialization failed\n");
1831                 return ret;
1832         }
1833         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
1834         if (ret == -1) {
1835                 DBG("package app info DB initialization failed\n");
1836                 return ret;
1837         }
1838         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
1839         if (ret == -1) {
1840                 DBG("package app localized info DB initialization failed\n");
1841                 return ret;
1842         }
1843         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
1844         if (ret == -1) {
1845                 DBG("package app icon localized info DB initialization failed\n");
1846                 return ret;
1847         }
1848         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
1849         if (ret == -1) {
1850                 DBG("package app image info DB initialization failed\n");
1851                 return ret;
1852         }
1853         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
1854         if (ret == -1) {
1855                 DBG("package app app control DB initialization failed\n");
1856                 return ret;
1857         }
1858         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
1859         if (ret == -1) {
1860                 DBG("package app app category DB initialization failed\n");
1861                 return ret;
1862         }
1863         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
1864         if (ret == -1) {
1865                 DBG("package app app category DB initialization failed\n");
1866                 return ret;
1867         }
1868         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
1869         if (ret == -1) {
1870                 DBG("package app app permission DB initialization failed\n");
1871                 return ret;
1872         }
1873         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC);
1874         if (ret == -1) {
1875                 DBG("package app app svc DB initialization failed\n");
1876                 return ret;
1877         }
1878         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
1879         if (ret == -1) {
1880                 DBG("package app share allowed DB initialization failed\n");
1881                 return ret;
1882         }
1883         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
1884         if (ret == -1) {
1885                 DBG("package app share request DB initialization failed\n");
1886                 return ret;
1887         }
1888         /*Cert DB*/
1889         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
1890         if (ret == -1) {
1891                 DBG("package cert info DB initialization failed\n");
1892                 return ret;
1893         }
1894         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
1895         if (ret == -1) {
1896                 DBG("package cert index info DB initialization failed\n");
1897                 return ret;
1898         }
1899         return 0;
1900 }
1901
1902 int pkgmgr_parser_check_and_create_db()
1903 {
1904         int ret = -1;
1905         /*Manifest DB*/
1906         ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, PKGMGR_PARSER_DB_FILE);
1907         if (ret) {
1908                 DBG("Manifest DB creation Failed\n");
1909                 return -1;
1910         }
1911         /*Cert DB*/
1912         ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, PKGMGR_CERT_DB_FILE);
1913         if (ret) {
1914                 DBG("Cert DB creation Failed\n");
1915                 return -1;
1916         }
1917         return 0;
1918 }
1919
1920 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1921 {
1922         if (mfx == NULL) {
1923                 DBG("manifest pointer is NULL\n");
1924                 return -1;
1925         }
1926         int ret = 0;
1927         ret = pkgmgr_parser_check_and_create_db();
1928         if (ret == -1) {
1929                 DBG("Failed to open DB\n");
1930                 return ret;
1931         }
1932         ret = pkgmgr_parser_initialize_db();
1933         if (ret == -1)
1934                 goto err;
1935         /*Begin transaction*/
1936         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1937         if (ret != SQLITE_OK) {
1938                 DBG("Failed to begin transaction\n");
1939                 ret = -1;
1940                 goto err;
1941         }
1942         DBG("Transaction Begin\n");
1943         ret = __insert_manifest_info_in_db(mfx);
1944         if (ret == -1) {
1945                 DBG("Insert into DB failed. Rollback now\n");
1946                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1947                 goto err;
1948         }
1949         /*Commit transaction*/
1950         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1951         if (ret != SQLITE_OK) {
1952                 DBG("Failed to commit transaction. Rollback now\n");
1953                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1954                 ret = -1;
1955                 goto err;
1956         }
1957         DBG("Transaction Commit and End\n");
1958 err:
1959         sqlite3_close(pkgmgr_parser_db);
1960         sqlite3_close(pkgmgr_cert_db);
1961         return ret;
1962 }
1963
1964 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1965 {
1966         if (mfx == NULL) {
1967                 DBG("manifest pointer is NULL\n");
1968                 return -1;
1969         }
1970         int ret = 0;
1971         ret = pkgmgr_parser_check_and_create_db();
1972         if (ret == -1) {
1973                 DBG("Failed to open DB\n");
1974                 return ret;
1975         }
1976         ret = pkgmgr_parser_initialize_db();
1977         if (ret == -1)
1978                 goto err;
1979         /*Preserve guest mode visibility*/
1980         __preserve_guestmode_visibility_value( mfx);
1981         /*Begin transaction*/
1982         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1983         if (ret != SQLITE_OK) {
1984                 DBG("Failed to begin transaction\n");
1985                 ret = -1;
1986                 goto err;
1987         }
1988         DBG("Transaction Begin\n");
1989         ret = __delete_manifest_info_from_db(mfx);
1990         if (ret == -1) {
1991                 DBG("Delete from DB failed. Rollback now\n");
1992                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1993                 goto err;
1994         }
1995         ret = __insert_manifest_info_in_db(mfx);
1996         if (ret == -1) {
1997                 DBG("Insert into DB failed. Rollback now\n");
1998                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1999                 goto err;
2000         }
2001
2002         /*Commit transaction*/
2003         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2004         if (ret != SQLITE_OK) {
2005                 DBG("Failed to commit transaction. Rollback now\n");
2006                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2007                 ret = -1;
2008                 goto err;
2009         }
2010         DBG("Transaction Commit and End\n");
2011 err:
2012         sqlite3_close(pkgmgr_parser_db);
2013         sqlite3_close(pkgmgr_cert_db);
2014         return ret;
2015 }
2016
2017 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
2018 {
2019         if (mfx == NULL) {
2020                 DBG("manifest pointer is NULL\n");
2021                 return -1;
2022         }
2023         int ret = 0;
2024         ret = pkgmgr_parser_check_and_create_db();
2025         if (ret == -1) {
2026                 DBG("Failed to open DB\n");
2027                 return ret;
2028         }
2029         /*Begin transaction*/
2030         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2031         if (ret != SQLITE_OK) {
2032                 DBG("Failed to begin transaction\n");
2033                 ret = -1;
2034                 goto err;
2035         }
2036         DBG("Transaction Begin\n");
2037         ret = __delete_manifest_info_from_db(mfx);
2038         if (ret == -1) {
2039                 DBG("Delete from DB failed. Rollback now\n");
2040                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2041                 goto err;
2042         }
2043         /*Commit transaction*/
2044         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2045         if (ret != SQLITE_OK) {
2046                 DBG("Failed to commit transaction, Rollback now\n");
2047                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2048                 ret = -1;
2049                 goto err;
2050         }
2051         DBG("Transaction Commit and End\n");
2052         sqlite3_close(pkgmgr_parser_db);
2053 err:
2054         sqlite3_close(pkgmgr_parser_db);
2055         sqlite3_close(pkgmgr_cert_db);
2056         return ret;
2057 }