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