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