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