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