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