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