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