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