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