Merge "Adding Mime types parsing" into tizen_2.1
[platform/core/appfw/pkgmgr-info.git] / parser / pkgmgr_parser_db.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <db-util.h>
30 #include <glib.h>
31 #include "pkgmgr-info.h"
32 #include "pkgmgr_parser_internal.h"
33 #include "pkgmgr_parser_db.h"
34
35 #define PKGMGR_PARSER_DB_FILE "/opt/dbspace/.pkgmgr_parser.db"
36 #define PKGMGR_CERT_DB_FILE "/opt/dbspace/.pkgmgr_cert.db"
37 #define MAX_QUERY_LEN           4096
38 sqlite3 *pkgmgr_parser_db;
39 sqlite3 *pkgmgr_cert_db;
40 GList *pkglocale = NULL;
41 GList *applocale = NULL;
42 GList *appicon = NULL;
43 GList *appimage = NULL;
44 char *prev = NULL;
45
46 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
47                                                 "(package text primary key not null, " \
48                                                 "package_type text DEFAULT 'rpm', " \
49                                                 "package_version text, " \
50                                                 "install_location text, " \
51                                                 "package_size text, " \
52                                                 "package_removable text DEFAULT 'true', " \
53                                                 "package_preload text DEFAULT 'false', " \
54                                                 "package_readonly text DEFAULT 'false', " \
55                                                 "package_update text DEFAULT 'false', " \
56                                                 "package_appsetting text DEFAULT 'false', " \
57                                                 "package_nodisplay text DEFAULT 'false', " \
58                                                 "author_name text, " \
59                                                 "author_email text, " \
60                                                 "author_href text," \
61                                                 "installed_time text," \
62                                                 "installed_storage text," \
63                                                 "storeclient_id text," \
64                                                 "mainapp_id text," \
65                                                 "package_url text," \
66                                                 "root_path text)"
67
68 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
69                                                 "(package text not null, " \
70                                                 "package_locale text DEFAULT 'No Locale', " \
71                                                 "package_label text, " \
72                                                 "package_icon text, " \
73                                                 "package_description text, " \
74                                                 "package_license text, " \
75                                                 "package_author, " \
76                                                 "PRIMARY KEY(package, package_locale), " \
77                                                 "FOREIGN KEY(package) " \
78                                                 "REFERENCES package_info(package) " \
79                                                 "ON DELETE CASCADE)"
80
81 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
82                                                 "(package text not null, " \
83                                                 "privilege text not null, " \
84                                                 "PRIMARY KEY(package, privilege) " \
85                                                 "FOREIGN KEY(package) " \
86                                                 "REFERENCES package_info(package) " \
87                                                 "ON DELETE CASCADE)"
88
89 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
90                                                 "(app_id text primary key not null, " \
91                                                 "app_component text, " \
92                                                 "app_exec text, " \
93                                                 "app_nodisplay text DEFAULT 'false', " \
94                                                 "app_type text, " \
95                                                 "app_onboot text DEFAULT 'false', " \
96                                                 "app_multiple text DEFAULT 'false', " \
97                                                 "app_autorestart text DEFAULT 'false', " \
98                                                 "app_taskmanage text DEFAULT 'false', " \
99                                                 "app_enabled text DEFAULT 'true', " \
100                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
101                                                 "app_mainapp text, " \
102                                                 "app_recentimage text, " \
103                                                 "app_launchcondition text, " \
104                                                 "app_indicatordisplay text DEFAULT 'true', " \
105                                                 "app_portraitimg text, " \
106                                                 "app_landscapeimg text, " \
107                                                 "app_guestmodevisibility text DEFAULT 'true', " \
108                                                 "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         manifest_x *mfx = (manifest_x *)userdata;
636         uiapplication_x *up = (uiapplication_x*)userdata;
637         label_x *lbl = up->label;
638         icon_x *icn = up->icon;
639
640         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
641         if (!label && !icon)
642                 return;
643         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
644                 "app_label, app_icon) values " \
645                 "('%q', '%q', '%q', '%q')", up->appid, (char*)data,
646                 label, icon);
647         ret = __exec_query(query);
648         if (ret == -1)
649                 DBG("Package UiApp Localized Info DB Insert failed\n");
650
651         /*insert ui app locale info to pkg locale to get mainapp data */
652         if (strcasecmp(up->mainapp, "true")==0) {
653                 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
654                         "package_label, package_icon, package_description, package_license, package_author) values " \
655                         "('%q', '%q', '%q', '%q', '%q', '%q', '%q')", mfx->package, (char*)data,
656                         label, icon, NULL, NULL, NULL);
657                 ret = __exec_query_no_msg(query);
658                 if (ret == -1)
659                         DBG("Package locale info inserted before.\n");
660         }
661 }
662
663 static void __insert_uiapplication_icon_section_info(gpointer data, gpointer userdata)
664 {
665         int ret = -1;
666         char *icon = NULL;
667         char *resolution = NULL;
668         char query[MAX_QUERY_LEN] = {'\0'};
669
670         uiapplication_x *up = (uiapplication_x*)userdata;
671         icon_x *icn = up->icon;
672
673         __extract_icon_data(data, icn, &icon, &resolution);
674         if (!icon && !resolution)
675                 return;
676         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
677                 "app_icon, app_icon_section, app_icon_resolution) values " \
678                 "('%q', '%q', '%q', '%q')", up->appid,
679                 icon, (char*)data, resolution);
680
681         ret = __exec_query(query);
682         if (ret == -1)
683                 DBG("Package UiApp Localized Info DB Insert failed\n");
684
685 }
686
687 static void __insert_uiapplication_image_info(gpointer data, gpointer userdata)
688 {
689         int ret = -1;
690         char *lang = NULL;
691         char *img = NULL;
692         char query[MAX_QUERY_LEN] = {'\0'};
693
694         uiapplication_x *up = (uiapplication_x*)userdata;
695         image_x *image = up->image;
696
697         __extract_image_data(data, image, &lang, &img);
698         if (!lang && !img)
699                 return;
700         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
701                 "app_image_section, app_image) values " \
702                 "('%q', '%q', '%q', '%q')", up->appid, lang, (char*)data, img);
703
704         ret = __exec_query(query);
705         if (ret == -1)
706                 DBG("Package UiApp image Info DB Insert failed\n");
707
708 }
709
710
711 static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata)
712 {
713         int ret = -1;
714         char *icon = NULL;
715         char *label = NULL;
716         char query[MAX_QUERY_LEN] = {'\0'};
717
718         serviceapplication_x *sp = (serviceapplication_x*)userdata;
719         label_x *lbl = sp->label;
720         icon_x *icn = sp->icon;
721
722         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
723         if (!icon && !label)
724                 return;
725         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
726                 "app_label, app_icon) values " \
727                 "('%q', '%q', '%q', '%q')", sp->appid, (char*)data,
728                 label, icon);
729         ret = __exec_query(query);
730         if (ret == -1)
731                 DBG("Package ServiceApp Localized Info DB Insert failed\n");
732 }
733
734 static int __insert_ui_mainapp_info(manifest_x *mfx)
735 {
736         uiapplication_x *up = mfx->uiapplication;
737         int ret = -1;
738         char query[MAX_QUERY_LEN] = {'\0'};
739         while(up != NULL)
740         {
741                 snprintf(query, MAX_QUERY_LEN,
742                         "update package_app_info set app_mainapp='%s' where app_id='%s'", up->mainapp, up->appid);
743
744                 ret = __exec_query(query);
745                 if (ret == -1) {
746                         DBG("Package UiApp Info DB Insert Failed\n");
747                         return -1;
748                 }
749                 if (strcasecmp(up->mainapp, "True")==0)
750                         mfx->mainapp_id = strdup(up->appid);
751
752                 up = up->next;
753                 memset(query, '\0', MAX_QUERY_LEN);
754         }
755
756         if (mfx->mainapp_id == NULL){
757                 if (mfx->uiapplication
758                 && mfx->uiapplication->appid) {
759                         snprintf(query, MAX_QUERY_LEN,
760                         "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->uiapplication->appid);
761                 } else if (mfx->serviceapplication
762                         && mfx->serviceapplication->appid) {
763                         snprintf(query, MAX_QUERY_LEN,
764                         "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->serviceapplication->appid);
765                 } else {
766                         DBG("Not valid appid\n");
767                         return -1;
768                 }
769
770                 ret = __exec_query(query);
771                 if (ret == -1) {
772                         DBG("Package UiApp Info DB Insert Failed\n");
773                         return -1;
774                 }
775                 if (mfx->uiapplication && mfx->uiapplication->appid)
776                         mfx->mainapp_id = strdup(mfx->uiapplication->appid);
777                 else if (mfx->serviceapplication && mfx->serviceapplication->appid)
778                         mfx->mainapp_id = strdup(mfx->serviceapplication->appid);
779
780 }
781
782         memset(query, '\0', MAX_QUERY_LEN);
783         snprintf(query, MAX_QUERY_LEN,
784                 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
785         ret = __exec_query(query);
786         if (ret == -1) {
787                 DBG("Package Info DB update Failed\n");
788                 return -1;
789         }
790
791         return 0;
792 }
793 /* _PRODUCT_LAUNCHING_ENHANCED_
794 *  up->indicatordisplay, up->portraitimg, up->landscapeimg, up->guestmode_appstatus
795 */
796 static int __insert_uiapplication_info(manifest_x *mfx)
797 {
798         uiapplication_x *up = mfx->uiapplication;
799         int ret = -1;
800         char query[MAX_QUERY_LEN] = {'\0'};
801         while(up != NULL)
802         {
803                 snprintf(query, MAX_QUERY_LEN,
804                          "insert into package_app_info(app_id, app_component, app_exec, app_nodisplay, app_type, app_onboot, " \
805                         "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_mainapp , app_recentimage, " \
806                         "app_launchcondition, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, package) " \
807                         "values('%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
808                          up->appid, "uiapp", up->exec, up->nodisplay, up->type, "\0", up->multiple,
809                          "\0", up->taskmanage, up->enabled, up->hwacceleration,up->mainapp, up->recentimage,
810                          up->launchcondition, up->indicatordisplay, up->portraitimg, up->landscapeimg, up->guestmode_visibility, up->permission_type, mfx->package);
811                 ret = __exec_query(query);
812                 if (ret == -1) {
813                         DBG("Package UiApp Info DB Insert Failed\n");
814                         return -1;
815                 }
816                 up = up->next;
817                 memset(query, '\0', MAX_QUERY_LEN);
818         }
819         return 0;
820 }
821
822 static int __insert_uiapplication_appcategory_info(manifest_x *mfx)
823 {
824         uiapplication_x *up = mfx->uiapplication;
825         category_x *ct = NULL;
826         int ret = -1;
827         char query[MAX_QUERY_LEN] = {'\0'};
828         while(up != NULL)
829         {
830                 ct = up->category;
831                 while (ct != NULL)
832                 {
833                         snprintf(query, MAX_QUERY_LEN,
834                                 "insert into package_app_app_category(app_id, category) " \
835                                 "values('%s','%s')",\
836                                  up->appid, ct->name);
837                         ret = __exec_query(query);
838                         if (ret == -1) {
839                                 DBG("Package UiApp Category Info DB Insert Failed\n");
840                                 return -1;
841                         }
842                         ct = ct->next;
843                         memset(query, '\0', MAX_QUERY_LEN);
844                 }
845                 up = up->next;
846         }
847         return 0;
848 }
849
850 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx)
851 {
852         uiapplication_x *up = mfx->uiapplication;
853         metadata_x *md = NULL;
854         int ret = -1;
855         char query[MAX_QUERY_LEN] = {'\0'};
856         while(up != NULL)
857         {
858                 md = up->metadata;
859                 while (md != NULL)
860                 {
861                         if (md->key) {
862                                 snprintf(query, MAX_QUERY_LEN,
863                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
864                                         "values('%s','%s', '%s')",\
865                                          up->appid, md->key, md->value);
866                                 ret = __exec_query(query);
867                                 if (ret == -1) {
868                                         DBG("Package UiApp Metadata Info DB Insert Failed\n");
869                                         return -1;
870                                 }
871                         }
872                         md = md->next;
873                         memset(query, '\0', MAX_QUERY_LEN);
874                 }
875                 up = up->next;
876         }
877         return 0;
878 }
879
880 static int __insert_uiapplication_apppermission_info(manifest_x *mfx)
881 {
882         uiapplication_x *up = mfx->uiapplication;
883         permission_x *pm = NULL;
884         int ret = -1;
885         char query[MAX_QUERY_LEN] = {'\0'};
886         while(up != NULL)
887         {
888                 pm = up->permission;
889                 while (pm != NULL)
890                 {
891                         snprintf(query, MAX_QUERY_LEN,
892                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
893                                 "values('%s','%s', '%s')",\
894                                  up->appid, pm->type, pm->value);
895                         ret = __exec_query(query);
896                         if (ret == -1) {
897                                 DBG("Package UiApp permission Info DB Insert Failed\n");
898                                 return -1;
899                         }
900                         pm = pm->next;
901                         memset(query, '\0', MAX_QUERY_LEN);
902                 }
903                 up = up->next;
904         }
905         return 0;
906 }
907
908 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx)
909 {
910         uiapplication_x *up = mfx->uiapplication;
911         appcontrol_x *acontrol = NULL;
912         operation_x *op = NULL;
913         mime_x *mi = NULL;
914         uri_x *ui = NULL;
915         subapp_x *sub = NULL;
916         int ret = -1;
917         char query[MAX_QUERY_LEN] = {'\0'};
918         const char *operation = NULL;
919         const char *mime = NULL;
920         const char *uri = NULL;
921         const char *subapp = NULL;
922         while(up != NULL)
923         {
924                 acontrol = up->appcontrol;
925                 while(acontrol != NULL)
926                 {
927                         op = acontrol->operation;
928                         while(op != NULL)
929                         {
930                                 if (op)
931                                         operation = op->name;
932                                 mi = acontrol->mime;
933
934                                 do
935                                 {
936                                         if (mi)
937                                                 mime = mi->name;
938                                         sub = acontrol->subapp;
939                                         do
940                                         {
941                                                 if (sub)
942                                                         subapp = sub->name;
943                                                 ui = acontrol->uri;
944                                                 do
945                                                 {
946                                                         if (ui)
947                                                                 uri = ui->name;
948                                                         snprintf(query, MAX_QUERY_LEN,
949                                                                  "insert into package_app_app_control(app_id, operation, uri_scheme, mime_type, subapp_name) " \
950                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
951                                                                  up->appid, operation, uri, mime, subapp);
952
953                                                         ret = __exec_query(query);
954                                                         if (ret == -1) {
955                                                                 DBG("Package UiApp AppSvc DB Insert Failed\n");
956                                                                 return -1;
957                                                         }
958                                                         memset(query, '\0', MAX_QUERY_LEN);
959                                                         if (ui)
960                                                                 ui = ui->next;
961                                                         uri = NULL;
962                                                 } while(ui != NULL);
963                                                 if (sub)
964                                                         sub = sub->next;
965                                                 subapp = NULL;
966                                         }while(sub != NULL);
967                                         if (mi)
968                                                 mi = mi->next;
969                                         mime = NULL;
970                                 }while(mi != NULL);
971                                 if (op)
972                                         op = op->next;
973                                 operation = NULL;
974                         }
975                         acontrol = acontrol->next;
976                 }
977                 up = up->next;
978         }
979         return 0;
980 }
981
982 static int __insert_uiapplication_appsvc_info(manifest_x *mfx)
983 {
984         uiapplication_x *up = mfx->uiapplication;
985         appsvc_x *asvc = NULL;
986         operation_x *op = NULL;
987         mime_x *mi = NULL;
988         uri_x *ui = NULL;
989         subapp_x *sub = NULL;
990         int ret = -1;
991         char query[MAX_QUERY_LEN] = {'\0'};
992         const char *operation = NULL;
993         const char *mime = NULL;
994         const char *uri = NULL;
995         const char *subapp = NULL;
996         while(up != NULL)
997         {
998                 asvc = up->appsvc;
999                 while(asvc != NULL)
1000                 {
1001                         op = asvc->operation;
1002                         while(op != NULL)
1003                         {
1004                                 if (op)
1005                                         operation = op->name;
1006                                 mi = asvc->mime;
1007
1008                                 do
1009                                 {
1010                                         if (mi)
1011                                                 mime = mi->name;
1012                                         sub = asvc->subapp;
1013                                         do
1014                                         {
1015                                                 if (sub)
1016                                                         subapp = sub->name;
1017                                                 ui = asvc->uri;
1018                                                 do
1019                                                 {
1020                                                         if (ui)
1021                                                                 uri = ui->name;
1022                                                         snprintf(query, MAX_QUERY_LEN,
1023                                                                  "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1024                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
1025                                                                  up->appid, operation, uri, mime, subapp);
1026
1027                                                         ret = __exec_query(query);
1028                                                         if (ret == -1) {
1029                                                                 DBG("Package UiApp AppSvc DB Insert Failed\n");
1030                                                                 return -1;
1031                                                         }
1032                                                         memset(query, '\0', MAX_QUERY_LEN);
1033                                                         if (ui)
1034                                                                 ui = ui->next;
1035                                                         uri = NULL;
1036                                                 } while(ui != NULL);
1037                                                 if (sub)
1038                                                         sub = sub->next;
1039                                                 subapp = NULL;
1040                                         }while(sub != NULL);
1041                                         if (mi)
1042                                                 mi = mi->next;
1043                                         mime = NULL;
1044                                 }while(mi != NULL);
1045                                 if (op)
1046                                         op = op->next;
1047                                 operation = NULL;
1048                         }
1049                         asvc = asvc->next;
1050                 }
1051                 up = up->next;
1052         }
1053         return 0;
1054 }
1055
1056 static int __insert_uiapplication_share_request_info(manifest_x *mfx)
1057 {
1058         uiapplication_x *up = mfx->uiapplication;
1059         datashare_x *ds = NULL;
1060         request_x *rq = NULL;
1061         int ret = -1;
1062         char query[MAX_QUERY_LEN] = {'\0'};
1063         while(up != NULL)
1064         {
1065                 ds = up->datashare;
1066                 while(ds != NULL)
1067                 {
1068                         rq = ds->request;
1069                         while(rq != NULL)
1070                         {
1071                                 snprintf(query, MAX_QUERY_LEN,
1072                                          "insert into package_app_share_request(app_id, data_share_request) " \
1073                                         "values('%s', '%s')",\
1074                                          up->appid, rq->text);
1075                                 ret = __exec_query(query);
1076                                 if (ret == -1) {
1077                                         DBG("Package UiApp Share Request DB Insert Failed\n");
1078                                         return -1;
1079                                 }
1080                                 memset(query, '\0', MAX_QUERY_LEN);
1081                                 rq = rq->next;
1082                         }
1083                         ds = ds->next;
1084                 }
1085                 up = up->next;
1086         }
1087         return 0;
1088 }
1089
1090 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx)
1091 {
1092         uiapplication_x *up = mfx->uiapplication;
1093         datashare_x *ds = NULL;
1094         define_x *df = NULL;
1095         allowed_x *al = NULL;
1096         int ret = -1;
1097         char query[MAX_QUERY_LEN] = {'\0'};
1098         while(up != NULL)
1099         {
1100                 ds = up->datashare;
1101                 while(ds != NULL)
1102                 {
1103                         df = ds->define;
1104                         while(df != NULL)
1105                         {
1106                                 al = df->allowed;
1107                                 while(al != NULL)
1108                                 {
1109                                         snprintf(query, MAX_QUERY_LEN,
1110                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1111                                                 "values('%s', '%s', '%s')",\
1112                                                  up->appid, df->path, al->text);
1113                                         ret = __exec_query(query);
1114                                         if (ret == -1) {
1115                                                 DBG("Package UiApp Share Allowed DB Insert Failed\n");
1116                                                 return -1;
1117                                         }
1118                                         memset(query, '\0', MAX_QUERY_LEN);
1119                                         al = al->next;
1120                                 }
1121                                 df = df->next;
1122                         }
1123                         ds = ds->next;
1124                 }
1125                 up = up->next;
1126         }
1127         return 0;
1128 }
1129
1130 static int __insert_serviceapplication_info(manifest_x *mfx)
1131 {
1132         serviceapplication_x *sp = mfx->serviceapplication;
1133         int ret = -1;
1134         char query[MAX_QUERY_LEN] = {'\0'};
1135         while(sp != NULL)
1136         {
1137                 snprintf(query, MAX_QUERY_LEN,
1138                          "insert into package_app_info(app_id, app_component, app_exec, app_type, app_onboot, " \
1139                         "app_multiple, app_autorestart, app_enabled, app_permissiontype, package) " \
1140                         "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1141                          sp->appid, "svcapp", sp->exec, sp->type, sp->onboot, "\0",
1142                          sp->autorestart, sp->enabled, sp->permission_type, mfx->package);
1143                 ret = __exec_query(query);
1144                 if (ret == -1) {
1145                         DBG("Package ServiceApp Info DB Insert Failed\n");
1146                         return -1;
1147                 }
1148                 sp = sp->next;
1149                 memset(query, '\0', MAX_QUERY_LEN);
1150         }
1151         return 0;
1152 }
1153
1154 static int __insert_serviceapplication_appcategory_info(manifest_x *mfx)
1155 {
1156         serviceapplication_x *sp = mfx->serviceapplication;
1157         category_x *ct = NULL;
1158         int ret = -1;
1159         char query[MAX_QUERY_LEN] = {'\0'};
1160         while(sp != NULL)
1161         {
1162                 ct = sp->category;
1163                 while (ct != NULL)
1164                 {
1165                         snprintf(query, MAX_QUERY_LEN,
1166                                 "insert into package_app_app_category(app_id, category) " \
1167                                 "values('%s','%s')",\
1168                                  sp->appid, ct->name);
1169                         ret = __exec_query(query);
1170                         if (ret == -1) {
1171                                 DBG("Package ServiceApp Category Info DB Insert Failed\n");
1172                                 return -1;
1173                         }
1174                         ct = ct->next;
1175                         memset(query, '\0', MAX_QUERY_LEN);
1176                 }
1177                 sp = sp->next;
1178         }
1179         return 0;
1180 }
1181
1182 static int __insert_serviceapplication_appmetadata_info(manifest_x *mfx)
1183 {
1184         serviceapplication_x *sp = mfx->serviceapplication;
1185         metadata_x *md = NULL;
1186         int ret = -1;
1187         char query[MAX_QUERY_LEN] = {'\0'};
1188         while(sp != NULL)
1189         {
1190                 md = sp->metadata;
1191                 while (md != NULL)
1192                 {
1193                         if (md->key) {
1194                                 snprintf(query, MAX_QUERY_LEN,
1195                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
1196                                         "values('%s','%s', '%s')",\
1197                                          sp->appid, md->key, md->value);
1198                                 ret = __exec_query(query);
1199                                 if (ret == -1) {
1200                                         DBG("Package ServiceApp Metadata Info DB Insert Failed\n");
1201                                         return -1;
1202                                 }
1203                         }
1204                         md = md->next;
1205                         memset(query, '\0', MAX_QUERY_LEN);
1206                 }
1207                 sp = sp->next;
1208         }
1209         return 0;
1210 }
1211
1212 static int __insert_serviceapplication_apppermission_info(manifest_x *mfx)
1213 {
1214         serviceapplication_x *sp = mfx->serviceapplication;
1215         permission_x *pm = NULL;
1216         int ret = -1;
1217         char query[MAX_QUERY_LEN] = {'\0'};
1218         while(sp != NULL)
1219         {
1220                 pm = sp->permission;
1221                 while (pm != NULL)
1222                 {
1223                         snprintf(query, MAX_QUERY_LEN,
1224                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
1225                                 "values('%s','%s', '%s')",\
1226                                  sp->appid, pm->type, pm->value);
1227                         ret = __exec_query(query);
1228                         if (ret == -1) {
1229                                 DBG("Package ServiceApp permission Info DB Insert Failed\n");
1230                                 return -1;
1231                         }
1232                         pm = pm->next;
1233                         memset(query, '\0', MAX_QUERY_LEN);
1234                 }
1235                 sp = sp->next;
1236         }
1237         return 0;
1238 }
1239
1240 static int __insert_serviceapplication_appcontrol_info(manifest_x *mfx)
1241 {
1242         serviceapplication_x *sp = mfx->serviceapplication;
1243         appcontrol_x *acontrol = NULL;
1244         int ret = -1;
1245         char query[MAX_QUERY_LEN] = {'\0'};
1246         operation_x *op = NULL;
1247         mime_x *mi = NULL;
1248         uri_x *ui = NULL;
1249         subapp_x *sub = NULL;
1250         const char *operation = NULL;
1251         const char *mime = NULL;
1252         const char *uri = NULL;
1253         const char *subapp = NULL;
1254         while(sp != NULL)
1255         {
1256                 acontrol = sp->appcontrol;
1257                 while(acontrol != NULL)
1258                 {
1259                         op = acontrol->operation;
1260                         while(op != NULL)
1261                         {
1262                         if (op)
1263                                 operation = op->name;
1264                         mi = acontrol->mime;
1265                                 do
1266                                 {
1267                                 if (mi)
1268                                         mime = mi->name;
1269                                 sub = acontrol->subapp;
1270                                         do
1271                                         {
1272                                         if (sub)
1273                                                 subapp = sub->name;
1274                                         ui = acontrol->uri;
1275                                                 do
1276                                                 {
1277                                                         if (ui)
1278                                                                 uri = ui->name;
1279                                                         snprintf(query, MAX_QUERY_LEN,
1280                                                                  "insert into package_app_app_control(app_id, operation, uri_scheme, mime_type,subapp_name) " \
1281                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
1282                                                                  sp->appid, operation, uri, mime, subapp);
1283                                                         ret = __exec_query(query);
1284                                                         if (ret == -1) {
1285                                                                 DBG("Package UiApp AppSvc DB Insert Failed\n");
1286                                                                 return -1;
1287                                                         }
1288                                                         memset(query, '\0', MAX_QUERY_LEN);
1289                                                         if (ui)
1290                                                                 ui = ui->next;
1291                                                         uri = NULL;
1292                                                 } while(ui != NULL);
1293                                                 if (sub)
1294                                                         sub = sub->next;
1295                                                 subapp = NULL;
1296                                                 }while(sub != NULL);
1297                                         if (mi)
1298                                                 mi = mi->next;
1299                                         mime = NULL;
1300                                 }while(mi != NULL);
1301                                 if (op)
1302                                         op = op->next;
1303                                 operation = NULL;
1304                         }
1305                         acontrol = acontrol->next;
1306                 }
1307                 sp = sp->next;
1308         }
1309         return 0;
1310 }
1311
1312 static int __insert_serviceapplication_appsvc_info(manifest_x *mfx)
1313 {
1314         serviceapplication_x *sp = mfx->serviceapplication;
1315         appsvc_x *asvc = NULL;
1316         int ret = -1;
1317         char query[MAX_QUERY_LEN] = {'\0'};
1318         operation_x *op = NULL;
1319         mime_x *mi = NULL;
1320         uri_x *ui = NULL;
1321         subapp_x *sub = NULL;
1322         const char *operation = NULL;
1323         const char *mime = NULL;
1324         const char *uri = NULL;
1325         const char *subapp = NULL;
1326         while(sp != NULL)
1327         {
1328                 asvc = sp->appsvc;
1329                 while(asvc != NULL)
1330                 {
1331                         op = asvc->operation;
1332                         while(op != NULL)
1333                         {
1334                         if (op)
1335                                 operation = op->name;
1336                         mi = asvc->mime;
1337                                 do
1338                                 {
1339                                 if (mi)
1340                                         mime = mi->name;
1341                                 sub = asvc->subapp;
1342                                         do
1343                                         {
1344                                         if (sub)
1345                                                 subapp = sub->name;
1346                                         ui = asvc->uri;
1347                                                         do
1348                                                         {
1349                                                                 if (ui)
1350                                                                         uri = ui->name;
1351                                                                 snprintf(query, MAX_QUERY_LEN,
1352                                                                          "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1353                                                                         "values('%s', '%s', '%s', '%s', '%s')",\
1354                                                                          sp->appid, operation, uri, mime, subapp);
1355                                                                 ret = __exec_query(query);
1356                                                                 if (ret == -1) {
1357                                                                         DBG("Package UiApp AppSvc DB Insert Failed\n");
1358                                                                         return -1;
1359                                                                 }
1360                                                                 memset(query, '\0', MAX_QUERY_LEN);
1361                                                                 if (ui)
1362                                                                         ui = ui->next;
1363                                                                 uri = NULL;
1364                                                         } while(ui != NULL);
1365                                                 if (sub)
1366                                                         sub     = sub->next;
1367                                                 subapp = NULL;
1368                                         }while(sub != NULL);
1369                                         if (mi)
1370                                                 mi = mi->next;
1371                                         mime = NULL;
1372                                 }while(mi != NULL);
1373                                 if (op)
1374                                         op = op->next;
1375                                 operation = NULL;
1376                         }
1377                         asvc = asvc->next;
1378                 }
1379                 sp = sp->next;
1380         }
1381         return 0;
1382 }
1383
1384
1385
1386 static int __insert_serviceapplication_share_request_info(manifest_x *mfx)
1387 {
1388         serviceapplication_x *sp = mfx->serviceapplication;
1389         datashare_x *ds = NULL;
1390         request_x *rq = NULL;
1391         int ret = -1;
1392         char query[MAX_QUERY_LEN] = {'\0'};
1393         while(sp != NULL)
1394         {
1395                 ds = sp->datashare;
1396                 while(ds != NULL)
1397                 {
1398                         rq = ds->request;
1399                         while(rq != NULL)
1400                         {
1401                                 snprintf(query, MAX_QUERY_LEN,
1402                                          "insert into package_app_share_request(app_id, data_share_request) " \
1403                                         "values('%s', '%s')",\
1404                                          sp->appid, rq->text);
1405                                 ret = __exec_query(query);
1406                                 if (ret == -1) {
1407                                         DBG("Package ServiceApp Share Request DB Insert Failed\n");
1408                                         return -1;
1409                                 }
1410                                 memset(query, '\0', MAX_QUERY_LEN);
1411                                 rq = rq->next;
1412                         }
1413                         ds = ds->next;
1414                 }
1415                 sp = sp->next;
1416         }
1417         return 0;
1418 }
1419
1420
1421
1422 static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx)
1423 {
1424         serviceapplication_x *sp = mfx->serviceapplication;
1425         datashare_x *ds = NULL;
1426         define_x *df = NULL;
1427         allowed_x *al = NULL;
1428         int ret = -1;
1429         char query[MAX_QUERY_LEN] = {'\0'};
1430         while(sp != NULL)
1431         {
1432                 ds = sp->datashare;
1433                 while(ds != NULL)
1434                 {
1435                         df = ds->define;
1436                         while(df != NULL)
1437                         {
1438                                 al = df->allowed;
1439                                 while(al != NULL)
1440                                 {
1441                                         snprintf(query, MAX_QUERY_LEN,
1442                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1443                                                 "values('%s', '%s', '%s')",\
1444                                                  sp->appid, df->path, al->text);
1445                                         ret = __exec_query(query);
1446                                         if (ret == -1) {
1447                                                 DBG("Package App Share Allowed DB Insert Failed\n");
1448                                                 return -1;
1449                                         }
1450                                         memset(query, '\0', MAX_QUERY_LEN);
1451                                         al = al->next;
1452                                 }
1453                                 df = df->next;
1454                         }
1455                         ds = ds->next;
1456                 }
1457                 sp = sp->next;
1458         }
1459         return 0;
1460 }
1461
1462 static int __insert_manifest_info_in_db(manifest_x *mfx)
1463 {
1464         label_x *lbl = mfx->label;
1465         license_x *lcn = mfx->license;
1466         icon_x *icn = mfx->icon;
1467         description_x *dcn = mfx->description;
1468         author_x *ath = mfx->author;
1469         uiapplication_x *up = mfx->uiapplication;
1470         uiapplication_x *up_icn = mfx->uiapplication;
1471         uiapplication_x *up_image = mfx->uiapplication;
1472         serviceapplication_x *sp = mfx->serviceapplication;
1473         privileges_x *pvs = NULL;
1474         privilege_x *pv = NULL;
1475         char query[MAX_QUERY_LEN] = { '\0' };
1476         char root[MAX_QUERY_LEN] = { '\0' };
1477         int ret = -1;
1478         char *type = NULL;
1479         char *path = NULL;
1480         const char *auth_name = NULL;
1481         const char *auth_email = NULL;
1482         const char *auth_href = NULL;
1483         if (ath) {
1484                 if (ath->text)
1485                         auth_name = ath->text;
1486                 if (ath->email)
1487                         auth_email = ath->email;
1488                 if (ath->href)
1489                         auth_href = ath->href;
1490         }
1491
1492         /*Insert in the package_info DB*/
1493         if (mfx->type)
1494                 type = strdup(mfx->type);
1495         else
1496                 type = strdup("rpm");
1497         /*Insert in the package_info DB*/
1498         if (mfx->root_path)
1499                 path = strdup(mfx->root_path);
1500         else{
1501                 if (strcmp(type,"rpm")==0)
1502                         snprintf(root, MAX_QUERY_LEN - 1, "/usr/apps/%s", mfx->package);
1503                 else
1504                         snprintf(root, MAX_QUERY_LEN - 1, "/opt/usr/apps/%s", mfx->package);
1505
1506                 path = strdup(root);
1507         }
1508         snprintf(query, MAX_QUERY_LEN,
1509                  "insert into package_info(package, package_type, package_version, install_location, package_size, " \
1510                 "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) " \
1511                 "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1512                  mfx->package, type, mfx->version, mfx->installlocation, mfx->package_size, mfx->removable, mfx->preload,
1513                  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);
1514         ret = __exec_query(query);
1515         if (ret == -1) {
1516                 DBG("Package Info DB Insert Failed\n");
1517                 if (type) {
1518                         free(type);
1519                         type = NULL;
1520                 }
1521                 if (path) {
1522                         free(path);
1523                         path = NULL;
1524                 }
1525                 return -1;
1526         }
1527         if (type) {
1528                 free(type);
1529                 type = NULL;
1530         }
1531         if (path) {
1532                 free(path);
1533                 path = NULL;
1534         }
1535
1536         /*Insert in the package_privilege_info DB*/
1537         pvs = mfx->privileges;
1538         while (pvs != NULL) {
1539                 pv = pvs->privilege;
1540                 while (pv != NULL) {
1541                         memset(query, '\0', MAX_QUERY_LEN);
1542                         snprintf(query, MAX_QUERY_LEN,
1543                                 "insert into package_privilege_info(package, privilege) " \
1544                                 "values('%s','%s')",\
1545                                  mfx->package, pv->text);
1546                         ret = __exec_query(query);
1547                         if (ret == -1) {
1548                                 DBG("Package Privilege Info DB Insert Failed\n");
1549                                 return -1;
1550                         }
1551                         pv = pv->next;
1552                 }
1553                 pvs = pvs->next;
1554         }
1555
1556         ret = __insert_ui_mainapp_info(mfx);
1557         if (ret == -1)
1558                 return -1;
1559
1560         /*Insert the package locale and app locale info */
1561         pkglocale = __create_locale_list(pkglocale, lbl, lcn, icn, dcn, ath);
1562         g_list_foreach(pkglocale, __trimfunc1, NULL);
1563         prev = NULL;
1564
1565         while(up != NULL)
1566         {
1567                 applocale = __create_locale_list(applocale, up->label, NULL, up->icon, NULL, NULL);
1568                 up = up->next;
1569         }
1570         while(sp != NULL)
1571         {
1572                 applocale = __create_locale_list(applocale, sp->label, NULL, sp->icon, NULL, NULL);
1573                 sp = sp->next;
1574         }
1575         g_list_foreach(applocale, __trimfunc2, NULL);
1576         prev = NULL;
1577
1578         /*Insert the app icon info */
1579         while(up_icn != NULL)
1580         {
1581                 appicon = __create_icon_list(appicon, up_icn->icon);
1582                 up_icn = up_icn->next;
1583         }
1584         g_list_foreach(appicon, __trimfunc3, NULL);
1585         prev = NULL;
1586
1587         /*Insert the image info */
1588         while(up_image != NULL)
1589         {
1590                 appimage = __create_image_list(appimage, up_image->image);
1591                 up_image = up_image->next;
1592         }
1593         g_list_foreach(appimage, __trimfunc4, NULL);
1594         prev = NULL;
1595
1596         /*g_list_foreach(pkglocale, __printfunc, NULL);*/
1597         /*DBG("\n");*/
1598         /*g_list_foreach(applocale, __printfunc, NULL);*/
1599
1600         /*package locale info*/
1601         g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1602         /*native app locale info*/
1603         up = mfx->uiapplication;
1604         while(up != NULL)
1605         {
1606                 g_list_foreach(applocale, __insert_uiapplication_locale_info, (gpointer)up);
1607                 up = up->next;
1608         }
1609         /*agent app locale info*/
1610         sp = mfx->serviceapplication;
1611         while(sp != NULL)
1612         {
1613                 g_list_foreach(applocale, __insert_serviceapplication_locale_info, (gpointer)sp);
1614                 sp = sp->next;
1615         }
1616
1617         /*app icon locale info*/
1618         up_icn = mfx->uiapplication;
1619         while(up_icn != NULL)
1620         {
1621                 g_list_foreach(appicon, __insert_uiapplication_icon_section_info, (gpointer)up_icn);
1622                 up_icn = up_icn->next;
1623         }
1624
1625         /*app image info*/
1626         up_image = mfx->uiapplication;
1627         while(up_image != NULL)
1628         {
1629                 g_list_foreach(appimage, __insert_uiapplication_image_info, (gpointer)up_image);
1630                 up_image = up_image->next;
1631         }
1632
1633         g_list_free(pkglocale);
1634         pkglocale = NULL;
1635         g_list_free(applocale);
1636         applocale = NULL;
1637         g_list_free(appicon);
1638         appicon = NULL;
1639         g_list_free(appimage);
1640         appimage = NULL;
1641
1642
1643         /*Insert in the package_app_info DB*/
1644         ret = __insert_uiapplication_info(mfx);
1645         if (ret == -1)
1646                 return -1;
1647         ret = __insert_serviceapplication_info(mfx);
1648         if (ret == -1)
1649                 return -1;
1650
1651         /*Insert in the package_app_app_control DB*/
1652         ret = __insert_uiapplication_appcontrol_info(mfx);
1653         if (ret == -1)
1654                 return -1;
1655         ret = __insert_serviceapplication_appcontrol_info(mfx);
1656         if (ret == -1)
1657                 return -1;
1658
1659         /*Insert in the package_app_app_category DB*/
1660         ret = __insert_uiapplication_appcategory_info(mfx);
1661         if (ret == -1)
1662                 return -1;
1663         ret = __insert_serviceapplication_appcategory_info(mfx);
1664         if (ret == -1)
1665                 return -1;
1666
1667         /*Insert in the package_app_app_metadata DB*/
1668         ret = __insert_uiapplication_appmetadata_info(mfx);
1669         if (ret == -1)
1670                 return -1;
1671         ret = __insert_serviceapplication_appmetadata_info(mfx);
1672         if (ret == -1)
1673                 return -1;
1674
1675         /*Insert in the package_app_app_permission DB*/
1676         ret = __insert_uiapplication_apppermission_info(mfx);
1677         if (ret == -1)
1678                 return -1;
1679         ret = __insert_serviceapplication_apppermission_info(mfx);
1680         if (ret == -1)
1681                 return -1;
1682
1683         /*Insert in the package_app_app_svc DB*/
1684         ret = __insert_uiapplication_appsvc_info(mfx);
1685         if (ret == -1)
1686                 return -1;
1687         ret = __insert_serviceapplication_appsvc_info(mfx);
1688         if (ret == -1)
1689                 return -1;
1690
1691         /*Insert in the package_app_share_allowed DB*/
1692         ret = __insert_uiapplication_share_allowed_info(mfx);
1693         if (ret == -1)
1694                 return -1;
1695         ret = __insert_serviceapplication_share_allowed_info(mfx);
1696         if (ret == -1)
1697                 return -1;
1698
1699         /*Insert in the package_app_share_request DB*/
1700         ret = __insert_uiapplication_share_request_info(mfx);
1701         if (ret == -1)
1702                 return -1;
1703         ret = __insert_serviceapplication_share_request_info(mfx);
1704         if (ret == -1)
1705                 return -1;
1706
1707         return 0;
1708
1709 }
1710
1711 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1712 {
1713         char query[MAX_QUERY_LEN] = { '\0' };
1714         int ret = -1;
1715         memset(query, '\0', MAX_QUERY_LEN);
1716         snprintf(query, MAX_QUERY_LEN,
1717                  "delete from %s where app_id='%s'", db_table, appid);
1718         ret = __exec_query(query);
1719         if (ret == -1) {
1720                 DBG("DB Deletion from table (%s) Failed\n", db_table);
1721                 return -1;
1722         }
1723         return 0;
1724 }
1725
1726 static int __delete_manifest_info_from_db(manifest_x *mfx)
1727 {
1728         char query[MAX_QUERY_LEN] = { '\0' };
1729         int ret = -1;
1730         uiapplication_x *up = mfx->uiapplication;
1731         serviceapplication_x *sp = mfx->serviceapplication;
1732         /*Delete from cert table*/
1733         ret = pkgmgrinfo_delete_certinfo(mfx->package);
1734         if (ret) {
1735                 DBG("Cert Info  DB Delete Failed\n");
1736                 return -1;
1737         }
1738
1739         /*Delete from Package Info DB*/
1740         snprintf(query, MAX_QUERY_LEN,
1741                  "delete from package_info where package='%s'", mfx->package);
1742         ret = __exec_query(query);
1743         if (ret == -1) {
1744                 DBG("Package Info DB Delete Failed\n");
1745                 return -1;
1746         }
1747         memset(query, '\0', MAX_QUERY_LEN);
1748
1749         /*Delete from Package Localized Info*/
1750         snprintf(query, MAX_QUERY_LEN,
1751                  "delete from package_localized_info where package='%s'", mfx->package);
1752         ret = __exec_query(query);
1753         if (ret == -1) {
1754                 DBG("Package Localized Info DB Delete Failed\n");
1755                 return -1;
1756         }
1757
1758         /*Delete from Package Privilege Info*/
1759         snprintf(query, MAX_QUERY_LEN,
1760                  "delete from package_privilege_info where package='%s'", mfx->package);
1761         ret = __exec_query(query);
1762         if (ret == -1) {
1763                 DBG("Package Privilege Info DB Delete Failed\n");
1764                 return -1;
1765         }
1766
1767         while (up != NULL) {
1768                 ret = __delete_appinfo_from_db("package_app_info", up->appid);
1769                 if (ret < 0)
1770                         return ret;
1771                 ret = __delete_appinfo_from_db("package_app_localized_info", up->appid);
1772                 if (ret < 0)
1773                         return ret;
1774                 ret = __delete_appinfo_from_db("package_app_icon_section_info", up->appid);
1775                 if (ret < 0)
1776                         return ret;
1777                 ret = __delete_appinfo_from_db("package_app_image_info", up->appid);
1778                 if (ret < 0)
1779                         return ret;
1780                 ret = __delete_appinfo_from_db("package_app_app_svc", up->appid);
1781                 if (ret < 0)
1782                         return ret;
1783                 ret = __delete_appinfo_from_db("package_app_app_control", up->appid);
1784                 if (ret < 0)
1785                         return ret;
1786                 ret = __delete_appinfo_from_db("package_app_app_category", up->appid);
1787                 if (ret < 0)
1788                         return ret;
1789                 ret = __delete_appinfo_from_db("package_app_app_metadata", up->appid);
1790                 if (ret < 0)
1791                         return ret;
1792                 ret = __delete_appinfo_from_db("package_app_app_permission", up->appid);
1793                 if (ret < 0)
1794                         return ret;
1795                 ret = __delete_appinfo_from_db("package_app_share_allowed", up->appid);
1796                 if (ret < 0)
1797                         return ret;
1798                 ret = __delete_appinfo_from_db("package_app_share_request", up->appid);
1799                 if (ret < 0)
1800                         return ret;
1801                 up = up->next;
1802         }
1803
1804         while (sp != NULL) {
1805                 ret = __delete_appinfo_from_db("package_app_info", sp->appid);
1806                 if (ret < 0)
1807                         return ret;
1808                 ret = __delete_appinfo_from_db("package_app_localized_info", sp->appid);
1809                 if (ret < 0)
1810                         return ret;
1811                 ret = __delete_appinfo_from_db("package_app_icon_section_info", sp->appid);
1812                 if (ret < 0)
1813                         return ret;
1814                 ret = __delete_appinfo_from_db("package_app_image_info", sp->appid);
1815                 if (ret < 0)
1816                         return ret;
1817                 ret = __delete_appinfo_from_db("package_app_app_svc", sp->appid);
1818                 if (ret < 0)
1819                         return ret;
1820                 ret = __delete_appinfo_from_db("package_app_app_control", sp->appid);
1821                 if (ret < 0)
1822                         return ret;
1823                 ret = __delete_appinfo_from_db("package_app_app_category", sp->appid);
1824                 if (ret < 0)
1825                         return ret;
1826                 ret = __delete_appinfo_from_db("package_app_app_metadata", sp->appid);
1827                 if (ret < 0)
1828                         return ret;
1829                 ret = __delete_appinfo_from_db("package_app_app_permission", sp->appid);
1830                 if (ret < 0)
1831                         return ret;
1832                 ret = __delete_appinfo_from_db("package_app_share_allowed", sp->appid);
1833                 if (ret < 0)
1834                         return ret;
1835                 ret = __delete_appinfo_from_db("package_app_share_request", sp->appid);
1836                 if (ret < 0)
1837                         return ret;
1838                 sp = sp->next;
1839         }
1840         return 0;
1841 }
1842
1843
1844 int pkgmgr_parser_initialize_db()
1845 {
1846         int ret = -1;
1847         /*Manifest DB*/
1848         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1849         if (ret == -1) {
1850                 DBG("package info DB initialization failed\n");
1851                 return ret;
1852         }
1853         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
1854         if (ret == -1) {
1855                 DBG("package localized info DB initialization failed\n");
1856                 return ret;
1857         }
1858         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
1859         if (ret == -1) {
1860                 DBG("package app app privilege DB initialization failed\n");
1861                 return ret;
1862         }
1863         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
1864         if (ret == -1) {
1865                 DBG("package app info DB initialization failed\n");
1866                 return ret;
1867         }
1868         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
1869         if (ret == -1) {
1870                 DBG("package app localized info DB initialization failed\n");
1871                 return ret;
1872         }
1873         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
1874         if (ret == -1) {
1875                 DBG("package app icon localized info DB initialization failed\n");
1876                 return ret;
1877         }
1878         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
1879         if (ret == -1) {
1880                 DBG("package app image info DB initialization failed\n");
1881                 return ret;
1882         }
1883         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
1884         if (ret == -1) {
1885                 DBG("package app app control DB initialization failed\n");
1886                 return ret;
1887         }
1888         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
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_METADATA);
1894         if (ret == -1) {
1895                 DBG("package app app category DB initialization failed\n");
1896                 return ret;
1897         }
1898         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
1899         if (ret == -1) {
1900                 DBG("package app app permission DB initialization failed\n");
1901                 return ret;
1902         }
1903         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC);
1904         if (ret == -1) {
1905                 DBG("package app app svc DB initialization failed\n");
1906                 return ret;
1907         }
1908         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
1909         if (ret == -1) {
1910                 DBG("package app share allowed DB initialization failed\n");
1911                 return ret;
1912         }
1913         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
1914         if (ret == -1) {
1915                 DBG("package app share request DB initialization failed\n");
1916                 return ret;
1917         }
1918         /*Cert DB*/
1919         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
1920         if (ret == -1) {
1921                 DBG("package cert info DB initialization failed\n");
1922                 return ret;
1923         }
1924         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
1925         if (ret == -1) {
1926                 DBG("package cert index info DB initialization failed\n");
1927                 return ret;
1928         }
1929         return 0;
1930 }
1931
1932 int pkgmgr_parser_check_and_create_db()
1933 {
1934         int ret = -1;
1935         /*Manifest DB*/
1936         ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, PKGMGR_PARSER_DB_FILE);
1937         if (ret) {
1938                 DBG("Manifest DB creation Failed\n");
1939                 return -1;
1940         }
1941         /*Cert DB*/
1942         ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, PKGMGR_CERT_DB_FILE);
1943         if (ret) {
1944                 DBG("Cert DB creation Failed\n");
1945                 return -1;
1946         }
1947         return 0;
1948 }
1949
1950 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1951 {
1952         if (mfx == NULL) {
1953                 DBG("manifest pointer is NULL\n");
1954                 return -1;
1955         }
1956         int ret = 0;
1957         ret = pkgmgr_parser_check_and_create_db();
1958         if (ret == -1) {
1959                 DBG("Failed to open DB\n");
1960                 return ret;
1961         }
1962         ret = pkgmgr_parser_initialize_db();
1963         if (ret == -1)
1964                 goto err;
1965         /*Begin transaction*/
1966         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1967         if (ret != SQLITE_OK) {
1968                 DBG("Failed to begin transaction\n");
1969                 ret = -1;
1970                 goto err;
1971         }
1972         DBG("Transaction Begin\n");
1973         ret = __insert_manifest_info_in_db(mfx);
1974         if (ret == -1) {
1975                 DBG("Insert into DB failed. Rollback now\n");
1976                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1977                 goto err;
1978         }
1979         /*Commit transaction*/
1980         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1981         if (ret != SQLITE_OK) {
1982                 DBG("Failed to commit transaction. Rollback now\n");
1983                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1984                 ret = -1;
1985                 goto err;
1986         }
1987         DBG("Transaction Commit and End\n");
1988 err:
1989         sqlite3_close(pkgmgr_parser_db);
1990         sqlite3_close(pkgmgr_cert_db);
1991         return ret;
1992 }
1993
1994 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1995 {
1996         if (mfx == NULL) {
1997                 DBG("manifest pointer is NULL\n");
1998                 return -1;
1999         }
2000         int ret = 0;
2001         ret = pkgmgr_parser_check_and_create_db();
2002         if (ret == -1) {
2003                 DBG("Failed to open DB\n");
2004                 return ret;
2005         }
2006         ret = pkgmgr_parser_initialize_db();
2007         if (ret == -1)
2008                 goto err;
2009         /*Preserve guest mode visibility*/
2010         __preserve_guestmode_visibility_value( mfx);
2011         /*Begin transaction*/
2012         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2013         if (ret != SQLITE_OK) {
2014                 DBG("Failed to begin transaction\n");
2015                 ret = -1;
2016                 goto err;
2017         }
2018         DBG("Transaction Begin\n");
2019         ret = __delete_manifest_info_from_db(mfx);
2020         if (ret == -1) {
2021                 DBG("Delete from DB failed. Rollback now\n");
2022                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2023                 goto err;
2024         }
2025         ret = __insert_manifest_info_in_db(mfx);
2026         if (ret == -1) {
2027                 DBG("Insert into DB failed. Rollback now\n");
2028                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2029                 goto err;
2030         }
2031
2032         /*Commit transaction*/
2033         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2034         if (ret != SQLITE_OK) {
2035                 DBG("Failed to commit transaction. Rollback now\n");
2036                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2037                 ret = -1;
2038                 goto err;
2039         }
2040         DBG("Transaction Commit and End\n");
2041 err:
2042         sqlite3_close(pkgmgr_parser_db);
2043         sqlite3_close(pkgmgr_cert_db);
2044         return ret;
2045 }
2046
2047 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
2048 {
2049         if (mfx == NULL) {
2050                 DBG("manifest pointer is NULL\n");
2051                 return -1;
2052         }
2053         int ret = 0;
2054         ret = pkgmgr_parser_check_and_create_db();
2055         if (ret == -1) {
2056                 DBG("Failed to open DB\n");
2057                 return ret;
2058         }
2059         /*Begin transaction*/
2060         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2061         if (ret != SQLITE_OK) {
2062                 DBG("Failed to begin transaction\n");
2063                 ret = -1;
2064                 goto err;
2065         }
2066         DBG("Transaction Begin\n");
2067         ret = __delete_manifest_info_from_db(mfx);
2068         if (ret == -1) {
2069                 DBG("Delete from DB failed. Rollback now\n");
2070                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2071                 goto err;
2072         }
2073         /*Commit transaction*/
2074         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2075         if (ret != SQLITE_OK) {
2076                 DBG("Failed to commit transaction, Rollback now\n");
2077                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2078                 ret = -1;
2079                 goto err;
2080         }
2081         DBG("Transaction Commit and End\n");
2082         sqlite3_close(pkgmgr_parser_db);
2083 err:
2084         sqlite3_close(pkgmgr_parser_db);
2085         sqlite3_close(pkgmgr_cert_db);
2086         return ret;
2087 }