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