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