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