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