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