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