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