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