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