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