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