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