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