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