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