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