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