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