Set app_external_path when insert package info
[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_external_path, 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++, mfx->external_path);
1722                 __BIND_TEXT(db, stmt, idx++, app->setup_appid);
1723
1724                 ret = sqlite3_step(stmt);
1725                 if (ret != SQLITE_DONE) {
1726                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1727                         sqlite3_finalize(stmt);
1728                         return -1;
1729                 }
1730
1731                 sqlite3_reset(stmt);
1732
1733                 if (__insert_appcontrol_info(db, app)) {
1734                         sqlite3_finalize(stmt);
1735                         return -1;
1736                 }
1737                 if (__insert_category_info(db, app)) {
1738                         sqlite3_finalize(stmt);
1739                         return -1;
1740                 }
1741                 if (__insert_metadata_info(db, app)) {
1742                         sqlite3_finalize(stmt);
1743                         return -1;
1744                 }
1745                 if (__insert_datacontrol_info(db, app)) {
1746                         sqlite3_finalize(stmt);
1747                         return -1;
1748                 }
1749                 if (__insert_splashscreen_info(db, app)) {
1750                         sqlite3_finalize(stmt);
1751                         return -1;
1752                 }
1753                 if (__insert_app_localized_info(db, app)) {
1754                         sqlite3_finalize(stmt);
1755                         return -1;
1756                 }
1757         }
1758
1759         sqlite3_finalize(stmt);
1760
1761         return 0;
1762 }
1763
1764 static int __insert_package_update_info(sqlite3 *db, manifest_x *mfx)
1765 {
1766         static const char query[] =
1767                 "INSERT INTO package_update_info (package, update_version) "
1768                 "VALUES (?, ?)";
1769         int ret;
1770         int idx;
1771         sqlite3_stmt *stmt;
1772
1773         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1774         if (ret != SQLITE_OK) {
1775                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1776                 return -1;
1777         }
1778
1779         idx = 1;
1780         __BIND_TEXT(db, stmt, idx++, mfx->package);
1781         __BIND_TEXT(db, stmt, idx, mfx->version);
1782         ret = sqlite3_step(stmt);
1783         if (ret != SQLITE_DONE) {
1784                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1785                 sqlite3_finalize(stmt);
1786                 return -1;
1787         }
1788         sqlite3_finalize(stmt);
1789
1790         return 0;
1791 }
1792
1793 static int __insert_package_localized_info(sqlite3 *db, manifest_x *mfx)
1794 {
1795         static const char query[] =
1796                 "INSERT INTO package_localized_info (package, package_locale,"
1797                 "  package_label, package_icon, package_description,"
1798                 "  package_license, package_author) "
1799                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
1800         int ret;
1801         sqlite3_stmt *stmt;
1802         int idx;
1803         GList *tmp;
1804         GList *locales;
1805         const char *locale;
1806         char *label;
1807         char *icon;
1808         char *description;
1809         char *license;
1810         char *author;
1811
1812         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1813         if (ret != SQLITE_OK) {
1814                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1815                 return -1;
1816         }
1817
1818         locales = __create_locale_list(mfx->label, mfx->license, mfx->icon,
1819                         mfx->description, mfx->author);
1820         for (tmp = locales; tmp; tmp = tmp->next) {
1821                 locale = (const char *)tmp->data;
1822                 label = NULL;
1823                 icon = NULL;
1824                 description = NULL;
1825                 license = NULL;
1826                 author = NULL;
1827                 __extract_data(locale, mfx->label, mfx->license, mfx->icon,
1828                                 mfx->description, mfx->author,
1829                                 &label, &license, &icon, &description, &author);
1830                 if (!label && !license && !icon && !description && !author)
1831                         continue;
1832
1833                 idx = 1;
1834                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1835                 __BIND_TEXT(db, stmt, idx++, locale);
1836                 __BIND_TEXT(db, stmt, idx++, label);
1837                 __BIND_TEXT(db, stmt, idx++, icon);
1838                 __BIND_TEXT(db, stmt, idx++, description);
1839                 __BIND_TEXT(db, stmt, idx++, license);
1840                 __BIND_TEXT(db, stmt, idx++, author);
1841
1842                 ret = sqlite3_step(stmt);
1843                 if (ret != SQLITE_DONE) {
1844                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1845                         g_list_free(locales);
1846                         sqlite3_finalize(stmt);
1847                         return -1;
1848                 }
1849
1850                 sqlite3_reset(stmt);
1851         }
1852
1853         g_list_free(locales);
1854         sqlite3_finalize(stmt);
1855
1856         return 0;
1857 }
1858
1859 static int __insert_package_info(sqlite3 *db, manifest_x *mfx)
1860 {
1861         static const char query[] =
1862                 "INSERT INTO package_info (package, package_type,"
1863                 "  package_version, package_api_version, package_tep_name,"
1864                 "  package_zip_mount_file, install_location, package_size,"
1865                 "  package_removable, package_preload, package_readonly,"
1866                 "  package_update, package_appsetting, package_nodisplay,"
1867                 "  package_system, author_name, author_email, author_href,"
1868                 "  installed_time, installed_storage, storeclient_id,"
1869                 "  mainapp_id, package_url, root_path, external_path,"
1870                 "  csc_path, package_support_mode, package_support_disable) "
1871                 "VALUES (?, ?,"
1872                 "  ?, ?, ?,"
1873                 "  ?, ?, ?,"
1874                 "  LOWER(?), LOWER(?), LOWER(?),"
1875                 "  LOWER(?), LOWER(?), LOWER(?),"
1876                 "  LOWER(?), ?, ?, ?,"
1877                 "  ?, ?, ?,"
1878                 "  ?, ?, ?, ?,"
1879                 "  ?, ?, LOWER(?))";
1880         int ret;
1881         sqlite3_stmt *stmt;
1882         int idx = 1;
1883         const char *author_name = NULL;
1884         const char *author_email = NULL;
1885         const char *author_href = NULL;
1886
1887         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1888         if (ret != SQLITE_OK) {
1889                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1890                 return -1;
1891         }
1892
1893         if (mfx->author && mfx->author->data) {
1894                 author_name = ((author_x *)mfx->author->data)->text;
1895                 author_email = ((author_x *)mfx->author->data)->email;
1896                 author_href = ((author_x *)mfx->author->data)->href;
1897         }
1898
1899         __BIND_TEXT(db, stmt, idx++, mfx->package);
1900         __BIND_TEXT(db, stmt, idx++, mfx->type);
1901         __BIND_TEXT(db, stmt, idx++, mfx->version);
1902         __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1903         __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1904         __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1905         __BIND_TEXT(db, stmt, idx++, mfx->installlocation);
1906         __BIND_TEXT(db, stmt, idx++, mfx->package_size);
1907         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, true));
1908         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1909         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->readonly, false));
1910         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->update, false));
1911         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->appsetting, false));
1912         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->nodisplay_setting, false));
1913         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1914         __BIND_TEXT(db, stmt, idx++, author_name);
1915         __BIND_TEXT(db, stmt, idx++, author_email);
1916         __BIND_TEXT(db, stmt, idx++, author_href);
1917         __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1918         __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1919         __BIND_TEXT(db, stmt, idx++, mfx->storeclient_id);
1920         __BIND_TEXT(db, stmt, idx++, mfx->mainapp_id);
1921         __BIND_TEXT(db, stmt, idx++, mfx->package_url);
1922         __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1923         __BIND_TEXT(db, stmt, idx++, mfx->external_path);
1924         __BIND_TEXT(db, stmt, idx++, mfx->csc_path);
1925         __BIND_TEXT(db, stmt, idx++, mfx->support_mode);
1926         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->support_disable, false));
1927
1928         ret = sqlite3_step(stmt);
1929         if (ret != SQLITE_DONE) {
1930                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1931                 sqlite3_finalize(stmt);
1932                 return -1;
1933         }
1934
1935         sqlite3_finalize(stmt);
1936
1937         if (__insert_package_update_info(db, mfx))
1938                 return -1;
1939         if (__insert_package_localized_info(db, mfx))
1940                 return -1;
1941         if (__insert_application_info(db, mfx))
1942                 return -1;
1943         if (__insert_package_privilege_info(db, mfx))
1944                 return -1;
1945         if (__insert_package_appdefined_privilege_info(db, mfx))
1946                 return -1;
1947
1948         return 0;
1949 }
1950
1951 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1952 {
1953         int ret;
1954         const char *dbpath;
1955         sqlite3 *db;
1956
1957         if (mfx == NULL) {
1958                 _LOGE("invalid parameter");
1959                 return PM_PARSER_R_EINVAL;
1960         }
1961
1962         dbpath = __get_parser_db_path(uid);
1963
1964         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1965         if (ret != SQLITE_OK) {
1966                 _LOGE("open db failed: %d", ret);
1967                 return PM_PARSER_R_ERROR;
1968         }
1969
1970         __BEGIN_TRANSACTION(db);
1971         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1972         __END_TRANSACTION(db);
1973
1974         sqlite3_close_v2(db);
1975
1976         return PM_PARSER_R_OK;
1977 }
1978
1979 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1980 {
1981         return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid());
1982 }
1983
1984 static int __delete_package_info(sqlite3 *db, const char *pkgid)
1985 {
1986         static const char query[] =
1987                 "DELETE FROM package_info WHERE package=?";
1988         int ret;
1989         sqlite3_stmt *stmt;
1990
1991         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1992         if (ret != SQLITE_OK) {
1993                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1994                 return -1;
1995         }
1996
1997         __BIND_TEXT(db, stmt, 1, pkgid);
1998
1999         ret = sqlite3_step(stmt);
2000         if (ret != SQLITE_DONE) {
2001                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2002                 sqlite3_finalize(stmt);
2003                 return -1;
2004         }
2005
2006         sqlite3_finalize(stmt);
2007
2008         return 0;
2009 }
2010
2011 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx,
2012         uid_t uid)
2013 {
2014         int ret;
2015         const char *dbpath;
2016         sqlite3 *db;
2017
2018         if (mfx == NULL) {
2019                 _LOGE("invalid parameter");
2020                 return PM_PARSER_R_EINVAL;
2021         }
2022
2023         dbpath = __get_parser_db_path(uid);
2024
2025         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2026         if (ret != SQLITE_OK) {
2027                 _LOGE("open db failed: %d", ret);
2028                 return PM_PARSER_R_ERROR;
2029         }
2030
2031         __BEGIN_TRANSACTION(db);
2032         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
2033         __END_TRANSACTION(db);
2034
2035         sqlite3_close_v2(db);
2036
2037         return PM_PARSER_R_OK;
2038 }
2039
2040 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
2041 {
2042         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid());
2043 }
2044
2045 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2046 {
2047         int ret;
2048         const char *dbpath;
2049         sqlite3 *db;
2050
2051         if (mfx == NULL) {
2052                 _LOGE("invalid parameter");
2053                 return PM_PARSER_R_EINVAL;
2054         }
2055
2056         dbpath = __get_parser_db_path(uid);
2057
2058         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2059         if (ret != SQLITE_OK) {
2060                 _LOGE("open db failed: %d", ret);
2061                 return PM_PARSER_R_ERROR;
2062         }
2063
2064         __BEGIN_TRANSACTION(db);
2065         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
2066         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
2067         __END_TRANSACTION(db);
2068
2069         sqlite3_close_v2(db);
2070
2071         return PM_PARSER_R_OK;
2072 }
2073
2074 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
2075 {
2076         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid());
2077 }
2078
2079 static int __set_global_app_disable_for_uid(sqlite3 *db, const char *appid,
2080                 uid_t uid, bool is_disable)
2081 {
2082         static const char query[] =
2083                 "INSERT OR REPLACE INTO package_app_info_for_uid ("
2084                 "  app_id, uid, is_disabled, is_splash_screen_enabled) "
2085                 "VALUES (?, ?, ?,"
2086                 "  (SELECT app_splash_screen_display FROM package_app_info"
2087                 "   WHERE app_id=?))";
2088         int ret;
2089         sqlite3_stmt *stmt;
2090         int idx = 1;
2091
2092         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2093         if (ret != SQLITE_OK) {
2094                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2095                 return -1;
2096         }
2097
2098         __BIND_TEXT(db, stmt, idx++, appid);
2099         __BIND_INT(db, stmt, idx++, uid);
2100         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2101         __BIND_TEXT(db, stmt, idx++, appid);
2102
2103         ret = sqlite3_step(stmt);
2104         if (ret != SQLITE_DONE) {
2105                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2106                 sqlite3_finalize(stmt);
2107                 return -1;
2108         }
2109
2110         sqlite3_finalize(stmt);
2111
2112         return 0;
2113 }
2114
2115 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
2116                 const char *appid, uid_t uid, int is_disable)
2117 {
2118         int ret;
2119         const char *dbpath;
2120         sqlite3 *db;
2121
2122         if (appid == NULL) {
2123                 _LOGE("invalid parameter");
2124                 return PM_PARSER_R_EINVAL;
2125         }
2126
2127         dbpath = __get_parser_db_path(GLOBAL_USER);
2128
2129         ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE);
2130         if (ret != SQLITE_OK) {
2131                 _LOGE("open db failed: %d", ret);
2132                 return PM_PARSER_R_ERROR;
2133         }
2134
2135         __BEGIN_TRANSACTION(db);
2136         __DO_TRANSACTION(db, __set_global_app_disable_for_uid(db, appid,
2137                                 uid, (bool)is_disable));
2138         __END_TRANSACTION(db);
2139
2140         sqlite3_close_v2(db);
2141
2142         return PM_PARSER_R_OK;
2143 }
2144
2145 static int __set_app_disable(sqlite3 *db, const char *appid, uid_t uid,
2146                 bool is_disable)
2147 {
2148         static const char query[] =
2149                 "UPDATE package_app_info SET app_disable=? "
2150                 "WHERE app_id=?";
2151         int ret;
2152         sqlite3_stmt *stmt;
2153         int idx = 1;
2154
2155         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2156         if (ret != SQLITE_OK) {
2157                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2158                 return -1;
2159         }
2160
2161         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2162         __BIND_TEXT(db, stmt, idx++, appid);
2163
2164         ret = sqlite3_step(stmt);
2165         if (ret != SQLITE_DONE) {
2166                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2167                 sqlite3_finalize(stmt);
2168                 return -1;
2169         }
2170
2171         sqlite3_finalize(stmt);
2172
2173         return 0;
2174 }
2175
2176 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid,
2177                 uid_t uid, int is_disable)
2178 {
2179         int ret;
2180         const char *dbpath;
2181         sqlite3 *db;
2182
2183         if (appid == NULL) {
2184                 _LOGE("invalid parameter");
2185                 return PM_PARSER_R_EINVAL;
2186         }
2187
2188         dbpath = __get_parser_db_path(uid);
2189
2190         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2191         if (ret != SQLITE_OK) {
2192                 _LOGE("open db failed: %d", ret);
2193                 return PM_PARSER_R_ERROR;
2194         }
2195
2196         __BEGIN_TRANSACTION(db);
2197         __DO_TRANSACTION(db, __set_app_disable(db, appid, uid,
2198                                 (bool)is_disable));
2199         __END_TRANSACTION(db);
2200
2201         sqlite3_close_v2(db);
2202
2203         return PM_PARSER_R_OK;
2204 }
2205
2206 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid,
2207                 int is_disable)
2208 {
2209         return pkgmgr_parser_update_app_disable_info_in_usr_db(appid,
2210                         __getuid(), is_disable);
2211 }
2212
2213 static int __set_pkg_disable(sqlite3 *db, const char *pkgid, uid_t uid,
2214                 bool is_disable)
2215 {
2216         static const char query[] =
2217                 "UPDATE package_info SET package_disable=? "
2218                 "WHERE package=?";
2219         int ret;
2220         sqlite3_stmt *stmt;
2221         int idx = 1;
2222
2223         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2224         if (ret != SQLITE_OK) {
2225                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2226                 return -1;
2227         }
2228
2229         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2230         __BIND_TEXT(db, stmt, idx++, pkgid);
2231
2232         ret = sqlite3_step(stmt);
2233         if (ret != SQLITE_DONE) {
2234                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2235                 sqlite3_finalize(stmt);
2236                 return -1;
2237         }
2238
2239         sqlite3_finalize(stmt);
2240
2241         return 0;
2242 }
2243
2244 API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid,
2245                 uid_t uid, int is_disable)
2246 {
2247         int ret;
2248         const char *dbpath;
2249         sqlite3 *db;
2250
2251         if (pkgid == NULL) {
2252                 _LOGE("invalid parameter");
2253                 return PM_PARSER_R_EINVAL;
2254         }
2255
2256         dbpath = __get_parser_db_path(uid);
2257
2258         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2259         if (ret != SQLITE_OK) {
2260                 _LOGE("open db failed: %d", ret);
2261                 return PM_PARSER_R_ERROR;
2262         }
2263
2264         __BEGIN_TRANSACTION(db);
2265         __DO_TRANSACTION(db, __set_pkg_disable(db, pkgid, uid,
2266                                 (bool)is_disable));
2267         __END_TRANSACTION(db);
2268
2269         sqlite3_close_v2(db);
2270
2271         return PM_PARSER_R_OK;
2272 }
2273
2274 API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid,
2275                 int is_disable)
2276 {
2277         return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid,
2278                         __getuid(), is_disable);
2279 }
2280
2281 static int __set_global_app_splash_screen_for_uid(sqlite3 *db,
2282                 const char *appid, uid_t uid, bool is_enabled)
2283 {
2284         static const char query[] =
2285                 "INSERT OR REPLACE INTO package_app_info_for_uid("
2286                 "  appid, uid, is_splash_screen_enabled) "
2287                 "VALUES (?, ?, ?)";
2288         int ret;
2289         sqlite3_stmt *stmt;
2290         int idx = 1;
2291
2292         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2293         if (ret != SQLITE_OK) {
2294                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2295                 return -1;
2296         }
2297
2298         __BIND_TEXT(db, stmt, idx++, appid);
2299         __BIND_INT(db, stmt, idx++, uid);
2300         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2301
2302         ret = sqlite3_step(stmt);
2303         if (ret != SQLITE_DONE) {
2304                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2305                 sqlite3_finalize(stmt);
2306                 return -1;
2307         }
2308
2309         sqlite3_finalize(stmt);
2310
2311         return 0;
2312 }
2313
2314 API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(
2315                 const char *appid, uid_t uid, int flag)
2316 {
2317         int ret;
2318         const char *dbpath;
2319         sqlite3 *db;
2320
2321         if (appid == NULL) {
2322                 _LOGE("invalid parameter");
2323                 return PM_PARSER_R_EINVAL;
2324         }
2325
2326         dbpath = __get_parser_db_path(GLOBAL_USER);
2327
2328         ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE);
2329         if (ret != SQLITE_OK) {
2330                 _LOGE("open db failed: %d", ret);
2331                 return PM_PARSER_R_ERROR;
2332         }
2333
2334         __BEGIN_TRANSACTION(db);
2335         __DO_TRANSACTION(db, __set_global_app_splash_screen_for_uid(db,
2336                                 appid, uid, (bool)flag));
2337         __END_TRANSACTION(db);
2338
2339         sqlite3_close_v2(db);
2340
2341         return PM_PARSER_R_OK;
2342 }
2343
2344 static int __set_app_splash_screen(sqlite3 *db, const char *appid,
2345                 bool is_enabled)
2346 {
2347         static const char query[] =
2348                 "UPDATE package_app_info SET app_splash_screen_display=? "
2349                 "WHERE app_id=?";
2350         int ret;
2351         sqlite3_stmt *stmt;
2352         int idx = 1;
2353
2354         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2355         if (ret != SQLITE_OK) {
2356                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2357                 return -1;
2358         }
2359
2360         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2361         __BIND_TEXT(db, stmt, idx++, appid);
2362
2363         ret = sqlite3_step(stmt);
2364         if (ret != SQLITE_DONE) {
2365                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2366                 sqlite3_finalize(stmt);
2367                 return -1;
2368         }
2369
2370         sqlite3_finalize(stmt);
2371
2372         return 0;
2373 }
2374
2375 API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2376                 const char *appid, uid_t uid, int flag)
2377 {
2378         int ret;
2379         const char *dbpath;
2380         sqlite3 *db;
2381
2382         if (appid == NULL) {
2383                 _LOGE("invalid parameter");
2384                 return PM_PARSER_R_EINVAL;
2385         }
2386
2387         dbpath = __get_parser_db_path(uid);
2388
2389         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2390         if (ret != SQLITE_OK) {
2391                 _LOGE("open db failed: %d", ret);
2392                 return PM_PARSER_R_ERROR;
2393         }
2394
2395         __BEGIN_TRANSACTION(db);
2396         __DO_TRANSACTION(db, __set_app_splash_screen(db, appid, (bool)flag));
2397         __END_TRANSACTION(db);
2398
2399         sqlite3_close_v2(db);
2400
2401         return PM_PARSER_R_OK;
2402 }
2403
2404 API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(
2405                 const char *appid, int flag)
2406 {
2407         return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2408                         appid, __getuid(), flag);
2409 }
2410
2411 static int __set_app_label(sqlite3 *db, const char *appid, const char *label)
2412 {
2413         static const char query[] =
2414                 "UPDATE package_app_localized_info SET app_label=? "
2415                 "WHERE app_id=? AND app_label IS NOT NULL";
2416         int ret;
2417         sqlite3_stmt *stmt;
2418         int idx = 1;
2419
2420         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2421         if (ret != SQLITE_OK) {
2422                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2423                 return -1;
2424         }
2425
2426         __BIND_TEXT(db, stmt, idx++, label);
2427         __BIND_TEXT(db, stmt, idx++, appid);
2428
2429         ret = sqlite3_step(stmt);
2430         if (ret != SQLITE_DONE) {
2431                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2432                 sqlite3_finalize(stmt);
2433                 return -1;
2434         }
2435
2436         sqlite3_finalize(stmt);
2437
2438         return 0;
2439 }
2440
2441 API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid,
2442                 uid_t uid, const char *label)
2443 {
2444         int ret;
2445         const char *dbpath;
2446         sqlite3 *db;
2447
2448         if (appid == NULL) {
2449                 _LOGE("invalid parameter");
2450                 return PM_PARSER_R_EINVAL;
2451         }
2452
2453         dbpath = __get_parser_db_path(uid);
2454
2455         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2456         if (ret != SQLITE_OK) {
2457                 _LOGE("open db failed: %d", ret);
2458                 return PM_PARSER_R_ERROR;
2459         }
2460
2461         __BEGIN_TRANSACTION(db);
2462         __DO_TRANSACTION(db, __set_app_label(db, appid, label));
2463         __END_TRANSACTION(db);
2464
2465         sqlite3_close_v2(db);
2466
2467         return PM_PARSER_R_OK;
2468 }
2469
2470 API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
2471                 const char *label)
2472 {
2473         return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(),
2474                         label);
2475 }
2476
2477 static int __set_app_icon(sqlite3 *db, const char *appid, const char *icon_path)
2478 {
2479         static const char query[] =
2480                 "UPDATE package_app_localized_info SET app_icon=? "
2481                 "WHERE app_id=? AND app_icon IS NOT NULL";
2482         int ret;
2483         sqlite3_stmt *stmt;
2484         int idx = 1;
2485
2486         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2487         if (ret != SQLITE_OK) {
2488                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2489                 return -1;
2490         }
2491
2492         __BIND_TEXT(db, stmt, idx++, icon_path);
2493         __BIND_TEXT(db, stmt, idx++, appid);
2494
2495         ret = sqlite3_step(stmt);
2496         if (ret != SQLITE_DONE) {
2497                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2498                 sqlite3_finalize(stmt);
2499                 return -1;
2500         }
2501
2502         sqlite3_finalize(stmt);
2503
2504         return 0;
2505 }
2506
2507 API int pkgmgr_parser_update_app_icon_info_in_usr_db(const char *appid,
2508                 uid_t uid, const char *icon_path)
2509 {
2510         int ret;
2511         const char *dbpath;
2512         sqlite3 *db;
2513
2514         if (appid == NULL) {
2515                 _LOGE("invalid parameter");
2516                 return PM_PARSER_R_EINVAL;
2517         }
2518
2519         dbpath = __get_parser_db_path(uid);
2520
2521         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2522         if (ret != SQLITE_OK) {
2523                 _LOGE("open db failed: %d", ret);
2524                 return PM_PARSER_R_ERROR;
2525         }
2526
2527         __BEGIN_TRANSACTION(db);
2528         __DO_TRANSACTION(db, __set_app_icon(db, appid, icon_path));
2529         __END_TRANSACTION(db);
2530
2531         sqlite3_close_v2(db);
2532
2533         return PM_PARSER_R_OK;
2534 }
2535
2536 API int pkgmgr_parser_update_app_icon_info_in_db(const char *appid,
2537                 const char *icon_path)
2538 {
2539         return pkgmgr_parser_update_app_icon_info_in_usr_db(appid, __getuid(),
2540                         icon_path);
2541 }
2542
2543 static int __set_tep_path(sqlite3 *db, const char *pkgid, const char *tep_path)
2544 {
2545         static const char query[] =
2546                 "UPDATE package_info SET package_tep_name=? "
2547                 "WHERE package=?";
2548         int ret;
2549         sqlite3_stmt *stmt;
2550         int idx = 1;
2551
2552         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2553         if (ret != SQLITE_OK) {
2554                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2555                 return -1;
2556         }
2557
2558         __BIND_TEXT(db, stmt, idx++, tep_path);
2559         __BIND_TEXT(db, stmt, idx++, pkgid);
2560
2561         ret = sqlite3_step(stmt);
2562         if (ret != SQLITE_DONE) {
2563                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2564                 sqlite3_finalize(stmt);
2565                 return -1;
2566         }
2567
2568         sqlite3_finalize(stmt);
2569
2570         return 0;
2571 }
2572
2573 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid,
2574                 const char *tep_path, uid_t uid)
2575 {
2576         int ret;
2577         const char *dbpath;
2578         sqlite3 *db;
2579
2580         if (pkgid == NULL) {
2581                 _LOGE("invalid parameter");
2582                 return PM_PARSER_R_EINVAL;
2583         }
2584
2585         dbpath = __get_parser_db_path(uid);
2586
2587         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2588         if (ret != SQLITE_OK) {
2589                 _LOGE("open db failed: %d", ret);
2590                 return PM_PARSER_R_ERROR;
2591         }
2592
2593         __BEGIN_TRANSACTION(db);
2594         __DO_TRANSACTION(db, __set_tep_path(db, pkgid, tep_path));
2595         __END_TRANSACTION(db);
2596
2597         sqlite3_close_v2(db);
2598
2599         return PM_PARSER_R_OK;
2600 }
2601
2602 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid,
2603                 const char *tep_path)
2604 {
2605         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path,
2606                         __getuid());
2607 }
2608
2609 static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type,
2610                 const char **update_type)
2611 {
2612         if (type == PMINFO_UPDATEINFO_NONE)
2613                 *update_type = PMINFO_UPDATEINFO_TYPE_NONE;
2614         else if (type == PMINFO_UPDATEINFO_FORCE)
2615                 *update_type = PMINFO_UPDATEINFO_TYPE_FORCE;
2616         else if (type == PMINFO_UPDATEINFO_OPTIONAL)
2617                 *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL;
2618         else
2619                 return -1;
2620         return 0;
2621 }
2622
2623 static int __register_pkg_update_info(sqlite3 *db, updateinfo_x *info,
2624                 const char *update_type)
2625 {
2626         static const char query[] =
2627                 "UPDATE package_update_info "
2628                 "SET update_version=?, update_type=? "
2629                 "WHERE package=?";
2630         int ret;
2631         sqlite3_stmt *stmt;
2632         int idx = 1;
2633
2634         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2635         if (ret != SQLITE_OK) {
2636                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2637                 return -1;
2638         }
2639
2640         __BIND_TEXT(db, stmt, idx++, info->version);
2641         __BIND_TEXT(db, stmt, idx++, update_type);
2642         __BIND_TEXT(db, stmt, idx++, info->pkgid);
2643
2644         ret = sqlite3_step(stmt);
2645         if (ret != SQLITE_DONE) {
2646                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2647                 sqlite3_finalize(stmt);
2648                 return -1;
2649         }
2650
2651         sqlite3_finalize(stmt);
2652
2653         return 0;
2654 }
2655
2656 API int pkgmgr_parser_register_pkg_update_info_in_usr_db(
2657                 pkgmgrinfo_updateinfo_h handle, uid_t uid)
2658 {
2659         int ret;
2660         updateinfo_x *update_info;
2661         updateinfo_x *prev_update_info;
2662         pkgmgrinfo_updateinfo_h prev_update_handle;
2663         pkgmgrinfo_pkginfo_h pkginfo;
2664         pkgmgrinfo_version_compare_type compare_result;
2665         bool is_global_pkg;
2666         const char *update_type;
2667         const char *dbpath;
2668         sqlite3 *db;
2669
2670         if (handle == NULL) {
2671                 _LOGE("invalid parameter");
2672                 return PM_PARSER_R_EINVAL;
2673         }
2674
2675         update_info = (updateinfo_x *)handle;
2676         if (update_info->pkgid == NULL || update_info->version == NULL)
2677                 return PM_PARSER_R_EINVAL;
2678         if (__convert_update_type(update_info->type, &update_type) != 0)
2679                 return PM_PARSER_R_EINVAL;
2680
2681         ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid,
2682                         &prev_update_handle, uid);
2683         if (ret != PMINFO_R_OK)
2684                 return PM_PARSER_R_ERROR;
2685
2686         prev_update_info = (updateinfo_x *)prev_update_handle;
2687         ret = pkgmgrinfo_compare_package_version(update_info->version,
2688                         prev_update_info->version, &compare_result);
2689         if (ret != PMINFO_R_OK) {
2690                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2691                 return PM_PARSER_R_ERROR;
2692         }
2693
2694         if (compare_result == PMINFO_VERSION_SAME &&
2695                         prev_update_info->type == PMINFO_UPDATEINFO_NONE) {
2696                 _LOGI("Given update info version[%s] of pkgid[%s] "
2697                                 "will be ignored",
2698                                 update_info->version, update_info->pkgid);
2699                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2700                 return PM_PARSER_R_OK;
2701         }
2702         pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2703
2704         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid,
2705                         &pkginfo);
2706         if (ret != PMINFO_R_OK)
2707                 return PM_PARSER_R_ERROR;
2708
2709         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2710         if (ret != PMINFO_R_OK) {
2711                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2712                 return PM_PARSER_R_ERROR;
2713         }
2714         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2715
2716         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2717
2718         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2719         if (ret != SQLITE_OK) {
2720                 _LOGE("open db failed: %d", ret);
2721                 return PM_PARSER_R_ERROR;
2722         }
2723
2724         __BEGIN_TRANSACTION(db);
2725         __DO_TRANSACTION(db, __register_pkg_update_info(db, update_info,
2726                                 update_type));
2727         __END_TRANSACTION(db);
2728
2729         sqlite3_close_v2(db);
2730
2731         return PM_PARSER_R_OK;
2732 }
2733
2734 API int pkgmgr_parser_register_pkg_update_info_in_db(
2735                 pkgmgrinfo_updateinfo_h handle)
2736 {
2737         return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle,
2738                         __getuid());
2739 }
2740
2741 static int __unregister_pkg_update_info(sqlite3 *db, const char *pkgid)
2742 {
2743         static const char query[] =
2744                 "UPDATE package_update_info SET update_type='none' "
2745                 "WHERE package=?";
2746         int ret;
2747         sqlite3_stmt *stmt;
2748         int idx = 1;
2749
2750         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2751         if (ret != SQLITE_OK) {
2752                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2753                 return -1;
2754         }
2755
2756         __BIND_TEXT(db, stmt, idx++, pkgid);
2757
2758         ret = sqlite3_step(stmt);
2759         if (ret != SQLITE_DONE) {
2760                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2761                 sqlite3_finalize(stmt);
2762                 return -1;
2763         }
2764
2765         sqlite3_finalize(stmt);
2766
2767         return 0;
2768 }
2769
2770 API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid,
2771                 uid_t uid)
2772 {
2773         int ret;
2774         const char *dbpath;
2775         sqlite3 *db;
2776         pkgmgrinfo_pkginfo_h pkginfo;
2777         bool is_global_pkg;
2778
2779         if (pkgid == NULL) {
2780                 _LOGE("invalid parameter");
2781                 return PM_PARSER_R_EINVAL;
2782         }
2783
2784         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
2785         if (ret != PMINFO_R_OK)
2786                 return PM_PARSER_R_EINVAL;
2787
2788         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2789         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2790         if (ret != PMINFO_R_OK)
2791                 return PM_PARSER_R_ERROR;
2792
2793         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2794
2795         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2796         if (ret != SQLITE_OK) {
2797                 _LOGE("open db failed: %d", ret);
2798                 return PM_PARSER_R_ERROR;
2799         }
2800
2801         __BEGIN_TRANSACTION(db);
2802         __DO_TRANSACTION(db, __unregister_pkg_update_info(db, pkgid));
2803         __END_TRANSACTION(db);
2804
2805         sqlite3_close_v2(db);
2806
2807         return PM_PARSER_R_OK;
2808 }
2809
2810 API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid)
2811 {
2812         return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid,
2813                         __getuid());
2814 }
2815
2816 static int __unregister_all_pkg_update_info(sqlite3 *db)
2817 {
2818         static const char query[] =
2819                 "UPDATE package_update_info SET update_type='none'";
2820         int ret;
2821         sqlite3_stmt *stmt;
2822
2823         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2824         if (ret != SQLITE_OK) {
2825                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2826                 return -1;
2827         }
2828
2829         ret = sqlite3_step(stmt);
2830         if (ret != SQLITE_DONE) {
2831                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2832                 sqlite3_finalize(stmt);
2833                 return -1;
2834         }
2835
2836         sqlite3_finalize(stmt);
2837
2838         return 0;
2839 }
2840
2841 API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid)
2842 {
2843         int ret;
2844         const char *dbpath;
2845         sqlite3 *db;
2846
2847         dbpath = __get_parser_db_path(uid);
2848
2849         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2850         if (ret != SQLITE_OK) {
2851                 _LOGE("open db failed: %d", ret);
2852                 return PM_PARSER_R_ERROR;
2853         }
2854
2855         __BEGIN_TRANSACTION(db);
2856         __DO_TRANSACTION(db, __unregister_all_pkg_update_info(db));
2857         __END_TRANSACTION(db);
2858
2859         sqlite3_close_v2(db);
2860
2861         return PM_PARSER_R_OK;
2862 }
2863
2864 API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
2865 {
2866         return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
2867                         __getuid());
2868 }