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