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