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