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