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