Set default value for launch_mode
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser_db.c
1 /*
2  * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/smack.h>
25 #include <linux/limits.h>
26 #include <unistd.h>
27 #include <pwd.h>
28
29 #include <glib.h>
30 #include <sqlite3.h>
31
32 #include <tzplatform_config.h>
33 #include <system_info.h>
34
35 #include "pkgmgr-info.h"
36 #include "pkgmgrinfo_basic.h"
37 #include "pkgmgr_parser.h"
38 #include "pkgmgr_parser_db_queries.h"
39 #include "pkgmgr_parser_debug.h"
40 #include "pkgmgr_parser_internal.h"
41
42 #ifndef OWNER_ROOT
43 #define OWNER_ROOT 0
44 #endif
45 #ifndef GLOBAL_USER
46 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
47 #endif
48 #ifndef APPFW_USER
49 #define APPFW_USER "app_fw"
50 #endif
51
52 #define BUFSIZE 4096
53
54 #define LDPI "ldpi"
55 #define MDPI "mdpi"
56 #define HDPI "hdpi"
57 #define XHDPI "xhdpi"
58 #define XXHDPI "xxhdpi"
59
60 #define LDPI_MIN 0
61 #define LDPI_MAX 240
62 #define MDPI_MIN 241
63 #define MDPI_MAX 300
64 #define HDPI_MIN 301
65 #define HDPI_MAX 380
66 #define XHDPI_MIN 381
67 #define XHDPI_MAX 480
68 #define XXHDPI_MIN 481
69 #define XXHDPI_MAX 600
70
71 /* app background category value */
72 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_VAL 0x00000
73 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL  0x00001
74 #define APP_BG_CATEGORY_MEDIA_VAL              0x00002
75 #define APP_BG_CATEGORY_DOWNLOAD_VAL           0x00004
76 #define APP_BG_CATEGORY_BGNETWORK_VAL          0x00008
77 #define APP_BG_CATEGORY_LOCATION_VAL           0x00010
78 #define APP_BG_CATEGORY_SENSOR_VAL             0x00020
79 #define APP_BG_CATEGORY_IOTCOMM_VAL            0x00040
80 #define APP_BG_CATEGORY_SYSTEM_VAL             0x00080
81
82 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_STR "enable"
83 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_STR  "disable"
84 #define APP_BG_CATEGORY_MEDIA_STR              "media"
85 #define APP_BG_CATEGORY_DOWNLOAD_STR           "download"
86 #define APP_BG_CATEGORY_BGNETWORK_STR          "background-network"
87 #define APP_BG_CATEGORY_LOCATION_STR           "location"
88 #define APP_BG_CATEGORY_SENSOR_STR             "sensor"
89 #define APP_BG_CATEGORY_IOTCOMM_STR            "iot-communication"
90 #define APP_BG_CATEGORY_SYSTEM                 "system"
91
92 #define REGULAR_USER 5000
93 static inline uid_t __getuid(void)
94 {
95         uid_t uid = getuid();
96
97         if (uid < REGULAR_USER)
98                 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
99         else
100                 return uid;
101 }
102
103 static const char *__get_bool(char *value, bool is_true)
104 {
105         if (value != NULL) {
106                 if (!strcmp(value, ""))
107                         return (is_true) ? "true" : "false";
108                 return value;
109         }
110
111         return (is_true) ? "true" : "false";
112 }
113
114 #define __BEGIN_TRANSACTION(db)                                                \
115 do {                                                                           \
116         if (sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL) !=           \
117                         SQLITE_OK) {                                           \
118                 _LOGE("begin transaction failed: %s", sqlite3_errmsg(db));     \
119                 sqlite3_close_v2(db);                                          \
120                 return PM_PARSER_R_ERROR;                                      \
121         }                                                                      \
122 } while (0)                                                                    \
123
124 #define __DO_TRANSACTION(db, func)                                             \
125 do {                                                                           \
126         if (func) {                                                            \
127                 _LOGE("transaction failed: %s, rollback", sqlite3_errmsg(db)); \
128                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) !=          \
129                                 SQLITE_OK)                                     \
130                         _LOGE("roll back transaction failed: %s",              \
131                                         sqlite3_errmsg(db));                   \
132                 sqlite3_close_v2(db);                                          \
133                 return PM_PARSER_R_ERROR;                                      \
134         }                                                                      \
135 } while (0)                                                                    \
136
137 #define __END_TRANSACTION(db)                                                  \
138 do {                                                                           \
139         if (sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) !=                    \
140                         SQLITE_OK) {                                           \
141                 _LOGE("commit failed: %s, rollback", sqlite3_errmsg(db));      \
142                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) !=          \
143                                 SQLITE_OK)                                     \
144                         _LOGE("roll back transaction failed: %s",              \
145                                         sqlite3_errmsg(db));                   \
146                 sqlite3_close_v2(db);                                          \
147                 return PM_PARSER_R_ERROR;                                      \
148         }                                                                      \
149 } while (0)                                                                    \
150
151 #define __BIND_TEXT(db, stmt, i, text)                                         \
152 do {                                                                           \
153         if (sqlite3_bind_text(stmt, i, text, -1, SQLITE_STATIC) != SQLITE_OK) {\
154                 _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));      \
155                 sqlite3_finalize(stmt);                                        \
156                 return -1;                                                     \
157         }                                                                      \
158 } while (0)
159
160 #define __BIND_INT(db, stmt, i, int)                                           \
161 do {                                                                           \
162         if (sqlite3_bind_int(stmt, i, int) != SQLITE_OK) {                     \
163                 _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));      \
164                 sqlite3_finalize(stmt);                                        \
165                 return -1;                                                     \
166         }                                                                      \
167 } while (0)
168
169 static const char *__get_parser_db_path(uid_t uid)
170 {
171         char buf[PATH_MAX];
172         const char *path;
173
174         if (uid == GLOBAL_USER || uid == OWNER_ROOT) {
175                 path = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
176         } else {
177                 snprintf(buf, sizeof(buf), "user/%d/.pkgmgr_parser.db", uid);
178                 path = tzplatform_mkpath(TZ_SYS_DB, buf);
179         }
180
181         return path;
182 }
183
184 static const char *__get_cert_db_path(void)
185 {
186         return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
187 }
188
189 static int __set_db_version(sqlite3 *db)
190 {
191         static const char query_raw[] = "PRAGMA user_version=";
192         char query[BUFSIZE];
193         int ret;
194         int version;
195
196         version = atoi(TIZEN_MAJOR_VER) * 10000 + atoi(TIZEN_MINOR_VER) * 100 +
197                 atoi(TIZEN_PATCH_VER);
198         snprintf(query, sizeof(query), "%s%d", query_raw, version);
199
200         ret = sqlite3_exec(db, query, NULL, NULL, NULL);
201         if (ret != SQLITE_OK) {
202                 _LOGE("exec failed: %s", sqlite3_errmsg(db));
203                 return -1;
204         }
205
206         return 0;
207 }
208
209 /* TODO: Do not labeling directly */
210 #define DB_LABEL "User::Home"
211 #define SET_SMACK_LABEL(x)                                                     \
212 do {                                                                           \
213         if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS))                 \
214                 _LOGE("failed chsmack -a %s %s", DB_LABEL, x);                 \
215         else                                                                   \
216                 _LOGD("chsmack -a %s %s", DB_LABEL, x);                        \
217 } while (0)
218
219 static int __set_db_permission(const char *path, uid_t uid)
220 {
221         int fd;
222         const char *files[2];
223         char journal_file[BUFSIZE];
224         struct stat sb;
225         mode_t mode;
226         struct passwd pwd;
227         struct passwd *result;
228         char buf[BUFSIZE];
229         int ret;
230         int i;
231
232         if (getuid() != OWNER_ROOT)
233                 return 0;
234
235         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
236                 ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
237                 if (result == NULL) {
238                         if (ret == 0)
239                                 _LOGE("no such user: %d", uid);
240                         else
241                                 _LOGE("getpwuid_r failed: %d", errno);
242                         return -1;
243                 }
244                 uid = pwd.pw_uid;
245         }
246
247         snprintf(journal_file, sizeof(journal_file), "%s-journal", path);
248         files[0] = path;
249         files[1] = journal_file;
250
251         ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
252         if (result == NULL) {
253                 if (ret == 0)
254                         _LOGE("no such user: %d", uid);
255                 else
256                         _LOGE("getpwuid_r failed: %d", errno);
257                 return -1;
258         }
259
260         for (i = 0; i < 2; i++) {
261                 fd = open(files[i], O_RDONLY);
262                 if (fd == -1) {
263                         _LOGE("open %s failed: %d", files[i], errno);
264                         return -1;
265                 }
266                 ret = fstat(fd, &sb);
267                 if (ret == -1) {
268                         _LOGE("stat %s failed: %d", files[i], errno);
269                         close(fd);
270                         return -1;
271                 }
272                 if (S_ISLNK(sb.st_mode)) {
273                         _LOGE("%s is symlink!", files[i]);
274                         close(fd);
275                         return -1;
276                 }
277                 ret = fchown(fd, uid, pwd.pw_gid);
278                 if (ret == -1) {
279                         _LOGE("fchown %s failed: %d", files[i], errno);
280                         close(fd);
281                         return -1;
282                 }
283
284                 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
285                 if (!strcmp(path, __get_cert_db_path()))
286                         mode |= S_IWOTH;
287                 ret = fchmod(fd, mode);
288                 if (ret == -1) {
289                         _LOGD("fchmod %s failed: %d", files[i], errno);
290                         close(fd);
291                         return -1;
292                 }
293                 close(fd);
294                 SET_SMACK_LABEL(files[i]);
295         }
296
297         return 0;
298 }
299
300 static const char *parser_init_queries[] = {
301         QUERY_CREATE_TABLE_PACKAGE_INFO,
302         QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO,
303         QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO,
304         QUERY_CREATE_TABLE_PACKAGE_APP_INFO,
305         QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO,
306         QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO, /* ? */
307         QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO, /* ? */
308         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL,
309         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY,
310         QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA,
311         QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION, /* ? */
312         QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED, /* ? */
313         QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST, /* ? */
314         QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL,
315         QUERY_CREATE_TABLE_PACKAGE_APP_INFO_FOR_UID,
316         QUERY_CREATE_TRIGGER_UPDATE_PACKAGE_APP_INFO_FOR_UID,
317         QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN,
318         NULL,
319 };
320
321 static const char *cert_init_queries[] = {
322         QUERY_CREATE_TABLE_PACKAGE_CERT_INFO,
323         QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO,
324         QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO,
325         QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2,
326         QUERY_CREATE_TRIGGER_DELETE_CERT_INFO,
327         QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO,
328         NULL
329 };
330
331 static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
332 {
333         int ret;
334         const char **queries;
335         int i;
336
337         if (__set_db_version(db))
338                 return -1;
339
340         if (strstr(dbpath, ".pkgmgr_parser.db")) {
341                 queries = parser_init_queries;
342         } else if (strstr(dbpath, ".pkgmgr_cert.db")) {
343                 queries = cert_init_queries;
344         } else {
345                 _LOGE("unexpected dbpath: %s", dbpath);
346                 return -1;
347         }
348
349         for (i = 0; queries[i] != NULL; i++) {
350                 ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL);
351                 if (ret != SQLITE_OK) {
352                         _LOGE("exec failed: %s", sqlite3_errmsg(db));
353                         return -1;
354                 }
355         }
356
357         if (__set_db_permission(dbpath, uid))
358                 _LOGE("failed to set db permission");
359
360         return 0;
361 }
362
363 API int pkgmgr_parser_initialize_parser_db(uid_t uid)
364 {
365         int ret;
366         const char *dbpath;
367         sqlite3 *db;
368
369         dbpath = __get_parser_db_path(uid);
370         if (access(dbpath, F_OK) != -1) {
371                 _LOGE("Manifest db for user %d is already exists", uid);
372                 return PM_PARSER_R_ERROR;
373         }
374
375         ret = sqlite3_open_v2(dbpath, &db,
376                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
377         if (ret != SQLITE_OK) {
378                 _LOGE("open db failed: %d", ret);
379                 return PM_PARSER_R_ERROR;
380         }
381
382         if (__initialize_db(db, dbpath, uid)) {
383                 sqlite3_close_v2(db);
384                 return PM_PARSER_R_ERROR;
385         }
386         sqlite3_close_v2(db);
387
388         return PM_PARSER_R_OK;
389 }
390
391 API int pkgmgr_parser_initialize_cert_db(void)
392 {
393         int ret;
394         const char *dbpath;
395         sqlite3 *db;
396
397         dbpath = __get_cert_db_path();
398         if (access(dbpath, F_OK) != -1) {
399                 _LOGE("Cert db is already exists");
400                 return PM_PARSER_R_ERROR;
401         }
402
403         ret = sqlite3_open_v2(dbpath, &db,
404                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
405         if (ret != SQLITE_OK) {
406                 _LOGE("open db failed: %d", ret);
407                 return PM_PARSER_R_ERROR;
408         }
409
410         if (__initialize_db(db, dbpath, GLOBAL_USER)) {
411                 sqlite3_close_v2(db);
412                 return PM_PARSER_R_ERROR;
413         }
414         sqlite3_close_v2(db);
415
416         return PM_PARSER_R_OK;
417 }
418
419 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
420 {
421         int ret;
422         struct passwd pwd;
423         struct passwd *result;
424         char buf[BUFSIZE];
425
426         ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
427         if (result == NULL) {
428                 if (ret == 0)
429                         _LOGE("no such user: %s", APPFW_USER);
430                 else
431                         _LOGE("getpwnam_r failed: %d", errno);
432                 return PM_PARSER_R_ERROR;
433         }
434
435         if (getuid() != OWNER_ROOT && getuid() != pwd.pw_uid) {
436                 _LOGE("Only root or app_fw user is allowed");
437                 return PM_PARSER_R_EINVAL;
438         }
439
440         if (pkgmgr_parser_initialize_parser_db(uid))
441                 return PM_PARSER_R_ERROR;
442
443         if (pkgmgr_parser_initialize_cert_db())
444                 return PM_PARSER_R_ERROR;
445
446         return PM_PARSER_R_OK;
447 }
448
449 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
450 #define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
451 static int __db_busy_handler(void *data, int count)
452 {
453         if (count < BUSY_WAITING_MAX) {
454                 usleep(BUSY_WAITING_USEC);
455                 return 1;
456         } else {
457                 /* sqlite3_prepare_v2 will return SQLITE_BUSY */
458                 return 0;
459         }
460 }
461
462 static int __open_db(uid_t uid, const char *path, sqlite3 **db, int flags)
463 {
464         int ret;
465
466         /* FIXME: always open with OPEN_CREATE flag for keeping previous
467          * implementation
468          */
469         if (flags & SQLITE_OPEN_READWRITE)
470                 flags = flags | SQLITE_OPEN_CREATE;
471
472         ret = sqlite3_open_v2(path, db, flags, NULL);
473         if (ret != SQLITE_OK)
474                 return ret;
475
476         if (flags & SQLITE_OPEN_CREATE) {
477                 ret = __initialize_db(*db, path, uid);
478                 if (ret) {
479                         _LOGE("failed to initialize db: %s\n");
480                         sqlite3_close_v2(*db);
481                         return -1;
482                 }
483         }
484
485         ret = sqlite3_exec(*db, "PRAGMA foreign_keys=ON", NULL, NULL, NULL);
486         if (ret != SQLITE_OK) {
487                 _LOGE("failed to enable foreign key support: %s",
488                                 sqlite3_errmsg(*db));
489                 sqlite3_close_v2(*db);
490                 return ret;
491         }
492
493         ret = sqlite3_busy_handler(*db, __db_busy_handler, NULL);
494         if (ret != SQLITE_OK) {
495                 _LOGE("failed to register busy handler: %s",
496                                 sqlite3_errmsg(*db));
497                 sqlite3_close_v2(*db);
498                 return ret;
499         }
500
501         return ret;
502 }
503
504
505 static int __convert_background_category(GList *category_list)
506 {
507         int ret = 0;
508         GList *tmp;
509         char *category_data;
510
511         if (category_list == NULL)
512                 return 0;
513
514         for (tmp = category_list; tmp; tmp = tmp->next) {
515                 category_data = (char *)tmp->data;
516                 if (category_data == NULL)
517                         continue;
518                 if (!strcmp(category_data, APP_BG_CATEGORY_MEDIA_STR))
519                         ret |= APP_BG_CATEGORY_MEDIA_VAL;
520                 else if (!strcmp(category_data, APP_BG_CATEGORY_DOWNLOAD_STR))
521                         ret |= APP_BG_CATEGORY_DOWNLOAD_VAL;
522                 else if (!strcmp(category_data, APP_BG_CATEGORY_BGNETWORK_STR))
523                         ret |= APP_BG_CATEGORY_BGNETWORK_VAL;
524                 else if (!strcmp(category_data, APP_BG_CATEGORY_LOCATION_STR))
525                         ret |= APP_BG_CATEGORY_LOCATION_VAL;
526                 else if (!strcmp(category_data, APP_BG_CATEGORY_SENSOR_STR))
527                         ret |= APP_BG_CATEGORY_SENSOR_VAL;
528                 else if (!strcmp(category_data, APP_BG_CATEGORY_IOTCOMM_STR))
529                         ret |= APP_BG_CATEGORY_IOTCOMM_VAL;
530                 else if (!strcmp(category_data, APP_BG_CATEGORY_SYSTEM))
531                         ret |= APP_BG_CATEGORY_SYSTEM_VAL;
532                 else
533                         _LOGE("Unidentified category [%s]", category_data);
534         }
535
536         return ret;
537 }
538
539 #define EFFECTIVE_APPID_KEY "http://tizen.org/metadata/effective-appid"
540 static const char *__find_effective_appid(GList *metadata_list)
541 {
542         GList *tmp;
543         metadata_x *md;
544
545         for (tmp = metadata_list; tmp; tmp = tmp->next) {
546                 md = (metadata_x *)tmp->data;
547                 if (md == NULL || md->key == NULL)
548                         continue;
549
550                 if (strcmp(md->key, EFFECTIVE_APPID_KEY) == 0) {
551                         if (md->value)
552                                 return md->value;
553                 }
554         }
555
556         return NULL;
557 }
558
559 static int __insert_appcontrol_info(sqlite3 *db, application_x *app)
560 {
561         static const char query[] =
562                 "INSERT INTO package_app_app_control (app_id, app_control) "
563                 "VALUES (?, ?)";
564         int ret;
565         sqlite3_stmt *stmt;
566         int idx;
567         char app_control[BUFSIZE];
568         GList *tmp;
569         appcontrol_x *ac;
570
571         if (app->appcontrol == NULL)
572                 return 0;
573
574         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
575         if (ret != SQLITE_OK) {
576                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
577                 return -1;
578         }
579
580         for (tmp = app->appcontrol; tmp; tmp = tmp->next) {
581                 ac = (appcontrol_x *)tmp->data;
582                 if (ac == NULL)
583                         continue;
584                 idx = 1;
585                 snprintf(app_control, sizeof(app_control), "%s|%s|%s",
586                                 ac->operation ? (strlen(ac->operation) > 0 ?
587                                         ac->operation : "NULL") : "NULL",
588                                 ac->uri ? (strlen(ac->uri) > 0 ?
589                                         ac->uri : "NULL") : "NULL",
590                                 ac->mime ? (strlen(ac->mime) > 0 ?
591                                         ac->mime : "NULL") : "NULL");
592                 __BIND_TEXT(db, stmt, idx++, app->appid);
593                 __BIND_TEXT(db, stmt, idx++, app_control);
594
595                 ret = sqlite3_step(stmt);
596                 if (ret != SQLITE_DONE) {
597                         _LOGE("step failed: %s", sqlite3_errmsg(db));
598                         sqlite3_finalize(stmt);
599                         return -1;
600                 }
601
602                 sqlite3_reset(stmt);
603         }
604
605         sqlite3_finalize(stmt);
606
607         return 0;
608 }
609
610 static int __insert_category_info(sqlite3 *db, application_x *app)
611 {
612         static const char query[] =
613                 "INSERT INTO package_app_app_category (app_id, category) "
614                 "VALUES (?, ?)";
615         int ret;
616         sqlite3_stmt *stmt;
617         int idx;
618         GList *tmp;
619         const char *category;
620
621         if (app->category == NULL)
622                 return 0;
623
624         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
625         if (ret != SQLITE_OK) {
626                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
627                 return -1;
628         }
629
630         for (tmp = app->category; tmp; tmp = tmp->next) {
631                 category = (const char *)tmp->data;
632                 if (category == NULL)
633                         continue;
634                 idx = 1;
635                 __BIND_TEXT(db, stmt, idx++, app->appid);
636                 __BIND_TEXT(db, stmt, idx++, category);
637
638                 ret = sqlite3_step(stmt);
639                 if (ret != SQLITE_DONE) {
640                         _LOGE("step failed: %s", sqlite3_errmsg(db));
641                         sqlite3_finalize(stmt);
642                         return -1;
643                 }
644
645                 sqlite3_reset(stmt);
646         }
647
648         sqlite3_finalize(stmt);
649
650         return 0;
651 }
652
653 static int __insert_metadata_info(sqlite3 *db, application_x *app)
654 {
655         static const char query[] =
656                 "INSERT INTO package_app_app_metadata (app_id,"
657                 "  md_key, md_value) VALUES (?, ?, ?)";
658         int ret;
659         sqlite3_stmt *stmt;
660         int idx;
661         GList *tmp;
662         metadata_x *md;
663
664         if (app->metadata == NULL)
665                 return 0;
666
667         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
668         if (ret != SQLITE_OK) {
669                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
670                 return -1;
671         }
672
673         for (tmp = app->metadata; tmp; tmp = tmp->next) {
674                 md = (metadata_x *)tmp->data;
675                 if (md == NULL)
676                         continue;
677                 idx = 1;
678                 __BIND_TEXT(db, stmt, idx++, app->appid);
679                 __BIND_TEXT(db, stmt, idx++, md->key);
680                 __BIND_TEXT(db, stmt, idx++, md->value);
681
682                 ret = sqlite3_step(stmt);
683                 if (ret != SQLITE_DONE) {
684                         _LOGE("step failed: %s", sqlite3_errmsg(db));
685                         sqlite3_finalize(stmt);
686                         return -1;
687                 }
688
689                 sqlite3_reset(stmt);
690         }
691
692         sqlite3_finalize(stmt);
693
694         return 0;
695 }
696
697 static int __insert_datacontrol_info(sqlite3 *db, application_x *app)
698 {
699         static const char query[] =
700                 "INSERT INTO package_app_data_control (app_id, providerid,"
701                 "  access, type, trusted) VALUES (?, ?, ?, ?, ?)";
702         int ret;
703         sqlite3_stmt *stmt;
704         int idx;
705         GList *tmp;
706         datacontrol_x *dc;
707
708         if (app->datacontrol == NULL)
709                 return 0;
710
711         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
712         if (ret != SQLITE_OK) {
713                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
714                 return -1;
715         }
716
717         for (tmp = app->datacontrol; tmp; tmp = tmp->next) {
718                 dc = (datacontrol_x *)tmp->data;
719                 if (dc == NULL)
720                         continue;
721                 idx = 1;
722                 __BIND_TEXT(db, stmt, idx++, app->appid);
723                 __BIND_TEXT(db, stmt, idx++, dc->providerid);
724                 __BIND_TEXT(db, stmt, idx++, dc->access);
725                 __BIND_TEXT(db, stmt, idx++, dc->type);
726                 __BIND_TEXT(db, stmt, idx++, dc->trusted);
727
728                 ret = sqlite3_step(stmt);
729                 if (ret != SQLITE_DONE) {
730                         _LOGE("step failed: %s", sqlite3_errmsg(db));
731                         sqlite3_finalize(stmt);
732                         return -1;
733                 }
734
735                 sqlite3_reset(stmt);
736         }
737
738         sqlite3_finalize(stmt);
739
740         return 0;
741 }
742
743 /* TODO: move to installer */
744 static int __check_dpi(const char *dpi_char, int dpi_int)
745 {
746         if (dpi_char == NULL)
747                 return -1;
748
749         if (strcasecmp(dpi_char, LDPI) == 0) {
750                 if (dpi_int >= LDPI_MIN && dpi_int <= LDPI_MAX)
751                         return 0;
752                 else
753                         return -1;
754         } else if (strcasecmp(dpi_char, MDPI) == 0) {
755                 if (dpi_int >= MDPI_MIN && dpi_int <= MDPI_MAX)
756                         return 0;
757                 else
758                         return -1;
759         } else if (strcasecmp(dpi_char, HDPI) == 0) {
760                 if (dpi_int >= HDPI_MIN && dpi_int <= HDPI_MAX)
761                         return 0;
762                 else
763                         return -1;
764         } else if (strcasecmp(dpi_char, XHDPI) == 0) {
765                 if (dpi_int >= XHDPI_MIN && dpi_int <= XHDPI_MAX)
766                         return 0;
767                 else
768                         return -1;
769         } else if (strcasecmp(dpi_char, XXHDPI) == 0) {
770                 if (dpi_int >= XXHDPI_MIN && dpi_int <= XXHDPI_MAX)
771                         return 0;
772                 else
773                         return -1;
774         } else
775                 return -1;
776 }
777
778 static gint __compare_splashscreen_with_orientation_dpi(gconstpointer a,
779                 gconstpointer b)
780 {
781         splashscreen_x *ss = (splashscreen_x *)a;
782         const char *orientation = (const char *)b;
783         int dpi = -1;
784         int ret;
785
786         if (ss->operation || ss->dpi == NULL)
787                 return -1;
788
789         ret = system_info_get_platform_int(
790                         "http://tizen.org/feature/screen.dpi", &dpi);
791         if (ret != SYSTEM_INFO_ERROR_NONE)
792                 return -1;
793
794         if (strcasecmp(ss->orientation, orientation) == 0 &&
795                         __check_dpi(ss->dpi, dpi) == 0)
796                 return 0;
797
798         return -1;
799 }
800
801 static gint __compare_splashscreen_with_orientation(gconstpointer a,
802                 gconstpointer b)
803 {
804         splashscreen_x *ss = (splashscreen_x *)a;
805         const char *orientation = (const char *)b;
806
807         if (ss->operation || ss->dpi)
808                 return -1;
809
810         if (strcasecmp(ss->orientation, orientation) == 0)
811                 return 0;
812
813         return -1;
814 }
815
816 static splashscreen_x *__find_default_splashscreen(GList *splashscreens,
817                 const char *orientation)
818 {
819         GList *tmp;
820
821         tmp = g_list_find_custom(splashscreens, orientation,
822                         (GCompareFunc)
823                         __compare_splashscreen_with_orientation_dpi);
824         if (tmp)
825                 return (splashscreen_x *)tmp->data;
826
827         tmp = g_list_find_custom(splashscreens, orientation,
828                         (GCompareFunc)__compare_splashscreen_with_orientation);
829         if (tmp)
830                 return (splashscreen_x *)tmp->data;
831
832         return NULL;
833 }
834
835 static void __find_appcontrol_splashscreen_with_dpi(gpointer data,
836                 gpointer user_data)
837 {
838         splashscreen_x *ss = (splashscreen_x *)data;
839         GList **list = (GList **)user_data;
840         int dpi = -1;
841         int ret;
842
843         if (ss->operation == NULL || ss->dpi == NULL)
844                 return;
845
846         ret = system_info_get_platform_int(
847                         "http://tizen.org/feature/screen.dpi", &dpi);
848         if (ret != SYSTEM_INFO_ERROR_NONE)
849                 return;
850
851         if (__check_dpi(ss->dpi, dpi) != 0)
852                 return;
853
854         *list = g_list_append(*list, ss);
855 }
856
857 static void __find_appcontrol_splashscreen(gpointer data, gpointer user_data)
858 {
859         splashscreen_x *ss = (splashscreen_x *)data;
860         GList **list = (GList **)user_data;
861         splashscreen_x *ss_tmp;
862         GList *tmp;
863
864         if (ss->operation == NULL || ss->dpi)
865                 return;
866
867         for (tmp = *list; tmp; tmp = tmp->next) {
868                 ss_tmp = (splashscreen_x *)tmp->data;
869                 if (ss_tmp->operation
870                         && strcmp(ss_tmp->operation, ss->operation) == 0
871                         && strcmp(ss_tmp->orientation, ss->orientation) == 0)
872                         return;
873         }
874
875         *list = g_list_append(*list, ss);
876 }
877
878 static GList *__find_splashscreens(GList *splashscreens)
879 {
880         GList *list = NULL;
881         splashscreen_x *ss;
882
883         g_list_foreach(splashscreens,
884                         __find_appcontrol_splashscreen_with_dpi, &list);
885         g_list_foreach(splashscreens,
886                         __find_appcontrol_splashscreen, &list);
887
888         ss = __find_default_splashscreen(splashscreens, "portrait");
889         if (ss)
890                 list = g_list_append(list, ss);
891         ss = __find_default_splashscreen(splashscreens, "landscape");
892         if (ss)
893                 list = g_list_append(list, ss);
894
895         return list;
896 }
897
898 static int __insert_splashscreen_info(sqlite3 *db, application_x *app)
899 {
900         static const char query[] =
901                 "INSERT INTO package_app_splash_screen (app_id, src, type,"
902                 "  orientation, indicatordisplay, operation, color_depth) "
903                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
904         int ret;
905         sqlite3_stmt *stmt;
906         int idx;
907         GList *tmp;
908         splashscreen_x *ss;
909         GList *ss_list;
910
911         if (app->splashscreens == NULL)
912                 return 0;
913
914         ss_list = __find_splashscreens(app->splashscreens);
915         if (ss_list == NULL)
916                 return 0;
917
918         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
919         if (ret != SQLITE_OK) {
920                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
921                 return -1;
922         }
923
924         for (tmp = ss_list; tmp; tmp = tmp->next) {
925                 ss = (splashscreen_x *)tmp->data;
926                 if (ss == NULL)
927                         continue;
928                 idx = 1;
929                 __BIND_TEXT(db, stmt, idx++, app->appid);
930                 __BIND_TEXT(db, stmt, idx++, ss->src);
931                 __BIND_TEXT(db, stmt, idx++, ss->type);
932                 __BIND_TEXT(db, stmt, idx++, ss->orientation);
933                 __BIND_TEXT(db, stmt, idx++, ss->indicatordisplay);
934                 __BIND_TEXT(db, stmt, idx++, ss->operation);
935                 __BIND_TEXT(db, stmt, idx++, ss->color_depth);
936
937                 ret = sqlite3_step(stmt);
938                 if (ret != SQLITE_DONE) {
939                         _LOGE("step failed: %s", sqlite3_errmsg(db));
940                         sqlite3_finalize(stmt);
941                         return -1;
942                 }
943
944                 sqlite3_reset(stmt);
945         }
946
947         sqlite3_finalize(stmt);
948
949         return 0;
950 }
951
952 static void __trimfunc(GList *trim_list)
953 {
954         char *trim_data;
955         char *prev = NULL;
956         GList *list = g_list_first(trim_list);
957
958         while (list) {
959                 trim_data = (char *)list->data;
960                 if (trim_data) {
961                         if (prev) {
962                                 if (strcmp(trim_data, prev) == 0) {
963                                         trim_list = g_list_remove(trim_list,
964                                                         trim_data);
965                                         list = g_list_first(trim_list);
966                                         prev = NULL;
967                                         continue;
968                                 } else
969                                         prev = trim_data;
970                         } else {
971                                 prev = trim_data;
972                         }
973                 }
974                 list = g_list_next(list);
975         }
976 }
977
978 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
979 {
980         if (a == NULL || b == NULL)
981                 return 0;
982         if (strcmp((char *)a, (char *)b) == 0)
983                 return 0;
984         if (strcmp((char *)a, (char *)b) < 0)
985                 return -1;
986         if (strcmp((char *)a, (char *)b) > 0)
987                 return 1;
988         return 0;
989 }
990
991 /* TODO: refactor inserting localized info */
992 static GList *__create_locale_list(GList *lbls, GList *lcns, GList *icns,
993                 GList *dcns, GList *aths)
994 {
995         GList *locale = NULL;
996         GList *tmp;
997         label_x *lbl;
998         license_x *lcn;
999         icon_x *icn;
1000         description_x *dcn;
1001         author_x *ath;
1002
1003         for (tmp = lbls; tmp; tmp = tmp->next) {
1004                 lbl = (label_x *)tmp->data;
1005                 if (lbl == NULL)
1006                         continue;
1007                 if (lbl->lang)
1008                         locale = g_list_insert_sorted_with_data(
1009                                         locale, (gpointer)lbl->lang,
1010                                         __comparefunc, NULL);
1011         }
1012         for (tmp = lcns; tmp; tmp = tmp->next) {
1013                 lcn = (license_x *)tmp->data;
1014                 if (lcn == NULL)
1015                         continue;
1016                 if (lcn->lang)
1017                         locale = g_list_insert_sorted_with_data(
1018                                         locale, (gpointer)lcn->lang,
1019                                         __comparefunc, NULL);
1020         }
1021         for (tmp = icns; tmp; tmp = tmp->next) {
1022                 icn = (icon_x *)tmp->data;
1023                 if (icn == NULL)
1024                         continue;
1025                 if (icn->lang)
1026                         locale = g_list_insert_sorted_with_data(
1027                                         locale, (gpointer)icn->lang,
1028                                         __comparefunc, NULL);
1029         }
1030         for (tmp = dcns; tmp; tmp = tmp->next) {
1031                 dcn = (description_x *)tmp->data;
1032                 if (dcn == NULL)
1033                         continue;
1034                 if (dcn->lang)
1035                         locale = g_list_insert_sorted_with_data(
1036                                         locale, (gpointer)dcn->lang,
1037                                         __comparefunc, NULL);
1038         }
1039         for (tmp = aths; tmp; tmp = tmp->next) {
1040                 ath = (author_x *)tmp->data;
1041                 if (ath == NULL)
1042                         continue;
1043                 if (ath->lang)
1044                         locale = g_list_insert_sorted_with_data(
1045                                         locale, (gpointer)ath->lang,
1046                                         __comparefunc, NULL);
1047         }
1048         __trimfunc(locale);
1049         return locale;
1050 }
1051
1052 static gint __check_icon_resolution(const char *orig_icon_path,
1053                 char **new_icon_path)
1054 {
1055         int ret;
1056         char *dpi_path[2];
1057         char *icon_filename;
1058         char modified_iconpath[BUFSIZE];
1059         char icon_path[BUFSIZE];
1060         int i;
1061         int dpi = -1;
1062
1063         if (orig_icon_path == NULL)
1064                 return -1;
1065
1066         ret = system_info_get_platform_int(
1067                         "http://tizen.org/feature/screen.dpi", &dpi);
1068         if (ret != SYSTEM_INFO_ERROR_NONE)
1069                 return -1;
1070
1071         if (dpi >= LDPI_MIN && dpi <= LDPI_MAX) {
1072                 dpi_path[0] = "LDPI";
1073                 dpi_path[1] = "ldpi";
1074         } else if (dpi >= MDPI_MIN && dpi <= MDPI_MAX) {
1075                 dpi_path[0] = "MDPI";
1076                 dpi_path[1] = "mdpi";
1077         } else if (dpi >= HDPI_MIN && dpi <= HDPI_MAX) {
1078                 dpi_path[0] = "HDPI";
1079                 dpi_path[1] = "hdpi";
1080         } else if (dpi >= XHDPI_MIN && dpi <= XHDPI_MAX) {
1081                 dpi_path[0] = "XHDPI";
1082                 dpi_path[1] = "xhdpi";
1083         } else if (dpi >= XXHDPI_MIN && dpi <= XXHDPI_MAX) {
1084                 dpi_path[0] = "XXHDPI";
1085                 dpi_path[1] = "xxhdpi";
1086         } else {
1087                 _LOGE("Unidentified dpi[%d]", dpi);
1088                 return -1;
1089         }
1090
1091         icon_filename = strrchr(orig_icon_path, '/');
1092         if (icon_filename == NULL)
1093                 return -1;
1094
1095         snprintf(icon_path,
1096                         strlen(orig_icon_path) - (strlen(icon_filename) - 1),
1097                         "%s", orig_icon_path);
1098         for (i = 0; i < 2; i++) {
1099                 snprintf(modified_iconpath, BUFSIZE - 1, "%s/%s%s",
1100                                 icon_path, dpi_path[i], icon_filename);
1101                 if (access(modified_iconpath, F_OK) != -1) {
1102                         /* if exists, return modified icon path */
1103                         *new_icon_path = strdup(modified_iconpath);
1104                         return 0;
1105                 }
1106         }
1107
1108         return -1;
1109 }
1110
1111 static gint __compare_icon(gconstpointer a, gconstpointer b)
1112 {
1113         icon_x *icon = (icon_x *)a;
1114         char *icon_path;
1115
1116         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1117                 return -1;
1118
1119         if (icon->dpi != NULL)
1120                 return -1;
1121
1122         if (__check_icon_resolution(icon->text, &icon_path) == 0) {
1123                 free(icon->text);
1124                 icon->text = icon_path;
1125         }
1126
1127         return 0;
1128 }
1129
1130 static gint __compare_icon_with_dpi(gconstpointer a, gconstpointer b)
1131 {
1132         icon_x *icon = (icon_x *)a;
1133         int dpi = GPOINTER_TO_INT(b);
1134
1135         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1136                 return -1;
1137
1138         if (icon->dpi == NULL)
1139                 return -1;
1140
1141         if (__check_dpi(icon->dpi, dpi) == 0)
1142                 return 0;
1143
1144         return -1;
1145 }
1146
1147 static gint __compare_icon_with_lang(gconstpointer a, gconstpointer b)
1148 {
1149         icon_x *icon = (icon_x *)a;
1150         char *lang = (char *)b;
1151         char *icon_path;
1152
1153         if (icon->dpi != NULL)
1154                 return -1;
1155
1156         if (strcasecmp(icon->lang, lang) == 0) {
1157                 if (strcasecmp(icon->lang, DEFAULT_LOCALE) == 0) {
1158                         /* icon for no locale. check existance of
1159                          * folder-hierachied default icons
1160                          */
1161                         if (__check_icon_resolution(icon->text,
1162                                                 &icon_path) == 0) {
1163                                 free(icon->text);
1164                                 icon->text = icon_path;
1165                         }
1166                 }
1167                 return 0;
1168         }
1169
1170         return -1;
1171 }
1172
1173 static gint __compare_icon_with_lang_dpi(gconstpointer a, gconstpointer b)
1174 {
1175         int ret;
1176         icon_x *icon = (icon_x *)a;
1177         char *lang = (char *)b;
1178         int dpi = -1;
1179
1180         ret = system_info_get_platform_int(
1181                         "http://tizen.org/feature/screen.dpi", &dpi);
1182         if (ret != SYSTEM_INFO_ERROR_NONE)
1183                 return -1;
1184
1185         if (strcasecmp(icon->lang, lang) == 0 &&
1186                         __check_dpi(icon->dpi, dpi) == 0)
1187                 return 0;
1188
1189         return -1;
1190 }
1191
1192 static char *__find_icon(GList *icons, const char *lang)
1193 {
1194         GList *tmp;
1195         icon_x *icon;
1196         int dpi = 0;
1197         int ret;
1198
1199         /* first, find icon whose locale and dpi with given lang and
1200          * system's dpi has matched
1201          */
1202         tmp = g_list_find_custom(icons, lang,
1203                         (GCompareFunc)__compare_icon_with_lang_dpi);
1204         if (tmp != NULL) {
1205                 icon = (icon_x *)tmp->data;
1206                 return (char *)icon->text;
1207         }
1208
1209         /* if first has failed, find icon whose locale has matched */
1210         tmp = g_list_find_custom(icons, lang,
1211                         (GCompareFunc)__compare_icon_with_lang);
1212         if (tmp != NULL) {
1213                 icon = (icon_x *)tmp->data;
1214                 return (char *)icon->text;
1215         }
1216
1217         /* if second has failed, find icon whose dpi has matched with
1218          * system's dpi
1219          */
1220         ret = system_info_get_platform_int(
1221                         "http://tizen.org/feature/screen.dpi", &dpi);
1222         if (ret == SYSTEM_INFO_ERROR_NONE) {
1223                 tmp = g_list_find_custom(icons, GINT_TO_POINTER(dpi),
1224                                 (GCompareFunc)__compare_icon_with_dpi);
1225                 if (tmp != NULL) {
1226                         icon = (icon_x *)tmp->data;
1227                         return (char *)icon->text;
1228                 }
1229         }
1230
1231         /* last, find default icon marked as "No Locale" */
1232         tmp = g_list_find_custom(icons, NULL, (GCompareFunc)__compare_icon);
1233         if (tmp != NULL) {
1234                 icon = (icon_x *)tmp->data;
1235                 return (char *)icon->text;
1236         }
1237
1238         return NULL;
1239 }
1240
1241 static void __extract_data(const char *locale, GList *lbls, GList *lcns,
1242                 GList *icns, GList *dcns, GList *aths, char **label,
1243                 char **license, char **icon, char **description, char **author)
1244 {
1245         GList *tmp;
1246         label_x *lbl;
1247         license_x *lcn;
1248         description_x *dcn;
1249         author_x *ath;
1250
1251         for (tmp = lbls; tmp; tmp = tmp->next) {
1252                 lbl = (label_x *)tmp->data;
1253                 if (lbl == NULL)
1254                         continue;
1255                 if (lbl->lang) {
1256                         if (strcmp(lbl->lang, locale) == 0) {
1257                                 *label = (char *)lbl->text;
1258                                 break;
1259                         }
1260                 }
1261         }
1262         for (tmp = lcns; tmp; tmp = tmp->next) {
1263                 lcn = (license_x *)tmp->data;
1264                 if (lcn == NULL)
1265                         continue;
1266                 if (lcn->lang) {
1267                         if (strcmp(lcn->lang, locale) == 0) {
1268                                 *license = (char *)lcn->text;
1269                                 break;
1270                         }
1271                 }
1272         }
1273
1274         *icon = __find_icon(icns, locale);
1275
1276         for (tmp = dcns; tmp; tmp = tmp->next) {
1277                 dcn = (description_x *)tmp->data;
1278                 if (dcn == NULL)
1279                         continue;
1280                 if (dcn->lang) {
1281                         if (strcmp(dcn->lang, locale) == 0) {
1282                                 *description = (char *)dcn->text;
1283                                 break;
1284                         }
1285                 }
1286         }
1287         for (tmp = aths; tmp; tmp = tmp->next) {
1288                 ath = (author_x *)tmp->data;
1289                 if (ath == NULL)
1290                         continue;
1291                 if (ath->lang) {
1292                         if (strcmp(ath->lang, locale) == 0) {
1293                                 *author = (char *)ath->text;
1294                                 break;
1295                         }
1296                 }
1297         }
1298 }
1299
1300 static int __insert_mainapp_localized_info(sqlite3 *db, application_x *app,
1301                 const char *locale, const char *label, const char *icon)
1302 {
1303         static const char query[] =
1304                 "INSERT OR REPLACE INTO package_localized_info ("
1305                 "  package, package_locale, package_label, package_icon,"
1306                 "  package_description, package_license, package_author) "
1307                 "VALUES (?, ?,"
1308                 "  COALESCE((SELECT package_label FROM package_localized_info"
1309                 "            WHERE package=? AND package_locale=?), ?),"
1310                 "  COALESCE((SELECT package_icon FROM package_localized_info"
1311                 "            WHERE package=? AND package_icon=?), ?),"
1312                 "  (SELECT package_description FROM package_localized_info"
1313                 "   WHERE package=? AND package_locale=?),"
1314                 "  (SELECT package_description FROM package_localized_info"
1315                 "   WHERE package=? AND package_locale=?),"
1316                 "  (SELECT package_description FROM package_localized_info"
1317                 "   WHERE package=? AND package_locale=?))";
1318         int ret;
1319         sqlite3_stmt *stmt;
1320         int idx = 1;
1321
1322         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1323         if (ret != SQLITE_OK) {
1324                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1325                 return -1;
1326         }
1327
1328         __BIND_TEXT(db, stmt, idx++, app->package);
1329         __BIND_TEXT(db, stmt, idx++, locale);
1330         __BIND_TEXT(db, stmt, idx++, app->package);
1331         __BIND_TEXT(db, stmt, idx++, locale);
1332         __BIND_TEXT(db, stmt, idx++, label);
1333         __BIND_TEXT(db, stmt, idx++, app->package);
1334         __BIND_TEXT(db, stmt, idx++, locale);
1335         __BIND_TEXT(db, stmt, idx++, icon);
1336         __BIND_TEXT(db, stmt, idx++, app->package);
1337         __BIND_TEXT(db, stmt, idx++, locale);
1338         __BIND_TEXT(db, stmt, idx++, app->package);
1339         __BIND_TEXT(db, stmt, idx++, locale);
1340         __BIND_TEXT(db, stmt, idx++, app->package);
1341         __BIND_TEXT(db, stmt, idx++, locale);
1342
1343         ret = sqlite3_step(stmt);
1344         if (ret != SQLITE_DONE) {
1345                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1346                 sqlite3_finalize(stmt);
1347                 return -1;
1348         }
1349
1350         sqlite3_finalize(stmt);
1351
1352         return 0;
1353 }
1354
1355 static int __insert_app_localized_info(sqlite3 *db, application_x *app)
1356 {
1357         static const char query[] =
1358                 "INSERT INTO package_app_localized_info (app_id, app_locale,"
1359                 "  app_label, app_icon) "
1360                 "VALUES (?, ?, ?, ?)";
1361         int ret;
1362         sqlite3_stmt *stmt;
1363         int idx;
1364         GList *tmp;
1365         GList *locales;
1366         const char *locale;
1367         char *label;
1368         char *icon;
1369
1370         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1371         if (ret != SQLITE_OK) {
1372                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1373                 return -1;
1374         }
1375
1376         locales = __create_locale_list(app->label, NULL, app->icon, NULL, NULL);
1377         for (tmp = locales; tmp; tmp = tmp->next) {
1378                 locale = (const char *)tmp->data;
1379                 label = NULL;
1380                 icon = NULL;
1381                 __extract_data(locale, app->label, NULL, app->icon, NULL, NULL,
1382                                 &label, NULL, &icon, NULL, NULL);
1383                 if (!label && !icon)
1384                         continue;
1385
1386                 idx = 1;
1387                 __BIND_TEXT(db, stmt, idx++, app->appid);
1388                 __BIND_TEXT(db, stmt, idx++, locale);
1389                 __BIND_TEXT(db, stmt, idx++, label);
1390                 __BIND_TEXT(db, stmt, idx++, icon);
1391
1392                 ret = sqlite3_step(stmt);
1393                 if (ret != SQLITE_DONE) {
1394                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1395                         g_list_free(locales);
1396                         sqlite3_finalize(stmt);
1397                         return -1;
1398                 }
1399
1400                 sqlite3_reset(stmt);
1401
1402                 if (strcasecmp(app->mainapp, "true")) {
1403                         if (__insert_mainapp_localized_info(db, app, locale,
1404                                                 label, icon))
1405                                 _LOGE("insert mainapp localized info failed");
1406                 }
1407         }
1408
1409         g_list_free(locales);
1410         sqlite3_finalize(stmt);
1411
1412         return 0;
1413 }
1414
1415 static int __insert_package_privilege_info(sqlite3 *db, manifest_x *mfx)
1416 {
1417         static const char query[] =
1418                 "INSERT INTO package_privilege_info (package, privilege, type) "
1419                 "VALUES (?, ?, ?)";
1420         int ret;
1421         sqlite3_stmt *stmt;
1422         int idx;
1423         GList *tmp;
1424         privilege_x *priv;
1425
1426         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1427         if (ret != SQLITE_OK) {
1428                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1429                 return -1;
1430         }
1431
1432         for (tmp = mfx->privileges; tmp; tmp = tmp->next) {
1433                 priv = (privilege_x *)tmp->data;
1434                 if (priv == NULL)
1435                         continue;
1436
1437                 idx = 1;
1438                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1439                 __BIND_TEXT(db, stmt, idx++, priv->value);
1440                 __BIND_TEXT(db, stmt, idx++, priv->type);
1441
1442                 ret = sqlite3_step(stmt);
1443                 if (ret != SQLITE_DONE) {
1444                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1445                         sqlite3_finalize(stmt);
1446                         return -1;
1447                 }
1448                 sqlite3_reset(stmt);
1449         }
1450
1451         sqlite3_finalize(stmt);
1452
1453         return 0;
1454 }
1455
1456 /* _PRODUCT_LAUNCHING_ENHANCED_
1457  *  app->indicatordisplay, app->portraitimg, app->landscapeimg,
1458  *  app->guestmode_appstatus
1459  */
1460 static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
1461 {
1462         static const char query[] =
1463                 "INSERT INTO package_app_info (app_id, app_component,"
1464                 "  app_exec, app_nodisplay, app_type, app_onboot, app_multiple,"
1465                 "  app_autorestart, app_taskmanage, app_hwacceleration,"
1466                 "  app_screenreader, app_mainapp, app_recentimage,"
1467                 "  app_launchcondition, app_indicatordisplay, app_portraitimg,"
1468                 "  app_landscapeimg, app_guestmodevisibility,"
1469                 "  app_permissiontype, app_preload, app_submode,"
1470                 "  app_submode_mainid, app_installed_storage, app_process_pool,"
1471                 "  app_launch_mode, app_ui_gadget, app_support_mode,"
1472                 "  app_support_disable, component_type, package, app_tep_name,"
1473                 "  app_zip_mount_file, app_background_category,"
1474                 "  app_package_type, app_root_path, app_api_version,"
1475                 "  app_effective_appid, app_splash_screen_display,"
1476                 "  app_package_system, app_removable,"
1477                 "  app_package_installed_time, app_support_ambient,"
1478                 "  app_setup_appid) "
1479                 "VALUES (?, ?, "
1480                 "  ?, LOWER(?), ?, LOWER(?), LOWER(?),"
1481                 "  LOWER(?), LOWER(?), ?,"
1482                 "  ?, LOWER(?), ?,"
1483                 "  ?, LOWER(?), ?,"
1484                 "  ?, LOWER(?),"
1485                 "  ?, LOWER(?), LOWER(?),"
1486                 "  ?, ?, LOWER(?),"
1487                 "  COALESCE(?, 'single'), LOWER(?), ?,"
1488                 "  LOWER(?), ?, ?, ?,"
1489                 "  ?, ?,"
1490                 "  ?, ?, ?,"
1491                 "  ?, LOWER(?),"
1492                 "  LOWER(?), LOWER(?),"
1493                 "  ?, LOWER(?),"
1494                 "  ?)";
1495         int ret;
1496         sqlite3_stmt *stmt;
1497         int idx;
1498         GList *tmp;
1499         application_x *app;
1500         int bg_category;
1501         const char *effective_appid;
1502
1503         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1504         if (ret != SQLITE_OK) {
1505                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1506                 return -1;
1507         }
1508
1509         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1510                 app = (application_x *)tmp->data;
1511                 if (app == NULL)
1512                         continue;
1513
1514                 bg_category = __convert_background_category(
1515                                 app->background_category);
1516                 effective_appid = __find_effective_appid(app->metadata);
1517
1518                 idx = 1;
1519                 __BIND_TEXT(db, stmt, idx++, app->appid);
1520                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1521                 __BIND_TEXT(db, stmt, idx++, app->exec);
1522                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->nodisplay, false));
1523                 __BIND_TEXT(db, stmt, idx++, app->type);
1524                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->onboot, false));
1525                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->multiple, false));
1526                 __BIND_TEXT(db, stmt, idx++,
1527                                 __get_bool(app->autorestart, false));
1528                 __BIND_TEXT(db, stmt, idx++,
1529                                 __get_bool(app->taskmanage, false));
1530                 __BIND_TEXT(db, stmt, idx++, app->hwacceleration);
1531                 __BIND_TEXT(db, stmt, idx++, app->screenreader);
1532                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->mainapp, false));
1533                 __BIND_TEXT(db, stmt, idx++, app->recentimage);
1534                 __BIND_TEXT(db, stmt, idx++, app->launchcondition);
1535                 __BIND_TEXT(db, stmt, idx++,
1536                                 __get_bool(app->indicatordisplay, true));
1537                 __BIND_TEXT(db, stmt, idx++, app->portraitimg);
1538                 __BIND_TEXT(db, stmt, idx++, app->landscapeimg);
1539                 __BIND_TEXT(db, stmt, idx++,
1540                                 __get_bool(app->guestmode_visibility, true));
1541                 __BIND_TEXT(db, stmt, idx++, app->permission_type);
1542                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1543                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->submode, false));
1544                 __BIND_TEXT(db, stmt, idx++, app->submode_mainid);
1545                 __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1546                 __BIND_TEXT(db, stmt, idx++,
1547                                 __get_bool(app->process_pool, false));
1548                 __BIND_TEXT(db, stmt, idx++, app->launch_mode);
1549                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->ui_gadget, false));
1550                 __BIND_TEXT(db, stmt, idx++, app->support_mode);
1551                 __BIND_TEXT(db, stmt, idx++,
1552                                 __get_bool(mfx->support_disable, false));
1553                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1554                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1555                 __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1556                 __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1557                 __BIND_INT(db, stmt, idx++, bg_category);
1558                 __BIND_TEXT(db, stmt, idx++, mfx->type ? mfx->type : "tpk");
1559                 __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1560                 __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1561                 __BIND_TEXT(db, stmt, idx++, effective_appid);
1562                 __BIND_TEXT(db, stmt, idx++,
1563                                 __get_bool(app->splash_screen_display, false));
1564                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1565                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, false));
1566                 __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1567                 __BIND_TEXT(db, stmt, idx++,
1568                                 __get_bool(app->support_ambient, false));
1569                 __BIND_TEXT(db, stmt, idx++, app->setup_appid);
1570
1571                 ret = sqlite3_step(stmt);
1572                 if (ret != SQLITE_DONE) {
1573                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1574                         sqlite3_finalize(stmt);
1575                         return -1;
1576                 }
1577
1578                 sqlite3_reset(stmt);
1579
1580                 if (__insert_appcontrol_info(db, app)) {
1581                         sqlite3_finalize(stmt);
1582                         return -1;
1583                 }
1584                 if (__insert_category_info(db, app)) {
1585                         sqlite3_finalize(stmt);
1586                         return -1;
1587                 }
1588                 if (__insert_metadata_info(db, app)) {
1589                         sqlite3_finalize(stmt);
1590                         return -1;
1591                 }
1592                 if (__insert_datacontrol_info(db, app)) {
1593                         sqlite3_finalize(stmt);
1594                         return -1;
1595                 }
1596                 if (__insert_splashscreen_info(db, app)) {
1597                         sqlite3_finalize(stmt);
1598                         return -1;
1599                 }
1600                 if (__insert_app_localized_info(db, app)) {
1601                         sqlite3_finalize(stmt);
1602                         return -1;
1603                 }
1604         }
1605
1606         sqlite3_finalize(stmt);
1607
1608         return 0;
1609 }
1610
1611 static int __insert_package_localized_info(sqlite3 *db, manifest_x *mfx)
1612 {
1613         static const char query[] =
1614                 "INSERT INTO package_localized_info (package, package_locale,"
1615                 "  package_label, package_icon, package_description,"
1616                 "  package_license, package_author) "
1617                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
1618         int ret;
1619         sqlite3_stmt *stmt;
1620         int idx;
1621         GList *tmp;
1622         GList *locales;
1623         const char *locale;
1624         char *label;
1625         char *icon;
1626         char *description;
1627         char *license;
1628         char *author;
1629
1630         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1631         if (ret != SQLITE_OK) {
1632                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1633                 return -1;
1634         }
1635
1636         locales = __create_locale_list(mfx->label, mfx->license, mfx->icon,
1637                         mfx->description, mfx->author);
1638         for (tmp = locales; tmp; tmp = tmp->next) {
1639                 locale = (const char *)tmp->data;
1640                 label = NULL;
1641                 icon = NULL;
1642                 description = NULL;
1643                 license = NULL;
1644                 author = NULL;
1645                 __extract_data(locale, mfx->label, mfx->license, mfx->icon,
1646                                 mfx->description, mfx->author,
1647                                 &label, &license, &icon, &description, &author);
1648                 if (!label && !license && !icon && !description && !author)
1649                         continue;
1650
1651                 idx = 1;
1652                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1653                 __BIND_TEXT(db, stmt, idx++, locale);
1654                 __BIND_TEXT(db, stmt, idx++, label);
1655                 __BIND_TEXT(db, stmt, idx++, icon);
1656                 __BIND_TEXT(db, stmt, idx++, description);
1657                 __BIND_TEXT(db, stmt, idx++, license);
1658                 __BIND_TEXT(db, stmt, idx++, author);
1659
1660                 ret = sqlite3_step(stmt);
1661                 if (ret != SQLITE_DONE) {
1662                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1663                         g_list_free(locales);
1664                         sqlite3_finalize(stmt);
1665                         return -1;
1666                 }
1667
1668                 sqlite3_reset(stmt);
1669         }
1670
1671         g_list_free(locales);
1672         sqlite3_finalize(stmt);
1673
1674         return 0;
1675 }
1676
1677 static int __insert_package_info(sqlite3 *db, manifest_x *mfx)
1678 {
1679         static const char query[] =
1680                 "INSERT INTO package_info (package, package_type,"
1681                 "  package_version, package_api_version, package_tep_name,"
1682                 "  package_zip_mount_file, install_location, package_size,"
1683                 "  package_removable, package_preload, package_readonly,"
1684                 "  package_update, package_appsetting, package_nodisplay,"
1685                 "  package_system, author_name, author_email, author_href,"
1686                 "  installed_time, installed_storage, storeclient_id,"
1687                 "  mainapp_id, package_url, root_path, external_path,"
1688                 "  csc_path, package_support_mode, package_support_disable) "
1689                 "VALUES (?, ?,"
1690                 "  ?, ?, ?,"
1691                 "  ?, ?, ?,"
1692                 "  LOWER(?), LOWER(?), LOWER(?),"
1693                 "  LOWER(?), LOWER(?), LOWER(?),"
1694                 "  LOWER(?), ?, ?, ?,"
1695                 "  ?, ?, ?,"
1696                 "  ?, ?, ?, ?,"
1697                 "  ?, ?, LOWER(?))";
1698         int ret;
1699         sqlite3_stmt *stmt;
1700         int idx = 1;
1701         const char *author_name = NULL;
1702         const char *author_email = NULL;
1703         const char *author_href = NULL;
1704
1705         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1706         if (ret != SQLITE_OK) {
1707                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1708                 return -1;
1709         }
1710
1711         if (mfx->author && mfx->author->data) {
1712                 author_name = ((author_x *)mfx->author->data)->text;
1713                 author_email = ((author_x *)mfx->author->data)->email;
1714                 author_href = ((author_x *)mfx->author->data)->href;
1715         }
1716
1717         __BIND_TEXT(db, stmt, idx++, mfx->package);
1718         __BIND_TEXT(db, stmt, idx++, mfx->type);
1719         __BIND_TEXT(db, stmt, idx++, mfx->version);
1720         __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1721         __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1722         __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1723         __BIND_TEXT(db, stmt, idx++, mfx->installlocation);
1724         __BIND_TEXT(db, stmt, idx++, mfx->package_size);
1725         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, true));
1726         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1727         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->readonly, false));
1728         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->update, false));
1729         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->appsetting, false));
1730         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->nodisplay_setting, false));
1731         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1732         __BIND_TEXT(db, stmt, idx++, author_name);
1733         __BIND_TEXT(db, stmt, idx++, author_email);
1734         __BIND_TEXT(db, stmt, idx++, author_href);
1735         __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1736         __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1737         __BIND_TEXT(db, stmt, idx++, mfx->storeclient_id);
1738         __BIND_TEXT(db, stmt, idx++, mfx->mainapp_id);
1739         __BIND_TEXT(db, stmt, idx++, mfx->package_url);
1740         __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1741         __BIND_TEXT(db, stmt, idx++, mfx->external_path);
1742         __BIND_TEXT(db, stmt, idx++, mfx->csc_path);
1743         __BIND_TEXT(db, stmt, idx++, mfx->support_mode);
1744         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->support_disable, false));
1745
1746         ret = sqlite3_step(stmt);
1747         if (ret != SQLITE_DONE) {
1748                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1749                 sqlite3_finalize(stmt);
1750                 return -1;
1751         }
1752
1753         sqlite3_finalize(stmt);
1754
1755         if (__insert_package_localized_info(db, mfx))
1756                 return -1;
1757         if (__insert_application_info(db, mfx))
1758                 return -1;
1759         if (__insert_package_privilege_info(db, mfx))
1760                 return -1;
1761
1762         return 0;
1763 }
1764
1765 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1766 {
1767         int ret;
1768         const char *dbpath;
1769         sqlite3 *db;
1770
1771         if (mfx == NULL) {
1772                 _LOGE("invalid parameter");
1773                 return PM_PARSER_R_EINVAL;
1774         }
1775
1776         dbpath = __get_parser_db_path(uid);
1777
1778         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1779         if (ret != SQLITE_OK) {
1780                 _LOGE("open db failed: %d", ret);
1781                 return PM_PARSER_R_ERROR;
1782         }
1783
1784         __BEGIN_TRANSACTION(db);
1785         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1786         __END_TRANSACTION(db);
1787
1788         sqlite3_close_v2(db);
1789
1790         return PM_PARSER_R_OK;
1791 }
1792
1793 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1794 {
1795         return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid());
1796 }
1797
1798 static int __delete_package_info(sqlite3 *db, const char *pkgid)
1799 {
1800         static const char query[] =
1801                 "DELETE FROM package_info WHERE package=?";
1802         int ret;
1803         sqlite3_stmt *stmt;
1804
1805         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1806         if (ret != SQLITE_OK) {
1807                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1808                 return -1;
1809         }
1810
1811         __BIND_TEXT(db, stmt, 1, pkgid);
1812
1813         ret = sqlite3_step(stmt);
1814         if (ret != SQLITE_DONE) {
1815                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1816                 sqlite3_finalize(stmt);
1817                 return -1;
1818         }
1819
1820         sqlite3_finalize(stmt);
1821
1822         return 0;
1823 }
1824
1825 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx,
1826         uid_t uid)
1827 {
1828         int ret;
1829         const char *dbpath;
1830         sqlite3 *db;
1831
1832         if (mfx == NULL) {
1833                 _LOGE("invalid parameter");
1834                 return PM_PARSER_R_EINVAL;
1835         }
1836
1837         dbpath = __get_parser_db_path(uid);
1838
1839         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1840         if (ret != SQLITE_OK) {
1841                 _LOGE("open db failed: %d", ret);
1842                 return PM_PARSER_R_ERROR;
1843         }
1844
1845         __BEGIN_TRANSACTION(db);
1846         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
1847         __END_TRANSACTION(db);
1848
1849         sqlite3_close_v2(db);
1850
1851         return PM_PARSER_R_OK;
1852 }
1853
1854 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
1855 {
1856         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid());
1857 }
1858
1859 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1860 {
1861         int ret;
1862         const char *dbpath;
1863         sqlite3 *db;
1864
1865         if (mfx == NULL) {
1866                 _LOGE("invalid parameter");
1867                 return PM_PARSER_R_EINVAL;
1868         }
1869
1870         dbpath = __get_parser_db_path(uid);
1871
1872         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1873         if (ret != SQLITE_OK) {
1874                 _LOGE("open db failed: %d", ret);
1875                 return PM_PARSER_R_ERROR;
1876         }
1877
1878         __BEGIN_TRANSACTION(db);
1879         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
1880         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1881         __END_TRANSACTION(db);
1882
1883         sqlite3_close_v2(db);
1884
1885         return PM_PARSER_R_OK;
1886 }
1887
1888 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1889 {
1890         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid());
1891 }
1892
1893 static int __set_global_app_disable_for_uid(sqlite3 *db, const char *appid,
1894                 uid_t uid, bool is_disable)
1895 {
1896         static const char query[] =
1897                 "INSERT OR REPLACE INTO package_app_info_for_uid ("
1898                 "  app_id, uid, is_disabled, is_splash_screen_enabled) "
1899                 "VALUES (?, ?, ?,"
1900                 "  (SELECT app_splash_screen_display FROM package_app_info"
1901                 "   WHERE app_id=?))";
1902         int ret;
1903         sqlite3_stmt *stmt;
1904         int idx = 1;
1905
1906         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1907         if (ret != SQLITE_OK) {
1908                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1909                 return -1;
1910         }
1911
1912         __BIND_TEXT(db, stmt, idx++, appid);
1913         __BIND_INT(db, stmt, idx++, uid);
1914         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
1915         __BIND_TEXT(db, stmt, idx++, appid);
1916
1917         ret = sqlite3_step(stmt);
1918         if (ret != SQLITE_DONE) {
1919                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1920                 sqlite3_finalize(stmt);
1921                 return -1;
1922         }
1923
1924         sqlite3_finalize(stmt);
1925
1926         return 0;
1927 }
1928
1929 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
1930                 const char *appid, uid_t uid, int is_disable)
1931 {
1932         int ret;
1933         const char *dbpath;
1934         sqlite3 *db;
1935
1936         if (appid == NULL) {
1937                 _LOGE("invalid parameter");
1938                 return PM_PARSER_R_EINVAL;
1939         }
1940
1941         dbpath = __get_parser_db_path(uid);
1942
1943         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1944         if (ret != SQLITE_OK) {
1945                 _LOGE("open db failed: %d", ret);
1946                 return PM_PARSER_R_ERROR;
1947         }
1948
1949         __BEGIN_TRANSACTION(db);
1950         __DO_TRANSACTION(db, __set_global_app_disable_for_uid(db, appid,
1951                                 uid, (bool)is_disable));
1952         __END_TRANSACTION(db);
1953
1954         sqlite3_close_v2(db);
1955
1956         return PM_PARSER_R_OK;
1957 }
1958
1959 static int __set_app_disable(sqlite3 *db, const char *appid, uid_t uid,
1960                 bool is_disable)
1961 {
1962         static const char query[] =
1963                 "UPDATE package_app_info SET app_disable=? "
1964                 "WHERE app_id=?";
1965         int ret;
1966         sqlite3_stmt *stmt;
1967         int idx = 1;
1968
1969         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1970         if (ret != SQLITE_OK) {
1971                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1972                 return -1;
1973         }
1974
1975         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
1976         __BIND_TEXT(db, stmt, idx++, appid);
1977
1978         ret = sqlite3_step(stmt);
1979         if (ret != SQLITE_DONE) {
1980                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1981                 sqlite3_finalize(stmt);
1982                 return -1;
1983         }
1984
1985         sqlite3_finalize(stmt);
1986
1987         return 0;
1988 }
1989
1990 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid,
1991                 uid_t uid, int is_disable)
1992 {
1993         int ret;
1994         const char *dbpath;
1995         sqlite3 *db;
1996
1997         if (appid == NULL) {
1998                 _LOGE("invalid parameter");
1999                 return PM_PARSER_R_EINVAL;
2000         }
2001
2002         dbpath = __get_parser_db_path(uid);
2003
2004         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2005         if (ret != SQLITE_OK) {
2006                 _LOGE("open db failed: %d", ret);
2007                 return PM_PARSER_R_ERROR;
2008         }
2009
2010         __BEGIN_TRANSACTION(db);
2011         __DO_TRANSACTION(db, __set_app_disable(db, appid, uid,
2012                                 (bool)is_disable));
2013         __END_TRANSACTION(db);
2014
2015         sqlite3_close_v2(db);
2016
2017         return PM_PARSER_R_OK;
2018 }
2019
2020 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid,
2021                 int is_disable)
2022 {
2023         return pkgmgr_parser_update_app_disable_info_in_usr_db(appid,
2024                         __getuid(), is_disable);
2025 }
2026
2027 static int __set_pkg_disable(sqlite3 *db, const char *pkgid, uid_t uid,
2028                 bool is_disable)
2029 {
2030         static const char query[] =
2031                 "UPDATE package_info SET package_disable=? "
2032                 "WHERE package=?";
2033         int ret;
2034         sqlite3_stmt *stmt;
2035         int idx = 1;
2036
2037         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2038         if (ret != SQLITE_OK) {
2039                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2040                 return -1;
2041         }
2042
2043         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2044         __BIND_TEXT(db, stmt, idx++, pkgid);
2045
2046         ret = sqlite3_step(stmt);
2047         if (ret != SQLITE_DONE) {
2048                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2049                 sqlite3_finalize(stmt);
2050                 return -1;
2051         }
2052
2053         sqlite3_finalize(stmt);
2054
2055         return 0;
2056 }
2057
2058 API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid,
2059                 uid_t uid, int is_disable)
2060 {
2061         int ret;
2062         const char *dbpath;
2063         sqlite3 *db;
2064
2065         if (pkgid == NULL) {
2066                 _LOGE("invalid parameter");
2067                 return PM_PARSER_R_EINVAL;
2068         }
2069
2070         dbpath = __get_parser_db_path(uid);
2071
2072         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2073         if (ret != SQLITE_OK) {
2074                 _LOGE("open db failed: %d", ret);
2075                 return PM_PARSER_R_ERROR;
2076         }
2077
2078         __BEGIN_TRANSACTION(db);
2079         __DO_TRANSACTION(db, __set_pkg_disable(db, pkgid, uid,
2080                                 (bool)is_disable));
2081         __END_TRANSACTION(db);
2082
2083         sqlite3_close_v2(db);
2084
2085         return PM_PARSER_R_OK;
2086 }
2087
2088 API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid,
2089                 int is_disable)
2090 {
2091         return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid,
2092                         __getuid(), is_disable);
2093 }
2094
2095 static int __set_global_app_splash_screen_for_uid(sqlite3 *db,
2096                 const char *appid, uid_t uid, bool is_enabled)
2097 {
2098         static const char query[] =
2099                 "INSERT OR REPLACE INTO package_app_info_for_uid("
2100                 "  appid, uid, is_splash_screen_enabled) "
2101                 "VALUES (?, ?, ?)";
2102         int ret;
2103         sqlite3_stmt *stmt;
2104         int idx = 1;
2105
2106         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2107         if (ret != SQLITE_OK) {
2108                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2109                 return -1;
2110         }
2111
2112         __BIND_TEXT(db, stmt, idx++, appid);
2113         __BIND_INT(db, stmt, idx++, uid);
2114         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2115
2116         ret = sqlite3_step(stmt);
2117         if (ret != SQLITE_DONE) {
2118                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2119                 sqlite3_finalize(stmt);
2120                 return -1;
2121         }
2122
2123         sqlite3_finalize(stmt);
2124
2125         return 0;
2126 }
2127
2128 API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(
2129                 const char *appid, uid_t uid, int flag)
2130 {
2131         int ret;
2132         const char *dbpath;
2133         sqlite3 *db;
2134
2135         if (appid == NULL) {
2136                 _LOGE("invalid parameter");
2137                 return PM_PARSER_R_EINVAL;
2138         }
2139
2140         dbpath = __get_parser_db_path(uid);
2141
2142         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2143         if (ret != SQLITE_OK) {
2144                 _LOGE("open db failed: %d", ret);
2145                 return PM_PARSER_R_ERROR;
2146         }
2147
2148         __BEGIN_TRANSACTION(db);
2149         __DO_TRANSACTION(db, __set_global_app_splash_screen_for_uid(db,
2150                                 appid, uid, (bool)flag));
2151         __END_TRANSACTION(db);
2152
2153         sqlite3_close_v2(db);
2154
2155         return PM_PARSER_R_OK;
2156 }
2157
2158 static int __set_app_splash_screen(sqlite3 *db, const char *appid,
2159                 bool is_enabled)
2160 {
2161         static const char query[] =
2162                 "UPDATE package_app_info SET app_splash_screen_display=? "
2163                 "WHERE app_id=?";
2164         int ret;
2165         sqlite3_stmt *stmt;
2166         int idx = 1;
2167
2168         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2169         if (ret != SQLITE_OK) {
2170                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2171                 return -1;
2172         }
2173
2174         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2175         __BIND_TEXT(db, stmt, idx++, appid);
2176
2177         ret = sqlite3_step(stmt);
2178         if (ret != SQLITE_DONE) {
2179                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2180                 sqlite3_finalize(stmt);
2181                 return -1;
2182         }
2183
2184         sqlite3_finalize(stmt);
2185
2186         return 0;
2187 }
2188
2189 API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2190                 const char *appid, uid_t uid, int flag)
2191 {
2192         int ret;
2193         const char *dbpath;
2194         sqlite3 *db;
2195
2196         if (appid == NULL) {
2197                 _LOGE("invalid parameter");
2198                 return PM_PARSER_R_EINVAL;
2199         }
2200
2201         dbpath = __get_parser_db_path(uid);
2202
2203         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2204         if (ret != SQLITE_OK) {
2205                 _LOGE("open db failed: %d", ret);
2206                 return PM_PARSER_R_ERROR;
2207         }
2208
2209         __BEGIN_TRANSACTION(db);
2210         __DO_TRANSACTION(db, __set_app_splash_screen(db, appid, (bool)flag));
2211         __END_TRANSACTION(db);
2212
2213         sqlite3_close_v2(db);
2214
2215         return PM_PARSER_R_OK;
2216 }
2217
2218 API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(
2219                 const char *appid, int flag)
2220 {
2221         return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2222                         appid, __getuid(), flag);
2223 }
2224
2225 static int __set_app_label(sqlite3 *db, const char *appid, const char *label)
2226 {
2227         static const char query[] =
2228                 "UPDATE package_app_localized_info SET app_label=? "
2229                 "WHERE app_id=? AND app_label IS NOT NULL";
2230         int ret;
2231         sqlite3_stmt *stmt;
2232         int idx = 1;
2233
2234         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2235         if (ret != SQLITE_OK) {
2236                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2237                 return -1;
2238         }
2239
2240         __BIND_TEXT(db, stmt, idx++, label);
2241         __BIND_TEXT(db, stmt, idx++, appid);
2242
2243         ret = sqlite3_step(stmt);
2244         if (ret != SQLITE_DONE) {
2245                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2246                 sqlite3_finalize(stmt);
2247                 return -1;
2248         }
2249
2250         sqlite3_finalize(stmt);
2251
2252         return 0;
2253 }
2254
2255 API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid,
2256                 uid_t uid, const char *label)
2257 {
2258         int ret;
2259         const char *dbpath;
2260         sqlite3 *db;
2261
2262         if (appid == NULL) {
2263                 _LOGE("invalid parameter");
2264                 return PM_PARSER_R_EINVAL;
2265         }
2266
2267         dbpath = __get_parser_db_path(uid);
2268
2269         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2270         if (ret != SQLITE_OK) {
2271                 _LOGE("open db failed: %d", ret);
2272                 return PM_PARSER_R_ERROR;
2273         }
2274
2275         __BEGIN_TRANSACTION(db);
2276         __DO_TRANSACTION(db, __set_app_label(db, appid, label));
2277         __END_TRANSACTION(db);
2278
2279         sqlite3_close_v2(db);
2280
2281         return PM_PARSER_R_OK;
2282 }
2283
2284 API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
2285                 const char *label)
2286 {
2287         return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(),
2288                         label);
2289 }
2290
2291 static int __set_tep_path(sqlite3 *db, const char *pkgid, const char *tep_path)
2292 {
2293         static const char query[] =
2294                 "UPDATE package_info SET package_tep_name=? "
2295                 "WHERE package=?";
2296         int ret;
2297         sqlite3_stmt *stmt;
2298         int idx = 1;
2299
2300         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2301         if (ret != SQLITE_OK) {
2302                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2303                 return -1;
2304         }
2305
2306         __BIND_TEXT(db, stmt, idx++, tep_path);
2307         __BIND_TEXT(db, stmt, idx++, pkgid);
2308
2309         ret = sqlite3_step(stmt);
2310         if (ret != SQLITE_DONE) {
2311                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2312                 sqlite3_finalize(stmt);
2313                 return -1;
2314         }
2315
2316         sqlite3_finalize(stmt);
2317
2318         return 0;
2319 }
2320
2321 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid,
2322                 const char *tep_path, uid_t uid)
2323 {
2324         int ret;
2325         const char *dbpath;
2326         sqlite3 *db;
2327
2328         if (pkgid == NULL) {
2329                 _LOGE("invalid parameter");
2330                 return PM_PARSER_R_EINVAL;
2331         }
2332
2333         dbpath = __get_parser_db_path(uid);
2334
2335         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2336         if (ret != SQLITE_OK) {
2337                 _LOGE("open db failed: %d", ret);
2338                 return PM_PARSER_R_ERROR;
2339         }
2340
2341         __BEGIN_TRANSACTION(db);
2342         __DO_TRANSACTION(db, __set_tep_path(db, pkgid, tep_path));
2343         __END_TRANSACTION(db);
2344
2345         sqlite3_close_v2(db);
2346
2347         return PM_PARSER_R_OK;
2348 }
2349
2350 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid,
2351                 const char *tep_path)
2352 {
2353         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path,
2354                         __getuid());
2355 }
2356
2357 static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type,
2358                 const char **update_type)
2359 {
2360         if (type == PMINFO_UPDATEINFO_NONE)
2361                 *update_type = PMINFO_UPDATEINFO_TYPE_NONE;
2362         else if (type == PMINFO_UPDATEINFO_FORCE)
2363                 *update_type = PMINFO_UPDATEINFO_TYPE_FORCE;
2364         else if (type == PMINFO_UPDATEINFO_OPTIONAL)
2365                 *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL;
2366         else
2367                 return -1;
2368         return 0;
2369 }
2370
2371 static int __register_pkg_update_info(sqlite3 *db, updateinfo_x *info,
2372                 const char *update_type)
2373 {
2374         static const char query[] =
2375                 "UPDATE package_update_info "
2376                 "SET update_version=?, update_type=? "
2377                 "WHERE package=?";
2378         int ret;
2379         sqlite3_stmt *stmt;
2380         int idx = 1;
2381
2382         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2383         if (ret != SQLITE_OK) {
2384                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2385                 return -1;
2386         }
2387
2388         __BIND_TEXT(db, stmt, idx++, info->version);
2389         __BIND_TEXT(db, stmt, idx++, update_type);
2390         __BIND_TEXT(db, stmt, idx++, info->pkgid);
2391
2392         ret = sqlite3_step(stmt);
2393         if (ret != SQLITE_DONE) {
2394                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2395                 sqlite3_finalize(stmt);
2396                 return -1;
2397         }
2398
2399         sqlite3_finalize(stmt);
2400
2401         return 0;
2402 }
2403
2404 API int pkgmgr_parser_register_pkg_update_info_in_usr_db(
2405                 pkgmgrinfo_updateinfo_h handle, uid_t uid)
2406 {
2407         int ret;
2408         updateinfo_x *update_info;
2409         updateinfo_x *prev_update_info;
2410         pkgmgrinfo_updateinfo_h prev_update_handle;
2411         pkgmgrinfo_pkginfo_h pkginfo;
2412         pkgmgrinfo_version_compare_type compare_result;
2413         bool is_global_pkg;
2414         const char *update_type;
2415         const char *dbpath;
2416         sqlite3 *db;
2417
2418         if (handle == NULL) {
2419                 _LOGE("invalid parameter");
2420                 return PM_PARSER_R_EINVAL;
2421         }
2422
2423         update_info = (updateinfo_x *)handle;
2424         if (update_info->pkgid == NULL || update_info->version == NULL)
2425                 return PM_PARSER_R_EINVAL;
2426         if (__convert_update_type(update_info->type, &update_type) != 0)
2427                 return PM_PARSER_R_EINVAL;
2428
2429         ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid,
2430                         &prev_update_handle, uid);
2431         if (ret != PMINFO_R_OK)
2432                 return PM_PARSER_R_ERROR;
2433
2434         prev_update_info = (updateinfo_x *)prev_update_handle;
2435         ret = pkgmgrinfo_compare_package_version(update_info->version,
2436                         prev_update_info->version, &compare_result);
2437         if (ret != PMINFO_R_OK) {
2438                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2439                 return PM_PARSER_R_ERROR;
2440         }
2441
2442         if (compare_result == PMINFO_VERSION_SAME &&
2443                         prev_update_info->type == PMINFO_UPDATEINFO_NONE) {
2444                 _LOGI("Given update info version[%s] of pkgid[%s] "
2445                                 "will be ignored",
2446                                 update_info->version, update_info->pkgid);
2447                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2448                 return PM_PARSER_R_OK;
2449         }
2450         pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2451
2452         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid,
2453                         &pkginfo);
2454         if (ret != PMINFO_R_OK)
2455                 return PM_PARSER_R_ERROR;
2456
2457         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2458         if (ret != PMINFO_R_OK) {
2459                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2460                 return PM_PARSER_R_ERROR;
2461         }
2462         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2463
2464         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2465
2466         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2467         if (ret != SQLITE_OK) {
2468                 _LOGE("open db failed: %d", ret);
2469                 return PM_PARSER_R_ERROR;
2470         }
2471
2472         __BEGIN_TRANSACTION(db);
2473         __DO_TRANSACTION(db, __register_pkg_update_info(db, update_info,
2474                                 update_type));
2475         __END_TRANSACTION(db);
2476
2477         sqlite3_close_v2(db);
2478
2479         return PM_PARSER_R_OK;
2480 }
2481
2482 API int pkgmgr_parser_register_pkg_update_info_in_db(
2483                 pkgmgrinfo_updateinfo_h handle)
2484 {
2485         return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle,
2486                         __getuid());
2487 }
2488
2489 static int __unregister_pkg_update_info(sqlite3 *db, const char *pkgid)
2490 {
2491         static const char query[] =
2492                 "UPDATE package_update_info SET update_type='none' "
2493                 "WHERE package=?";
2494         int ret;
2495         sqlite3_stmt *stmt;
2496         int idx = 1;
2497
2498         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2499         if (ret != SQLITE_OK) {
2500                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2501                 return -1;
2502         }
2503
2504         __BIND_TEXT(db, stmt, idx++, pkgid);
2505
2506         ret = sqlite3_step(stmt);
2507         if (ret != SQLITE_DONE) {
2508                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2509                 sqlite3_finalize(stmt);
2510                 return -1;
2511         }
2512
2513         sqlite3_finalize(stmt);
2514
2515         return 0;
2516 }
2517
2518 API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid,
2519                 uid_t uid)
2520 {
2521         int ret;
2522         const char *dbpath;
2523         sqlite3 *db;
2524         pkgmgrinfo_pkginfo_h pkginfo;
2525         bool is_global_pkg;
2526
2527         if (pkgid == NULL) {
2528                 _LOGE("invalid parameter");
2529                 return PM_PARSER_R_EINVAL;
2530         }
2531
2532         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
2533         if (ret != PMINFO_R_OK)
2534                 return PM_PARSER_R_EINVAL;
2535
2536         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2537         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2538         if (ret != PMINFO_R_OK)
2539                 return PM_PARSER_R_ERROR;
2540
2541         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2542
2543         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2544         if (ret != SQLITE_OK) {
2545                 _LOGE("open db failed: %d", ret);
2546                 return PM_PARSER_R_ERROR;
2547         }
2548
2549         __BEGIN_TRANSACTION(db);
2550         __DO_TRANSACTION(db, __unregister_pkg_update_info(db, pkgid));
2551         __END_TRANSACTION(db);
2552
2553         sqlite3_close_v2(db);
2554
2555         return PM_PARSER_R_OK;
2556 }
2557
2558 API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid)
2559 {
2560         return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid,
2561                         __getuid());
2562 }
2563
2564 static int __unregister_all_pkg_update_info(sqlite3 *db)
2565 {
2566         static const char query[] =
2567                 "UPDATE package_update_info SET update_type='none'";
2568         int ret;
2569         sqlite3_stmt *stmt;
2570
2571         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2572         if (ret != SQLITE_OK) {
2573                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2574                 return -1;
2575         }
2576
2577         ret = sqlite3_step(stmt);
2578         if (ret != SQLITE_DONE) {
2579                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2580                 sqlite3_finalize(stmt);
2581                 return -1;
2582         }
2583
2584         sqlite3_finalize(stmt);
2585
2586         return 0;
2587 }
2588
2589 API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid)
2590 {
2591         int ret;
2592         const char *dbpath;
2593         sqlite3 *db;
2594
2595         dbpath = __get_parser_db_path(uid);
2596
2597         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2598         if (ret != SQLITE_OK) {
2599                 _LOGE("open db failed: %d", ret);
2600                 return PM_PARSER_R_ERROR;
2601         }
2602
2603         __BEGIN_TRANSACTION(db);
2604         __DO_TRANSACTION(db, __unregister_all_pkg_update_info(db));
2605         __END_TRANSACTION(db);
2606
2607         sqlite3_close_v2(db);
2608
2609         return PM_PARSER_R_OK;
2610 }
2611
2612 API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
2613 {
2614         return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
2615                         __getuid());
2616 }