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