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