tizen 2.3.1 release
[framework/appfw/pkgmgr-info.git] / parser / pkgmgr_parser_feature.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 #define _GNU_SOURCE
23 #include <dlfcn.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <time.h>
30 #include <libxml/parser.h>
31 #include <libxml/xmlreader.h>
32 #include <libxml/xmlschemas.h>
33 #include <vconf.h>
34 #include <glib.h>
35 #include <db-util.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <dlfcn.h>
42
43 #include "pkgmgr-info.h"
44 #include "pkgmgr_parser.h"
45 #include "pkgmgr_parser_internal.h"
46 #include "pkgmgr_parser_db.h"
47 #include "pkgmgr_parser_db_util.h"
48 #include "pkgmgr_parser_plugin.h"
49
50 #include "pkgmgrinfo_debug.h"
51 #include "pkgmgr_parser_feature.h"
52
53 #ifdef LOG_TAG
54 #undef LOG_TAG
55 #endif
56 #define LOG_TAG "PKGMGR_PARSER"
57
58 #define MAX_QUERY_LEN           4096
59
60 sqlite3 *pkgmgr_parser_db;
61 sqlite3 *pkgmgr_cert_db;
62
63 #define QUERY_CREATE_TABLE_DISABLED_PACKAGE_INFO "create table if not exists disabled_package_info " \
64                                                 "(package text primary key not null, " \
65                                                 "package_type text DEFAULT 'rpm', " \
66                                                 "package_version text, " \
67                                                 "install_location text, " \
68                                                 "package_size text, " \
69                                                 "package_removable text DEFAULT 'true', " \
70                                                 "package_preload text DEFAULT 'false', " \
71                                                 "package_readonly text DEFAULT 'false', " \
72                                                 "package_update text DEFAULT 'false', " \
73                                                 "package_appsetting text DEFAULT 'false', " \
74                                                 "package_nodisplay text DEFAULT 'false', " \
75                                                 "package_system text DEFAULT 'false', " \
76                                                 "author_name text, " \
77                                                 "author_email text, " \
78                                                 "author_href text," \
79                                                 "installed_time text," \
80                                                 "installed_storage text," \
81                                                 "storeclient_id text," \
82                                                 "mainapp_id text," \
83                                                 "package_url text," \
84                                                 "root_path text," \
85                                                 "csc_path text)"
86
87 #define QUERY_CREATE_TABLE_DISABLED_PACKAGE_APP_INFO "create table if not exists disabled_package_app_info " \
88                                                 "(app_id text primary key not null, " \
89                                                 "app_component text, " \
90                                                 "app_exec text, " \
91                                                 "app_nodisplay text DEFAULT 'false', " \
92                                                 "app_type text, " \
93                                                 "app_onboot text DEFAULT 'false', " \
94                                                 "app_multiple text DEFAULT 'false', " \
95                                                 "app_autorestart text DEFAULT 'false', " \
96                                                 "app_taskmanage text DEFAULT 'false', " \
97                                                 "app_enabled text DEFAULT 'true', " \
98                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
99                                                 "app_screenreader text DEFAULT 'use-system-setting', " \
100                                                 "app_mainapp text, " \
101                                                 "app_recentimage text, " \
102                                                 "app_launchcondition text, " \
103                                                 "app_indicatordisplay text DEFAULT 'true', " \
104                                                 "app_portraitimg text, " \
105                                                 "app_landscapeimg text, " \
106                                                 "app_guestmodevisibility text DEFAULT 'true', " \
107                                                 "app_permissiontype text DEFAULT 'normal', " \
108                                                 "app_preload text DEFAULT 'false', " \
109                                                 "app_submode text DEFAULT 'false', " \
110                                                 "app_submode_mainid text, " \
111                                                 "app_installed_storage text, " \
112                                                 "component_type text, " \
113                                                 "package text not null, " \
114                                                 "FOREIGN KEY(package) " \
115                                                 "REFERENCES package_info(package) " \
116                                                 "ON DELETE CASCADE)"
117
118 #define QUERY_CREATE_TABLE_DISABLED_PACKAGE_LOCALIZED_INFO "create table if not exists disabled_package_localized_info " \
119                                                         "(package text not null, " \
120                                                         "package_locale text DEFAULT 'No Locale', " \
121                                                         "package_label text, " \
122                                                         "package_icon text, " \
123                                                         "package_description text, " \
124                                                         "package_license text, " \
125                                                         "package_author, " \
126                                                         "PRIMARY KEY(package, package_locale), " \
127                                                         "FOREIGN KEY(package) " \
128                                                         "REFERENCES package_info(package) " \
129                                                         "ON DELETE CASCADE)"
130
131 #define QUERY_CREATE_TABLE_DISABLED_PACKAGE_APP_LOCALIZED_INFO "create table if not exists disabled_package_app_localized_info " \
132                                                 "(app_id text not null, " \
133                                                 "app_locale text DEFAULT 'No Locale', " \
134                                                 "app_label text, " \
135                                                 "app_icon text, " \
136                                                 "package text not null, " \
137                                                 "PRIMARY KEY(app_id,app_locale) " \
138                                                 "FOREIGN KEY(app_id) " \
139                                                 "REFERENCES package_app_info(app_id) " \
140                                                 "ON DELETE CASCADE)"
141
142 #define QUERY_CREATE_TABLE_DISABLED_PACKAGE_APP_APP_CATEGORY "create table if not exists disabled_package_app_app_category " \
143                                                 "(app_id text not null, " \
144                                                 "category text not null, " \
145                                                 "package text not null, " \
146                                                 "PRIMARY KEY(app_id,category) " \
147                                                 "FOREIGN KEY(app_id) " \
148                                                 "REFERENCES package_app_info(app_id) " \
149                                                 "ON DELETE CASCADE)"
150
151
152 #define QUERY_CREATE_TABLE_DISABLED_PACKAGE_APP_APP_METADATA "create table if not exists disabled_package_app_app_metadata " \
153                                                 "(app_id text not null, " \
154                                                 "md_key text not null, " \
155                                                 "md_value text not null, " \
156                                                 "package text not null, " \
157                                                 "PRIMARY KEY(app_id, md_key, md_value) " \
158                                                 "FOREIGN KEY(app_id) " \
159                                                 "REFERENCES package_app_info(app_id) " \
160                                                 "ON DELETE CASCADE)"
161
162 int __init_tables_for_wearable(void)
163 {
164         int ret = 0;
165
166         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_DISABLED_PACKAGE_INFO);
167         if (ret == -1) {
168                 _LOGD("package pkg reserve info DB initialization failed\n");
169                 return ret;
170         }
171
172         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_DISABLED_PACKAGE_APP_INFO);
173         if (ret == -1) {
174                 _LOGD("package pkg reserve info DB initialization failed\n");
175                 return ret;
176         }
177
178         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_DISABLED_PACKAGE_LOCALIZED_INFO);
179         if (ret == -1) {
180                 _LOGD("package pkg reserve info DB initialization failed\n");
181                 return ret;
182         }
183
184         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_DISABLED_PACKAGE_APP_LOCALIZED_INFO);
185         if (ret == -1) {
186                 _LOGD("package pkg reserve info DB initialization failed\n");
187                 return ret;
188         }
189
190         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_DISABLED_PACKAGE_APP_APP_CATEGORY);
191         if (ret == -1) {
192                 _LOGD("package pkg reserve info DB initialization failed\n");
193                 return ret;
194         }
195
196         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_DISABLED_PACKAGE_APP_APP_METADATA);
197
198         if (ret == -1) {
199                 _LOGD("package pkg reserve info DB initialization failed\n");
200                 return ret;
201         }
202
203         return ret;
204 }
205
206 static int __insert_disabled_ui_mainapp_info(manifest_x *mfx)
207 {
208         uiapplication_x *up = mfx->uiapplication;
209         int ret = -1;
210         char *query = NULL;
211         while(up != NULL)
212         {
213                 query = sqlite3_mprintf("update disabled_package_app_info set app_mainapp=%Q where app_id=%Q", up->mainapp, up->appid);
214
215                 ret = __exec_query(query);
216                 sqlite3_free(query);
217                 if (ret == -1) {
218                         _LOGD("Package UiApp Info DB Insert Failed\n");
219                         return -1;
220                 }
221                 if (strcasecmp(up->mainapp, "True") == 0) {
222                         FREE_AND_NULL(mfx->mainapp_id);
223                         mfx->mainapp_id = strdup(up->appid);
224                 }
225
226                 up = up->next;
227         }
228
229         if (mfx->mainapp_id == NULL){
230                 if (mfx->uiapplication && mfx->uiapplication->appid) {
231                         query = sqlite3_mprintf("update disabled_package_app_info set app_mainapp='true' where app_id=%Q", mfx->uiapplication->appid);
232                 } else {
233                         _LOGD("Not valid appid\n");
234                         return -1;
235                 }
236
237                 ret = __exec_query(query);
238                 sqlite3_free(query);
239                 if (ret == -1) {
240                         _LOGD("Package UiApp Info DB Insert Failed\n");
241                         return -1;
242                 }
243
244                 free((void *)mfx->uiapplication->mainapp);
245                 mfx->uiapplication->mainapp= strdup("true");
246                 mfx->mainapp_id = strdup(mfx->uiapplication->appid);
247         }
248
249         query = sqlite3_mprintf("update disabled_package_info set mainapp_id=%Q where package=%Q", mfx->mainapp_id, mfx->package);
250         ret = __exec_query(query);
251         sqlite3_free(query);
252         if (ret == -1) {
253                 _LOGD("Package Info DB update Failed\n");
254                 return -1;
255         }
256
257         return 0;
258 }
259
260 static int __insert_disabled_uiapplication_info(manifest_x *mfx)
261 {
262         uiapplication_x *up = mfx->uiapplication;
263         int ret = -1;
264         char *query = NULL;
265         while(up != NULL)
266         {
267                 query = sqlite3_mprintf("insert into disabled_package_app_info(app_id, app_component, app_exec, app_ambient_support, app_nodisplay, app_type, app_onboot, " \
268                         "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_screenreader, app_mainapp , app_recentimage, " \
269                         "app_launchcondition, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, "\
270                         "app_preload, app_submode, app_submode_mainid, app_installed_storage, component_type, package) " \
271                         "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)",\
272                          up->appid,
273                          "uiapp",
274                          up->exec,
275                          up->ambient_support,
276                          up->nodisplay,
277                          up->type,
278                          PKGMGR_PARSER_EMPTY_STR,
279                          up->multiple,
280                          PKGMGR_PARSER_EMPTY_STR,
281                          up->taskmanage,
282                          up->enabled,
283                          up->hwacceleration,
284                          up->screenreader,
285                          up->mainapp,
286                          __get_str(up->recentimage),
287                          up->launchcondition,
288                          up->indicatordisplay,
289                          __get_str(up->portraitimg),
290                          __get_str(up->landscapeimg),
291                          up->guestmode_visibility,
292                          up->permission_type,
293                          mfx->preload,
294                          up->submode,
295                          __get_str(up->submode_mainid),
296                          mfx->installed_storage,
297                          up->component_type,
298                          mfx->package);
299
300                 ret = __exec_query(query);
301                 sqlite3_free(query);
302                 if (ret == -1) {
303                         _LOGD("Package UiApp Info DB Insert Failed\n");
304                         return -1;
305                 }
306                 up = up->next;
307         }
308         return 0;
309 }
310
311 static void __insert_disabled_uiapplication_locale_info(gpointer data, gpointer userdata)
312 {
313         int ret = -1;
314         char *label = NULL;
315         char *icon = NULL;
316         char *query = NULL;
317
318         uiapplication_x *up = (uiapplication_x*)userdata;
319         label_x *lbl = up->label;
320         icon_x *icn = up->icon;
321
322         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
323         if (!label && !icon)
324                 return;
325         query = sqlite3_mprintf("insert into disabled_package_app_localized_info(app_id, app_locale, " \
326                 "app_label, app_icon, package) values " \
327                 "(%Q, %Q, %Q, %Q, %Q)", up->appid, (char*)data,
328                 label, icon, up->package);
329         ret = __exec_query(query);
330         sqlite3_free(query);
331         if (ret == -1)
332                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
333
334         /*insert ui app locale info to pkg locale to get mainapp data */
335         if (strcasecmp(up->mainapp, "true")==0) {
336                 char *update_query = NULL;
337                 update_query = sqlite3_mprintf("insert into disabled_package_localized_info(package, package_locale, " \
338                         "package_label, package_icon, package_description, package_license, package_author) values " \
339                         "(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
340                         up->package,
341                         (char*)data,
342                         label,
343                         icon,
344                         PKGMGR_PARSER_EMPTY_STR,
345                         PKGMGR_PARSER_EMPTY_STR,
346                         PKGMGR_PARSER_EMPTY_STR);
347
348                 ret = __exec_query_no_msg(update_query);
349                 sqlite3_free(update_query);
350
351                 if (icon != NULL) {
352                         update_query = sqlite3_mprintf("update package_localized_info set package_icon=%Q " \
353                                 "where package=%Q and package_locale=%Q", icon, up->package, (char*)data);
354                         ret = __exec_query_no_msg(update_query);
355                         sqlite3_free(update_query);
356                 }
357         }
358 }
359
360 static int __insert_disabled_uiapplication_appcategory_info(manifest_x *mfx)
361 {
362         uiapplication_x *up = mfx->uiapplication;
363         category_x *ct = NULL;
364         int ret = -1;
365         char *query = NULL;
366         while(up != NULL)
367         {
368                 ct = up->category;
369                 while (ct != NULL)
370                 {
371                         query = sqlite3_mprintf("insert into disabled_package_app_app_category(app_id, category, package) " \
372                                 "values(%Q, %Q, %Q)",\
373                                  up->appid, ct->name, up->package);
374                         ret = __exec_query(query);
375                         sqlite3_free(query);
376                         if (ret == -1) {
377                                 _LOGD("Package UiApp Category Info DB Insert Failed\n");
378                                 return -1;
379                         }
380                         ct = ct->next;
381                 }
382                 up = up->next;
383         }
384         return 0;
385 }
386
387 static int __insert_disabled_uiapplication_appmetadata_info(manifest_x *mfx)
388 {
389         uiapplication_x *up = mfx->uiapplication;
390         metadata_x *md = NULL;
391         int ret = -1;
392         char *query = NULL;
393         while(up != NULL)
394         {
395                 md = up->metadata;
396                 while (md != NULL)
397                 {
398                         if (md->key) {
399                                 query = sqlite3_mprintf("insert into disabled_package_app_app_metadata(app_id, md_key, md_value, package) " \
400                                         "values(%Q, %Q, %Q, %Q)",\
401                                          up->appid, md->key, md->value, up->package);
402                                 ret = __exec_query(query);
403                                 sqlite3_free(query);
404                                 if (ret == -1) {
405                                         _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
406                                         return -1;
407                                 }
408                         }
409                         md = md->next;
410                 }
411                 up = up->next;
412         }
413         return 0;
414 }
415
416 static void __insert_disabled_pkglocale_info(gpointer data, gpointer userdata)
417 {
418         int ret = -1;
419         char *label = NULL;
420         char *icon = NULL;
421         char *description = NULL;
422         char *license = NULL;
423         char *author = NULL;
424         char *query = NULL;
425
426         manifest_x *mfx = (manifest_x *)userdata;
427         label_x *lbl = mfx->label;
428         license_x *lcn = mfx->license;
429         icon_x *icn = mfx->icon;
430         description_x *dcn = mfx->description;
431         author_x *ath = mfx->author;
432
433         __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
434         if (!label && !description && !icon && !license && !author)
435                 return;
436
437         query = sqlite3_mprintf("insert into disabled_package_localized_info(package, package_locale, " \
438                 "package_label, package_icon, package_description, package_license, package_author) values " \
439                 "(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
440                 mfx->package,
441                 (char*)data,
442                 label,
443                 icon,
444                 __get_str(description),
445                 __get_str(license),
446                 __get_str(author));
447
448         ret = __exec_query(query);
449         sqlite3_free(query);
450         if (ret == -1)
451                 _LOGD("Package Localized Info DB Insert failed\n");
452 }
453
454 static int __insert_disabled_pkg_info_in_db(manifest_x *mfx)
455 {
456         label_x *lbl = mfx->label;
457         license_x *lcn = mfx->license;
458         icon_x *icn = mfx->icon;
459         description_x *dcn = mfx->description;
460         author_x *ath = mfx->author;
461         uiapplication_x *up = mfx->uiapplication;
462
463         char *query = NULL;
464         char root[MAX_QUERY_LEN] = { '\0' };
465         int ret = -1;
466         char *type = NULL;
467         char *path = NULL;
468         const char *auth_name = NULL;
469         const char *auth_email = NULL;
470         const char *auth_href = NULL;
471
472         GList *pkglocale = NULL;
473         GList *applocale = NULL;
474
475         if (ath) {
476                 if (ath->text)
477                         auth_name = ath->text;
478                 if (ath->email)
479                         auth_email = ath->email;
480                 if (ath->href)
481                         auth_href = ath->href;
482         }
483
484         /*Insert in the package_info DB*/
485         if (mfx->type)
486                 type = strdup(mfx->type);
487         else
488                 type = strdup("rpm");
489         /*Insert in the package_info DB*/
490         if (mfx->root_path)
491                 path = strdup(mfx->root_path);
492         else{
493                 if (type && strcmp(type,"rpm")==0)
494                         snprintf(root, MAX_QUERY_LEN - 1, "/usr/apps/%s", mfx->package);
495                 else
496                         snprintf(root, MAX_QUERY_LEN - 1, "/opt/usr/apps/%s", mfx->package);
497
498                 path = strdup(root);
499         }
500         query = sqlite3_mprintf("insert into disabled_package_info(package, package_type, package_version, install_location, package_size, " \
501                 "package_removable, package_preload, package_readonly, package_update, package_appsetting, package_nodisplay, package_system," \
502                 "author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path, csc_path) " \
503                 "values(%Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q)",\
504                  mfx->package,
505                  type,
506                  mfx->version,
507                  __get_str(mfx->installlocation),
508                  __get_str(mfx->package_size),
509                  mfx->removable,
510                  mfx->preload,
511                  mfx->readonly,
512                  mfx->update,
513                  mfx->appsetting,
514                  mfx->nodisplay_setting,
515                  mfx->system,
516                  __get_str(auth_name),
517                  __get_str(auth_email),
518                  __get_str(auth_href),
519                  mfx->installed_time,
520                  mfx->installed_storage,
521                  __get_str(mfx->storeclient_id),
522                  mfx->mainapp_id,
523                  __get_str(mfx->package_url),
524                  path,
525                  __get_str(mfx->csc_path));
526
527         ret = __exec_query(query);
528         sqlite3_free(query);
529         if (ret == -1) {
530                 _LOGD("Package Info DB Insert Failed\n");
531                 if (type) {
532                         free(type);
533                         type = NULL;
534                 }
535                 if (path) {
536                         free(path);
537                         path = NULL;
538                 }
539                 return -1;
540         }
541
542         if (type) {
543                 free(type);
544                 type = NULL;
545         }
546         if (path) {
547                 free(path);
548                 path = NULL;
549         }
550
551         ret = __insert_disabled_ui_mainapp_info(mfx);
552         if (ret == -1)
553                 return -1;
554
555         /*Insert the package locale*/
556         pkglocale = __create_locale_list(pkglocale, lbl, lcn, icn, dcn, ath);
557         /*remove duplicated data in pkglocale*/
558         __trimfunc(pkglocale);
559
560         /*Insert the app locale info */
561         while(up != NULL)
562         {
563                 applocale = __create_locale_list(applocale, up->label, NULL, up->icon, NULL, NULL);
564                 up = up->next;
565         }
566
567         /*remove duplicated data in applocale*/
568         __trimfunc(applocale);
569
570         /*g_list_foreach(pkglocale, __printfunc, NULL);*/
571         /*_LOGD("\n");*/
572         /*g_list_foreach(applocale, __printfunc, NULL);*/
573
574         g_list_foreach(pkglocale, __insert_disabled_pkglocale_info, (gpointer)mfx);
575
576         /*native app locale info*/
577         up = mfx->uiapplication;
578         while(up != NULL)
579         {
580                 g_list_foreach(applocale, __insert_disabled_uiapplication_locale_info, (gpointer)up);
581                 up = up->next;
582         }
583
584         g_list_free(pkglocale);
585         pkglocale = NULL;
586         g_list_free(applocale);
587         applocale = NULL;
588
589         /*Insert in the package_app_info DB*/
590         ret = __insert_disabled_uiapplication_info(mfx);
591         if (ret == -1)
592                 return -1;
593
594         /*Insert in the package_app_app_category DB*/
595         ret = __insert_disabled_uiapplication_appcategory_info(mfx);
596         if (ret == -1)
597                 return -1;
598
599         /*Insert in the package_app_app_metadata DB*/
600         ret = __insert_disabled_uiapplication_appmetadata_info(mfx);
601         if (ret == -1)
602                 return -1;
603
604         return 0;
605 }
606
607 static int __delete_disabled_pkg_info_from_pkgid(const char *pkgid)
608 {
609         char *query = NULL;
610         int ret = -1;
611
612         /*Delete from Package Info DB*/
613         query = sqlite3_mprintf("delete from disabled_package_info where package=%Q", pkgid);
614         ret = __exec_query(query);
615         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "Package Info DB Delete Failed\n");
616
617         /*Delete from Package Localized Info*/
618         sqlite3_free(query);
619         query = sqlite3_mprintf("delete from disabled_package_localized_info where package=%Q", pkgid);
620         ret = __exec_query(query);
621         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "Package Localized Info DB Delete Failed\n");
622
623         /*Delete from app Info*/
624         sqlite3_free(query);
625         query = sqlite3_mprintf("delete from disabled_package_app_info where package=%Q", pkgid);
626         ret = __exec_query(query);
627         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "app Info DB Delete Failed\n");
628
629         /*Delete from app Localized Info*/
630         sqlite3_free(query);
631         query = sqlite3_mprintf("delete from disabled_package_app_localized_info where package=%Q", pkgid);
632         ret = __exec_query(query);
633         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "app Localized Info DB Delete Failed\n");
634
635         /*Delete from app category Info*/
636         sqlite3_free(query);
637         query = sqlite3_mprintf("delete from disabled_package_app_app_category where package=%Q", pkgid);
638         ret = __exec_query(query);
639         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "app category Info DB Delete Failed\n");
640
641         /*Delete from app metadata Info*/
642         sqlite3_free(query);
643         query = sqlite3_mprintf("delete from disabled_package_app_app_metadata where package=%Q", pkgid);
644         ret = __exec_query(query);
645         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "app metadata Info DB Delete Failed\n");
646
647 catch:
648         sqlite3_free(query);
649         return 0;
650 }
651
652 API int pkgmgr_parser_insert_disabled_pkg_info_in_db(manifest_x *mfx)
653 {
654         if (mfx == NULL) {
655                 _LOGD("manifest pointer is NULL\n");
656                 return -1;
657         }
658         int ret = 0;
659         ret = pkgmgr_parser_check_and_create_db();
660         if (ret == -1) {
661                 _LOGD("Failed to open DB\n");
662                 return ret;
663         }
664         ret = pkgmgr_parser_initialize_db();
665         if (ret == -1)
666                 goto err;
667         /*Begin transaction*/
668         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
669         if (ret != SQLITE_OK) {
670                 _LOGD("Failed to begin transaction[%d]\n", ret);
671                 ret = -1;
672                 goto err;
673         }
674         _LOGD("Transaction Begin\n");
675         ret = __insert_disabled_pkg_info_in_db(mfx);
676         if (ret == -1) {
677                 _LOGE("Insert into DB failed. Rollback now\n");
678                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
679                 if (ret != SQLITE_OK)
680                         _LOGE("ROLLBACK is fail after insert_disabled_pkg_info_in_db\n");
681
682                 ret = -1;
683                 goto err;
684         }
685         /*Commit transaction*/
686         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
687         if (ret != SQLITE_OK) {
688                 _LOGE("Failed to commit transaction. Rollback now\n");
689                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
690                 if (ret != SQLITE_OK)
691                 _LOGE("Failed to commit transaction. Rollback now\n");
692
693                 ret = -1;
694                 goto err;
695         }
696         _LOGD("Transaction Commit and End\n");
697 err:
698         pkgmgr_parser_close_db();
699         return ret;
700 }
701
702 API int pkgmgr_parser_delete_disabled_pkgid_info_from_db(const char *pkgid)
703 {
704         retvm_if(pkgid == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
705
706         int ret = 0;
707
708         /*open db*/
709         ret = pkgmgr_parser_check_and_create_db();
710         retvm_if(ret < 0, PMINFO_R_ERROR, "Failed to open DB\n");
711
712         /*Begin transaction*/
713         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
714         if (ret != SQLITE_OK) {
715                 _LOGE("Failed to begin transaction\n");
716                 ret = -1;
717                 goto err;
718         }
719
720         _LOGD("Start to Delete pkgid[%s] info from db\n", pkgid);
721
722         /*delete pkg info*/
723         ret = __delete_disabled_pkg_info_from_pkgid(pkgid);
724         if (ret == -1) {
725                 _LOGD("Delete from DB failed. Rollback now\n");
726                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
727                 if (ret != SQLITE_OK)
728                         _LOGE("Failed to begin transaction\n");
729                 goto err;
730         }
731
732         /*Commit transaction*/
733         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
734         if (ret != SQLITE_OK) {
735                 _LOGD("Failed to commit transaction, Rollback now\n");
736                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
737                 if (ret != SQLITE_OK)
738                         _LOGE("Failed to commit transaction, Rollback now\n");
739
740                 ret = -1;
741                 goto err;
742         }
743
744 err:
745         pkgmgr_parser_close_db();
746         return ret;
747 }
748
749 API int pkgmgr_parser_insert_disabled_pkg(const char *pkgid, char *const tagv[])
750 {
751         retvm_if(pkgid == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
752
753         char *manifest = NULL;
754         manifest_x *mfx = NULL;
755         int ret = -1;
756
757         _LOGD("parsing manifest for installation: %s\n", pkgid);
758
759         manifest = pkgmgr_parser_get_manifest_file(pkgid);
760         if (manifest == NULL) {
761                 _LOGE("can not get the manifest.xml\n");
762                 return -1;
763         }
764
765         xmlInitParser();
766         mfx = pkgmgr_parser_process_manifest_xml(manifest);
767         retvm_if(mfx == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
768
769         _LOGD("Parsing Finished\n");
770
771         __add_preload_info(mfx, manifest);
772
773         _LOGD("Added preload infomation\n");
774
775         FREE_AND_NULL(manifest);
776
777         ret = pkgmgr_parser_insert_disabled_pkg_info_in_db(mfx);
778         retvm_if(ret == PM_PARSER_R_ERROR, PM_PARSER_R_ERROR, "DB Insert failed");
779
780         _LOGD("DB Insert Success\n");
781
782         pkgmgr_parser_free_manifest_xml(mfx);
783         _LOGD("Free Done\n");
784         xmlCleanupParser();
785
786         return PM_PARSER_R_OK;
787 }
788
789 API int pkgmgr_parser_delete_disabled_pkg(const char *pkgid, char *const tagv[])
790 {
791         retvm_if(pkgid == NULL, PM_PARSER_R_ERROR, "argument supplied is NULL");
792
793         int ret = -1;
794
795         _LOGD("Start uninstall for pkgid : delete pkgid[%s]\n", pkgid);
796
797         /* delete pkgmgr db */
798         ret = pkgmgr_parser_delete_disabled_pkgid_info_from_db(pkgid);
799         if (ret == -1)
800                 _LOGD("DB pkgid info Delete failed\n");
801         else
802                 _LOGD("DB pkgid info Delete Success\n");
803
804         _LOGD("Finish : uninstall for pkgid\n");
805
806         return PM_PARSER_R_OK;
807 }