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