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