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