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