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