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