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