tizen 2.3 release
[framework/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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <db-util.h>
30 #include <glib.h>
31 #include <dlfcn.h>
32
33 #include "pkgmgr_parser_internal.h"
34 #include "pkgmgr_parser_db.h"
35
36 #include "pkgmgrinfo_debug.h"
37 #include "pkgmgrinfo_type.h"
38
39 #ifdef LOG_TAG
40 #undef LOG_TAG
41 #endif
42 #define LOG_TAG "PKGMGR_PARSER"
43
44 #define MAX_QUERY_LEN           4096
45 #define MAX_PACKAGE_STR_SIZE 512
46 #define MAX_ALIAS_INI_ENTRY     (2 * MAX_PACKAGE_STR_SIZE) + 1
47 #define USR_APPSVC_ALIAS_INI "/usr/share/appsvc/alias.ini"
48 #define OPT_APPSVC_ALIAS_INI "/opt/usr/share/appsvc/alias.ini"
49 #define MANIFEST_DB "/opt/dbspace/.pkgmgr_parser.db"
50 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
51
52 sqlite3 *pkgmgr_parser_db;
53 sqlite3 *pkgmgr_cert_db;
54
55
56 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
57                                                 "(package text primary key not null, " \
58                                                 "package_type text DEFAULT 'rpm', " \
59                                                 "package_version text, " \
60                                                 "install_location text, " \
61                                                 "package_size text, " \
62                                                 "package_removable text DEFAULT 'true', " \
63                                                 "package_preload text DEFAULT 'false', " \
64                                                 "package_readonly text DEFAULT 'false', " \
65                                                 "package_update text DEFAULT 'false', " \
66                                                 "package_appsetting text DEFAULT 'false', " \
67                                                 "package_nodisplay text DEFAULT 'false', " \
68                                                 "package_system text DEFAULT 'false', " \
69                                                 "author_name text, " \
70                                                 "author_email text, " \
71                                                 "author_href text," \
72                                                 "installed_time text," \
73                                                 "installed_storage text," \
74                                                 "storeclient_id text," \
75                                                 "mainapp_id text," \
76                                                 "package_url text," \
77                                                 "root_path text," \
78                                                 "csc_path text," \
79                                                 "package_support_disable text DEFAULT 'false', " \
80                                                 "package_mother_package text DEFAULT 'false', " \
81                                                 "package_disable text DEFAULT 'false', " \
82                                                 "package_support_mode text, " \
83                                                 "package_hash text, " \
84                                                 "package_reserve1 text," \
85                                                 "package_reserve2 text," \
86                                                 "package_reserve3 text," \
87                                                 "package_reserve4 text," \
88                                                 "package_reserve5 text)"
89
90 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
91                                                 "(package text not null, " \
92                                                 "package_locale text DEFAULT 'No Locale', " \
93                                                 "package_label text, " \
94                                                 "package_icon text, " \
95                                                 "package_description text, " \
96                                                 "package_license text, " \
97                                                 "package_author, " \
98                                                 "PRIMARY KEY(package, package_locale), " \
99                                                 "FOREIGN KEY(package) " \
100                                                 "REFERENCES package_info(package) " \
101                                                 "ON DELETE CASCADE)"
102
103 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
104                                                 "(package text not null, " \
105                                                 "privilege text not null, " \
106                                                 "PRIMARY KEY(package, privilege) " \
107                                                 "FOREIGN KEY(package) " \
108                                                 "REFERENCES package_info(package) " \
109                                                 "ON DELETE CASCADE)"
110
111 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
112                                                 "(app_id text primary key not null, " \
113                                                 "app_component text, " \
114                                                 "app_exec text, " \
115                                                 "app_nodisplay text DEFAULT 'false', " \
116                                                 "app_type text, " \
117                                                 "app_onboot text DEFAULT 'false', " \
118                                                 "app_multiple text DEFAULT 'false', " \
119                                                 "app_autorestart text DEFAULT 'false', " \
120                                                 "app_taskmanage text DEFAULT 'false', " \
121                                                 "app_enabled text DEFAULT 'true', " \
122                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
123                                                 "app_screenreader text DEFAULT 'use-system-setting', " \
124                                                 "app_mainapp text, " \
125                                                 "app_recentimage text, " \
126                                                 "app_launchcondition text, " \
127                                                 "app_indicatordisplay text DEFAULT 'true', " \
128                                                 "app_portraitimg text, " \
129                                                 "app_landscapeimg text, " \
130                                                 "app_effectimage_type text, " \
131                                                 "app_guestmodevisibility text DEFAULT 'true', " \
132                                                 "app_permissiontype text DEFAULT 'normal', " \
133                                                 "app_preload text DEFAULT 'false', " \
134                                                 "app_submode text DEFAULT 'false', " \
135                                                 "app_submode_mainid text, " \
136                                                 "app_installed_storage text, " \
137                                                 "app_process_pool text DEFAULT 'false', " \
138                                                 "app_multi_instance text DEFAULT 'false', " \
139                                                 "app_multi_instance_mainid text, " \
140                                                 "app_multi_window text DEFAULT 'false', " \
141                                                 "app_support_disable text DEFAULT 'false', " \
142                                                 "app_disable text DEFAULT 'false', " \
143                                                 "app_ui_gadget text DEFAULT 'false', " \
144                                                 "app_removable text DEFAULT 'false', " \
145                                                 "app_support_mode text, " \
146                                                 "app_support_feature text, " \
147                                                 "component_type text, " \
148                                                 "package text not null, " \
149                                                 "app_package_type text DEFAULT 'rpm', " \
150                                                 "app_package_system text, " \
151                                                 "app_package_installed_time text, " \
152                                                 "app_reserve1 text, " \
153                                                 "app_reserve2 text, " \
154                                                 "app_reserve3 text, " \
155                                                 "app_reserve4 text, " \
156                                                 "app_reserve5 text, " \
157                                                 "FOREIGN KEY(package) " \
158                                                 "REFERENCES package_info(package) " \
159                                                 "ON DELETE CASCADE)"
160
161 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
162                                                 "(app_id text not null, " \
163                                                 "app_locale text DEFAULT 'No Locale', " \
164                                                 "app_label text, " \
165                                                 "app_icon text, " \
166                                                 "PRIMARY KEY(app_id,app_locale) " \
167                                                 "FOREIGN KEY(app_id) " \
168                                                 "REFERENCES package_app_info(app_id) " \
169                                                 "ON DELETE CASCADE)"
170
171 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
172                                                 "(app_id text not null, " \
173                                                 "app_icon text, " \
174                                                 "app_icon_section text, " \
175                                                 "app_icon_resolution text, " \
176                                                 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
177                                                 "FOREIGN KEY(app_id) " \
178                                                 "REFERENCES package_app_info(app_id) " \
179                                                 "ON DELETE CASCADE)"
180
181 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
182                                                 "(app_id text not null, " \
183                                                 "app_locale text DEFAULT 'No Locale', " \
184                                                 "app_image_section text, " \
185                                                 "app_image text, " \
186                                                 "PRIMARY KEY(app_id,app_image_section) " \
187                                                 "FOREIGN KEY(app_id) " \
188                                                 "REFERENCES package_app_info(app_id) " \
189                                                 "ON DELETE CASCADE)"
190
191 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
192                                                 "(app_id text not null, " \
193                                                 "app_control text not null, " \
194                                                 "PRIMARY KEY(app_id,app_control) " \
195                                                 "FOREIGN KEY(app_id) " \
196                                                 "REFERENCES package_app_info(app_id) " \
197                                                 "ON DELETE CASCADE)"
198
199 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC "create table if not exists package_app_app_svc " \
200                                                 "(app_id text not null, " \
201                                                 "operation text not null, " \
202                                                 "uri_scheme text, " \
203                                                 "mime_type text, " \
204                                                 "subapp_name text, " \
205                                                 "PRIMARY KEY(app_id,operation,uri_scheme,mime_type,subapp_name) " \
206                                                 "FOREIGN KEY(app_id) " \
207                                                 "REFERENCES package_app_info(app_id) " \
208                                                 "ON DELETE CASCADE)"
209
210 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "create table if not exists package_app_app_category " \
211                                                 "(app_id text not null, " \
212                                                 "category text not null, " \
213                                                 "PRIMARY KEY(app_id,category) " \
214                                                 "FOREIGN KEY(app_id) " \
215                                                 "REFERENCES package_app_info(app_id) " \
216                                                 "ON DELETE CASCADE)"
217
218 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "create table if not exists package_app_app_metadata " \
219                                                 "(app_id text not null, " \
220                                                 "md_key text not null, " \
221                                                 "md_value text not null, " \
222                                                 "PRIMARY KEY(app_id, md_key, md_value) " \
223                                                 "FOREIGN KEY(app_id) " \
224                                                 "REFERENCES package_app_info(app_id) " \
225                                                 "ON DELETE CASCADE)"
226
227 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "create table if not exists package_app_app_permission " \
228                                                 "(app_id text not null, " \
229                                                 "pm_type text not null, " \
230                                                 "pm_value text not null, " \
231                                                 "PRIMARY KEY(app_id, pm_type, pm_value) " \
232                                                 "FOREIGN KEY(app_id) " \
233                                                 "REFERENCES package_app_info(app_id) " \
234                                                 "ON DELETE CASCADE)"
235
236 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "create table if not exists package_app_share_allowed " \
237                                                 "(app_id text not null, " \
238                                                 "data_share_path text not null, " \
239                                                 "data_share_allowed text not null, " \
240                                                 "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \
241                                                 "FOREIGN KEY(app_id) " \
242                                                 "REFERENCES package_app_info(app_id) " \
243                                                 "ON DELETE CASCADE)"
244
245 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "create table if not exists package_app_share_request " \
246                                                 "(app_id text not null, " \
247                                                 "data_share_request text not null, " \
248                                                 "PRIMARY KEY(app_id,data_share_request) " \
249                                                 "FOREIGN KEY(app_id) " \
250                                                 "REFERENCES package_app_info(app_id) " \
251                                                 "ON DELETE CASCADE)"
252
253 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO "create table if not exists package_cert_index_info " \
254                                                 "(cert_info text not null, " \
255                                                 "cert_id integer, " \
256                                                 "cert_ref_count integer, " \
257                                                 "PRIMARY KEY(cert_id)) "
258
259 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO "create table if not exists package_cert_info " \
260                                                 "(package text not null, " \
261                                                 "author_root_cert integer, " \
262                                                 "author_im_cert integer, " \
263                                                 "author_signer_cert integer, " \
264                                                 "dist_root_cert integer, " \
265                                                 "dist_im_cert integer, " \
266                                                 "dist_signer_cert integer, " \
267                                                 "dist2_root_cert integer, " \
268                                                 "dist2_im_cert integer, " \
269                                                 "dist2_signer_cert integer, " \
270                                                 "PRIMARY KEY(package)) "
271
272 #define QUERY_CREATE_TABLE_PACKAGE_PKG_RESERVE "create table if not exists package_pkg_reserve " \
273                                                 "(pkgid text not null, " \
274                                                 "reserve1 text, " \
275                                                 "reserve2 text, " \
276                                                 "reserve3 text, " \
277                                                 "reserve4 text, " \
278                                                 "reserve5 text, " \
279                                                 "PRIMARY KEY(pkgid)) "
280
281 #define QUERY_CREATE_TABLE_PACKAGE_APP_RESERVE "create table if not exists package_app_reserve " \
282                                                 "(app_id text not null, " \
283                                                 "reserve1 text, " \
284                                                 "reserve2 text, " \
285                                                 "reserve3 text, " \
286                                                 "reserve4 text, " \
287                                                 "reserve5 text, " \
288                                                 "PRIMARY KEY(app_id)) "
289
290 #define QUERY_CREATE_TABLE_PACKAGE_PLUGIN_INFO "create table if not exists package_plugin_info " \
291                                                 "(pkgid text primary key not null, " \
292                                                 "enabled_plugin INTEGER DEFAULT 0)"
293
294 #define QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL "create table if not exists package_app_data_control " \
295                                                 "(app_id text not null, " \
296                                                 "providerid text not null, " \
297                                                 "access text not null, " \
298                                                 "type text not null, " \
299                                                 "PRIMARY KEY(app_id, providerid, access, type) " \
300                                                 "FOREIGN KEY(app_id) " \
301                                                 "REFERENCES package_app_info(app_id) " \
302                                                 "ON DELETE CASCADE)"
303
304 #define QUERY_CREATE_TABLE_PACKAGE_APP_ALIASID "create table if not exists package_app_aliasid" \
305                                                 "(app_id text primary key not null, "\
306                                                 "alias_id text not null)"
307
308 static int __insert_uiapplication_info(manifest_x *mfx);
309 static int __insert_uiapplication_appsvc_info(manifest_x *mfx);
310 static int __insert_uiapplication_appcategory_info(manifest_x *mfx);
311 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx);
312 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx);
313 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx);
314 static int __insert_uiapplication_share_request_info(manifest_x *mfx);
315 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata);
316 static int __insert_uiapplication_datacontrol_info(manifest_x *mfx);
317 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
318 static int __delete_manifest_info_from_pkgid(const char *pkgid);
319 static int __delete_manifest_info_from_appid(const char *appid);
320 static int __insert_manifest_info_in_db(manifest_x *mfx);
321 static int __delete_manifest_info_from_db(manifest_x *mfx);
322 static int __delete_appinfo_from_db(char *db_table, const char *appid);
323 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
324 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
325
326 static void __str_trim(char *input)
327 {
328         char *trim_str = input;
329
330         if (input == NULL)
331                 return;
332
333         while (*input != 0) {
334                 if (!isspace(*input)) {
335                         *trim_str = *input;
336                         trim_str++;
337                 }
338                 input++;
339         }
340
341         *trim_str = 0;
342         return;
343 }
344
345 const char *__get_str(const char *str)
346 {
347         if (str == NULL)
348         {
349                 return PKGMGR_PARSER_EMPTY_STR;
350         }
351
352         return str;
353 }
354
355 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
356 {
357         int ret = -1;
358         sqlite3 *handle;
359         if (access(db_path, F_OK) == 0) {
360                 ret =
361                     db_util_open(db_path, &handle,
362                                  DB_UTIL_REGISTER_HOOK_METHOD);
363                 if (ret != SQLITE_OK) {
364                         _LOGD("connect db [%s] failed!\n",
365                                db_path);
366                         return -1;
367                 }
368                 *db_handle = handle;
369                 return 0;
370         }
371         _LOGD("%s DB does not exists. Create one!!\n", db_path);
372
373         ret =
374             db_util_open(db_path, &handle,
375                          DB_UTIL_REGISTER_HOOK_METHOD);
376
377         if (ret != SQLITE_OK) {
378                 _LOGD("connect db [%s] failed!\n", db_path);
379                 return -1;
380         }
381         *db_handle = handle;
382         return 0;
383 }
384
385 int __evaluate_query(sqlite3 *db_handle, char *query)
386 {
387         sqlite3_stmt* p_statement;
388         int result;
389     result = sqlite3_prepare_v2(db_handle, query, strlen(query), &p_statement, NULL);
390     if (result != SQLITE_OK) {
391         _LOGE("Sqlite3 error [%d] : <%s> preparing <%s> querry\n", result, sqlite3_errmsg(db_handle), query);
392                 return -1;
393     }
394
395         result = sqlite3_step(p_statement);
396         if (result != SQLITE_DONE) {
397                 _LOGE("Sqlite3 error [%d] : <%s> executing <%s> statement\n", result, sqlite3_errmsg(db_handle), query);
398                 return -1;
399         }
400
401         result = sqlite3_finalize(p_statement);
402         if (result != SQLITE_OK) {
403                 _LOGE("Sqlite3 error [%d] : <%s> finalizing <%s> statement\n", result, sqlite3_errmsg(db_handle), query);
404                 return -1;
405         }
406         return 0;
407 }
408
409 int __initialize_db(sqlite3 *db_handle, char *db_query)
410 {
411         return (__evaluate_query(db_handle, db_query));
412 }
413
414 int __exec_query(char *query)
415 {
416         return (__evaluate_query(pkgmgr_parser_db, query));
417 }
418
419 int __exec_query_no_msg(char *query)
420 {
421         char *error_message = NULL;
422         if (SQLITE_OK !=
423             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
424                 sqlite3_free(error_message);
425                 return -1;
426         }
427         sqlite3_free(error_message);
428         return 0;
429 }
430
431 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
432 {
433         char *error_message = NULL;
434         if (SQLITE_OK !=
435             sqlite3_exec(db, query, callback, data, &error_message)) {
436                 _LOGE("Don't execute query = %s error message = %s\n", query,
437                        error_message);
438                 sqlite3_free(error_message);
439                 return -1;
440         }
441         sqlite3_free(error_message);
442         return 0;
443 }
444
445 GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath)
446 {
447
448         while(lbl != NULL)
449         {
450                 if (lbl->lang)
451                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
452                 lbl = lbl->next;
453         }
454         while(lcn != NULL)
455         {
456                 if (lcn->lang)
457                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
458                 lcn = lcn->next;
459         }
460         while(icn != NULL)
461         {
462                 if (icn->lang)
463                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
464                 icn = icn->next;
465         }
466         while(dcn != NULL)
467         {
468                 if (dcn->lang)
469                         locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
470                 dcn = dcn->next;
471         }
472         while(ath != NULL)
473         {
474                 if (ath->lang)
475                         locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
476                 ath = ath->next;
477         }
478         return locale;
479
480 }
481
482 static GList *__create_icon_list(GList *locale, icon_x *icn)
483 {
484         while(icn != NULL)
485         {
486                 if (icn->section)
487                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->section, __comparefunc, NULL);
488                 icn = icn->next;
489         }
490         return locale;
491 }
492
493 static GList *__create_image_list(GList *locale, image_x *image)
494 {
495         while(image != NULL)
496         {
497                 if (image->section)
498                         locale = g_list_insert_sorted_with_data(locale, (gpointer)image->section, __comparefunc, NULL);
499                 image = image->next;
500         }
501         return locale;
502 }
503
504 void __trimfunc(GList* trim_list)
505 {
506         char *trim_data = NULL;
507         char *prev = NULL;
508
509         GList *list = NULL;
510         list = g_list_first(trim_list);
511
512         while (list) {
513                 trim_data = (char *)list->data;
514                 if (trim_data) {
515                         if (prev) {
516                                 if (strcmp(trim_data, prev) == 0) {
517                                         trim_list = g_list_remove(trim_list, trim_data);
518                                         list = g_list_first(trim_list);
519                                         prev = NULL;
520                                         continue;
521                                 } else
522                                         prev = trim_data;
523                         }
524                         else
525                                 prev = trim_data;
526                 }
527                 list = g_list_next(list);
528         }
529 }
530
531 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
532 {
533         if (a == NULL || b == NULL)
534                 return 0;
535         if (strcmp((char*)a, (char*)b) == 0)
536                 return 0;
537         if (strcmp((char*)a, (char*)b) < 0)
538                 return -1;
539         if (strcmp((char*)a, (char*)b) > 0)
540                 return 1;
541         return 0;
542 }
543
544 void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
545                 char **label, char **license, char **icon, char **description, char **author)
546 {
547         while(lbl != NULL)
548         {
549                 if (lbl->lang) {
550                         if (strcmp(lbl->lang, (char *)data) == 0) {
551                                 *label = (char*)lbl->text;
552                                 break;
553                         }
554                 }
555                 lbl = lbl->next;
556         }
557         while(lcn != NULL)
558         {
559                 if (lcn->lang) {
560                         if (strcmp(lcn->lang, (char *)data) == 0) {
561                                 *license = (char*)lcn->text;
562                                 break;
563                         }
564                 }
565                 lcn = lcn->next;
566         }
567         while(icn != NULL)
568         {
569                 if (icn->section == NULL) {
570                         if (icn->lang) {
571                                 if (strcmp(icn->lang, (char *)data) == 0) {
572                                         *icon = (char*)icn->text;
573                                         break;
574                                 }
575                         }
576                 }
577                 icn = icn->next;
578         }
579         while(dcn != NULL)
580         {
581                 if (dcn->lang) {
582                         if (strcmp(dcn->lang, (char *)data) == 0) {
583                                 *description = (char*)dcn->text;
584                                 break;
585                         }
586                 }
587                 dcn = dcn->next;
588         }
589         while(ath != NULL)
590         {
591                 if (ath->lang) {
592                         if (strcmp(ath->lang, (char *)data) == 0) {
593                                 *author = (char*)ath->text;
594                                 break;
595                         }
596                 }
597                 ath = ath->next;
598         }
599
600 }
601
602 static void __extract_icon_data(gpointer data, icon_x *icn, char **icon, char **resolution)
603 {
604         while(icn != NULL)
605         {
606                 if (icn->section) {
607                         if (strcmp(icn->section, (char *)data) == 0) {
608                                 *icon = (char*)icn->text;
609                                 *resolution = (char*)icn->resolution;
610                                 break;
611                         }
612                 }
613                 icn = icn->next;
614         }
615 }
616
617 static void __extract_image_data(gpointer data, image_x*image, char **lang, char **img)
618 {
619         while(image != NULL)
620         {
621                 if (image->section) {
622                         if (strcmp(image->section, (char *)data) == 0) {
623                                 *lang = (char*)image->lang;
624                                 *img = (char*)image->text;
625                                 break;
626                         }
627                 }
628                 image = image->next;
629         }
630 }
631
632 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
633 {
634         int ret = -1;
635         char *label = NULL;
636         char *icon = NULL;
637         char *description = NULL;
638         char *license = NULL;
639         char *author = NULL;
640         char *query = NULL;
641
642         manifest_x *mfx = (manifest_x *)userdata;
643         label_x *lbl = mfx->label;
644         license_x *lcn = mfx->license;
645         icon_x *icn = mfx->icon;
646         description_x *dcn = mfx->description;
647         author_x *ath = mfx->author;
648
649         __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
650         if (!label && !description && !icon && !license && !author)
651                 return;
652
653         query = sqlite3_mprintf("insert into package_localized_info(package, package_locale, " \
654                 "package_label, package_icon, package_description, package_license, package_author) values " \
655                 "(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
656                 mfx->package,
657                 (char*)data,
658                 __get_str(label),
659                 __get_str(icon),
660                 __get_str(description),
661                 __get_str(license),
662                 __get_str(author));
663
664         ret = __exec_query(query);
665         if (ret == -1)
666                 _LOGD("Package Localized Info DB Insert failed\n");
667
668         sqlite3_free(query);
669 }
670
671 static int __insert_uiapplication_datacontrol_info(manifest_x *mfx)
672 {
673         uiapplication_x *up = mfx->uiapplication;
674         datacontrol_x *dc = NULL;
675         int ret = -1;
676         char *query = NULL;
677
678         while (up != NULL)
679         {
680                 dc = up->datacontrol;
681                 while (dc != NULL) {
682                         query = sqlite3_mprintf("insert into package_app_data_control(app_id, providerid, access, type) " \
683                                 "values(%Q, %Q, %Q, %Q)", \
684                                 up->appid,
685                                 dc->providerid,
686                                 dc->access,
687                                 dc->type);
688
689                         ret = __exec_query(query);
690                         sqlite3_free(query);
691                         if (ret == -1) {
692                                 _LOGD("Package UiApp Data Control DB Insert Failed\n");
693                                 return -1;
694                         }
695                         dc = dc->next;
696                 }
697                 up = up->next;
698         }
699         return 0;
700 }
701
702 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata)
703 {
704         int ret = -1;
705         char *label = NULL;
706         char *icon = NULL;
707         char *query = NULL;
708
709         uiapplication_x *up = (uiapplication_x*)userdata;
710         label_x *lbl = up->label;
711         icon_x *icn = up->icon;
712
713         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
714         if (!label && !icon)
715                 return;
716
717         query = sqlite3_mprintf("insert into package_app_localized_info(app_id, app_locale, " \
718                 "app_label, app_icon) values " \
719                 "(%Q, %Q, %Q, %Q)", up->appid, (char*)data,
720                 __get_str(label), __get_str(icon));
721         ret = __exec_query(query);
722         if (ret == -1)
723                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
724
725         sqlite3_free(query);
726
727         /*insert ui app locale info to pkg locale to get mainapp data */
728         if (strcasecmp(up->mainapp, "true")==0) {
729                 char *update_query = NULL;
730                 update_query = sqlite3_mprintf("insert into package_localized_info(package, package_locale, " \
731                         "package_label, package_icon, package_description, package_license, package_author) values " \
732                         "(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
733                         up->package,
734                         (char*)data,
735                         __get_str(label),
736                         __get_str(icon),
737                         PKGMGR_PARSER_EMPTY_STR,
738                         PKGMGR_PARSER_EMPTY_STR,
739                         PKGMGR_PARSER_EMPTY_STR);
740
741                 ret = __exec_query_no_msg(update_query);
742                 sqlite3_free(update_query);
743
744                 if (icon != NULL) {
745                         update_query = sqlite3_mprintf("update package_localized_info set package_icon=%Q " \
746                                 "where package=%Q and package_locale=%Q", icon, up->package, (char*)data);
747                         ret = __exec_query_no_msg(update_query);
748                         sqlite3_free(update_query);
749                 }
750         }
751 }
752
753 static void __insert_uiapplication_icon_section_info(gpointer data, gpointer userdata)
754 {
755         int ret = -1;
756         char *icon = NULL;
757         char *resolution = NULL;
758         char * query = NULL;
759
760         uiapplication_x *up = (uiapplication_x*)userdata;
761         icon_x *icn = up->icon;
762
763         __extract_icon_data(data, icn, &icon, &resolution);
764         if (!icon && !resolution)
765                 return;
766
767         query = sqlite3_mprintf("insert into package_app_icon_section_info(app_id, " \
768                 "app_icon, app_icon_section, app_icon_resolution) values " \
769                 "(%Q, %Q, %Q, %Q)", up->appid,
770                 icon, (char*)data, resolution);
771
772         ret = __exec_query(query);
773         if (ret == -1)
774                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
775
776         sqlite3_free(query);
777 }
778
779 static void __insert_uiapplication_image_info(gpointer data, gpointer userdata)
780 {
781         int ret = -1;
782         char *lang = NULL;
783         char *img = NULL;
784         char *query = NULL;
785
786         uiapplication_x *up = (uiapplication_x*)userdata;
787         image_x *image = up->image;
788
789         __extract_image_data(data, image, &lang, &img);
790         if (!lang && !img)
791                 return;
792
793         query = sqlite3_mprintf("insert into package_app_image_info(app_id, app_locale, " \
794                 "app_image_section, app_image) values " \
795                 "(%Q, %Q, %Q, %Q)", up->appid, lang, (char*)data, img);
796
797         ret = __exec_query(query);
798         if (ret == -1)
799                 _LOGD("Package UiApp image Info DB Insert failed\n");
800
801         sqlite3_free(query);
802 }
803
804 static int __insert_ui_mainapp_info(manifest_x *mfx)
805 {
806         uiapplication_x *up = mfx->uiapplication;
807         int ret = -1;
808         char *query = NULL;
809         while(up != NULL)
810         {
811                 query = sqlite3_mprintf("update package_app_info set app_mainapp=%Q where app_id=%Q", up->mainapp, up->appid);
812
813                 ret = __exec_query(query);
814                 sqlite3_free(query);
815                 if (ret == -1) {
816                         _LOGD("Package UiApp Info DB Insert Failed\n");
817                         return -1;
818                 }
819                 if (strcasecmp(up->mainapp, "True")==0)
820                         mfx->mainapp_id = strdup(up->appid);
821
822                 up = up->next;
823         }
824
825         if (mfx->mainapp_id == NULL){
826                 if (mfx->uiapplication && mfx->uiapplication->appid) {
827                         query = sqlite3_mprintf("update package_app_info set app_mainapp='true' where app_id=%Q", mfx->uiapplication->appid);
828                 } else {
829                         _LOGD("Not valid appid\n");
830                         return -1;
831                 }
832
833                 ret = __exec_query(query);
834                 sqlite3_free(query);
835                 if (ret == -1) {
836                         _LOGD("Package UiApp Info DB Insert Failed\n");
837                         return -1;
838                 }
839
840                 FREE_AND_NULL(mfx->uiapplication->mainapp);
841                 mfx->uiapplication->mainapp= strdup("true");
842                 mfx->mainapp_id = strdup(mfx->uiapplication->appid);
843         }
844
845         query = sqlite3_mprintf("update package_info set mainapp_id=%Q where package=%Q", mfx->mainapp_id, mfx->package);
846         ret = __exec_query(query);
847         sqlite3_free(query);
848         if (ret == -1) {
849                 _LOGD("Package Info DB update Failed\n");
850                 return -1;
851         }
852
853         return 0;
854 }
855 /* _PRODUCT_LAUNCHING_ENHANCED_
856 *  up->indicatordisplay, up->portraitimg, up->landscapeimg, up->guestmode_appstatus
857 */
858 static int __insert_uiapplication_info(manifest_x *mfx)
859 {
860         uiapplication_x *up = mfx->uiapplication;
861         int ret = -1;
862         char *query = NULL;
863         char *type = NULL;
864
865         if (mfx->type)
866                 type = strdup(mfx->type);
867         else
868                 type = strdup("rpm");
869
870         while(up != NULL)
871         {
872                 query = sqlite3_mprintf("insert into package_app_info(app_id, app_component, app_exec, app_nodisplay, app_type, app_onboot, " \
873                         "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_screenreader, app_mainapp , app_recentimage, " \
874                         "app_launchcondition, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_effectimage_type, app_guestmodevisibility, app_permissiontype, "\
875                         "app_preload, app_submode, app_submode_mainid, app_installed_storage, app_process_pool, app_multi_instance, app_multi_instance_mainid, app_multi_window, app_support_disable, "\
876                         "app_ui_gadget, app_removable, app_support_mode, app_support_feature, component_type, package, "\
877                         "app_package_type, app_package_system, app_package_installed_time"\
878                         ") " \
879                         "values(%Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q)",\
880                         up->appid,
881                         "uiapp",
882                         up->exec,
883                         up->nodisplay,
884                         up->type,
885                         up->onboot,
886                         up->multiple,
887                         up->autorestart,
888                         up->taskmanage,
889                         up->enabled,
890                         up->hwacceleration,
891                         up->screenreader,
892                         up->mainapp,
893                         __get_str(up->recentimage),
894                         up->launchcondition,
895                         up->indicatordisplay,
896                         __get_str(up->portraitimg),
897                         __get_str(up->landscapeimg),
898                         __get_str(up->effectimage_type),
899                         up->guestmode_visibility,
900                         up->permission_type,
901                         mfx->preload,
902                         up->submode,
903                         __get_str(up->submode_mainid),
904                         mfx->installed_storage,
905                         up->process_pool,
906                         up->multi_instance,
907                         __get_str(up->multi_instance_mainid),
908                         up->multi_window,
909                         mfx->support_disable,
910                         up->ui_gadget,
911                         mfx->removable,
912                         mfx->support_mode,
913                         up->support_feature,
914                         up->component_type,
915                         mfx->package,
916                         type,
917                         mfx->system,
918                         mfx->installed_time
919                         );
920
921                 ret = __exec_query(query);
922                 sqlite3_free(query);
923                 if (ret == -1) {
924                         _LOGD("Package UiApp Info DB Insert Failed\n");
925                         FREE_AND_NULL(type);
926                         return -1;
927                 }
928                 up = up->next;
929         }
930         FREE_AND_NULL(type);
931         return 0;
932 }
933
934 static int __insert_uiapplication_appcategory_info(manifest_x *mfx)
935 {
936         uiapplication_x *up = mfx->uiapplication;
937         category_x *ct = NULL;
938         int ret = -1;
939         char *query = NULL;
940         while(up != NULL)
941         {
942                 ct = up->category;
943                 while (ct != NULL)
944                 {
945                         query = sqlite3_mprintf("insert into package_app_app_category(app_id, category) " \
946                                 "values(%Q, %Q)",\
947                                  up->appid, ct->name);
948                         ret = __exec_query(query);
949                         sqlite3_free(query);
950                         if (ret == -1) {
951                                 _LOGD("Package UiApp Category Info DB Insert Failed\n");
952                                 return -1;
953                         }
954                         ct = ct->next;
955                 }
956                 up = up->next;
957         }
958         return 0;
959 }
960
961 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx)
962 {
963         uiapplication_x *up = mfx->uiapplication;
964         metadata_x *md = NULL;
965         int ret = -1;
966         char *query = NULL;
967         while(up != NULL)
968         {
969                 md = up->metadata;
970                 while (md != NULL)
971                 {
972                         if (md->key) {
973                                 query = sqlite3_mprintf("insert into package_app_app_metadata(app_id, md_key, md_value) " \
974                                         "values(%Q, %Q, %Q)",\
975                                          up->appid, md->key, __get_str(md->value));
976                                 ret = __exec_query(query);
977                                 sqlite3_free(query);
978                                 if (ret == -1) {
979                                         _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
980                                         return -1;
981                                 }
982                         }
983                         md = md->next;
984                 }
985                 up = up->next;
986         }
987         return 0;
988 }
989
990 static int __insert_uiapplication_apppermission_info(manifest_x *mfx)
991 {
992         uiapplication_x *up = mfx->uiapplication;
993         permission_x *pm = NULL;
994         int ret = -1;
995         char *query = NULL;
996         while(up != NULL)
997         {
998                 pm = up->permission;
999                 while (pm != NULL)
1000                 {
1001                         query = sqlite3_mprintf("insert into package_app_app_permission(app_id, pm_type, pm_value) " \
1002                                 "values(%Q, %Q, %Q)",\
1003                                  up->appid, pm->type, pm->value);
1004                         ret = __exec_query(query);
1005                         sqlite3_free(query);
1006                         if (ret == -1) {
1007                                 _LOGD("Package UiApp permission Info DB Insert Failed\n");
1008                                 return -1;
1009                         }
1010                         pm = pm->next;
1011                 }
1012                 up = up->next;
1013         }
1014         return 0;
1015 }
1016
1017 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx)
1018 {
1019         int ret = 0;
1020         char *buf = NULL;
1021         char *buftemp = NULL;
1022         char *query = NULL;
1023         uiapplication_x *up = mfx->uiapplication;
1024
1025         buf = (char *)calloc(1, BUFMAX);
1026         retvm_if(!buf, PMINFO_R_ERROR, "Malloc Failed\n");
1027
1028         buftemp = (char *)calloc(1, BUFMAX);
1029         tryvm_if(!buftemp, ret = PMINFO_R_ERROR, "Malloc Failed\n");
1030
1031         for(; up; up=up->next) {
1032                 if (up->appsvc) {
1033                         appsvc_x *asvc = NULL;
1034                         operation_x *op = NULL;
1035                         mime_x *mi = NULL;
1036                         uri_x *ui = NULL;
1037                         subapp_x *sub = NULL;
1038                         const char *operation = NULL;
1039                         const char *mime = NULL;
1040                         const char *uri = NULL;
1041                         const char *subapp = NULL;
1042                         int i = 0;
1043                         memset(buf, '\0', BUFMAX);
1044                         memset(buftemp, '\0', BUFMAX);
1045
1046                         asvc = up->appsvc;
1047                         while(asvc != NULL) {
1048                                 op = asvc->operation;
1049                                 while(op != NULL) {
1050                                         if (op)
1051                                                 operation = op->name;
1052                                         mi = asvc->mime;
1053
1054                                         do
1055                                         {
1056                                                 if (mi)
1057                                                         mime = mi->name;
1058                                                 sub = asvc->subapp;
1059                                                 do
1060                                                 {
1061                                                         if (sub)
1062                                                                 subapp = sub->name;
1063                                                         ui = asvc->uri;
1064                                                         do
1065                                                         {
1066                                                                 if (ui)
1067                                                                         uri = ui->name;
1068
1069                                                                 if(i++ > 0) {
1070                                                                         strncpy(buftemp, buf, BUFMAX);
1071                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
1072                                                                 }
1073
1074                                                                 strncpy(buftemp, buf, BUFMAX);
1075                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
1076 //                                                              _LOGD("buf[%s]\n", buf);
1077
1078                                                                 if (ui)
1079                                                                         ui = ui->next;
1080                                                                 uri = NULL;
1081                                                         } while(ui != NULL);
1082                                                         if (sub)
1083                                                                 sub = sub->next;
1084                                                         subapp = NULL;
1085                                                 }while(sub != NULL);
1086                                                 if (mi)
1087                                                         mi = mi->next;
1088                                                 mime = NULL;
1089                                         }while(mi != NULL);
1090                                         if (op)
1091                                                 op = op->next;
1092                                         operation = NULL;
1093                                 }
1094                                 asvc = asvc->next;
1095                         }
1096
1097                         query= sqlite3_mprintf("insert into package_app_app_control(app_id, app_control) values(%Q, %Q)", up->appid,  buf);
1098                         ret = __exec_query(query);
1099                         if (ret < 0) {
1100                                 _LOGD("Package UiApp app_control DB Insert Failed\n");
1101                         }
1102                         sqlite3_free(query);
1103                 }
1104         }
1105
1106 catch:
1107         FREE_AND_NULL(buf);
1108         FREE_AND_NULL(buftemp);
1109
1110         return 0;
1111 }
1112
1113 static int __insert_uiapplication_appsvc_info(manifest_x *mfx)
1114 {
1115         uiapplication_x *up = mfx->uiapplication;
1116         appsvc_x *asvc = NULL;
1117         operation_x *op = NULL;
1118         mime_x *mi = NULL;
1119         uri_x *ui = NULL;
1120         subapp_x *sub = NULL;
1121         int ret = -1;
1122         char *query = NULL;
1123         const char *operation = NULL;
1124         const char *mime = NULL;
1125         const char *uri = NULL;
1126         const char *subapp = NULL;
1127         while(up != NULL)
1128         {
1129                 asvc = up->appsvc;
1130                 while(asvc != NULL)
1131                 {
1132                         op = asvc->operation;
1133                         while(op != NULL)
1134                         {
1135                                 if (op)
1136                                         operation = op->name;
1137                                 mi = asvc->mime;
1138
1139                                 do
1140                                 {
1141                                         if (mi)
1142                                                 mime = mi->name;
1143                                         sub = asvc->subapp;
1144                                         do
1145                                         {
1146                                                 if (sub)
1147                                                         subapp = sub->name;
1148                                                 ui = asvc->uri;
1149                                                 do
1150                                                 {
1151                                                         if (ui)
1152                                                                 uri = ui->name;
1153                                                         query = sqlite3_mprintf("insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1154                                                                 "values(%Q, %Q, %Q, %Q, %Q)",\
1155                                                                  up->appid,
1156                                                                  operation,
1157                                                                  __get_str(uri),
1158                                                                  __get_str(mime),
1159                                                                  __get_str(subapp));
1160
1161                                                         ret = __exec_query(query);
1162                                                         sqlite3_free(query);
1163                                                         if (ret == -1) {
1164                                                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1165                                                                 return -1;
1166                                                         }
1167                                                         if (ui)
1168                                                                 ui = ui->next;
1169                                                         uri = NULL;
1170                                                 } while(ui != NULL);
1171                                                 if (sub)
1172                                                         sub = sub->next;
1173                                                 subapp = NULL;
1174                                         }while(sub != NULL);
1175                                         if (mi)
1176                                                 mi = mi->next;
1177                                         mime = NULL;
1178                                 }while(mi != NULL);
1179                                 if (op)
1180                                         op = op->next;
1181                                 operation = NULL;
1182                         }
1183                         asvc = asvc->next;
1184                 }
1185                 up = up->next;
1186         }
1187         return 0;
1188 }
1189
1190 static int __insert_uiapplication_share_request_info(manifest_x *mfx)
1191 {
1192         uiapplication_x *up = mfx->uiapplication;
1193         datashare_x *ds = NULL;
1194         request_x *rq = NULL;
1195         int ret = -1;
1196         char *query = NULL;
1197         while(up != NULL)
1198         {
1199                 ds = up->datashare;
1200                 while(ds != NULL)
1201                 {
1202                         rq = ds->request;
1203                         while(rq != NULL)
1204                         {
1205                                 query = sqlite3_mprintf("insert into package_app_share_request(app_id, data_share_request) " \
1206                                         "values(%Q, %Q)",\
1207                                          up->appid, rq->text);
1208                                 ret = __exec_query(query);
1209                                 sqlite3_free(query);
1210                                 if (ret == -1) {
1211                                         _LOGD("Package UiApp Share Request DB Insert Failed\n");
1212                                         return -1;
1213                                 }
1214                                 rq = rq->next;
1215                         }
1216                         ds = ds->next;
1217                 }
1218                 up = up->next;
1219         }
1220         return 0;
1221 }
1222
1223 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx)
1224 {
1225         uiapplication_x *up = mfx->uiapplication;
1226         datashare_x *ds = NULL;
1227         define_x *df = NULL;
1228         allowed_x *al = NULL;
1229         int ret = -1;
1230         char *query = NULL;
1231         while(up != NULL)
1232         {
1233                 ds = up->datashare;
1234                 while(ds != NULL)
1235                 {
1236                         df = ds->define;
1237                         while(df != NULL)
1238                         {
1239                                 al = df->allowed;
1240                                 while(al != NULL)
1241                                 {
1242                                         query = sqlite3_mprintf("insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1243                                                 "values(%Q, %Q, %Q)",\
1244                                                  up->appid, df->path, al->text);
1245                                         ret = __exec_query(query);
1246                                         sqlite3_free(query);
1247                                         if (ret == -1) {
1248                                                 _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
1249                                                 return -1;
1250                                         }
1251                                         al = al->next;
1252                                 }
1253                                 df = df->next;
1254                         }
1255                         ds = ds->next;
1256                 }
1257                 up = up->next;
1258         }
1259         return 0;
1260 }
1261
1262 static int __insert_manifest_info_in_db(manifest_x *mfx)
1263 {
1264         label_x *lbl = mfx->label;
1265         license_x *lcn = mfx->license;
1266         icon_x *icn = mfx->icon;
1267         description_x *dcn = mfx->description;
1268         author_x *ath = mfx->author;
1269         uiapplication_x *up = mfx->uiapplication;
1270         uiapplication_x *up_icn = mfx->uiapplication;
1271         uiapplication_x *up_image = mfx->uiapplication;
1272         privileges_x *pvs = NULL;
1273         privilege_x *pv = NULL;
1274         char *query = NULL;
1275         char root[MAX_QUERY_LEN] = { '\0' };
1276         int ret = -1;
1277         char *type = NULL;
1278         char *path = NULL;
1279         const char *auth_name = NULL;
1280         const char *auth_email = NULL;
1281         const char *auth_href = NULL;
1282
1283         GList *pkglocale = NULL;
1284         GList *applocale = NULL;
1285         GList *appicon = NULL;
1286         GList *appimage = NULL;
1287
1288         if (ath) {
1289                 if (ath->text)
1290                         auth_name = ath->text;
1291                 if (ath->email)
1292                         auth_email = ath->email;
1293                 if (ath->href)
1294                         auth_href = ath->href;
1295         }
1296
1297         /*Insert in the package_info DB*/
1298         if (mfx->type)
1299                 type = strdup(mfx->type);
1300         else
1301                 type = strdup("rpm");
1302         /*Insert in the package_info DB*/
1303         if (mfx->root_path) {
1304                 path = strdup(mfx->root_path);
1305         } else {
1306                 if (strcmp(type,"rpm")==0) {
1307                         snprintf(root, MAX_QUERY_LEN - 1, "/opt/share/packages/%s.xml", mfx->package);
1308                         if (access(root, F_OK) == 0) {
1309                                 memset(root, '\0', MAX_QUERY_LEN);
1310                                 snprintf(root, MAX_QUERY_LEN - 1, "/opt/usr/apps/%s", mfx->package);
1311                         } else {
1312                                 memset(root, '\0', MAX_QUERY_LEN);
1313                                 snprintf(root, MAX_QUERY_LEN - 1, "/usr/apps/%s", mfx->package);
1314                         }
1315                 } else {
1316                         snprintf(root, MAX_QUERY_LEN - 1, "/opt/usr/apps/%s", mfx->package);
1317                 }
1318
1319                 path = strdup(root);
1320         }
1321
1322         query = sqlite3_mprintf("insert into package_info(package, package_type, package_version, install_location, package_size, " \
1323                 "package_removable, package_preload, package_readonly, package_update, package_appsetting, package_nodisplay, package_system," \
1324                 "author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path, csc_path, package_support_disable, " \
1325                 "package_mother_package, package_support_mode, package_reserve1, package_reserve2, package_hash) " \
1326                 "values(%Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q)",\
1327                  mfx->package,
1328                  type,
1329                  mfx->version,
1330                  __get_str(mfx->installlocation),
1331                  __get_str(mfx->package_size),
1332                  mfx->removable,
1333                  mfx->preload,
1334                  mfx->readonly,
1335                  mfx->update,
1336                  mfx->appsetting,
1337                  mfx->nodisplay_setting,
1338                  mfx->system,
1339                  __get_str(auth_name),
1340                  __get_str(auth_email),
1341                  __get_str(auth_href),
1342                  mfx->installed_time,
1343                  mfx->installed_storage,
1344                  __get_str(mfx->storeclient_id),
1345                  mfx->mainapp_id,
1346                  __get_str(mfx->package_url),
1347                  path,
1348                  __get_str(mfx->csc_path),
1349                  mfx->support_disable,
1350                  mfx->mother_package,
1351                  mfx->support_mode,
1352                  __get_str(mfx->support_reset),
1353                  mfx->use_reset,
1354                  mfx->hash
1355                  );
1356
1357         ret = __exec_query(query);
1358         sqlite3_free(query);
1359         if (ret == -1) {
1360                 _LOGD("Package Info DB Insert Failed\n");
1361                 FREE_AND_NULL(type);
1362                 FREE_AND_NULL(path);
1363                 return -1;
1364         }
1365
1366         FREE_AND_NULL(type);
1367         FREE_AND_NULL(path);
1368
1369         /*Insert in the package_privilege_info DB*/
1370         pvs = mfx->privileges;
1371         while (pvs != NULL) {
1372                 pv = pvs->privilege;
1373                 while (pv != NULL) {
1374                         query = sqlite3_mprintf("insert into package_privilege_info(package, privilege) " \
1375                                 "values(%Q, %Q)",\
1376                                  mfx->package, pv->text);
1377                         ret = __exec_query(query);
1378                         sqlite3_free(query);
1379                         if (ret == -1) {
1380                                 _LOGD("Package Privilege Info DB Insert Failed\n");
1381                                 return -1;
1382                         }
1383                         pv = pv->next;
1384                 }
1385                 pvs = pvs->next;
1386         }
1387
1388         ret = __insert_ui_mainapp_info(mfx);
1389         if (ret == -1)
1390                 return -1;
1391
1392         /*Insert the package locale*/
1393         pkglocale = __create_locale_list(pkglocale, lbl, lcn, icn, dcn, ath);
1394         /*remove duplicated data in pkglocale*/
1395         __trimfunc(pkglocale);
1396
1397         /*Insert the app locale info */
1398         while(up != NULL)
1399         {
1400                 applocale = __create_locale_list(applocale, up->label, NULL, up->icon, NULL, NULL);
1401                 up = up->next;
1402         }
1403         /*remove duplicated data in applocale*/
1404         __trimfunc(applocale);
1405
1406         /*Insert the app icon info */
1407         while(up_icn != NULL)
1408         {
1409                 appicon = __create_icon_list(appicon, up_icn->icon);
1410                 up_icn = up_icn->next;
1411         }
1412         /*remove duplicated data in appicon*/
1413         __trimfunc(appicon);
1414
1415         /*Insert the image info */
1416         while(up_image != NULL)
1417         {
1418                 appimage = __create_image_list(appimage, up_image->image);
1419                 up_image = up_image->next;
1420         }
1421         /*remove duplicated data in appimage*/
1422         __trimfunc(appimage);
1423
1424         /*g_list_foreach(pkglocale, __printfunc, NULL);*/
1425         /*_LOGD("\n");*/
1426         /*g_list_foreach(applocale, __printfunc, NULL);*/
1427
1428         g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1429
1430         /*native app locale info*/
1431         up = mfx->uiapplication;
1432         while(up != NULL)
1433         {
1434                 g_list_foreach(applocale, __insert_uiapplication_locale_info, (gpointer)up);
1435                 up = up->next;
1436         }
1437
1438         /*app icon locale info*/
1439         up_icn = mfx->uiapplication;
1440         while(up_icn != NULL)
1441         {
1442                 g_list_foreach(appicon, __insert_uiapplication_icon_section_info, (gpointer)up_icn);
1443                 up_icn = up_icn->next;
1444         }
1445
1446         /*app image info*/
1447         up_image = mfx->uiapplication;
1448         while(up_image != NULL)
1449         {
1450                 g_list_foreach(appimage, __insert_uiapplication_image_info, (gpointer)up_image);
1451                 up_image = up_image->next;
1452         }
1453
1454         g_list_free(pkglocale);
1455         pkglocale = NULL;
1456         g_list_free(applocale);
1457         applocale = NULL;
1458         g_list_free(appicon);
1459         appicon = NULL;
1460         g_list_free(appimage);
1461         appimage = NULL;
1462
1463
1464         /*Insert in the package_app_info DB*/
1465         ret = __insert_uiapplication_info(mfx);
1466         if (ret == -1)
1467                 return -1;
1468
1469         /*Insert in the package_app_app_control DB*/
1470         ret = __insert_uiapplication_appcontrol_info(mfx);
1471         if (ret == -1)
1472                 return -1;
1473
1474         /*Insert in the package_app_app_category DB*/
1475         ret = __insert_uiapplication_appcategory_info(mfx);
1476         if (ret == -1)
1477                 return -1;
1478
1479         /*Insert in the package_app_app_metadata DB*/
1480         ret = __insert_uiapplication_appmetadata_info(mfx);
1481         if (ret == -1)
1482                 return -1;
1483
1484         /*Insert in the package_app_app_permission DB*/
1485         ret = __insert_uiapplication_apppermission_info(mfx);
1486         if (ret == -1)
1487                 return -1;
1488
1489         /*Insert in the package_app_app_svc DB*/
1490         ret = __insert_uiapplication_appsvc_info(mfx);
1491         if (ret == -1)
1492                 return -1;
1493
1494         /*Insert in the package_app_share_allowed DB*/
1495         ret = __insert_uiapplication_share_allowed_info(mfx);
1496         if (ret == -1)
1497                 return -1;
1498
1499         /*Insert in the package_app_share_request DB*/
1500         ret = __insert_uiapplication_share_request_info(mfx);
1501         if (ret == -1)
1502                 return -1;
1503
1504         /*Insert in the package_app_data_control DB*/
1505         ret = __insert_uiapplication_datacontrol_info(mfx);
1506         if (ret == -1)
1507                 return -1;
1508
1509         return 0;
1510
1511 }
1512
1513 static int __update_pkg_info_for_disable(const char *pkgid, bool disable)
1514 {
1515         char *query = NULL;
1516         int ret = -1;
1517
1518         /*Update from package info*/
1519         query = sqlite3_mprintf("update package_info set package_disable=%Q where package=%Q", disable?"true":"false", pkgid);
1520         ret = __exec_query(query);
1521         sqlite3_free(query);
1522         retvm_if(ret < 0, PMINFO_R_ERROR, "__exec_query() failed.\n");
1523
1524         /*Update from app info*/
1525         query = sqlite3_mprintf("update package_app_info set app_disable=%Q where package=%Q", disable?"true":"false", pkgid);
1526         ret = __exec_query(query);
1527         sqlite3_free(query);
1528         retvm_if(ret < 0, PMINFO_R_ERROR, "__exec_query() failed.\n");
1529
1530         return PMINFO_R_OK;
1531 }
1532
1533 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1534 {
1535         int ret = 0;
1536         char *query = sqlite3_mprintf("delete from %Q where app_id=%Q", db_table, appid);
1537         ret = __exec_query(query);
1538         sqlite3_free(query);
1539         if (ret == -1) {
1540                 _LOGD("DB Deletion from table (%s) Failed\n", db_table);
1541                 ret = -1;
1542         }
1543
1544         return ret;
1545 }
1546
1547 static int __delete_manifest_info_from_db(manifest_x *mfx)
1548 {
1549         int ret = -1;
1550         uiapplication_x *up = mfx->uiapplication;
1551
1552         /* Delete package info DB */
1553         ret = __delete_manifest_info_from_pkgid(mfx->package);
1554         retvm_if(ret < 0, PMINFO_R_ERROR, "Package Info DB Delete Failed\n");
1555
1556         while (up != NULL) {
1557                 ret = __delete_manifest_info_from_appid(up->appid);
1558                 retvm_if(ret < 0, PMINFO_R_ERROR, "App Info DB Delete Failed\n");
1559
1560                 up = up->next;
1561         }
1562
1563         return 0;
1564 }
1565
1566 static int __delete_appsvc_db(const char *appid)
1567 {
1568         int ret = -1;
1569         void *lib_handle = NULL;
1570         int (*appsvc_operation) (const char *);
1571
1572         if ((lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY)) == NULL) {
1573                 _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAPPSVC_PATH);
1574                 return ret;
1575         }
1576         if ((appsvc_operation =
1577                  dlsym(lib_handle, "appsvc_unset_defapp")) == NULL || dlerror() != NULL) {
1578                 _LOGE("can not find symbol \n");
1579                 goto END;
1580         }
1581
1582         ret = appsvc_operation(appid);
1583
1584 END:
1585         if (lib_handle)
1586                 dlclose(lib_handle);
1587
1588         return ret;
1589 }
1590
1591 static int __delete_appsvc_info_from_db(manifest_x *mfx)
1592 {
1593         int ret = 0;
1594         uiapplication_x *uiapplication = mfx->uiapplication;
1595
1596         for(; uiapplication; uiapplication=uiapplication->next) {
1597                 ret = __delete_appsvc_db(uiapplication->appid);
1598                 if (ret <0)
1599                         _LOGE("can not remove_appsvc_db\n");
1600         }
1601
1602         return ret;
1603 }
1604
1605 static int __delete_manifest_info_from_pkgid(const char *pkgid)
1606 {
1607         char *query = NULL;
1608         int ret = -1;
1609
1610         /*Delete from cert table*/
1611         ret = pkgmgrinfo_delete_certinfo(pkgid);
1612         retvm_if(ret < 0, PMINFO_R_ERROR, "Cert Info  DB Delete Failed\n");
1613
1614         /*Delete from Package Info DB*/
1615         query = sqlite3_mprintf("delete from package_info where package=%Q", pkgid);
1616         ret = __exec_query(query);
1617         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "Package Info DB Delete Failed\n");
1618
1619         /*Delete from Package Localized Info*/
1620         sqlite3_free(query);
1621         query = sqlite3_mprintf("delete from package_localized_info where package=%Q", pkgid);
1622         ret = __exec_query(query);
1623         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "Package Localized Info DB Delete Failed\n");
1624
1625         /*Delete from Package Privilege Info*/
1626         sqlite3_free(query);
1627         query = sqlite3_mprintf("delete from package_privilege_info where package=%Q", pkgid);
1628         ret = __exec_query(query);
1629         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "Package Privilege Info DB Delete Failed\n");
1630
1631 catch:
1632         sqlite3_free(query);
1633         return 0;
1634 }
1635
1636
1637 static  int __delete_manifest_info_from_appid(const char *appid)
1638 {
1639         int ret = -1;
1640
1641         ret = __delete_appinfo_from_db("package_app_info", appid);
1642         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1643
1644         ret = __delete_appinfo_from_db("package_app_localized_info", appid);
1645         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1646
1647         ret = __delete_appinfo_from_db("package_app_icon_section_info", appid);
1648         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1649
1650         ret = __delete_appinfo_from_db("package_app_image_info", appid);
1651         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1652
1653         ret = __delete_appinfo_from_db("package_app_app_svc", appid);
1654         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1655
1656         ret = __delete_appinfo_from_db("package_app_app_control", appid);
1657         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1658
1659         ret = __delete_appinfo_from_db("package_app_app_category", appid);
1660         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1661
1662         ret = __delete_appinfo_from_db("package_app_app_metadata", appid);
1663         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1664
1665         ret = __delete_appinfo_from_db("package_app_app_permission", appid);
1666         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1667
1668         ret = __delete_appinfo_from_db("package_app_share_allowed", appid);
1669         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1670
1671         ret = __delete_appinfo_from_db("package_app_share_request", appid);
1672         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1673
1674         ret = __delete_appinfo_from_db("package_app_data_control", appid);
1675         retvm_if(ret < 0, PMINFO_R_ERROR, "Fail to get handle");
1676
1677         return 0;
1678 }
1679
1680
1681 static int __update_preload_condition_in_db()
1682 {
1683         int ret = -1;
1684         char *query = NULL;
1685
1686         query = sqlite3_mprintf("update package_info set package_preload='true'");
1687
1688         ret = __exec_query(query);
1689         sqlite3_free(query);
1690         if (ret == -1)
1691                 _LOGD("Package preload_condition update failed\n");
1692
1693         return ret;
1694 }
1695
1696 int pkgmgr_parser_initialize_db()
1697 {
1698         int ret = -1;
1699         /*Manifest DB*/
1700         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1701         if (ret == -1) {
1702                 _LOGD("package info DB initialization failed\n");
1703                 return ret;
1704         }
1705         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
1706         if (ret == -1) {
1707                 _LOGD("package localized info DB initialization failed\n");
1708                 return ret;
1709         }
1710         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
1711         if (ret == -1) {
1712                 _LOGD("package app app privilege DB initialization failed\n");
1713                 return ret;
1714         }
1715         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
1716         if (ret == -1) {
1717                 _LOGD("package app info DB initialization failed\n");
1718                 return ret;
1719         }
1720         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
1721         if (ret == -1) {
1722                 _LOGD("package app localized info DB initialization failed\n");
1723                 return ret;
1724         }
1725         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
1726         if (ret == -1) {
1727                 _LOGD("package app icon localized info DB initialization failed\n");
1728                 return ret;
1729         }
1730         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
1731         if (ret == -1) {
1732                 _LOGD("package app image info DB initialization failed\n");
1733                 return ret;
1734         }
1735         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
1736         if (ret == -1) {
1737                 _LOGD("package app app control DB initialization failed\n");
1738                 return ret;
1739         }
1740         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
1741         if (ret == -1) {
1742                 _LOGD("package app app category DB initialization failed\n");
1743                 return ret;
1744         }
1745         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
1746         if (ret == -1) {
1747                 _LOGD("package app app category DB initialization failed\n");
1748                 return ret;
1749         }
1750         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
1751         if (ret == -1) {
1752                 _LOGD("package app app permission DB initialization failed\n");
1753                 return ret;
1754         }
1755         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC);
1756         if (ret == -1) {
1757                 _LOGD("package app app svc DB initialization failed\n");
1758                 return ret;
1759         }
1760         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
1761         if (ret == -1) {
1762                 _LOGD("package app share allowed DB initialization failed\n");
1763                 return ret;
1764         }
1765         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
1766         if (ret == -1) {
1767                 _LOGD("package app share request DB initialization failed\n");
1768                 return ret;
1769         }
1770         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
1771         if (ret == -1) {
1772                 _LOGD("package app data control DB initialization failed\n");
1773                 return ret;
1774         }
1775         /*Cert DB*/
1776         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
1777         if (ret == -1) {
1778                 _LOGD("package cert info DB initialization failed\n");
1779                 return ret;
1780         }
1781         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
1782         if (ret == -1) {
1783                 _LOGD("package cert index info DB initialization failed\n");
1784                 return ret;
1785         }
1786         /*reserve DB*/
1787         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PKG_RESERVE);
1788         if (ret == -1) {
1789                 _LOGD("package pkg reserve info DB initialization failed\n");
1790                 return ret;
1791         }
1792         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_RESERVE);
1793         if (ret == -1) {
1794                 _LOGD("package pkg reserve info DB initialization failed\n");
1795                 return ret;
1796         }
1797
1798         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PLUGIN_INFO);
1799         if (ret == -1) {
1800                 _LOGD("package pkg reserve info DB initialization failed\n");
1801                 return ret;
1802         }
1803         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ALIASID);
1804         if (ret == -1) {
1805                 _LOGD("package app aliasId DB initialization failed\n");
1806                 return ret;
1807         }
1808 #ifdef _APPFW_FEATURE_PROFILE_WEARABLE
1809         ret = __init_tables_for_wearable();
1810         if (ret == -1) {
1811                 _LOGD("package pkg reserve info DB initialization failed\n");
1812                 return ret;
1813         }
1814 #endif
1815
1816         return 0;
1817 }
1818
1819 int pkgmgr_parser_check_and_create_db()
1820 {
1821         int ret = -1;
1822         /*Manifest DB*/
1823         ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, PKGMGR_PARSER_DB_FILE);
1824         if (ret) {
1825                 _LOGD("Manifest DB creation Failed\n");
1826                 return -1;
1827         }
1828         /*Cert DB*/
1829         ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, PKGMGR_CERT_DB_FILE);
1830         if (ret) {
1831                 _LOGD("Cert DB creation Failed\n");
1832                 return -1;
1833         }
1834         return 0;
1835 }
1836
1837 void pkgmgr_parser_close_db()
1838 {
1839         sqlite3_close(pkgmgr_parser_db);
1840         sqlite3_close(pkgmgr_cert_db);
1841 }
1842
1843 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1844 {
1845         if (mfx == NULL) {
1846                 _LOGD("manifest pointer is NULL\n");
1847                 return -1;
1848         }
1849         int ret = 0;
1850         ret = pkgmgr_parser_check_and_create_db();
1851         if (ret == -1) {
1852                 _LOGD("Failed to open DB\n");
1853                 return ret;
1854         }
1855         ret = pkgmgr_parser_initialize_db();
1856         if (ret == -1)
1857                 goto err;
1858         /*Begin transaction*/
1859         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1860         if (ret != SQLITE_OK) {
1861                 _LOGD("Failed to begin transaction[%d]\n", ret);
1862                 ret = -1;
1863                 goto err;
1864         }
1865         _LOGD("Transaction Begin\n");
1866         ret = __insert_manifest_info_in_db(mfx);
1867         if (ret == -1) {
1868                 _LOGD("Insert into DB failed. Rollback now\n");
1869                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1870                 if (ret != SQLITE_OK)
1871                         _LOGD("ROLLBACK is fail after insert_disabled_pkg_info_in_db\n");
1872
1873                 ret = -1;
1874                 goto err;
1875         }
1876         /*Commit transaction*/
1877         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1878         if (ret != SQLITE_OK) {
1879                 _LOGD("Failed to commit transaction. Rollback now\n");
1880                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1881                 if (ret != SQLITE_OK)
1882                 _LOGE("Failed to commit transaction. Rollback now\n");
1883
1884                 ret = -1;
1885                 goto err;
1886         }
1887         _LOGD("Transaction Commit and End\n");
1888 err:
1889         pkgmgr_parser_close_db();
1890         return ret;
1891 }
1892
1893 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1894 {
1895         if (mfx == NULL) {
1896                 _LOGD("manifest pointer is NULL\n");
1897                 return -1;
1898         }
1899         int ret = 0;
1900         ret = pkgmgr_parser_check_and_create_db();
1901         if (ret == -1) {
1902                 _LOGD("Failed to open DB\n");
1903                 return ret;
1904         }
1905         ret = pkgmgr_parser_initialize_db();
1906         if (ret == -1)
1907                 goto err;
1908
1909         /*Begin transaction*/
1910         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1911         if (ret != SQLITE_OK) {
1912                 _LOGD("Failed to begin transaction\n");
1913                 ret = -1;
1914                 goto err;
1915         }
1916         _LOGD("Transaction Begin\n");
1917         ret = __delete_manifest_info_from_db(mfx);
1918         if (ret == -1) {
1919                 _LOGD("Delete from DB failed. Rollback now\n");
1920                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1921                 if (ret != SQLITE_OK)
1922                         _LOGD("Failed to commit transaction. Rollback now\n");
1923                 goto err;
1924         }
1925         ret = __insert_manifest_info_in_db(mfx);
1926         if (ret == -1) {
1927                 _LOGD("Insert into DB failed. Rollback now\n");
1928                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1929                 if (ret != SQLITE_OK)
1930                         _LOGD("Failed to commit transaction. Rollback now\n");
1931                 goto err;
1932         }
1933
1934         /*Commit transaction*/
1935         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1936         if (ret != SQLITE_OK) {
1937                 _LOGD("Failed to commit transaction. Rollback now\n");
1938                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1939                 if (ret != SQLITE_OK)
1940                         _LOGD("Failed to commit transaction. Rollback now\n");
1941
1942                 ret = -1;
1943                 goto err;
1944         }
1945         _LOGD("Transaction Commit and End\n");
1946 err:
1947         pkgmgr_parser_close_db();
1948         return ret;
1949 }
1950
1951 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
1952 {
1953         if (mfx == NULL) {
1954                 _LOGD("manifest pointer is NULL\n");
1955                 return -1;
1956         }
1957         int ret = 0;
1958         ret = pkgmgr_parser_check_and_create_db();
1959         if (ret == -1) {
1960                 _LOGD("Failed to open DB\n");
1961                 return ret;
1962         }
1963         /*Begin transaction*/
1964         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1965         if (ret != SQLITE_OK) {
1966                 _LOGD("Failed to begin transaction\n");
1967                 ret = -1;
1968                 goto err;
1969         }
1970         _LOGD("Transaction Begin\n");
1971         ret = __delete_manifest_info_from_db(mfx);
1972         if (ret == -1) {
1973                 _LOGD("Delete from DB failed. Rollback now\n");
1974                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1975                 if (ret != SQLITE_OK)
1976                         _LOGE("Failed to commit transaction, Rollback now\n");
1977
1978                 goto err;
1979         }
1980         /*Commit transaction*/
1981         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1982         if (ret != SQLITE_OK) {
1983                 _LOGD("Failed to commit transaction, Rollback now\n");
1984                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1985                 if (ret != SQLITE_OK)
1986                         _LOGE("Failed to commit transaction, Rollback now\n");
1987
1988                 ret = -1;
1989                 goto err;
1990         }
1991
1992         ret = __delete_appsvc_info_from_db(mfx);
1993         if (ret == -1)
1994                 _LOGD("Removing appsvc_db failed\n");
1995         else
1996                 _LOGD("Removing appsvc_db Success\n");
1997
1998         _LOGD("Transaction Commit and End\n");
1999 err:
2000         pkgmgr_parser_close_db();
2001         return ret;
2002 }
2003
2004 API int pkgmgr_parser_update_preload_info_in_db()
2005 {
2006         int ret = 0;
2007         ret = pkgmgr_parser_check_and_create_db();
2008         if (ret == -1) {
2009                 _LOGD("Failed to open DB\n");
2010                 return ret;
2011         }
2012         /*Begin transaction*/
2013         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2014         if (ret != SQLITE_OK) {
2015                 _LOGD("Failed to begin transaction\n");
2016                 ret = -1;
2017                 goto err;
2018         }
2019         _LOGD("Transaction Begin\n");
2020         ret = __update_preload_condition_in_db();
2021         if (ret == -1) {
2022                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2023                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2024                 if (ret != SQLITE_OK)
2025                         _LOGE("Failed to commit transaction, Rollback now\n");
2026
2027                 goto err;
2028         }
2029         /*Commit transaction*/
2030         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2031         if (ret != SQLITE_OK) {
2032                 _LOGD("Failed to commit transaction, Rollback now\n");
2033                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2034                 if (ret != SQLITE_OK)
2035                         _LOGE("Failed to commit transaction, Rollback now\n");
2036
2037                 ret = -1;
2038                 goto err;
2039         }
2040         _LOGD("Transaction Commit and End\n");
2041 err:
2042         pkgmgr_parser_close_db();
2043         return ret;
2044 }
2045
2046 API int pkgmgr_parser_delete_pkgid_info_from_db(const char *pkgid)
2047 {
2048         retvm_if(pkgid == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2049
2050         int ret = 0;
2051
2052         /*open db*/
2053         ret = pkgmgr_parser_check_and_create_db();
2054         retvm_if(ret < 0, PMINFO_R_ERROR, "Failed to open DB\n");
2055
2056         /*Begin transaction*/
2057         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2058         if (ret != SQLITE_OK) {
2059                 _LOGE("Failed to begin transaction\n");
2060                 ret = -1;
2061                 goto err;
2062         }
2063
2064         _LOGD("Start to Delete pkgid[%s] info from db\n", pkgid);
2065
2066         /*delete pkg info*/
2067         ret = __delete_manifest_info_from_pkgid(pkgid);
2068         if (ret == -1) {
2069                 _LOGD("Delete from DB failed. Rollback now\n");
2070                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2071                 if (ret != SQLITE_OK)
2072                         _LOGE("Failed to begin transaction\n");
2073                 goto err;
2074         }
2075
2076         /*Commit transaction*/
2077         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2078         if (ret != SQLITE_OK) {
2079                 _LOGD("Failed to commit transaction, Rollback now\n");
2080                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2081                 if (ret != SQLITE_OK)
2082                         _LOGE("Failed to commit transaction, Rollback now\n");
2083
2084                 ret = -1;
2085                 goto err;
2086         }
2087
2088 err:
2089         pkgmgr_parser_close_db();
2090         return ret;
2091 }
2092
2093 API int pkgmgr_parser_delete_appid_info_from_db(const char *appid)
2094 {
2095         retvm_if(appid == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2096
2097         int ret = 0;
2098
2099         /*open db*/
2100         ret = pkgmgr_parser_check_and_create_db();
2101         retvm_if(ret < 0, PMINFO_R_ERROR, "Failed to open DB\n");
2102
2103         /*Begin transaction*/
2104         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2105         if (ret != SQLITE_OK) {
2106                 _LOGE("Failed to begin transaction\n");
2107                 ret = -1;
2108                 goto err;
2109         }
2110
2111         _LOGD("Start to Delete appid[%s] info from db\n", appid);
2112
2113         /*delete appinfo db*/
2114         ret = __delete_manifest_info_from_appid(appid);
2115         if (ret < 0)
2116                 _LOGE("Fail to delete appid[%s] info from pkgmgr db\n", appid);
2117
2118         /*delete appsvc db*/
2119         ret = __delete_appsvc_db(appid);
2120         if (ret < 0)
2121                 _LOGE("Fail to delete appid[%s] info from appsvc db\n", appid);
2122
2123         /*Commit transaction*/
2124         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2125         if (ret != SQLITE_OK) {
2126                 _LOGD("Failed to commit transaction, Rollback now\n");
2127                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2128                 if (ret != SQLITE_OK)
2129                         _LOGE("Failed to commit transaction, Rollback now\n");
2130
2131                 ret = -1;
2132                 goto err;
2133         }
2134
2135 err:
2136         pkgmgr_parser_close_db();
2137         return ret;
2138 }
2139
2140 API int pkgmgr_parser_update_enabled_pkg_info_in_db(const char *pkgid)
2141 {
2142         retvm_if(pkgid == NULL, PMINFO_R_ERROR, "pkgid is NULL.");
2143
2144         int ret = 0;
2145
2146         _LOGD("pkgmgr_parser_update_enabled_pkg_info_in_db(%s)\n", pkgid);
2147
2148         /*open db*/
2149         ret = pkgmgr_parser_check_and_create_db();
2150         retvm_if(ret < 0, PMINFO_R_ERROR, "pkgmgr_parser_check_and_create_db(%s) failed.\n", pkgid);
2151
2152         /*Begin transaction*/
2153         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2154         if (ret != SQLITE_OK) {
2155                 _LOGE("sqlite3_exec(BEGIN EXCLUSIVE) failed.\n");
2156                 ret = -1;
2157                 goto err;
2158         }
2159
2160         /*update pkg info*/
2161         ret = __update_pkg_info_for_disable(pkgid, false);
2162         if (ret == -1) {
2163                 _LOGD("__update_pkg_info_for_disable(%s) failed.\n", pkgid);
2164                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2165                 if (ret != SQLITE_OK)
2166                         _LOGE("sqlite3_exec(ROLLBACK) failed.\n");
2167                 goto err;
2168         }
2169
2170         /*Commit transaction*/
2171         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2172         if (ret != SQLITE_OK) {
2173                 _LOGD("sqlite3_exec(COMMIT) failed.\n");
2174                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2175                 if (ret != SQLITE_OK)
2176                         _LOGE("sqlite3_exec(ROLLBACK) failed.\n");
2177
2178                 ret = -1;
2179                 goto err;
2180         }
2181
2182 err:
2183         pkgmgr_parser_close_db();
2184         return ret;
2185 }
2186
2187 API int pkgmgr_parser_update_disabled_pkg_info_in_db(const char *pkgid)
2188 {
2189         retvm_if(pkgid == NULL, PMINFO_R_ERROR, "pkgid is NULL.");
2190
2191         int ret = 0;
2192
2193         _LOGD("pkgmgr_parser_update_disabled_pkg_info_in_db(%s)\n", pkgid);
2194
2195         /*open db*/
2196         ret = pkgmgr_parser_check_and_create_db();
2197         retvm_if(ret < 0, PMINFO_R_ERROR, "pkgmgr_parser_check_and_create_db(%s) failed.\n", pkgid);
2198
2199         /*Begin transaction*/
2200         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2201         if (ret != SQLITE_OK) {
2202                 _LOGE("sqlite3_exec(BEGIN EXCLUSIVE) failed.\n");
2203                 ret = -1;
2204                 goto err;
2205         }
2206
2207         /*update pkg info*/
2208         ret = __update_pkg_info_for_disable(pkgid, true);
2209         if (ret == -1) {
2210                 _LOGD("__update_pkg_info_for_disable(%s) failed.\n", pkgid);
2211                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2212                 if (ret != SQLITE_OK)
2213                         _LOGE("sqlite3_exec(ROLLBACK) failed.\n");
2214                 goto err;
2215         }
2216
2217         /*Commit transaction*/
2218         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2219         if (ret != SQLITE_OK) {
2220                 _LOGD("sqlite3_exec(COMMIT) failed.\n");
2221                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2222                 if (ret != SQLITE_OK)
2223                         _LOGE("sqlite3_exec(ROLLBACK) failed.\n");
2224
2225                 ret = -1;
2226                 goto err;
2227         }
2228
2229 err:
2230         pkgmgr_parser_close_db();
2231         return ret;
2232 }
2233
2234 int pkgmgr_parser_insert_app_aliasid_info_in_db(void)
2235 {
2236         FILE *ini_file = NULL;
2237         char *match = NULL;
2238         char *tizen_id = NULL;
2239         char *app_id = NULL;
2240         int len = 0;
2241         char buf[ MAX_ALIAS_INI_ENTRY ] = {0};
2242         int ret = PM_PARSER_R_OK;
2243         sqlite3 *pkgmgr_db = NULL;
2244         char *query = NULL;
2245
2246         /*Open the alias.ini file*/
2247         ini_file  = fopen(USR_APPSVC_ALIAS_INI,"r");
2248         if(ini_file){
2249                 /*Open the pkgmgr DB*/
2250                 ret = db_util_open_with_options(MANIFEST_DB, &pkgmgr_db, SQLITE_OPEN_READWRITE, NULL);
2251                 if (ret != SQLITE_OK) {
2252                         _LOGE("connect db [%s] failed!\n", MANIFEST_DB);
2253                         fclose(ini_file);
2254                         return PM_PARSER_R_ERROR;
2255
2256                 }
2257                 /*Begin Transaction*/
2258                 ret = sqlite3_exec(pkgmgr_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2259                 tryvm_if(ret != SQLITE_OK, ret = PM_PARSER_R_ERROR, "Failed to begin transaction");
2260
2261                 _LOGD("Transaction Begin\n");
2262
2263                 /*delete all the previous entries from package_app_aliasid tables*/
2264                 query = sqlite3_mprintf("delete from package_app_aliasid");
2265                 ret = __exec_db_query(pkgmgr_db, query, NULL, NULL);
2266                 sqlite3_free(query);
2267                 tryvm_if(ret != SQLITE_OK,ret = PM_PARSER_R_ERROR,"sqlite exec failed to delete entries from package_app_aliasid!!");
2268
2269                 while ( fgets ( buf, sizeof(buf), ini_file ) != NULL ) /* read a line */
2270                 {
2271                         match = strstr(buf,"=");
2272                         if(match == NULL)
2273                                 continue;
2274                         /*format of alias.ini entry is 'tizen_app_id=alias_id'*/
2275                         /*get the tizen appid id*/
2276                         len = strlen(buf)-strlen(match);
2277                         tizen_id = malloc(len + 1);
2278                         tryvm_if(tizen_id == NULL, ret = PM_PARSER_R_ERROR,"Malloc failed!!");
2279                         memset(tizen_id,'\0',len +1);
2280                         strncpy(tizen_id,buf,len);
2281                         tizen_id[len]='\0';
2282                         __str_trim(tizen_id);
2283
2284                         /*get the corresponding alias id*/
2285                         len = strlen(match)-1;
2286                         app_id = malloc(len + 1);
2287                         tryvm_if(app_id == NULL, ret = PM_PARSER_R_ERROR,"Malloc failed!!");
2288                         memset(app_id,'\0',len + 1);
2289                         strncpy(app_id, match + 1, len);
2290                         app_id[len]='\0';
2291                         __str_trim(app_id);
2292
2293                         /* Insert the data into DB*/
2294                         query = sqlite3_mprintf("insert into package_app_aliasid(app_id, alias_id) values(%Q ,%Q)",tizen_id,app_id);
2295                         ret = __exec_db_query(pkgmgr_db, query, NULL, NULL);
2296                         sqlite3_free(query);
2297                         tryvm_if(ret != SQLITE_OK,ret = PM_PARSER_R_ERROR, "sqlite exec failed to insert entries into package_app_aliasid table!!");
2298
2299                         FREE_AND_NULL(tizen_id);
2300                         FREE_AND_NULL(app_id);
2301                         memset(buf,'\0',MAX_ALIAS_INI_ENTRY);
2302                 }
2303
2304                 /*Commit transaction*/
2305                 ret = sqlite3_exec(pkgmgr_db, "COMMIT", NULL, NULL, NULL);
2306                 if (ret != SQLITE_OK) {
2307                         _LOGE("Failed to commit transaction, Rollback now\n");
2308                         ret = sqlite3_exec(pkgmgr_db, "ROLLBACK", NULL, NULL, NULL);
2309                         if (ret != SQLITE_OK)
2310                                 _LOGE("Failed to Rollback\n");
2311
2312                         ret = PM_PARSER_R_ERROR;
2313                         goto catch;
2314                 }
2315                 _LOGE("Transaction Commit and End\n");
2316                 ret =  PM_PARSER_R_OK;
2317         }else{
2318                 perror(USR_APPSVC_ALIAS_INI);
2319                 ret = -1;
2320         }
2321
2322 catch:
2323         if(ini_file)
2324                 fclose ( ini_file);
2325
2326         FREE_AND_NULL(tizen_id);
2327         FREE_AND_NULL(app_id);
2328         sqlite3_close(pkgmgr_db);
2329         return ret;
2330
2331
2332 }
2333
2334 int pkgmgr_parser_update_app_aliasid_info_in_db(void)
2335 {
2336         FILE *ini_file = NULL;
2337         char *match = NULL;
2338         char *tizen_id = NULL;
2339         char *app_id = NULL;
2340         int len = 0;
2341         char buf[ MAX_ALIAS_INI_ENTRY ] = {0};
2342         int ret = PM_PARSER_R_OK;
2343         sqlite3 *pkgmgr_db = NULL;
2344         char *query = NULL;
2345
2346         /*Open the alias.ini file*/
2347         ini_file  = fopen(OPT_APPSVC_ALIAS_INI,"r");
2348         if(ini_file){
2349                 /*Open the pkgmgr DB*/
2350                 ret = db_util_open_with_options(MANIFEST_DB, &pkgmgr_db, SQLITE_OPEN_READWRITE, NULL);
2351                 if (ret != SQLITE_OK) {
2352                         _LOGE("connect db [%s] failed!\n", MANIFEST_DB);
2353                         fclose(ini_file);
2354                         return PM_PARSER_R_ERROR;
2355
2356                 }
2357                 /*Begin Transaction*/
2358                 ret = sqlite3_exec(pkgmgr_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2359                 tryvm_if(ret != SQLITE_OK, ret = PM_PARSER_R_ERROR, "Failed to begin transaction");
2360
2361                 _LOGD("Transaction Begin\n");
2362
2363                 while ( fgets ( buf, sizeof(buf), ini_file ) != NULL ) /* read a line */
2364                 {
2365                         match = strstr(buf,"=");
2366                         if(match == NULL)
2367                                 continue;
2368                         /*format of alias.ini entry is 'tizen_app_id=alias_id'*/
2369                         /*get the tizen appid id*/
2370                         len = strlen(buf)-strlen(match);
2371                         tizen_id = malloc(len + 1);
2372                         tryvm_if(tizen_id == NULL, ret = PM_PARSER_R_ERROR,"Malloc failed!!");
2373                         memset(tizen_id,'\0',len +1);
2374                         strncpy(tizen_id,buf,len);
2375                         tizen_id[len]='\0';
2376                         __str_trim(tizen_id);
2377
2378                         /*get the corresponding alias id*/
2379                         len = strlen(match)-1;
2380                         app_id = malloc(len + 1);
2381                         tryvm_if(app_id == NULL, ret = PM_PARSER_R_ERROR,"Malloc failed!!");
2382                         memset(app_id,'\0',len + 1);
2383                         strncpy(app_id, match + 1, len);
2384                         app_id[len]='\0';
2385                         __str_trim(app_id);
2386
2387                         /* Insert the data into DB*/
2388                         query = sqlite3_mprintf("insert or replace into package_app_aliasid(app_id, alias_id) values(%Q ,%Q)",tizen_id,app_id);
2389                         ret = __exec_db_query(pkgmgr_db, query, NULL, NULL);
2390                         sqlite3_free(query);
2391                         tryvm_if(ret != SQLITE_OK,ret = PM_PARSER_R_ERROR, "sqlite exec failed to insert entries into package_app_aliasid table!!");
2392
2393                         FREE_AND_NULL(tizen_id);
2394                         FREE_AND_NULL(app_id);
2395                         memset(buf,'\0',MAX_ALIAS_INI_ENTRY);
2396                 }
2397
2398                 /*Commit transaction*/
2399                 ret = sqlite3_exec(pkgmgr_db, "COMMIT", NULL, NULL, NULL);
2400                 if (ret != SQLITE_OK) {
2401                         _LOGE("Failed to commit transaction, Rollback now\n");
2402                         ret = sqlite3_exec(pkgmgr_db, "ROLLBACK", NULL, NULL, NULL);
2403                         if (ret != SQLITE_OK)
2404                                 _LOGE("Failed to Rollback\n");
2405
2406                         ret = PM_PARSER_R_ERROR;
2407                         goto catch;
2408                 }
2409                 _LOGE("Transaction Commit and End\n");
2410                 ret =  PM_PARSER_R_OK;
2411         }else{
2412                 perror(OPT_APPSVC_ALIAS_INI);
2413                 ret = -1;
2414         }
2415
2416 catch:
2417         if(ini_file)
2418                 fclose ( ini_file);
2419
2420         FREE_AND_NULL(tizen_id);
2421         FREE_AND_NULL(app_id);
2422         sqlite3_close(pkgmgr_db);
2423         return ret;
2424
2425
2426 }
2427