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