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