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